mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-10-14 15:59:03 +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
|
@ -1044,6 +1044,7 @@ fn parse_create_table() {
|
|||
external: false,
|
||||
file_format: None,
|
||||
location: None,
|
||||
query: _query,
|
||||
} => {
|
||||
assert_eq!("uk_cities", name.to_string());
|
||||
assert_eq!(
|
||||
|
@ -1177,6 +1178,36 @@ fn parse_drop_schema() {
|
|||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_create_table_as() {
|
||||
let sql = "CREATE TABLE t AS SELECT * FROM a";
|
||||
|
||||
match verified_stmt(sql) {
|
||||
Statement::CreateTable { name, query, .. } => {
|
||||
assert_eq!(name.to_string(), "t".to_string());
|
||||
assert_eq!(query, Some(Box::new(verified_query("SELECT * FROM a"))));
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
|
||||
// BigQuery allows specifying table schema in CTAS
|
||||
// ANSI SQL and PostgreSQL let you only specify the list of columns
|
||||
// (without data types) in a CTAS, but we have yet to support that.
|
||||
let sql = "CREATE TABLE t (a INT, b INT) AS SELECT 1 AS b, 2 AS a";
|
||||
match verified_stmt(sql) {
|
||||
Statement::CreateTable { columns, query, .. } => {
|
||||
assert_eq!(columns.len(), 2);
|
||||
assert_eq!(columns[0].to_string(), "a INT".to_string());
|
||||
assert_eq!(columns[1].to_string(), "b INT".to_string());
|
||||
assert_eq!(
|
||||
query,
|
||||
Some(Box::new(verified_query("SELECT 1 AS b, 2 AS a")))
|
||||
);
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_create_table_with_on_delete_on_update_2in_any_order() -> Result<(), ParserError> {
|
||||
let sql = |options: &str| -> String {
|
||||
|
@ -1245,6 +1276,7 @@ fn parse_create_external_table() {
|
|||
external,
|
||||
file_format,
|
||||
location,
|
||||
query: _query,
|
||||
} => {
|
||||
assert_eq!("uk_cities", name.to_string());
|
||||
assert_eq!(
|
||||
|
@ -1307,12 +1339,6 @@ fn parse_create_external_table_lowercase() {
|
|||
assert_matches!(ast, Statement::CreateTable{..});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_create_table_empty() {
|
||||
// Zero-column tables are weird, but supported by at least PostgreSQL.
|
||||
let _ = verified_stmt("CREATE TABLE t ()");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_alter_table() {
|
||||
let add_column = "ALTER TABLE tab ADD COLUMN foo TEXT";
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue