mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-09-13 09:26:19 +00:00
Add CREATE TABLE AS support (#206)
We parse it as a regular `CREATE TABLE` statement
followed by an `AS <query>`, which is how BigQuery works:
https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_table_statement
ANSI SQL and PostgreSQL only support a plain list of columns
after the table name in a CTAS
`CREATE TABLE t (a) AS SELECT a FROM foo`
We currently only allow specifying a full schema with data
types, or omitting it altogether.
https://www.postgresql.org/docs/12/sql-createtableas.html
https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#as-subquery-clause
Finally, when no schema is specified, we print empty parens after a
plain `CREATE TABLE t ();` as required by PostgreSQL, but skip them
in a CTAS: `CREATE TABLE t AS ...`. This affects serialization only,
the parser allows omitting the schema in a regular `CREATE TABLE` too
since the first release of the parser:
7d27abdfb4/src/sqlparser.rs (L325-L332)
Co-authored-by: Nickolay Ponomarev <asqueella@gmail.com>
This commit is contained in:
parent
26361fd854
commit
15d5f71646
6 changed files with 104 additions and 26 deletions
|
@ -1020,6 +1020,7 @@ impl Parser {
|
|||
external: true,
|
||||
file_format: Some(file_format),
|
||||
location: Some(location),
|
||||
query: None,
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -1108,8 +1109,17 @@ impl Parser {
|
|||
let table_name = self.parse_object_name()?;
|
||||
// parse optional column list (schema)
|
||||
let (columns, constraints) = self.parse_columns()?;
|
||||
|
||||
// PostgreSQL supports `WITH ( options )`, before `AS`
|
||||
let with_options = self.parse_with_options()?;
|
||||
|
||||
// Parse optional `AS ( query )`
|
||||
let query = if self.parse_keyword(Keyword::AS) {
|
||||
Some(Box::new(self.parse_query()?))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
Ok(Statement::CreateTable {
|
||||
name: table_name,
|
||||
columns,
|
||||
|
@ -1119,6 +1129,7 @@ impl Parser {
|
|||
external: false,
|
||||
file_format: None,
|
||||
location: None,
|
||||
query,
|
||||
})
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue