merge CreateExternalTable & CreateTable.

This commit is contained in:
Zhiyuan Zheng 2019-04-14 01:05:26 +08:00
parent 35556593f5
commit d8f824c400
4 changed files with 63 additions and 82 deletions

View file

@ -249,15 +249,9 @@ pub enum SQLStatement {
name: SQLObjectName, name: SQLObjectName,
/// Optional schema /// Optional schema
columns: Vec<SQLColumnDef>, columns: Vec<SQLColumnDef>,
}, external: bool,
/// CREATE EXTERNAL TABLE file_format: Option<FileFormat>,
SQLCreateExternalTable { location: Option<String>,
/// Table name
name: SQLObjectName,
/// Optional schema
columns: Vec<SQLColumnDef>,
file_format: FileFormat,
location: String,
}, },
/// ALTER TABLE /// ALTER TABLE
SQLAlterTable { SQLAlterTable {
@ -370,21 +364,13 @@ impl ToString for SQLStatement {
query.to_string() query.to_string()
) )
} }
SQLStatement::SQLCreateTable { name, columns } => format!( SQLStatement::SQLCreateTable {
"CREATE TABLE {} ({})",
name.to_string(),
columns
.iter()
.map(|c| c.to_string())
.collect::<Vec<String>>()
.join(", ")
),
SQLStatement::SQLCreateExternalTable {
name, name,
columns, columns,
external,
file_format, file_format,
location, location,
} => format!( } if *external => format!(
"CREATE EXTERNAL TABLE {} ({}) STORED AS {} LOCATION '{}'", "CREATE EXTERNAL TABLE {} ({}) STORED AS {} LOCATION '{}'",
name.to_string(), name.to_string(),
columns columns
@ -392,8 +378,23 @@ impl ToString for SQLStatement {
.map(|c| c.to_string()) .map(|c| c.to_string())
.collect::<Vec<String>>() .collect::<Vec<String>>()
.join(", "), .join(", "),
file_format.to_string(), file_format.as_ref().map(|f| f.to_string()).unwrap(),
location location.as_ref().unwrap()
),
SQLStatement::SQLCreateTable {
name,
columns,
external: _,
file_format: _,
location: _,
} => format!(
"CREATE TABLE {} ({})",
name.to_string(),
columns
.iter()
.map(|c| c.to_string())
.collect::<Vec<String>>()
.join(", ")
), ),
SQLStatement::SQLAlterTable { name, operation } => { SQLStatement::SQLAlterTable { name, operation } => {
format!("ALTER TABLE {} {}", name.to_string(), operation.to_string()) format!("ALTER TABLE {} {}", name.to_string(), operation.to_string())

View file

@ -641,11 +641,12 @@ impl Parser {
self.expect_keyword("LOCATION")?; self.expect_keyword("LOCATION")?;
let location = self.parse_literal_string()?; let location = self.parse_literal_string()?;
Ok(SQLStatement::SQLCreateExternalTable { Ok(SQLStatement::SQLCreateTable {
name: table_name, name: table_name,
columns, columns,
file_format, external: true,
location, file_format: Some(file_format),
location: Some(location),
}) })
} }
@ -676,6 +677,9 @@ impl Parser {
Ok(SQLStatement::SQLCreateTable { Ok(SQLStatement::SQLCreateTable {
name: table_name, name: table_name,
columns, columns,
external: false,
file_format: None,
location: None,
}) })
} }

View file

@ -434,7 +434,13 @@ fn parse_create_table() {
lng double)", lng double)",
); );
match ast { match ast {
SQLStatement::SQLCreateTable { name, columns } => { SQLStatement::SQLCreateTable {
name,
columns,
external: _,
file_format: _,
location: _,
} => {
assert_eq!("uk_cities", name.to_string()); assert_eq!("uk_cities", name.to_string());
assert_eq!(3, columns.len()); assert_eq!(3, columns.len());
@ -475,9 +481,10 @@ fn parse_create_external_table() {
STORED AS TEXTFILE LOCATION '/tmp/example.csv'", STORED AS TEXTFILE LOCATION '/tmp/example.csv'",
); );
match ast { match ast {
SQLStatement::SQLCreateExternalTable { SQLStatement::SQLCreateTable {
name, name,
columns, columns,
external,
file_format, file_format,
location, location,
} => { } => {
@ -499,58 +506,9 @@ fn parse_create_external_table() {
assert_eq!(SQLType::Double, c_lng.data_type); assert_eq!(SQLType::Double, c_lng.data_type);
assert_eq!(true, c_lng.allow_null); assert_eq!(true, c_lng.allow_null);
assert_eq!(FileFormat::TEXTFILE, file_format); assert!(external);
assert_eq!("/tmp/example.csv", location); assert_eq!(FileFormat::TEXTFILE, file_format.unwrap());
} assert_eq!("/tmp/example.csv", location.unwrap());
_ => assert!(false),
}
}
#[test]
fn parse_create_external_table_newline() {
let sql = String::from(
"CREATE EXTERNAL TABLE uk_cities (\
name VARCHAR(100) NOT NULL,\
lat DOUBLE NULL,\
lng DOUBLE NULL)\
STORED AS TEXTFILE
LOCATION '/tmp/example.csv",
);
let ast = one_statement_parses_to(
&sql,
"CREATE EXTERNAL TABLE uk_cities (\
name character varying(100) NOT NULL, \
lat double, \
lng double) \
STORED AS TEXTFILE LOCATION '/tmp/example.csv'",
);
match ast {
SQLStatement::SQLCreateExternalTable {
name,
columns,
file_format,
location,
} => {
assert_eq!("uk_cities", name.to_string());
assert_eq!(3, columns.len());
let c_name = &columns[0];
assert_eq!("name", c_name.name);
assert_eq!(SQLType::Varchar(Some(100)), c_name.data_type);
assert_eq!(false, c_name.allow_null);
let c_lat = &columns[1];
assert_eq!("lat", c_lat.name);
assert_eq!(SQLType::Double, c_lat.data_type);
assert_eq!(true, c_lat.allow_null);
let c_lng = &columns[2];
assert_eq!("lng", c_lng.name);
assert_eq!(SQLType::Double, c_lng.data_type);
assert_eq!(true, c_lng.allow_null);
assert_eq!(FileFormat::TEXTFILE, file_format);
assert_eq!("/tmp/example.csv", location);
} }
_ => assert!(false), _ => assert!(false),
} }

View file

@ -163,7 +163,13 @@ fn parse_create_table_with_defaults() {
active integer NOT NULL)", active integer NOT NULL)",
); );
match one_statement_parses_to(&sql, "") { match one_statement_parses_to(&sql, "") {
SQLStatement::SQLCreateTable { name, columns } => { SQLStatement::SQLCreateTable {
name,
columns,
external: _,
file_format: _,
location: _,
} => {
assert_eq!("public.customer", name.to_string()); assert_eq!("public.customer", name.to_string());
assert_eq!(10, columns.len()); assert_eq!(10, columns.len());
@ -204,7 +210,13 @@ fn parse_create_table_from_pg_dump() {
active integer active integer
)"); )");
match one_statement_parses_to(&sql, "") { match one_statement_parses_to(&sql, "") {
SQLStatement::SQLCreateTable { name, columns } => { SQLStatement::SQLCreateTable {
name,
columns,
external: _,
file_format: _,
location: _,
} => {
assert_eq!("public.customer", name.to_string()); assert_eq!("public.customer", name.to_string());
let c_customer_id = &columns[0]; let c_customer_id = &columns[0];
@ -261,7 +273,13 @@ fn parse_create_table_with_inherit() {
)", )",
); );
match verified_stmt(&sql) { match verified_stmt(&sql) {
SQLStatement::SQLCreateTable { name, columns } => { SQLStatement::SQLCreateTable {
name,
columns,
external: _,
file_format: _,
location: _,
} => {
assert_eq!("bazaar.settings", name.to_string()); assert_eq!("bazaar.settings", name.to_string());
let c_name = &columns[0]; let c_name = &columns[0];