Skip to content

Fix WITH TOTALS losing constant column values in JOIN - #108807

Merged
alexey-milovidov merged 1 commit into
masterfrom
vdimir/fix-join-totals-const-swap
Jun 30, 2026
Merged

alexey-milovidov merged 1 commit into
masterfrom
vdimir/fix-join-totals-const-swap

Conversation

@vdimir

@vdimir vdimir commented Jun 29, 2026 •

Copy link
Copy Markdown
Member

When a JOIN has WITH TOTALS on one side, the totals row of the other side is synthesized from default values. QueryPipelineBuilder::addDefaultTotals built every column with type->createColumn + insertDefault, which discarded ColumnConst values and collapsed them to type defaults.

As a result, the constant values of a constant subquery (e.g. SELECT 42 AS x, 99 AS y) were lost in the totals row whenever that subquery ended up on the side without its own totals. Which side that is depends on the order the tables are written and on build/probe swapping (query_plan_join_swap_table), so the same query could return correct constants in one ordering and zeros in another.

This preserves constant column values in the synthesized totals row, making all orderings consistent. Surfaced by 03205_column_type_check under query_plan_optimize_join_order_randomize.

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 a bug where the WITH TOTALS row of a JOIN could contain default values (such as 0) instead of the constant values coming from a constant subquery on one side of the join. The wrong result appeared only for some join orderings or with the query_plan_join_swap_table setting.

🤖 Generated with Claude Code

Version info

  • Merged into: 26.7.1.311 (included in 26.7 and later)

The synthesized default TOTALS row in `QueryPipelineBuilder::addDefaultTotals`
built every column with `type->createColumn` followed by `insertDefault`, which
discarded `ColumnConst` values and collapsed them to type defaults.

For a JOIN with `WITH TOTALS`, the default TOTALS row is produced for the side
that has no totals of its own (for example, a constant subquery). Which side
that is can change with build/probe ordering (`query_plan_join_swap_table`) or
simply by the order the tables are written, so the constants were lost in some
orderings but preserved in others. Now constant columns keep their value in the
synthesized TOTALS row, making all orderings consistent.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jun 29, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [8bb32ea]

Summary: ✅


AI Review

Summary

This PR fixes synthesized WITH TOTALS rows for JOIN so a side without totals preserves existing ColumnConst values instead of replacing them with type defaults. The new stateless coverage checks both table orders and both query_plan_join_swap_table modes. I did not find correctness, safety, or metadata issues in the current diff.

Missing context / blind spots
  • ⚠️ The linked PR CI report for commit 8bb32eae currently returns zero test results, and this checkout has no local build directory or binary to run the new stateless test. Normal CI execution would close this gap.
Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jun 29, 2026
@antaljanosbenjamin antaljanosbenjamin self-assigned this Jun 29, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.50% 85.40% -0.10%
Functions 92.60% 92.60% +0.00%
Branches 77.70% 77.60% -0.10%

Changed lines: Changed C/C++ lines covered by tests: 8/8 (100.00%) | Lost baseline coverage: none · Uncovered code

Full report · Diff report

@antaljanosbenjamin antaljanosbenjamin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jun 30, 2026
Merged via the queue into master with commit ddbfc52 Jun 30, 2026
174 checks passed
@alexey-milovidov
alexey-milovidov deleted the vdimir/fix-join-totals-const-swap branch June 30, 2026 23:59
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 1, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

Found via ClickGap automated review. Please close or comment if this is incorrect or needs adjustment.

TL;DR

Fix WITH TOTALS losing constant column values in JOIN

ClickGap verified: reproduced it with the script below on 26.5; tested 6 version(s) — 5 affected, 1 clean (see Affects); bisected to a candidate range — a single introducing PR could not be pinned (see Bisect).

Reproduction

-- Constant subquery on the JOIN side that has no totals of its own.
-- Pre-fix, the synthesized WITH TOTALS row collapsed the constants to 0.
SELECT x, y
FROM (SELECT 42 AS x, 99 AS y) AS u,
     (SELECT 1 AS k GROUP BY k WITH TOTALS) AS t
FORMAT JSONCompact;
-- Buggy: "totals": [0, 0]   Correct: "totals": [42, 99]

▶ Run on ClickHouse Fiddle

Bisect

  • The bisect boundary was rejected — PR add tsa annotation to column sizes and indexes around lazy evaluation #99240 only adds TSA annotations to MergeTree storage layer code for thread-safety analysis, while the bug involves WITH TOTALS losing constant column values in JOIN operations which is a query execution/planning issue unrelated to storage metadata or lazy evaluation. — a coincidental boundary, not the cause; no introducing PR pinned.

Affects

  • ✓ Fix is on master — this PR's change fixes the bug on master. Branches below still have the bug and need the backport.
  • Reproduces on: 26.5, 26.4, 26.3, 26.2, 26.1
  • Does NOT reproduce on: master
  • Backport needed: 26.5, 26.4, 26.3, 26.2, 26.1

Suggested next step

The bug reproduces as far back as the oldest tested release (26.1) and the only converged boundary is topically unrelated — this is a long-standing / pre-existing bug in comp-query-execution, not a recent regression; no introducing PR exists within the tested range.

Component: comp-query-execution
Severity: P2

cc fix PR author: @vdimir
cc fix PR reviewer: @antaljanosbenjamin


ClickGap · Finding: phase_d_pr108807

Triage metadata

  • Primary component: comp-query-execution
  • Secondary components: comp-testing
  • Component owners (comp-query-execution): @KochetovNicolai @al13n321

@clickgapai

Copy link
Copy Markdown
Contributor

Hi — this looks like a backport candidate for 26.1, 26.2, 26.3, 26.4, 26.5. I ran this PR's own regression test against each branch's binary and the bug reproduces there (the fix isn't present yet).

zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…totals-const-swap

Fix WITH TOTALS losing constant column values in JOIN
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.

5 participants