Repository navigation
Conversation
…ads it For `x IN (v1, ..., vN)` with a constant right side, `CollectSetsVisitor` converted the constant back to an AST (`ConstantNode::toAST`) for every query, to give the `FutureSetFromTuple` its source AST. For a `Tuple` constant that conversion materializes the whole value as a `Field` twice, infers a type per element, hashes the column, deep-copies the literal and builds the `Tuple(...)` type name: O(N) allocations per list, comparable to building the set itself. The only readers of that AST are the filters `ReadFromRemote` sends to remote servers (`tryBuildAdditionalFilterAST`, under `allow_push_predicate_ast_for_distributed_subqueries`) and distributed index analysis, so a local query built it and dropped it. `FutureSetFromTuple` now takes an optional clone of the constant node and converts it on the first `getSourceAST` call under `callOnce`, then releases the node, the same idiom it already uses for `getKeyColumns` and `getContentHash`. The clone is O(1) (a `ConstantNode` has no children and shares its column, type and source expression), fixes the AST to the node's state at `CollectSets` time and does not share the original node's unsynchronized `cached_ast`. Remote filters get the same AST as before, built once. Every other caller keeps passing an AST (or none) through the defaulted trailing parameter. Measured (RelWithDebInfo, 201 paired runs per query in a Latin-square crossover, server-side time): 10000 UInt64 keys in a primary key window -11% (28.4 -> 25.2 ms), 10000 String keys -12%, with use_statistics = 0 -14% to -16%, `set_index.xml` queries ClickHouse#4 and ClickHouse#8 -15% and -18%; queries without a literal list unchanged; the secondary query sent by `remote()` is byte-identical. With the `createColumnConst` change of ClickHouse#132144 applied the gains are the same (-12%, -12%, -15%). Cost: the set now keeps the constant's column until the query ends instead of until planning ends. With one 4 KiB `PODArray` chunk per element column on current master that is +39 MiB of peak memory for a 10000-value `NOT IN` before a `GROUP BY`; after ClickHouse#132144 it is +3.4 to +7.5 MiB (300 to 800 bytes per value), so this should be merged after ClickHouse#132144. The new test checks that a literal `IN` list still reaches the remote server's query; it fails when `getSourceAST` returns no AST. Closes ClickHouse#132161 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop the CollectSets call-site comment; the FutureSetFromTuple constructor comment already states that only remote filters read the source AST. 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; 2 findings) plus my own review (1 residual).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261117-044601 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-41:20261117-015900 |
|
Workflow [PR], commit [0c924b0] Summary: ✅
AI ReviewSummaryThe PR defers conversion of literal Performance & SafetyPreserve the documented merge order: #132227 remains open and should land first. Retaining the constant column otherwise adds approximately 39 MiB per 10000 literal values until the query ends; the PR reports substantially lower overhead with that prerequisite. This is an acknowledged merge prerequisite, with no additional code change requested here. Final VerdictStatus: ✅ Approve |
The `#if CLICKHOUSE_CLOUD` SharedSet dispatch sat in the constant-input branch, which is the `else` of `if (storage_set)`, so `storage_set` is always null there and `storage_set->getName()` dereferenced it for every literal `IN` list in a Cloud build. Move it, unchanged, into the branch that handles a Set table, as `ActionsMatcher::makeSet` does; the literal branch now only calls `addFromTuple`. Public builds compile the block out (`CLICKHOUSE_CLOUD` is 0), so their preprocessed translation unit is identical. The block was compiled with `-fsyntax-only` in its new place against stub `StorageSharedSet` types. Reported by the automated review on ClickHouse#132247 (r4596124036). 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 units218 translation units recompiled, 2651 s compile time in total, 218 of them have a recent master baseline. |
Closes: #132161
Related: #132227
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
A query with a large literal
INlist no longer converts the list back to an AST during planning unless a filter is sent to remote servers: lookups with 10000 literal values are 11-18% faster. Closes #132161.Description
Not a
Bug Fix: no result changes, the work was only wasted.CollectSetsVisitorcalledConstantNode::toASTfor everyINwith a constant right side, to give theFutureSetFromTupleits source AST. For aTupleconstant that materializes the value as aFieldtwice, infers a type per element, hashes and copies it: O(N) allocations, comparable to building the set. Only the filtersReadFromRemotesends to remote servers and distributed index analysis read that AST, so a local query built and dropped it.The set now keeps a clone of the constant node (O(1): no children, shared column) and converts it on the first
getSourceASTcall undercallOnce, as it already does forgetKeyColumnsandgetContentHash. The new test checks that a literalINlist still reaches the remote query; it fails whengetSourceASTreturns no AST.Measured (RelWithDebInfo, 201 paired runs, Latin-square crossover, 95% bootstrap CI), PR / master: 10000
UInt64keys in a primary key window 0.887 (0.870-0.905), 10000Stringkeys 0.884 (0.867-0.902), withuse_statistics = 00.845,set_index.xml#4 0.845 and #8 0.823; a full scan withoutIN1.020 (0.990-1.050); aremote()query that pushes the list 0.981 (0.968-0.995). PR+#132227 / #132227: 0.882, 0.880, 0.851.remote()secondary queries (19 shapes, incl. distributed index analysis) and 45 localINstatements are byte-identical.Cost: the set holds the constant's column until the query ends instead of until planning ends. With #132227 that is a few hundred bytes per value: peak memory +3.4 to +7.5 MiB for a 10000-value
NOT INbefore aGROUP BY, +2.5 MiB (9.1 to 11.6 MiB) for a one-row 10000-value lookup over HTTP. Without it, +39 MiB (one 4 KiBPODArraychunk per element column), so this PR should be merged after #132227.Thanks to @ snelheidai for the analysis and the patch in #132161.
Workflow [PR]
Sync PR [sync-upstream/pr/132247]