Skip to content

Fix mismatched -Tuple combinator state name when a nulls-action modifier is applied under nested combinators - #111570

Merged
alexey-milovidov merged 5 commits into
masterfrom
fix-tuple-combinator-nested-suffix-nulls-action
Jul 26, 2026
Merged

alexey-milovidov merged 5 commits into
masterfrom
fix-tuple-combinator-nested-suffix-nulls-action

Conversation

@alexey-milovidov

Copy link
Copy Markdown
Member

The -Tuple combinator names its shared state via getAssociatedNameByNullsAction, but the respect_nulls / ignore_nulls maps hold only base aggregate names. When the nested name itself still carries combinator suffixes (e.g. anyRespectNullsStateTuple nests anyRespectNullsState), the lookup missed and the shared name stayed unadjusted, while getImpl applied the nulls action at the base of the chain when instantiating the tuple elements. The state type name then identified a different function than the elements actually held, and a -State type-name round-trip (shard to initiator in a distributed query) reconstructed a mismatched function: CANNOT_CONVERT_TYPE in the checked path — with a confusing message where both printed types look identical — and a logical error Bad cast from type DB::ColumnAggregateFunction to DB::ColumnNullable (or an invalid memory access in the state destructor under WITH TOTALS) when the check was bypassed.

The fix: when the name passed to getAssociatedNameByNullsAction is not a registered base function, strip the combinator suffix, adjust the nested name for the action recursively, and re-append the suffix. Base function names whose tail merely looks like a combinator suffix (e.g. sumMap) are not stripped. This is a follow-up to the same-shaped base-name fix in #110930.

Minimal reproducer (previously CANNOT_CONVERT_TYPE):

SELECT anyRespectNullsStateTuple(tuple(toNullable(1))) IGNORE NULLS
FROM remote('127.0.0.{1,2}', system.one) GROUP BY ALL WITH TOTALS;

and the fuzzed shape with a longer chain under -OrDefault aborted the server in the state destructor.

Found by AST fuzzer on an unrelated PR: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=110105&sha=cdc8066194937d0e1b72ab104a00dac0b285d2b3&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%29

Related: #110930
Related: #110105

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fixed an exception (CANNOT_CONVERT_TYPE or a logical error) in distributed queries using the -Tuple combinator with a RESPECT NULLS / IGNORE NULLS modifier under nested combinators, e.g. anyRespectNullsStateTuple(...) IGNORE NULLS.

…e names

The `-Tuple` combinator names its shared state via
`getAssociatedNameByNullsAction`, but the `respect_nulls` / `ignore_nulls`
maps hold only base aggregate names. When the nested name itself carries
further combinator suffixes (e.g. `anyRespectNullsStateTuple` nests
`anyRespectNullsState`), the lookup missed and the name stayed unadjusted,
while `getImpl` applied the nulls action at the base of the chain when
instantiating the elements. The state type name then identified a different
function than the elements actually held, and a `-State` type-name
round-trip (shard to initiator in a distributed query) reconstructed a
mismatched function: `CANNOT_CONVERT_TYPE` in the checked path, and a
logical error `Bad cast from type DB::ColumnAggregateFunction to
DB::ColumnNullable` (or an invalid memory access in the state destructor
under `WITH TOTALS`) when the check was bypassed.

Fix: when the name is not a registered base function, strip the combinator
suffix, adjust the nested name for the action recursively, and re-append
the suffix. Follow-up to the same-shaped base-name fix in #110930.

Found by AST fuzzer:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=110105&sha=cdc8066194937d0e1b72ab104a00dac0b285d2b3&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%29
@clickhouse-gh

clickhouse-gh Bot commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [a78094f]

Summary: ✅


AI Review

Summary

This PR fixes the -Tuple shared-state naming path so IGNORE NULLS / RESPECT NULLS are applied at the base of a nested combinator chain, which makes the serialized state name match the nested functions that getImpl actually instantiates. The added stateless coverage exercises the distributed round-trip, a longer nested chain, and the guard against stripping real base names like sumMap; I did not find a remaining correctness or metadata issue in the current diff.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 23, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member Author

@groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=111570&sha=9da0bb63238fd12de7a1c20b069635e06565a0b5&name_0=PR&name_1=Stateless%20tests%20%28amd_asan_ubsan%2C%20distributed%20plan%2C%20parallel%29 and provide a fix in a separate PR. If the fix is already in progress, link it here. The run hit server-wide memory pressure (RSS 43 GiB, (total) memory limit exceeded), taking down several memory-hungry tests at once (01661_extract_all_groups_throw_fast, 03216_arrayWithConstant_limits, 01276_random_string, 00053_replicate_segfault, 04033_tpc_ds_q88) — all of them fail regularly across other PRs (e.g. 03216_arrayWithConstant_limits: 32 failures in 32 PRs over 14 days) and are unrelated to this PR.

@groeneai

Copy link
Copy Markdown
Collaborator

@alexey-milovidov Confirmed: unrelated, server-wide host-OOM. Not caused by this PR.

Evidence (this run: Stateless tests (amd_asan_ubsan, distributed plan, parallel)):

  • 407 (total) memory limit exceeded events (Code 241, OvercommitTracker), RSS pinned at 44.85 GiB against the 44.83 GiB host cap, sustained across the 14:41-14:47 window. Every killed query is co-scheduled collateral, not a per-query/per-user limit.
  • This PR's diff is aggregate-combinator naming (AggregateFunctionFactory) plus one new stateless test; none of the killed queries touch that code.

The 6 failing tests are all innocent victims (one more than listed above: 04418_docs_web_ui_importless_mdx_badge failed with curl (56) Recv failure: Connection reset by peer against /docs inside the same window; 12/12 reruns passed). Rerun outcomes: 00053_replicate_segfault 13/13 pass, 04033_tpc_ds_q88 6/6 pass, 04418 12/12 pass, 01276_random_string flaky victim (11/17 pass). The two "reproducible" ones are the deliberately memory-hungry tests: 01661_extract_all_groups_throw_fast (all 407 events are its repeat('abc...', number*100) FROM numbers(1023) query) and 03216_arrayWithConstant_limits (near-limit array coverage, ~1 GiB just under the 1e9-byte limit).

Fixes:

No new PR needed here beyond the in-progress #111513; the rest is host-OOM co-scheduling covered by #110503/#110574.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

The remaining red — Upgrade check (amd_release) — is unrelated to this PR:

Once those merge, an update of this branch should clear the check.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

@groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=111570&sha=6ac7b754d536ff5988ff1fe348e22a810f1ba016&name_0=PR&name_1=Stateless%20tests%20%28amd_llvm_coverage%2C%20old%20analyzer%2C%20s3%20storage%2C%20DBReplicated%2C%20WasmEdge%2C%20parallel%2C%202%2F3%29 and provide a fix in a separate PR. If the fix is already in progress, link it here. The shard died from a server abort on a logical error !part.empty() (chassert in ASTIdentifier, reported under STID 2735-47bf with a FunctionBinaryArithmetic multiply dry-run stack via ActionsDAG::updateHeader); every other failed test in the run is "server died" collateral. The same signature hit at least 15 unrelated PRs on the coverage shards in the last 24 hours (e.g. #110955, #96978, #111286, #111063, #111852), so it is unrelated to this PR. Possibly connected to the misattribution addressed in #111708.

The other red, Upgrade check (amd_release) system.metric_log readonly-ALTER QUERY_IS_TOO_LARGE, is fixed by the now-merged #111703 — I merged master to pull it in and retrigger CI.

@groeneai

Copy link
Copy Markdown
Collaborator

Confirmed at head 6ac7b754: the shard's only <Fatal> is a multiply type-mismatch logical error, not a genuine !part.empty() chassert.

Server log (592faba8, 2026.07.24 20:02:44.852562):
Logical error: 'Arguments of 'multiply' have incorrect data types: 'n' of type 'Int256', '0.123' of type 'Float64''
sibling error line: (comment: 03915_exchange_tables_race.sh-...) (in query: SELECT n * 0.123 FROM (SELECT * FROM tbl_03007_1)).

Root cause: 03915_exchange_tables_race runs EXCHANGE TABLES concurrently with SELECT n * 0.123, swapping n's type (Int256 <-> Float64) between analysis and the header dry-run. The stale header reaches multiply in ActionsDAG::updateHeader, which aborts under the coverage build. The !part.empty() STID label is a crash-reporter misattribution (its log scan grabs an unrelated echoed test comment); there is no part.empty() Fatal in the log. Every other failed test in the run is server-died collateral. Not caused by this PR (the unrelated PRs you list confirm it is master-side).

Fix already in progress, so no new PR is needed:

@alexey-milovidov

Copy link
Copy Markdown
Member Author

@groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=111570&sha=bd189239c8b922391952fb7391ce1f32bea7f0bd&name_0=PR&name_1=Stress%20test%20%28arm_debug%29 and provide a fix in a separate PR. If the fix is already in progress, link it here.

The only remaining red is Logical error: Inconsistent KeyCondition behavior (STID: 5182-684a), hit by the AST fuzzer inside the stress test via optimizeUseAggregateProjections -> MergeTreeDataSelectExecutor::markRangesFromPKRange. It is unrelated to this PR (the diff only touches AggregateFunctionFactory combinator naming plus one stateless test) and it is master-wide: the same signature occurred 45 times across 37 PRs on 2026-07-24, 52 times across 48 PRs on 2026-07-25, and on master itself (pull_request_number = 0) — up from ~1-3 per day before 07-24, which suggests a recent regression or a fuzzer-coverage change on master.

Known related items: issue #111421, issue #107951 (AST-fuzzer-internal-path assertions, KeyCondition among the listed sites), and the open fix #109023. Given the sharp spike starting 2026-07-24, please check whether a master commit from that day widened the trigger, and link the fix here.

I merged master into the branch to retrigger CI.

@groeneai

Copy link
Copy Markdown
Collaborator

The fix is in progress: #109023 (one-line change in KeyCondition::matchesExactContinuousRange). This failure is covered by it.

Ground truth from clickhouse-server.err.log of that job (CIDB's Failed query field is misleading for fuzzer aborts, it records an earlier harmless mutation under the same query id):

  • canary: test_qtvh08zi.t_both_rev ... mark range [10, 18) (exact subrange [11, 17)) of part all_1_6_1 ... Participating conditions: key condition (useful: true) (relaxed: false): (_CAST(column 0) in ['2', '2']), (column 1 in [10, 10])
  • the query that actually aborted, from the BaseDaemon line for query id 3a73a6c6-c857-48ab-a425-e21b7cfa0a19: SELECT count() FROM test_qtvh08zi.t_both_rev WHERE (a = toUInt128(2)) AND (10 = b)

_CAST on the leading key is the widening-cast manifestation of #90461: toUInt128(2) against a UInt8/Int8 key leaves a non-empty monotonic function chain, matchesExactContinuousRange() still reports POINT for it, while markRangesFromPKRange applies that chain to granule bounds with forced-closed bounds and so cannot honour exactness at a boundary granule. #109023 requires the chain to be empty before promising POINT.

The optimizeUseAggregateProjections frame is not a separate carrier: the canary is thrown inside markRangesFromPKRange whoever calls it, and #109023's test already pins the aggregate/implicit exact-count projection path (_exact_count_projection assertions), including a DESC leading key and the toUInt128/toInt128/toUInt256 variants. So this instance needs no additional change.

On the 07-24 spike: it is not a new regression, it is the same pre-existing class being fed to the fuzzer far more often. The trigger is #111059 (merged 2026-07-24 08:39 UTC, 3993b0f) through its new test 04612_reverse_key_index_analysis.sql, not through its reverse-key code changes. t_both_rev above is that test's table, mutated into t_both_rev__fuzz_N, and its a = 2 AND b = 2 shape is exactly what the fuzzer widens into a = toUInt128(2) on a two-column key with index_granularity small enough for the analyzer and the reader to disagree. I tested the "reverse-key logic itself regressed" hypothesis and refuted it: ascending-key tables fail in the same spike, and there are no non-cast canary lines in the server logs I read. Full write-up: #108091 (comment)

So no separate PR is needed for this red; #109023 is the fix for the family and is mergeable, awaiting review.

@clickhouse-gh

clickhouse-gh Bot commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.20% 86.30% +0.10%
Functions 92.00% 92.00% +0.00%
Branches 78.40% 78.40% +0.00%

Changed lines: Changed C/C++ lines covered: 20/25 (80.00%) · Uncovered code

Full report · Diff report

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.

@alexey-milovidov alexey-milovidov self-assigned this Jul 26, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jul 26, 2026
Merged via the queue into master with commit 84d39eb Jul 26, 2026
179 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-tuple-combinator-nested-suffix-nulls-action branch July 26, 2026 20:18
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants