Skip to content

Make TTL elements, codecs, statistics and index types plain AST nodes for generic tree walks - #126056

Draft
azat wants to merge 10 commits into
ClickHouse:masterfrom
azat:ast-ttl-element-use-children
Draft

azat wants to merge 10 commits into
ClickHouse:masterfrom
azat:ast-ttl-element-use-children

Conversation

@azat

@azat azat commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

This change should be mostly cosmetic, it changes some corner cases, but it should be backward compatible

Changes

  • ASTTTLElement does not uses children, hence requires special tree walk - store all nodes in children
  • This uncovers few cases like you can replace i.e. ZSTD (CODEC children) with UDFs, so now there are separate nodes for ASTCodec and ASTNameWithParameters
  • CODEC(...)/STATISTICS(...)/TYPE no longer accept function-only syntax (window OVER, IGNORE NULLS, trim(BOTH …), parametric f(1)(2)), which were never valid codecs/types anyway and are now syntax errors instead of later UNKNOWN_CODEC/INCORRECT_QUERY
  • system.data_skipping_indices.type_full no longer appends empty parenthesis for implicitly created indexes (previously it lacks no_empty_args) so now it matches with the explicitly created indexes
    • In RMT prio d5b2ebc they were written to ZK, but we have a normalization for them, and there is a backward compatibility test

Changelog category (leave one):

  • Improvement

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

A SQL UDF whose name coincides with a compression codec or a skip-index type (e.g. ZSTD, minmax) no longer breaks CREATE TABLE/ALTER TABLE statements that use that codec or index. system.data_skipping_indices.type_full shows an implicit minmax without extra parenthesis (minmax())


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

@clickhouse-gh

clickhouse-gh Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [55a59fa]

Summary: ❌

job_name test_name status info comment
Style check FAIL
ruff FAIL cidb
Fast test FAIL
Build ClickHouse FAIL
Build (arm_tidy, 1/4) FAIL
Build ClickHouse FAIL cidb
Build (arm_tidy, 3/4) FAIL
Build ClickHouse FAIL cidb
Code Review DROPPED
Build (amd_debug) DROPPED
Build (amd_asan_ubsan) DROPPED
Build (amd_tsan) DROPPED
Build (amd_msan) DROPPED
Build (amd_binary) DROPPED

