Skip to content

Commit 9c4e6fb

Browse files
zdenallustefaniak
authored andcommitted
Support Snowflake TRIM.
1 parent ca9bb76 commit 9c4e6fb

File tree

5 files changed

+103
-0
lines changed

5 files changed

+103
-0
lines changed

.github/workflows/test.yml

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Test Suite
2+
on:
3+
push:
4+
branches:
5+
- master
6+
pull_request:
7+
types: [opened, synchronize]
8+
9+
jobs:
10+
check:
11+
name: Check
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v2
15+
- uses: actions-rs/toolchain@v1
16+
with:
17+
profile: minimal
18+
toolchain: stable
19+
override: true
20+
- uses: actions-rs/cargo@v1
21+
with:
22+
command: check
23+
24+
test:
25+
name: Test Suite
26+
runs-on: ubuntu-latest
27+
steps:
28+
- uses: actions/checkout@v2
29+
- uses: actions-rs/toolchain@v1
30+
with:
31+
profile: minimal
32+
toolchain: stable
33+
override: true
34+
- uses: actions-rs/cargo@v1
35+
with:
36+
command: nextest run

src/ast/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -517,12 +517,14 @@ pub enum Expr {
517517
/// ```sql
518518
/// TRIM([BOTH | LEADING | TRAILING] [<expr> FROM] <expr>)
519519
/// TRIM(<expr>)
520+
/// TRIM(<expr>, [, characters]) -- only Snowflake
520521
/// ```
521522
Trim {
522523
expr: Box<Expr>,
523524
// ([BOTH | LEADING | TRAILING]
524525
trim_where: Option<TrimWhereField>,
525526
trim_what: Option<Box<Expr>>,
527+
trim_characters: Option<Vec<Expr>>,
526528
},
527529
/// ```sql
528530
/// OVERLAY(<expr> PLACING <expr> FROM <expr>[ FOR <expr> ]
@@ -946,6 +948,7 @@ impl fmt::Display for Expr {
946948
expr,
947949
trim_where,
948950
trim_what,
951+
trim_characters,
949952
} => {
950953
write!(f, "TRIM(")?;
951954
if let Some(ident) = trim_where {
@@ -956,6 +959,9 @@ impl fmt::Display for Expr {
956959
} else {
957960
write!(f, "{expr}")?;
958961
}
962+
if let Some(characters) = trim_characters {
963+
write!(f, ", {}", display_comma_separated(characters))?;
964+
}
959965

960966
write!(f, ")")
961967
}

src/parser/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1337,6 +1337,7 @@ impl<'a> Parser<'a> {
13371337
/// ```sql
13381338
/// TRIM ([WHERE] ['text' FROM] 'text')
13391339
/// TRIM ('text')
1340+
/// TRIM(<expr>, [, characters]) -- only Snowflake
13401341
/// ```
13411342
pub fn parse_trim_expr(&mut self) -> Result<Expr, ParserError> {
13421343
self.expect_token(&Token::LParen)?;
@@ -1358,13 +1359,24 @@ impl<'a> Parser<'a> {
13581359
expr: Box::new(expr),
13591360
trim_where,
13601361
trim_what: Some(trim_what),
1362+
trim_characters: None,
1363+
})
1364+
} else if self.consume_token(&Token::Comma) && dialect_of!(self is SnowflakeDialect) {
1365+
let characters = self.parse_comma_separated(Parser::parse_expr)?;
1366+
self.expect_token(&Token::RParen)?;
1367+
Ok(Expr::Trim {
1368+
expr: Box::new(expr),
1369+
trim_where: None,
1370+
trim_what: None,
1371+
trim_characters: Some(characters),
13611372
})
13621373
} else {
13631374
self.expect_token(&Token::RParen)?;
13641375
Ok(Expr::Trim {
13651376
expr: Box::new(expr),
13661377
trim_where,
13671378
trim_what: None,
1379+
trim_characters: None,
13681380
})
13691381
}
13701382
}

tests/sqlparser_common.rs

+24
Original file line numberDiff line numberDiff line change
@@ -5245,6 +5245,30 @@ fn parse_trim() {
52455245
ParserError::ParserError("Expected ), found: 'xyz'".to_owned()),
52465246
parse_sql_statements("SELECT TRIM(FOO 'xyz' FROM 'xyzfooxyz')").unwrap_err()
52475247
);
5248+
5249+
//keep Snowflake TRIM syntax failing
5250+
let all_expected_snowflake = TestedDialects {
5251+
dialects: vec![
5252+
Box::new(GenericDialect {}),
5253+
Box::new(PostgreSqlDialect {}),
5254+
Box::new(MsSqlDialect {}),
5255+
Box::new(AnsiDialect {}),
5256+
//Box::new(SnowflakeDialect {}),
5257+
Box::new(HiveDialect {}),
5258+
Box::new(RedshiftSqlDialect {}),
5259+
Box::new(MySqlDialect {}),
5260+
Box::new(BigQueryDialect {}),
5261+
Box::new(SQLiteDialect {}),
5262+
Box::new(DuckDbDialect {}),
5263+
],
5264+
options: None,
5265+
};
5266+
assert_eq!(
5267+
ParserError::ParserError("Expected ), found: 'a'\nNear `SELECT TRIM('xyz',`".to_owned()),
5268+
all_expected_snowflake
5269+
.parse_sql_statements("SELECT TRIM('xyz', 'a')")
5270+
.unwrap_err()
5271+
);
52485272
}
52495273

52505274
#[test]

tests/sqlparser_snowflake.rs

+25
Original file line numberDiff line numberDiff line change
@@ -1047,3 +1047,28 @@ fn test_snowflake_stage_object_names() {
10471047
}
10481048
}
10491049
}
1050+
1051+
#[test]
1052+
fn test_snowflake_trim() {
1053+
let real_sql = r#"SELECT customer_id, TRIM(sub_items.value:item_price_id, '"', "a") AS item_price_id FROM models_staging.subscriptions"#;
1054+
assert_eq!(snowflake().verified_stmt(real_sql).to_string(), real_sql);
1055+
1056+
let sql_only_select = "SELECT TRIM('xyz', 'a')";
1057+
let select = snowflake().verified_only_select(sql_only_select);
1058+
assert_eq!(
1059+
&Expr::Trim {
1060+
expr: Box::new(Expr::Value(Value::SingleQuotedString("xyz".to_owned()))),
1061+
trim_where: None,
1062+
trim_what: None,
1063+
trim_characters: Some(vec![Expr::Value(Value::SingleQuotedString("a".to_owned()))]),
1064+
},
1065+
expr_from_projection(only(&select.projection))
1066+
);
1067+
1068+
// missing comma separation
1069+
let error_sql = "SELECT TRIM('xyz' 'a')";
1070+
assert_eq!(
1071+
ParserError::ParserError("Expected ), found: 'a'\nNear `SELECT TRIM('xyz'`".to_owned()),
1072+
snowflake().parse_sql_statements(error_sql).unwrap_err()
1073+
);
1074+
}

0 commit comments

Comments
 (0)