Repository navigation
Do not rewrite the sharding-key IN set in the SELECT projection - #117252
Conversation
`optimize_skip_unused_shards_rewrite_in` prunes the elements of an `IN` set over the sharding key to the ones routed to each shard. Applied to an `IN` in the projection, the value of the expression stays correct - a row on a shard can only equal an element routed to that shard - but the result column's name changes with the set, so the initiator could not bind the column the shard returned and the query threw `NOT_FOUND_COLUMN_IN_BLOCK` (or `THERE_IS_NO_COLUMN` under the analyzer, with diverging `__set_*` hashes) instead of returning rows. Both arms of the visitor now skip the projection and keep rewriting everywhere else. Closes: #116908
|
Workflow [PR], commit [17d9766] Summary: ✅
AI ReviewSummaryThe PR confines Final VerdictStatus: ✅ Approve LLVM Coverage ReportMeasured on commit 17d9766.
Changed lines: Changed C/C++ lines covered: 32/37 (86.49%) · Uncovered code |
|
The same per-shard set-name mismatch survives in every other position that is shipped by name, with both analyzers, at the same settings (
Also worth noting: the rewrite fires on every matching |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units8 translation units recompiled, 23 s compile time in total, 8 of them have a recent master baseline. |
…g clauses Skipping only the `SELECT` projection was not enough. The header a shard returns for a distributed intermediate stage is not limited to the final `SELECT` outputs: it also carries the aggregation keys and the `before_order_by` results, and `ReadFromRemote` matches the shard result to it by column name. So `GROUP BY key IN (...)`, `ORDER BY key IN (...)` and `LIMIT 1 BY key IN (...)` could still rename a required remote column per shard and fail with the same `NOT_FOUND_COLUMN_IN_BLOCK` / `THERE_IS_NO_COLUMN` class of exception. Both rewriters now descend only into `WHERE` and `PREWHERE` (and into the join tree, so a subquery in `FROM` keeps being pruned by its own filters). An aliased `IN` is skipped entirely: the alias is shared with the other clauses in the analyzer and expanded into a copy of the expression in the old analyzer, where rewriting only the filter left `MULTIPLE_EXPRESSIONS_FOR_ALIAS` behind. The test now covers every remote-header carrier under both analyzers.
…rds-rewrite-in-projection
Confining the rewrite to `WHERE` / `PREWHERE` is still not enough on the old-analyzer path, because the header the initiator expects can contain a column that only the filter computes. Verified locally: `SELECT dummy FROM dist WHERE dummy IN (0, 2) ORDER BY dummy LIMIT 1 BY dummy IN (0, 2)` kept throwing `Not found column in(dummy, (0, 2)) in block`, since `before_order_by` on the initiator carries the column that `WHERE` produced while the shard now names it `in(dummy, tuple(0))`. The AST rewriter therefore first collects the names of the `IN` expressions that are reachable without going through a filter, and skips those. The analyzer resolves the remote columns by node rather than by name and handles this shape already, so its rewrite stays as wide as it can be. All the shapes in the test - projection, `GROUP BY`, `ORDER BY`, `LIMIT BY` and the two aliased ones - were checked against a local two-shard cluster under both analyzers, and the filter-only shape still prunes.
|
🕵 @zlareb1 thank you - every shape you listed reproduced, and they are all covered now. Both rewriters descend only into Your two alias cases and
The analyzer binds the remote columns by node rather than by name and handles that last shape already, so its rewrite stays as wide as it can be. That is why the two blocks of the test's reference differ in row count: with the same On your last remark - the rewrite firing even when no shard is actually skipped - that is unchanged here and is a separate (pre-existing) matter: the rewrite is entered whenever The test now covers the projection, |
|
🕵 @groeneai, investigate the failure: AST fuzzer (amd_debug, targeted) and provide a fix in a separate PR. If the fix is already in progress, link it here.
It is unrelated to this pull request: the query uses neither |
|
Acknowledged. No fix is in progress for this path, so I am investigating it and will follow up here with the separate pull request (or with my findings, if it turns out not to be actionable). On the "already in progress" question: the open pull requests on this same message all cover different paths. #107603 and #112219 are analyzer-side (a table expression shared between the join tree and an |
|
Fixed in #117817: the check now registers the expression's sets in the planner context it is about to resolve them from, which is what the other consumers of that visitor already do. One line, plus a stateless test covering the three clauses the check is evaluated for ( On the recurring signature, the 14 rows CIDB has for it in the last 35 days are three separate things. 8 are the |
…rds-rewrite-in-projection
…pressions of the join tree Since 26.9 `enable_analyzer = 0` is refused by the settings constraints, so the AST arm of `optimizeShardingKeyRewriteIn` is unreachable. Drop the changes to it (the per-`SELECT` exclusion set of the `IN` names used outside the filters and the alias skip) and keep the old-analyzer rewriter as it is on master; the test no longer has an `enable_analyzer = 0` block either, which also removes the failure of `01930_optimize_skip_unused_shards_rewrite_in` in the old-analyzer CI job. In the query-tree arm, address the review: within the join tree only the table expressions are visited now. The `ON` section of a `JOIN`, the expressions of an `ARRAY JOIN` and the arguments of a table function belong to a different source, which is not partitioned by the sharding key of this table even when its column shares the name, so pruning `b.dummy IN (0, 2)` in `... JOIN b ON a.dummy = b.key AND b.dummy IN (0, 2)` changed the join result rather than the coverage of the optimization. The test covers both review shapes: a subquery that spells the same `IN` in its projection does not disable the rewrite in the outer `WHERE`, and the `IN` in a `JOIN ON` keeps its set. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=117252&sha=c9049df6653e294129d4285dbfb6c3c7d3093e35&name_0=PR&name_1=Stateless%20tests%20%28amd_llvm_coverage%2C%20old%20analyzer%2C%20s3%20storage%2C%20DBReplicated%2C%20parallel%2C%203%2F3%29 PR: #117252 Co-Authored-By: Claude Fable 5.1 <[email protected]>
…rds-rewrite-in-projection
… table `optimize_skip_unused_shards_rewrite_in` matched the sharding column by name only, so in a joined query a `WHERE` filter over another source (the other side of a `JOIN`, a subquery over another table) whose column shares the name of the sharding column was pruned per shard as well, dropping rows from the join result. Match the column source against the distributed table expression, which is recognized in the query for the shard by its (unique) alias. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Closes: #116908
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix
THERE_IS_NO_COLUMNfor aDistributedquery that has an
INover the sharding key outside itsWHERE- in theSELECTprojection, inGROUP BY, inORDER BYor inLIMIT BY. The default-enabledoptimize_skip_unused_shards_rewrite_inrewrote thatINper shard, which changed the resultcolumn's name, so the initiator could not bind the column the shard returned.
Description
The rewrite prunes the elements of an
INset over the sharding key to the ones routed to eachshard. That is sound as a filter, and even the value of such an expression stays correct - a row
that lives on a shard can only equal an element routed to that shard. What breaks is the column
identity:
in(key, (1, 4))becomesin(key, tuple(4))on one shard andin(key, tuple(1))on theother, and the header a shard returns for a distributed intermediate stage is matched by name on the
initiator (
ReadFromRemote).That header is not limited to the final
SELECToutputs - it also carries the aggregation keys andthe
before_order_byresults - so the rewrite is now confined to the filtering clauses: the visitordescends into
WHEREandPREWHEREonly, plus the join tree so that a subquery inFROMkeepsbeing pruned by its own filters. Within the join tree only the table expressions are visited: the
ONsection of aJOIN, the expressions of anARRAY JOINand the arguments of a table functionbelong to a different source, which is not partitioned by the sharding key of this table even when
its column shares the name, so pruning
b.key IN (1, 4)in... JOIN b ON a.key = b.k AND b.key IN (1, 4)would change the join result. An aliased
INis skipped entirely, since the aliased node is sharedwith the other clauses that reference the alias.
Only the query-tree rewriter is changed: since 26.9
enable_analyzer = 0is refused by thesettings constraints, so the AST rewriter of the old analyzer is unreachable and stays as it is.
The shard-pruning behaviour the existing
01756_/01930_optimize_skip_unused_shards_rewrite_intests pin is unchanged - none of their queries has an
INover the sharding key outsideWHERE.New test
05071_skip_unused_shards_rewrite_in_projectioncovers the projection,GROUP BY,ORDER BY,LIMIT BYand the two aliased shapes, asserts that the filter-only shape still prunes,that a subquery spelling the same
INin its projection does not disable the rewrite of the outerWHERE, and that theINin aJOIN ONkeeps its set. Note that both shards oftest_cluster_two_shardsread the samesystem.one, which does not respect the sharding key, sothe pruning legitimately changes the row count there.
Workflow [PR]
Sync PR [sync-upstream/pr/117252]
Version info
26.10.1.817-master(included in26.10and later)