Repository navigation
Reject a PRIMARY KEY that a table created from a table function cannot format back - #116645
Conversation
…t format back A PRIMARY KEY declared in the column list of CREATE TABLE ... AS <table function> was normalized into a synthesized storage definition, leaving as_table_function and storage both set on the ASTCreateQuery. A table built from a table function has no storage definition of its own, which is the same rule that already rejects an explicit ENGINE, so formatting printed the synthesized node after the table function, where the grammar has no production for it. Two consequences followed. In debug and sanitizer builds the format-and-reparse check raised 'Inconsistent AST formatting', which is how the AST fuzzer surfaced this. In release builds nothing on the CREATE path rejected the combination, so the table was created and DatabaseOnDisk wrote its metadata through the same formatter; on the next start the database failed to load with Code: 62 SYNTAX_ERROR and the server did not come up. Measured on 26.8.1.2016 (official build), and the formatter emits the same unparseable text on every release back to 25.8 LTS. Reject the statement at parse time instead, mirroring the guard ParserCreateViewQuery already carries for views (390df2a). The same mutual-exclusion check is added to ASTCreateQuery::readJSON, where a clickhouse_json payload carrying both members reached the identical unparseable metadata and, unlike the SQL path, did so silently even in a debug build. Tests go in gtest_tree_hash_completeness.cpp next to the view case. The stateless arms that 390df2a added were removed again by 6336358, leaving the gtest as the convention for this defect class.
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 1 finding; 1 full pass,
Also noted, not blocking: my own commit message does not wrap identifiers in inline code Session id: cron:clickhouse-review-slot-11:20260827-043500 |
|
Workflow [PR], commit [8977ecc] Summary: ✅
AI ReviewSummaryThis PR closes the parser/formatter hole where a column-list Final Verdict✅ Approve. The SQL-side guard in src/Parsers/ParserCreateQuery.cpp and the AST-JSON guard in src/Parsers/ASTCreateQuery.cpp cover the two reachable constructors of the broken LLVM Coverage ReportMeasured on commit 8977ecc.
Changed lines: Changed C/C++ lines covered: 36/36 (100.00%) · Uncovered code |
Pre-PR validation gate (click to expand)
Suites: Session id: cron:clickhouse-impl-slot-5:20260827-034700 |
|
cc @evillique @alexey-milovidov, could you review this? A column-list |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Compile time of recompiled translation units8 translation units recompiled, 12 s compile time in total, 8 of them have a recent master baseline. |
Reproducer-- accepted, PRIMARY KEY silently ignored
CREATE TABLE t (x UInt64 PRIMARY KEY) AS numbers(5);
-- the form written to metadata does not parse back:
SELECT formatQuery(formatQuery('CREATE TABLE t (x UInt64 PRIMARY KEY) AS numbers(5)'));
-- Code: 62. Syntax error: failed at position 49 ('PRIMARY') (line 5, col 1): PRIMARY KEY tuple(x)
-- On a server, the same metadata makes the database fail to load on the next start.Results:
Backport the fix to CC component owner: @alexey-milovidov @vitlibar Analysis metadata
Introducing change: not identified
|
|
Confirmed, with one correction to the target list: 26.8 is affected too, the tested-versions list above did not include it. Measured just now on the official per-branch builds, one statement each. @PedroTadim could you apply |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
A
PRIMARY KEYin the column list ofCREATE TABLE ... AS <table function>was accepted, and the table metadata was then written in a form the server could not parse back, so the database failed to load withCode: 62 SYNTAX_ERRORon the next start. Such a statement is now rejected, as it already is for views.Description
Found by the AST fuzzer in the stress test,
Stress test (amd_debug), STID1941-26fa:https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=113077&sha=8ed17407ce766eefd63ecefecaac7a9f0f8f2063&name_0=PR&name_1=Stress%20test%20%28amd_debug%29
CREATE TABLE t (x UInt64 PRIMARY KEY) AS numbers(5)is accepted on every release I tested (25.8 LTS through master), thePRIMARY KEYis ignored at execution, and the metadata written is unparseable, so the server does not come back up:Root cause: the parser normalizes a column-list
PRIMARY KEYinto a synthesized storage definition without checkingas_table_function(ParserCreateQuery.cpp:1088-1122), so the AST carries both. A table built from a table function has no storage definition of its own, which is the same rule that already rejects an explicitENGINE(:1020), so formatting prints it afterAS numbers(5), where the grammar has no production for it. Debug builds reportInconsistent AST formatting.I reject the statement at parse time, mirroring the existing guard for views (
390df2a54779c2e), and add the same mutual-exclusion check to the JSON AST reader, where a payload carrying both members reached the same unparseable metadata.Test shape follows
633635892484d83, which removed the stateless arms390df2a54779c2eadded, leaving thegtest_tree_hash_completeness.cppcase. Both new arm sets fail without their guard.Residuals, not addressed here: metadata already persisted this way stays unreadable, and
CREATE WINDOW VIEW wv (x UInt64 PRIMARY KEY) AS SELECT ...has a different root cause (the clause is dropped silently). If #101177 lands, makingstorageplusas_table_functionlegal, both guards need revisiting.Workflow [PR]
Sync PR [sync-upstream/pr/116645]
Version info
26.9.1.229(included in26.9and later)