mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-10-09 13:40:22 +00:00
Support arbitrary WITH options for CREATE [TABLE|VIEW]
Both Postgres and MSSQL accept this syntax, though the particular options they accept differ.
This commit is contained in:
parent
1931c76eb8
commit
69f0082db6
4 changed files with 140 additions and 4 deletions
|
@ -765,6 +765,7 @@ fn parse_create_table() {
|
|||
name,
|
||||
columns,
|
||||
constraints,
|
||||
with_options,
|
||||
external: false,
|
||||
file_format: None,
|
||||
location: None,
|
||||
|
@ -787,6 +788,31 @@ fn parse_create_table() {
|
|||
assert_eq!("lng", c_lng.name);
|
||||
assert_eq!(SQLType::Double, c_lng.data_type);
|
||||
assert_eq!(true, c_lng.allow_null);
|
||||
|
||||
assert_eq!(with_options, vec![]);
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_create_table_with_options() {
|
||||
let sql = "CREATE TABLE t (c int) WITH (foo = 'bar', a = 123)";
|
||||
match verified_stmt(sql) {
|
||||
SQLStatement::SQLCreateTable { with_options, .. } => {
|
||||
assert_eq!(
|
||||
vec![
|
||||
SQLOption {
|
||||
name: "foo".into(),
|
||||
value: Value::SingleQuotedString("bar".into())
|
||||
},
|
||||
SQLOption {
|
||||
name: "a".into(),
|
||||
value: Value::Long(123)
|
||||
},
|
||||
],
|
||||
with_options
|
||||
);
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
|
@ -818,6 +844,7 @@ fn parse_create_external_table() {
|
|||
name,
|
||||
columns,
|
||||
constraints,
|
||||
with_options,
|
||||
external,
|
||||
file_format,
|
||||
location,
|
||||
|
@ -844,6 +871,8 @@ fn parse_create_external_table() {
|
|||
assert!(external);
|
||||
assert_eq!(FileFormat::TEXTFILE, file_format.unwrap());
|
||||
assert_eq!("/tmp/example.csv", location.unwrap());
|
||||
|
||||
assert_eq!(with_options, vec![]);
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
|
@ -1512,10 +1541,35 @@ fn parse_create_view() {
|
|||
name,
|
||||
query,
|
||||
materialized,
|
||||
with_options,
|
||||
} => {
|
||||
assert_eq!("myschema.myview", name.to_string());
|
||||
assert_eq!("SELECT foo FROM bar", query.to_string());
|
||||
assert!(!materialized);
|
||||
assert_eq!(with_options, vec![]);
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_create_view_with_options() {
|
||||
let sql = "CREATE VIEW v WITH (foo = 'bar', a = 123) AS SELECT 1";
|
||||
match verified_stmt(sql) {
|
||||
SQLStatement::SQLCreateView { with_options, .. } => {
|
||||
assert_eq!(
|
||||
vec![
|
||||
SQLOption {
|
||||
name: "foo".into(),
|
||||
value: Value::SingleQuotedString("bar".into())
|
||||
},
|
||||
SQLOption {
|
||||
name: "a".into(),
|
||||
value: Value::Long(123)
|
||||
},
|
||||
],
|
||||
with_options
|
||||
);
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
|
@ -1529,10 +1583,12 @@ fn parse_create_materialized_view() {
|
|||
name,
|
||||
query,
|
||||
materialized,
|
||||
with_options,
|
||||
} => {
|
||||
assert_eq!("myschema.myview", name.to_string());
|
||||
assert_eq!("SELECT foo FROM bar", query.to_string());
|
||||
assert!(materialized);
|
||||
assert_eq!(with_options, vec![]);
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
|
|
|
@ -18,12 +18,14 @@ fn parse_create_table_with_defaults() {
|
|||
activebool boolean DEFAULT true NOT NULL,
|
||||
create_date date DEFAULT now()::text NOT NULL,
|
||||
last_update timestamp without time zone DEFAULT now() NOT NULL,
|
||||
active integer NOT NULL)";
|
||||
active integer NOT NULL
|
||||
) WITH (fillfactor = 20, user_catalog_table = true, autovacuum_vacuum_threshold = 100)";
|
||||
match pg_and_generic().one_statement_parses_to(sql, "") {
|
||||
SQLStatement::SQLCreateTable {
|
||||
name,
|
||||
columns,
|
||||
constraints,
|
||||
with_options,
|
||||
external: false,
|
||||
file_format: None,
|
||||
location: None,
|
||||
|
@ -46,6 +48,24 @@ fn parse_create_table_with_defaults() {
|
|||
assert_eq!("first_name", c_lng.name);
|
||||
assert_eq!(SQLType::Varchar(Some(45)), c_lng.data_type);
|
||||
assert_eq!(false, c_lng.allow_null);
|
||||
|
||||
assert_eq!(
|
||||
with_options,
|
||||
vec![
|
||||
SQLOption {
|
||||
name: "fillfactor".into(),
|
||||
value: Value::Long(20)
|
||||
},
|
||||
SQLOption {
|
||||
name: "user_catalog_table".into(),
|
||||
value: Value::Boolean(true)
|
||||
},
|
||||
SQLOption {
|
||||
name: "autovacuum_vacuum_threshold".into(),
|
||||
value: Value::Long(100)
|
||||
},
|
||||
]
|
||||
);
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
|
@ -72,6 +92,7 @@ fn parse_create_table_from_pg_dump() {
|
|||
name,
|
||||
columns,
|
||||
constraints,
|
||||
with_options,
|
||||
external: false,
|
||||
file_format: None,
|
||||
location: None,
|
||||
|
@ -116,6 +137,8 @@ fn parse_create_table_from_pg_dump() {
|
|||
])),
|
||||
c_release_year.data_type
|
||||
);
|
||||
|
||||
assert_eq!(with_options, vec![]);
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
|
@ -135,6 +158,7 @@ fn parse_create_table_with_inherit() {
|
|||
name,
|
||||
columns,
|
||||
constraints,
|
||||
with_options,
|
||||
external: false,
|
||||
file_format: None,
|
||||
location: None,
|
||||
|
@ -155,6 +179,8 @@ fn parse_create_table_with_inherit() {
|
|||
assert_eq!(true, c_name.allow_null);
|
||||
assert_eq!(false, c_name.is_primary);
|
||||
assert_eq!(true, c_name.is_unique);
|
||||
|
||||
assert_eq!(with_options, vec![]);
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue