Skip to content

Reject a TTL where_expr on non-DELETE elements in AST JSON deserialization - #125475

Merged
PedroTadim merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-ast-json-ttl-where-only-for-delete
Oct 22, 2026
Merged

PedroTadim merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-ast-json-ttl-where-only-for-delete

Conversation

@groeneai

@groeneai groeneai commented Oct 12, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #108069

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

The clickhouse_json dialect and formatQueryFromJSON now reject a TTL element that has a where_expr with a mode other than DELETE (TO DISK/TO VOLUME, RECOMPRESS, GROUP BY) with BAD_ARGUMENTS, as SQL parsing does, instead of storing table metadata that cannot be parsed at the next server start.

Description

Requested in #108069 (comment), which adds the same check for its new CLEAR INDEX mode.

ParserTTLElement accepts WHERE only after DELETE, and TTLDescription applies it only for DELETE, but ASTTTLElement::readJSON restored where_expr for every mode. With dialect = 'clickhouse_json', a CREATE TABLE ... TTL d TO DISK 'default' whose JSON carries a where_expr was accepted and stored as TTL d TO DISK 'default' WHERE x, so the next start failed with Code: 62 ... failed at position 150 (WHERE) and the database did not load. The same happened for RECOMPRESS and GROUP BY. formatQueryFromJSON returned the same unparsable SQL.

readJSON now rejects where_expr unless the mode is DELETE, next to the existing per-mode checks for the codec, destination and GROUP BY fields. The check is mode != DELETE, so it also covers the CLEAR INDEX mode from #108069.

The new test rejects all three modes on ALTER ... MODIFY TTL and CREATE TABLE, and keeps DELETE ... WHERE, both from SQL and from JSON. On master the four rejected cases return SQL instead of an error. It passes 50 randomized runs, and all 92 AST JSON tests pass. Category: the harmful path needs the experimental enable_json_ast_dialect, so this is an Improvement.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.11.1.574-master (included in 26.11 and later)

…alization

`ParserTTLElement` accepts `WHERE` only after `DELETE`, and `TTLDescription`
applies it only for `DELETE`, but `ASTTTLElement::readJSON` restored
`where_expr` for every mode. A `clickhouse_json` `CREATE TABLE ... TTL d TO DISK
'default'` carrying a `where_expr` was therefore accepted, the `WHERE` was
ignored, and `formatImpl` stored `TTL d TO DISK 'default' WHERE x`, which fails
to parse at the next start (`Code: 62`), so the database does not load. Same for
`RECOMPRESS` and `GROUP BY`; `formatQueryFromJSON` returned the same
unparsable SQL.

Reject `where_expr` unless the mode is `DELETE`, like the existing per-mode
checks for the codec, destination and `GROUP BY` fields in the same function.
The check sits at the read site so it does not textually overlap ClickHouse#108069 (which
adds the same check for its new `CLEAR INDEX` mode) or ClickHouse#121585.

Requested in ClickHouse#108069 (comment)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-request PR origin: a maintainer pinged or directed groeneai labels Oct 12, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review (click to expand)

Own cold review + Gate B (codex) on 6919dd4c10a1d: 0 blockers, 0 majors. Gate B: 0 findings.

Finding Verdict
💡 if_exists on a non-MOVE TTL element is also parser-impossible, but formatImpl drops it and only move TTLs read it, so it has no effect. Noted, out of scope
💡 Once #108069 merges, its CLEAR INDEX where_expr check is reached after this one, so this check fires first (same error code). Noted, harmless

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. formatQueryFromJSON(replace(parseQueryToJSON('ALTER TABLE t MODIFY TTL d TO DISK ''fast'''), <ttl_expr>, <ttl_expr + where_expr>)) returns ALTER TABLE t (MODIFY TTL d TO DISK 'fast' WHERE x) on master, every time; same for RECOMPRESS, GROUP BY and CREATE TABLE.
b Root cause explained? ParserTTLElement parses WHERE only after DELETE and TTLDescription applies it only for DELETE, but ASTTTLElement::readJSON restored where_expr for every mode, and formatImpl then writes a WHERE the parser cannot read back.
c Fix matches root cause? Yes. The missing per-mode check at the JSON boundary, next to the existing codec, destination and GROUP BY checks in the same function.
d Test intent preserved / new tests added? New 05367_ast_json_ttl_where_only_for_delete: 2 accepted DELETE cases (one proves the injected JSON is well-formed), 4 rejected cases.
e Both directions demonstrated? Yes. On master the 4 rejected cases return SQL; with the fix they fail with BAD_ARGUMENTS. End to end with dialect = 'clickhouse_json': master accepts the CREATE and the next start fails with Code: 62 on the stored WHERE; with the fix the CREATE is rejected and the next start loads.
f Fix is general across code paths? Yes. Every JSON route builds TTLElement through the one factory and this readJSON; the parser and the TimeSeries normalizer never set a non-DELETE WHERE.
g Fix generalizes across inputs (params/datatypes/wrappers)? All non-DELETE modes (TO DISK, RECOMPRESS, GROUP BY ... SET) on ALTER ... MODIFY TTL and CREATE TABLE; mode != DELETE also covers CLEAR INDEX from #108069. The WHERE content does not matter.
h Backward compatible? (maintainer-approved exception only) Yes. Only parser-impossible JSON is rejected; JSON produced by parseQueryToJSON never has it. No setting, no stored format change.
i Invariants and contracts preserved? "A TTL element has a WHERE only for DELETE" now holds for JSON input too; the check runs before setWhere, so no partly built element escapes.

