apache / datafusion-sqlparser-rs

Extensible SQL Lexer and Parser for Rust
Apache License 2.0
2.75k stars 527 forks source link

Support postgresql on commit for ctas #1274

Open fdzuJ opened 5 months ago

fdzuJ commented 5 months ago

Add support for postgresql [ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]. Currently panics:

use sqlparser::dialect::PostgreSqlDialect;
use sqlparser::parser::Parser;

fn main() {
    let sql = "create local temporary table test on commit preserve rows as select 1 as a;";

    let dialect = PostgreSqlDialect {};

    let ast = Parser::parse_sql(&dialect, sql).unwrap();

    for stmt in ast.iter() {
        println!("{}", stmt.to_string())
    }
}

ParserError("Expected end of statement, found: as at Line: 1, Column 59")

git-hulk commented 3 months ago

Hi @fdzuJ

I cannot find where mentioned the syntax that supports the alias after ON COMMIT clause, seems it only supports [TABLESPACE tablespace_name] according to Postgre documentation: https://www.postgresql.org/docs/current/sql-createtable.html.

It will be helpful if you can provide the reference and I'm happy to support this feature if it did miss in sqlparse-rs.

fdzuJ commented 3 months ago

Hi @git-hulk! Here is documentation for this syntax.

https://www.postgresql.org/docs/current/sql-createtableas.html

git-hulk commented 3 months ago

@fdzuJ Thank you! I will submit a PR to resolve this.

git-hulk commented 3 months ago

@fdzuJ Would you mind having a try at the latest version, it should be resolved now. cc @alamb