Skip to content

Preserve quoting for TOP column names in metadata - #118754

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
kirillgarbar:fix-view-cte-alias-serialization
Sep 12, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
kirillgarbar:fix-view-cte-alias-serialization

Conversation

@kirillgarbar

@kirillgarbar kirillgarbar commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

There is a bug in parser when TOP is used as column name.

create table test_3 (TOP String) engine = MergeTree() order by TOP;

create view test_4 (c String) as with A as (select `TOP` AS c from test_3)  select c from A;

show create test_4;
Output:

Received exception from server (version 26.6.1):
Code: 62. DB::Exception: Received from localhost:9000. DB::Exception: Syntax error (in file store/38e/38efbf7e-17a6-40d9-a310-d1392812ae57/test_4.sql): failed at position 116 (AS) (line 7, col 20): AS c
        FROM default.test_3
    )
SELECT c
FROM
A
. Expected one of: token, OpeningRoundBracket, number. (SYNTAX_ERROR)
(query: show create test_4;)

After fix:

SHOW CREATE TABLE test_4
FORMAT Raw

Query id: 682faeb3-fa8e-48ca-a855-ee22716fd789

CREATE VIEW default.test_4
(
    `c` String
)
AS WITH
    A AS
    (
        SELECT `TOP` AS c
        FROM default.test_3
    )
SELECT c
FROM A

Also covers #106817

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 SYNTAX_ERROR when loading views, materialized views, or projections whose SELECT list starts with a column named TOP.


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

Version info

  • Merged into: 26.9.1.1313 (included in 26.9 and later)

@clickhouse-gh clickhouse-gh Bot added the comp-sql-syntax SQL/grammar parsing, AST nodes, syntax-level features. label Sep 8, 2026
@evillique evillique self-assigned this Sep 11, 2026
@evillique evillique added the can be tested Allows running workflows for external contributors label Sep 11, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [dddfc6e]

Summary: ✅


AI Review

Summary

This PR adds TOP to the identifier formatter's must-quote list so newly serialized view / projection metadata does not emit bare TOP and then fail to parse. The write-side change itself is reasonable, but it does not satisfy the broader bug-fix contract for already persisted metadata: existing objects that already have SELECT TOP AS ... in their .sql definition still hit the same parser error on load.

Findings

❌ Blockers

  • [src/IO/WriteHelpers.cpp:134] The change only affects formatting of newly written metadata. Existing broken view / materialized-view / projection definitions are still reparsed verbatim by DatabaseOnDisk::parseQueryFromMetadata, and ParserSelectQuery still interprets bare TOP after SELECT as the TOP n clause. Upgrading therefore does not actually fix loading the objects users already created on affected releases.
    Suggested fix: add a read-side compatibility path or metadata migration for old definitions, or narrow the PR claim to newly created objects on the patched build.
Tests
  • ⚠️ Add the smallest focused regression that proves the promised load path: attach or restart with a metadata file produced by an older build containing SELECT TOP AS ..., and cover at least one persisted object type from the changelog claim.
Final Verdict

❌ Changes requested: the patch prevents writing new broken metadata, but it does not fix loading already persisted broken metadata, which is the user-visible compatibility problem described by the PR.

LLVM Coverage Report

Measured on commit dddfc6e.

Metric Baseline Current Δ
Lines 88.80% 88.80% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 81.20% 81.10% -0.10%

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

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 11, 2026
Comment thread src/IO/WriteHelpers.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing dddfc6e7f with master 6bdba314e (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

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

Job report

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 12, 2026
Merged via the queue into ClickHouse:master with commit 2618454 Sep 12, 2026
174 checks passed
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 12, 2026
@clickgapai

Copy link
Copy Markdown
Contributor
Reproducer
CREATE TABLE top_src (`TOP` String) ENGINE = MergeTree ORDER BY `TOP`;

CREATE VIEW top_view (c String) AS
WITH a AS (SELECT `TOP` AS c FROM top_src)
SELECT c FROM a;

SHOW CREATE VIEW top_view;
-- Code: 62. DB::Exception: Syntax error (in file store/.../top_view.sql):
--   failed at position 116 (AS) ... Expected one of: token, OpeningRoundBracket, number. (SYNTAX_ERROR)
-- The view's metadata was written with a bare TOP, which re-parses as the `TOP <n>` clause.

▶ Run on ClickHouse Fiddle

Results:

  • Reproduces on: 26.8, 26.7, 26.6, 26.3
  • Does not reproduce on: master
  • Tested versions: master, 26.8, 26.7, 26.6, 26.3
  • Also tested (unsupported, context only): 26.2 reproduces, 24.8 reproduces, 23.8 reproduces, 22.8 reproduces
  • ✓ Fix is on master — this PR's change fixes the bug on master. The releases listed above still have the bug and need the backport.

Backport the fix to 26.8, 26.7, 26.6, 26.3.

CC component owner: @alexey-milovidov @vitlibar

Analysis metadata

comp-sql-syntax · Severity P2 · Finding phase_d_pr118754

  • Verified against master build 044e813459c6 (aarch64)
Introducing change: not identified
  • No clean release baseline found and no lower bound established — source-build bisect is required to pin the introducing PR (not yet performed).
  • Walked 6 release(s) below the supported window: still reproduces down to 25.8.
  • Secondary components: comp-testing

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-sql-syntax SQL/grammar parsing, AST nodes, syntax-level features. 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