Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat(query-type): recognize sql server explain (#14392)

h3n4lc8dbf2fa0657
diff --git a/backend/plugin/parser/base/interface.go b/backend/plugin/parser/base/interface.go
index ed9378248c..e75348fef3 100644
--- a/backend/plugin/parser/base/interface.go
+++ b/backend/plugin/parser/base/interface.go
@@ -4,6 +4,8 @@ import (
"bytes"
"context"
"fmt"
+ "regexp"
+ "strings"
"sync"
"github.com/pkg/errors"
@@ -196,10 +198,12 @@ func GetQuerySpan(ctx context.Context, gCtx GetQuerySpanContext, engine storepb.
return nil, err
}
var results []*QuerySpan
+ var nonEmptyStatement []string
for _, stmt := range statements {
if stmt.Empty {
continue
}
+ nonEmptyStatement = append(nonEmptyStatement, stmt.Text)
result, err := f(ctx, gCtx, stmt.Text, database, schema, ignoreCaseSensitive)
if err != nil {
// Try to unwrap the error to see if it's a ResourceNotFoundError to decrease the error noise.
@@ -216,6 +220,9 @@ func GetQuerySpan(ctx context.Context, gCtx GetQuerySpanContext, engine storepb.
}
results = append(results, result)
}
+ if engine == storepb.Engine_MSSQL {
+ TSQLRecognizeExplainType(results, nonEmptyStatement)
+ }
return results, nil
}
@@ -277,3 +284,43 @@ func NewRange(statement, singleSQL string) *storepb.Range {
End: int32(start + len(singleSQLBytes)),
}
}
+
+// REFACTOR(zp): Put it here to avoid circular import for now.
+var (
+ showplanReg = regexp.MustCompile(`(?mi)^\s*SET\s+SHOWPLAN_(ALL|XML|TEXT)\s+(?P<status>(ON|OFF))\s*;?$`)
+)
+
+// TSQLRecognizeExplainType walks the spans, and rewrite the select type to explain type if previous statement is SET SHOWPLAN_ALL.
+func TSQLRecognizeExplainType(spans []*QuerySpan, stmt []string) {
+ if len(spans) != len(stmt) {
+ return
+ }
+ on := false
+ for i := range spans {
+ matches := showplanReg.FindStringSubmatch(stmt[i])
+ if matches != nil {
+ for k, name := range showplanReg.SubexpNames() {
+ if k != 0 && name == "status" {
+ switch strings.ToLower(matches[k]) {
+ case "on":
+ on = true
+ case "off":
+ on = false
+ }
+ }
+ }
+ }
+ if on {
+ if matches != nil {
+ spans[i].Type = Explain
+ } else if spans[i].Type == Select {
+ spans[i].Type = Explain
+ }
+ } else {
+ // SET SHOW_PLANALL OFF, this statement is explain either.
+ if matches != nil {
+ spans[i].Type = Explain
+ }
+ }
+ }
+}
diff --git a/backend/plugin/parser/base/interface_test.go b/backend/plugin/parser/base/interface_test.go
new file mode 100644
index 0000000000..03ce811f45
--- /dev/null
+++ b/backend/plugin/parser/base/interface_test.go
@@ -0,0 +1,64 @@
+package base
+
+import "testing"
+
+func TestTSQLRecognizeExplainType(t *testing.T) {
+ testCases := []struct {
+ spans []*QuerySpan
+ stmts []string
+ wantSpans []*QuerySpan
+ }{
+ {
+ spans: []*QuerySpan{
+ {
+ Type: Select,
+ },
+ {
+ Type: Select,
+ },
+ {
+ Type: Select,
+ },
+ {
+ Type: Select,
+ },
+ {
+ Type: Select,
+ },
+ },
+ stmts: []string{
+ "SELECT 1",
+ "SET SHOWPLAN_TEXT ON",
+ "SELECT 2",
+ "SET SHOWPLAN_TEXT OFF",
+ "SELECT 3",
+ },
+ wantSpans: []*QuerySpan{
+ {
+ Type: Select,
+ },
+ {
+ Type: Explain,
+ },
+ {
+ Type: Explain,
+ },
+ {
+ Type: Explain,
+ },
+ {
+ Type: Select,
+ },
+ },
+ },
+ }
+
+ for _, tc := range testCases {
+ TSQLRecognizeExplainType(tc.spans, tc.stmts)
+ for i := range tc.spans {
+ if tc.spans[i].Type != tc.wantSpans[i].Type {
+ t.Errorf("TSQLRecognizeExplainType() = %v, want %v", tc.spans[i].Type, tc.wantSpans[i].Type)
+ }
+ }
+ }
+}