Repository navigation
Fix *Cluster table functions sending the replicas a CTE name without its definition - #122931
Conversation
`IStorageCluster::read` forwarded `query_info.query` to the replicas and re-analyzed it for the header. That AST is the initiator's local rendering of the query tree (`queryNodeToSelectQuery` with `set_subquery_cte_name`), so a subquery that reads a CTE is written as the bare CTE name, and the analyzer's `grouping` specializations and duplicate projection aliases are kept. A replica cannot resolve the CTE name and throws `UNKNOWN_TABLE`, which the forced `skip_unavailable_shards` turns into a skipped replica. So `WITH t AS (...) SELECT ... FROM fileCluster(...) WHERE n IN (SELECT id FROM t)` returned no row, an `INSERT ... SELECT` of it inserted nothing, and a table named like the CTE in the replica's database was read instead. `ROLLUP`, `CUBE` and `GROUPING SETS` with `grouping()` failed with `UNKNOWN_IDENTIFIER` (`__grouping_set`), and `SELECT *, n + 10 AS n` with `MULTIPLE_EXPRESSIONS_FOR_ALIAS`, already in the header re-analysis. Render the query the way the other remote fan-outs do (`Distributed`, `SelectStreamFactory`, parallel replicas): clone the query tree, remove the grouping function specializations, and convert it with `queryNodeToDistributedSelectQuery`, which prints CTE bodies and deduplicates projection aliases. The header is computed from the same AST, since that is what the replicas analyze. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 1 finding).
Severity: ❌ blocker / Session id: cron:clickhouse-review-self-slot-58:20260929-172200 |
|
Workflow [PR], commit [f841544] Summary: ✅
AI ReviewSummary
Missing context / blind spots
Final VerdictStatus: ✅ Approve. No code changes requested. No new inline findings to post. |
| /// The replicas re-analyze this query, so it must not name CTEs or `grouping` specializations of the initiator's query tree. | ||
| auto query_tree_to_send = query_info.query_tree->clone(); | ||
| removeGroupingFunctionSpecializations(query_tree_to_send); | ||
| ASTPtr query_to_send = queryNodeToDistributedSelectQuery(query_tree_to_send); |
There was a problem hiding this comment.
The forwarded query is still not fully self-contained for CTEs whose body reads through a database-sensitive table function. queryNodeToDistributedSelectQuery now inlines the CTE body, but the normalization that follows still calls AddDefaultDatabaseVisitor with only_replace_in_join = true, so non-JOIN table expressions inside that inlined subquery are never canonicalized. TableFunctionNode::toASTImpl only qualifies parameterized views, so a case like WITH t AS (SELECT * FROM merge('^foo')) ... IN (SELECT ... FROM t) will still be rebound on each replica against that replica's current database instead of the initiator's, which can produce wrong rows or UNKNOWN_TABLE. This needs a full table-expression normalization pass on the forwarded AST after CTE inlining (for example visitTableExpressions), not just the join side.
Build profile diff (arm_release)Commit See the job log for details. |
|
@groeneai check the bot comments |
|
@PedroTadim I checked both; neither needs a change in this PR. Measured on this head and on today's master (371b23a):
|
|
query_info.query_tree->clone() dereferences a null pointer when enable_analyzer = 0. The old interpreter (InterpreterSelectQuery) never sets SelectQueryInfo::query_tree, so it keeps its nullptr default. That path still reaches IStorageCluster::read: on master, SELECT sum(n), count() FROM fileCluster('test_cluster_one_shard_two_replicas', 'f.tsv', 'TSV', 'n UInt64') SETTINGS enable_analyzer = 0 returns 6 4. With this change, the same query would bring the server down. I found this by reading the code and running the query on master, not by building the PR. CI doesn't catch it because no stateless job runs with the old analyzer any more. Could the old rendering stay as the fallback when there is no query tree, the same way StorageDistributed checks if (!query_info.query_tree)? For example: A line with SETTINGS enable_analyzer = 0 in the test would cover it. A CTE query on the old analyzer still returns no row, as on master; it could go in the "Not covered" list. |
|
@PedroTadim I measured it: on the CI build of this head ( On master So a If you still want the fallback, I'll add it. |
…orward-self-contained-query Master marks the header analysis in `IStorageCluster::read` with `positionalArgumentsAlreadyResolved`; apply it to the `query_to_send` printed from the query tree, which is also resolved. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #122791
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed cluster table functions (
s3Cluster,urlCluster,fileCluster,azureBlobStorageCluster,hdfsCluster, the data lake*Clusterfunctions, andurl/s3reads distributed byparallel_replicas_for_cluster_engines) returning no rows or wrong rows, andINSERT ... SELECTfrom them inserting nothing, when a subquery reads a CTE, e.g.WITH t AS (...) SELECT ... WHERE x IN (SELECT ... FROM t): the replicas received the CTE name without its definition.GROUP BY ROLLUP/CUBE/GROUPING SETSwithgrouping()andSELECT *, x + 1 AS xover these functions no longer fail withUNKNOWN_IDENTIFIERandMULTIPLE_EXPRESSIONS_FOR_ALIAS.Description
On 26.8.2.7 and master, a cluster table function query whose
INsubquery reads a CTE returns no row, with no error:IStorageCluster::readsent the replicasquery_info.query, the initiator's own rendering of the query tree. It writes a CTE subquery as the bare CTE name, and keeps the analyzer'sgroupingspecializations and duplicate projection aliases. A replica cannot resolve the name and throwsUNKNOWN_TABLE, and the forcedskip_unavailable_shardsthen drops every replica. So the query returns nothing,INSERT ... SELECTinserts nothing, and a table named like the CTE in the replica's database is read instead.The query is now rendered the way
Distributedand parallel replicas render it for a remote server (clone,removeGroupingFunctionSpecializations,queryNodeToDistributedSelectQuery), and the header is computed from that same text.The new test fails on master and on 26.8.2.7 and passes here.
Not covered:
INSERT ... SELECTwith parallel replicas (same symptom, fails loudly), aMATERIALIZEDCTE used twice (read from a temporary table), replicas still dropped silently onUNKNOWN_TABLE, and a cluster function inside a subquery of a cluster read (#46646).Workflow [PR]
Sync PR [sync-upstream/pr/122931]
Version info
26.11.1.217-master(included in26.11and later)