Skip to content

Fix AST formatting roundtrip for FROM and VALUES used as function names - #102338

Merged
alexey-milovidov merged 10 commits into
masterfrom
fix-ast-formatting-from-keyword
Apr 25, 2026
Merged

alexey-milovidov merged 10 commits into
masterfrom
fix-ast-formatting-from-keyword

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Apr 10, 2026 •

Copy link
Copy Markdown
Member

The BuzzHouse fuzzer found that when FROM or VALUES are used as function names, the AST formatter outputs them without backtick-quoting. When parsing the formatted query back, the parser interprets these as SQL clause keywords instead of function names, causing a SYNTAX_ERROR.

Add FROM and VALUES to the list of keywords that must be backtick-quoted in writeProbablyQuotedStringImpl, alongside the existing DISTINCT, ALL, TABLE, and SELECT entries.

https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=101054&sha=1a927fce3bc0eb606438ccc418daafbd3060ad2b&name_0=PR&name_1=BuzzHouse%20%28amd_debug%29

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

Version info

  • Merged into: 26.5.1.78

… names

The BuzzHouse fuzzer found that when `FROM` or `VALUES` are used as
function names, the AST formatter outputs them without backtick-quoting.
When parsing the formatted query back, the parser interprets these as
SQL clause keywords instead of function names, causing a SYNTAX_ERROR.

Add `FROM` and `VALUES` to the list of keywords that must be
backtick-quoted in `writeProbablyQuotedStringImpl`, alongside the
existing `DISTINCT`, `ALL`, `TABLE`, and `SELECT` entries.

https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=101054&sha=1a927fce3bc0eb606438ccc418daafbd3060ad2b&name_0=PR&name_1=BuzzHouse%20%28amd_debug%29
@clickhouse-gh

clickhouse-gh Bot commented Apr 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e0d8287]

Summary: ✅


AI Review

Summary

This PR fixes AST formatting roundtrip for keyword-like function identifiers by ensuring FROM and VALUES are quoted in writeProbablyQuotedStringImpl, and updates stateless references accordingly. I did not find correctness, safety, or performance issues in the code changes.

ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅
No large/binary files ✅
Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Apr 10, 2026

@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.

Looks good.

alexey-milovidov and others added 3 commits April 10, 2026 06:49
…ename test to avoid number conflict

- Update `02868_select_support_from_keywords.reference`: column `from` is now
  backtick-quoted in SELECT lists (correct behavior — unquoted `from` would be
  ambiguous with the FROM clause keyword)
- Update `02184_default_table_engine.reference`: `values(...)` function call
  is now backtick-quoted as `\`values\`(...)` in SHOW CREATE output
- Update `03786_AST_formatting_inconsistencies_in_debug_check.reference`:
  `VALUES(...)` table function is now backtick-quoted in formatQuery output
- Rename test 04093 → 04094 to avoid conflict with
  `04093_merge_function_custom_defaults_remote` merged from master

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
…robablyQuotedStringImpl`

The `strncasecmp` call is safe because the size is explicitly passed as the
third argument, but clang-tidy 21 does not recognize that the second
`data()` argument is length-bounded. Suppress with NOLINT.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
alexey-milovidov added a commit that referenced this pull request Apr 12, 2026
…_random` to fix timeout

The test inserts 4 × 1,000,000 rows and runs FINAL queries. With
randomized settings (especially `max_threads=1`) in a debug build,
this exceeds the 600s timeout. Reduce to 200,000 rows per insert —
still sufficient for skip index correctness testing with 3,125
granules per part (index_granularity=64).

https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=102338&sha=41026906138c371d1fe6cd79676d664f8abd5a4f&name_0=PR&name_1=Stateless%20tests%20%28amd_debug%2C%20flaky%20check%29
#102338

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
alexey-milovidov added a commit that referenced this pull request Apr 12, 2026
The test was timing out (>600s) under MSan with randomized settings.
The `.sql.j2` template contained no Jinja2 syntax and the previous
`Random settings limits` approach only constrained `index_granularity`
and `index_granularity_bytes`, leaving other problematic settings
(`merge_max_block_size`, `prewarm_mark_cache`, `prewarm_primary_key_cache`)
unconstrained.

Convert from `.sql.j2` to `.sql` (no template features used) and pin
the same performance-critical MergeTree settings as the sibling
`_2_wide_merge_tree.sql` test: `index_granularity`, `merge_max_block_size`,
`prewarm_*_cache`, and `auto_statistics_types`. Also override
`max_threads` and `min_bytes_to_use_direct_io` at session level.

https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=102338&sha=41026906138c371d1fe6cd79676d664f8abd5a4f&name_0=PR&name_1=Stateless%20tests%20%28amd_msan%2C%20WasmEdge%2C%20parallel%2C%201%2F2%29
#102338

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
alexey-milovidov added a commit to alexey-milovidov/ClickHouse that referenced this pull request Apr 12, 2026
…_random`

The test timed out (600s) in the debug flaky check when the randomizer
set `--max_threads 1` — 4 million rows with FINAL queries is too slow
for a debug build with a single thread.

https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=102338&sha=41026906138c371d1fe6cd79676d664f8abd5a4f&name_0=PR&name_1=Stateless%20tests%20%28amd_debug%2C%20flaky%20check%29
ClickHouse#102338

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@george-larionov george-larionov self-assigned this Apr 13, 2026

@george-larionov george-larionov 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

Comment thread src/IO/WriteHelpers.cpp
/// These keywords cause parsing ambiguity when used as function or identifier names
/// because the parser consumes them as clause-starting keywords.
&& !isCaseInsensitiveEqual(s, "from")
&& !isCaseInsensitiveEqual(s, "values"))

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.

This looks much nicer than the old implementation

@clickhouse-gh

clickhouse-gh Bot commented Apr 25, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 83.80% 83.80% +0.00%
Functions 91.10% 91.10% +0.00%
Branches 76.20% 76.20% +0.00%

Changed lines: 100.00% (11/11) · Uncovered code

Full report · Diff report

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Apr 25, 2026
Merged via the queue into master with commit b32eb4f Apr 25, 2026
165 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-ast-formatting-from-keyword branch April 25, 2026 12:51
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Apr 25, 2026
@SaltTan

SaltTan commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

I have a table with a Tuple(values String).
After an upgrade new replica could not download older parts because of different columns hash.

Code: 40. DB::Exception: Part all_900_1859_113 from replica-3 has different columns hash (it may rarely happen on race condition with KILL MUTATION). (CHECKSUM_DOESNT_MATCH)

I was lucky that the table was small and I could afford OPTIMIZE FINAL :)

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

Labels

pr-ci 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.

4 participants