Skip to content

Snowflake: support for object constants #1223

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

Merged
merged 1 commit into from
Apr 26, 2024
Merged
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
7 changes: 7 additions & 0 deletions src/dialect/duckdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,11 @@ impl Dialect for DuckDbDialect {
fn supports_named_fn_args_with_eq_operator(&self) -> bool {
true
}

// DuckDB uses this syntax for `STRUCT`s.
//
// https://duckdb.org/docs/sql/data_types/struct.html#creating-structs
fn supports_dictionary_syntax(&self) -> bool {
true
}
}
4 changes: 4 additions & 0 deletions src/dialect/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,8 @@ impl Dialect for GenericDialect {
fn supports_start_transaction_modifier(&self) -> bool {
true
}

fn supports_dictionary_syntax(&self) -> bool {
true
}
}
5 changes: 5 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ pub trait Dialect: Debug + Any {
fn supports_named_fn_args_with_eq_operator(&self) -> bool {
false
}
/// Returns true if the dialect supports defining structs or objects using a
/// syntax like `{'x': 1, 'y': 2, 'z': 3}`.
fn supports_dictionary_syntax(&self) -> bool {
false
}
/// Returns true if the dialect has a CONVERT function which accepts a type first
/// and an expression second, e.g. `CONVERT(varchar, 1)`
fn convert_type_before_value(&self) -> bool {
Expand Down
8 changes: 8 additions & 0 deletions src/dialect/snowflake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ impl Dialect for SnowflakeDialect {
true
}

// Snowflake uses this syntax for "object constants" (the values of which
// are not actually required to be constants).
//
// https://docs.snowflake.com/en/sql-reference/data-types-semistructured#label-object-constant
fn supports_dictionary_syntax(&self) -> bool {
true
}

fn parse_statement(&self, parser: &mut Parser) -> Option<Result<Statement, ParserError>> {
if parser.parse_keyword(Keyword::CREATE) {
// possibly CREATE STAGE
Expand Down
2 changes: 1 addition & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1192,7 +1192,7 @@ impl<'a> Parser<'a> {
self.prev_token();
Ok(Expr::Value(self.parse_value()?))
}
Token::LBrace if dialect_of!(self is DuckDbDialect | GenericDialect) => {
Token::LBrace if self.dialect.supports_dictionary_syntax() => {
self.prev_token();
self.parse_duckdb_struct_literal()
}
Expand Down
56 changes: 56 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9288,3 +9288,59 @@ fn insert_into_with_parentheses() {
};
dialects.verified_stmt("INSERT INTO t1 (id, name) (SELECT t2.id, t2.name FROM t2)");
}

#[test]
fn test_dictionary_syntax() {
fn check(sql: &str, expect: Expr) {
assert_eq!(
all_dialects_where(|d| d.supports_dictionary_syntax()).verified_expr(sql),
expect
);
}

check(
"{'Alberta': 'Edmonton', 'Manitoba': 'Winnipeg'}",
Expr::Dictionary(vec![
DictionaryField {
key: Ident::with_quote('\'', "Alberta"),
value: Box::new(Expr::Value(Value::SingleQuotedString(
"Edmonton".to_owned(),
))),
},
DictionaryField {
key: Ident::with_quote('\'', "Manitoba"),
value: Box::new(Expr::Value(Value::SingleQuotedString(
"Winnipeg".to_owned(),
))),
},
]),
);

check(
"{'start': CAST('2023-04-01' AS TIMESTAMP), 'end': CAST('2023-04-05' AS TIMESTAMP)}",
Expr::Dictionary(vec![
DictionaryField {
key: Ident::with_quote('\'', "start"),
value: Box::new(Expr::Cast {
kind: CastKind::Cast,
expr: Box::new(Expr::Value(Value::SingleQuotedString(
"2023-04-01".to_owned(),
))),
data_type: DataType::Timestamp(None, TimezoneInfo::None),
format: None,
}),
},
DictionaryField {
key: Ident::with_quote('\'', "end"),
value: Box::new(Expr::Cast {
kind: CastKind::Cast,
expr: Box::new(Expr::Value(Value::SingleQuotedString(
"2023-04-05".to_owned(),
))),
data_type: DataType::Timestamp(None, TimezoneInfo::None),
format: None,
}),
},
]),
)
}
Loading