Skip to content

Commit

Permalink
Fix select statement detection when used in parenthesis (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
yfuruyama authored Nov 12, 2024
1 parent ed6c09a commit 892b4f3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
8 changes: 5 additions & 3 deletions statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ type QueryStats struct {
}

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

// DDL
createDatabaseRe = regexp.MustCompile(`(?is)^CREATE\s+DATABASE\s.+$`)
Expand Down Expand Up @@ -143,7 +145,7 @@ func BuildStatementWithComments(stripped, raw string) (Statement, error) {
case useRe.MatchString(stripped):
matched := useRe.FindStringSubmatch(stripped)
return &UseStatement{Database: unquoteIdentifier(matched[1]), Role: unquoteIdentifier(matched[2])}, nil
case selectRe.MatchString(stripped):
case selectRe.MatchString(stripped), graphRe.MatchString(stripped), callRe.MatchString(stripped):
return &SelectStatement{Query: raw}, nil
case createDatabaseRe.MatchString(stripped):
return &CreateDatabaseStatement{CreateStatement: stripped}, nil
Expand Down
15 changes: 15 additions & 0 deletions statement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@ func TestBuildStatement(t *testing.T) {
input: "@{USE_ADDITIONAL_PARALLELISM=TRUE} SELECT * FROM t1",
want: &SelectStatement{Query: "@{USE_ADDITIONAL_PARALLELISM=TRUE} SELECT * FROM t1"},
},
{
desc: "SELECT statement in parenthesis",
input: "(SELECT * FROM t1)",
want: &SelectStatement{Query: "(SELECT * FROM t1)"},
},
{
desc: "WITH statement in parenthesis",
input: "(WITH sub AS (SELECT 1) SELECT * FROM sub)",
want: &SelectStatement{Query: "(WITH sub AS (SELECT 1) SELECT * FROM sub)"},
},
{
desc: "SELECT statement in parenthesis with statement hint",
input: "@{USE_ADDITIONAL_PARALLELISM=TRUE} (SELECT * FROM t1)",
want: &SelectStatement{Query: "@{USE_ADDITIONAL_PARALLELISM=TRUE} (SELECT * FROM t1)"},
},
{
desc: "CREATE DATABASE statement",
input: "CREATE DATABASE d1",
Expand Down

0 comments on commit 892b4f3

Please sign in to comment.