From 27cd7899f2b071bfe57e608fa063ef0cd26b16ff Mon Sep 17 00:00:00 2001 From: Hiranmaya Gundu Date: Wed, 17 Apr 2024 10:09:52 -0700 Subject: [PATCH] fix: have wildcard replace work in duckdb and snowflake syntax --- src/parser/mod.rs | 2 +- tests/sqlparser_duckdb.rs | 29 +++++++++++++++++++++++++++++ tests/sqlparser_snowflake.rs | 22 ++++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 9f5078f01..8ddff71fe 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -8793,7 +8793,7 @@ impl<'a> Parser<'a> { None }; - let opt_replace = if dialect_of!(self is GenericDialect | BigQueryDialect | ClickHouseDialect) + let opt_replace = if dialect_of!(self is GenericDialect | BigQueryDialect | ClickHouseDialect | SnowflakeDialect | DuckDbDialect) { self.parse_optional_select_item_replace()? } else { diff --git a/tests/sqlparser_duckdb.rs b/tests/sqlparser_duckdb.rs index 45ae01bf0..469608488 100644 --- a/tests/sqlparser_duckdb.rs +++ b/tests/sqlparser_duckdb.rs @@ -246,3 +246,32 @@ fn test_duckdb_load_extension() { stmt ); } + +#[test] +fn test_select_wildcard_with_replace() { + let select = duckdb_and_generic() + .verified_only_select(r#"SELECT * REPLACE (lower(city) AS city) FROM addresses"#); + let expected = SelectItem::Wildcard(WildcardAdditionalOptions { + opt_replace: Some(ReplaceSelectItem { + items: vec![Box::new(ReplaceSelectElement { + expr: Expr::Function(Function { + name: ObjectName(vec![Ident::new("lower")]), + args: vec![FunctionArg::Unnamed(FunctionArgExpr::Expr( + Expr::Identifier(Ident::new("city")), + ))], + filter: None, + null_treatment: None, + within_group: None, + over: None, + distinct: false, + special: false, + order_by: vec![], + }), + column_name: Ident::new("city"), + as_keyword: true, + })], + }), + ..Default::default() + }); + assert_eq!(expected, select.projection[0]); +} diff --git a/tests/sqlparser_snowflake.rs b/tests/sqlparser_snowflake.rs index c7420c73f..95b0d4330 100644 --- a/tests/sqlparser_snowflake.rs +++ b/tests/sqlparser_snowflake.rs @@ -1593,3 +1593,25 @@ fn parse_connect_by() { )))] ); } + +#[test] +fn test_select_wildcard_with_replace() { + let select = snowflake_and_generic().verified_only_select( + r#"SELECT * REPLACE ('DEPT-' || department_id AS department_id) FROM tbl"#, + ); + let expected = SelectItem::Wildcard(WildcardAdditionalOptions { + opt_replace: Some(ReplaceSelectItem { + items: vec![Box::new(ReplaceSelectElement { + expr: Expr::BinaryOp { + left: Box::new(Expr::Value(Value::SingleQuotedString("DEPT-".to_owned()))), + op: BinaryOperator::StringConcat, + right: Box::new(Expr::Identifier(Ident::new("department_id"))), + }, + column_name: Ident::new("department_id"), + as_keyword: true, + })], + }), + ..Default::default() + }); + assert_eq!(expected, select.projection[0]); +}