Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix select statement detection when used in parenthesis #197

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading