Skip to content

Add Dialect::supports_cte_materialization and enable it for DuckDB - #2651

Merged
LucaCappelletti94 merged 3 commits into
apache:mainfrom
adriangb:cte-materialization-capability
Oct 17, 2026
Merged

LucaCappelletti94 merged 3 commits into
apache:mainfrom
adriangb:cte-materialization-capability

Conversation

@adriangb

Copy link
Copy Markdown
Contributor

WITH c AS [NOT] MATERIALIZED (...) parses only with PostgreSqlDialect, because the parser uses a hard-coded dialect check. DuckDB also accepts this syntax, and custom dialects cannot opt in to it.

let sql = "WITH c AS MATERIALIZED (SELECT 1) SELECT * FROM c";
Parser::parse_sql(&DuckDbDialect {}, sql) // Err: Expected: (, found: MATERIALIZED

This PR replaces the check with a Dialect::supports_cte_materialization method that is off by default. The method returns true for PostgreSQL and DuckDB. GenericDialect still rejects the syntax. Tests check the AST value, the SQL round trip, and the rejection by every dialect that does not opt in.

@codecov-commenter

codecov-commenter commented Oct 17, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.25%. Comparing base (c983513) to head (420fab6).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2651   +/-   ##
=======================================
  Coverage   81.24%   81.25%           
=======================================
  Files          42       42           
  Lines       33901    33910    +9     
  Branches    33901    33910    +9     
=======================================
+ Hits        27543    27553   +10     
  Misses       2792     2792           
+ Partials     3566     3565    -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@adriangb

Copy link
Copy Markdown
Contributor Author

cc @LucaCappelletti94

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

The focused change preserves defaults and includes relevant coverage, with no blocking issues identified; tests were not executed.

No open issues 🎉

What changed in this PR

Makes CTE materialization support configurable by dialect, enabling DuckDB while preserving existing defaults.

Changes:

  • Adds Dialect::supports_cte_materialization, enabled for PostgreSQL and DuckDB.
  • Replaces the parser’s hard-coded PostgreSQL check.
  • Adds AST, round-trip, and unsupported-dialect rejection tests.
File Description
tests/​sqlparser_common.rs Tests materialization directives and rejection.
src/​parser/​mod.rs Uses the dialect capability flag.
src/​dialect/​postgresql.rs Preserves PostgreSQL support.
src/​dialect/​mod.rs Defines the capability and test-wrapper delegation.
src/​dialect/​duckdb.rs Enables DuckDB support.

Review effort: Balanced

Comment thread tests/sqlparser_common.rs
),
] {
let sql = format!("WITH c AS {directive}(SELECT 1) SELECT * FROM c");
let query = all_dialects_where(|d| d.supports_cte_materialization()).verified_query(&sql);

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.

You should add this DuckDB red test, because nothing fails if DuckDbDialect::supports_cte_materialization returns false. DuckDB then moves into the rejecting set and parse_cte_materialization still passes. With this line, that regression fails with Expected: (, found: MATERIALIZED.

Suggested change
let query = all_dialects_where(|d| d.supports_cte_materialization()).verified_query(&sql);
TestedDialects::new(vec![Box::new(DuckDbDialect {})]).verified_query(&sql);
let query = all_dialects_where(|d| d.supports_cte_materialization()).verified_query(&sql);

@LucaCappelletti94 LucaCappelletti94 added enhancement New feature or request waiting on contributor The review needs further refinements by its author labels Oct 17, 2026
Co-Authored-By: LucaCappelletti94 <7738570+LucaCappelletti94@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@adriangb
adriangb force-pushed the cte-materialization-capability branch from db428f8 to 420fab6 Compare October 17, 2026 15:13

@LucaCappelletti94 LucaCappelletti94 left a comment

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.

LGTM! @adriangb if you have time, please do review some of the other PRs.

cc @alamb

@LucaCappelletti94
LucaCappelletti94 added this pull request to the merge queue Oct 17, 2026
Merged via the queue into apache:main with commit 1aabd12 Oct 17, 2026
13 checks passed
@adriangb
adriangb deleted the cte-materialization-capability branch October 17, 2026 15:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

DuckDB enhancement New feature or request waiting on contributor The review needs further refinements by its author

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants