Repository navigation
Log a warning when the MATERIALIZED CTE keyword is ignored because enable_materialized_cte is disabled - #118032
Conversation
When `enable_materialized_cte` is disabled, a CTE declared as `MATERIALIZED` is silently inlined at each reference like an ordinary CTE. `QueryAnalyzer::tryResolveIdentifierFromCTE` now logs a warning for every such reference so the ignored keyword is visible in the server log and to clients with `send_logs_level` set to `warning` or lower. Also describe both states of the setting in its declaration in `Settings.cpp` and in the `WITH` documentation embedded in `ParserWithElement.cpp`. Co-Authored-By: Claude Fable 5.1 <[email protected]>
…the setting `04561_materialized_cte_totals_gate` and `04816_promql_shared_subqueries_materialized` deliberately run `MATERIALIZED` CTEs with `enable_materialized_cte = 0`. The analyzer now logs a warning for that, which the test harness would report as unexpected stderr, so those statements run with `send_logs_level` set to `error`. Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
Workflow [PR], commit [fd44476] Summary: ✅
AI ReviewSummaryThe PR adds a warning when a Findings
💡 Nits
Tests
Final VerdictChanges requested. One runtime-mode gap still preserves the old silent-ignore behavior, the LLVM Coverage Report |
| DROP TABLE IF EXISTS t_04561_plan SYNC; | ||
| CREATE TABLE t_04561_plan (id UInt64) ENGINE = Memory; | ||
| " | ||
| # With the setting disabled the analyzer warns that MATERIALIZED is ignored; keep it out of stderr. |
There was a problem hiding this comment.
This patch teaches the existing tests to ignore the new warning, but it still does not add a focused regression that proves the warning is actually emitted. Since the only user-visible contract here is "warn when MATERIALIZED is ignored under enable_materialized_cte = 0", I think we need one small .sh coverage point that runs with --send_logs_level=warning, asserts the warning is present when the setting is 0, and absent when it is 1. A yes/no grep like 04869_materialized_ephemeral_stale_warning.sh should be enough; I would avoid asserting an exact count because analysis can happen more than once per query.
`inlineMaterializedCTEIfNeeded` inlines a `MATERIALIZED` CTE that is referenced only once, so the description must not claim that every reference reads from a temporary table when the setting is enabled. Co-Authored-By: Claude Fable 5.1 <[email protected]>
| { | ||
| LOG_WARNING( | ||
| getLogger("QueryAnalyzer"), | ||
| "CTE '{}' is declared as MATERIALIZED, but the setting 'enable_materialized_cte' is disabled: " |
There was a problem hiding this comment.
The new warning only covers the query-tree resolver path. With enable_analyzer = 0, InterpreterSelectQuery still accepts WITH ... AS MATERIALIZED (it only rejects WITH RECURSIVE at InterpreterSelectQuery.cpp:595-598), and ApplyWithSubqueryVisitor copies ast_with_elem->subquery without consulting ASTWithElement::is_materialized (ApplyWithSubqueryVisitor.cpp:44-51). That means WITH t AS MATERIALIZED (...) SELECT ... SETTINGS enable_analyzer = 0 still keeps the old silent-ignore behavior.
Since the user-facing contract here is “ignored MATERIALIZED should be visible”, I think the old-analyzer path needs the same signal too, either by throwing UNSUPPORTED_METHOD or by emitting a matching warning before ApplyWithSubqueryVisitor strips the flag. A tiny SETTINGS enable_analyzer = 0 regression would then pin whichever behavior you choose.
There was a problem hiding this comment.
The old analyzer does not support MATERIALIZED CTEs, so there is no point in addressing this comment.
| <Note> | ||
| Materialized CTEs are an **experimental** feature. | ||
| They require the [analyzer](/guides/clickhouse/performance-and-monitoring/analyzer) and the setting `enable_materialized_cte` to be enabled. | ||
| If the setting is disabled, the `MATERIALIZED` keyword is ignored: the CTE is inlined at each reference like an ordinary CTE, and a warning is logged. |
There was a problem hiding this comment.
The new note covers the disabled-setting case, but the opening paragraph just above still says every MATERIALIZED CTE is executed once, stored in a temporary table, and read from there. That contradicts inlineMaterializedCTEIfNeeded, which inlines single-use CTEs even when enable_materialized_cte = 1, so the generated page will still describe a stronger contract than the planner implements.
Can we mirror the softened Settings.cpp wording here too, so the WITH docs stay internally consistent?
There was a problem hiding this comment.
The qualification is already on this page: the <Tip> in the "When to use" section right below says that a materialized CTE referenced only once is inlined back into a regular subquery. The opening sentence is pre-existing text and is not changed by this PR, so leaving it as is.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units9 translation units recompiled, 30 s compile time in total, 9 of them have a recent master baseline. |
When
enable_materialized_cteis disabled (the default), a CTE declared asWITH name AS MATERIALIZED (...)is silently inlined at each reference like an ordinary CTE. Nothing in the query result, the plan, or the logs indicated that the keyword had no effect, so a user who forgot to enable the setting had no way to notice.QueryAnalyzer::tryResolveIdentifierFromCTEis the only place the setting is consulted forSELECTqueries. It now logs a warning for every reference to aMATERIALIZEDCTE that is resolved with the setting disabled:The message reaches the server log and clients that run with
send_logs_levelset towarningor lower. The description ofenable_materialized_cteand the embedded documentation ofWITHinParserWithElement.cppnow describe both states of the setting (thedocs/pages are regenerated from these sources by the autogeneration workflow and are not edited here).Two existing tests deliberately run
MATERIALIZEDCTEs with the setting disabled (04561_materialized_cte_totals_gate,04816_promql_shared_subqueries_materialized); those statements now run withsend_logs_level = 'error'so the expected warning does not appear as unexpected stderr. No new test asserts on the log message itself. Other paths were already loud or intentional:WITH RECURSIVE ... AS MATERIALIZEDthrowsUNSUPPORTED_METHODregardless of the setting,StorageInputalready explains the hint semantics in its error text, and the PromQL storages force-enable the setting unless the user changed it explicitly.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
A warning is now logged when a CTE is declared as
MATERIALIZEDbut the settingenable_materialized_cteis disabled, in which case the keyword is ignored and the CTE is inlined at each reference. Previously the keyword was silently ignored.Workflow [PR]
Sync PR [sync-upstream/pr/118032]