From 13b0efd3cd17f1ff88ab3f35cac679a0088de28b Mon Sep 17 00:00:00 2001 From: Yoav Cohen Date: Fri, 21 Feb 2025 12:04:59 +0100 Subject: [PATCH] Parse castign to array using double colon operator --- src/dialect/duckdb.rs | 2 +- src/dialect/generic.rs | 2 +- src/dialect/mod.rs | 8 +++++--- src/dialect/postgresql.rs | 2 +- src/dialect/redshift.rs | 4 ++++ src/parser/mod.rs | 4 ++-- tests/sqlparser_common.rs | 7 +++++++ 7 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/dialect/duckdb.rs b/src/dialect/duckdb.rs index a2e7fb6c0..089f18d53 100644 --- a/src/dialect/duckdb.rs +++ b/src/dialect/duckdb.rs @@ -82,7 +82,7 @@ impl Dialect for DuckDbDialect { } // See DuckDB - fn supports_array_typedef_size(&self) -> bool { + fn supports_array_typedef_with_brackets(&self) -> bool { true } diff --git a/src/dialect/generic.rs b/src/dialect/generic.rs index e04a288d6..041d44bb2 100644 --- a/src/dialect/generic.rs +++ b/src/dialect/generic.rs @@ -148,7 +148,7 @@ impl Dialect for GenericDialect { true } - fn supports_array_typedef_size(&self) -> bool { + fn supports_array_typedef_with_brackets(&self) -> bool { true } diff --git a/src/dialect/mod.rs b/src/dialect/mod.rs index 86e23c86f..a1056b46a 100644 --- a/src/dialect/mod.rs +++ b/src/dialect/mod.rs @@ -931,9 +931,11 @@ pub trait Dialect: Debug + Any { false } - /// Returns true if the dialect supports size definition for array types. - /// For example: ```CREATE TABLE my_table (my_array INT[3])```. - fn supports_array_typedef_size(&self) -> bool { + /// Returns true if the dialect supports array type definition with brackets with + /// an optional size. For example: + /// ```CREATE TABLE my_table (arr1 INT[], arr2 INT[3])``` + /// ```SELECT x::INT[]``` + fn supports_array_typedef_with_brackets(&self) -> bool { false } /// Returns true if the dialect supports geometric types. diff --git a/src/dialect/postgresql.rs b/src/dialect/postgresql.rs index a20cfac4c..57ed0b684 100644 --- a/src/dialect/postgresql.rs +++ b/src/dialect/postgresql.rs @@ -247,7 +247,7 @@ impl Dialect for PostgreSqlDialect { } /// See: - fn supports_array_typedef_size(&self) -> bool { + fn supports_array_typedef_with_brackets(&self) -> bool { true } diff --git a/src/dialect/redshift.rs b/src/dialect/redshift.rs index 3dda762c3..25b8f1644 100644 --- a/src/dialect/redshift.rs +++ b/src/dialect/redshift.rs @@ -117,4 +117,8 @@ impl Dialect for RedshiftSqlDialect { fn supports_geometric_types(&self) -> bool { true } + + fn supports_array_typedef_with_brackets(&self) -> bool { + true + } } diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 69268bc51..20332c194 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -9123,9 +9123,9 @@ impl<'a> Parser<'a> { _ => self.expected_at("a data type name", next_token_index), }?; - if self.dialect.supports_array_typedef_size() { - // Parse array data type size + if self.dialect.supports_array_typedef_with_brackets() { while self.consume_token(&Token::LBracket) { + // Parse optional array data type size let size = self.maybe_parse(|p| p.parse_literal_uint())?; self.expect_token(&Token::RBracket)?; data = DataType::Array(ArrayElemTypeDef::SquareBracket(Box::new(data), size)) diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs index 653142dc6..20821e505 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs @@ -14218,3 +14218,10 @@ fn test_geometric_binary_operators() { } )); } + +#[test] +fn parse_array_type_def_with_brackets() { + let dialects = all_dialects_where(|d| d.supports_array_typedef_with_brackets()); + dialects.verified_stmt("SELECT x::INT[]"); + dialects.verified_stmt("SELECT STRING_TO_ARRAY('1,2,3', ',')::INT[3]"); +}