Skip to content

Field::restoreFromDump: read the UUID/IPv4/IPv6 dumps Field::dump writes; prefix-check other dumps - #121052

Open
qoega wants to merge 5 commits into
ClickHouse:masterfrom
qoega:json-fuzz-group2-field-restore-uuid-ip
Open

qoega wants to merge 5 commits into
ClickHouse:masterfrom
qoega:json-fuzz-group2-field-restore-uuid-ip

Conversation

@qoega

@qoega qoega commented Sep 19, 2026 •

Copy link
Copy Markdown
Member

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 to CHANGELOG.md):

Field::restoreFromDump now reads the UUID_/IPv4_/IPv6_ dumps that Field::dump writes. Previously a well-formed dump for one of these three types round-tripped as CANNOT_RESTORE_FROM_FIELD_DUMP; the additional prefix check now surfaces malformed payloads as CANNOT_PARSE_QUOTED_STRING with a clearer message.

Documentation entry for user-facing changes

No documentation change needed.

Detailed description / Documentation

Field::dump writes UUID_('...'), IPv4_('...'), IPv6_('...') for the corresponding field types, but Field::restoreFromDump never learned to read those three prefixes: any round-trip through dump/restore of a UUID or IPv4/IPv6 value produced CANNOT_RESTORE_FROM_FIELD_DUMP. Fix adds the three parsers to restoreFromDump and prefixes the dispatch with a <type>_ check so an unrelated dump is rejected with Field '<name>' dump must start with '<type>_' instead of a generic parse error deeper inside.

Also updates the 04320 expected message (its dump-mismatch case now trips the prefix check first) and the 04654 depth-guard test's expected message (a deep Field dump is now rejected at the prefix check before recursion, so the Field dump payload exceeds maximum AST depth limit message is no longer reachable in the depth case).

Note: Object_(…) and CustomType_(…) dumps still are not restorable; that is a separate follow-up (CustomType in particular is tied to #118993 / #119643).

Closes: #121023

cc @groeneai — fuzz-lab find (JSON AST fuzzer)

🤖 Generated with Claude Code


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

qoega and others added 2 commits September 19, 2026 18:40
…::dump writes

`FieldVisitorDump` writes `UUID_'...'`, `IPv4_'...'` and `IPv6_'...'`, but
`Field::restoreFromDump` had no branch for them, so a `Literal` of these types
could not be deserialized from the JSON AST (`CANNOT_RESTORE_FROM_FIELD_DUMP`).

`JSONObjectReader::readFieldFromObjectImpl` now requires the dump payload to start
with `<field_type>_`; before, a payload such as `'VALUE&` declared as `UUID` was
handed to the `String` reader and leaked `CANNOT_PARSE_QUOTED_STRING` instead of
`BAD_ARGUMENTS`. `json_ast_sql_parser_fuzzer` accepts the `CANNOT_PARSE_*` family at
the JSON -> AST stage, since the dump payload readers legitimately throw them.

Found by `json_ast_sql_parser_fuzzer`.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…restoreFromDump

Follow-up to the previous commit: the tightened `Field::restoreFromDump`
now rejects a deep `Field` dump payload at the prefix check
(`Field 'value' dump must start with 'Object_'`) before any recursion
would happen, so the depth-guard test's expected message changes
accordingly.

Found by json_ast_sql_parser_fuzzer.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
@qoega qoega added the fuzz Problem found by one of the fuzzers label Sep 19, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f39a643]

Summary: ✅


AI Review

Summary

This PR fixes the missing UUID_ / IPv4_ / IPv6_ branches in Field::restoreFromDump and adds a useful <field_type>_ prefix check before AST JSON deserialization falls back to dump parsing. The direction is right, but two correctness gaps remain on the same dump / restoreFromDump surface: several quoted branches still accept trailing garbage, and AggregateFunctionState dumps still are not round-trippable even when they were produced by Field::dump.

Findings

⚠️ Majors

  • [src/Core/Field.cpp:840-881] The exact-dump invariant is still open for the sibling quoted branches that were not updated here. Decimal32 / Decimal64 / Decimal128 / Decimal256 and the plain quoted-string branch still return immediately after readQuoted, so malformed payloads such as Decimal32_'1.23'junk and 'abc'junk are still accepted through Field::restoreFromDump. That is reachable both from formatQueryFromJSON for decimal literals and from custom-setting parsing through BaseSettings::stringToValueUtil. Suggested fix: apply the same if (!buf.eof()) show_error(); check to every quoted branch and add a focused regression case for at least one Decimal* payload with trailing bytes.

  • [src/Core/Field.cpp:997-1014] AggregateFunctionState still violates the Field::dump / Field::restoreFromDump round-trip contract. The parser finds the separator comma and closing ) with raw find before it reads the quoted strings, so a valid dump like AggregateFunctionState_('quantiles(0.5, 0.9)', 'x') is rejected because the comma inside the aggregate name is treated as syntax. The same branch also rejects any serialized data containing ), which is realistic for arbitrary aggregate-state bytes. Suggested fix: parse the two quoted strings sequentially from a ReadBuffer, then require the trailing ) and EOF.

