Skip to content

Commit

Permalink
Nake DT query parser handle two comment positions
Browse files Browse the repository at this point in the history
The "correct" place to specify the query comment in a dynamic table DDL
is at the end of the metadata argument list, right before the query.
However, when retrieving an existing query from Snowflake using SHOW
DYNAMIC TABLE, the comment is placed at the beginning of the metadata
argument list.

This commit updates the DT query parser to handle comments that can show
up in either position.
  • Loading branch information
sonya committed Feb 11, 2024
1 parent 0bc8b9d commit 615f7be
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
2 changes: 2 additions & 0 deletions pkg/snowflake/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ func (e *ViewSelectStatementExtractor) ExtractDynamicTable() (string, error) {
e.consumeSpace()
e.consumeTokenParameter("warehouse")
e.consumeSpace()
e.consumeComment()
e.consumeSpace()
e.consumeToken("as")
e.consumeSpace()

Expand Down
16 changes: 11 additions & 5 deletions pkg/snowflake/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ lag = 'DOWNSTREAM'
refresh_mode = 'AUTO'
initialize = 'ON_CREATE'
warehouse = COMPUTE_WH
as select * from bar;`
as select *
from bar;`

multilineComment := `
create dynamic table foo
Expand All @@ -160,10 +161,14 @@ as
select *
from bar;`

comment := `create dynamic table foo comment = 'asdf' lag = 'DOWNSTREAM' refresh_mode = 'AUTO' initialize = 'ON_CREATE' warehouse = COMPUTE_WH as select * from bar;`
commentEscape := `create dynamic table foo comment = 'asdf\'s are fun' lag = 'DOWNSTREAM' refresh_mode = 'AUTO' initialize = 'ON_CREATE' warehouse = COMPUTE_WH as select * from bar;`
orReplace := `create or replace dynamic table foo comment = 'asdf' lag = 'DOWNSTREAM' refresh_mode = 'AUTO' initialize = 'ON_CREATE' warehouse = COMPUTE_WH as select * from bar;`
identifier := `create or replace dynamic table "foo"."bar"."bam" comment = 'asdf\'s are fun' lag = 'DOWNSTREAM' refresh_mode = 'AUTO' initialize = 'ON_CREATE' warehouse = COMPUTE_WH as select * from bar;`
comment := `create dynamic table foo lag = 'DOWNSTREAM' refresh_mode = 'AUTO' initialize = 'ON_CREATE' warehouse = COMPUTE_WH comment = 'asdf' as select * from bar;`
commentEscape := `create dynamic table foo lag = 'DOWNSTREAM' refresh_mode = 'AUTO' initialize = 'ON_CREATE' warehouse = COMPUTE_WH comment = 'asdf\'s are fun' as select * from bar;`
orReplace := `create or replace dynamic table foo lag = 'DOWNSTREAM' refresh_mode = 'AUTO' initialize = 'ON_CREATE' warehouse = COMPUTE_WH comment = 'asdf' as select * from bar;`
identifier := `create or replace dynamic table "foo"."bar"."bam" lag = 'DOWNSTREAM' refresh_mode = 'AUTO' initialize = 'ON_CREATE' warehouse = COMPUTE_WH comment = 'asdf\'s are fun' as select * from bar;`
// running SHOW DYNAMIC TABLE in Snowflake actually returns the query with
// the comment before other parameters, even though this is inconsistent
// with the order they are specified in CREATE DYNAMIC TABLE
commentBeforeOtherParams := `create dynamic table foo comment = 'asdf\'s are fun' lag = 'DOWNSTREAM' refresh_mode = 'AUTO' initialize = 'ON_CREATE' warehouse = COMPUTE_WH as select * from bar;`

type args struct {
input string
Expand All @@ -183,6 +188,7 @@ from bar;`
{"commentEscape", args{commentEscape}, "select * from bar;", false},
{"orReplace", args{orReplace}, "select * from bar;", false},
{"identifier", args{identifier}, "select * from bar;", false},
{"commentBeforeOtherParams", args{commentBeforeOtherParams}, "select * from bar;", false},
}
for _, tt := range tests {
tt := tt
Expand Down

0 comments on commit 615f7be

Please sign in to comment.