Skip to content

Fix ASTDatabaseOrNone: always backquote database name to prevent NONE keyword collision - #111527

Open
ybapat wants to merge 8 commits into
ClickHouse:masterfrom
ybapat:fix-ast-database-or-none-quoting
Open

ybapat wants to merge 8 commits into
ClickHouse:masterfrom
ybapat:fix-ast-database-or-none-quoting

Conversation

@ybapat

@ybapat ybapat commented Jul 23, 2026 •

Copy link
Copy Markdown

Closes: #111403

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry:

Fixed a bug where a user's default database was silently lost after a serialization round trip when the database was literally named NONE. The formatter now always back-quotes the database name in DEFAULT DATABASE clauses, preventing the parser from misreading the name as the "no default database" sentinel.

What this fixes

ASTDatabaseOrNone::formatImpl called backQuoteIfNeed(database_name). Since NONE is a lexically valid identifier, it was emitted unquoted — serialized as DEFAULT DATABASE NONE. On read-back, ParserDatabaseOrNone::parseImpl matches the keyword NONE before trying to parse an identifier, so the name was consumed as the sentinel (meaning "no default database") and the user's default database was silently dropped.

The fix

Replace backQuoteIfNeed with backQuote (unconditional) in ASTDatabaseOrNone::formatImpl. This ensures the database name is always backtick-quoted in the serialized output (e.g. DEFAULT DATABASE `NONE`). The parser's parseIdentifierOrStringLiteral correctly handles backtick-quoted identifiers, so the round trip is now safe for NONE and for any future keyword that might collide.

Why unconditional quoting (not a targeted NONE check)

NONE is not the only possible collision: any database name that matches a keyword the surrounding grammar could intercept is a latent risk. Unconditional quoting of persisted identifiers is the defensive and idiomatic approach, matching how many other ClickHouse AST nodes serialize names that must round-trip.

Test

Updated tests/queries/0_stateless/01939_user_with_default_database — the reference output now shows DEFAULT DATABASE `NONE` (backtick-quoted) instead of DEFAULT DATABASE NONE (unquoted). No other test reference files contain DEFAULT DATABASE, so there are no other affected tests.


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

@CLAassistant

CLAassistant commented Jul 23, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@clickhouse-gh

clickhouse-gh Bot commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [a5dd52d]

Summary: ✅


AI Review

Summary

The ASTDatabaseOrNone change itself is correct: always back-quoting the stored database name fixes the DEFAULT DATABASE NONE round-trip, and the updated stateless test now exercises the real DiskAccessStorage reload path. This PR is still not ready to merge because it also adds an unrelated Settings.cpp history record that duplicates the introduction of join_runtime_filter_min_probe_rows and makes the compatibility metadata incorrect.

Findings

⚠️ Majors

  • [src/Core/Settings.cpp:10415-10417] join_runtime_filter_min_probe_rows is now recorded as a new setting in both 26.8 and 26.7, but system.settings_changes is supposed to list the introduction version and later default changes, not duplicate the same introduction across adjacent releases. Keeping both entries makes the compatibility history and generated documentation lie about when this setting changed. Suggested fix: drop the added 26.7 record and keep only the existing 26.8 introduction entry.
Final Verdict

Request changes: the ASTDatabaseOrNone fix looks good, but the unrelated duplicated compatibility-history record in src/Core/Settings.cpp should be removed before merge.

LLVM Coverage Report

Measured on commit a5dd52d.

Metric Baseline Current Δ
Lines 88.20% 88.20% +0.00%
Functions 91.70% 91.70% +0.00%
Branches 79.50% 79.50% +0.00%

Changed lines: Changed C/C++ lines covered: 9/11 (81.82%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 23, 2026
Comment thread src/Parsers/ASTDatabaseOrNone.cpp Resolved
@ybapat

ybapat commented Jul 23, 2026

Copy link
Copy Markdown
Author

Addressed the AI review concern: I've extended 01939_user_with_default_database.sh to exercise the full DiskAccessStorage serialize/deserialize round trip:

  1. Set user's default database to `NONE` (the database named NONE)
  2. SYSTEM RELOAD USERS — forces ClickHouse to re-read user definitions from /var/lib/clickhouse/access/
  3. Verify SHOW CREATE USER still shows DEFAULT DATABASE `NONE` (not dropped)
  4. Verify SELECT currentDatabase() for that user returns NONE (not default)

Before the fix: serializeAccessEntity wrote DEFAULT DATABASE NONE (unquoted) → ParserDatabaseOrNone re-parsed it as the sentinel on reload → default database silently dropped → currentDatabase() returned default.

After the fix: serialized as DEFAULT DATABASE `NONE` → parsed correctly as a database name → currentDatabase() returns NONE.

Updated the .reference file accordingly (6 lines instead of 4).

ybapat and others added 2 commits July 24, 2026 00:28
…tory 26.7

The setting was added to Settings.cpp on master but the SettingsChangesHistory
entry was not yet in this branch, causing the upgrade check to fail.
The join_runtime_filter_min_probe_rows history entry was added in a prior
commit and is byte-identical to master, but the Upgrade check reused a
cached amd_release binary built before that entry existed, so it still
reported the setting as unreflected in the settings changes history.
Rewording the description busts the build digest to trigger a fresh
amd_release build without altering the validated {name, prev, new} tuple.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Comment thread src/Core/SettingsChangesHistory.cpp Outdated
{"skip_unavailable_shards_mode", "unavailable_or_table_missing", "unavailable_or_table_missing", "New setting to control which exceptions from a remote shard are ignored when `skip_unavailable_shards` is enabled. The default matches the historical behavior: a shard whose table is missing is treated as unavailable."},
{"use_text_index_tokens_cache", false, true, "Enabled the text index tokens cache globally."},
{"use_text_index_header_cache", false, true, "Enabled the text index header cache globally."},
{"join_runtime_filter_min_probe_rows", 0, 1000, "New setting to control the minimum probe-side size for installing JOIN runtime filters. Previously there was no such threshold, so the previous value is 0, meaning runtime filters were always installed."},

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.

system.settings_changes now claims that join_runtime_filter_min_probe_rows was introduced in both 26.7 and 26.8. That 26.7 entry was deliberately moved out in #111671 once master rolled to 26.8, so re-adding it here recreates duplicate history for the same setting change. Please drop the 26.7 copy and keep this setting only in the 26.8 block.

@clickhouse-gh

clickhouse-gh Bot commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 49.90% 86.20% +36.30%
Functions 63.90% 92.00% +28.10%
Branches 38.80% 78.40% +39.60%

Changed lines: Changed C/C++ lines covered: 8/10 (80.00%) · Uncovered code

Full report · Diff report

…-none-quoting

Resolve the conflict with the move of the settings changes history into the setting declarations
(ee6b44f): the history records this PR added to `SettingsChangesHistory.cpp` now live as
trailing arguments of the `DECLARE` of each setting.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@maxknv maxknv added the external Author is not a member of the ClickHouse organization label Nov 2, 2026

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-sql-syntax SQL/grammar parsing, AST nodes, syntax-level features. external Author is not a member of the ClickHouse organization pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

5 participants