Final Verdict

Status: ⚠️ Request changes

Minimum required actions:

  • Make every quoted Field::restoreFromDump branch enforce full input consumption, not only the new UUID / IPv4 / IPv6 ones.
  • Rework AggregateFunctionState_ parsing to be quote-aware so valid dumps emitted by Field::dump round-trip correctly.

LLVM Coverage Report

Measured on commit f39a643.

Metric Baseline Current Δ
Lines 88.10% 88.10% +0.00%
Functions 91.70% 91.70% +0.00%
Branches 79.40% 79.40% +0.00%

Changed lines: Changed C/C++ lines covered: 32/33 (96.97%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 19, 2026
@groeneai

Copy link
Copy Markdown
Collaborator

I had the same three branches queued from your issue, so I am not opening a competing PR. Two things I
measured while preparing mine look worth folding in here, plus one question.

The three new branches accept trailing garbage. readQuoted stops at the closing quote and nothing
inspects the buffer afterwards, so UUID_'61f0c404-5cb3-11e7-907b-a6006ad3dba0'junk restores the UUID and
silently discards the rest. I measured that on a build carrying exactly these three branches without the
guard: the value comes back and no error is raised. Per branch,

if (!buf.eof())
    show_error();

turns it back into CANNOT_RESTORE_FROM_FIELD_DUMP. #119687 adds that same check to the other readQuoted
branches of this function, so without it these three would be the only ones left accepting a partially
read dump.

The dump depth guard looks unreachable after the prefix check. computeFieldDumpNestingDepth has one
call site, just below the new check. A payload deep enough to trip it has to start with Array_, Tuple_
or Map_, which now forces field_type to be Array, Tuple or Map, and those three are taken by the
structural branches above, which require a JSON array value rather than a string. That is consistent with
the 04654 update: case 4 no longer reaches the recursion it was written for. Is the guard meant to be
retired along with it, or should case 4 keep a payload that still reaches it?

Two smaller notes. The changelog entry attributes CANNOT_PARSE_QUOTED_STRING to the missing branches; a
well-formed UUID_'...' dump surfaced CANNOT_RESTORE_FROM_FIELD_DUMP, and code 26 was the separate
malformed-payload defect that your prefix check fixes. On the CustomType_( follow-up: #118993 rests on a
CustomType dump being unrestorable, because FieldFromASTImpl implements only operator== while the
settings-constraint configuration route reaches BaseSettings::stringToValueUtil, so a CustomType_ branch
would make a constraint bound constructible and turn that residual into a reachable LOGICAL_ERROR.
#119643 looks like the prerequisite for it.

Comment thread src/Core/Field.cpp Resolved
@clickhouse-gh clickhouse-gh Bot added the comp-sql-syntax SQL/grammar parsing, AST nodes, syntax-level features. label Sep 19, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Commit f39a64306a7945e7cd24ad2deb1d464b595ee82f was not compared: the CI logs cluster did not answer (every POST attempt failed with an exception).

See the job log for details.

…umed entirely

Trailing bytes after the quoted value are an error like in the other branches.
The dump-depth guard in the JSON AST reader is retired: after the dump prefix
check a nested dump can only be declared as Array, Tuple or Map, which are read
structurally and never reach `restoreFromDump`.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Comment thread src/Core/Field.cpp
UUID value;
ReadBufferFromString buf{dump.substr(prefix.length())};
readQuoted(value, buf);
if (!buf.eof())

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.

The new buf.eof() checks close the partial-read hole for UUID / IPv4 / IPv6, but the same exact-dump invariant is still open in the sibling quoted branches above. Decimal32 / Decimal64 / Decimal128 / Decimal256 still do readQuoted(decimal, buf) and return immediately, and the plain quoted-string branch does the same. As a result, malformed payloads like Decimal32_'1.23'junk still deserialize successfully through formatQueryFromJSON, while 'abc'junk is still accepted by direct Field::restoreFromDump / custom-setting parsing.

Because this PR is tightening malformed-dump handling on the same surface, leaving the other quoted branches as partial reads keeps the contract inconsistent across dump-encoded types. Please apply the same tail check there and add at least one regression case for a Decimal* dump with trailing bytes.

@qoega

qoega commented Sep 21, 2026

Copy link
Copy Markdown
Member Author

Review follow-up landed as d5db720853a. Applies your if (!buf.eof()) show_error() shape to the three new UUID/IPv4/IPv6 branches in Field::restoreFromDump, adds trailing-junk assertions to 05236, and retires computeFieldDumpNestingDepth (unreachable after the prefix check; 04654 case 4 keeps its prefix-check expectation). PR body reworded per your note (CANNOT_RESTORE_FROM_FIELD_DUMP for well-formed dumps, CANNOT_PARSE_QUOTED_STRING for malformed via the new prefix check). CustomType_ remains deferred to #118993 / #119643 as you noted.

/// Every dump-encoded type is written by `FieldVisitorDump` as `<type name>_<payload>`, so a
/// payload of a different type (or plain garbage) is rejected here with `BAD_ARGUMENTS` instead
/// of by whichever text reader `Field::restoreFromDump` happens to pick for it.
if (!dump_str.starts_with(field_type) || dump_str.size() <= field_type.size() || dump_str[field_type.size()] != '_')

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.

AggregateFunctionState still is not round-trippable on this path. Field::restoreFromDump splits that payload with raw find(',') / find(')') before it parses the quoted name and data strings (src/Core/Field.cpp:1000-1013), so a valid dump such as AggregateFunctionState_('quantiles(0.5, 0.9)', 'x') is rejected because the comma inside the aggregate name is mistaken for the separator. Any serialized data containing ) fails for the same reason. Since writeFieldJSON emits AggregateFunctionState via Field::dump, formatQueryFromJSON still cannot round-trip this dump-encoded type even after the new prefix check. Please make that restoreFromDump branch parse the two quoted strings sequentially and only then verify the closing ) / EOF.

