Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/ast/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,9 @@ impl fmt::Display for DataType {
ArrayElemTypeDef::SquareBracket(t, Some(size)) => write!(f, "{t}[{size}]"),
ArrayElemTypeDef::AngleBracket(t) => write!(f, "ARRAY<{t}>"),
ArrayElemTypeDef::Parenthesis(t) => write!(f, "Array({t})"),
ArrayElemTypeDef::ParenthesisNotNull(t) => {
write!(f, "ARRAY({t} NOT NULL)")
}
ArrayElemTypeDef::Qualified(t, None) => write!(f, "{t} ARRAY"),
ArrayElemTypeDef::Qualified(t, Some(size)) => write!(f, "{t} ARRAY[{size}]"),
},
Expand Down Expand Up @@ -1165,6 +1168,8 @@ pub enum ArrayElemTypeDef {
SquareBracket(Box<DataType>, Option<u64>),
/// Parenthesis style, e.g. `Array(Int64)`.
Parenthesis(Box<DataType>),
/// Parenthesis style with a non-null element constraint, e.g. `ARRAY(INT NOT NULL)`.
ParenthesisNotNull(Box<DataType>),
/// Qualified by a data type and optional size, e.g. `INT ARRAY` or `INT ARRAY[4]`.
Qualified(Box<DataType>, Option<u64>),
}
Expand Down
4 changes: 4 additions & 0 deletions src/dialect/clickhouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ impl Dialect for ClickHouseDialect {
true
}

fn supports_array_typedef_with_parentheses(&self) -> bool {
true
}

// ClickHouse uses this for some FORMAT expressions in `INSERT` context, e.g. when inserting
// with FORMAT JSONEachRow a raw JSON key-value expression is valid and expected.
//
Expand Down
16 changes: 16 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,22 @@ pub trait Dialect: Debug + Any {
false
}

/// Returns true if this dialect supports the `ARRAY(element_type)` syntax.
///
/// Example:
/// ```sql
/// CREATE TABLE t (a ARRAY(VARCHAR));
/// ```
fn supports_array_typedef_with_parentheses(&self) -> bool {
false
}

/// Returns true if this dialect supports `NOT NULL` on an element type in
/// an `ARRAY(element_type)` definition.
fn supports_array_element_not_null(&self) -> bool {
false
}

/// Returns true if this dialect supports extra parentheses around
/// lone table names or derived tables in the `FROM` clause.
///
Expand Down
9 changes: 9 additions & 0 deletions src/dialect/snowflake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,15 @@ impl Dialect for SnowflakeDialect {
true
}

/// See [doc](https://docs.snowflake.com/en/sql-reference/data-types-structured#label-structured-types-array)
fn supports_array_typedef_with_parentheses(&self) -> bool {
true
}

fn supports_array_element_not_null(&self) -> bool {
true
}

/// See [doc](https://docs.snowflake.com/en/sql-reference/constructs/from)
fn supports_parens_around_table_factor(&self) -> bool {
true
Expand Down
28 changes: 23 additions & 5 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13120,12 +13120,30 @@ impl<'a> Parser<'a> {
Keyword::ENUM16 => Ok(DataType::Enum(self.parse_enum_values()?, Some(16))),
Keyword::SET => Ok(DataType::Set(self.parse_string_values()?)),
Keyword::ARRAY => {
if self.dialect.supports_array_typedef_without_element_type() {
if self.dialect.supports_array_typedef_with_parentheses() {
if self.peek_token_ref().token == Token::LParen {
self.expect_token(&Token::LParen)?;
let internal_type = self.parse_data_type()?;
let not_null = self.dialect.supports_array_element_not_null()
&& self.parse_keywords(&[Keyword::NOT, Keyword::NULL]);
self.expect_token(&Token::RParen)?;

if not_null {
Ok(DataType::Array(ArrayElemTypeDef::ParenthesisNotNull(
Box::new(internal_type),
)))
} else {
Ok(DataType::Array(ArrayElemTypeDef::Parenthesis(Box::new(
internal_type,
))))
}
} else if self.dialect.supports_array_typedef_without_element_type() {
Ok(DataType::Array(ArrayElemTypeDef::None))
} else {
self.expected("(", self.peek_token())
}
} else if self.dialect.supports_array_typedef_without_element_type() {
Ok(DataType::Array(ArrayElemTypeDef::None))
} else if dialect_of!(self is ClickHouseDialect) {
Ok(self.parse_sub_type(|internal_type| {
DataType::Array(ArrayElemTypeDef::Parenthesis(internal_type))
})?)
} else {
self.expect_token(&Token::Lt)?;
let (inside_type, _trailing_bracket) = self.parse_data_type_helper()?;
Expand Down
10 changes: 10 additions & 0 deletions tests/sqlparser_clickhouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,16 @@ fn parse_create_table_with_nested_data_types() {
}
}

#[test]
fn reject_angle_bracket_array_type() {
assert_eq!(
clickhouse()
.parse_sql_statements("CREATE TABLE t (a ARRAY<INT>)")
.unwrap_err(),
ParserError("Expected: (, found: <".to_string())
);
}

#[test]
fn parse_create_table_with_primary_key() {
match clickhouse_and_generic().verified_stmt(concat!(
Expand Down
24 changes: 24 additions & 0 deletions tests/sqlparser_snowflake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4913,6 +4913,30 @@ fn test_select_dollar_column_from_stage() {
snowflake().verified_stmt("SELECT $1, $2 FROM @mystage1(file_format => 'myformat')");
}

#[test]
fn test_structured_array_type() {
snowflake().one_statement_parses_to(
"CREATE TABLE t (a ARRAY(VARCHAR))",
"CREATE TABLE t (a Array(VARCHAR))",
);
snowflake().one_statement_parses_to(
"SELECT CAST(a AS ARRAY(NUMBER(10, 2))) FROM t",
"SELECT CAST(a AS Array(NUMBER(10, 2))) FROM t",
);
snowflake().verified_stmt("CREATE TABLE t (a ARRAY(VARCHAR NOT NULL))");
let select =
snowflake().verified_only_select("SELECT CAST(a AS ARRAY(VARCHAR NOT NULL)) FROM t");
let Expr::Cast { data_type, .. } = expr_from_projection(only(&select.projection)) else {
unreachable!();
};
assert_eq!(
data_type,
&DataType::Array(ArrayElemTypeDef::ParenthesisNotNull(Box::new(
DataType::Varchar(None)
)))
);
}

#[test]
fn test_snowflake_stage_name_with_escaped_quotes() {
snowflake().verified_stmt("REMOVE @````");
Expand Down
Loading