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

Đang mở
#2,622 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

  • #2708 của @princejain756 — đã đóng, không merge

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
72/100
Loại issue
Tính năng
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
rust

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Rust
Star
3.5k
Fork
805
Merge trung bình
2 ngày 3 giờ
Pull request đã merge (30 ngày)
116

Chuẩn bị môi trường

  • Không có Dockerfile hay tệp Docker Compose
  • Có mẫu pull request
  • Không có hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của apache/datafusion-sqlparser-rs

Tất cả issue của apache/datafusion-sqlparser-rs

Issue tương tự

Thêm issue về Rust

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.