Skip to content

Do not construct exceptions in the Values format streaming parser fast path - #109981

Closed
pamarcos wants to merge 17 commits into
masterfrom
values-no-exception-fast-path
Closed

pamarcos wants to merge 17 commits into
masterfrom
values-no-exception-fast-path

Conversation

@pamarcos

@pamarcos pamarcos commented Jul 10, 2026 •

Copy link
Copy Markdown
Member

The Values format used exceptions as control flow when its fast streaming parser could not read a
value and had to fall back to the SQL expression parser. Constructing those expected exceptions
recorded errors in system.errors and system.error_log, even though the query succeeded.

Use non-throwing serialization probes for the streaming attempt. A Values-specific probe state
preserves legacy integer, temporal, QBit, and escaped UUID literal behavior without changing
Variant or Dynamic arm selection. Decimal probes distinguish overflow from other parse failures,
so overflows retain the existing ARGUMENT_OUT_OF_BOUND behavior without exception-backed retries
for unrelated expression fallbacks.

Regression coverage verifies that successful expression fallbacks leave all parse-error counters
unchanged and that the previous literal compatibility behavior is preserved.

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

The Values format no longer records parse errors in system.errors and system.error_log when it successfully falls back from streaming parsing to SQL expression parsing.

…st path

Every constructed `DB::Exception` is counted in `system.errors` and `system.error_log` even when
it is caught and handled. The `Values` format parser used exceptions for control flow: when the
fast streaming parser could not read a value (e.g. it was an expression like `1 + 1`, or the
`DEFAULT` keyword prefix did not match), it threw `CANNOT_PARSE_INPUT_ASSERTION_FAILED`, caught it
and fell back to the SQL expression parser. As a result, perfectly successful INSERT queries
produced confusing `Cannot parse input: expected ',' before: ...` records in `system.error_log`,
attributed to the query id of the insert (or of the `AsyncInsertFlush` query for async inserts).
Rework `tryReadValue` in `ValuesBlockInputFormat` to use the non-throwing
`tryDeserializeTextQuoted` serialization methods plus `checkDelimiterAfterValue`, so that a
fast-path miss reaches the SQL expression parser without constructing any exception. This also
avoids collecting a stack trace for every such value, which is much faster.
Columns that nest a `Decimal` keep the old throwing code path: an overflowing decimal literal
must fail the query with `ARGUMENT_OUT_OF_BOUND` instead of being passed to the expression
parser, which would read it as a `Float64` literal and silently lose precision. The non-throwing
parser cannot distinguish the overflow from a genuine parse failure.
A new `SerializationNullable::tryDeserializeNullAsDefaultOrNestedTextQuoted` overload exposes
whether the value was `NULL`, which `tryReadValue` needs to fill `BlockMissingValues`.
@clickhouse-gh

clickhouse-gh Bot commented Jul 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [996bf77]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_llvm_coverage, old analyzer, s3 storage, DBReplicated, WasmEdge, parallel, 2/3) FAIL
Server died FAIL cidb
04541_create_as_select_table_scoped_insert_grant UNKNOWN cidb
03236_squashing_high_memory UNKNOWN cidb
04549_text_index_exclusion_search_max_steps UNKNOWN cidb
03338_http_compression_profile_events UNKNOWN cidb
02579_fill_empty_chunk_analyzer UNKNOWN cidb
04041_attach_table_no_partition_key_metadata_cache UNKNOWN cidb
Unknown error FAIL cidb
Stateless tests (amd_llvm_coverage, old analyzer, s3 storage, DBReplicated, WasmEdge, sequential, 2/2) FAIL
04549_values_expression_fallback_no_error_counters FAIL cidb
Stateless tests (amd_llvm_coverage, ParallelReplicas, s3 storage, parallel) FAIL
04507_mvt_encode_state_serialize FAIL cidb
01147_partial_merge_full_join NOT_FAILED cidb
02499_monotonicity_toUnixTimestamp64 NOT_FAILED cidb
02345_implicit_transaction NOT_FAILED cidb
03707_statistics_cache NOT_FAILED cidb
01010_partial_merge_join NOT_FAILED cidb
Stateless tests (amd_llvm_coverage, AsyncInsert, s3 storage, parallel) ERROR
LLVM Coverage DROPPED

