Skip to content

Do not reuse a server constant built in another scope. - #123523

Open
ianton-ru wants to merge 2 commits into
ClickHouse:masterfrom
ianton-ru:fix-union-hostname-server-constant
Open

ianton-ru wants to merge 2 commits into
ClickHouse:masterfrom
ianton-ru:fix-union-hostname-server-constant

Conversation

@ianton-ru

@ianton-ru ianton-ru commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

hostName captured Context::isDistributed from the first UNION branch and was then folded on a cluster branch, so the GROUP BY headers no longer matched.

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):

Fix UNION of a local query and cluster() (or another cluster function) throwing NUMBER_OF_COLUMNS_DOESNT_MATCH or THERE_IS_NO_COLUMN on GROUP BY hostName. A server constant built on the local branch was reused for the cluster branch and folded to the initiator value. Each scope now builds its own value, so shards report their own hostName, serverUUID, tcpPort, and the other server constants.


Workflow [PR]
Sync PR [sync-upstream/pr/123523]

`hostName` captured `Context::isDistributed` from the first `UNION` branch and was then folded on a cluster branch, so the `GROUP BY` headers no longer matched.

Co-authored-by: Cursor <[email protected]>
@ianton-ru
ianton-ru marked this pull request as ready for review October 2, 2026 12:14
@clickhouse-gh clickhouse-gh Bot added comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). external Author is not a member of the ClickHouse organization labels Oct 2, 2026
@evillique evillique self-assigned this Nov 2, 2026
@evillique evillique added the can be tested Allows running workflows for external contributors label Nov 2, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [bdf2a85]

Summary: ❌

job_name test_name status info comment
Finish Workflow FAIL
python3 ./ci/jobs/scripts/workflow_hooks/can_be_merged.py --review-threads FAIL

AI Review

Summary

The PR prevents server constants built for a local query from being reused in a distributed UNION branch. The added regression passed in CI, but disabling the cache entirely introduces inconsistent results within a single scope. One reproduced blocker remains.

Findings

❌ Blockers

  • src/Analyzer/Resolve/resolveFunction.cpp:3279: Preserve one captured server-constant value within each scope. uptime captures the current uptime at construction, so bypassing the cache gives repeated calls and alias expansions different values if scalar-subquery evaluation takes time. With the PR's fast-test binary, this query returns u = 0, delay = 0, and u_string = '2':

    SELECT uptime() AS u, (SELECT sleep(2)) AS delay, toString(u) AS u_string;

    Replacing the last expression with uptime() AS u also throws AMBIGUOUS_COLUMN_NAME because the constants disagree. Cache server constants by scope/context while retaining sharing within each scope.

Tests
  • ⚠️ Add a focused regression proving repeated uptime calls and alias expansions retain the same captured value across analysis-time scalar-subquery execution. The existing local/distributed hostName test does not exercise changing snapshots.
Final Verdict

❌ Block — preserve within-scope sharing of server constants and add regression coverage.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Nov 15, 2026
if (function && !function->isDeterministic() && !function->isStateful()
&& function->isDeterministicInScopeOfQuery())
&& function->isDeterministicInScopeOfQuery()
&& !function->isServerConstant())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[Blocker] Preserve server-constant sharing within each scope

isServerConstant also covers uptime, which captures Context::getUptimeSeconds in its constructor. Excluding it from the cache entirely gives repeated expressions and alias expansions different snapshots within the same scope. Reproduced with this PR's fast-test binary:

SELECT uptime() AS u, (SELECT sleep(2)) AS delay, toString(u) AS u_string;

This returns u = 0, delay = 0, and u_string = '2': converting the alias changes its value. Replacing the last expression with uptime() AS u also makes this valid query throw AMBIGUOUS_COLUMN_NAME because the constants disagree. A slow scalar subquery is enough to trigger this on a running server; the explicit delay just makes it reproducible.

Keep server constants in a cache keyed by their scope/context so repeated uses within a scope share the captured value and local/distributed branches get separate instances. Please add a regression covering this within-scope consistency.

@clickhouse-gh

clickhouse-gh Bot commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing bdf2a856e with master cbd8a0ffb (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 18 s compile time in total, 7 of them have a recent master baseline.

Job report

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). external Author is not a member of the ClickHouse organization pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants