Repository navigation
Conversation
A window function that references no column of a distributed table expression made the shard's mergeable-stage header empty, and a Block carries its row count only inside its columns, so the shards' rows were silently dropped. With the default prefer_localhost_replica the answer is a plausible but short row count rather than nothing at all, which is worse. The initiator now adds a String literal to the query it sends the shards when the header it just derived for them has no column. That header is derived from the very query being amended, so the two stay in step; and because the shards receive an ordinary constant, no version of the server has to understand anything new. An initiator that does not amend the query still agrees with its shards, which is why an upgraded initiator fixes the query for shards of any version while an old initiator behaves exactly as it does today. Only the root amends: a nested Distributed forwards the text it was given, so one text, and therefore one header, holds across every rank. A rank reached at distributed_depth >= 1 is consequently not covered, and is fixed by upgrading the root that composes the query. A literal rather than an identifier so it cannot collide with a column name, as appendUnusedGroupByColumn already does for GROUP BY; the query is cloned first because the caller keeps using its own AST afterwards. The analyzer path is untouched: the amendment sits inside the branch taken when allow_experimental_analyzer is off. PR ClickHouse#113469 covers that half.
The cell that aliases a window function to the internal name asserted only row values, so it passed unchanged whether or not the user's alias survived. Assert the name with DESCRIBE, which reads __row_count_marker of type UInt64: the internal element is an unaliased String literal, so its column name is the quoted form and cannot shadow an identically spelled identifier. The cell's comment claimed the two names were the same and that the alias had to win. State what was measured instead: the names differ, so there is nothing to win.
The previous commit asserted the user's alias survives by running DESCRIBE over the query. DESCRIBE cannot observe the amendment at all: it resolves columns through getSampleBlock, which analyses only, and the branch that analyses adds a ReadNothingStep while executeFetchColumns is called in the other one. Since storage->read has a single call site, inside executeFetchColumns, StorageDistributed::read never runs, so the cell read the same bytes whether the amendment was present or deleted entirely. Select the rows instead and print the column name with FORMAT TSVWithNames, which writes the executed pipeline's own header. The cell now asserts both the name and the rows, and it fails when the rows are lost. The two DESCRIBE cells above are blind for the same reason but are kept: they are correct assertions about the names analysis derives, which is a different question from what execution returns.
A window function defined in a WITH clause takes its result column name from its alias, so a user can spell that name as the display name of the constant this change adds. The window step appends its result columns to the header it is given, which is then the header the constant is in, and Block::insert rejects two columns of one name and different types as ambiguous. Such a query works on master and this branch turned it into Code: 352, which is the one direction the change is supposed to rule out. Skip the amendment when a window function of the query is already named that way. The names come from syntax_analyzer_result->window_function_asts, which is what ExpressionAnalyzer reads to set each window function's internal column name, so they cannot disagree with what the window step will insert; and the check is the last conjunct of the existing test, so a query that does not already reach the amendment computes nothing new. A missing analyzer result is treated as a collision for the same reason. The consequence is deliberate and is not a fix: such a query keeps the rows it loses on master today. Losing rows for one exotic spelling leaves a bug unfixed, whereas failing the query outright breaks something that works, so the guard takes the smaller harm. Renaming the constant is not an alternative, because back-quoted identifiers process escape sequences and so any display name can be written as an alias. Pin it with a cell that runs the colliding query. Its expected output is the pristine build's, byte for byte, because that is what the guard promises; a build with only this conjunct removed fails that one cell with Code: 352 and its output differs from this branch's parent only in the port the message prints.
…tion Two steps append to the header the added constant is in, and the previous commit covered only one of them. The window step appends each window function's result column, from syntax_analyzer_result->window_function_asts. The step after it, built by appendExpressionsAfterWindowFunctions (ExpressionAnalyzer.cpp:1589), appends each expression that wraps a window function, from syntax_analyzer_result->expressions_with_window_function. The two vectors are disjoint for a wrapping expression: TreeRewriter.cpp:1501 fills the first from getWindowFunctions, whose nodes satisfy isWindowFunction(), while :1502 fills the second from getExpressionsWithWindowFunctions, whose collector returns early on a window function and pushes only a function whose subtree holds one (GetAggregatesVisitor.cpp:73-79). So a WITH alias on such an expression reaches the header without ever appearing in the first vector, and a query legal on master still became Code: 352 AMBIGUOUS_COLUMN_NAME. Check both vectors against the same name. The helper is renamed to isMarkerNameTaken because the second vector holds expressions that are not window functions. Everything else is unchanged: the check remains the last conjunct of the existing test, so a query that does not already reach the amendment computes nothing new, and a missing analyzer result is still treated as a collision. The wrapping expression has to contribute no column of its own for the amendment to be reached at all: with a second operand, as in count(*) OVER () + 1, that operand is a required output of the same step, the shard header is not empty and nothing is added. The test therefore pins abs(count(*) OVER ()), whose shard header is empty, and a mutant removing only the new loop reddens exactly that one cell.
Internal second-model review (7 rounds) - one known residual, disclosed belowI reviewed this change cold against the tree, and an independent second-opinion pass re-reviewed it ❌ Known residual, shipped unfixed - a nested alias spelled as the internal constantThe change adds an internal constant to the shard query when that query's header would otherwise It does not cover a wrapper nested inside another wrapper: SET enable_analyzer = 0;
WITH abs(count(*) OVER ()) AS `'__row_count_marker'`
SELECT toString(`'__row_count_marker'`) FROM dist;The outer Scope, measured rather than assumed: this needs a back-quoted I found this in the last review round available to me. I am not going to push a fix I cannot
|
Pre-PR validation gate (click to expand)
Additional verification: 100 runs of the new test pass, 50 randomized and 50 with randomization off, counted from per-test verdict lines. An eight-mutant matrix, each mutant built into its own binary with a distinct build id and run against the whole test on its own server: deleting the amendment reddens exactly the 19 differing blocks and nothing else; keeping the amendment but not re-deriving the header reproduces the cross-version failure this change exists to avoid, within a single version, as |
|
cc @devcrafter @antonio2368, could you review this? With enable_analyzer = 0 a window function that references no column of a distributed table prunes the shard's mergeable-stage header to zero columns, and a block with no column cannot carry a row count, so the rows are silently lost; the initiator now adds a constant to the shard query when the header it just derived is empty and re-derives the header from that same amended query. One known residual is disclosed in the review comment above. |
|
@groeneai the old analyzer is going to be removed soon, so dont care about it |
|
@PedroTadim understood, and I am not going to argue the direction. Two things worth weighing before I close it:
If you still want it dropped, say so and I will close it. The one residual I disclosed in the description is old-analyzer-only too, so closing takes both. |
|
Workflow [PR], commit [ae422db] Summary: ❌
AI ReviewSummaryThis PR fixes the old-analyzer Final Verdict✅ No new findings. LLVM Coverage ReportMeasured on commit ae422db.
Changed lines: Changed C/C++ lines covered: 23/24 (95.83%) · Uncovered code |
… output The flaky check ran the new test 50 times and it failed 7 times across amd_asan_ubsan and amd_debug, always with the same diff: the six row_number values were all present exactly once, but printed 4,5,6,1,2,3 instead of 1..6. The numbering was right; only the order in which the two shards' blocks arrived had moved. row_number is the only witness in the file whose rows are not all equal, so it is the only one whose printed output an arrival-order permutation can change. Every other unordered witness returns one constant value per row, where a permutation is not observable. Checked by extracting all 90 statements, excluding those that already order or describe, running each against the full fixture and flagging any with more than one row and more than one distinct value: exactly one was flagged. Reproduced locally on the same binary at 12-way concurrency, 1 run in 300; it does not reproduce serially, which is why a 50x flaky check catches it and a single run does not. The reference is unchanged, since it already read 1..6 ascending. The witness still witnesses the bug: on pristine master the ordered form returns no rows at all, exactly like the unordered one. EXPLAIN gains a Sorting step above the same Window and ReadFromRemote subtree, so the order is now imposed by the plan.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 18 s compile time in total, 7 of them have a recent master baseline. |
CI finish ledger - 39d2c84Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20260809-050000 |
CI finish ledger - ae422dbEvery failure below has an owner: a fixing PR, or a full-effort fix task whose fixing-PR link
Attribution for the third row, since the only test that arm selected is this pull request's own new
That is the mechanism #112393 addresses: at The same test passed at this same tree on Session id: cron:our-pr-ci-monitor:20260831-063000 |
|
@alexey-milovidov you decide if you want to continue on this |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed lost rows for a window function that references no column of a distributed table, such as
SELECT count(*) OVER () FROM dist, whenenable_analyzer = 0. The query silently returned too few rows, or none at all. The fix lives on the initiator, so an upgraded initiator fixes the query for shards of any version.Description
With
enable_analyzer = 0,SELECT count(*) OVER () FROM distreturns nothing where it must return one row per row of the table, and at the defaultprefer_localhost_replica = 1it returns the local shard's share only: a plausible answer that is silently short. No error, no log.A
Blockcarries its row count only inside its columns, so a header with no column cannot express one. The mergeable-stage header of a windowed, non-aggregating query is the result columns of thebefore_windowstep (InterpreterSelectQuery.cpp:1362), whose required outputs are the window functions' arguments and theirPARTITION BYandORDER BYcolumns.count(*) OVER ()has none, so that step is pruned to nothing and the header cannot count.The initiator now appends a
Stringliteral to the query it sends the shards when the header it has just derived for them has no column, and re-derives that header from the amended query, so the two stay in step. The shards receive an ordinary constant no version has to understand. Only the root amends, so one text and therefore one header holds across every rank; a rank atdistributed_depth >= 1needs an upgraded root. #113469 covers the analyzer half.Carriers fixed, each measured failing before and correct after: a plain
Distributedtable,cluster/clusterAllReplicas/remoteoverview(...),MergeoverDistributed,DistributedoverDistributed, and those underORDER BY,DISTINCT,UNION ALL, a subquery or a filter.Validation: splitting both arms' output positionally on the new test's 46 labels, 19 blocks differ between a pristine
masterbuild and this one and 27 are byte-identical; no control and no section heading differs. 100 runs pass. Mutants pin the text change, the re-derivation and the emptiness test. Mixed-version runs show a new initiator against old shards returning correct rows.Workflow [PR]
Sync PR [sync-upstream/pr/113955]