AI Review

Summary

This PR reworks the Values fast path to avoid exception-backed fallbacks and to preserve existing literal semantics for several types. The correctness regressions raised earlier look addressed in the current head, but there is still a hot-path performance regression in the new AggregateFunction lookahead, so the review remains at request-changes.

Findings

⚠️ Majors

  • [src/Processors/Formats/Impl/ValuesBlockInputFormat.cpp:309] The new AggregateFunction precheck reads and unescapes the entire quoted state into a temporary String just to see whether the next token is a delimiter, then the real deserializer reads the same value again after rollbackToCheckpoint. For large aggregate states this turns every successful quoted literal into two full scans plus an extra allocation/copy on the exact insert path this PR is trying to speed up, and the added performance query only exercises Bool, not this path.
    Suggested fix: replace the materializing lookahead with a non-allocating quoted-string skip (escape-aware scan to the closing quote, then delimiter check), or otherwise keep the lookahead from reparsing the full state when the value is an ordinary literal.
Final Verdict
  • Status: ⚠️ Request changes

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Jul 10, 2026

Copilot AI left a comment

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.

Pull request overview

This PR removes exception construction from the Values format streaming-parser fallback path so that successful INSERT ... VALUES (...) queries (that contain expressions / DEFAULT / escaped complex values) no longer produce misleading parse errors in system.errors / system.error_log, and avoids the overhead of collecting stack traces on these expected fallbacks.

Changes:

  • Refactors ValuesBlockInputFormat::tryReadValue to use non-throwing tryDeserializeTextQuoted-style parsing for fast-path attempts and cleanly fall back to the SQL expression parser without constructing exceptions.
  • Preserves the throwing path for columns that are (or contain) Decimal, to ensure decimal overflows still fail with ARGUMENT_OUT_OF_BOUND instead of silently degrading via expression parsing.
  • Adds a stateless query test that asserts successful fallback does not increment parse-related counters in system.errors.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/Processors/Formats/Impl/ValuesBlockInputFormat.h Adds streaming helper declarations and tracks which columns nest Decimal to choose the appropriate parsing path.
src/Processors/Formats/Impl/ValuesBlockInputFormat.cpp Implements the new non-throwing streaming fast path and refactors fallback logic; keeps a throwing path for Decimal overflow semantics.
src/DataTypes/Serializations/SerializationNullable.h Adds an overload exposing whether a parsed value was NULL when using “NULL as default” logic.
src/DataTypes/Serializations/SerializationNullable.cpp Implements the new overload and reuses it for the existing API.
tests/queries/0_stateless/04510_values_expression_fallback_no_error_counters.sh New regression test exercising expression/DEFAULT/escaped complex fallbacks and validating system.errors counters don’t change.
tests/queries/0_stateless/04510_values_expression_fallback_no_error_counters.reference Expected output for the new test.

Keep throwing deserialization for Decimal overflow detection, but use the non-throwing delimiter check before falling back to the SQL expression parser. Extend the regression test so a Decimal expression cannot increment parse-error counters.

Copilot AI left a comment

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.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Use non-throwing `DEFAULT` detection in the Decimal-preserving streaming path. Extend the regression test with `divide` on a Decimal column so successful fallback cannot increment parse-error counters.

Copilot AI left a comment

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.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

@Algunenano Algunenano self-assigned this Jul 10, 2026
`SerializationDynamic::tryDeserializeTextQuoted` called the throwing field reader, so expressions such as `toDate` failed before `ValuesBlockInputFormat` could fall back to SQL expression parsing. Use `tryReadQuotedField` and share the existing inferred-field insertion logic. Extend the error-counter regression with `Dynamic` function expressions.

