Skip to content

Log a warning when the MATERIALIZED CTE keyword is ignored because enable_materialized_cte is disabled - #118032

Merged
novikd merged 3 commits into
masterfrom
materialized-cte-disabled-warning
Sep 7, 2026
Merged

novikd merged 3 commits into
masterfrom
materialized-cte-disabled-warning

Conversation

@novikd

@novikd novikd commented Sep 4, 2026 •

Copy link
Copy Markdown
Member

When enable_materialized_cte is disabled (the default), a CTE declared as WITH 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::tryResolveIdentifierFromCTE is the only place the setting is consulted for SELECT queries. It now logs a warning for every reference to a MATERIALIZED CTE that is resolved with the setting disabled:

<Warning> QueryAnalyzer: CTE 'cte' is declared as MATERIALIZED, but the setting 'enable_materialized_cte' is disabled: the MATERIALIZED keyword is ignored and the CTE is inlined at each reference

The message reaches the server log and clients that run with send_logs_level set to warning or lower. The description of enable_materialized_cte and the embedded documentation of WITH in ParserWithElement.cpp now describe both states of the setting (the docs/ pages are regenerated from these sources by the autogeneration workflow and are not edited here).

Two existing tests deliberately run MATERIALIZED CTEs with the setting disabled (04561_materialized_cte_totals_gate, 04816_promql_shared_subqueries_materialized); those statements now run with send_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 MATERIALIZED throws UNSUPPORTED_METHOD regardless of the setting, StorageInput already 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):

  • Improvement

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 MATERIALIZED but the setting enable_materialized_cte is 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]

novikd and others added 2 commits September 4, 2026 11:31
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]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [fd44476]

Summary: ✅


AI Review

Summary

The PR adds a warning when a MATERIALIZED CTE is ignored because enable_materialized_cte is disabled, updates the source documentation, and suppresses the new warning in two existing tests. I’m not ready to approve it yet: the old-analyzer path still silently ignores MATERIALIZED, the WITH documentation still overstates the enabled behavior, and the new warning itself is still missing focused regression coverage.

Findings

⚠️ Majors

  • [src/Analyzer/Resolve/QueryAnalyzer.cpp:1347] 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), and ApplyWithSubqueryVisitor copies ast_with_elem->subquery without consulting ASTWithElement::is_materialized, so WITH t AS MATERIALIZED (...) SELECT ... SETTINGS enable_analyzer = 0 still silently degrades to an ordinary CTE. That leaves one whole analyzer mode with the original silent-ignore behavior this PR is trying to eliminate.
  • Suggested fix: either reject ASTWithElement::is_materialized on the old-analyzer path with UNSUPPORTED_METHOD, or emit a matching warning there before ApplyWithSubqueryVisitor strips the flag. Add one focused enable_analyzer = 0 regression for the chosen behavior.

💡 Nits

  • [src/Parsers/ParserWithElement.cpp:144-150] The new note explains what happens when enable_materialized_cte is disabled, but the opening paragraph still says every MATERIALIZED CTE is executed once, stored in a temporary table, and read from there. That is stronger than the actual planner contract, because inlineMaterializedCTEIfNeeded inlines single-use CTEs even when the setting is enabled.
  • Quick fix: mirror the softened wording already used in Settings.cpp so the generated WITH documentation stays internally consistent.
Tests
  • ⚠️ [tests/queries/0_stateless/04561_materialized_cte_totals_gate.sh:16] The change still has no focused regression proving that the warning is emitted when enable_materialized_cte = 0 and absent when it is 1. The current test edits only suppress the new warning in unrelated coverage. Add a small .sh test that runs with send_logs_level = 'warning' and greps for presence/absence rather than an exact warning count.
Final Verdict

Changes requested. One runtime-mode gap still preserves the old silent-ignore behavior, the WITH docs remain slightly stronger than the implementation, and the new warning is not yet pinned by a dedicated regression.

LLVM Coverage Report

⚠️ No coverage measurement for commit fd44476: incomplete coverage measurement: 1 of 21 shard profiles are missing: LLVM_COVERAGE_FILE_it_7.profdata.

@clickhouse-gh clickhouse-gh Bot added the pr-experimental Experimental Feature label Sep 4, 2026
Comment thread src/Core/Settings.cpp Resolved
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.

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.

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]>
@clickhouse-gh clickhouse-gh Bot added pr-improvement Pull request with some product improvements and removed pr-experimental Experimental Feature labels Sep 4, 2026
{
LOG_WARNING(
getLogger("QueryAnalyzer"),
"CTE '{}' is declared as MATERIALIZED, but the setting 'enable_materialized_cte' is disabled: "

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.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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.

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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@clickhouse-gh

clickhouse-gh Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

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

✅ No significant changes.

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.

Compile time of recompiled translation units

9 translation units recompiled, 30 s compile time in total, 9 of them have a recent master baseline.

Job report

@clickhouse-gh clickhouse-gh Bot added the comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). label Sep 4, 2026
@novikd
novikd added this pull request to the merge queue Sep 7, 2026
Merged via the queue into master with commit aeee21c Sep 7, 2026
174 checks passed
@novikd
novikd deleted the materialized-cte-disabled-warning branch September 7, 2026 16:27
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). 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.

2 participants