Skip to content

Commit

Permalink
fix: have wildcard replace work in duckdb and snowflake syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
HiranmayaGundu committed Apr 17, 2024
1 parent 2f03fad commit f90a446
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9059,7 +9059,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 | DuckDbDialect | SnowflakeDialect)
{
self.parse_optional_select_item_replace()?
} else {
Expand Down
28 changes: 28 additions & 0 deletions tests/sqlparser_duckdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,3 +508,31 @@ fn test_duckdb_named_argument_function_with_assignment_operator() {
expr_from_projection(only(&select.projection))
);
}

#[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,
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]);
}
22 changes: 22 additions & 0 deletions tests/sqlparser_snowflake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1554,3 +1554,25 @@ fn parse_comma_outer_join() {
fn test_sf_trailing_commas() {
snowflake().verified_only_select_with_canonical("SELECT 1, 2, FROM t", "SELECT 1, 2 FROM t");
}

#[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]);
}

0 comments on commit f90a446

Please sign in to comment.