Repository navigation
Description
Company or project name
ClickHouse Cloud (found by replaying a production query against 26.4 and 26.6).
Describe what's wrong
GROUP BY can emit a group out of a zero-row block, producing a result row with count() = 0. A group only exists because at least one row mapped to it, so count() in a GROUP BY result is >= 1 by construction, and a query whose aggregation input is empty must return no rows at all.
There are two separate things here, and the second one is the actual defect:
- Since
26.6, an emptyINset makesinreturn aColumnConstrather than a full column, and that constness propagates throughif. This is what made the bug reachable from ordinary SQL. Aggregatorhas a long-standing bug: withoptimize_group_by_constant_keys = 1(the default), when allGROUP BYkey columns are constant it emplaces key row0unconditionally, with no check that the block has any rows. This is present in26.4and26.5as well, and is reachable there withmaterialize().
The visible effect is a silent wrong result under default settings on stock MergeTree, with nothing to opt into and no error: a chart that should be empty instead shows a phantom bucket with zero volume.
Does it reproduce on the most recent release?
Yes
How to reproduce
Reproduces with clickhouse-local, no server and no non-default settings.
Case 1, the 26.6 regression. facts is empty at runtime because no element of kind ever equals 'request', so the ARRAY JOIN yields zero rows. The IN set is therefore empty, in returns a constant, the GROUP BY key becomes constant, and a group is emitted anyway:
CREATE TABLE repro_arr (id UInt64, kind Array(String)) ENGINE = MergeTree ORDER BY id;
INSERT INTO repro_arr SELECT number, ['intent', 'cause'] FROM numbers(1000);
WITH facts AS
(
SELECT t AS raw_key
FROM ( SELECT arrayFilter(x -> (x = 'request'), kind) AS arr FROM repro_arr ) AS raw
ARRAY JOIN arr AS t
)
SELECT key, count() AS volume
FROM
(
SELECT *, if(raw_key IN (SELECT raw_key FROM facts), toString(raw_key), 'OTHER') AS key
FROM facts
)
GROUP BY key;| version | result |
|---|---|
26.4.4.38 |
empty (correct) |
26.5.5.8 |
empty (correct) |
26.6.2.160 |
OTHER 0 |
26.8.1.2745 |
OTHER 0 |
Case 2, the underlying Aggregator bug, which does not need the 26.6 change and reproduces on every version above including 26.4.4.38 and 26.5.5.8:
WITH facts AS
(
SELECT t AS raw_key
FROM ( SELECT arrayFilter(x -> (x = 'request'), kind) AS arr FROM repro_arr ) AS raw
ARRAY JOIN arr AS t
)
SELECT materialize('OTHER') AS key, count() AS volume
FROM facts
GROUP BY key;
-- 26.4.4.38, 26.5.5.8, 26.6.2.160, 26.8.1.2745: all return `OTHER 0`Replacing materialize('OTHER') with the plain literal 'OTHER' correctly returns nothing on all four versions, so only the runtime-constant path is affected.
Both cases require the ARRAY JOIN. It forwards an empty block to the aggregator, whereas an equivalent WHERE filter drops the chunk outright and does not reproduce.
The constness change in case 1 is directly observable:
SELECT dumpColumnStructure(number IN (SELECT number FROM numbers(100) WHERE number > 1000)) FROM numbers(3) LIMIT 1;
-- 26.4, 26.5: UInt8, UInt8(size = 1)
-- 26.6+: UInt8, Const(size = 1, UInt8(size = 1))A non-empty set returns a full column on all versions, so only the empty-set case changed.
Expected behavior
Both queries return an empty result, as 26.4 and 26.5 do for case 1. Zero input rows means zero groups.
Note that optimize_group_by_constant_keys is documented as an optimization ("Optimize GROUP BY when all keys in block are constant"), yet setting it to 0 changes the answer from one row to zero rows on the same data and the same version. An optimization must not change query results, which is independent evidence that the optimized path is the wrong one.
Error message and/or stacktrace
None. The query succeeds and returns a wrong row.
Related issues and pull requests
Caused by: #100226
Related: #116018
Related: #116057
Additional context
Workaround for affected users:
SETTINGS optimize_group_by_constant_keys = 0Verified to restore the empty result for case 1 on 26.6.2.160 and on ClickHouse Cloud 26.6.
Suggested fix location. The constant-key path in Aggregator never checks whether the block has rows before creating a group:
src/Interpreters/Aggregator.cpp,if (all_keys_are_const) { process_row(0); return; }src/Interpreters/Aggregator.cpp,key_start = 0; key_end = 1;src/Interpreters/Aggregator.cpp, theis_simple_countbranch, which writesrow_end - row_begin(that is,0) into a freshly emplaced key
All three want a row_begin == row_end early-out. Returning a ColumnConst for an empty set looks deliberate and worth keeping, and the comment in src/Functions/in.cpp says the constness is needed so ConstantFilterDescription can short-circuit filters, so in is probably not the place to change.
Bisect. Bisected on master over the 26.6 development window, 9874 commits between cdee4c67ae5d33f5d1853693325b23ed88a36cfc (26.5.1.878, empty result) and d5846cd8b28736322e10fa5f4158d542224fd16d (26.6.1.1185, phantom row), 12 steps, no skipped or errored steps. First bad commit:
46bbaccaa56101b28d6950568a490db0ea208f22
Merge pull request #100226 from ClickHouse/column-set-always-const
Enforce invariant that `ActionsDAG::Node::column` is always ColumnConst
Verified directly across the boundary with official ARM release builds:
| commit | version | result |
|---|---|---|
bc96550adf42a551ce7ad1b6e68deccaed64a129 (first parent) |
26.6.1.91 |
empty (correct) |
46bbaccaa56101b28d6950568a490db0ea208f22 (the merge) |
26.6.1.92 |
OTHER 0 |
This is the same pull request that #116018 bisected to, which supports reading the two issues as one cause with two downstream symptoms. Note that it made the latent Aggregator bug reachable, and case 2 above shows the Aggregator bug itself predates it, so this pull request is not necessarily the right place to fix.
Relationship to #116018. That issue shares the trigger family (an empty IN set becoming constant, at the same 26.5 to 26.6 boundary) but has a different symptom and a different consumer of the constness: an ILLEGAL_COLUMN exception from ActionsDAG::updateHeader. #116057 makes FunctionIn declare the fold via getConstantResultForNonConstArguments, which would turn case 1 into a plan-time literal and therefore likely fix case 1 as a side effect. It would not fix case 2, since that never involves in at all.
Version info
- Resolved by: Do not emit a GROUP BY group out of a zero-row block #117503
- Merged into:
26.9.1.563(included in26.9and later) - Backported to:
26.8.3.58,26.7.7.43,26.6.5.69
Activity
fm4v commentedon Aug 28, 2026
Updated the issue body with the bisect result.
Bisected on
masteracross the26.6development window (9874 commits, 12 steps, no skipped or errored steps). First bad commit is the merge of #100226, "Enforce invariant that `ActionsDAG::Node::column` is always ColumnConst":bc96550adf42a551ce7ad1b6e68deccaed64a129(first parent)26.6.1.9146bbaccaa56101b28d6950568a490db0ea208f22(the merge)26.6.1.92OTHER 0That is the same pull request #116018 bisected to, so the two issues most likely share one cause with two downstream symptoms: an
ILLEGAL_COLUMNexception fromActionsDAG::updateHeaderthere, and a silent wrong result fromAggregatorhere.Worth keeping in mind for whoever picks this up: case 2 in the body reproduces on
26.4.4.38and26.5.5.8as well, usingmaterialize()and noINat all. So theAggregatorconstant-key path was already emitting groups out of zero-row blocks before #100226, and #100226 only made it reachable from ordinary SQL. FixingFunctionInalone (as #116057 does) would very likely fix case 1 as a side effect, but would leave case 2 broken.