Skip to content

Backport #114740 to 26.6: Fix a server crash when merging -OrNull/-OrDefault states under group_by_overflow_mode = 'any' - #115137

Merged
clickhouse-gh[bot] merged 2 commits into
26.6from
backport/26.6/114740
Sep 2, 2026
Merged

clickhouse-gh[bot] merged 2 commits into
26.6from
backport/26.6/114740

Conversation

@robot-clickhouse-ci-1

@robot-clickhouse-ci-1 robot-clickhouse-ci-1 commented Aug 17, 2026 •

Copy link
Copy Markdown
Contributor

Original pull-request #114740

This pull-request is a last step of an automated backporting.
Treat it as a standard pull-request: look at the checks and resolve conflicts.
Merge it only if you intend to backport changes to the target branch, otherwise just close it.

The PR source

The PR is created in the CI job

Version info

  • Merged into: 26.6.5.31

…Default states under group_by_overflow_mode = 'any'
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added pr-backport Changes, backported to release branch. Do not use manually - automated use only! pr-bugfix Pull request with bugfix, not backported by default labels Aug 17, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 17, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [BackportPR], commit [4037ef6]

Summary: ✅

@fm4v

fm4v commented Aug 29, 2026

Copy link
Copy Markdown
Member

@groeneai fix broken test

@groeneai

Copy link
Copy Markdown
Collaborator

Root cause is a missing prerequisite on 26.6, not flakiness.

Projection p and two of the arms use the -Tuple combinator (sumTupleOrNull, sumOrNullTuple), which does not exist on this branch: src/AggregateFunctions/Combinators/AggregateFunctionTuple.cpp is present on 26.7 and on master, absent on 26.6. So 26.6.3 rejects the CREATE TABLE with Code: 46 ... Function with name sumTupleOrNull does not exist. (UNKNOWN_FUNCTION) and every later query cascades off that. The runner's own rerun makes it deterministic rather than flaky: 45 of 45 failed with the randomized settings, 103 of 103 without.

The -OrNull, -OrDefault, two-key and overflow-throw arms do not use -Tuple, and those are the ones that cover the null-place mergeBatch guard this backport carries. So the adaptation is to drop the two -Tuple arms and the two -Tuple aggregates from projection p and keep the rest, then check on a 26.6 build that the remaining arms still fail without the guard, so the test does not go vacuous.

I have no push access to backport/26.6/114740, so that goes in as a PR into this branch, as in #116104 and #116636. I will link it here.

@groeneai

Copy link
Copy Markdown
Collaborator

Correction to my note above: I said this would go in as a pull request into this branch. AGENTS.md prohibits stacked pull requests, so here is the patch instead; it needs someone with push access to backport/26.6/114740.

Three gaps, each hidden behind the one before it, so CI only reported the first:

  1. The -Tuple combinator does not exist on 26.6 (AggregateFunctionTuple.cpp is absent here, present on 26.7 and master). That is the reported Code: 46. The patch drops the two -Tuple arms and the two -Tuple aggregates from projection p, which cover code that is not on this branch.
  2. The cherry-pick predates Keep 04896_orfill_merge_group_by_overflow_any on the projection merge path at any granularity #114943, which fixed this same test on master. Without its index_granularity_bytes = 0 and preferred_optimize_projection_name pins, randomization drops projection p and the test fails with Code: 117. The label route cannot deliver this half: Keep 04896_orfill_merge_group_by_overflow_any on the projection merge path at any granularity #114943 edits a file that does not exist on 26.6.
  3. explain_query_plan_default does not exist on 26.6, so EXPLAIN here does not default to the pretty renderer that master now uses. The two-keys guard therefore spells actions = 1, pretty = 1; its assertion text and reference value stay as master has them. Same remedy as 48b38c07b849 on this branch.

The result is master's copy minus the two -Tuple arms, plus that one option line.

Verified against a 26.6.3.1 debug build:

  • the unmodified test dies on the CREATE TABLE with the reported error, exit 46
  • without the mergeBatch null-place guard this backport adds, the adapted test still crashes on two guard-less binaries, so it is not vacuous
  • output 3000/1000/1000/1/1000, byte-identical across both analyzer flavours crossed with serialize_query_plan 0 and 1 (the distributed plan lanes use 1)
  • the index_granularity_bytes pin is load-bearing: stripping it gives Code: 117 at 1024 and 7801
  • both EXPLAIN options are needed and no third one helps: actions alone and pretty alone each give 0
  • 50 of 50 randomized runs pass in each of three configurations

2 files changed, 10 insertions(+), 20 deletions(-), applies to 0362f4a7b0ee with git apply. This base gets no CI by any route, so pushing it moves this PR's head, and its own BackportPR run is where the green comes from.

diff --git a/tests/queries/0_stateless/04896_orfill_merge_group_by_overflow_any.reference b/tests/queries/0_stateless/04896_orfill_merge_group_by_overflow_any.reference
index c3829354d6af4fc..32e65b1a3753364 100644
--- a/tests/queries/0_stateless/04896_orfill_merge_group_by_overflow_any.reference
+++ b/tests/queries/0_stateless/04896_orfill_merge_group_by_overflow_any.reference
@@ -4,10 +4,6 @@ overflow any OrNull
 1000	0
 overflow any OrDefault
 1000	0
-overflow any OrNull(Tuple)
-1000	0
-overflow any Tuple(OrNull)
-1000	0
 overflow any two keys
 1
 1000	0
diff --git a/tests/queries/0_stateless/04896_orfill_merge_group_by_overflow_any.sql b/tests/queries/0_stateless/04896_orfill_merge_group_by_overflow_any.sql
index 7a855b9f64cdb8d..4656186ed59800a 100644
--- a/tests/queries/0_stateless/04896_orfill_merge_group_by_overflow_any.sql
+++ b/tests/queries/0_stateless/04896_orfill_merge_group_by_overflow_any.sql
@@ -7,6 +7,12 @@
 -- over the test runner's client-level randomization. force_optimize_projection_name keeps each
 -- arm on the aggregate-projection merge path, and the exact surviving-row counts below hold only
 -- while both hold, so an arm that stops covering the null place fails instead of passing.
+-- Row-based granules keep a projection's mark count independent of how wide its aggregate states
+-- serialize: adaptive granularity charges an aggregate-state column at its serialized width, so at
+-- a small index_granularity_bytes p reads more marks than the base table and is dropped. Pinning
+-- index_granularity_bytes (not index_granularity, whose lower bound the runner deliberately raises
+-- on slow builds) leaves the granule count driven by rows, so the test stays cheap under sanitizers.
+-- The OrNull arm also names p preferred because p2 answers it over fewer columns.
 -- Each oracle counts NULL as a mismatch, because sum() skips NULL rows and a bare s != k
 -- therefore stays 0 when a surviving row comes back NULL instead of its aggregate.
 -- Merges stay stopped so the three parts below reach the SELECT separately: one projection
@@ -16,9 +22,9 @@
 DROP TABLE IF EXISTS t_orfill;
 
 CREATE TABLE t_orfill (k UInt64, k2 UInt64, v UInt64,
-    PROJECTION p (SELECT k, sumOrNull(v), sumOrDefault(v), sumTupleOrNull(tuple(v)), sumOrNullTuple(tuple(v)) GROUP BY k),
+    PROJECTION p (SELECT k, sumOrNull(v), sumOrDefault(v) GROUP BY k),
     PROJECTION p2 (SELECT k, k2, sumOrNull(v) GROUP BY k, k2))
-ENGINE = MergeTree ORDER BY tuple();
+ENGINE = MergeTree ORDER BY tuple() SETTINGS index_granularity_bytes = 0;
 
 SYSTEM STOP MERGES t_orfill;
 
@@ -34,7 +40,7 @@ SELECT 'overflow any OrNull';
 SELECT count(), sum(s IS NULL OR s != k) FROM (SELECT k, sumOrNull(v) AS s FROM t_orfill GROUP BY k)
 SETTINGS optimize_use_projections = 1, max_threads = 1, optimize_aggregation_in_order = 0,
     max_rows_to_group_by = 10, group_by_overflow_mode = 'any',
-    force_optimize_projection_name = 'p';
+    preferred_optimize_projection_name = 'p', force_optimize_projection_name = 'p';
 
 SELECT 'overflow any OrDefault';
 SELECT count(), sum(s IS NULL OR s != k) FROM (SELECT k, sumOrDefault(v) AS s FROM t_orfill GROUP BY k)
@@ -42,22 +48,10 @@ SETTINGS optimize_use_projections = 1, max_threads = 1, optimize_aggregation_in_
     max_rows_to_group_by = 10, group_by_overflow_mode = 'any',
     force_optimize_projection_name = 'p';
 
-SELECT 'overflow any OrNull(Tuple)';
-SELECT count(), sum(s.1 IS NULL OR s.1 != k) FROM (SELECT k, sumTupleOrNull(tuple(v)) AS s FROM t_orfill GROUP BY k)
-SETTINGS optimize_use_projections = 1, max_threads = 1, optimize_aggregation_in_order = 0,
-    max_rows_to_group_by = 10, group_by_overflow_mode = 'any',
-    force_optimize_projection_name = 'p';
-
-SELECT 'overflow any Tuple(OrNull)';
-SELECT count(), sum(s.1 IS NULL OR s.1 != k) FROM (SELECT k, sumOrNullTuple(tuple(v)) AS s FROM t_orfill GROUP BY k)
-SETTINGS optimize_use_projections = 1, max_threads = 1, optimize_aggregation_in_order = 0,
-    max_rows_to_group_by = 10, group_by_overflow_mode = 'any',
-    force_optimize_projection_name = 'p';
-
 SELECT 'overflow any two keys';
 -- k2 is a stored column, not an expression over k, so optimize_group_by_function_keys cannot
 -- drop it and the aggregation really uses the fixed-keys (keys128) method.
-SELECT count() > 0 FROM (EXPLAIN SELECT k, k2, sumOrNull(v) FROM t_orfill GROUP BY k, k2)
+SELECT count() > 0 FROM (EXPLAIN actions = 1, pretty = 1 SELECT k, k2, sumOrNull(v) FROM t_orfill GROUP BY k, k2)
 WHERE explain ILIKE '%Keys: k, k2%';
 SELECT count(), sum(s IS NULL OR s != k) FROM (SELECT k, k2, sumOrNull(v) AS s FROM t_orfill GROUP BY k, k2)
 SETTINGS optimize_use_projections = 1, max_threads = 1, optimize_aggregation_in_order = 0,

@groeneai

groeneai commented Sep 2, 2026

Copy link
Copy Markdown
Collaborator

Hi @fm4v, gentle ping - any update on the 04896 patch above? It needs someone with push access to backport/26.6/114740; I cannot push there. Thanks.

@nickitat

nickitat commented Sep 2, 2026

Copy link
Copy Markdown
Member

Hi @fm4v, gentle ping - any update on the 04896 patch above? It needs someone with push access to backport/26.6/114740; I cannot push there. Thanks.

@copilot implement the change

Copilot AI commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Hi @fm4v, gentle ping - any update on the 04896 patch above? It needs someone with push access to backport/26.6/114740; I cannot push there. Thanks.

@copilot implement the change

Applied @groeneai's patch in 4037ef6: dropped the two -Tuple combinator arms, pinned index_granularity_bytes = 0, and switched the two-keys EXPLAIN to actions = 1, pretty = 1.

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@groeneai

groeneai commented Sep 2, 2026

Copy link
Copy Markdown
Collaborator

Thanks @nickitat. 4037ef69 produces the exact tree I validated locally (4c59dbf3, same parent 0362f4a7), so both files are byte-identical to what I tested; nothing in the adaptation needs re-reviewing.

That validation was on a real 26.6.3 build: the file completes with output byte-identical to its .reference in four configurations (new and old analyzer x serialize_query_plan 0 and 1, the latter being this PR's distributed plan server default), 50 of 50 runs clean, and the segfault still reproduces on two binaries built without the AggregateFunctionOrFill.h guard, so dropping the two -Tuple arms did not make the test vacuous.

Why the patch also touches index_granularity_bytes and the EXPLAIN: no CI run has ever reached statement 2 of this file on 26.6, because every one died on the CREATE TABLE with Code: 46 ... sumTupleOrNull does not exist. Two further 26.6 gaps were hiding behind that error and had to be found locally. Stateless tests (amd_asan_ubsan, distributed plan, parallel, 2/2) on this head is therefore the first real signal for the rest of the file; if it comes back red, I will post a corrected patch here.

@clickhouse-gh
clickhouse-gh Bot merged commit 06183b7 into 26.6 Sep 2, 2026
38 of 39 checks passed
@clickhouse-gh
clickhouse-gh Bot deleted the backport/26.6/114740 branch September 2, 2026 18:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backport Changes, backported to release branch. Do not use manually - automated use only! pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants