Skip to content

Fix Inconsistent AST formatting for a Tuple with mixed named/unnamed elements - #108915

Merged
cv4g merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-ast-format-named-tuple-json
Jul 2, 2026
Merged

cv4g merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-ast-format-named-tuple-json

Conversation

@groeneai

@groeneai groeneai commented Jun 30, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #108372

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

Fixed a LOGICAL_ERROR ("Inconsistent AST formatting") that aborted the server in debug and sanitizer builds when formatting a Tuple data type that mixes named and unnamed elements (for example Tuple(a UInt8, UInt16)).

Description

The AST fuzzer hit Inconsistent AST formatting (STID 1941-26fa) on a fuzzer-mangled CREATE TABLE whose column type was a Tuple mixing named and unnamed elements. This is the nested named-tuple / JSON shape tracked by #108372; it is unrelated to the PRs it shows up on and reproduces on master. It is a different shape from #107806 (no-argument window function in CODEC).

Root cause: a Tuple(a UInt8, UInt16) is parsed into ASTTupleDataType whose element_names holds an empty-string placeholder for each unnamed element. ASTTupleDataType::formatImpl emitted that empty name via backQuoteIfNeed(""), which produces an empty backquoted identifier ``. The formatted SQL Tuple(a UInt8, `` UInt16) cannot be parsed back, so the parse/format round-trip check in executeQueryImpl (#ifndef NDEBUG) raised the LOGICAL_ERROR.

Fix: skip the name and its trailing space when the placeholder is empty, in both the single-line and pretty (multiline) branches. The type now formats as Tuple(a UInt8, UInt16), which round-trips and still raises the normal BAD_ARGUMENTS ("Names are specified not for all elements of Tuple type") at type resolution.

Verified on a debug build (both directions): the previously-aborting clickhouse-local CREATE TABLE t (x Tuple(a UInt8, UInt16)) now returns a clean BAD_ARGUMENTS instead of SIGABRT, the server survives, and valid named/unnamed tuples are unchanged. Regression coverage added to 03210_inconsistent_formatting_of_data_types.

CI report (AST fuzzer (amd_debug), STID 1941-26fa, on #91358): https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=91358&sha=fec6b590416b97167791253cb52586db7bd50d56&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%29

Version info

  • Merged into: 26.7.1.428 (included in 26.7 and later)

…elements

A Tuple data type that mixes named and unnamed elements (for example
Tuple(a UInt8, UInt16), as produced by the AST fuzzer) was parsed into an
ASTTupleDataType whose element_names holds an empty string placeholder for
each unnamed element. ASTTupleDataType::formatImpl emitted that empty name
through backQuoteIfNeed(""), which produces an empty backquoted identifier ``.
The resulting SQL (Tuple(a UInt8, `` UInt16)) cannot be parsed back, so the
parse-format-parse round-trip check in executeQueryImpl raised the
LOGICAL_ERROR "Inconsistent AST formatting" and aborted the server in debug
and sanitizer builds (STID 1941-26fa).

Skip the name and its trailing space when the placeholder is empty, in both
the single-line and the pretty (multiline) formatting branches. The type now
formats as Tuple(a UInt8, UInt16), which round-trips and still produces the
normal BAD_ARGUMENTS exception ("Names are specified not for all elements of
Tuple type") at type resolution.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Check Result
a Deterministic repro? Yes. clickhouse-local -q "CREATE TABLE t (x Tuple(a UInt8, UInt16)) ENGINE=Memory" aborts (SIGABRT, exit 134) on master debug; clickhouse format of the same emits Tuple(a UInt8, `` UInt16) which fails to re-parse.
b Root cause explained? Yes. A mixed named/unnamed Tuple parses to ASTTupleDataType with an empty-string placeholder name for each unnamed element. formatImpl emits it via backQuoteIfNeed("") = `` (empty backquoted identifier), so the formatted SQL cannot be parsed back; the #ifndef NDEBUG round-trip check in executeQueryImpl raises the LOGICAL_ERROR.
c Fix matches root cause? Yes. Skip the name + trailing space when the placeholder is empty, at both formatting sites (single-line and pretty/multiline).
d Test intent preserved / new tests added? Added 4 cases to 03210_inconsistent_formatting_of_data_types asserting the round-trip is now stable; existing assertions unchanged.
e Demonstrated both directions? Yes. OLD binary (Build 64c70dfd): SIGABRT exit 134. FIXED binary (Build 33eba7ba): clean BAD_ARGUMENTS exit 36, server survives. 03210 reference byte-matches.
f Fix general, not a narrow patch? Yes. Both formatter branches in ASTTupleDataType::formatImpl guarded. ASTNameTypePair::formatImpl (the fallback parser path) only receives a non-empty name (its parser requires the identifier to parse first), so it cannot produce the empty-name case.
g Generalizes across inputs? Verified the matrix: single-element, all-named, all-unnamed, leading-unnamed, nested mixed, keyword-named (SELECT), and JSON-typed elements all round-trip; mixed tuples still raise BAD_ARGUMENTS at resolution.
h Backward compatible? Yes. No setting, format, or default changed. Output only changes for previously-unparseable mixed tuples (Tuple(a UInt8, `` UInt16) -> Tuple(a UInt8, UInt16)); valid named/unnamed tuples are byte-identical.
i Invariants/contracts preserved? Yes. element_names[i] still aligns with arguments->children[i]; the empty-placeholder contract documented on ASTTupleDataType is honored (skip on format). Resolution-time validation (DataTypeTuple: "Names ... cannot be empty") is unchanged.

Session id: cron:clickhouse-worker-slot-3:20260630-095200

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @evillique @george-larionov — could you review this? It fixes the Inconsistent AST formatting LOGICAL_ERROR (STID 1941-26fa, tracked in #108372) for a Tuple that mixes named and unnamed elements: the unnamed element's empty placeholder name was formatted as an empty backquoted identifier , breaking the parse round-trip. Same formatter family as the merged #107806 (window-func-in-CODEC) and our #108354 (INT-substring type names).

Comment thread src/Parsers/ASTTupleDataType.cpp Outdated
/// Print element name if present.
/// An empty name is the placeholder for an unnamed element in a
/// mixed named/unnamed tuple; formatting it as `` (an empty
/// backquoted identifier) is not parseable back, so skip it.

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.

@groeneai Please consider to remove the sentence:

formatting it as `` (an empty
/// backquoted identifier) is not parseable back, so skip it.

It may be confusing to unfamiliar readers of the code. it's totally expected that unnamed elements do not have a name serialized. Explaining that an empty string in backticks is not parseable confuses the point in my opinion.

Comment thread src/Parsers/ASTTupleDataType.cpp Outdated
@@ -97,8 +100,8 @@ void ASTTupleDataType::formatImpl(WriteBuffer & ostr, const FormatSettings & set
if (i > 0)
ostr << ", ";

/// Print element name if present
if (i < element_names.size())
/// See the multiline branch above: skip an empty placeholder name.

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.

@groeneai Please mention the term unnamed element in this comment. If you want to you can add that unnamed elements are represented by empty strings which in turn serve as placeholder. Although I would go one step further and suggest to not introduce the term placeholder here. It's an unnecessary indirection to what is actually meant.

@cv4g cv4g self-assigned this Jun 30, 2026
@cv4g cv4g added the can be tested Allows running workflows for external contributors label Jun 30, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 30, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [88cb888]

Summary: ❌

job_name test_name status info comment
Stress test (amd_tsan) FAIL
Hung check failed, possible deadlock found FAIL cidb, issue ISSUE EXISTS
Stress test (arm_tsan) FAIL
Hung check failed, possible deadlock found FAIL cidb, issue ISSUE EXISTS

AI Review

Summary

This PR fixes formatting for parsed Tuple data types that contain both named and unnamed elements by skipping empty element-name placeholders in both one-line and pretty formatting. The added formatter regression cases cover trailing unnamed, leading unnamed, nested mixed, ALTER, and the pretty branch; I did not find a new actionable correctness issue.

Missing context / blind spots
  • ⚠️ No local build* directory or clickhouse-format binary is available in this checkout, so I could not run 03210_inconsistent_formatting_of_data_types locally. The available Praktika PR report showed no failed tests, but several jobs were still queued or running at review time.
Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jun 30, 2026
@cv4g

cv4g commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

@groeneai Can you please add a test for the multi line branch?

Add four cases to 03210_inconsistent_formatting_of_data_types that format
the mixed named/unnamed tuples without --oneline, so the first pass takes
the pretty (multi-line) branch of ASTTupleDataType::formatImpl, then
re-format with --oneline to assert the multi-line output reparses to a
stable result.

Reword the two element-name comments to talk about unnamed elements
(empty names) directly, instead of explaining that an empty backquoted
identifier is unparseable.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author

@cv4g done in 88cb888:

  1. Added 4 cases to 03210 that format the mixed named/unnamed tuples without --oneline, so the first pass takes the pretty (multi-line) branch of ASTTupleDataType::formatImpl, then re-format with --oneline to assert the multi-line output reparses to a stable result. Verified both directions: on the pre-fix formatter the multi-line branch emits UInt16`` and the reparse fails with Syntax error; with the fix it emits `UInt16` and round-trips.
  2. Removed the "empty backquoted identifier is not parseable" sentence.
  3. Reworded both comments to talk about unnamed elements (empty names) directly, dropped "placeholder".

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand) — covers 88cb888
# Check Result
a Deterministic repro? Yes. clickhouse format (no --oneline) of CREATE TABLE t (x Tuple(a UInt8, UInt16)) takes the multi-line branch; on the pre-fix formatter it emits UInt16`` (empty backquoted identifier) which fails to re-parse (Syntax error).
b Root cause explained? Yes. The multi-line branch of ASTTupleDataType::formatImpl emitted the unnamed element's empty name via backQuoteIfNeed("") = ``, so the formatted SQL could not be parsed back; the round-trip check raised the LOGICAL_ERROR. The single-line branch (--oneline) had the identical defect (already covered by the original test).
c Fix matches root cause? Yes (unchanged from the original commit). Both branches skip the name + trailing space when the element name is empty. This commit only adds the multi-line test and rewords two comments.
d Test intent preserved / new tests added? Added 4 cases exercising the multi-line branch (format without --oneline, reparse with --oneline); existing assertions unchanged.
e Demonstrated both directions? Yes. Pre-fix binary (Build 1dc68eb9): multi-line emits UInt16``, reparse -> Syntax error. Fixed binary (Build 6f6e7c69): emits `UInt16`, round-trips; `03210` passes via `clickhouse-test` (`[ OK ]`) and the reference byte-matches.
f Fix general, not a narrow patch? Yes. Both formatter branches (single-line and multi-line) are guarded; this commit verifies the multi-line branch is covered too.
g Generalizes across inputs? Verified: leading-unnamed (Tuple(JSON, f1 ...)), trailing-unnamed (Tuple(a UInt8, UInt16)), nested mixed, and ALTER MODIFY all round-trip through the multi-line branch.
h Backward compatible? Yes. No setting, format, or default changed.
i Invariants/contracts preserved? Yes. element_names[i] still aligns with arguments->children[i]; resolution-time validation (DataTypeTuple: "Names ... cannot be empty") unchanged.

Session id: cron:clickhouse-worker-slot-3:20260630-150500

@clickhouse-gh

clickhouse-gh Bot commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.50% 85.50% +0.00%
Functions 92.70% 92.70% +0.00%
Branches 77.80% 77.70% -0.10%

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

Full report · Diff report

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — 88cb888

CI fully finished (Finish Workflow: success @ 19:11 UTC). This PR fixes Inconsistent AST formatting for a Tuple with mixed named/unnamed elements (src/Parsers/ASTTupleDataType.cpp + 03210 round-trip test). Every failure below has an owner; none is PR-caused.

Check / test Reason Owner / fixing PR
Stress test (amd_tsan) / "Hung check failed, possible deadlock found" deadlock (chronic hung-check family, 636 distinct PRs / 445 master hits / 30d) #108212 (ours, merged 2026-06-28) / #105905 (ours, open)
Stress test (arm_tsan) / "Hung check failed, possible deadlock found" deadlock (same chronic hung-check family) #108212 (ours, merged) / #105905 (ours, open)
CH Inc sync CH Inc sync (private, not actionable by us) —

The diff is confined to the AST formatter for Tuple data-type names plus a .sh/.reference round-trip test; it cannot introduce a TSan hung-check deadlock. Both reds are the chronic stress hung-check family already owned by our fixing PRs.

Session id: cron:our-pr-ci-monitor:20260630-193000

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — 88cb888

Every failure below has an owner. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Stress test (amd_tsan) / Hung check, possible deadlock deadlock (chronic trunk: 182 PRs + 71 master hits / 24h) #108212 (ours, merged) / #105905 (ours, open)
Stress test (arm_tsan) / Hung check, possible deadlock deadlock (chronic trunk, same family) #108212 (ours, merged) / #105905 (ours, open)
PR (rollup) aggregates the stress sub-checks above #108212 (ours, merged) / #105905 (ours, open)
CH Inc sync — CH Inc sync (private, not actionable)

No PR-caused failure. This PR fixes the AST formatter for a Tuple with mixed named/unnamed elements; it cannot cause a TSAN ThreadPool-shutdown deadlock.

Session id: cron:our-pr-ci-monitor:20260630-200000

@cv4g
cv4g added this pull request to the merge queue Jul 2, 2026
Merged via the queue into ClickHouse:master with commit c124391 Jul 2, 2026
171 of 174 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 2, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…ed-tuple-json

Fix Inconsistent AST formatting for a Tuple with mixed named/unnamed elements
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 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