Copilot AI left a comment

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.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated no new comments.

Use the throwing streaming parser only when Array, Map, or Tuple input starts with its literal delimiter, preserving Decimal overflow checks for raw literals. Reuse the same non-throwing streaming probe when `parseExpression` checks whether it can return to streaming mode. Cover function expressions, quoted complex values, mixed expression shapes, and nested Decimal overflow.

Copilot AI left a comment

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.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated no new comments.

pamarcos added 2 commits July 14, 2026 19:28
Keep literal retries from applying `input_format_null_as_default`, and route
identifier-leading and composite Decimal expressions through non-throwing
probes. Retain throwing retries only for raw Decimal failures that can
represent overflow.

Extend regression coverage for `Dynamic`, `Variant`, `JSON`, nested Decimal
expressions, and Decimal map-key overflow.

Copilot AI left a comment

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.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated no new comments.

@pamarcos
pamarcos requested a review from Copilot July 16, 2026 14:22
@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default and removed pr-improvement Pull request with some product improvements labels Jul 16, 2026

Copilot AI left a comment

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.

Pull request overview

Copilot reviewed 25 out of 25 changed files in this pull request and generated no new comments.

Initialize the local result before compile-time branch selection to satisfy the `cppcoreguidelines-init-variables` check in the arm_tidy build.

Copilot AI left a comment

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.

Pull request overview

Copilot reviewed 25 out of 25 changed files in this pull request and generated no new comments.

Keep `Dynamic` probes non-throwing, isolate quoted complex retries from compatibility mode, and distinguish Decimal overflow from parse failures. Avoid aggregate fallback exceptions while preserving overflow diagnostics.

Copilot AI left a comment

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.

Pull request overview

Copilot reviewed 26 out of 26 changed files in this pull request and generated no new comments.

Bypass the `Values` aggregate probe only when a quoted token is followed by an expression. Structural delimiters still reach deserialization and retain the existing specific error.

Copilot AI left a comment

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.

Pull request overview

Copilot reviewed 26 out of 26 changed files in this pull request and generated no new comments.

Retry every classified `Decimal` overflow with the throwing parser to match the previous behavior before operators. Let parsed literals restore streaming mode after expressions.

Copilot AI left a comment

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.

Pull request overview

Copilot reviewed 27 out of 27 changed files in this pull request and generated 1 comment.

Comment on lines +312 to +316
if (starts_with_aggregate_function_string)
{
String field;
bool starts_with_expression = tryReadQuotedStringWithSQLStyle(field, *buf);
if (starts_with_expression)
@pamarcos

Copy link
Copy Markdown
Member Author

Alternative implementation: #111141. It keeps the existing Values parser and suppresses only system-error accounting for the handled speculative parse exceptions.

@pamarcos pamarcos closed this Jul 21, 2026
pull Bot pushed a commit to Haofei/ClickHouse that referenced this pull request Jul 23, 2026
The `Values` streaming parser uses parse exceptions to fall back to SQL expression parsing.
Those handled exceptions were recorded in `system.errors` and `system.error_log` even when the
insert succeeded.

Add a thread-local scope that suppresses system error accounting around the two speculative
parses. Exceptions that propagate, including `Decimal` overflow, are recorded explicitly before
they leave the parser.

Related: ClickHouse/support-escalation#8208
Alternative to: ClickHouse#109981
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
The `Values` streaming parser uses parse exceptions to fall back to SQL expression parsing.
Those handled exceptions were recorded in `system.errors` and `system.error_log` even when the
insert succeeded.

Add a thread-local scope that suppresses system error accounting around the two speculative
parses. Exceptions that propagate, including `Decimal` overflow, are recorded explicitly before
they leave the parser.

Related: ClickHouse/support-escalation#8208
Alternative to: ClickHouse#109981
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants