Repository navigation
Conversation
IColumn::compareAt returns 0 for any two aggregate function states, and DataTypeAggregateFunction::equals compares normalized state types, so two constants holding different states (or the same bytes of different functions, e.g. quantileState(0.5) and quantileState(0.9)) were the same constant in ConstantNode::isEqualImpl and in the ActionsDAG constant dedup. Both now also compare the names of the nested AggregateFunction types and the states position by position, walking the columns instead of building a Field of the whole value, which cannot hold an array of more than 1000000 elements; values inside Dynamic and JSON are not looked into. Both constant hashes add the same names: IDataType::updateHash skips aggregate function parameter values, and the tree hash alone keys IN subquery sets. Move sameConstantValue from ActionsChain.cpp to Field.cpp (iterative, with Object) and use it in the projection rename, whose Field != threw for states of different functions. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review: my own cold review plus an independent model (engine: codex; 6 rounds; the last round found nothing).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261009-085200 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-40:20261009-014700 |
|
Workflow [PR], commit [f409687] Summary: ❌
AI ReviewSummaryThe PR distinguishes aggregate-state constants by function, parameters, versions, and serialized values in analyzer and optimizer identities. The main regression test matches its reference on the PR-head Fast test binary, but wrapper casts still let different live functions share an identity and produce wrong results. One earlier time zone finding also remains reproducible and is tracked separately. Findings❌ Blockers
Tests
Final VerdictStatus: ❌ Block Fix the wrapper-cast identity gap and add focused regression coverage. The earlier time zone issue remains tracked in PR #117161; incorporate or land that fix before treating that finding as resolved. |
The constant identity compared the full names of the nested AggregateFunction types, which spell argument types that equals ignores on purpose (LowCardinality, which the aggregate function factory strips, and time zones), so two spellings of one state, e.g. AggregateFunction(argMax, LowCardinality(String), DateTime) and AggregateFunction(argMax, String, DateTime), became a conflicting alias. Compare and hash only the state version, the function name and its parameters, which equals no longer sees after normalization. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rees matchTrees compared an unmapped constant child with Field ==, which throws ILLEGAL_TYPE_OF_ARGUMENT for states of different functions whose types IDataType::equals considers equal, e.g. quantileState(0.5) and quantileState(0.9). A query ordering or grouping by such an expression over a table whose sorting key or aggregate projection uses the other one failed in read-in-order or projection matching. Use sameConstantValue, which compares those states as different, so the trees do not match. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The line checks that a constant of more than 1000000 elements carrying an aggregate state is compared without building a Field of it. It used an array of 1000001 countState values, which took about 7 s alone under ASAN and made the test exceed 180 s in the flaky check. The cap of ColumnArray::get is a compile-time constant, so keep the array length and put a single state next to range(1000001) instead. The line still fails with TOO_LARGE_ARRAY_SIZE on the earlier Field-based comparison, and takes about 1.5 s under ASAN. Co-Authored-By: Claude Opus 5.5 <[email protected]>
CI finish ledger - b6259d7Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
The earlier Session id: cron:our-pr-ci-monitor:20261009-230008 |
…state-constant-identity
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes181 object files changed (+21.31 KiB total), 0 added.
716 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 units3539 translation units recompiled, 14308 s compile time in total, 3539 of them have a recent master baseline. |
CI finish ledger - 7fff5c8Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20261010-073010 |
…state-constant-identity Resolve the conflict with ClickHouse#121725, which removed the `function` argument of `DataTypeAggregateFunction::formatParameters`: `getAggregateFunctionNames` now spells the parameters with the one-argument form, as `getNameImpl` does. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…stant identity forEachInTypeTree does not descend into the argument types of an AggregateFunction type, so AggregateFunction(groupArray, AggregateFunction(quantile(0.5), UInt8)) and its quantile(0.9) counterpart recorded only groupArray. IDataType::equals normalizes the nested parameters away and the states serialize to the same bytes, so the two constants were still merged: an IN over the two finalized values shared one set, a filter on one was merged with the other expression, and a duplicate alias over them was accepted. Collect the aggregate functions with their parameters and state versions from the argument types too. The rest of the argument types is still left to IDataType::equals, so spellings such as LowCardinality(String) and String of one state stay equal. Co-Authored-By: Claude Opus 5.5 <[email protected]>
CI finish ledger - 0563f9cEvery 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:20261024-180006 |
…state-constant-identity Only the include lists of `DataTypeAggregateFunction.cpp` (master added `ColumnReplicated.h`) and `ActionsDAG.cpp` (master added `ColumnAggregateFunction.h`) conflicted; both sides' headers are kept. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…state-constant-identity # Conflicts: # src/Analyzer/ConstantNode.cpp # src/Processors/QueryPlan/Optimizations/actionsDAGUtils.cpp
| { | ||
| Field lhs_state = (*lhs_column)[lhs_row]; | ||
| Field rhs_state = (*rhs_column)[rhs_row]; | ||
| return lhs_state.safeGet<AggregateFunctionStateData>().data != rhs_state.safeGet<AggregateFunctionStateData>().data; |
There was a problem hiding this comment.
[Blocker] Include the function held by the column in constant identity
The declared type can disagree with the function in ColumnAggregateFunction: FunctionCast::prepareImpl uses an identity conversion for normalized-equal tuple types, retaining the original nested column. A cast from Tuple(AggregateFunction(quantile(0.9), UInt8)) to its 0.5 counterpart therefore leaves a column that still finalizes to 70.9, while its declared state type says 0.5. This branch compares only the serialized data, and both new hashes collect functions only from the declared type, so they still conflate that constant with the actual 0.5 state.
On the PR-head Fast test binary, this returns 1 instead of 2:
WITH
arrayReduce('quantileState(0.5)', [1, 2, 3, 100]) AS lo,
tupleElement(CAST(
tuple(arrayReduce('quantileState(0.9)', [1, 2, 3, 100])),
'Tuple(AggregateFunction(quantile(0.5), UInt8))'), 1) AS hi
SELECT count() FROM numbers(100)
WHERE number IN (SELECT toUInt64(finalizeAggregation(materialize(lo))) AS v)
OR number IN (SELECT toUInt64(finalizeAggregation(materialize(hi))) AS v);Evaluated independently, the two subqueries produce 2 and 70. Using the same pair in the PR's filter reproducer also returns no rows; disabling query_plan_merge_expressions restores both rows.
Please ensure column/type agreement before constructing constant identities, or consistently include the actual column function and its parameters in comparison and both hashes. Add a regression covering a state carried through a wrapper cast; extending the declared-type traversal alone cannot distinguish this pair.
There was a problem hiding this comment.
This returns 1 on master too, and the cause is the column rather than the identity: DataTypeAggregateFunction::equals ignores the quantile level, so a CAST inside a Tuple keeps the quantile(0.9) function under the declared quantile(0.5) type (70.9 here, while a top-level CAST to the same type and a MergeTree round trip of this value both give 2.5). With a column that agrees with its type, both constants are the same quantile(0.5) state and one set is correct, so I keep this PR's identity on the declared type.
Related: #122362
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix wrong results when two expressions differ only in an aggregate function state constant, for example
initializeAggregation('groupArrayState', 'x')andinitializeAggregation('groupArrayState', 'y'), orquantileState(0.5)andquantileState(0.9)of the same data. Such constants were treated as the same value, so a filter, anINsubquery or a subquery column could use the other one, and a conflicting alias was not reported.Description
On 26.8.18.2,
SELECT number, a FROM (SELECT number, finalizeAggregation(materialize(arrayReduce('quantileState(0.5)', [1, 2, 3, 100]))) + number AS a FROM numbers(2)) WHERE finalizeAggregation(materialize(arrayReduce('quantileState(0.9)', [1, 2, 3, 100]))) + number > 50returns no rows instead of two, and a subquery whose two columns differ only in an aggregate state constant returns the first column twice. The second shape is theSubquery wrap oracle mismatchofAST fuzzer (amd_release, oracle)on #122362 (job.log).ConstantNode::isEqualImpland theActionsDAGconstant deduplication (ConstantKeyEqual) compare constants withIColumn::compareAt, which returns 0 for any two aggregate function states, andDataTypeAggregateFunction::equalscompares normalized state types, soquantile(0.5)andquantile(0.9)states with equal bytes also match. The duplicate alias check, GROUP BY validation and the projection rename then treat two different constants as one, andmergeExpressionsevaluates the filter with the other constant. The tree hash, the only key ofINsubquery sets, also ignores the parameter values.Both places now also compare the nested aggregate functions, including those in a state's argument types, with their parameters (other argument types stay with
equals) and, walking the columns, the states position by position; both constant hashes add the functions.sameConstantValuemoves fromActionsChain.cpptoField.cppand replaces theFieldcomparison of the constant projection rename and ofmatchTrees(read-in-order and aggregate projection matching), which threwCode: 43for different functions.Not covered: a state stored inside
DynamicorJSONkeeps the previous comparison.The new test fails on master and passes with the fix.
🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/124867]