Parse `WITH ... AS [NOT] MATERIALIZED` in `GenericDialect`, `DuckDbDialect` and `SQLiteDialect`

Offen
#2,622 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

  • #2708 von @princejain756 — ohne Merge geschlossen

Bewertung

Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Anfängerfreundlichkeit
72/100
Issue-Typ
Feature
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
rust

Rechercherichtung

Start by reading Parser::parse_cte and the Dialect implementations for PostgreSqlDialect, GenericDialect, DuckDbDialect, and SQLiteDialect. Check the parser tests for CTE syntax and run the relevant test suite. Done means WITH ... AS MATERIALIZED and AS NOT MATERIALIZED parse in all four dialects while existing dialect behavior remains covered.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

DuckDB PostgreSQL SQLite

WITH x AS [NOT] MATERIALIZED (...) is parsed only by PostgreSqlDialect. With GenericDialect (and all other dialects) the parser returns an error.

use sqlparser::dialect::{GenericDialect, PostgreSqlDialect};
use sqlparser::parser::Parser;

let sql = "WITH c AS MATERIALIZED (SELECT 1) SELECT * FROM c";
assert!(Parser::parse_sql(&PostgreSqlDialect {}, sql).is_ok());
// sql parser error: Expected: (, found: MATERIALIZED
assert!(Parser::parse_sql(&GenericDialect {}, sql).is_err());

The cause is the dialect check in Parser::parse_cte:

let mut is_materialized = None;
if dialect_of!(self is PostgreSqlDialect) {
    if self.parse_keyword(Keyword::MATERIALIZED) {
        is_materialized = Some(CteAsMaterialized::Materialized);
    } else if self.parse_keywords(&[Keyword::NOT, Keyword::MATERIALIZED]) {
        is_materialized = Some(CteAsMaterialized::NotMaterialized);
    }
}
Why

DataFusion is adding support for the directive in https://github.com/apache/datafusion/pull/25580 (issue https://github.com/apache/datafusion/issues/17737). The default dialect of DataFusion is GenericDialect, so users must change the dialect to PostgreSQL to use it.

DuckDB and SQLite accept the same syntax in the same position, so DuckDbDialect and SQLiteDialect should also parse it.

Proposal

Replace the dialect_of! check with a dialect method, for example Dialect::supports_cte_materialized(), that returns true for PostgreSqlDialect, GenericDialect, DuckDbDialect and SQLiteDialect.

MATERIALIZED directly after AS and before ( is not ambiguous with other syntax, so the change is small.

Vorherrschende Sprache
Rust
Sterne
3.5k
Forks
805
Ø Merge
2 T. 3 Std.
Gemergte PRs (30 T.)
116

Entwicklungsumgebung

  • Kein Dockerfile und keine Docker-Compose-Datei
  • Hat eine Pull-Request-Vorlage
  • Kein Beitragsleitfaden

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus apache/datafusion-sqlparser-rs

Alle Issues in apache/datafusion-sqlparser-rs

Ähnliche Issues

Weitere Issues zu Rust

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.