…ggregateFunctionState quote-aware

The ack commit added `if (!buf.eof()) show_error()` to the new UUID / IPv4 /
IPv6 branches, but the sibling quoted branches (Decimal32/64/128/256 and the
plain `'...'` string branch) still fell through after `readQuoted`, so dumps
like `Decimal32_'1.23'junk` or (reached via Array/Tuple/Map recursion)
`'abc'junk` were silently accepted. Add the same EOF check so the whole
`Field::dump` -> `Field::restoreFromDump` contract holds across every quoted
branch.

`AggregateFunctionState_('name', 'data')` was parsed with a raw search for
`,` / `)` before the inner strings were read. That is quote-unaware and
rejects legitimate dumps whose aggregate name contains `,` (e.g.
`quantiles(0.5, 0.9)`) or whose serialized state bytes contain `)`. Walk the
two quoted strings in order via `readQuotedString`, then require the
closing paren and EOF, so a round-trip `dump` -> `restoreFromDump` works
for any payload `FieldVisitorDump` can produce.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
@qoega

qoega commented Nov 1, 2026

Copy link
Copy Markdown
Member Author

Review follow-up landed as 04bdcac515f. Addresses the two remaining AI-reviewer line reviews from 2026-09-21 and 2026-09-23.

EOF check on sibling quoted branches. The ack commit d5db720853a only covered the three new UUID/IPv4/IPv6 branches. Extended the same if (!buf.eof()) show_error(); shape to the four Decimal32/64/128/256_'...' branches and the plain '...' string branch. Decimal32_'1.23'junk and 'abc'junk now surface CANNOT_RESTORE_FROM_FIELD_DUMP instead of silently discarding the tail.

AggregateFunctionState_ raw-find hazard. Reworked the branch to be quote-aware: the single ReadBufferFromString feeds readQuotedString for both strings in order, then requires ',', ')' and EOF, with skipWhitespaceIfAny between tokens. Legitimate dumps whose aggregate name contains , (AggregateFunctionState_('quantiles(0.5, 0.9)', 'x')) and whose serialized data bytes contain ) now round-trip through Field::dump / Field::restoreFromDump.

Regression cases added to 05236_ast_json_literal_uuid_ip_dump.{sql,reference}:

  • Decimal32 round-trip control
  • Decimal32 / Decimal64 trailing-junk → CANNOT_RESTORE_FROM_FIELD_DUMP
  • AggregateFunctionState with aggregate name quantiles(0.5, 0.9) (embedded ,)
  • AggregateFunctionState with data a)b (embedded ))

Plain-string trailing-junk isn't directly reachable through formatQueryFromJSON (ASTJSONReadHelpers routes String via its own scalar branch); the EOF fix is kept for the internal recursive callers from Array/Tuple/Map and the dump contract.

@alexey-milovidov alexey-milovidov self-assigned this Nov 23, 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

comp-sql-syntax SQL/grammar parsing, AST nodes, syntax-level features. fuzz Problem found by one of the fuzzers pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

3 participants