@clickhouse-gh clickhouse-gh Bot added pr-not-for-changelog This PR should not be mentioned in the changelog comp-sql-syntax SQL/grammar parsing, AST nodes, syntax-level features. labels Oct 15, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing bac05e49a with master 927e545cc (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

⚠️ Significant changes: object file sizes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes ⚠️

43 object files changed (+366.66 KiB total), 2 added.

Object file Master PR Δ
src/Parsers/CMakeFiles/clickhouse_parsers.dir/ASTNameWithParameters.cpp.o new 307.15 KiB +307.15 KiB
src/Parsers/CMakeFiles/clickhouse_parsers.dir/ASTCodec.cpp.o new 84.79 KiB +84.79 KiB

719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

1125 translation units recompiled, 7080 s compile time in total, 1061 of them have a recent master baseline.

Translation units without a recent master baseline:

  • programs/keeper/Keeper.cpp (clickhouse-keeper): 7.2 s

Job report

groeneai added a commit to groeneai/ClickHouse that referenced this pull request Oct 16, 2026
…heck

`* APPLY f` lists `f(c)` expressions, so a later `expr AS `f(c)`` is a second
column, not a redefinition of a starred one (ClickHouse#14739). It now gets a unique
internal name like the explicit `f(c), expr AS `f(c)`` pair, and an outer
`SELECT *` keeps both values. Found by the AST fuzzer oracle on ClickHouse#126056.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
azat and others added 4 commits October 20, 2026 19:09
…ment children

Otherwise every generic tree walk had to special-case ASTTTLElement
`CODEC(LZ4)` became a child of `ASTTTLElement`, so the recursive screen behind
`readExpressionChild` started walking into it and rejecting `LZ4`:

    ALTER TABLE t MODIFY TTL d + toIntervalDay(1) RECOMPRESS CODEC(LZ4)
    Code: 36. Function 'LZ4' for key 'ttl' has no 'arguments' list during AST JSON deserialization

The slot is already validated by `readSpecialFunctionChild` in
`ASTTTLElement::readJSON`, so skip just that child and keep screening the rest
(the `GROUP BY` keys are now screened too, which they were not before).

Fixes 05230_ast_json_storage_expression_argumentless_function.
Putting the TTL `RECOMPRESS` codec into `ASTTTLElement::children` exposed it to
`UserDefinedSQLFunctionVisitor`, so `CREATE FUNCTION ZSTD AS x -> x + 1` rewrote
`RECOMPRESS CODEC(ZSTD(1))` into `UNKNOWN_CODEC: Unknown codec family: plus`,
and a parameterless `CODEC(LZ4)` into an out-of-range error - it has no
`arguments` child. Column `CODEC`/`STATISTICS` were already affected.

Codec and statistics elements are names, not expressions: skip those subtrees.

Refs: ClickHouse#126056 (comment)
…rs instead of ASTFunction

`CODEC(LZ4, ZSTD(3))` was an `ASTFunction` of `Kind::CODEC` whose elements were
`ASTFunction`s without an `arguments` list - the only such shape the parser
produces - so every generic function pass had to be taught about it (UDF
expansion, the AST JSON screen, `no_empty_args` formatting), the kind was
settable from `clickhouse_json`, and consumers dereferenced a null list.

`ASTCodec` holds a never-empty `ASTExpressionList` of `ASTNameWithParameters`
(name plus optional parameter list); `STATISTICS(...)` and the `TYPE` list of
`ALTER ... STATISTICS` are the same element list without a wrapper. This drops
`Kind::CODEC`/`Kind::STATISTICS`, `readSpecialFunctionChild` and the
`ASTTTLElement` carve-out in the argumentless-function screen.

`TYPE minmax` of a skip index or an index projection was an `ASTFunction`
without `arguments`, so a UDF named `minmax` rewrote it like a call.
`ASTIndexDeclaration::getType` and `ASTProjectionDeclaration::type` are now
`ASTNameWithParameters`; `ParserExpressionWithOptionalArguments` is retired.

Co-Authored-By: Claude <[email protected]>
@azat
azat marked this pull request as draft October 20, 2026 21:51
@azat
azat force-pushed the ast-ttl-element-use-children branch from b656052 to 163f448 Compare October 20, 2026 22:05
@azat azat changed the title Keep TTL GROUP BY keys, assignments and RECOMPRESS codec in ASTTTLElement children Make TTL elements, codecs, statistics and index types plain AST nodes for generic tree walks Oct 20, 2026
azat added 2 commits October 21, 2026 08:47
…st tests to the new grammar

`Kind::CODEC`/`Kind::STATISTICS` used to force `allow_operators = false`; keep that so the
stored codec text is unchanged (04241). Window functions, NULLS modifiers and `trim(BOTH ...)`
no longer parse inside `CODEC`/`STATISTICS`/`TYPE` (04356, 05175), and an implicit minmax
index now reports `type_full` as `minmax` like a declared one (03748, 03792).
}

node = function_node;
node = make_intrusive<ASTNameWithParameters>(getIdentifierName(name), parameters ? std::move(parameters->children) : ASTs{});

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.

[P1] Preserve loading of previously accepted table metadata

The stricter grammar also runs when loading existing tables, and the removed forms were not all rejected by the old interpreters. For example, the previous fast-test binary successfully creates and populates:

CREATE TABLE t (x UInt64 STATISTICS(tdigest() IGNORE NULLS))
ENGINE = MergeTree ORDER BY x;
INSERT INTO t VALUES (1), (2);

SHOW CREATE TABLE retains STATISTICS(tdigest() IGNORE NULLS). I verified that the old binary reloads the persisted table and returns both rows, while this head's binary opening the same data directory exits with code 62: Cannot parse definition from metadata file, at IGNORE. STATISTICS(tdigest(1)(2)) is also accepted and persisted before this change; so is CODEC(Delta() IGNORE NULLS, ZSTD) on Tuple(UInt32, UInt64), where codec preprocessing preserves the original descriptor.

Even if these modifiers have no useful semantics, rejecting their stored definitions makes existing data inaccessible after upgrade, contrary to the backward compatibility promised here. Please preserve parsing/normalization of legacy descriptors during metadata loading and add a focused ATTACH regression for an old accepted definition. Restrictions on new definitions can be enforced separately.

@clickhouse-gh clickhouse-gh Bot added pr-improvement Pull request with some product improvements and removed pr-not-for-changelog This PR should not be mentioned in the changelog labels Oct 21, 2026
…on calls

Until 26.11 these slots held an `ASTFunction`, so stored metadata can spell a name as
`name(x)(y)` - the second list is the one the factories read - or carry `IGNORE NULLS` /
`RESPECT NULLS` / `OVER ...`, which nothing read. `ParserNameWithParameters` accepts those
tails and drops them, marking the node `legacy_syntax`; formatting writes the canonical name.

A new definition is still rejected: `rejectLegacyNameSyntax` runs in the create (non-attach),
alter and create-index interpreters, which no stored-metadata path goes through.
@azat
azat force-pushed the ast-ttl-element-use-children branch from 99d38dc to a865c2f Compare October 21, 2026 11:55

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. pr-improvement Pull request with some product improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant