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

View file

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

View file

@ -434,7 +434,13 @@ fn parse_create_table() {
lng double)",
);
match ast {
SQLStatement::SQLCreateTable { name, columns } => {
SQLStatement::SQLCreateTable {
name,
columns,
external: _,
file_format: _,
location: _,
} => {
assert_eq!("uk_cities", name.to_string());
assert_eq!(3, columns.len());
@ -475,9 +481,10 @@ fn parse_create_external_table() {
STORED AS TEXTFILE LOCATION '/tmp/example.csv'",
);
match ast {
SQLStatement::SQLCreateExternalTable {
SQLStatement::SQLCreateTable {
name,
columns,
external,
file_format,
location,
} => {
@ -499,58 +506,9 @@ fn parse_create_external_table() {
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),
}
}
#[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!(external);
assert_eq!(FileFormat::TEXTFILE, file_format.unwrap());
assert_eq!("/tmp/example.csv", location.unwrap());
}
_ => assert!(false),
}

View file

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