Repository navigation
Conversation
…function read An `additional_table_filters` entry keyed by the alias of a `*Cluster` table function (`urlCluster`, `s3Cluster`, `fileCluster`, ..., and `url`/`s3` converted automatically by `parallel_replicas_for_cluster_engines`) was silently ignored: the query returned unfiltered rows. The initiator matches the entry by the original alias and builds the filter, but it reads the cluster storage at `WithMergeableState`, where the planner adds the filter steps only for `FetchColumns`, so the filter is left to the replicas. They receive the query rendered from the analyzed tree, in which the table expression is renamed `__table1`, together with the unchanged setting (in the settings packet and in the text's own `SETTINGS` clause), and resolve it against that text: the alias matches nothing. A key naming the table function by its full name (`_table_function.urlCluster`) worked only because the replica resolves that name itself, and with both kinds of entries the replica applied the full-name one instead of the first match. `IStorageCluster::read` now ANDs the filter the initiator resolved into the `WHERE` of the forwarded query when the read is shipped above `FetchColumns` and reads a table function, so the replica evaluates it before any partial aggregation, like a user-written `WHERE`. It also removes the entries keyed by the full name of the forwarded table function from the forwarded settings (the packet and the text's top-level `SETTINGS`), so a replica cannot apply a second entry to the same read. Entries naming other tables of the forwarded query are forwarded unchanged, and a read at `FetchColumns`, where the initiator applies the filter itself, and data-lake catalog table reads are unchanged. Closes ClickHouse#122544 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…e function read The filter the initiator resolved now reaches the replica as the first `additional_table_filters` entry, keyed by the full name of the forwarded table function, so the replica applies it as its own filter step below the query's `WHERE`, as for a plain read. Folding it into the forwarded `WHERE` let the query's own condition see the removed rows (`throwIf`) and changed the full-name key, which already worked. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Keep only the invariants in the IStorageCluster::read comment: first-match resolution of additional_table_filters on the replicas, the generated table alias in the forwarded query, and that the initiator applies the filter itself at FetchColumns. 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 (3 findings over two rounds) plus my own review (nits only).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20260927-215900 |
|
Workflow [PR], commit [28460ff] Summary: ✅
AI ReviewSummaryThis PR forwards the initiator-matched Findings❌ Blockers
Tests
Missing context / blind spots
Final Verdict❌ Block. Preserve expression-alias and function-alias scope during filter qualification/substitution, and add the focused regressions above. Both findings already have inline threads; no duplicate review comments were posted. The fixed quoted-subcolumn thread was resolved. |
… a cluster table function read
A replica resolves an unqualified table name in the forwarded filter against its own
current database, which is the default database of the user, not the current database of
the initiator's session. After `USE other_db`, a filter such as
`{'t': 'n IN (SELECT id FROM probe)'}` failed on every replica with `UNKNOWN_TABLE`,
which `skip_unavailable_shards` (always on for cluster table functions) turned into an
empty result. A plain `url` read resolves the filter on the initiator and returns the
filtered rows.
The forwarded filter now carries the tables qualified with the initiator's current
database (table expressions, the right argument of `IN`, `dictGet`/`joinGet`), like the
tables of the forwarded query itself. Common table expression names stay unqualified.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes12 object files changed (+92.01 KiB total), 0 added.
719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units19 translation units recompiled, 169 s compile time in total, 19 of them have a recent master baseline. Median compile-time ratio to the baselines is ×1.08 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. |
|
📊 Cloud Performance Report ✅ AI verdict: This PR only changes IStorageCluster::read, and the new block runs only when a *Cluster table function is combined with additional_table_filters. Neither ClickBench nor TPC-H runs that path. The one flagged row, clickbench Q34 at +5.4%, was already held at not_sure because it falls inside master's normal variance band. CPU time for that query is flat and the PR's measurements were noisier than the baseline, so the suppression matches the diff and no query shows a performance change from this PR. clickbenchFlagged queries (1 of 43)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_official🟢 No significant changes Debug info
|
…le function name A replica names the storage of a table function after `ITableFunction::getName()` of the function `TableFunctionFactory` resolves (`Context::executeTableFunction`), so the entry the initiator forwards for a cluster table function read is now keyed by that name instead of by the spelling in the forwarded query. Every `*Cluster` table function is currently registered case-sensitively and without aliases, so the key is unchanged today; this keeps it matching if a case-insensitive name or an alias is added. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ion-additional-table-filters Resolve the conflict in src/Storages/IStorageCluster.cpp includes: keep both sets. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Master no longer converts a url or s3 read to its cluster variant when additional_table_filters is set (Context::canReplaceClusterEngineWithClusterVariant, since ClickHouse#122306), so that line read through a plain url and passed without the fix. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The filter forwarded to the replicas of a cluster table function was qualified with `visitTableExpressions` directly on the expression, which collects aliases only inside a select query. So in `n IN ([2, 3] AS allowed) AND n IN allowed` the second `allowed` became `<db>.allowed`, and the replica failed with UNKNOWN_IDENTIFIER. This also broke the full-name key, which worked before because the replica parsed the original text. `currentDatabase()` in the filter, and the body of a SQL function used in it, were interpreted on the replica, whose current database is its own (the `database` setting is not forwarded), unlike for a plain `url` read or for the query's own `WHERE`, where it is the initiator's. The SQL functions of the filter are now expanded on the initiator, and `AddDefaultDatabaseVisitor::visitStandaloneExpression` qualifies the expression as a select list, so its aliases are collected, and replaces `currentDatabase()` (and its aliases, now including `current_schema`) with the database, keeping the alias of the replaced call. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…er filter The initiator expanded the SQL functions of the filter forwarded to the replicas of a cluster table function with `UserDefinedSQLFunctionVisitor`, which substitutes the arguments by name, so a lambda parameter of the body captured them: with `threshold -> arrayExists(n -> n > threshold, [3])`, the filter `f(n)` became `arrayExists(n -> n > n, [3])`. This also broke the full-name key, which worked before. The replicas expand the SQL functions themselves again. An identifier on the right of `IN` was qualified with the database even when it names a column or a lambda parameter, which the analyzer resolves before a table, so `n IN arr` and `arrayExists(x -> n IN x, [[2, 3]])` failed with UNKNOWN_IDENTIFIER with either key. The columns of the table are now listed next to the filter, like its aliases, and a lambda parameter is not qualified inside its lambda (this also holds for the metadata load path, which shares the pass). A lambda parameter does not hide a table of the same name outside it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`visitStandaloneExpression` qualified a plain name on the right of `IN` inside a subquery of the filter as a table, but there it may be a column of the subquery's tables, which the analyzer resolves before a table. `n IN (SELECT id FROM ids WHERE id IN allowed)`, with `allowed` an array column of `ids`, became `... IN <db>.allowed` and failed with UNKNOWN_IDENTIFIER for the alias key and for the full-name key `_table_function.urlCluster`, which returns 5 on master. The columns of a nested select query are not known to the visitor, so the right argument of `IN` is left as written there, as master forwards it. Table expressions of the subquery are still qualified. Co-Authored-By: Claude Opus 5.5 <[email protected]>
CI finish ledger - 9def424Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
CIDB has no other Session id: cron:our-pr-ci-monitor:20261101-213002 |
| expression_list.children.push_back(std::move(column)); | ||
| } | ||
| standalone_expression_select = select.get(); | ||
| visitTableExpressions(*select); |
There was a problem hiding this comment.
[P1] Keep lambda-local aliases out of the enclosing filter scope
The synthetic select makes collectAliases descend into lambda bodies and collect their expression aliases into the enclosing expression_aliases. For n IN ids AND arrayExists(x -> (x AS ids) > 0, [1]), the alias ids belongs only to the lambda, but it now prevents the outer IN ids table reference from being qualified. The new lambda handling saves and restores parameter names in with_expression_aliases; it does not isolate these expression aliases.
Reproduced with session database review_other, review_other.ids containing 2,3, and the worker's default.ids containing only 3: over the test's 0..3 source, a plain url read returns 5, whereas urlCluster returns 3 with either filter key. Removing the lambda's AS ids restores 5, so a local alias silently changes which table an unrelated part of the filter reads.
Please collect and restore expression aliases at lambda scope boundaries, preserving aliases inside their own lambda without exposing them to the enclosing expression. Cover this predicate after switching databases.
…uster filter
`CURRENT_DATABASE`, `DATABASE`, `SCHEMA`, `CURRENT_SCHEMA` (and the other
spellings of `currentDatabase`) without parentheses stay identifiers in the
AST until the analyzer resolves them as niladic functions, so the filter
forwarded to the replicas was evaluated in the replica's database: after
`USE other_db`, `{'t': 'n > 1 AND CURRENT_DATABASE = ''other_db''}` returned
0 instead of 5, with either key.
The standalone-expression path now replaces such an identifier with the
database too, but only where the analyzer would resolve it as the function:
not when it is a column of the table, or an alias or a lambda parameter of
its scope (a lambda has its own aliases), and not inside a subquery of the
filter, whose columns are not known there. `x AS x` is not taken for an
alias, as in the analyzer. A call `currentDatabase()` is also left as
written when the filter defines a lambda with the alias of that name, e.g.
in a `WITH` clause of a subquery: the analyzer calls the lambda. The query's
own WHERE is a separate path (it is rendered from the initiator's query
tree) and is not changed here.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
| NameSet lambda_aliases; | ||
| collectLambdaAliases(*expression_list.children.at(0), lambda_aliases); | ||
| substituteCurrentDatabase(expression_list.children.at(0), expression_list, lambda_aliases); |
There was a problem hiding this comment.
[P1] Keep lambda function aliases scoped during database substitution
collectLambdaAliases descends into every subquery and lambda, then substituteCurrentDatabase uses that one name set for the entire predicate. Consequently, a lambda named currentDatabase in one subquery suppresses substitution of unrelated built-in calls outside that subquery.
For example, after USE review_other, add this predicate to the test's 0..3 source:
n > 1
AND currentDatabase() = 'review_other'
AND (WITH (() -> 'shadow') AS currentDatabase SELECT currentDatabase()) = 'shadow'The outer call must resolve to the session database and the subquery call must invoke its local lambda, giving sum(n) = 5. Here both calls remain in the forwarded AST; the worker evaluates the outer built-in in default, so the predicate rejects every row and returns 0. This affects both the alias key and the previously working full-name key.
Please track lambda bindings per query/lambda scope while substituting calls, so a local declaration protects only calls that actually bind to it. Cover the combination of an outer built-in call and a shadowing lambda in a nested subquery with both keys.
The filter forwarded to the replicas of a cluster table function has its table names qualified with the initiator's database. The right side of `IN` is taken for a table unless it is a column, but only the top-level physical columns were known. So ``n IN `tup.arr` ``, `'x' NOT IN _tags` and `tuple(n) IN nest` (for the `Nested` columns `nest.*`) had the column qualified as a table, and the replicas failed with UNKNOWN_IDENTIFIER. That broke the full-name key too, which works on master. The virtual columns are now known as well. A name also counts as a column when the analyzer would resolve it as one: a subcolumn (some split into a column and a subcolumn its type has, static or dynamic, e.g. `Map`, `JSON` paths) or the prefix of `Nested` `Array` columns. A table whose name merely starts with a column name and a dot is still qualified. The same check keeps a `Nested` prefix named `database` from being replaced with the database name. A table name with an alias on the right of `IN` is left as written: once `ids` was qualified, `n IN (ids AS allowed) AND n IN allowed` no longer resolved `allowed`, while the full-name key works on master. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@nickitat master's #130531 (the revert of #114923) removed the table-name qualification pass in
I would take B: it is the smaller fix for the reported bug, and the database case is not specific to alias keys. |
Closes: #122544
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix
additional_table_filtersfor reads through*Clustertable functions (urlCluster,s3Cluster,fileClusterand the others): an entry keyed by the alias of the table function was silently ignored, so the query returned unfiltered rows. Closes #122544.Description
On 26.8.10.6-lts and 26.9.1.1629-stable,
SELECT sum(n) FROM urlCluster('test_cluster_one_shard_three_replicas_localhost', '<url returning 0..3>', TSV, 'n UInt64') AS t SETTINGS additional_table_filters = {'t': 'n > 1'}returns the unfiltered6instead of5.Root cause. The initiator matches the entry by the alias
t, but reads the cluster storage atWithMergeableState, leaving the filter to the replicas. They receive the query rendered from the analyzed tree, where the table expression is__table1, so the alias entry matches nothing. A full-name key (_table_function.urlCluster) worked only because the replica resolves that name itself.Change. One site,
IStorageCluster::read, used by every cluster storage. AboveFetchColumns, the initiator forwards its resolved filter as the firstadditional_table_filtersentry, keyed by the full name the replica resolves for the forwarded table function (_table_function.urlCluster), and removes the setting from the text's top-levelSETTINGS, which would override it. The replica applies it below the query'sWHERE, as for a plain read. A replica has its own current database, so the filter's table names are qualified andcurrentDatabase(), also written without parentheses, is replaced with the session's database; its aliases, lambda parameters and columns, including subcolumns, virtual columns andNestedcolumns, stay as written, as does the right side ofINin a subquery.Not covered (pre-existing, as for the full-name key): an alias-keyed entry for a table read in an
INsubquery, which the replicas plan themselves; data-lake catalog tables; a table on the right ofINin a subquery or with an alias, read from the replica's database; a temporary table in the filter, never sent to the replicas; the current database in a SQL function body, or written without parentheses in a subquery of the filter.Validation. The new test fails on the base build and passes with the fix, 50 randomized + 50 plain runs.
Measured matrix, base -> fix
Filter
n > 1over the numbers 0..3 (correct sum: 5).urlCluster,fileCluster,s3Cluster, alias key: 6 -> 5; alias + full-name keys (n > 2): 3 -> 5; full-name key: 5 -> 5urlClusterin aJOIN, left or right, everyjoin_algorithm, with and without the table swap: 6 -> 5enable_parallel_replicas0/1 xserialize_query_plan0/1, and the setting given at session level: 6 -> 5toString(n)filter: base ignored it (6), nowILLEGAL_TYPE_OF_COLUMN_FOR_FILTER, as for a plainurlreadINsubquery keeps applying (fix 2, base 3)INsubquery,IN <table>,dictGet,joinGet) afterUSE other_db: 6 -> 5USE other_db,currentDatabase()/current_schema()in the filter: 6 -> 5; full-name key: 0 -> 5CURRENT_DATABASE/DATABASE/SCHEMA/CURRENT_SCHEMAwithout parentheses, unless the name is a column, an alias or a lambda parameter: 6 -> 5; full-name key: 0 -> 5n IN arr(an array column),arrayExists(x -> n IN x, [[2, 3]]): 6 -> 5; full-name key: 5 -> 5n IN (SELECT id FROM ids WHERE id IN allowed): 6 -> 5; full-name key: 5 -> 5threshold -> arrayExists(n -> n > threshold, [3])applied ton(correct sum: 3): 6 -> 3; full-name key: 3 -> 3n IN ([2, 3] AS allowed) AND n IN allowed: 6 -> 5; full-name key: 5 -> 5Nestedcolumn on the right ofIN(n IN `tup.arr`,'x' NOT IN _tags,tuple(n) IN nest): 6 -> 5; full-name key: 5 -> 5WHERE throwIf(n = 0) = 0, with default settings and with short-circuit evaluation off and filters unmerged: base error -> 5; full-name key: 5 -> 5Workflow [PR]
Sync PR [sync-upstream/pr/122579]