Skip to content

Commit

Permalink
[Snowflake]Refacto expr & id_ (#3884)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
MasterKuat committed Dec 13, 2023
1 parent a137695 commit 5792cf2
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 40 deletions.
96 changes: 58 additions & 38 deletions sql/snowflake/SnowflakeParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -2572,6 +2572,7 @@ object_type_plural
| STAGES
| STREAMS
| TASKS
| ALERTS
;

// drop commands
Expand Down Expand Up @@ -3363,24 +3364,26 @@ id_fn
;

id_
//id_ is used for object name. Snowflake is very permissive
//so we could use nearly all keyword as object name (table, column etc..)
: ID
| ID2
| DOUBLE_QUOTE_ID
| DOUBLE_QUOTE_BLANK
| keyword
| non_reserved_words
| object_type_plural
| data_type
| builtin_function
| ALERT
| ALERTS
| CONDITION
| binary_builtin_function
| binary_or_ternary_builtin_function
| ternary_builtin_function
;

keyword
: INT
| BIGINT
| STAGE
//List here keyword (SnowSQL meaning) allowed as object name
// Name of builtin function should be included in specifique section (ie builtin_function)
: STAGE
| USER
| TYPE
| CLUSTER
Expand All @@ -3398,10 +3401,27 @@ keyword
| DIRECTION
| LENGTH
| LANGUAGE
| KEY
| ALERT
| CONDITION
| ROLE
| ROW_NUMBER
| VALUE
| FIRST_VALUE
| VALUES
| TARGET_LAG
| EMAIL
| MAX_CONCURRENCY_LEVEL
| WAREHOUSE_TYPE
| TAG
| WAREHOUSE
| MODE
| ACTION
// etc
;

non_reserved_words
//List here lexer token referenced by rules which is not a keyword (SnowSQL Meaning) and allowed has object name
: ORGADMIN
| ACCOUNTADMIN
| SECURITYADMIN
Expand All @@ -3414,23 +3434,14 @@ non_reserved_words
| SOURCE
| PROCEDURE_NAME
| STATE
| ROLE
| DEFINITION
| TIMEZONE
| LOCAL
| ROW_NUMBER
| VALUE
| NAME
| TAG
| WAREHOUSE
| VERSION
| OPTION
| NVL2
| FIRST_VALUE
| RESPECT
| NVL
| RESTRICT
| VALUES
| EVENT
| DOWNSTREAM
| DYNAMIC
Expand All @@ -3444,9 +3455,8 @@ non_reserved_words

builtin_function
// If there is a lexer entry for a function we also need to add the token here
// as it otherwise will not be picked up by the id_ rule
: IFF
| SUM
// as it otherwise will not be picked up by the id_ rule (See also derived rule below)
: SUM
| AVG
| MIN
| COUNT
Expand All @@ -3459,23 +3469,19 @@ builtin_function
| FLATTEN
| SPLIT_TO_TABLE
| CAST
| TRY_CAST
;

list_operator
// lexer entry which admit a list of comma separated expr
: CONCAT
| CONCAT_WS
| COALESCE
// To complete as needed
;
//TODO : Split builtin between NoParam func,special_builtin_func (like CAST), unary_builtin_function and unary_or_binary_builtin_function for better AST

binary_builtin_function
// lexer entry of function name which admit 2 parameters
// expr rule use this
: ifnull = (IFNULL | NVL)
| GET
| LEFT
| RIGHT
| DATE_PART
| to_date = ( TO_DATE | DATE)
| to_date = (TO_DATE | DATE)
| SPLIT
| NULLIF
| EQUAL_NULL
Expand All @@ -3484,6 +3490,8 @@ binary_builtin_function
;

binary_or_ternary_builtin_function
// lexer entry of function name which admit 2 or 3 parameters
// expr rule use this
: CHARINDEX
| REPLACE
| substring = ( SUBSTRING | SUBSTR)
Expand All @@ -3492,10 +3500,22 @@ binary_or_ternary_builtin_function
;

ternary_builtin_function
// lexer entry of function name which admit 3 parameters
// expr rule use this
: dateadd = (DATEADD | TIMEADD | TIMESTAMPADD)
| datefiff = ( DATEDIFF | TIMEDIFF | TIMESTAMPDIFF)
| datefiff = (DATEDIFF | TIMEDIFF | TIMESTAMPDIFF)
| SPLIT_PART
| NVL2
| IFF
;

list_function
// lexer entry of function name which admit a list of comma separated expr
// expr rule use this
: CONCAT
| CONCAT_WS
| COALESCE
// To complete as needed
;

pattern
Expand Down Expand Up @@ -3544,8 +3564,6 @@ expr_list_sorted

expr
: object_name DOT NEXTVAL
| primitive_expression
| function_call
| expr LSB expr RSB //array access
| expr COLON expr //json access
| expr DOT (VALUE | expr)
Expand All @@ -3562,21 +3580,20 @@ expr
| expr OR expr //bool operation
| arr_literal
// | expr time_zone
| expr COLON_COLON data_type //cast
| expr over_clause
| cast_expr
| json_literal
| binary_builtin_function LR_BRACKET expr COMMA expr RR_BRACKET
| binary_or_ternary_builtin_function LR_BRACKET expr COMMA expr (COMMA expr)* RR_BRACKET
| ternary_builtin_function LR_BRACKET expr COMMA expr COMMA expr RR_BRACKET
| subquery
| expr COLON_COLON data_type // Cast also
| try_cast_expr
| json_literal
| trim_expression
| function_call
| subquery
| expr IS null_not_null
| expr NOT? IN LR_BRACKET (subquery | expr_list) RR_BRACKET
| expr NOT? ( LIKE | ILIKE) expr (ESCAPE expr)?
| expr NOT? RLIKE expr
| expr NOT? (LIKE | ILIKE) ANY LR_BRACKET expr (COMMA expr)* RR_BRACKET (ESCAPE expr)?
| primitive_expression //Should be latest rule as it's nearly a catch all
;

iff_expr
Expand Down Expand Up @@ -3680,11 +3697,14 @@ over_clause
;

function_call
: ranking_windowed_function
: binary_builtin_function LR_BRACKET expr COMMA expr RR_BRACKET
| binary_or_ternary_builtin_function LR_BRACKET expr COMMA expr (COMMA expr)* RR_BRACKET
| ternary_builtin_function LR_BRACKET expr COMMA expr COMMA expr RR_BRACKET
| ranking_windowed_function
| aggregate_function
// | aggregate_windowed_function
| object_name '(' expr_list? ')'
| list_operator LR_BRACKET expr_list RR_BRACKET
| list_function LR_BRACKET expr_list RR_BRACKET
| to_date = ( TO_DATE | DATE) LR_BRACKET expr RR_BRACKET
| length = ( LENGTH | LEN) LR_BRACKET expr RR_BRACKET
| TO_BOOLEAN LR_BRACKET expr RR_BRACKET
Expand Down
1 change: 0 additions & 1 deletion sql/snowflake/examples/create_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,3 @@ create or replace table tz2(i TIMESTAMPNTZ);
create or replace table tz3(i TIMESTAMPTZ);
CREATE TABLE TESTSEED2 (ident int IDENTITY INCREMENT 2 ORDER);
CREATE TABLE TESTSEED2 (ident int IDENTITY START = 2 INCREMENT BY 1 NOORDER);
CREATE TABLE DIRECTION (LENGTH INT,LANGUAGE INT);
6 changes: 5 additions & 1 deletion sql/snowflake/examples/ids.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
create table alert(i int);
create table alerts(i int);
create table condition(i int);
create table dt_t(dynamic int, downstream int, target_lag int);
create table wh_id(MAX_CONCURRENCY_LEVEL int, WAREHOUSE_TYPE int);
CREATE TABLE CAST (KEY int,TRY_CAST int);
CREATE TABLE DIRECTION (LENGTH INT,LANGUAGE INT);
CREATE or replace TEMP TABLE ROLES (USERS INT,WAREHOUSES int,INTEGRATIONS int,DATABASES int,SCHEMAS int);
CREATE or replace TEMP TABLE TABLES (VIEWS INT,STAGES int,STREAMS int,TASKS int,ALERTS int);
CREATE or replace TEMP TABLE ACTION (MODE int);

0 comments on commit 5792cf2

Please sign in to comment.