Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/dialect/duckdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ pub struct DuckDbDialect;

// In most cases the redshift dialect is identical to [`PostgresSqlDialect`].
impl Dialect for DuckDbDialect {
fn supports_cte_materialization(&self) -> bool {
true
}

fn supports_trailing_commas(&self) -> bool {
true
}
Expand Down
10 changes: 10 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,16 @@ pub trait Dialect: Debug + Any {
false
}

/// Returns true if the dialect supports `AS MATERIALIZED` and `AS NOT MATERIALIZED`
/// in a common table expression.
///
/// ```sql
/// WITH cte AS MATERIALIZED (SELECT 1) SELECT * FROM cte
/// ```
fn supports_cte_materialization(&self) -> bool {
false
}

/// Returns true if the dialects supports `group sets, roll up, or cube` expressions.
fn supports_group_by_expr(&self) -> bool {
false
Expand Down
4 changes: 4 additions & 0 deletions src/dialect/postgresql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ const AND_PREC: u8 = 20;
const OR_PREC: u8 = 10;

impl Dialect for PostgreSqlDialect {
fn supports_cte_materialization(&self) -> bool {
true
}

fn identifier_quote_style(&self, _identifier: &str) -> Option<char> {
Some('"')
}
Expand Down
2 changes: 1 addition & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15349,7 +15349,7 @@ impl<'a> Parser<'a> {
};

let mut is_materialized = None;
if dialect_of!(self is PostgreSqlDialect) {
if self.dialect.supports_cte_materialization() {
if self.parse_keyword(Keyword::MATERIALIZED) {
is_materialized = Some(CteAsMaterialized::Materialized);
} else if self.parse_keywords(&[Keyword::NOT, Keyword::MATERIALIZED]) {
Expand Down
22 changes: 22 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20682,3 +20682,25 @@ fn parse_array_suffix_after_nested_type() {
})
.verified_stmt("SELECT CAST(NULL AS MAP<INT, STRUCT<a INT>> ARRAY)");
}

#[test]
fn parse_cte_materialization() {
for (directive, expected) in [
("", None),
("MATERIALIZED ", Some(CteAsMaterialized::Materialized)),
(
"NOT MATERIALIZED ",
Some(CteAsMaterialized::NotMaterialized),
),
] {
let sql = format!("WITH c AS {directive}(SELECT 1) SELECT * FROM c");
TestedDialects::new(vec![Box::new(DuckDbDialect {})]).verified_query(&sql);
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);

assert_eq!(query.with.unwrap().cte_tables[0].materialized, expected);
if expected.is_some() {
all_dialects_where(|d| !d.supports_cte_materialization())
.parse_sql_statements(&sql)
.unwrap_err();
}
}
}
Loading