Skip to content

Commit 8822d19

Browse files
cmlee-accelinstaozhi8833998
authored andcommitted
fix(snowflake): support try_cast
1 parent 72f3924 commit 8822d19

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

pegjs/snowflake.pegjs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4039,7 +4039,7 @@ cast_double_colon
40394039
}
40404040

40414041
cast_expr
4042-
= c:KW_CAST __ LPAREN __ e:expr __ KW_AS __ t:data_type __ RPAREN {
4042+
= c:(KW_CAST / KW_TRY_CAST) __ LPAREN __ e:expr __ KW_AS __ t:data_type __ RPAREN {
40434043
// => IGNORE
40444044
return {
40454045
type: 'cast',
@@ -4049,7 +4049,7 @@ cast_expr
40494049
target: [t],
40504050
};
40514051
}
4052-
/ c:KW_CAST __ LPAREN __ e:expr __ KW_AS __ KW_DECIMAL __ LPAREN __ precision:int __ RPAREN __ RPAREN {
4052+
/ c:(KW_CAST / KW_TRY_CAST) __ LPAREN __ e:expr __ KW_AS __ KW_DECIMAL __ LPAREN __ precision:int __ RPAREN __ RPAREN {
40534053
// => IGNORE
40544054
return {
40554055
type: 'cast',
@@ -4061,7 +4061,7 @@ cast_expr
40614061
}]
40624062
};
40634063
}
4064-
/ c:KW_CAST __ LPAREN __ e:expr __ KW_AS __ KW_DECIMAL __ LPAREN __ precision:int __ COMMA __ scale:int __ RPAREN __ RPAREN {
4064+
/ c:(KW_CAST / KW_TRY_CAST) __ LPAREN __ e:expr __ KW_AS __ KW_DECIMAL __ LPAREN __ precision:int __ COMMA __ scale:int __ RPAREN __ RPAREN {
40654065
// => IGNORE
40664066
return {
40674067
type: 'cast',
@@ -4073,7 +4073,7 @@ cast_expr
40734073
}]
40744074
};
40754075
}
4076-
/ c:KW_CAST __ LPAREN __ e:expr __ KW_AS __ s:signedness __ t:KW_INTEGER? __ RPAREN { /* MySQL cast to un-/signed integer */
4076+
/ c:(KW_CAST / KW_TRY_CAST) __ LPAREN __ e:expr __ KW_AS __ s:signedness __ t:KW_INTEGER? __ RPAREN { /* MySQL cast to un-/signed integer */
40774077
// => IGNORE
40784078
return {
40794079
type: 'cast',
@@ -4403,6 +4403,7 @@ KW_ELSE = "ELSE"i !ident_start
44034403
KW_END = "END"i !ident_start
44044404

44054405
KW_CAST = "CAST"i !ident_start { return 'CAST' }
4406+
KW_TRY_CAST = "TRY_CAST"i !ident_start { return 'TRY_CAST' }
44064407

44074408
KW_BINARY = "BINARY"i !ident_start { return 'BINARY'; }
44084409
KW_VARBINARY = "VARBINARY"i !ident_start { return 'VARBINARY'; }

test/snowflake.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ describe('snowflake', () => {
119119
`SELECT "listing_id", "listing_name", "room_type", "host_id", REPLACE("price_str", '$')::NUMBER(10, 2) AS "price", "created_at", "updated_at" FROM "src_listings"`
120120
]
121121
},
122+
{
123+
title: "try_cast",
124+
sql: [
125+
`SELECT SHA512(TRY_CAST(CONCAT(a, b, c) AS VARCHAR)) AS Hashed FROM v`,
126+
`SELECT SHA512(TRY_CAST(CONCAT("a", "b", "c") AS VARCHAR)) AS "Hashed" FROM "v"`,
127+
],
128+
},
122129
{
123130
title: 'regexp operator',
124131
sql: [

0 commit comments

Comments
 (0)