Skip to content

Quote identifiers that the parser reads back as literals - #116802

Merged
alexey-milovidov merged 2 commits into
masterfrom
quote-identifiers-parsed-as-literals
Aug 30, 2026
Merged

alexey-milovidov merged 2 commits into
masterfrom
quote-identifiers-parsed-as-literals

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Aug 27, 2026 •

Copy link
Copy Markdown
Member

Related: #116645

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

A column named inf, infinity, nan, true or false was formatted without quotes, so a reference to it was read back as a constant instead of a column. A table using such a column in its sorting key had unparseable metadata written for it, and the next server start failed to load the table and with it the whole database.

Description

Found by the AST fuzzer on master, AST fuzzer (amd_debug), STID 1941-1bfa:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=6e6a2a508889e7e97149298d3490ac139f8ae320&name_0=MasterCI&name_1=AST%20fuzzer%20%28amd_debug%29

The two ASTs of the round-trip check differ in exactly one node — an identifier in the
sorting key came back as a float literal:

-  Identifier_NaN
+  Literal_Float64_nan

inf, infinity, nan, true and false are valid identifiers, but the parser reads a
bare word of any of those names back as a literal: ParserNumber hands a bare word to
strtod, which accepts inf, infinity and nan, and true/false are parsed as Bool
literals. writeProbablyQuotedStringImpl quoted only names that are not valid identifiers
at all, so such a name was formatted unquoted and silently turned into a constant. null
was already rejected by isValidIdentifier for the same reason; this adds the remaining
names next to the keywords that are already listed there.

This is not limited to the formatting consistency check of debug builds. On a release build
the statement is accepted and the metadata written for the table cannot be parsed back:

$ clickhouse client -q "CREATE TABLE d.t (\`inf\` Int32, x Int32) ENGINE = MergeTree ORDER BY \`inf\`"   # accepted
$ cat store/.../t.sql
ATTACH TABLE _ UUID '...' (`inf` Int32, `x` Int32) ENGINE = MergeTree ORDER BY inf
$ # restart
Code: 44. DB::Exception: Sorting key cannot contain constants: Cannot attach table `d`.`t`
  from metadata file ... (ILLEGAL_COLUMN)

The whole database fails to load, not just the table.

Only names that are parsed as a literal are quoted; infx, nanx and inf_1 are left
unquoted, and no function, table engine or data type family has a colliding name.

The test uses DETACH + ATTACH, which re-reads the metadata file and so exercises the
same path as a server start. It fails before this change with the Sorting key cannot contain constants error above.

This is the same failure class as #116645 (metadata written in a form the server cannot read
back) but a different trigger, and it is the remaining Inconsistent AST formatting failure
on master once #116645 lands.


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

`inf`, `infinity`, `nan`, `true` and `false` are valid identifiers, but the
parser reads a bare word of any of those names back as a literal rather than an
identifier: `ParserNumber` hands a bare word to `strtod`, which accepts `inf`,
`infinity` and `nan`, and `true`/`false` are parsed as `Bool` literals.
`writeProbablyQuotedStringImpl` quoted only names that are not valid identifiers
at all, so a reference to a column with such a name was formatted unquoted and
silently turned into a constant. `null` was already rejected by
`isValidIdentifier` for the same reason.

This is not limited to the formatting consistency check of debug builds. A table
with a column named `inf` gets `ORDER BY inf` written to its metadata, and the
next server start fails to load the table, and with it the whole database:

    CREATE TABLE t (`inf` Int32, x Int32) ENGINE = MergeTree ORDER BY `inf`;
    -- metadata: ATTACH TABLE _ (`inf` Int32, `x` Int32) ENGINE = MergeTree ORDER BY inf
    -- next start: Code: 44. DB::Exception: Sorting key cannot contain constants

Found by the AST fuzzer in `AST fuzzer (amd_debug)`, STID `1941-1bfa`, where an
`Identifier_NaN` in the sorting key came back as `Literal_Float64_nan`:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=6e6a2a508889e7e97149298d3490ac139f8ae320&name_0=MasterCI&name_1=AST%20fuzzer%20%28amd_debug%29

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

clickhouse-gh Bot commented Aug 27, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c7626bc]

Summary: ✅


AI Review

Summary

This PR updates the generic identifier formatter so names that the expression parser reads back as literals (inf, infinity, nan, true, false) are quoted, and adds a stateless regression test that covers both direct AST formatting and a MergeTree metadata DETACH/ATTACH round-trip. I did not find a remaining correctness or compatibility gap in the changed paths.

Missing context / blind spots
  • ⚠️ The only CI artifact currently linked on the PR is Build profile diff; there was no local build in this workspace to execute 05045_identifier_parsed_as_literal_quoting.sql, so I could not independently observe the new stateless test pass.
Final Verdict

✅ No blocking or major findings from code review.

LLVM Coverage Report

Measured on commit c7626bc.

Metric Baseline Current Δ
Lines 88.50% 88.50% +0.00%
Functions 92.00% 92.00% +0.00%
Branches 80.70% 80.80% +0.10%

Changed lines: Changed C/C++ lines covered: 12/12 (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 Aug 27, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 28, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing c7626bc8b with master 5742d8296 (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

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

Job report

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Perfect.

@alexey-milovidov alexey-milovidov self-assigned this Aug 30, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Aug 30, 2026
Merged via the queue into master with commit e49f293 Aug 30, 2026
175 checks passed
@alexey-milovidov
alexey-milovidov deleted the quote-identifiers-parsed-as-literals branch August 30, 2026 08:41
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 30, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

@alexey-milovidov — ClickGap found the following in this PR:

Close anything that's wrong or already addressed.

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.

3 participants