Skip to content

Commit 5792cf2

Browse files
authored
[Snowflake]Refacto expr & id_ (#3884)
* Add test for keywword (cherry picked from commit 9d08003) * Refacto expr & id_ (WIP) * Add test for id_ * Fix wrong level of binary, ternary..function_call
1 parent a137695 commit 5792cf2

File tree

3 files changed

+63
-40
lines changed

3 files changed

+63
-40
lines changed

sql/snowflake/SnowflakeParser.g4

Lines changed: 58 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2572,6 +2572,7 @@ object_type_plural
25722572
| STAGES
25732573
| STREAMS
25742574
| TASKS
2575+
| ALERTS
25752576
;
25762577

25772578
// drop commands
@@ -3363,24 +3364,26 @@ id_fn
33633364
;
33643365

33653366
id_
3367+
//id_ is used for object name. Snowflake is very permissive
3368+
//so we could use nearly all keyword as object name (table, column etc..)
33663369
: ID
33673370
| ID2
33683371
| DOUBLE_QUOTE_ID
33693372
| DOUBLE_QUOTE_BLANK
33703373
| keyword
33713374
| non_reserved_words
3375+
| object_type_plural
33723376
| data_type
33733377
| builtin_function
3374-
| ALERT
3375-
| ALERTS
3376-
| CONDITION
33773378
| binary_builtin_function
3379+
| binary_or_ternary_builtin_function
3380+
| ternary_builtin_function
33783381
;
33793382

33803383
keyword
3381-
: INT
3382-
| BIGINT
3383-
| STAGE
3384+
//List here keyword (SnowSQL meaning) allowed as object name
3385+
// Name of builtin function should be included in specifique section (ie builtin_function)
3386+
: STAGE
33843387
| USER
33853388
| TYPE
33863389
| CLUSTER
@@ -3398,10 +3401,27 @@ keyword
33983401
| DIRECTION
33993402
| LENGTH
34003403
| LANGUAGE
3404+
| KEY
3405+
| ALERT
3406+
| CONDITION
3407+
| ROLE
3408+
| ROW_NUMBER
3409+
| VALUE
3410+
| FIRST_VALUE
3411+
| VALUES
3412+
| TARGET_LAG
3413+
| EMAIL
3414+
| MAX_CONCURRENCY_LEVEL
3415+
| WAREHOUSE_TYPE
3416+
| TAG
3417+
| WAREHOUSE
3418+
| MODE
3419+
| ACTION
34013420
// etc
34023421
;
34033422

34043423
non_reserved_words
3424+
//List here lexer token referenced by rules which is not a keyword (SnowSQL Meaning) and allowed has object name
34053425
: ORGADMIN
34063426
| ACCOUNTADMIN
34073427
| SECURITYADMIN
@@ -3414,23 +3434,14 @@ non_reserved_words
34143434
| SOURCE
34153435
| PROCEDURE_NAME
34163436
| STATE
3417-
| ROLE
34183437
| DEFINITION
34193438
| TIMEZONE
34203439
| LOCAL
3421-
| ROW_NUMBER
3422-
| VALUE
34233440
| NAME
3424-
| TAG
3425-
| WAREHOUSE
34263441
| VERSION
34273442
| OPTION
3428-
| NVL2
3429-
| FIRST_VALUE
34303443
| RESPECT
3431-
| NVL
34323444
| RESTRICT
3433-
| VALUES
34343445
| EVENT
34353446
| DOWNSTREAM
34363447
| DYNAMIC
@@ -3444,9 +3455,8 @@ non_reserved_words
34443455

34453456
builtin_function
34463457
// If there is a lexer entry for a function we also need to add the token here
3447-
// as it otherwise will not be picked up by the id_ rule
3448-
: IFF
3449-
| SUM
3458+
// as it otherwise will not be picked up by the id_ rule (See also derived rule below)
3459+
: SUM
34503460
| AVG
34513461
| MIN
34523462
| COUNT
@@ -3459,23 +3469,19 @@ builtin_function
34593469
| FLATTEN
34603470
| SPLIT_TO_TABLE
34613471
| CAST
3472+
| TRY_CAST
34623473
;
3463-
3464-
list_operator
3465-
// lexer entry which admit a list of comma separated expr
3466-
: CONCAT
3467-
| CONCAT_WS
3468-
| COALESCE
3469-
// To complete as needed
3470-
;
3474+
//TODO : Split builtin between NoParam func,special_builtin_func (like CAST), unary_builtin_function and unary_or_binary_builtin_function for better AST
34713475

34723476
binary_builtin_function
3477+
// lexer entry of function name which admit 2 parameters
3478+
// expr rule use this
34733479
: ifnull = (IFNULL | NVL)
34743480
| GET
34753481
| LEFT
34763482
| RIGHT
34773483
| DATE_PART
3478-
| to_date = ( TO_DATE | DATE)
3484+
| to_date = (TO_DATE | DATE)
34793485
| SPLIT
34803486
| NULLIF
34813487
| EQUAL_NULL
@@ -3484,6 +3490,8 @@ binary_builtin_function
34843490
;
34853491

34863492
binary_or_ternary_builtin_function
3493+
// lexer entry of function name which admit 2 or 3 parameters
3494+
// expr rule use this
34873495
: CHARINDEX
34883496
| REPLACE
34893497
| substring = ( SUBSTRING | SUBSTR)
@@ -3492,10 +3500,22 @@ binary_or_ternary_builtin_function
34923500
;
34933501

34943502
ternary_builtin_function
3503+
// lexer entry of function name which admit 3 parameters
3504+
// expr rule use this
34953505
: dateadd = (DATEADD | TIMEADD | TIMESTAMPADD)
3496-
| datefiff = ( DATEDIFF | TIMEDIFF | TIMESTAMPDIFF)
3506+
| datefiff = (DATEDIFF | TIMEDIFF | TIMESTAMPDIFF)
34973507
| SPLIT_PART
34983508
| NVL2
3509+
| IFF
3510+
;
3511+
3512+
list_function
3513+
// lexer entry of function name which admit a list of comma separated expr
3514+
// expr rule use this
3515+
: CONCAT
3516+
| CONCAT_WS
3517+
| COALESCE
3518+
// To complete as needed
34993519
;
35003520

35013521
pattern
@@ -3544,8 +3564,6 @@ expr_list_sorted
35443564

35453565
expr
35463566
: object_name DOT NEXTVAL
3547-
| primitive_expression
3548-
| function_call
35493567
| expr LSB expr RSB //array access
35503568
| expr COLON expr //json access
35513569
| expr DOT (VALUE | expr)
@@ -3562,21 +3580,20 @@ expr
35623580
| expr OR expr //bool operation
35633581
| arr_literal
35643582
// | expr time_zone
3565-
| expr COLON_COLON data_type //cast
35663583
| expr over_clause
35673584
| cast_expr
3568-
| json_literal
3569-
| binary_builtin_function LR_BRACKET expr COMMA expr RR_BRACKET
3570-
| binary_or_ternary_builtin_function LR_BRACKET expr COMMA expr (COMMA expr)* RR_BRACKET
3571-
| ternary_builtin_function LR_BRACKET expr COMMA expr COMMA expr RR_BRACKET
3572-
| subquery
3585+
| expr COLON_COLON data_type // Cast also
35733586
| try_cast_expr
3587+
| json_literal
35743588
| trim_expression
3589+
| function_call
3590+
| subquery
35753591
| expr IS null_not_null
35763592
| expr NOT? IN LR_BRACKET (subquery | expr_list) RR_BRACKET
35773593
| expr NOT? ( LIKE | ILIKE) expr (ESCAPE expr)?
35783594
| expr NOT? RLIKE expr
35793595
| expr NOT? (LIKE | ILIKE) ANY LR_BRACKET expr (COMMA expr)* RR_BRACKET (ESCAPE expr)?
3596+
| primitive_expression //Should be latest rule as it's nearly a catch all
35803597
;
35813598

35823599
iff_expr
@@ -3680,11 +3697,14 @@ over_clause
36803697
;
36813698

36823699
function_call
3683-
: ranking_windowed_function
3700+
: binary_builtin_function LR_BRACKET expr COMMA expr RR_BRACKET
3701+
| binary_or_ternary_builtin_function LR_BRACKET expr COMMA expr (COMMA expr)* RR_BRACKET
3702+
| ternary_builtin_function LR_BRACKET expr COMMA expr COMMA expr RR_BRACKET
3703+
| ranking_windowed_function
36843704
| aggregate_function
36853705
// | aggregate_windowed_function
36863706
| object_name '(' expr_list? ')'
3687-
| list_operator LR_BRACKET expr_list RR_BRACKET
3707+
| list_function LR_BRACKET expr_list RR_BRACKET
36883708
| to_date = ( TO_DATE | DATE) LR_BRACKET expr RR_BRACKET
36893709
| length = ( LENGTH | LEN) LR_BRACKET expr RR_BRACKET
36903710
| TO_BOOLEAN LR_BRACKET expr RR_BRACKET

sql/snowflake/examples/create_table.sql

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,3 @@ create or replace table tz2(i TIMESTAMPNTZ);
9595
create or replace table tz3(i TIMESTAMPTZ);
9696
CREATE TABLE TESTSEED2 (ident int IDENTITY INCREMENT 2 ORDER);
9797
CREATE TABLE TESTSEED2 (ident int IDENTITY START = 2 INCREMENT BY 1 NOORDER);
98-
CREATE TABLE DIRECTION (LENGTH INT,LANGUAGE INT);

sql/snowflake/examples/ids.sql

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
create table alert(i int);
2-
create table alerts(i int);
32
create table condition(i int);
43
create table dt_t(dynamic int, downstream int, target_lag int);
54
create table wh_id(MAX_CONCURRENCY_LEVEL int, WAREHOUSE_TYPE int);
5+
CREATE TABLE CAST (KEY int,TRY_CAST int);
6+
CREATE TABLE DIRECTION (LENGTH INT,LANGUAGE INT);
7+
CREATE or replace TEMP TABLE ROLES (USERS INT,WAREHOUSES int,INTEGRATIONS int,DATABASES int,SCHEMAS int);
8+
CREATE or replace TEMP TABLE TABLES (VIEWS INT,STAGES int,STREAMS int,TASKS int,ALERTS int);
9+
CREATE or replace TEMP TABLE ACTION (MODE int);

0 commit comments

Comments
 (0)