Repository navigation
Conversation
The eval table function parses and stores the generated inner SELECT but,
unlike executeQueryImpl, never applied the AST size limits. That made
max_ast_depth and max_ast_elements ineffective for the inner query: a tiny
outer SELECT * FROM eval('...') could smuggle a huge or very deep AST into
the analyzer, even though executing the same inner query directly would be
rejected.
Apply checkDepth/checkSize to the generated query, reading the limits from a
context that already has the generated query's own SETTINGS applied (resolved
before the normalization visitors rewrite the tree), so an inner SETTINGS
clause controls its own limits the same way it does for a standalone query.
Follow-up to ClickHouse#110132.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-0:20260713-042100 |
|
cc @alexey-milovidov — follow-up to #110132 as requested: applies the AST size limits to the query generated by |
|
Workflow [PR], commit [51f0d7c] Summary: ❌
AI ReviewSummaryThe PR applies AST size limits and strict identifier validation to queries generated by Missing context / blind spots
Findings
Final Verdict✅ Approve the scoped change, subject to normal CI completion. The retained construction-settings issue is an accepted, pre-existing follow-up. |
The generated query's INTERSECT/EXCEPT and UNION normalization ran with the outer context defaults, so an inner SETTINGS union_default_mode = 'DISTINCT' still threw EXPECTED_ALL_OR_DISTINCT even though the same query executes fine directly. Drive the normalization visitors from limits_context (the inner query's resolved SETTINGS), same as the AST size limits already do. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Fixed in f502546. The set-operation normalization visitors now read Verified both directions with your repro on a debug build:
Added a regression in Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-2:20260713-083200 |
Mirror executeQueryImpl ordering: run ApplyWithGlobalVisitor (gated by the inner query's enable_global_with_statement) before the AST size checks, so a global CTE that stays small before expansion but grows past max_ast_elements once inlined into every UNION branch is rejected in eval like in a direct query. Add a regression to 04512_eval_table_function. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Fixed in efef3e9. Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-1:20260713-093100 |
| -- non-deterministic and potentially reading a system table, and is not cached by default. | ||
| SELECT * FROM eval('SELECT now()') SETTINGS use_query_cache = 1; -- { serverError QUERY_CACHE_USED_WITH_NONDETERMINISTIC_FUNCTIONS } | ||
| SELECT * FROM eval('SELECT * FROM system.one') SETTINGS use_query_cache = 1, query_cache_nondeterministic_function_handling = 'save'; -- { serverError QUERY_CACHE_USED_WITH_SYSTEM_TABLE } | ||
|
|
There was a problem hiding this comment.
Could you add a new test instead of modifying existing one?
There was a problem hiding this comment.
Done in a755995. Moved the new regressions into a dedicated 04513_eval_table_function_ast_limits.sql; 04512_eval_table_function.sql/.reference are restored to their pre-PR content.
Pre-PR validation gate (click to expand)
| # | Check | Result |
|---|---|---|
| a | Deterministic repro? | N/A (test-file reorganization, no code change) |
| b | Root cause explained? | N/A |
| c | Fix matches root cause? | N/A |
| d | Test intent preserved / new tests added? | Yes: the seven assertions are byte-identical, only relocated; 04512 is back to its pre-PR state |
| e | Demonstrated both directions? | Yes: all 7 assertions verified against the fixed binary (inner DISTINCT -> 1, ambiguous UNION -> EXPECTED_ALL_OR_DISTINCT, outer/inner max_ast_elements -> TOO_BIG_AST, max_ast_depth -> TOO_DEEP_AST, inner relax -> 55, global-WITH expansion -> TOO_BIG_AST) |
| f | Fix general, not a narrow patch? | N/A (no code bug) |
| g | Generalizes across inputs/types? | N/A |
| h | Backward compatible? | N/A |
| i | Invariants/contracts preserved? | Yes: new test is parallel-safe (no shared state, no no-parallel) |
Session id: cron:clickhouse-worker-slot-1:20260713-113700
Move the union_default_mode / max_ast_* / global-WITH regressions added by this PR out of 04512_eval_table_function into a new 04513 test, restoring 04512 to its pre-PR content, as requested in review. Co-Authored-By: Claude Opus 4.8 <[email protected]>
CI finish ledger — a755995Every failure below has an owner: a fixing PR (ours), or the private sync (exempt).
Session id: cron:our-pr-ci-monitor:20260713-183000 |
The intersect_default_mode / except_default_mode branches of SelectIntersectExceptQueryVisitor are driven from the inner query's own settings, but 04513 only exercised UNION. Add a duplicate-sensitive INTERSECT case and an EXCEPT case with inner default mode ALL over an outer DISTINCT, so eval keeps duplicates the same way direct execution does. Co-Authored-By: Claude Opus 4.8 <[email protected]>
CI finish ledger — d9ed60bEvery failure below has an owner. Only
Session id: cron:our-pr-ci-monitor:20260714-093000 |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 16/16 (100.00%) · Uncovered code |
CI finish ledger — 442563cEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
This is a lane-reachability regression, not a defect in this PR: the test became reachable on the Session id: cron:our-pr-ci-monitor:20260729-060000 |
|
Dear @antaljanosbenjamin, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself. |
|
@groeneai can you merge latest master? |
Requested by @ antaljanosbenjamin on the pull request. Conflict in `src/TableFunctions/TableFunctionEval.cpp`: master added the construction-settings passes to `eval` (`wrapPerArmConstructionSettings`, `wrapNestedConstructionSettings`) in the same region this branch adds the AST size limits and drives the set-operation normalization from the generated query's own settings. Both are kept, ordered as in `executeQueryImpl`: the per-arm pass stays ahead of the private context built by `InterpreterSetQuery::applySettingsFromQuery`, so per-arm settings are not read as query-level ones, and the size limits run last, after `wrapNestedConstructionSettings`, so the AST that gets measured is the wrapped one. Nothing from master is reverted: the merge result differs from master by this branch's three files only. Co-Authored-By: Claude Opus 5 <[email protected]>
|
Merged master ( |
…n `eval`
`executeQueryImpl` runs the `enforce_strict_identifier_format` formatter pass
after a query's own `SETTINGS` clause is applied and before analysis, but
`TableFunctionEval::parseArguments` did not, so the setting was bypassed by
moving the query text into `eval`. With `enforce_strict_identifier_format = 1`,
`SELECT 1 AS "bad-name"` is rejected with `BAD_ARGUMENTS`, while
`SELECT * FROM eval('SELECT 1 AS "bad-name"')` returned a `bad-name` column.
`view(SELECT 1 AS "bad-name")` and a plain subquery are both rejected, because
their inner query is part of the outer AST that the pass formats, so `eval` was
the only mechanism that let such an identifier through.
Run the pass on the generated query, reading the flag from the generated query's
own resolved settings so an inner `SETTINGS enforce_strict_identifier_format = 1`
also applies. It runs next to the AST size limits, after the construction
settings are materialized, so it sees the AST that is stored and analyzed. That
also covers an identifier introduced only by the generated query's own `filter`
or `order`, which `executeQueryImpl` checks too. A generated query that is both
ambiguous in its set operation mode and carries a rejected identifier reports the
normalization error first, because `eval` has to resolve the modes from the inner
settings before it can measure the AST.
Reported by clickhouse-gh[bot] on the pull request.
Also trim the comments this pull request added in `parseArguments` down to the
local invariants; the motivation for each pass belongs in the commit messages.
Co-Authored-By: Claude Opus 5 <[email protected]>
| } | ||
| { | ||
| NormalizeSelectWithUnionQueryVisitor::Data data{settings[Setting::union_default_mode]}; | ||
| NormalizeSelectWithUnionQueryVisitor::Data data{inner_settings[Setting::union_default_mode]}; |
There was a problem hiding this comment.
union_default_mode now comes from inner_settings, but it still only reaches the AST that exists before wrapNestedConstructionSettings. Direct execution does the opposite: it materializes SETTINGS filter / select / order first and only then runs SelectIntersectExceptQueryVisitor / NormalizeSelectWithUnionQueryVisitor (executeQuery.cpp:2725-2784), so set operations introduced by those snippets inherit the generated query's own defaults too.
That means eval still diverges on this surface. A minimal repro is:
SELECT *
FROM eval(
'SELECT 1 AS x SETTINGS filter = ''1 IN (SELECT 1 UNION SELECT 1)'', union_default_mode = ''DISTINCT'''
);The scalar subquery inside filter is parsed only in wrapNestedConstructionSettings, after the normalization pass here has already run, so the later StorageView/analyzer path sees that UNION with union_default_mode still unspecified (or from the outer context) even though direct execution normalizes it as DISTINCT. The same hole exists for INTERSECT / EXCEPT subqueries introduced by construction settings. Please rerun the set-operation visitors on the wrapped AST, or move wrapNestedConstructionSettings ahead of them, and add a focused regression around a construction-setting-generated subquery.
There was a problem hiding this comment.
Confirmed, and it is wider than union_default_mode: any set operation a construction snippet introduces is rejected outright, whatever its mode.
The parser only fills list_of_modes and leaves union_mode at UNION_DEFAULT; SelectIntersectExceptQueryVisitor and NormalizeSelectWithUnionQueryVisitor are what resolve it. A set operation that never reaches them arrives at UnionNode's constructor as a *_DEFAULT mode and throws there (src/Analyzer/UnionNode.cpp:53). So the snippet-introduced case does not inherit a wrong mode, it fails. Every row below is SELECT 1 AS x SETTINGS filter = '<snippet>', run directly and through eval:
<snippet> |
direct | eval |
|---|---|---|
1 IN (SELECT 1 UNION SELECT 1), plus union_default_mode = 'DISTINCT' |
1 |
36 BAD_ARGUMENTS |
1 IN (SELECT 1 UNION ALL SELECT 1) |
1 |
36 |
1 IN (SELECT 1 INTERSECT SELECT 1) |
1 |
36 |
1 IN (SELECT 1 EXCEPT SELECT 2) |
1 |
36 |
1 IN (SELECT 1), control, no set operation |
1 |
1 |
Every cell is identical on a master build at 82cfa6a, which does not contain this PR, so none of it is introduced or worsened here. That is a result and not a blind arm: the same harness does see this PR where the visitors run, with eval('SELECT 1 AS x UNION SELECT 1 AS x SETTINGS union_default_mode = ''DISTINCT''') returning 1 on this head and 558 on that master build.
I am not making that change here. Both remedies you list alter the query-construction settings passes (f447e3a, 4fa3556), whose ordering the comment above wrapPerArmConstructionSettings states is deliberate. A regression test would pin the current failure rather than the fixed behaviour, so I am not adding one either. Both remaining divergences are in the description now, with the measurements, for whoever owns that pass to decide.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 10 s compile time in total, 7 of them have a recent master baseline. |
CI finish ledger — 0b0d3feEvery failure below has an owner: a fixing PR, or a full-effort fix task whose fixing-PR link gets posted here when it opens. Only
No failure is PR-caused: this diff is Session id: cron:our-pr-ci-monitor:20260826-223000 |
Merges master at b306e87 to resolve the conflict in src/TableFunctions/TableFunctionEval.cpp. - Setting externs: master renamed allow_experimental_eval_table_function to enable_eval_table_function and dropped allow_experimental_analyzer (the old analyzer is gone). Kept master's extern and this branch's additional ones. - ApplyWithGlobalVisitor::visit now takes max_expanded_ast_elements (master, e709ca8 / caed1b4). Pass it from the generated query's own settings, the same way executeQueryImpl passes it for a usual query, so the WITH expansion in eval is bounded like in direct execution. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…iscriminating The outer query `SELECT count() FROM eval(...)` needs `max_ast_depth = 9` on its own, so with `max_ast_depth = 3` it was rejected by its own check before `eval` parsed the generated query, and `(((((1)))))` parses to the same depth as `SELECT 1` anyway. The assertion passed with and without the check in `eval`. Use a limit the outer query fits into (15, with a positive control) and a generated query that needs depth 25: without the check `eval` returns 1, with it `TOO_DEEP_AST`, same as executing the generated query directly. Co-Authored-By: Claude Opus 5.5 <[email protected]>
CI finish ledger - 51f0d7cEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
CIDB has no other Session id: cron:our-pr-ci-monitor:20261104-180026 |
|
Fixing PR for Fast test (arm_darwin) / 05495_part_moves_between_shards_step_retry: #129409 |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
The query generated by the
evaltable function is now subject to the same checks as a query executed directly: the AST size limitsmax_ast_depthandmax_ast_elements, the set operation default modes, the order of globalWITHexpansion, andenforce_strict_identifier_format.Description
Follow-up to #110132 (per @ alexey-milovidov's review request on
src/TableFunctions/TableFunctionEval.cpp).evalparses and stores the generated innerSELECT, but unlikeexecuteQueryImplit skipped several prechecks, so a tiny outerSELECT * FROM eval('...')got past them. Each now runs on the generated query from a context that already has that query's ownSETTINGSapplied, so an innerSETTINGSclause controls its own prechecks:max_ast_depth/max_ast_elementswere not applied at all, so a huge or very deep generated AST reached the analyzer.SETTINGS union_default_mode = 'DISTINCT'was rejected although the same query executed directly succeeds. Likewise forintersect_default_modeandexcept_default_mode, which live in separate branches of the visitor.WITHaliases were expanded after the size limits instead of before, so a generated global CTE that grows past the limit once it is inlined slipped through.enforce_strict_identifier_formatwas not applied, soeval('SELECT 1 AS "bad-name"')returned abad-namecolumn whileSELECT 1 AS "bad-name"andview(SELECT 1 AS "bad-name")are both rejected withBAD_ARGUMENTS.Regression tests in
04513_eval_table_function_ast_limits.Two divergences are left, both pre-existing on master, identical with and without this PR, and both belonging to
wrapNestedConstructionSettingsrather than here. On a set operation root it does not consume the trailingSETTINGS, soeval('SELECT number AS n FROM numbers(5) EXCEPT SELECT 1 AS n SETTINGS filter = ''n > 2''')returns 4 rows against the direct query's 2, and the size check then measures the unwrapped tree. And because it runs after the normalization visitors, a set operation that afilter,selectorordersnippet introduces is never normalized and fails withBAD_ARGUMENTS, whether or not its mode is explicit.Workflow [PR]
Sync PR [sync-upstream/pr/110211]