Skip to content

Keep the table qualifier in the column names of APPLY (f, 'prefix') - #122702

Merged
qoega merged 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-122327-apply-prefix-join-qualifier
Sep 30, 2026
Merged

qoega merged 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-122327-apply-prefix-join-qualifier

Conversation

@groeneai

@groeneai groeneai commented Sep 28, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #122327
Related: #121339
Related: #109223

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

Description

SELECT * APPLY (f, 'prefix') over a join gave two different columns one name, so a query that reads the result by name returned the wrong column:

SELECT * FROM (SELECT * APPLY (toString, 'f_') FROM (SELECT 1 AS a) AS l CROSS JOIN (SELECT 2 AS a) AS r);
-- f_a  f_a
-- 1    1      <- the query itself returns 1  2

Other shapes failed instead: with Code: 352 AMBIGUOUS_COLUMN_NAME when the two results are different constants (toTypeName, for example), and with Code: 44 ILLEGAL_COLUMN for remote() over a prefixed join.

The analyzer names a column that another table expression of the scope could also bind with a qualifier (r.a), and APPLY f wraps that name (toString(r.a)). #109223 made the analyzer honor the prefix, but built the name from the short column name, so l.a and r.a both became f_a. The prefix now goes on the same name that * and APPLY f use (f_r.a), and a REPLACE result keeps the replaced column's name. Names change only where the unprefixed name is qualified (joins, a SELECT alias equal to a column name, ARRAY JOIN, tuple.*), which is why 04401 now expects f_x.a.

Found by AST fuzzer (amd_release, oracle) on #121339 ("Subquery wrap oracle mismatch", report); the same shape also failed in the fuzzer runs of two other PRs.

The new test fails before this change (wrong values and Code: 352) and passes after it, also in 50 randomized runs; the stateless tests that use APPLY pass. Not for changelog: #109223 is only on master, not in 26.9.


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

Version info

  • Merged into: 26.10.1.1086-master (included in 26.10 and later)

groeneai and others added 2 commits September 28, 2026 15:06
The analyzer names a matched column whose bare name also binds to another
table expression, an alias or an ARRAY JOIN expression with a qualified
projection name (`r.a`), and an unprefixed `APPLY f` wraps that name
(`toString(r.a)`). ClickHouse#109223 made the analyzer honor the `APPLY` name prefix,
but seeded the prefixed name from the short column name, so `l.a` and `r.a`
of a join both became `f_a`. Two different columns under one name then
collide wherever the name is the key: `SELECT * FROM (...)` over the query
reads the first `f_a` twice (wrong result, issue ClickHouse#122327 and the AST fuzzer
"Subquery wrap oracle mismatch" on ClickHouse#121339 and in two other PRs), two
different constants under one name fail the block structure check with
`Code: 352 AMBIGUOUS_COLUMN_NAME`, and `remote()` over such a query fails
with `Code: 44`.

Seed the prefixed name from the column's projection name instead: `f_r.a`,
the same base `*` and `APPLY f` use, and the same base a chained prefix after
an unprefixed transformer already had (`q_toString(r.a)`). Names change only
where the unprefixed name is already qualified: joins, a SELECT alias equal
to a column name, an ARRAY JOIN clash and `tuple.*` (`f_tup.x`); a plain
single-table `*` keeps `f_a`. A `REPLACE` result keeps the replaced column's
name, so a `REPLACE (untuple(a) AS a)` expansion resets the accumulator to it
(`a.1`, not `r.a.1`).

The new test covers JOIN USING/ON, two tables, three joins, qualified and
tuple matchers, join_use_nulls, a lambda, chained prefixes, named untuple and
COLUMNS, with single-table and REPLACE controls. 04401 now expects `f_x.a`
for a column qualified by a clashing SELECT alias, like its unprefixed
`toString(x.a)`.

Closes ClickHouse#122327

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Condense the seed comment of the APPLY prefix accumulator to one line.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-review-human PR origin: human review feedback on a groeneai PR labels Sep 28, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 0 findings on the code and tests), plus my own review of the same tree in two rounds.

# Sev Finding Verdict Evidence / action
1 ⚠️ The commit message linked two unrelated PRs where the same fuzzer failure fired AGREE, fixed the message now gives a count; the tree did not change
2 ⚠️ The commit message said single-table names do not change, but the clashing-alias shape (04401) and tuple.* do AGREE, fixed the message lists the shapes whose names change
3 💡 The new test's header uses the analyzer term "projection name" noted not blocking
4 💡 The REPLACE control also pins values, and the single-table control repeats a line of 04401 noted both are controls; not blocking

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-9:20260928-152400

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. SELECT * FROM (SELECT * APPLY (toString, 'f_') FROM (SELECT 1 AS a) AS l CROSS JOIN (SELECT 2 AS a) AS r) returns f_a f_a / 1 1 on every run on master; without the wrapper it returns 1 2.
b Root cause explained? resolveMatcher built the prefixed APPLY name from the short column name instead of the qualified projection name (r.a), so l.a and r.a both became f_a.
c Fix matches root cause? Yes. The prefix now starts from the name * and APPLY f already use, the only point where the qualifier was dropped; no collision check, no guard.
d Test intent preserved / new tests added? New 05291_apply_prefix_join_qualified_name (joins, qualified and tuple matchers, join_use_nulls, lambda, chained prefixes, untuple, COLUMNS, single-table and REPLACE controls); 04401 now expects f_x.a, like the unprefixed toString(x.a).
e Both directions demonstrated? Yes, through tests/clickhouse-test: both tests pass with the fix; without it 05291 fails (Code: 352) and 04401 differs on exactly the one updated line.
f Fix is general across code paths? Checked *, t.*, tuple.*, COLUMNS, the USING key, join_use_nulls, group_by_use_nulls, lambda, chained prefixes, untuple, REPLACE, EXCEPT and remote().
g Fix generalizes across inputs (params/datatypes/wrappers)? Names do not depend on types (checked Nullable, LowCardinality, Array, Tuple, one to three joins); single-table queries keep f_a.
h Backward compatible? (maintainer-approved exception only) Only prefixed names in a scope that qualifies them change, and that naming exists only on master since #109223; no setting or format change.
i Invariants and contracts preserved? The name list stays in step with the columns; the private-copy rule and REPLACE/EXCEPT naming are unchanged; 50/50 randomized runs pass, and so do the stateless tests that use APPLY.

Session id: cron:clickhouse-impl-slot-6:20260928-130800

@clickhouse-gh

clickhouse-gh Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [3c957a8]

Summary: ✅


AI Review

Summary

This PR fixes named APPLY column generation so a prefixed result keeps the same qualified projection base that plain * and unprefixed APPLY already use. That removes duplicate names like f_a / f_a in joined scopes and keeps outer reads, REPLACE, and chained transformers aligned with the existing projection-name rules. I did not find a correctness, compatibility, or coverage issue that justifies a review comment on the current diff.

Final Verdict

✅ No blocking issues found.

LLVM Coverage Report

Measured on commit 3c957a8.

Metric Baseline Current Δ
Lines 87.90% 87.90% +0.00%
Functions 91.80% 91.70% -0.10%
Branches 79.30% 79.30% +0.00%

Changed lines: Changed C/C++ lines covered: 13/13 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Sep 28, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). label Sep 28, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 28, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 3c957a841 with master daa7bdbd3 (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, 20 s compile time in total, 7 of them have a recent master baseline.

Job report

@qoega qoega self-assigned this Sep 30, 2026
@qoega
qoega added this pull request to the merge queue Sep 30, 2026
Merged via the queue into ClickHouse:master with commit e611107 Sep 30, 2026
149 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 30, 2026
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). groeneai-origin-review-human PR origin: human review feedback on a groeneai PR 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.

SELECT * EXCEPT/APPLY/REPLACE with JOIN USING gives different results when wrapped in subquery

4 participants