Repository navigation
Conversation
`currentDatabase()` written as a call is constant-folded by `resolveFunction`, so a remote server receives a literal. The same function written without parentheses (`CURRENT_DATABASE`, `DATABASE`, `SCHEMA`, ...) is resolved by the niladic fallback of `QueryAnalyzer::tryResolveIdentifier` and by the alias fallback of `tryResolveIdentifierFromAliases`, which returned an unfolded function node. It was sent to remote servers as `currentDatabase()` and evaluated in each server's own database, so `remote`, `Distributed` and `*Cluster` reads returned wrong results (`WHERE SCHEMA = currentDatabase()` matched nothing), and with several shards `GROUP BY CURRENT_DATABASE` failed with `NOT_FOUND_COLUMN_IN_BLOCK` because the shards named the folded key column differently from the initiator. Fold the node at both places with `foldConstantCast`, which already folds a resolved function with constant arguments exactly like `resolveFunction` (suitability gate, type check, 1 MiB guard, determinism propagation, function kept as the source expression). The result is the same node the call with parentheses produces. Like the call, it is not folded inside `ASOF JOIN ... ON`, using the check of `resolveFunction`, moved to `IdentifierResolveScope::isResolvingAsofJoinExpression`. The doc comment of `foldConstantCast` now says it is not limited to `_CAST`. 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; 3 rounds; 2 findings, both fixed; the last round found nothing).
Severity: ❌ blocker / Session id: cron:clickhouse-review-self-slot-58:20261101-210500 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-self-slot-57:20261101-205500 |
|
cc @novikd @vdimir, could you review this and add the |
|
Workflow [PR], commit [6b403e2] Summary: ✅
AI ReviewSummaryReviewed #128517 at Final Verdict✅ Approve. |
Both tests used the parenthesis-less `database` as an expression the analyzer did not constant-fold while ActionsDAG did. 04545 needs a GROUP BY key that only ActionsDAG folds to reach the `wrap_with_nullable` result-type mismatch of ClickHouse#110551, and 04906 needs a PREWHERE that reads `ver` until ActionsDAG folds it, so that pruning drops `ver`. Now that the niladic form folds like `database()`, neither test reached its code path any more (Fast test: the 04906 pruning control arm printed 1, and 04545 lost the NULL row of its positional key, which was a non-constant key only because of the missing fold). Use `arrayExists(x -> x > 0, [1])` instead: the analyzer does not fold a function with a lambda argument, ActionsDAG does. The references are unchanged. They match on the base commit and on this branch. With the original fixes reverted (the ActionsDAG reconcile of ClickHouse#110551 disabled, the `output_header->has` filter of the 04906 fix removed), the new versions abort with the original LOGICAL_ERRORs, while the bare-`database` versions pass on that binary. 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 Compile time of recompiled translation units24 translation units recompiled, 180 s compile time in total, 24 of them have a recent master baseline. |
The niladic fallbacks of `tryResolveIdentifier` and
`tryResolveIdentifierFromAliases` built a separate function for each
spelling, and since the previous commit fold it right away. A scalar
subquery executed during analysis between two spellings therefore gave
them different values:
SELECT NOW AS a, (SELECT sleep(2)) AS pause, CURRENT_TIMESTAMP AS b, a = b
returned 0 (1 on master, where the unfolded nodes share the planner name
`now()`). The same held for `LOCALTIME` / `localtime` and for an alias of
such a function referenced from a subquery.
Both fallbacks now call `QueryAnalyzer::resolveNiladicFunction`, which
takes the built function from `functions_cache` under the key that
`resolveFunction` uses for the call `<canonical name>()`. So `NOW`,
`CURRENT_TIMESTAMP` and `now()` share one value per query, as repeated
calls `now()` already did.
The test gets one case per fallback, with a scalar `sleep(1)` between the
spellings so that the analysis always crosses a second boundary.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed wrong results when
CURRENT_DATABASE,DATABASE,SCHEMAor another function written without parentheses is used in a query that reads throughremote, aDistributedtable or a*Clustertable function, andNOT_FOUND_COLUMN_IN_BLOCK/THERE_IS_NO_COLUMNerrors when such a function is aGROUP BYkey over several shards. The function was evaluated on every remote server, which forCURRENT_DATABASEreturned that server's database, instead of once on the initiator like the same function written with parentheses.Description
Related: #95949 (added functions without parentheses).
On 26.3.60.2 and 26.10.2.103, with a non-default session database,
SELECT count() FROM remote('127.0.0.2', numbers(4)) WHERE SCHEMA = currentDatabase()returns 0 instead of 4, andSELECT CURRENT_DATABASE FROM remote(...)returns the remote server'sdefault. With two shards, aGROUP BYkey over such a function fails withNOT_FOUND_COLUMN_IN_BLOCK(THERE_IS_NO_COLUMNwhen one shard is local). The same queries withcurrentDatabase()are correct.resolveFunctionconstant-foldscurrentDatabase(), so remote servers receive a literal. A function written without parentheses is built by the fallback inQueryAnalyzer::tryResolveIdentifierand intryResolveIdentifierFromAliases, and was left unfolded: it was sent ascurrentDatabase()and evaluated in each remote server's own database, and the shards named the folded column differently from the initiator.Both places now take the built function from the cache that
resolveFunctionuses for the call<canonical name>()and fold it with the existingfoldConstantCast, so the node is the constant the call with parentheses produces, andNOW,CURRENT_TIMESTAMPandnow()keep one value in a query. InsideASOF JOIN ... ON, whereresolveFunctiondoes not fold the call either, the node stays unfolded.The new test fails on master and passes with the fix (100 runs, with and without random settings); reverting either place alone reddens its own case. 04545 and 04906 relied on
databasestaying unfolded to reach their bugs; they now usearrayExists(x -> x > 0, [1]), which only ActionsDAG folds, with unchanged references. Other stateless tests that use such functions without parentheses give the same results with and without the change.Workflow [PR]
Sync PR [sync-upstream/pr/128517]