Skip to content

Commit

Permalink
[Snowflake]CREATE TABLE improvment (#4228)
Browse files Browse the repository at this point in the history
* * Remove useless paren
* Add support for comment on outline constraint
* Add support for OR ALTER

(cherry picked from commit 1732a5c5b03da7764124c93470ba71b8b617ecac)

* Fix Format

(cherry picked from commit 450f0308640fec08cafa9e3f7a486c5b86f1ea70)

* Add FREQUENCY keyword
  • Loading branch information
MasterKuat authored Sep 15, 2024
1 parent 97bfedb commit 5d7723a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
35 changes: 21 additions & 14 deletions sql/snowflake/SnowflakeParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -1721,7 +1721,8 @@ col_decl
;

virtual_column_decl
: AS '(' function_call ')'
: AS LR_BRACKET function_call RR_BRACKET
| AS function_call
;

function_definition
Expand Down Expand Up @@ -2347,7 +2348,7 @@ create_stream
: CREATE or_replace? STREAM if_not_exists? object_name with_tags? copy_grants? ON (
TABLE
|VIEW
) object_name stream_time? append_only? show_initial_rows? comment_clause?
) object_name stream_time? append_only? show_initial_rows? comment_clause?
//-- External table
| CREATE or_replace? STREAM if_not_exists? object_name with_tags? copy_grants? ON EXTERNAL TABLE object_name stream_time? insert_only?
comment_clause?
Expand All @@ -2361,7 +2362,8 @@ temporary
;

table_type
: (( LOCAL | GLOBAL)? temporary | VOLATILE)
: (LOCAL | GLOBAL)? temporary
| VOLATILE
| TRANSIENT
;

Expand Down Expand Up @@ -2416,14 +2418,12 @@ out_of_line_constraint
: (CONSTRAINT id_)? (
(UNIQUE | primary_key) column_list_in_parentheses common_constraint_properties*
| foreign_key column_list_in_parentheses REFERENCES object_name column_list_in_parentheses constraint_properties
)
) inline_comment_clause?
;

//For classic table
full_col_decl
: col_decl (collate | inline_constraint | null_not_null | (default_value | NULL_))* with_masking_policy? with_tags? (
COMMENT string
)?
: col_decl (collate | inline_constraint | null_not_null | default_value)* with_masking_policy? with_tags? inline_comment_clause?
;

//Column declaration for materialized table
Expand All @@ -2445,24 +2445,22 @@ column_decl_item_list
;

create_table
: CREATE or_replace? table_type? TABLE (
: CREATE (or_replace | or_alter)? table_type? TABLE (
if_not_exists? object_name
| object_name if_not_exists?
) ((comment_clause? create_table_clause) | (create_table_clause comment_clause?))
) (comment_clause? create_table_clause | create_table_clause comment_clause?)
;

column_decl_item_list_paren
: '(' column_decl_item_list ')'
: LR_BRACKET column_decl_item_list RR_BRACKET
;

create_table_clause
: (
column_decl_item_list_paren cluster_by?
| cluster_by? comment_clause? column_decl_item_list_paren
) stage_file_format? (STAGE_COPY_OPTIONS EQ LR_BRACKET copy_options RR_BRACKET)? (
DATA_RETENTION_TIME_IN_DAYS EQ num
)? (MAX_DATA_EXTENSION_TIME_IN_DAYS EQ num)? change_tracking? default_ddl_collation? copy_grants? comment_clause? with_row_access_policy?
with_tags?
) stage_file_format? (STAGE_COPY_OPTIONS EQ LR_BRACKET copy_options RR_BRACKET)? set_data_retention_params? change_tracking?
default_ddl_collation? copy_grants? comment_clause? with_row_access_policy? with_tags?
;

create_table_as_select
Expand Down Expand Up @@ -2962,6 +2960,10 @@ comment_clause
: COMMENT EQ string
;

inline_comment_clause
: COMMENT string
;

if_suspended
: IF SUSPENDED
;
Expand All @@ -2978,6 +2980,10 @@ or_replace
: OR REPLACE
;

or_alter
: OR ALTER
;

describe
: DESC
| DESCRIBE
Expand Down Expand Up @@ -3685,6 +3691,7 @@ non_reserved_words
| VALUES
| VERSION
| WAREHOUSE_TYPE
| FREQUENCY
;

builtin_function
Expand Down
15 changes: 11 additions & 4 deletions sql/snowflake/examples/create_table.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
--Keyword tester
CREATE TABLE TESTKEYWORD(IFNULL int, NVL int, GET int, LEFT int,RIGHT int, DATE_PART int,TO_DATE int,DATE int,SPLIT int,NULLIF int,EQUAL_NULL int );
create table TestK (NVL2 string, FIRST_VALUE string, RESTRICT int, NVL int, RESPECT int);

create temporary table t(i int);
create table t1 (v varchar(16777216));
Expand Down Expand Up @@ -59,9 +60,9 @@ CREATE TABLE T1 (TIMESTAMP DATETIME,VALUE STRING,NAME STRING);
CREATE OR REPLACE TABLE T1 (C1 STRING,UNIQUE(C1));
CREATE OR REPLACE TABLE T2 (C2 STRING,PRIMARY KEY(C2));
CREATE OR REPLACE TABLE T3 (C3 STRING,FOREIGN KEY(C3) REFERENCES T2(C2));
CREATE OR REPLACE TABLE T1 (C1 STRING,CONSTRAINT ANAME UNIQUE(C1));
CREATE OR REPLACE TABLE T2 (C2 STRING,CONSTRAINT BNAME PRIMARY KEY(C2));
CREATE OR REPLACE TABLE T3 (C3 STRING,CONSTRAINT CNAME FOREIGN KEY(C3) REFERENCES T2(C2));
CREATE OR REPLACE TABLE T1 (C1 STRING,CONSTRAINT ANAME UNIQUE(C1) COMMENT 'Unique');
CREATE OR REPLACE TABLE T2 (C2 STRING,CONSTRAINT BNAME PRIMARY KEY(C2) COMMENT 'PK');
CREATE OR REPLACE TABLE T3 (C3 STRING,CONSTRAINT CNAME FOREIGN KEY(C3) REFERENCES T2(C2) COMMENT 'FK');
-- constraint properties
CREATE OR REPLACE TABLE T1 (C1 STRING,CONSTRAINT ANAME UNIQUE(C1) RELY ENFORCED VALIDATE );
CREATE OR REPLACE TABLE T1 (C1 STRING,CONSTRAINT ANAME UNIQUE(C1) INITIALLY IMMEDIATE NOT DEFERRABLE );
Expand All @@ -73,7 +74,7 @@ CREATE OR REPLACE TABLE T1 (C1 STRING UNIQUE INITIALLY IMMEDIATE NOT DEFERRABLE
CREATE OR REPLACE TABLE T3 (C3 STRING FOREIGN KEY REFERENCES T2(C2) DEFERRABLE );
CREATE OR REPLACE TABLE T3 (C3 STRING CONSTRAINT INCNAME FOREIGN KEY REFERENCES T2(C2) ON UPDATE NO ACTION );
CREATE OR REPLACE TABLE T3 (C3 STRING FOREIGN KEY REFERENCES T2 MATCH PARTIAL ON UPDATE SET NULL ON DELETE RESTRICT );
create table TestK (NVL2 string, FIRST_VALUE string, RESTRICT int, NVL int, RESPECT int);


Create table T1(C1 string) WITH TAG ( TAG_NAME='T1');
create table t
Expand All @@ -86,8 +87,10 @@ select c from q;
create table tpk (i int primary key);
alter table tpk drop primary key;

--Comment in any order
create table tc1 comment = '' (i int);
create table tc2 (i int) comment = '';

create table tc3 (c char(4), c2 character(2));

create or replace table tz1(i TIMESTAMPLTZ);
Expand All @@ -106,3 +109,7 @@ create table if not exists t3 cluster by LINEAR(f1) (f1 varchar, f2 number) ;
create table t4 (f1 number, f2 number, f3 number as (hash(f1,f2)));
create table t4 (f1 number, f2 number, f3 number as (concat_ws(',',f1,f2)));
create table floor (any_value int,getdate int);
CREATE TABLE T (C1 int,C2 int,C3 int as hash(C1,C2));

--Create or alter
create or alter table T (C1 INT);

0 comments on commit 5d7723a

Please sign in to comment.