Skip to content

Fix logical error in CrossTab aggregate functions during cross state variant merge - #104277

Merged
nihalzp merged 4 commits into
ClickHouse:masterfrom
nihalzp:fix-crosstab-variant-crash
May 8, 2026
Merged

nihalzp merged 4 commits into
ClickHouse:masterfrom
nihalzp:fix-crosstab-variant-crash

Conversation

@nihalzp

@nihalzp nihalzp commented May 7, 2026 •

Copy link
Copy Markdown
Member

The following query has logical error in debug build:

SELECT round(cramersVMerge(s.`AggregateFunction(cramersV, UInt8, UInt8)`), 4)
FROM
(
    SELECT cramersVState(toUInt8(number % 10), toUInt8(number % 6)) OVER () AS s FROM numbers(100) LIMIT 1
    UNION ALL
    SELECT cramersVState(toUInt8(number % 10), toUInt8(number % 6)) AS s FROM numbers(100)
);

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

Not needed. This bug is not present in any release yet. Closes #104183.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.5.1.423

@clickhouse-gh

clickhouse-gh Bot commented May 7, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b9fe24d]

Summary: ❌

job_name test_name status info comment
AST fuzzer (amd_debug) FAIL
Logical error: Unexpected return type from A. Expected B. Got C (STID: 3262-4259) FAIL cidb IGNORED

AI Review

Summary

This PR fixes incorrect handling when merging aggregate states with different internal AggregateFunctionStateVariant representations into Variant-typed columns, which previously led to a logical exception in debug builds for cross-tab functions. The fix in FunctionCast::createColumnToVariantWrapper is targeted, preserves existing conversion rules, and is covered by a new stateless query test reproducing the reported issue; no correctness or safety regressions were identified in the changed code.

ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ✅
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅
No large/binary files ✅
Final Verdict
  • Status: ✅ Approve

@alexey-milovidov alexey-milovidov self-assigned this May 7, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 7, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 91.10% 91.10% +0.00%
Branches 76.60% 76.60% +0.00%

Changed lines: 96.36% (53/55) · Uncovered code

Full report · Diff report

@nihalzp

nihalzp commented May 7, 2026

Copy link
Copy Markdown
Member Author

@groeneai Investigate why the AST Fuzzer is failing.

@groeneai

groeneai commented May 7, 2026

Copy link
Copy Markdown
Collaborator

@nihalzp — investigated. Unrelated to your fix. This is a chronic trunk bug, safe to ignore on this PR.

Failing test: Logical error: 'Unexpected return type from tuple. Expected Tuple(UInt64). Got Tuple(Nullable(UInt64))' (STID: 3262-4259)

Stack frame: Planner/PlannerCorrelatedSubqueries.cpp:261 → decorrelateQueryPlan → ActionsDAG::evaluatePartialResult (ActionsDAG.cpp:926). Nothing in the AggregateFunctionCrossTab / CramersV path.

Failing query (excerpt):

SELECT tuple(tuple(number), (... ((SELECT (tuple(number), toNullable(toInt256(100)), -2147483648)), tuple(*), ...))) FROM ...

Note the (SELECT ... toNullable(...) ...) — that's the correlated subquery + Nullable type interaction, which is the canonical trigger of STID 3262.

CIDB cross-PR data (30d, STID 3262-4259/40aa/3b6a/3ec5 family):

Date PR Check
2026-05-07 #104277 AST fuzzer (amd_debug)
2026-05-06 master Stress test (experimental, serverfuzz, arm_msan)
2026-05-05 master Stress test (experimental, serverfuzz, azure, amd_tsan)
2026-05-04 #99909 AST fuzzer (amd_msan)
2026-04-29 #100526 AST fuzzer (arm_asan_ubsan)
2026-04-21 #100661 AST fuzzer (amd_debug, targeted)
2026-04-20 #103117 AST fuzzer (amd_debug)
2026-04-16 #92472 AST fuzzer (amd_debug)
2026-04-09 #96886 AST fuzzer (amd_msan)

7 unrelated PRs + 2 master hits. Diverse PR authors / changesets — none touch CrossTab.

Root cause (already isolated, deterministic local repro):
Correlated subquery + group_by_use_nulls = 1 + WITH ROLLUP/CUBE. After decorrelateActions(), INPUT nodes keep original non-Nullable types while the actual input header has Nullable types from group_by_use_nulls. updateInputHeader then trips the assertion at ActionsDAG.cpp:926.

Canonical fix: PR #100365 by @alexey-milovidov ("Fix exception in correlated subquery with group_by_use_nulls + ROLLUP/CUBE"), open since 2026-03-22 and being iterated on (latest activity 2026-05-06). Adds ActionsDAG::reconcileInputTypesAfterDecorrelation to fix INPUT node types post-decorrelation.

Recommendation: Just re-run the AST fuzzer (amd_debug) lane — it will pick a different fuzz seed and pass. Your fix is good to go from the CI side. Tracked under 2026-04-08-ci-p0-ast-fuzzerstress-logical-error- on our side; we monitor PR #100365 for merge.

@nihalzp
nihalzp added this pull request to the merge queue May 8, 2026
Merged via the queue into ClickHouse:master with commit 3fb6558 May 8, 2026
163 of 165 checks passed
@nihalzp
nihalzp deleted the fix-crosstab-variant-crash branch May 8, 2026 07:11
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 8, 2026
alexey-milovidov added a commit that referenced this pull request May 8, 2026
alexey-milovidov pushed a commit to clickgapai/ClickHouse that referenced this pull request Jul 3, 2026
…erge

The cramersV, cramersVBiasCorrected, contingency and theilsU aggregate
functions have two internal state representations (Aggregation and Window)
that share the same type name. PR ClickHouse#104277 fixed the crash when a scalar
AggregateFunction column carrying one representation is cast into a Variant
whose same-named slot expects the other (createColumnToVariantWrapper).

The Variant to Variant cast path (createVariantToVariantWrapper) had the same
gap: it mapped variant slots purely by name and copied their subcolumns
verbatim, so a subcolumn carrying one representation could be placed under a
slot expecting the other. Chained set operations (EXCEPT / INTERSECT) and
nested UNION ALL produce exactly this Variant to Variant cast, so a later read
of the state (finalization, serialization, or the -Merge combinator) then
interpreted the bytes with the wrong layout and the server aborted.

Convert the subcolumn to the destination representation in
createVariantToVariantWrapper when an old and a new slot match by name but
differ (equals() is false), reusing the existing AggregateFunction
representation-converting cast, instead of copying it as-is.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…erge

The cramersV, cramersVBiasCorrected, contingency and theilsU aggregate
functions have two internal state representations (Aggregation and Window)
that share the same type name. PR ClickHouse#104277 fixed the crash when a scalar
AggregateFunction column carrying one representation is cast into a Variant
whose same-named slot expects the other (createColumnToVariantWrapper).

The Variant to Variant cast path (createVariantToVariantWrapper) had the same
gap: it mapped variant slots purely by name and copied their subcolumns
verbatim, so a subcolumn carrying one representation could be placed under a
slot expecting the other. Chained set operations (EXCEPT / INTERSECT) and
nested UNION ALL produce exactly this Variant to Variant cast, so a later read
of the state (finalization, serialization, or the -Merge combinator) then
interpreted the bytes with the wrong layout and the server aborted.

Convert the subcolumn to the destination representation in
createVariantToVariantWrapper when an old and a new slot match by name but
differ (equals() is false), reusing the existing AggregateFunction
representation-converting cast, instead of copying it as-is.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-not-for-changelog This PR should not be mentioned in the changelog 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.

[CI crash] Segfault in AggregateFunctionCrossTab<CramersVData>::serializeBatch on TCP send path

4 participants