Add unit tests.

This commit is contained in:
Zhiyuan Zheng 2019-04-09 13:28:01 +08:00
parent f0f6082eff
commit 26940920ac
3 changed files with 114 additions and 10 deletions

View file

@ -379,8 +379,13 @@ impl ToString for SQLStatement {
.collect::<Vec<String>>() .collect::<Vec<String>>()
.join(", ") .join(", ")
), ),
SQLStatement::SQLCreateExternalTable { name, columns, file_format, location } => format!( SQLStatement::SQLCreateExternalTable {
"CREATE TABLE {} ({}) STORED AS {} LOCATION {}", name,
columns,
file_format,
location,
} => format!(
"CREATE EXTERNAL TABLE {} ({}) STORED AS {} LOCATION '{}'",
name.to_string(), name.to_string(),
columns columns
.iter() .iter()
@ -477,8 +482,8 @@ impl ToString for FileFormat {
} }
} }
use std::str::FromStr;
use sqlparser::ParserError; use sqlparser::ParserError;
use std::str::FromStr;
impl FromStr for FileFormat { impl FromStr for FileFormat {
type Err = ParserError; type Err = ParserError;
@ -493,9 +498,9 @@ impl FromStr for FileFormat {
"RCFILE" => Ok(RCFILE), "RCFILE" => Ok(RCFILE),
"JSONFILE" => Ok(JSONFILE), "JSONFILE" => Ok(JSONFILE),
_ => Err(ParserError::ParserError(format!( _ => Err(ParserError::ParserError(format!(
"Unexpected token for file format: {}", "Unexpected file format: {}",
s s
))) ))),
} }
} }
} }

View file

@ -645,7 +645,7 @@ impl Parser {
name: table_name, name: table_name,
columns, columns,
file_format, file_format,
location location,
}) })
} }

View file

@ -457,6 +457,105 @@ fn parse_create_table() {
} }
} }
#[test]
fn parse_create_external_table() {
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),
}
}
#[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),
}
}
#[test] #[test]
fn parse_scalar_function_in_projection() { fn parse_scalar_function_in_projection() {
let sql = "SELECT sqrt(id) FROM foo"; let sql = "SELECT sqrt(id) FROM foo";