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
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
10 changes: 10 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,16 @@ 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 extra parentheses around
/// lone table names or derived tables in the `FROM` clause.
///
Expand Down
5 changes: 5 additions & 0 deletions src/dialect/snowflake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ 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
}

/// See [doc](https://docs.snowflake.com/en/sql-reference/constructs/from)
fn supports_parens_around_table_factor(&self) -> bool {
true
Expand Down
8 changes: 5 additions & 3 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13101,12 +13101,14 @@ 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() {
Ok(DataType::Array(ArrayElemTypeDef::None))
} else if dialect_of!(self is ClickHouseDialect) {
if self.dialect.supports_array_typedef_with_parentheses()
&& self.peek_token_ref().token == Token::LParen
{
Ok(self.parse_sub_type(|internal_type| {
DataType::Array(ArrayElemTypeDef::Parenthesis(internal_type))
})?)
} else if self.dialect.supports_array_typedef_without_element_type() {
Ok(DataType::Array(ArrayElemTypeDef::None))
} else {
self.expect_token(&Token::Lt)?;
let (inside_type, _trailing_bracket) = self.parse_data_type_helper()?;
Expand Down
12 changes: 12 additions & 0 deletions tests/sqlparser_snowflake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4912,3 +4912,15 @@ fn test_select_dollar_column_from_stage() {
// With table function args, without alias
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",
);
}
Loading