From 26940920ac18ae9713f190bfe5b8ac56c8e89a10 Mon Sep 17 00:00:00 2001 From: Zhiyuan Zheng Date: Tue, 9 Apr 2019 13:28:01 +0800 Subject: [PATCH] Add unit tests. --- src/sqlast/mod.rs | 17 ++++--- src/sqlparser.rs | 8 +-- tests/sqlparser_generic.rs | 99 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+), 10 deletions(-) diff --git a/src/sqlast/mod.rs b/src/sqlast/mod.rs index 31605b21..bd2de545 100644 --- a/src/sqlast/mod.rs +++ b/src/sqlast/mod.rs @@ -379,8 +379,13 @@ impl ToString for SQLStatement { .collect::>() .join(", ") ), - SQLStatement::SQLCreateExternalTable { name, columns, file_format, location } => format!( - "CREATE TABLE {} ({}) STORED AS {} LOCATION {}", + SQLStatement::SQLCreateExternalTable { + name, + columns, + file_format, + location, + } => format!( + "CREATE EXTERNAL TABLE {} ({}) STORED AS {} LOCATION '{}'", name.to_string(), columns .iter() @@ -477,8 +482,8 @@ impl ToString for FileFormat { } } -use std::str::FromStr; use sqlparser::ParserError; +use std::str::FromStr; impl FromStr for FileFormat { type Err = ParserError; @@ -493,9 +498,9 @@ impl FromStr for FileFormat { "RCFILE" => Ok(RCFILE), "JSONFILE" => Ok(JSONFILE), _ => Err(ParserError::ParserError(format!( - "Unexpected token for file format: {}", + "Unexpected file format: {}", s - ))) + ))), } } -} \ No newline at end of file +} diff --git a/src/sqlparser.rs b/src/sqlparser.rs index 89b7698c..726d9abe 100644 --- a/src/sqlparser.rs +++ b/src/sqlparser.rs @@ -645,7 +645,7 @@ impl Parser { name: table_name, columns, file_format, - location + location, }) } @@ -721,9 +721,9 @@ impl Parser { } other => { return parser_err!(format!( - "Expected ',' or ')' after column definition but found {:?}", - other - )); + "Expected ',' or ')' after column definition but found {:?}", + other + )); } } } diff --git a/tests/sqlparser_generic.rs b/tests/sqlparser_generic.rs index b3e418a0..a46b3ee0 100644 --- a/tests/sqlparser_generic.rs +++ b/tests/sqlparser_generic.rs @@ -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] fn parse_scalar_function_in_projection() { let sql = "SELECT sqrt(id) FROM foo";