Support parsing constraints in CREATE TABLE

<table element> ::= ... | <table constraint definition> | ...
https://jakewheat.github.io/sql-overview/sql-2011-foundation-grammar.html#table-element-list
This commit is contained in:
Nickolay Ponomarev 2019-05-11 23:51:30 +03:00
parent c69a1881c7
commit aab0c36443
4 changed files with 123 additions and 65 deletions

View file

@ -660,12 +660,14 @@ fn parse_create_table() {
SQLStatement::SQLCreateTable {
name,
columns,
constraints,
external: false,
file_format: None,
location: None,
} => {
assert_eq!("uk_cities", name.to_string());
assert_eq!(3, columns.len());
assert!(constraints.is_empty());
let c_name = &columns[0];
assert_eq!("name", c_name.name);
@ -705,12 +707,14 @@ fn parse_create_external_table() {
SQLStatement::SQLCreateTable {
name,
columns,
constraints,
external,
file_format,
location,
} => {
assert_eq!("uk_cities", name.to_string());
assert_eq!(3, columns.len());
assert!(constraints.is_empty());
let c_name = &columns[0];
assert_eq!("name", c_name.name);
@ -761,9 +765,29 @@ fn parse_alter_table_constraints() {
}
_ => unreachable!(),
}
verified_stmt(&format!("CREATE TABLE foo (id int, {})", constraint_text));
}
}
#[test]
fn parse_bad_constraint() {
let res = parse_sql_statements("ALTER TABLE tab ADD");
assert_eq!(
ParserError::ParserError(
"Expected a constraint in ALTER TABLE .. ADD, found: EOF".to_string()
),
res.unwrap_err()
);
let res = parse_sql_statements("CREATE TABLE tab (foo int,");
assert_eq!(
ParserError::ParserError(
"Expected column name or constraint definition, found: EOF".to_string()
),
res.unwrap_err()
);
}
#[test]
fn parse_scalar_function_in_projection() {
let sql = "SELECT sqrt(id) FROM foo";

View file

@ -23,12 +23,14 @@ fn parse_create_table_with_defaults() {
SQLStatement::SQLCreateTable {
name,
columns,
constraints,
external: false,
file_format: None,
location: None,
} => {
assert_eq!("public.customer", name.to_string());
assert_eq!(10, columns.len());
assert!(constraints.is_empty());
let c_name = &columns[0];
assert_eq!("customer_id", c_name.name);
@ -69,11 +71,13 @@ fn parse_create_table_from_pg_dump() {
SQLStatement::SQLCreateTable {
name,
columns,
constraints,
external: false,
file_format: None,
location: None,
} => {
assert_eq!("public.customer", name.to_string());
assert!(constraints.is_empty());
let c_customer_id = &columns[0];
assert_eq!("customer_id", c_customer_id.name);
@ -130,11 +134,13 @@ fn parse_create_table_with_inherit() {
SQLStatement::SQLCreateTable {
name,
columns,
constraints,
external: false,
file_format: None,
location: None,
} => {
assert_eq!("bazaar.settings", name.to_string());
assert!(constraints.is_empty());
let c_name = &columns[0];
assert_eq!("settings_id", c_name.name);