Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support the ARRAY type of Snowflake #699

Merged
merged 7 commits into from Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/ast/data_type.rs
Expand Up @@ -11,7 +11,7 @@
// limitations under the License.

#[cfg(not(feature = "std"))]
use alloc::{boxed::Box, string::String, vec::Vec};
use alloc::{boxed::Box, string::String, vec, vec::Vec};
use core::fmt;

#[cfg(feature = "serde")]
Expand Down Expand Up @@ -232,7 +232,13 @@ impl fmt::Display for DataType {
DataType::Text => write!(f, "TEXT"),
DataType::String => write!(f, "STRING"),
DataType::Bytea => write!(f, "BYTEA"),
DataType::Array(ty) => write!(f, "{}[]", ty),
DataType::Array(ty) => {
if ty == &Box::new(DataType::Custom(ObjectName(vec!["VARAINT".into()]), vec![])) {
write!(f, "ARRAY")
} else {
write!(f, "{}[]", ty)
}
}
DataType::Custom(ty, modifiers) => {
if modifiers.is_empty() {
write!(f, "{}", ty)
Expand Down
21 changes: 14 additions & 7 deletions src/parser.rs
Expand Up @@ -3672,13 +3672,20 @@ impl<'a> Parser<'a> {
Keyword::ENUM => Ok(DataType::Enum(self.parse_string_values()?)),
Keyword::SET => Ok(DataType::Set(self.parse_string_values()?)),
Keyword::ARRAY => {
// Hive array syntax. Note that nesting arrays - or other Hive syntax
// that ends with > will fail due to "C++" problem - >> is parsed as
// Token::ShiftRight
self.expect_token(&Token::Lt)?;
let inside_type = self.parse_data_type()?;
self.expect_token(&Token::Gt)?;
Ok(DataType::Array(Box::new(inside_type)))
if dialect_of!(self is SnowflakeDialect) {
Ok(DataType::Array(Box::new(DataType::Custom(
ObjectName(vec!["VARAINT".into()]),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer if this didn't add VARAINT into the parse tree when it wasn't in the original syntax. Can we model this like DataType::Array(None) perhaps?

vec![],
))))
} else {
// Hive array syntax. Note that nesting arrays - or other Hive syntax
// that ends with > will fail due to "C++" problem - >> is parsed as
// Token::ShiftRight
self.expect_token(&Token::Lt)?;
let inside_type = self.parse_data_type()?;
self.expect_token(&Token::Gt)?;
Ok(DataType::Array(Box::new(inside_type)))
}
}
_ => {
self.prev_token();
Expand Down
22 changes: 15 additions & 7 deletions tests/sqlparser_common.rs
Expand Up @@ -24,7 +24,7 @@ use sqlparser::ast::SelectItem::UnnamedExpr;
use sqlparser::ast::*;
use sqlparser::dialect::{
AnsiDialect, BigQueryDialect, ClickHouseDialect, GenericDialect, HiveDialect, MsSqlDialect,
PostgreSqlDialect, SQLiteDialect, SnowflakeDialect,
MySqlDialect, PostgreSqlDialect, SQLiteDialect, SnowflakeDialect,
};
use sqlparser::keywords::ALL_KEYWORDS;
use sqlparser::parser::{Parser, ParserError};
Expand Down Expand Up @@ -2109,12 +2109,20 @@ fn parse_create_table_hive_array() {
_ => unreachable!(),
}

let res =
parse_sql_statements("CREATE TABLE IF NOT EXISTS something (name int, val array<int)");
assert!(res
.unwrap_err()
.to_string()
.contains("Expected >, found: )"));
// SnowflakeDialect using array diffrent
let dialects = TestedDialects {
dialects: vec![
Box::new(PostgreSqlDialect {}),
Box::new(HiveDialect {}),
Box::new(MySqlDialect {}),
],
};
let sql = "CREATE TABLE IF NOT EXISTS something (name int, val array<int)";

assert_eq!(
dialects.parse_sql_statements(sql).unwrap_err(),
ParserError::ParserError("Expected >, found: )".to_string())
);
}

#[test]
Expand Down
16 changes: 16 additions & 0 deletions tests/sqlparser_snowflake.rs
Expand Up @@ -143,6 +143,22 @@ fn test_single_table_in_parenthesis_with_alias() {
);
}

#[test]
fn parse_array() {
let sql = "SELECT CAST(a AS ARRAY) FROM customer";
let select = snowflake().verified_only_select(sql);
assert_eq!(
&Expr::Cast {
expr: Box::new(Expr::Identifier(Ident::new("a"))),
data_type: DataType::Array(Box::new(DataType::Custom(
ObjectName(vec!["VARAINT".into()]),
vec![]
))),
},
expr_from_projection(only(&select.projection))
);
}

#[test]
fn parse_json_using_colon() {
let sql = "SELECT a:b FROM t";
Expand Down