Skip to content

Commit 892b4f3

Browse files
authored
Fix select statement detection when used in parenthesis (#197)
1 parent ed6c09a commit 892b4f3

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

statement.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,10 @@ type QueryStats struct {
8484
}
8585

8686
var (
87-
// SQL
88-
selectRe = regexp.MustCompile(`(?is)^(?:WITH|CALL|@{.+|SELECT|GRAPH)\s.+$`)
87+
// Query
88+
selectRe = regexp.MustCompile(`(?is)^(@{[^}]+}\s*)?(\(\s*)?(?:WITH|SELECT)\s.+$`)
89+
graphRe = regexp.MustCompile(`(?is)^GRAPH\s.+$`)
90+
callRe = regexp.MustCompile(`(?is)^CALL\s.+$`)
8991

9092
// DDL
9193
createDatabaseRe = regexp.MustCompile(`(?is)^CREATE\s+DATABASE\s.+$`)
@@ -143,7 +145,7 @@ func BuildStatementWithComments(stripped, raw string) (Statement, error) {
143145
case useRe.MatchString(stripped):
144146
matched := useRe.FindStringSubmatch(stripped)
145147
return &UseStatement{Database: unquoteIdentifier(matched[1]), Role: unquoteIdentifier(matched[2])}, nil
146-
case selectRe.MatchString(stripped):
148+
case selectRe.MatchString(stripped), graphRe.MatchString(stripped), callRe.MatchString(stripped):
147149
return &SelectStatement{Query: raw}, nil
148150
case createDatabaseRe.MatchString(stripped):
149151
return &CreateDatabaseStatement{CreateStatement: stripped}, nil

statement_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,21 @@ func TestBuildStatement(t *testing.T) {
6666
input: "@{USE_ADDITIONAL_PARALLELISM=TRUE} SELECT * FROM t1",
6767
want: &SelectStatement{Query: "@{USE_ADDITIONAL_PARALLELISM=TRUE} SELECT * FROM t1"},
6868
},
69+
{
70+
desc: "SELECT statement in parenthesis",
71+
input: "(SELECT * FROM t1)",
72+
want: &SelectStatement{Query: "(SELECT * FROM t1)"},
73+
},
74+
{
75+
desc: "WITH statement in parenthesis",
76+
input: "(WITH sub AS (SELECT 1) SELECT * FROM sub)",
77+
want: &SelectStatement{Query: "(WITH sub AS (SELECT 1) SELECT * FROM sub)"},
78+
},
79+
{
80+
desc: "SELECT statement in parenthesis with statement hint",
81+
input: "@{USE_ADDITIONAL_PARALLELISM=TRUE} (SELECT * FROM t1)",
82+
want: &SelectStatement{Query: "@{USE_ADDITIONAL_PARALLELISM=TRUE} (SELECT * FROM t1)"},
83+
},
6984
{
7085
desc: "CREATE DATABASE statement",
7186
input: "CREATE DATABASE d1",

0 commit comments

Comments
 (0)