Session id: cron:clickhouse-impl-slot-4:20261012-180800

@clickhouse-gh

clickhouse-gh Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e37acfd]

Summary: ✅


AI Review

Summary

PR #125475 rejects where_expr on non-DELETE TTL elements during JSON AST deserialization, preventing ignored predicates and unparsable stored metadata. No actionable findings remain at head e37acfd5740d13a992ca4bcf3564de5feb0fb31a. The focused regression test passes in the Fast test report.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-improvement Pull request with some product improvements comp-sql-syntax SQL/grammar parsing, AST nodes, syntax-level features. labels Oct 12, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 6919dd4

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Stateless tests (arm_binary, ParallelReplicas, s3 storage, parallel) / 04034_autopr_min_rows_per_replica_reuses_index_analysis flaky, not from this PR (it only rejects a TTL where_expr on non-DELETE elements in AST JSON deserialization): the automatic parallel replicas decision (pr_used) flipped from 1 to 0 on queries 1 and 2, and 36 of 67 reruns with the same randomized settings failed a fix task is created (investigating at full effort, fixing-PR link to follow here)
Integration tests (arm_binary, 1/4) / test_storage_iceberg_concurrent/test_concurrent_reads.py::test_concurrent_reads not from this PR: Spark's own CREATE TABLE hung until the 900 s pytest timeout, on the xdist worker that had run test_storage_delta/test_writes_interop.py earlier (the reused Spark JVM keeps a Hadoop filesystem bound to that module's MinIO); it failed on about 45 other PRs and 23 master runs since yesterday #125448 (mine, open)
Mergeable Check, PR praktika rollups of the rows above #125448 (mine, open); a fix task is created for the 04034 row

CIDB has no other FAIL or ERROR row on this head.

Session id: cron:our-pr-ci-monitor:20261013-013014

@groeneai

Copy link
Copy Markdown
Collaborator Author

Fixing PR for the 04034_autopr_min_rows_per_replica_reuses_index_analysis row of the ledger above: #125544.

huldarchen pushed a commit to huldarchen/ClickHouse that referenced this pull request Oct 19, 2026
… that read periodic data

`04034_autopr_min_rows_per_replica_reuses_index_analysis`,
`04034_autopr_dataflow_cache_reuse_between_different_queries`,
`05024_autopr_plan_based_parallel_replicas` and
`05059_autopr_settings_in_select_settings_clause` create the same table, insert the same
periodic data (`number % 1000 / 500 / 200`) and assert that the query reusing the collected
statistics chooses parallel replicas. With the tests' pinned `max_threads = 4` and
`max_parallel_replicas = 3` the gate in `considerEnablingParallelReplicas` reduces to
`input_bytes > 2 * output_bytes`, where `input_bytes` is the compressed size of the read.

A `Wide` part (`min_bytes_for_wide_part = 0`) with a ZSTD server codec compresses the 7 MB
read to ~131 KB, so the gate depends on the output estimate, which two-level aggregation makes
noisy: on the settings of the failing job of ClickHouse#125475 input was 131499 in every run, output took
values from 7735 to 76000, and every run with output above 65749 declined parallel replicas
(13 of 40 locally, 36 of 67 in CI). ClickHouse#120190 raised the aggregation state estimate of this query
from 12000 to 30000 bytes, which moved the high end of the output estimate up by 18000 (54636
before, 76000 after; 0 of 80 failures on a master build from before it). That is why the four
tests started failing on master and on unrelated PRs on 2026-10-12.

`CODEC(NONE)` on `WatchID` keeps the read large whatever the part format, server codec, row
order, compress block sizes or sparse serialization of a run: input is 2.4 MB or more in every
measured configuration (2942464 on the failing job's settings), at least a ~16x margin. Queries,
settings and references are unchanged, and no setting is pinned. Same approach as
`05045_autopr_skip_plan_build_for_small_reads` (e8588c7).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@PedroTadim PedroTadim self-assigned this Oct 22, 2026
@PedroTadim
PedroTadim enabled auto-merge October 22, 2026 15:42
@PedroTadim
PedroTadim added this pull request to the merge queue Oct 22, 2026
Merged via the queue into ClickHouse:master with commit 346d51b Oct 22, 2026
178 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 22, 2026
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. groeneai-origin-request PR origin: a maintainer pinged or directed groeneai pr-improvement Pull request with some product improvements 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