Skip to content

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

feat(span): skip temporary table schema (#14681)

h3n4lbc4d2b2a050f
diff --git a/backend/plugin/db/mssql/mssql.go b/backend/plugin/db/mssql/mssql.go
index 9080715f99..9c9bd705d7 100644
--- a/backend/plugin/db/mssql/mssql.go
+++ b/backend/plugin/db/mssql/mssql.go
@@ -365,6 +365,7 @@ func (driver *Driver) queryBatch(ctx context.Context, conn *sql.Conn, batch stri
// for SELECT statement for now.
if skipRowsAffected {
skipRowsAffected = false
+ nextAffectedRowsIdx = getNextAffectedRowsIdx(stmtTypes, nextAffectedRowsIdx+1)
continue
}
queryResult = util.BuildAffectedRowsResult(m.Count)
@@ -424,7 +425,7 @@ func isExitError(err error) error {
func getNextAffectedRowsIdx(s []stmtType, beginIdx int) int {
for i := beginIdx; i < len(s); i++ {
- if s[i] == stmtTypeResultSetRowCountGenerating {
+ if s[i]&stmtTypeRowCountGenerating != 0 {
return i
}
}
@@ -433,7 +434,7 @@ func getNextAffectedRowsIdx(s []stmtType, beginIdx int) int {
func getNextResultSetIdx(s []stmtType, beginIdx int) int {
for i := beginIdx; i < len(s); i++ {
- if s[i] == stmtTypeResultSetGenerating {
+ if s[i]&stmtTypeResultSetGenerating != 0 {
return i
}
}
diff --git a/backend/plugin/db/mssql/stmt_type.go b/backend/plugin/db/mssql/stmt_type.go
index d0a159eeaf..292b8ea483 100644
--- a/backend/plugin/db/mssql/stmt_type.go
+++ b/backend/plugin/db/mssql/stmt_type.go
@@ -11,9 +11,9 @@ import (
type stmtType = int
const (
- stmtTypeUnknown stmtType = iota
+ stmtTypeUnknown = 1 << iota
stmtTypeResultSetGenerating
- stmtTypeResultSetRowCountGenerating
+ stmtTypeRowCountGenerating
)
type stmtTypeListener struct {
@@ -52,10 +52,14 @@ func (l *stmtTypeListener) EnterBatch_without_go(ctx *rawparser.Batch_without_go
func getStmtTypeFromSQLClauses(ctx rawparser.ISql_clausesContext) (stmtType, error) {
switch {
case ctx.Dml_clause() != nil:
- if ctx.Dml_clause().Select_statement_standalone() != nil {
- return stmtTypeResultSetGenerating, nil
+ if v := ctx.Dml_clause().Select_statement_standalone(); v != nil {
+ if v.Select_statement().Query_expression().Query_specification() != nil && v.Select_statement().Query_expression().Query_specification().INTO() != nil {
+ // SELECT INTO will generate the row count only.
+ return stmtTypeRowCountGenerating, nil
+ }
+ return stmtTypeResultSetGenerating | stmtTypeRowCountGenerating, nil
}
- return stmtTypeResultSetRowCountGenerating, nil
+ return stmtTypeRowCountGenerating, nil
case ctx.Cfl_statement() != nil:
return stmtTypeUnknown, errors.Errorf("unsupported control flow statement")
case ctx.Another_statement() != nil:
diff --git a/backend/plugin/parser/tsql/query_span_extractor.go b/backend/plugin/parser/tsql/query_span_extractor.go
index 828b510aee..29919fee66 100644
--- a/backend/plugin/parser/tsql/query_span_extractor.go
+++ b/backend/plugin/parser/tsql/query_span_extractor.go
@@ -5,6 +5,7 @@ import (
"fmt"
"log/slog"
"sort"
+ "strings"
"unicode"
"github.com/antlr4-go/antlr/v4"
@@ -643,6 +644,10 @@ func (q *querySpanExtractor) tsqlFindTableSchema(fullTableName parser.IFull_tabl
// TODO(zp): How do we handle the linked server?
return nil, errors.Errorf("linked server is not supported yet, but found %q", fullTableName.GetText())
}
+ if strings.HasPrefix(normalizedTableName, "#") {
+ // TODO(masking): Considering SELECT * INTO #temp FROM dbo.t1; SELECT * FROM #temp. We should mask the #temp.
+ return &base.PseudoTable{}, nil
+ }
// For SQL Server, the cte will shadow the physical tables, so we check the ctes first,
// also, we record the cte in ascending order, so we should check the ctes in descending order