Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor(tsql): migrate query span extractor to omni (#20101)
rebelice14a6c3d21f3a
diff --git a/backend/plugin/parser/tsql/query_span.go b/backend/plugin/parser/tsql/query_span.goindex 633ec70f4f..5854307f7d 100644--- a/backend/plugin/parser/tsql/query_span.go+++ b/backend/plugin/parser/tsql/query_span.go@@ -12,6 +12,7 @@ func init() {}// GetQuerySpan returns the query span for the given statement.+// Runs on the omni AST extractor.func GetQuerySpan(ctx context.Context,gCtx base.GetQuerySpanContext,@@ -19,10 +20,6 @@ func GetQuerySpan(database, schema string,ignoreCaseSensitive bool,) (*base.QuerySpan, error) {- q := newQuerySpanExtractor(database, schema, gCtx, ignoreCaseSensitive)- querySpan, err := q.getQuerySpan(ctx, stmt.Text)- if err != nil {- return nil, err- }- return querySpan, nil+ q := newOmniQuerySpanExtractor(database, schema, gCtx, ignoreCaseSensitive)+ return q.getOmniQuerySpan(ctx, stmt.Text)}diff --git a/backend/plugin/parser/tsql/query_span_extractor.go b/backend/plugin/parser/tsql/query_span_extractor.goindex 0f935d2ede..b05d02392c 100644--- a/backend/plugin/parser/tsql/query_span_extractor.go+++ b/backend/plugin/parser/tsql/query_span_extractor.go@@ -2,13 +2,11 @@ package tsqlimport ("context"- "fmt""log/slog"- "slices""strings""unicode"- "github.com/antlr4-go/antlr/v4"+ "github.com/bytebase/omni/mssql/ast"parser "github.com/bytebase/parser/tsql""github.com/pkg/errors"@@ -16,6 +14,9 @@ import ("github.com/bytebase/bytebase/backend/plugin/parser/base")+// querySpanExtractor holds the shared identifier-resolution and scoping state+// used by the omni-based query span extractor. Only string-based helpers live+// on this type; AST-walking logic lives on *omniQuerySpanExtractor.type querySpanExtractor struct {ctx context.Context@@ -24,16 +25,15 @@ type querySpanExtractor struct {ignoreCaseSensitive boolgCtx base.GetQuerySpanContext- // Private fields.- // ctes is used to record the common table expressions (CTEs) in the query.- // It should be shrunk to the previous length while exiting the query scope.+ // ctes records the common table expressions visible in the current scope.+ // Pushed when entering a WITH clause; truncated on exit.ctes []*base.PseudoTable- // outerTableSources is used to record the outer table sources in the query.- // It's used to resolve the column name in the correlated subquery.+ // outerTableSources lets correlated subqueries resolve columns against the+ // enclosing query's FROM clause.outerTableSources []base.TableSource- // tableSourcesFrom is the list of table sources from the FROM clause.+ // tableSourcesFrom is this scope's FROM clause.tableSourcesFrom []base.TableSourcepredicateColumns base.SourceColumnSet@@ -54,752 +54,152 @@ func newQuerySpanExtractor(defaultDatabase string, defaultSchema string, gCtx ba}}-func (q *querySpanExtractor) getQuerySpan(ctx context.Context, statement string) (*base.QuerySpan, error) {- q.ctx = ctx-- antlrASTs, err := ParseTSQL(statement)- if err != nil {- return nil, err- }-- if len(antlrASTs) == 0 {- return &base.QuerySpan{- Type: base.Select,- SourceColumns: make(base.SourceColumnSet),- Results: []base.QuerySpanResult{},- }, nil- }-- if len(antlrASTs) != 1 {- return nil, errors.Errorf("expected exactly 1 statement, got %d", len(antlrASTs))- }-- ast := antlrASTs[0]- tree := ast.Tree- if tree == nil {- return nil, nil- }-- accessTables := getAccessTables(q.defaultDatabase, q.defaultSchema, tree)- // We do not support simultaneous access to the system table and the user table- // because we do not synchronize the schema of the system table.- // This causes an error (NOT_FOUND) when using querySpanExtractor.findTableSchema.- // As a result, we exclude getting query span results for accessing only the system table.- allSystems, mixed := isMixedQuery(accessTables, q.ignoreCaseSensitive)- if mixed {- return nil, base.MixUserSystemTablesError- }-- omniStmts, omniErr := ParseTSQLOmni(statement)- if omniErr != nil {- return nil, omniErr- }- queryType := base.Select- if len(omniStmts) > 0 {- queryType = classifyQueryType(omniStmts[0].AST, allSystems)- }- if queryType != base.Select {- return &base.QuerySpan{- Type: queryType,- SourceColumns: base.SourceColumnSet{},- Results: []base.QuerySpanResult{},- }, nil- }-- // We assume the caller had handled the statement type case,- // so we only need to handle the determined statement type here.- // In order to decrease the maintenance cost, we use listener- // to handle the select statement precisely.- listener := &tsqlSelectOnlyListener{- extractor: q,- }- // Walk all ASTs to collect temp table declarations from earlier statements- for _, a := range antlrASTs {- if a.Tree != nil {- antlr.ParseTreeWalkerDefault.Walk(listener, a.Tree)- }- }- err = listener.err- if err != nil {- var resourceNotFound *base.ResourceNotFoundError- if errors.As(err, &resourceNotFound) {- return &base.QuerySpan{- Type: queryType,- SourceColumns: accessTables,- Results: []base.QuerySpanResult{},- NotFoundError: resourceNotFound,- }, nil- }-- return nil, err- }-- return &base.QuerySpan{- Type: queryType,- SourceColumns: accessTables,- Results: listener.result,- PredicateColumns: q.predicateColumns,- }, nil-}--type tsqlSelectOnlyListener struct {- *parser.BaseTSqlParserListener-- extractor *querySpanExtractor- result []base.QuerySpanResult- err error-}--func (listener *tsqlSelectOnlyListener) EnterDeclare_statement(ctx *parser.Declare_statementContext) {- if ctx.Table_type_definition() != nil {- tableName := ctx.LOCAL_ID().GetText()- table := &base.PhysicalTable{- Name: tableName,- }-- l := &tempTableColumnDefinitionListener{- table: table,- }- antlr.ParseTreeWalkerDefault.Walk(l, ctx.Table_type_definition())- listener.extractor.gCtx.TempTables[tableName] = table- }-}--type tempTableColumnDefinitionListener struct {- *parser.BaseTSqlParserListener-- table *base.PhysicalTable-}--func (listener *tempTableColumnDefinitionListener) EnterColumn_definition(ctx *parser.Column_definitionContext) {- columnName := ctx.Id_().GetText()- listener.table.Columns = append(listener.table.Columns, columnName)-}--// EnterSelect_statement_standalone is called when production select_statement_standalone is entered.-func (listener *tsqlSelectOnlyListener) EnterDml_clause(ctx *parser.Dml_clauseContext) {- if ctx.Select_statement_standalone() == nil {- return- }-- result, err := listener.extractor.extractTSqlSensitiveFieldsFromSelectStatementStandalone(ctx.Select_statement_standalone())- if err != nil {- listener.err = err- return- }-- listener.result = result.GetQuerySpanResult()-}--// extractTSqlSensitiveFieldsFromSelectStatementStandalone extracts sensitive fields from select_statement_standalone.-func (q *querySpanExtractor) extractTSqlSensitiveFieldsFromSelectStatementStandalone(ctx parser.ISelect_statement_standaloneContext) (*base.PseudoTable, error) {- if ctx == nil {- return nil, nil- }-- if ctx.With_expression() != nil {- allCommonTableExpression := ctx.With_expression().AllCommon_table_expression()- // TSQL do not have `RECURSIVE` keyword, if we detect `UNION`, we will treat it as `RECURSIVE`.- for _, commonTableExpression := range allCommonTableExpression {- _, normalizedCTEName := NormalizeTSQLIdentifier(commonTableExpression.GetExpression_name())- var columns []base.QuerySpanResult- // If statement has more than one UNION, the first one is the anchor, and the rest are recursive.- recursiveCTE := false- queryExpression := commonTableExpression.Select_statement().Query_expression()- if queryExpression.Query_specification() != nil {- anchorTable, err := q.extractTSqlSensitiveFieldsFromQuerySpecification(queryExpression.Query_specification())- if err != nil {- return nil, errors.Wrapf(err, "failed to extract sensitive fields from `query_specification` in `query_expression`")- }- columns = anchorTable.GetQuerySpanResult()- if allSQLUnions := queryExpression.AllSql_union(); len(allSQLUnions) > 0 {- recursiveCTE = true- for i := 0; i < len(allSQLUnions)-1; i++ {- // For UNION operator, the number of the columns in the result set is the same, and will use the left part's column name.- // So we only need to extract the sensitive fields of the recursiveTableSource part.- recursiveTableSource, err := q.extractTSqlSensitiveFieldsFromQuerySpecification(allSQLUnions[i].Query_specification())- if err != nil {- return nil, errors.Wrapf(err, "failed to extract the %d set operator near line %d", i+1, allSQLUnions[i].GetStart().GetLine())- }- recursiveTableSourceQuerySpanResult := recursiveTableSource.GetQuerySpanResult()- if len(columns) != len(recursiveTableSourceQuerySpanResult) {- return nil, errors.Wrapf(err, "the number of columns in the query statement nearly line %d returns %d fields, but %d set operator near line %d returns %d fields", ctx.GetStart().GetLine(), len(columns), i+1, allSQLUnions[i].GetStart().GetLine(), len(recursiveTableSourceQuerySpanResult))- }- for j := range recursiveTableSourceQuerySpanResult {- columns[j].SourceColumns, _ = base.MergeSourceColumnSet(columns[j].SourceColumns, recursiveTableSourceQuerySpanResult[j].SourceColumns)- }- }- }- } else if allQueryExpression := queryExpression.AllQuery_expression(); len(allQueryExpression) > 0 {- if len(allQueryExpression) > 1 {- recursiveCTE = true- }- anchorTable, err := q.extractTSqlSensitiveFieldsFromQueryExpression(allQueryExpression[0])- if err != nil {- return nil, errors.Wrapf(err, "failed to extract sensitive fields from `query_specification` in `query_expression`")- }- columns = anchorTable.GetQuerySpanResult()- }- if recursiveCTE {- tempCte := &base.PseudoTable{- Name: normalizedCTEName,- Columns: columns,- }- originalSize := len(q.ctes)- for {- q.ctes = q.ctes[:originalSize]- q.ctes = append(q.ctes, tempCte)- change := false- if queryExpression.Query_specification() != nil && len(queryExpression.AllSql_union()) > 0 {- recursiveTableSource, err := q.extractTSqlSensitiveFieldsFromQuerySpecification(queryExpression.AllSql_union()[len(queryExpression.AllSql_union())-1].Query_specification())- if err != nil {- return nil, errors.Wrapf(err, "failed to extract sensitive fields of the recursive clause of recursive CTE %q near line %d", normalizedCTEName, queryExpression.AllSql_union()[len(queryExpression.AllSql_union())-1].Query_specification().GetStart().GetLine())- }- recursiveTableSourceQuerySpanResult := recursiveTableSource.GetQuerySpanResult()- if len(recursiveTableSourceQuerySpanResult) != len(tempCte.GetQuerySpanResult()) {- return nil, errors.Wrapf(err, "recursive clause returns %d fields, but anchor clause returns %d fields in recursive CTE %q near line %d", len(recursiveTableSourceQuerySpanResult), len(tempCte.GetQuerySpanResult()), normalizedCTEName, queryExpression.AllSql_union()[len(queryExpression.AllSql_union())-1].Query_specification().GetStart().GetLine())- }- for i := 0; i < len(recursiveTableSourceQuerySpanResult); i++ {- var anyChange bool- tempCte.Columns[i].SourceColumns, anyChange = base.MergeSourceColumnSet(tempCte.Columns[i].SourceColumns, recursiveTableSourceQuerySpanResult[i].SourceColumns)- change = change || anyChange- }- } else if allQueryExpression := queryExpression.AllQuery_expression(); len(allQueryExpression) > 1 {- recursiveTableSource, err := q.extractTSqlSensitiveFieldsFromQueryExpression(allQueryExpression[len(allQueryExpression)-1])- if err != nil {- return nil, errors.Wrapf(err, "failed to extract sensitive fields of the recursive clause of recursive CTE %q near line %d", normalizedCTEName, allQueryExpression[len(allQueryExpression)-1].GetStart().GetLine())- }- recursiveTableSourceQuerySpanResult := recursiveTableSource.GetQuerySpanResult()- if len(recursiveTableSourceQuerySpanResult) != len(tempCte.GetQuerySpanResult()) {- return nil, errors.Wrapf(err, "recursive clause returns %d fields, but anchor clause returns %d fields in recursive CTE %q near line %d", len(recursiveTableSourceQuerySpanResult), len(tempCte.GetQuerySpanResult()), normalizedCTEName, allQueryExpression[len(allQueryExpression)-1].GetStart().GetLine())- }- for i := 0; i < len(recursiveTableSourceQuerySpanResult); i++ {- var anyChange bool- tempCte.Columns[i].SourceColumns, change = base.MergeSourceColumnSet(tempCte.Columns[i].SourceColumns, recursiveTableSourceQuerySpanResult[i].SourceColumns)- change = change || anyChange- }- }- if !change {- break- }- }- q.ctes = q.ctes[:originalSize]- columns = tempCte.Columns- }- if v := commonTableExpression.Column_name_list(); v != nil {- if len(columns) != len(v.AllId_()) {- return nil, errors.Errorf("the number of column name list %d does not match the number of columns %d", len(v.AllId_()), len(columns))- }- for i, columnName := range v.AllId_() {- _, normalizedColumnName := NormalizeTSQLIdentifier(columnName)- columns[i].Name = normalizedColumnName- }- }- // Append to the extractor.schemaInfo.DatabaseList- q.ctes = append(q.ctes, &base.PseudoTable{- Name: normalizedCTEName,- Columns: columns,- })- }- }-- return q.extractTSqlSensitiveFieldsFromSelectStatement(ctx.Select_statement())-}--// extractTSqlSensitiveFieldsFromSelectStatement extracts sensitive fields from select_statement.-func (q *querySpanExtractor) extractTSqlSensitiveFieldsFromSelectStatement(ctx parser.ISelect_statementContext) (*base.PseudoTable, error) {- if ctx == nil {- return nil, nil- }-- queryResult, err := q.extractTSqlSensitiveFieldsFromQueryExpression(ctx.Query_expression())- if err != nil {- return nil, errors.Wrapf(err, "failed to extract sensitive fields from `query_expression` in `select_statement`")- }-- return queryResult, nil-}--func (q *querySpanExtractor) extractTSqlSensitiveFieldsFromQueryExpression(ctx parser.IQuery_expressionContext) (*base.PseudoTable, error) {- if ctx == nil {- return nil, nil- }-- if ctx.Query_specification() != nil {- anchor, err := q.extractTSqlSensitiveFieldsFromQuerySpecification(ctx.Query_specification())- if err != nil {- return nil, errors.Wrapf(err, "failed to extract sensitive fields from `query_specification` in `query_expression`")- }- if allSQLUnions := ctx.AllSql_union(); len(allSQLUnions) > 0 {- for i, sqlUnion := range allSQLUnions {- // For UNION operator, the number of the columns in the result set is the same, and will use the left part's column name.- // So we only need to extract the sensitive fields of the right part.- right, err := q.extractTSqlSensitiveFieldsFromQuerySpecification(sqlUnion.Query_specification())- if err != nil {- return nil, errors.Wrapf(err, "failed to extract the %d set operator near line %d", i+1, sqlUnion.GetStart().GetLine())- }- querySpanResult, err := unionTableSources(anchor, right)- if err != nil {- return nil, errors.Wrapf(err, "failed to union the %d set operator near line %d", i+1, sqlUnion.GetStart().GetLine())- }- anchor.Columns = querySpanResult- }- }- return anchor, nil- }-- if allQueryExpressions := ctx.AllQuery_expression(); len(allQueryExpressions) > 0 {- anchor, err := q.extractTSqlSensitiveFieldsFromQueryExpression(allQueryExpressions[0])- if err != nil {- return nil, errors.Wrapf(err, "failed to extract sensitive fields from `query_specification` in `query_expression`")- }- for i := 1; i < len(allQueryExpressions); i++ {- // For UNION operator, the number of the columns in the result set is the same, and will use the left part's column name.- // So we only need to extract the sensitive fields of the right part.- right, err := q.extractTSqlSensitiveFieldsFromQueryExpression(allQueryExpressions[i])- if err != nil {- return nil, errors.Wrapf(err, "failed to extract the %d set operator near line %d", i+1, allQueryExpressions[i].GetStart().GetLine())- }- querySpanResult, err := unionTableSources(anchor, right)- if err != nil {- return nil, errors.Wrapf(err, "failed to union the %d set operator near line %d", i+1, allQueryExpressions[i].GetStart().GetLine())- }- anchor.Columns = querySpanResult- }- return anchor, nil- }-- panic("never reach here")-}--func (q *querySpanExtractor) extractTSqlSensitiveFieldsFromQuerySpecification(ctx parser.IQuery_specificationContext) (*base.PseudoTable, error) {- if ctx == nil {- return nil, nil- }-- var compoundFrom []base.TableSource- if from := ctx.From_table_sources(); from != nil {- // Capture the boundary BEFORE extracting table sources: the extraction- // pushes intermediate join anchors onto q.tableSourcesFrom (see- // extractTSqlSensitiveFieldsFromTableSource), and those pushes must be- // scoped to this query specification so they don't leak into the- // enclosing scope — especially when alias names collide.- originalFromFieldList := len(q.tableSourcesFrom)- defer func() {- q.tableSourcesFrom = q.tableSourcesFrom[:originalFromFieldList]- }()- fromFieldList, err := q.extractTSqlSensitiveFieldsFromTableSources(from.GetFrom())- if err != nil {- return nil, errors.Wrapf(err, "failed to extract sensitive fields from `table_sources` in `query_specification`")- }- q.tableSourcesFrom = append(q.tableSourcesFrom, fromFieldList...)- compoundFrom = fromFieldList- }-- result := &base.PseudoTable{}-- selectList := ctx.Select_list()- for _, selectListElem := range selectList.AllSelect_list_elem() {- if asterisk := selectListElem.Asterisk(); asterisk != nil {- var normalizedDatabaseName, normalizedSchemaName, normalizedTableName string- if tableName := asterisk.Table_name(); tableName != nil {- normalizedDatabaseName, normalizedSchemaName, normalizedTableName = splitTableNameIntoNormalizedParts(tableName)- }- if normalizedDatabaseName == "" && normalizedSchemaName == "" && normalizedTableName == "" {- // SELECT * FROM ...- for _, tableSource := range compoundFrom {- result.Columns = append(result.Columns, tableSource.GetQuerySpanResult()...)- }- } else {- left, err := q.tsqlGetAllFieldsOfTableInFromOrOuterCTE(normalizedDatabaseName, normalizedSchemaName, normalizedTableName)- if err != nil {- return nil, errors.Wrapf(err, "failed to get all fields of table %s.%s.%s", normalizedDatabaseName, normalizedSchemaName, normalizedTableName)- }- result.Columns = append(result.Columns, left...)- }- } else if selectListElem.Udt_elem() != nil {- // TODO(zp): handle the UDT.- result.Columns = append(result.Columns, base.QuerySpanResult{- Name: fmt.Sprintf("UNSUPPORTED UDT %s", selectListElem.GetText()),- SourceColumns: make(base.SourceColumnSet, 0),- })- } else if selectListElem.LOCAL_ID() != nil {- // TODO(zp): handle the local variable, SELECT @a=id FROM blog.dbo.t1;- result.Columns = append(result.Columns, base.QuerySpanResult{- Name: fmt.Sprintf("UNSUPPORTED LOCALID %s", selectListElem.GetText()),- SourceColumns: make(base.SourceColumnSet, 0),- })- } else if expressionElem := selectListElem.Expression_elem(); expressionElem != nil {- querySpanResult, err := q.getQuerySpanResultFromExpr(expressionElem)- if err != nil {- return nil, errors.Wrapf(err, "failed to check if the expression element is sensitive")- }- result.Columns = append(result.Columns, querySpanResult)- }- }-- if where := ctx.Search_condition(); where != nil {- if err := q.extractPredicateColumnFromSearchCondition(where); err != nil {- return nil, errors.Wrapf(err, "failed to extract predicate columns from search condition")- }- }-- return result, nil-}--func (q *querySpanExtractor) extractTSqlSensitiveFieldsFromTableSources(ctx parser.ITable_sourcesContext) ([]base.TableSource, error) {- if ctx == nil {- return nil, nil- }-- allTableSources := ctx.GetSource()-- var result []base.TableSource- // If there are multiple table sources, the default join type is CROSS JOIN.- for _, tableSource := range allTableSources {- ts, err := q.extractTSqlSensitiveFieldsFromTableSource(tableSource)- if err != nil {- return nil, errors.Wrapf(err, "failed to extract sensitive fields from `table_source` in `table_sources`")- }- result = append(result, ts)- }- return result, nil-}--func (q *querySpanExtractor) extractTSqlSensitiveFieldsFromTableSource(ctx parser.ITable_sourceContext) (base.TableSource, error) {- if ctx == nil {- return nil, nil- }-- var columns []base.QuerySpanResult- anchor, err := q.extractTSqlSensitiveFieldsFromTableSourceItem(ctx.Table_source_item())- if err != nil {- return nil, errors.Wrapf(err, "failed to extract sensitive fields from `table_source_item` in `table_source`")- }- q.tableSourcesFrom = append(q.tableSourcesFrom, anchor)- name := anchor.GetTableName()- columns = append(columns, anchor.GetQuerySpanResult()...)-- if allJoinParts := ctx.AllJoin_part(); len(allJoinParts) > 0 {- name = ""- // https://learn.microsoft.com/en-us/sql/relational-databases/performance/joins?view=sql-server-ver16- for _, joinPart := range allJoinParts {- var joinTarget base.TableSource- if joinOn := joinPart.Join_on(); joinOn != nil {- right, err := q.extractTSqlSensitiveFieldsFromTableSource(joinOn.Table_source())- if err != nil {- return nil, errors.Wrapf(err, "failed to extract sensitive fields from `table_source` in `join_on`")- }- columns = append(columns, right.GetQuerySpanResult()...)- joinTarget = right- }- if crossJoin := joinPart.Cross_join(); crossJoin != nil {- right, err := q.extractTSqlSensitiveFieldsFromTableSourceItem(crossJoin.Table_source_item())- if err != nil {- return nil, errors.Wrapf(err, "failed to extract sensitive fields from `table_source` in `cross_join`")- }- columns = append(columns, right.GetQuerySpanResult()...)- joinTarget = right- }- if apply := joinPart.Apply_(); apply != nil {- right, err := q.extractTSqlSensitiveFieldsFromTableSourceItem(apply.Table_source_item())- if err != nil {- return nil, errors.Wrapf(err, "failed to extract sensitive fields from `table_source` in `apply`")- }- columns = append(columns, right.GetQuerySpanResult()...)- joinTarget = right- }- // TODO(zp): handle pivot and unpivot.- if pivot := joinPart.Pivot(); pivot != nil {- return nil, errors.New("pivot is not supported yet")- }- if unpivot := joinPart.Unpivot(); unpivot != nil {- return nil, errors.New("unpivot is not supported yet")- }- if joinTarget != nil {- q.tableSourcesFrom = append(q.tableSourcesFrom, joinTarget)- }- }- }-- return &base.PseudoTable{- Name: name,- Columns: columns,- }, nil-}--// extractTSqlSensitiveFieldsFromTableSourceItem extracts sensitive fields from table source item.-func (q *querySpanExtractor) extractTSqlSensitiveFieldsFromTableSourceItem(ctx parser.ITable_source_itemContext) (base.TableSource, error) {- if ctx == nil {- return nil, nil- }-- var result base.TableSource- var err error- // TODO(zp): handle other cases likes ROWSET_FUNCTION.- if ctx.Full_table_name() != nil {- result, err = q.tsqlFindTableSchema(ctx.Full_table_name())- if err != nil {- return nil, errors.Wrapf(err, "failed to find table schema for full table name")- }- } else if ctx.Derived_table() != nil {- result, err = q.extractTSqlSensitiveFieldsFromDerivedTable(ctx.Derived_table())- if err != nil {- return nil, errors.Wrapf(err, "failed to extract table source from derived table")- }- } else if ctx.Table_source() != nil {- return q.extractTSqlSensitiveFieldsFromTableSource(ctx.Table_source())- } else if ctx.LOCAL_ID() != nil {- tableName := ctx.LOCAL_ID().GetText()- if tempTable, ok := q.gCtx.TempTables[tableName]; ok {- return tempTable, nil- }- return nil, &base.ResourceNotFoundError{- Table: &tableName,- Err: errors.Errorf("temp table %s not found", tableName),- }- } else {- return nil, &base.TypeNotSupportedError{- Err: errors.Errorf("only full table name in table source item is supported"),- Type: fmt.Sprintf("%v", ctx),- }- }-- // If there are as_table_alias, we should patch the table name to the alias name, and reset the schema and database.- // For example:- // SELECT t1.id FROM blog.dbo.t1 AS TT1; -- The multi-part identifier "t1.id" could not be bound.- // SELECT TT1.id FROM blog.dbo.t1 AS TT1; -- OK- if asTableAlias := ctx.As_table_alias(); asTableAlias != nil {- _, asName := NormalizeTSQLIdentifier(asTableAlias.Table_alias().Id_())- result = &base.PseudoTable{- Name: asName,- Columns: result.GetQuerySpanResult(),- }- }-- if columnAliasList := ctx.Column_alias_list(); columnAliasList != nil {- allColumnAlias := columnAliasList.AllColumn_alias()- if len(allColumnAlias) != len(result.GetQuerySpanResult()) {- return nil, errors.Errorf("the number of column alias %d does not match the number of columns %d", len(allColumnAlias), len(result.GetQuerySpanResult()))- }- for i := 0; i < len(allColumnAlias); i++ {- if allColumnAlias[i].Id_() != nil {- _, name := NormalizeTSQLIdentifier(allColumnAlias[i].Id_())- result = &base.PseudoTable{- Name: result.GetTableName(),- Columns: result.GetQuerySpanResult(),- }- result.GetQuerySpanResult()[i].Name = name- continue- } else if allColumnAlias[i].STRING() != nil {- name := allColumnAlias[i].STRING().GetText()- result = &base.PseudoTable{- Name: result.GetTableName(),- Columns: result.GetQuerySpanResult(),- }- result.GetQuerySpanResult()[i].Name = name- continue- }- panic("never reach here")- }- }-- return result, nil-}--func (q *querySpanExtractor) extractTSqlSensitiveFieldsFromDerivedTable(ctx parser.IDerived_tableContext) (base.TableSource, error) {- if ctx == nil {- return nil, nil- }-- allSubquery := ctx.AllSubquery()- if len(allSubquery) > 0 {- anchor, err := q.extractTSqlSensitiveFieldsFromSubquery(allSubquery[0])- if err != nil {- return nil, errors.Wrapf(err, "failed to extract sensitive fields from `subquery` in `derived_table`")- }- for i := 1; i < len(allSubquery); i++ {- // For UNION operator, the number of the columns in the result set is the same, and will use the left part's column name.- // So we only need to extract the sensitive fields of the right part.- right, err := q.extractTSqlSensitiveFieldsFromSubquery(allSubquery[i])- rightQuerySpanResult := right.GetQuerySpanResult()- anchorQuerySpanResult := anchor.GetQuerySpanResult()- if err != nil {- return nil, errors.Wrapf(err, "failed to extract the %d set operator near line %d", i+1, allSubquery[i].GetStart().GetLine())- }- if len(anchorQuerySpanResult) != len(rightQuerySpanResult) {- return nil, errors.Wrapf(err, "the number of columns in the derived table statement nearly line %d returns %d fields, but %d set operator near line %d returns %d fields", ctx.GetStart().GetLine(), len(anchorQuerySpanResult), i+1, allSubquery[i].GetStart().GetLine(), len(rightQuerySpanResult))- }- for i := range rightQuerySpanResult {- anchorQuerySpanResult[i].SourceColumns, _ = base.MergeSourceColumnSet(anchorQuerySpanResult[i].SourceColumns, rightQuerySpanResult[i].SourceColumns)- }- }- return anchor, nil- }-- if tableValueConstructor := ctx.Table_value_constructor(); tableValueConstructor != nil {- return q.extractTSqlSensitiveFieldsFromTableValueConstructor(tableValueConstructor)- }-- panic("never reach here")-}--func (q *querySpanExtractor) extractTSqlSensitiveFieldsFromTableValueConstructor(ctx parser.ITable_value_constructorContext) (base.TableSource, error) {- if allExpressionList := ctx.AllExpression_list_(); len(allExpressionList) > 0 {- // The number of expression in each expression list should be the same.- // But we do not check, just use the first one, and engine will throw a compilation error if the number of expressions are not the same.- expressionList := allExpressionList[0]- pseudoTable := &base.PseudoTable{- Columns: make([]base.QuerySpanResult, 0, len(expressionList.AllExpression())),- }- for _, expression := range expressionList.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return nil, errors.Wrapf(err, "failed to extract sensitive fields from `expression` in `table_value_constructor`")- }- pseudoTable.Columns = append(pseudoTable.Columns, querySpanResult)- }- return pseudoTable, nil- }- panic("never reach here")-}--func (q *querySpanExtractor) extractTSqlSensitiveFieldsFromSubquery(ctx parser.ISubqueryContext) (*base.PseudoTable, error) {- return q.extractTSqlSensitiveFieldsFromSelectStatement(ctx.Select_statement())-}--func (q *querySpanExtractor) tsqlFindTableSchema(fullTableName parser.IFull_table_nameContext) (base.TableSource, error) {- normalizedLinkedServer, normalizedDatabaseName, normalizedSchemaName, normalizedTableName := normalizeFullTableNameFallback(fullTableName, "", "")- if normalizedLinkedServer != "" {+// tsqlFindTableSchemaByParts resolves a (linkedServer, database, schema, table)+// tuple to a TableSource. Empty strings for database/schema mean "not specified+// by the user"; defaults are applied here when looking up metadata. CTEs+// registered in q.ctes shadow physical tables when no database/schema was+// specified.+func (q *querySpanExtractor) tsqlFindTableSchemaByParts(linkedServer, rawDatabase, rawSchema, rawTable string) (base.TableSource, error) {+ if linkedServer != "" {// TODO(zp): How do we handle the linked server?- return nil, errors.Errorf("linked server is not supported yet, but found %q", fullTableName.GetText())+ return nil, errors.Errorf("linked server is not supported yet, but found %q", linkedServer)}- if strings.HasPrefix(normalizedTableName, "#") {+ if strings.HasPrefix(rawTable, "#") {// 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- // to find the nearest match.- if normalizedDatabaseName == "" && normalizedSchemaName == "" {+ // SQL Server: CTEs shadow physical tables — check first, nearest match wins.+ if rawDatabase == "" && rawSchema == "" {for _, cte := range q.ctes {- if q.isIdentifierEqual(normalizedTableName, cte.Name) {+ if q.isIdentifierEqual(rawTable, cte.Name) {return cte, nil}}}- normalizedLinkedServer, normalizedDatabaseName, normalizedSchemaName, normalizedTableName = normalizeFullTableNameFallback(fullTableName, q.defaultDatabase, q.defaultSchema)- if normalizedLinkedServer != "" {- // TODO(zp): How do we handle the linked server?- return nil, errors.Errorf("linked server is not supported yet, but found %q", fullTableName.GetText())+ database := q.defaultDatabase+ if rawDatabase != "" {+ database = rawDatabase}+ schema := q.defaultSchema+ if rawSchema != "" {+ schema = rawSchema+ }+allDatabases, err := q.gCtx.ListDatabaseNamesFunc(q.ctx, q.gCtx.InstanceID)if err != nil {return nil, errors.Errorf("failed to list databases: %v", err)}for _, databaseName := range allDatabases {- if normalizedDatabaseName != "" && !q.isIdentifierEqual(normalizedDatabaseName, databaseName) {+ if database != "" && !q.isIdentifierEqual(database, databaseName) {continue}- _, database, err := q.gCtx.GetDatabaseMetadataFunc(q.ctx, q.gCtx.InstanceID, databaseName)+ _, databaseMeta, err := q.gCtx.GetDatabaseMetadataFunc(q.ctx, q.gCtx.InstanceID, databaseName)if err != nil {return nil, errors.Wrapf(err, "failed to get database %s metadata", databaseName)}- allSchemaNames := database.ListSchemaNames()- for _, schemaName := range allSchemaNames {- if normalizedSchemaName != "" && !q.isIdentifierEqual(normalizedSchemaName, schemaName) {+ for _, schemaName := range databaseMeta.ListSchemaNames() {+ if schema != "" && !q.isIdentifierEqual(schema, schemaName) {continue}- schemaSchema := database.GetSchemaMetadata(schemaName)- allTableNames := schemaSchema.ListTableNames()- for _, tableName := range allTableNames {- if !q.isIdentifierEqual(normalizedTableName, tableName) {+ schemaSchema := databaseMeta.GetSchemaMetadata(schemaName)+ for _, tableName := range schemaSchema.ListTableNames() {+ if !q.isIdentifierEqual(rawTable, tableName) {continue}table := schemaSchema.GetTable(tableName)- physicalTableSource := &base.PhysicalTable{- Server: "",+ columns := make([]string, 0, len(table.GetProto().GetColumns()))+ for _, c := range table.GetProto().GetColumns() {+ columns = append(columns, c.Name)+ }+ return &base.PhysicalTable{Database: databaseName,Schema: schemaName,Name: table.GetProto().Name,- Columns: func() []string {- var result []string- for _, column := range table.GetProto().GetColumns() {- result = append(result, column.Name)- }- return result- }(),- }- return physicalTableSource, nil+ Columns: columns,+ }, nil}- // search views- allViewNames := schemaSchema.ListViewNames()- for _, viewName := range allViewNames {- if !q.isIdentifierEqual(normalizedTableName, viewName) {+ for _, viewName := range schemaSchema.ListViewNames() {+ if !q.isIdentifierEqual(rawTable, viewName) {continue}view := schemaSchema.GetView(viewName)- columns, err := q.getColumnsFromCreateView(view.Definition, databaseName, schemaName)+ viewColumns, err := q.getColumnsFromCreateView(view.Definition, databaseName, schemaName)if err != nil {return nil, errors.Wrapf(err, "failed to get columns for view %s.%s.%s", databaseName, schemaName, viewName)}- tableSource := &base.PhysicalView{- Server: "",+ return &base.PhysicalView{Database: databaseName,Schema: schemaName,Name: view.Name,- Columns: columns,- }- return tableSource, nil+ Columns: viewColumns,+ }, nil}}}return nil, &base.ResourceNotFoundError{- Database: &normalizedDatabaseName,- Schema: &normalizedSchemaName,- Table: &normalizedTableName,+ Database: &database,+ Schema: &schema,+ Table: &rawTable,}}+// getColumnsFromCreateView parses a CREATE VIEW definition with omni, extracts+// the body's result columns, and applies the optional column alias list.func (q *querySpanExtractor) getColumnsFromCreateView(definition string, viewDatabaseName string, viewSchemaName string) ([]base.QuerySpanResult, error) {- // Extract the SELECT body and optional column alias list from CREATE VIEW statement.- // e.g. CREATE VIEW vw (F1, F2) AS SELECT col_a, col_b FROM tbl- // selectBody = "SELECT col_a, col_b FROM tbl"- // columnAliases = ["F1", "F2"]- selectBody, columnAliases, err := getSelectBodyFromCreateView(definition)+ stmts, err := ParseTSQLOmni(definition)if err != nil {- return nil, errors.Wrapf(err, "failed to extract SELECT body from CREATE VIEW")+ return nil, errors.Wrapf(err, "failed to parse CREATE VIEW definition")+ }+ var createView *ast.CreateViewStmt+ for _, s := range stmts {+ if cv, ok := s.AST.(*ast.CreateViewStmt); ok {+ createView = cv+ break+ }+ }+ if createView == nil {+ return nil, errors.Errorf("no CREATE VIEW statement found in definition")}- if selectBody == "" {- return nil, errors.Errorf("no SELECT body found in CREATE VIEW statement")+ body, ok := createView.Query.(*ast.SelectStmt)+ if !ok || body == nil {+ return nil, errors.Errorf("CREATE VIEW body is not a SELECT")+ }+ var columnAliases []string+ if createView.Columns != nil {+ for _, it := range createView.Columns.Items {+ if s, ok := it.(*ast.String); ok {+ columnAliases = append(columnAliases, s.Str)+ }+ }}- // Use the view's database and schema as the default context for parsing the SELECT statement- // This ensures that cross-database references in the view definition are resolved correctly- newQ := newQuerySpanExtractor(viewDatabaseName, viewSchemaName, q.gCtx, q.ignoreCaseSensitive)- span, err := newQ.getQuerySpan(q.ctx, selectBody)+ newQ := newOmniQuerySpanExtractor(viewDatabaseName, viewSchemaName, q.gCtx, q.ignoreCaseSensitive)+ newQ.ctx = q.ctx+ newQ.source = definition+ pseudo, err := newQ.extractFromSelectStmt(body)if err != nil {- return nil, errors.Wrapf(err, "failed to get query span for view definition")- }- if span.NotFoundError != nil {- return nil, span.NotFoundError+ var resourceNotFound *base.ResourceNotFoundError+ if errors.As(err, &resourceNotFound) {+ return nil, resourceNotFound+ }+ return nil, errors.Wrapf(err, "failed to extract view body")}+ results := pseudo.GetQuerySpanResult()- // Apply column aliases from the CREATE VIEW header if present.- // The alias list renames the SELECT body columns positionally.- if len(columnAliases) > 0 && len(columnAliases) == len(span.Results) {+ if len(columnAliases) > 0 && len(columnAliases) == len(results) {for i, alias := range columnAliases {- span.Results[i].Name = alias+ results[i].Name = normIdent(alias)}}-- return span.Results, nil+ return results, nil}+// tsqlGetAllFieldsOfTableInFromOrOuterCTE expands `table.*` to the column set+// of the matching table source in scope.func (q *querySpanExtractor) tsqlGetAllFieldsOfTableInFromOrOuterCTE(normalizedDatabaseName, normalizedSchemaName, normalizedTableName string) ([]base.QuerySpanResult, error) {type maskType = uint8const (@@ -837,20 +237,12 @@ func (q *querySpanExtractor) tsqlGetAllFieldsOfTableInFromOrOuterCTE(normalizedD}return tableSource.GetQuerySpanResult(), nil}-return nil, errors.Errorf(`no matching table %q.%q.%q`, normalizedDatabaseName, normalizedSchemaName, normalizedTableName)}-func (q *querySpanExtractor) tsqlIsFullColumnNameSensitive(ctx parser.IFull_column_nameContext) (base.QuerySpanResult, error) {- normalizedLinkedServer, normalizedDatabaseName, normalizedSchemaName, normalizedTableName := normalizeFullTableNameFallback(ctx.Full_table_name(), "", "")- if normalizedLinkedServer != "" {- return base.QuerySpanResult{}, errors.Errorf("linked server is not supported yet, but found %q", ctx.GetText())- }- _, normalizedColumnName := NormalizeTSQLIdentifier(ctx.Id_())-- return q.tsqlIsFieldSensitive(normalizedDatabaseName, normalizedSchemaName, normalizedTableName, normalizedColumnName)-}-+// tsqlIsFieldSensitive resolves a (database, schema, table, column) tuple to a+// QuerySpanResult by scanning tableSourcesFrom first, then outerTableSources+// (nearest-first) for correlated-subquery resolution.func (q *querySpanExtractor) tsqlIsFieldSensitive(normalizedDatabaseName string, normalizedSchemaName string, normalizedTableName string, normalizedColumnName string) (base.QuerySpanResult, error) {type maskType = uint8const (@@ -888,18 +280,16 @@ func (q *querySpanExtractor) tsqlIsFieldSensitive(normalizedDatabaseName string,}// We just need to iterate through the fromFieldList sequentially until we find the first matching object.-+ //// It is safe if there are two or more objects in the fromFieldList have the same column name, because the executor// will throw a compilation error if the column name is ambiguous.// For example, there are two tables T1 and T2, and both of them have a column named "C1". The following query will throw// a compilation error:- // SELECT C1 FROM T1, T2;- //- // But users can specify the table name to avoid the compilation error:- // SELECT T1.C1 FROM T1, T2;- //- // Further more, users can not use the original table name if they specify the alias name:- // SELECT T1.C1 FROM T1 AS T3, T2; -- invalid identifier 'ADDRESS.ID'+ // SELECT C1 FROM T1, T2;+ // Users can specify the table name to disambiguate:+ // SELECT T1.C1 FROM T1, T2;+ // If an alias is set the original table name is shadowed:+ // SELECT T1.C1 FROM T1 AS T3, T2; -- invalidfor _, tableSource := range q.tableSourcesFrom {if mask&maskDatabaseName != 0 && !q.isIdentifierEqual(normalizedDatabaseName, tableSource.GetDatabaseName()) {continue@@ -939,8 +329,7 @@ func (q *querySpanExtractor) tsqlIsFieldSensitive(normalizedDatabaseName string,return base.QuerySpanResult{}, errors.Errorf(`no matching column %q.%q.%q.%q`, normalizedDatabaseName, normalizedSchemaName, normalizedTableName, normalizedColumnName)}-// isIdentifierEqual compares the identifier with the given normalized parts, returns true if they are equal.-// It will consider the case sensitivity based on the current database.+// isIdentifierEqual compares identifiers using the extractor's case sensitivity flag.func (q *querySpanExtractor) isIdentifierEqual(a, b string) bool {if !q.ignoreCaseSensitive {return a == b@@ -957,2651 +346,40 @@ func (q *querySpanExtractor) isIdentifierEqual(a, b string) bool {return true}-// getQuerySpanResultFromExpr returns true if the expression element is sensitive, and returns the column name.-// It is the closure of the expression_elemContext, it will recursively check the sub expression element.-func (q *querySpanExtractor) getQuerySpanResultFromExpr(ctx antlr.RuleContext) (base.QuerySpanResult, error) {- if ctx == nil {- return base.QuerySpanResult{- Name: "",- SourceColumns: make(base.SourceColumnSet, 0),- }, nil+// unionTableSources unions two or more table sources column-wise.+func unionTableSources(tableSources ...base.TableSource) ([]base.QuerySpanResult, error) {+ if len(tableSources) == 0 {+ return nil, errors.New("no table source to union")}- switch ctx := ctx.(type) {- case *parser.Expression_elemContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the expression element is sensitive")- }- if columnAlias := ctx.Column_alias(); columnAlias != nil {- _, querySpanResult.Name = NormalizeTSQLIdentifier(columnAlias.Id_())- } else if asColumnAlias := ctx.As_column_alias(); asColumnAlias != nil {- _, querySpanResult.Name = NormalizeTSQLIdentifier(asColumnAlias.Column_alias().Id_())- }- return querySpanResult, nil- case *parser.ExpressionContext:- if ctx.Primitive_expression() != nil {- return q.getQuerySpanResultFromExpr(ctx.Primitive_expression())- }- if ctx.Function_call() != nil {- return q.getQuerySpanResultFromExpr(ctx.Function_call())- }- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- if allExpressions := ctx.AllExpression(); len(allExpressions) > 0 {- for _, expression := range allExpressions {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the expression is sensitive")- }- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- }- }- if valueCall := ctx.Value_call(); valueCall != nil {- return q.getQuerySpanResultFromExpr(valueCall)- }- if queryCall := ctx.Query_call(); queryCall != nil {- return q.getQuerySpanResultFromExpr(queryCall)++ anchor := tableSources[0].GetQuerySpanResult()+ for i := 1; i < len(tableSources); i++ {+ current := tableSources[i].GetQuerySpanResult()+ if len(current) != len(anchor) {+ return nil, errors.Errorf("the %dth table source has different column number with previous anchors, previous: %d, current %d", i+1, len(anchor), len(current))}- if existCall := ctx.Exist_call(); existCall != nil {- return q.getQuerySpanResultFromExpr(existCall)+ for j := range anchor {+ anchor[j].SourceColumns, _ = base.MergeSourceColumnSet(anchor[j].SourceColumns, current[j].SourceColumns)}- if modifyCall := ctx.Modify_call(); modifyCall != nil {- return q.getQuerySpanResultFromExpr(modifyCall)+ }+ return anchor, nil+}++// isMixedQuery reports (allSystems, mixed): mixed=true means the query touches+// both user tables and system tables simultaneously, which we treat as an+// error. allSystems=true means every accessed table is a system table.+func isMixedQuery(m base.SourceColumnSet, ignoreCaseSensitive bool) (bool, bool) {+ hasSystem, hasUser := false, false+ for table := range m {+ if isSystemResource(table, ignoreCaseSensitive) {+ hasSystem = true+ } else {+ hasUser = true}- if hierarchyIDCall := ctx.Hierarchyid_call(); hierarchyIDCall != nil {- return q.getQuerySpanResultFromExpr(hierarchyIDCall)- }- if caseExpression := ctx.Case_expression(); caseExpression != nil {- return q.getQuerySpanResultFromExpr(caseExpression)- }- if fullColumnName := ctx.Full_column_name(); fullColumnName != nil {- return q.getQuerySpanResultFromExpr(fullColumnName)- }- if bracketExpression := ctx.Bracket_expression(); bracketExpression != nil {- return q.getQuerySpanResultFromExpr(bracketExpression)- }- if unaryOperationExpression := ctx.Unary_operator_expression(); unaryOperationExpression != nil {- return q.getQuerySpanResultFromExpr(unaryOperationExpression)- }- if overClause := ctx.Over_clause(); overClause != nil {- return q.getQuerySpanResultFromExpr(overClause)- }- return anchor, nil- case *parser.Unary_operator_expressionContext:- if expression := ctx.Expression(); expression != nil {- return q.getQuerySpanResultFromExpr(expression)- }- return base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }, nil- case *parser.Bracket_expressionContext:- if expression := ctx.Expression(); expression != nil {- return q.getQuerySpanResultFromExpr(expression)- }- if subquery := ctx.Subquery(); subquery != nil {- return q.getQuerySpanResultFromExpr(subquery)- }- return base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }, nil- case *parser.Case_expressionContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- if allExpressions := ctx.AllExpression(); len(allExpressions) > 0 {- for _, expression := range allExpressions {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the case_expression is sensitive")- }- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- }- }- if allSwitchSections := ctx.AllSwitch_section(); len(allSwitchSections) > 0 {- for _, switchSection := range allSwitchSections {- querySpanExtractor, err := q.getQuerySpanResultFromExpr(switchSection)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the case_expression is sensitive")- }- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanExtractor.SourceColumns)- }- }- if allSwitchSearchConditionSections := ctx.AllSwitch_search_condition_section(); len(allSwitchSearchConditionSections) > 0 {- for _, switchSearchConditionSection := range allSwitchSearchConditionSections {- querySpanExtractor, err := q.getQuerySpanResultFromExpr(switchSearchConditionSection)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the case_expression is sensitive")- }- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanExtractor.SourceColumns)- }- }- return anchor, nil- case *parser.Switch_sectionContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- if allExpressions := ctx.AllExpression(); len(allExpressions) > 0 {- for _, expression := range allExpressions {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the switch_setion is sensitive")- }- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- }- }- return anchor, nil- case *parser.Switch_search_condition_sectionContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- if searchCondition := ctx.Search_condition(); searchCondition != nil {- querySpanResult, err := q.getQuerySpanResultFromExpr(searchCondition)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the switch_search_condition_section is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- if expression := ctx.Expression(); expression != nil {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the switch_search_condition_section is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.Search_conditionContext:- if predicate := ctx.Predicate(); predicate != nil {- return q.getQuerySpanResultFromExpr(predicate)- }- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- if allSearchConditions := ctx.AllSearch_condition(); len(allSearchConditions) > 0 {- for _, searchCondition := range allSearchConditions {- querySpanResult, err := q.getQuerySpanResultFromExpr(searchCondition)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the search_condition is sensitive")- }- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- }- }- return anchor, nil- case *parser.PredicateContext:- if subquery := ctx.Subquery(); subquery != nil {- return q.getQuerySpanResultFromExpr(subquery)- }- if freeTextPredicate := ctx.Freetext_predicate(); freeTextPredicate != nil {- return q.getQuerySpanResultFromExpr(freeTextPredicate)- }-- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- if allExpressions := ctx.AllExpression(); len(allExpressions) > 0 {- for _, expression := range allExpressions {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the predicate is sensitive")- }- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- }- }- if expressionList := ctx.Expression_list_(); expressionList != nil {- querySpanResult, err := q.getQuerySpanResultFromExpr(expressionList)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the predicate is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.Freetext_predicateContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- if allExpressions := ctx.AllExpression(); len(allExpressions) > 0 {- for _, expression := range allExpressions {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the freetext_predicate is sensitive")- }- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- }- }- if allCullColumnName := ctx.AllFull_column_name(); len(allCullColumnName) > 0 {- for _, fullColumnName := range allCullColumnName {- querySpanResult, err := q.getQuerySpanResultFromExpr(fullColumnName)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the freetext_predicate is sensitive")- }- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- }- }- return anchor, nil- case *parser.SubqueryContext:- // For subquery, we clone the current extractor, reset the from list, but keep the cte, and then extract the sensitive fields from the subquery- cloneExtractor := &querySpanExtractor{- defaultDatabase: q.defaultDatabase,- defaultSchema: q.defaultSchema,- gCtx: q.gCtx,- ctx: q.ctx,- outerTableSources: append(q.outerTableSources, q.tableSourcesFrom...),- ctes: q.ctes,- ignoreCaseSensitive: q.ignoreCaseSensitive,- }- tableSource, err := cloneExtractor.extractTSqlSensitiveFieldsFromSubquery(ctx)- // The expect behavior is the fieldInfo contains only one field, which is the column name,- // but in order to do not block user, we just return isSensitive if there is any sensitive field.- // return fieldInfo[0].sensitive, err- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the subquery is sensitive")- }- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, field := range tableSource.GetQuerySpanResult() {- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, field.SourceColumns)- }- return anchor, nil- case *parser.Hierarchyid_callContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- if allExpressions := ctx.AllExpression(); len(allExpressions) > 0 {- for _, expression := range allExpressions {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the hierarchyid_call is sensitive")- }- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- }- }- return anchor, nil- case *parser.Query_callContext:- return base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }, nil- case *parser.Exist_callContext:- return base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }, nil- case *parser.Modify_callContext:- return base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }, nil- case *parser.Value_callContext:- return base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }, nil- case *parser.Primitive_expressionContext:- if ctx.Primitive_constant() != nil {- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Primitive_constant())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the primitive constant is sensitive")- }- return querySpanResult, nil- }- // Select local variable or null, we do not need to check the sensitive fields.- return base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }, nil- case *parser.Primitive_constantContext:- return base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }, nil- case *parser.Function_callContext:- // In parser.g4, the function_callContext is defined as:- // function_call- // : ranking_windowed_function #RANKING_WINDOWED_FUNC- // | aggregate_windowed_function #AGGREGATE_WINDOWED_FUNC- // ...- // ;- // So it will be parsed as RANKING_WINDOWED_FUNC, AGGREGATE_WINDOWED_FUNC, etc.- // We just need to check the first token to see if it is a sensitive function.- panic("never reach here")- case *parser.RANKING_WINDOWED_FUNCContext:- return q.getQuerySpanResultFromExpr(ctx.Ranking_windowed_function())- case *parser.Ranking_windowed_functionContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- if overClause := ctx.Over_clause(); overClause != nil {- querySpanResult, err := q.getQuerySpanResultFromExpr(overClause)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the ranking_windowed_function is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- if expression := ctx.Expression(); expression != nil {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the ranking_windowed_function is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.Over_clauseContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- if expressionList := ctx.Expression_list_(); expressionList != nil {- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression_list_())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the over_clause is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- if orderByClause := ctx.Order_by_clause(); orderByClause != nil {- querySpanResult, err := q.getQuerySpanResultFromExpr(orderByClause)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the over_clause is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- if rowOrRangeClause := ctx.Row_or_range_clause(); rowOrRangeClause != nil {- querySpanResult, err := q.getQuerySpanResultFromExpr(rowOrRangeClause)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the over_clause is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.Expression_list_Context:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the expression_list is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.Order_by_clauseContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, orderByExpression := range ctx.GetOrder_bys() {- querySpanResult, err := q.getQuerySpanResultFromExpr(orderByExpression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the order_by_clause is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.Order_by_expressionContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the order_by_expression is sensitive")- }- return querySpanResult, nil- case *parser.Row_or_range_clauseContext:- if windowFrameExtent := ctx.Window_frame_extent(); windowFrameExtent != nil {- querySpanResult, err := q.getQuerySpanResultFromExpr(windowFrameExtent)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the row_or_range_clause is sensitive")- }- return querySpanResult, nil- }- panic("never reach here")- case *parser.Window_frame_extentContext:- if windowFramePreceding := ctx.Window_frame_preceding(); windowFramePreceding != nil {- querySpanResult, err := q.getQuerySpanResultFromExpr(windowFramePreceding)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the window_frame_extent is sensitive")- }- return querySpanResult, nil- }- if windowFrameBounds := ctx.AllWindow_frame_bound(); len(windowFrameBounds) > 0 {- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, windowFrameBound := range windowFrameBounds {- querySpanResult, err := q.getQuerySpanResultFromExpr(windowFrameBound)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the window_frame_extent is sensitive")- }- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- }- return anchor, nil- }- panic("never reach here")- case *parser.Window_frame_boundContext:- if preceding := ctx.Window_frame_preceding(); preceding != nil {- querySpanResult, err := q.getQuerySpanResultFromExpr(preceding)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the window_frame_bound is sensitive")- }- return querySpanResult, nil- } else if following := ctx.Window_frame_following(); following != nil {- querySpanResult, err := q.getQuerySpanResultFromExpr(following)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the window_frame_bound is sensitive")- }- return querySpanResult, nil- }- panic("never reach here")- case *parser.Window_frame_precedingContext:- return base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }, nil- case *parser.Window_frame_followingContext:- return base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }, nil- case *parser.AGGREGATE_WINDOWED_FUNCContext:- return q.getQuerySpanResultFromExpr(ctx.Aggregate_windowed_function())- case *parser.Aggregate_windowed_functionContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- if allDistinctExpression := ctx.All_distinct_expression(); allDistinctExpression != nil {- querySpanResult, err := q.getQuerySpanResultFromExpr(allDistinctExpression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the aggregate_windowed_function is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- if overClause := ctx.Over_clause(); overClause != nil {- querySpanResult, err := q.getQuerySpanResultFromExpr(overClause)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the aggregate_windowed_function is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- if expression := ctx.Expression(); expression != nil {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the aggregate_windowed_function is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- if expressionList := ctx.Expression_list_(); expressionList != nil {- querySpanResult, err := q.getQuerySpanResultFromExpr(expressionList)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the aggregate_windowed_function is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.All_distinct_expressionContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the all_distinct_expression is sensitive")- }- return querySpanResult, nil- case *parser.ANALYTIC_WINDOWED_FUNCContext:- return q.getQuerySpanResultFromExpr(ctx.Analytic_windowed_function())- case *parser.Analytic_windowed_functionContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- if allExpressions := ctx.AllExpression(); len(allExpressions) > 0 {- for _, expression := range allExpressions {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the analytic_windowed_function is sensitive")- }- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- }- }- if overClause := ctx.Over_clause(); overClause != nil {- querySpanResult, err := q.getQuerySpanResultFromExpr(overClause)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the analytic_windowed_function is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- if expressionList := ctx.Expression_list_(); expressionList != nil {- querySpanResult, err := q.getQuerySpanResultFromExpr(expressionList)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the analytic_windowed_function is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- if orderByClause := ctx.Order_by_clause(); orderByClause != nil {- querySpanResult, err := q.getQuerySpanResultFromExpr(orderByClause)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the analytic_windowed_function is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.BUILT_IN_FUNCContext:- return q.getQuerySpanResultFromExpr(ctx.Built_in_functions())- case *parser.APP_NAMEContext:- return base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }, nil- case *parser.APPLOCK_MODEContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the applock_mode is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.APPLOCK_TESTContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the applock_test is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.ASSEMBLYPROPERTYContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the assemblyproperty is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.COL_LENGTHContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the col_length is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.COL_NAMEContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the col_name is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.COLUMNPROPERTYContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the columnproperty is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.DATABASEPROPERTYEXContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the databasepropertyex is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.DB_IDContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the db_id is sensitive")- }- return querySpanResult, nil- case *parser.DB_NAMEContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the db_name is sensitive")- }- return querySpanResult, nil- case *parser.FILE_IDContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the file_id is sensitive")- }- return querySpanResult, nil- case *parser.FILE_IDEXContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the file_idex is sensitive")- }- return querySpanResult, nil- case *parser.FILE_NAMEContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the file_name is sensitive")- }- return querySpanResult, nil- case *parser.FILEGROUP_IDContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the filegroup_id is sensitive")- }- return querySpanResult, nil- case *parser.FILEGROUP_NAMEContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the filegroup_name is sensitive")- }- return querySpanResult, nil- case *parser.FILEGROUPPROPERTYContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the filegroupproperty is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.FILEPROPERTYContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the fileproperty is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.FILEPROPERTYEXContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the filepropertyex is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.FULLTEXTCATALOGPROPERTYContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the fulltextcatalogproperty is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.FULLTEXTSERVICEPROPERTYContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the fulltextserviceproperty is sensitive")- }- return querySpanResult, nil- case *parser.INDEX_COLContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the index_col is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.INDEXKEY_PROPERTYContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the indexkey_property is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.INDEXPROPERTYContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the indexproperty is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.OBJECT_DEFINITIONContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the object_definition is sensitive")- }- return querySpanResult, nil- case *parser.OBJECT_IDContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the object_id is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.OBJECT_NAMEContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the object_name is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.OBJECT_SCHEMA_NAMEContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the object_schema_name is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.OBJECTPROPERTYContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the objectproperty is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.OBJECTPROPERTYEXContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the objectpropertyex is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.PARSENAMEContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the parsename is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.SCHEMA_IDContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the schema_id is sensitive")- }- return querySpanResult, nil- case *parser.SCHEMA_NAMEContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the schema_name is sensitive")- }- return querySpanResult, nil- case *parser.SERVERPROPERTYContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the serverproperty is sensitive")- }- return querySpanResult, nil- case *parser.STATS_DATEContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the stats_date is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.TYPE_IDContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the type_id is sensitive")- }- return querySpanResult, nil- case *parser.TYPE_NAMEContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the type_name is sensitive")- }- return querySpanResult, nil- case *parser.TYPEPROPERTYContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the typeproperty is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.ASCIIContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the ascii is sensitive")- }- return querySpanResult, nil- case *parser.CHARContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the char is sensitive")- }- return querySpanResult, nil- case *parser.CHARINDEXContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the charindex is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.CONCATContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the concat is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.CONCAT_WSContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the concat_ws is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.DIFFERENCEContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the difference is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.FORMATContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the format is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.LEFTContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the left is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.LENContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the len is sensitive")- }- return querySpanResult, nil- case *parser.LOWERContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the lower is sensitive")- }- return querySpanResult, nil- case *parser.LTRIMContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the ltrim is sensitive")- }- return querySpanResult, nil- case *parser.NCHARContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the nchar is sensitive")- }- return querySpanResult, nil- case *parser.PATINDEXContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the patindex is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.QUOTENAMEContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the quotename is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.REPLACEContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the replace is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.REPLICATEContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the replicate is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.REVERSEContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the reverse is sensitive")- }- return querySpanResult, nil- case *parser.RIGHTContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the right is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.RTRIMContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the rtrim is sensitive")- }- return querySpanResult, nil- case *parser.SOUNDEXContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the soundex is sensitive")- }- return querySpanResult, nil- case *parser.SPACEContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the space is sensitive")- }- return querySpanResult, nil- case *parser.STRContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the str is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.STRINGAGGContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the stringagg is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.STRING_ESCAPEContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the string_escape is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.STUFFContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the stuff is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.SUBSTRINGContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the substring is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.TRANSLATEContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the translate is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.TRIMContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the trim is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.UNICODEContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the unicode is sensitive")- }- return querySpanResult, nil- case *parser.UPPERContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the upper is sensitive")- }- return querySpanResult, nil- case *parser.BINARY_CHECKSUMContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the binary_checksum is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.CHECKSUMContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the checksum is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }, nil- case *parser.COMPRESSContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the compress is sensitive")- }- return querySpanResult, nil- case *parser.DECOMPRESSContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the decompress is sensitive")- }- return querySpanResult, nil- case *parser.FORMATMESSAGEContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the formatmessage is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.ISNULLContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the isnull is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.ISNUMERICContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the isnumeric is sensitive")- }- return querySpanResult, nil- case *parser.CASTContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the cast is sensitive")- }- return querySpanResult, nil- case *parser.TRY_CASTContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the try_cast is sensitive")- }- return querySpanResult, nil- case *parser.CONVERTContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the convert is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.COALESCEContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- if expressionList := ctx.Expression_list_(); expressionList != nil {- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression_list_())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the coalesce is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.CURSOR_STATUSContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the cursor_status is sensitive")- }- return querySpanResult, nil- case *parser.CERT_IDContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the cert_id is sensitive")- }- return querySpanResult, nil- case *parser.DATALENGTHContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the datalength is sensitive")- }- return querySpanResult, nil- case *parser.IDENT_CURRENTContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the ident_current is sensitive")- }- return querySpanResult, nil- case *parser.IDENT_INCRContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the ident_incr is sensitive")- }- return querySpanResult, nil- case *parser.IDENT_SEEDContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the ident_seed is sensitive")- }- return querySpanResult, nil- case *parser.SQL_VARIANT_PROPERTYContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the sql_variant_property is sensitive")- }- return querySpanResult, nil- case *parser.DATE_BUCKETContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the date_bucket is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.DATEADDContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the dateadd is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.DATEDIFFContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the datediff is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.DATEDIFF_BIGContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the datediff_big is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.DATEFROMPARTSContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the datefromparts is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.DATENAMEContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the datename is sensitive")- }- return querySpanResult, nil- case *parser.DATEPARTContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the datepart is sensitive")- }- return querySpanResult, nil- case *parser.DATETIME2FROMPARTSContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the datetime2fromparts is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.DATETIMEFROMPARTSContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the datetimefromparts is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.DATETIMEOFFSETFROMPARTSContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the datetimeoffsetfromparts is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.DATETRUNCContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the datetrunc is sensitive")- }- return querySpanResult, nil- case *parser.DAYContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the day is sensitive")- }- return querySpanResult, nil- case *parser.EOMONTHContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the eomonth is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.ISDATEContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the isdate is sensitive")- }- return querySpanResult, nil- case *parser.MONTHContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the month is sensitive")- }- return querySpanResult, nil- case *parser.SMALLDATETIMEFROMPARTSContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the smalldatetimefromparts is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.SWITCHOFFSETContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the switchoffset is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.TIMEFROMPARTSContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the timefromparts is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.TODATETIMEOFFSETContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the todatetimeoffset is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.YEARContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the year is sensitive")- }- return querySpanResult, nil- case *parser.NULLIFContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the nullif is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.PARSEContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the parse is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.IIFContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the iif is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.ISJSONContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the isjson is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.JSON_ARRAYContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- if expressionList := ctx.Expression_list_(); expressionList != nil {- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression_list_())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the json_array is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.JSON_VALUEContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the json_value is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.JSON_QUERYContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the json_query is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.JSON_MODIFYContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the json_modify is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.JSON_PATH_EXISTSContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the json_path_exists is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.ABSContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the abs is sensitive")- }- return querySpanResult, nil- case *parser.ACOSContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the acos is sensitive")- }- return querySpanResult, nil- case *parser.ASINContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the asin is sensitive")- }- return querySpanResult, nil- case *parser.ATANContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the atan is sensitive")- }- return querySpanResult, nil- case *parser.ATN2Context:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the atn2 is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.CEILINGContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the ceiling is sensitive")- }- return querySpanResult, nil- case *parser.COSContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the cos is sensitive")- }- return querySpanResult, nil- case *parser.COTContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the cot is sensitive")- }- return querySpanResult, nil- case *parser.DEGREESContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the degrees is sensitive")- }- return querySpanResult, nil- case *parser.EXPContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the exp is sensitive")- }- return querySpanResult, nil- case *parser.FLOORContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the floor is sensitive")- }- return querySpanResult, nil- case *parser.LOGContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the log is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.LOG10Context:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the log10 is sensitive")- }- return querySpanResult, nil- case *parser.POWERContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the power is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.RADIANSContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the radians is sensitive")- }- return querySpanResult, nil- case *parser.RANDContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the rand is sensitive")- }- return querySpanResult, nil- case *parser.ROUNDContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the round is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.MATH_SIGNContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the math_sign is sensitive")- }- return querySpanResult, nil- case *parser.SINContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the sin is sensitive")- }- return querySpanResult, nil- case *parser.SQRTContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the sqrt is sensitive")- }- return querySpanResult, nil- case *parser.SQUAREContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the square is sensitive")- }- return querySpanResult, nil- case *parser.TANContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the tan is sensitive")- }- return querySpanResult, nil- case *parser.GREATESTContext:- if expressionList := ctx.Expression_list_(); expressionList != nil {- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression_list_())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the greatest is sensitive")- }- return querySpanResult, nil- }- panic("never reach here")- case *parser.LEASTContext:- if expressionList := ctx.Expression_list_(); expressionList != nil {- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression_list_())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the least is sensitive")- }- return querySpanResult, nil- }- panic("never reach here")- case *parser.CERTENCODEDContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the certencoded is sensitive")- }- return querySpanResult, nil- case *parser.CERTPRIVATEKEYContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the certprivatekey is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.DATABASE_PRINCIPAL_IDContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the database_principal_id is sensitive")- }- return querySpanResult, nil- case *parser.HAS_DBACCESSContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the has_dbaccess is sensitive")- }- return querySpanResult, nil- case *parser.HAS_PERMS_BY_NAMEContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the has_perms_by_name is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.IS_MEMBERContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the is_member is sensitive")- }- return querySpanResult, nil- case *parser.IS_ROLEMEMBERContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the is_rolemember is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.IS_SRVROLEMEMBERContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the is_srvrolemember is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.LOGINPROPERTYContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the loginproperty is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.PERMISSIONSContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the permissions is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.PWDENCRYPTContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the pwdencrypt is sensitive")- }- return querySpanResult, nil- case *parser.PWDCOMPAREContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the pwdcompare is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.SESSIONPROPERTYContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the sessionproperty is sensitive")- }- return querySpanResult, nil- case *parser.SUSER_IDContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the suser_id is sensitive")- }- return querySpanResult, nil- case *parser.SUSER_SNAMEContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the suser_sname is sensitive")- }- return querySpanResult, nil- case *parser.SUSER_SIDContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, expression := range ctx.AllExpression() {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the suser_sid is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.USER_IDContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the user_id is sensitive")- }- return querySpanResult, nil- case *parser.USER_NAMEContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the user_name is sensitive")- }- return querySpanResult, nil- case *parser.SCALAR_FUNCTIONContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- if expressionList := ctx.Expression_list_(); expressionList != nil {- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Expression_list_())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the scalar_function is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- if scalarFunctionName := ctx.Scalar_function_name(); scalarFunctionName != nil {- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Scalar_function_name())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the scalar_function is sensitive")- }- var change bool- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- if !change {- return anchor, nil- }- }- return anchor, nil- case *parser.Scalar_function_nameContext:- return base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }, nil- case *parser.Freetext_functionContext:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- if allFullColumnName := ctx.AllFull_column_name(); len(allFullColumnName) > 0 {- for _, fullColumnName := range allFullColumnName {- querySpanResult, err := q.getQuerySpanResultFromExpr(fullColumnName)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the freetext_function is sensitive")- }- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- }- }- if allExpressions := ctx.AllExpression(); len(allExpressions) > 0 {- for _, expression := range allExpressions {- querySpanResult, err := q.getQuerySpanResultFromExpr(expression)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the freetext_function is sensitive")- }- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- }- }- return anchor, nil- case *parser.Full_column_nameContext:- querySpanResult, err := q.tsqlIsFullColumnNameSensitive(ctx)- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the full_column_name is sensitive")- }- return querySpanResult, nil- case *parser.PARTITION_FUNCContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Partition_function().Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the partition_function is sensitive")- }- return querySpanResult, nil- case *parser.HIERARCHYID_METHODContext:- querySpanResult, err := q.getQuerySpanResultFromExpr(ctx.Hierarchyid_static_method().Expression())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the hierarchyid_method is sensitive")- }- return querySpanResult, nil- default:- anchor := base.QuerySpanResult{- Name: ctx.GetText(),- SourceColumns: make(base.SourceColumnSet),- }- for _, child := range ctx.GetChildren() {- switch c := child.(type) {- case antlr.TerminalNode:- continue- case antlr.RuleNode:- querySpanResult, err := q.getQuerySpanResultFromExpr(c.GetRuleContext())- if err != nil {- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to check if the expr %s is sensitive", c.GetText())- }- anchor.SourceColumns, _ = base.MergeSourceColumnSet(anchor.SourceColumns, querySpanResult.SourceColumns)- default:- }- }- return anchor, nil- }-}--// unionTableSources union two or more table sources, return the original one if there is only one table source.-func unionTableSources(tableSources ...base.TableSource) ([]base.QuerySpanResult, error) {- if len(tableSources) == 0 {- return nil, errors.New("no table source to union")- }-- anchor := tableSources[0].GetQuerySpanResult()-- for i := 1; i < len(tableSources); i++ {- current := tableSources[i].GetQuerySpanResult()- if len(current) != len(anchor) {- return nil, errors.Errorf("the %dth table source has different column number with previous anchors, previous: %d, current %d", i+1, len(anchor), len(current))- }-- for j := range anchor {- anchor[j].SourceColumns, _ = base.MergeSourceColumnSet(anchor[j].SourceColumns, current[j].SourceColumns)- }- }-- return anchor, nil-}--// getAccessTables extracts the list of resources from the SELECT statement, and normalizes the object names with the NON-EMPTY currentNormalizedDatabase and currentNormalizedSchema.-func getAccessTables(currentNormalizedDatabase string, currentNormalizedSchema string, tree antlr.Tree) base.SourceColumnSet {- l := &accessTableListener{- currentDatabase: currentNormalizedDatabase,- currentSchema: currentNormalizedSchema,- resourceMap: make(base.SourceColumnSet),- }-- var result []base.SchemaResource- antlr.ParseTreeWalkerDefault.Walk(l, tree)- slices.SortFunc(result, func(a, b base.SchemaResource) int {- if a.String() < b.String() {- return -1- }- if a.String() > b.String() {- return 1- }- return 0- })-- return l.resourceMap-}--type accessTableListener struct {- *parser.BaseTSqlParserListener-- currentDatabase string- currentSchema string- resourceMap base.SourceColumnSet-}--// EnterTable_source_item is called when the parser enters the table_source_item production.-func (l *accessTableListener) EnterTable_source_item(ctx *parser.Table_source_itemContext) {- if fullTableName := ctx.Full_table_name(); fullTableName != nil {- name, err := NormalizeFullTableName(fullTableName)- if err != nil {- slog.Debug("Failed to normalize full table name", "error", err)- return- }- var linkedServer string- if name.LinkedServer != "" {- linkedServer = name.LinkedServer- }-- database := l.currentDatabase- if name.Database != "" {- database = name.Database- }-- schema := l.currentSchema- if name.Schema != "" {- schema = name.Schema- }-- var table string- if name.Table != "" {- table = name.Table- }-- l.resourceMap[base.ColumnResource{- Server: linkedServer,- Database: database,- Schema: schema,- Table: table,- }] = true- }-- if rowsetFunction := ctx.Rowset_function(); rowsetFunction != nil {- return- }-- // https://simonlearningsqlserver.wordpress.com/tag/changetable/- // It seems that the CHANGETABLE is only return some statistics, so we ignore it.- if changeTable := ctx.Change_table(); changeTable != nil {- return}-- // other...-}--// isMixedQuery checks whether the query accesses the user table and system table at the same time.-func isMixedQuery(m base.SourceColumnSet, ignoreCaseSensitive bool) (bool, bool) {- hasSystem, hasUser := false, false- for table := range m {- if isSystemResource(table, ignoreCaseSensitive) {- hasSystem = true- } else {- hasUser = true- }- }-if hasSystem && hasUser {return false, true}-return !hasUser && hasSystem, false}@@ -3615,35 +393,8 @@ func isSystemResource(resource base.ColumnResource, ignoreCaseSensitive bool) boreturn false}-// splitTableNameIntoNormalizedParts splits the table name into normalized 3 parts: database, schema, table.-func splitTableNameIntoNormalizedParts(tableName parser.ITable_nameContext) (string, string, string) {- var database string- if d := tableName.GetDatabase(); d != nil {- _, normalizedD := NormalizeTSQLIdentifier(d)- if normalizedD != "" {- database = normalizedD- }- }-- var schema string- if s := tableName.GetSchema(); s != nil {- _, normalizedS := NormalizeTSQLIdentifier(s)- if normalizedS != "" {- schema = normalizedS- }- }-- var table string- if t := tableName.GetTable(); t != nil {- _, normalizedT := NormalizeTSQLIdentifier(t)- if normalizedT != "" {- table = normalizedT- }- }- return database, schema, table-}--// normalizeFullTableNameFallback normalizes the each part of the full table name, returns (linkedServer, database, schema, table).+// normalizeFullTableNameFallback normalizes the parts of an ANTLR full_table_name+// context. Retained for non-query-span tsql callers (completion, backup).func normalizeFullTableNameFallback(fullTableName parser.IFull_table_nameContext, normalizedFallbackDatabaseName, normalizedFallbackSchemaName string) (string, string, string, string) {if fullTableName == nil {return "", "", "", ""@@ -3662,7 +413,6 @@ func normalizeFullTableNameFallback(fullTableName parser.IFull_table_nameContextif name.Database != "" {database = name.Database}-schema := normalizedFallbackSchemaNameif name.Schema != "" {schema = name.Schema@@ -3672,10 +422,11 @@ func normalizeFullTableNameFallback(fullTableName parser.IFull_table_nameContextif name.Table != "" {table = name.Table}-return linkedServer, database, schema, table}+// unquote strips surrounding brackets or `N'...'` from an identifier literal.+// Retained for non-query-span tsql callers.func unquote(name string) string {if len(name) < 2 {return name@@ -3683,61 +434,8 @@ func unquote(name string) string {if name[0] == '[' && name[len(name)-1] == ']' {return name[1 : len(name)-1]}-if len(name) > 3 && name[0] == 'N' && name[1] == '\'' && name[len(name)-1] == '\'' {return name[2 : len(name)-1]}return name}--func getSelectBodyFromCreateView(createView string) (string, []string, error) {- antlrASTs, err := ParseTSQL(createView)- if err != nil {- return "", nil, err- }-- if len(antlrASTs) != 1 {- return "", nil, errors.Errorf("expected exactly 1 statement, got %d", len(antlrASTs))- }-- ast := antlrASTs[0]- tree := ast.Tree- if tree == nil {- return "", nil, errors.Errorf("parse tree is nil")- }-- extractor := &selectBodyExtractor{}- antlr.ParseTreeWalkerDefault.Walk(extractor, tree)-- if extractor.selectBody == "" {- return "", nil, errors.Errorf("no SELECT statement found in CREATE VIEW")- }-- return extractor.selectBody, extractor.columnAliases, nil-}--type selectBodyExtractor struct {- *parser.BaseTSqlParserListener- selectBody string- columnAliases []string-}--func (e *selectBodyExtractor) EnterCreate_view(ctx *parser.Create_viewContext) {- // Get the SELECT statement part from CREATE VIEW- if selectStatement := ctx.Select_statement_standalone(); selectStatement != nil {- // Extract the text of the SELECT statement using GetTextFromInterval to preserve spaces- start := selectStatement.GetStart().GetStart()- stop := selectStatement.GetStop().GetStop()- input := selectStatement.GetStart().GetInputStream()- interval := antlr.NewInterval(start, stop)- e.selectBody = input.GetTextFromInterval(interval)- }-- // Extract the optional column alias list: CREATE VIEW vw (F1, F2) AS SELECT ...- if columnNameList := ctx.Column_name_list(); columnNameList != nil {- for _, id := range columnNameList.AllId_() {- _, normalizedName := NormalizeTSQLIdentifier(id)- e.columnAliases = append(e.columnAliases, normalizedName)- }- }-}diff --git a/backend/plugin/parser/tsql/query_span_extractor_omni.go b/backend/plugin/parser/tsql/query_span_extractor_omni.gonew file mode 100644index 0000000000..fc7af59fd2--- /dev/null+++ b/backend/plugin/parser/tsql/query_span_extractor_omni.go@@ -0,0 +1,1470 @@+package tsql++import (+ "context"+ "reflect"+ "strings"+ "unicode"++ "github.com/bytebase/omni/mssql/ast"+ "github.com/pkg/errors"++ "github.com/bytebase/bytebase/backend/plugin/parser/base"+)++// errOmniUnsupported is a development-time sentinel: signals that the omni+// extractor cannot handle the given construct yet. Only used by unit tests to+// distinguish "not yet implemented" from real bugs. Never returned from the+// public GetQuerySpan entry point after cutover.+var errOmniUnsupported = errors.New("omni query span extractor: unsupported construct")++// omniQuerySpanExtractor extracts query span information from omni MSSQL AST.+// It embeds querySpanExtractor so string-based resolution helpers+// (tsqlIsFieldSensitive, tsqlFindTableSchemaByParts, isIdentifierEqual,+// tsqlGetAllFieldsOfTableInFromOrOuterCTE) are reused directly.+type omniQuerySpanExtractor struct {+ *querySpanExtractor+ // source is the original SQL text, used to slice expression names via Loc.+ source string+}++func newOmniQuerySpanExtractor(defaultDatabase, defaultSchema string, gCtx base.GetQuerySpanContext, ignoreCaseSensitive bool) *omniQuerySpanExtractor {+ return &omniQuerySpanExtractor{+ querySpanExtractor: newQuerySpanExtractor(defaultDatabase, defaultSchema, gCtx, ignoreCaseSensitive),+ }+}++// getOmniQuerySpan is the public entry point. Mirrors the ANTLR getQuerySpan flow.+func (q *omniQuerySpanExtractor) getOmniQuerySpan(ctx context.Context, statement string) (*base.QuerySpan, error) {+ q.ctx = ctx+ q.source = statement++ stmts, err := ParseTSQLOmni(statement)+ if err != nil {+ return nil, err+ }+ if len(stmts) == 0 {+ return &base.QuerySpan{+ Type: base.Select,+ SourceColumns: make(base.SourceColumnSet),+ Results: []base.QuerySpanResult{},+ }, nil+ }+ if len(stmts) != 1 {+ return nil, errors.Errorf("expected exactly 1 statement, got %d", len(stmts))+ }++ root := stmts[0].AST+ if root == nil {+ // An empty statement. Match ANTLR's zero-AST behavior.+ return &base.QuerySpan{+ Type: base.Select,+ SourceColumns: make(base.SourceColumnSet),+ Results: []base.QuerySpanResult{},+ }, nil+ }+ if _, isGo := root.(*ast.GoStmt); isGo {+ // A bare "GO" batch separator. ANTLR discards it and returns an empty+ // Select span; match that.+ return &base.QuerySpan{+ Type: base.Select,+ SourceColumns: make(base.SourceColumnSet),+ Results: []base.QuerySpanResult{},+ }, nil+ }++ // DECLARE @t TABLE(...) populates gCtx.TempTables so subsequent statements+ // in the same session (the caller loops over split statements) can resolve+ // @t. Mirrors the legacy ANTLR tsqlSelectOnlyListener.EnterDeclare_statement+ // side-effect. Non-table DECLARE (plain scalar variables) is a no-op.+ if d, ok := root.(*ast.DeclareStmt); ok {+ populateOmniTempTables(d, q.gCtx.TempTables)+ }++ accessTables := collectOmniAccessTables(root, q.defaultDatabase, q.defaultSchema)+ allSystems, mixed := isMixedQuery(accessTables, q.ignoreCaseSensitive)+ if mixed {+ return nil, base.MixUserSystemTablesError+ }++ queryType := classifyQueryType(root, allSystems)+ sel, isSelect := root.(*ast.SelectStmt)+ // classifyQueryType maps SET/DECLARE/SetOption to base.Select for historical+ // reasons; those have no result columns. Return early if the root isn't a+ // real SELECT.+ if queryType != base.Select || !isSelect {+ return &base.QuerySpan{+ Type: queryType,+ SourceColumns: base.SourceColumnSet{},+ Results: []base.QuerySpanResult{},+ }, nil+ }++ pseudo, err := q.extractFromSelectStmt(sel)+ if err != nil {+ var resourceNotFound *base.ResourceNotFoundError+ if errors.As(err, &resourceNotFound) {+ return &base.QuerySpan{+ Type: base.Select,+ SourceColumns: accessTables,+ Results: []base.QuerySpanResult{},+ NotFoundError: resourceNotFound,+ }, nil+ }+ return nil, err+ }++ return &base.QuerySpan{+ Type: base.Select,+ SourceColumns: accessTables,+ Results: pseudo.GetQuerySpanResult(),+ PredicateColumns: q.predicateColumns,+ }, nil+}++// -------------------- SELECT processing --------------------++// extractFromSelectStmt processes a full SELECT statement (including set-ops,+// WITH clause) and returns the result column set as a PseudoTable.+func (q *omniQuerySpanExtractor) extractFromSelectStmt(sel *ast.SelectStmt) (*base.PseudoTable, error) {+ // WITH clause: populate CTE list BEFORE dispatching set-ops so that both+ // arms of a `WITH cte AS (...) SELECT ... FROM cte UNION SELECT ... FROM cte`+ // see the CTE in scope. The defer restores q.ctes on exit.+ ctesSnapshot := len(q.ctes)+ defer func() { q.ctes = q.ctes[:ctesSnapshot] }()+ if sel.WithClause != nil {+ if err := q.processWithClause(sel.WithClause); err != nil {+ return nil, err+ }+ }++ // Set operations: recurse into arms and union. The WITH clause above is+ // already in q.ctes, so both arms resolve CTE references correctly.+ if sel.Op != ast.SetOpNone && (sel.Larg != nil || sel.Rarg != nil) {+ return q.extractFromSetOp(sel)+ }++ // FROM clause — a SELECT without FROM is legal (e.g. SELECT 1).+ tableSourcesSnapshot := len(q.tableSourcesFrom)+ defer func() { q.tableSourcesFrom = q.tableSourcesFrom[:tableSourcesSnapshot] }()+ if sel.FromClause != nil {+ for _, item := range sel.FromClause.Items {+ ts, err := q.extractTableSource(item)+ if err != nil {+ return nil, err+ }+ q.tableSourcesFrom = append(q.tableSourcesFrom, ts...)+ }+ }++ // WHERE / HAVING predicate columns — resolved in THIS select's scope.+ // Subquery expressions encountered during the walk get cloned extractors;+ // their results merge back into q.predicateColumns. Errors (including+ // ResourceNotFoundError from a subquery referencing a missing table)+ // propagate up to getOmniQuerySpan which converts them into a span with+ // NotFoundError populated.+ if err := q.collectPredicatesInScope(sel.WhereClause); err != nil {+ return nil, err+ }+ if err := q.collectPredicatesInScope(sel.HavingClause); err != nil {+ return nil, err+ }++ // Target list.+ results, err := q.extractTargetList(sel.TargetList)+ if err != nil {+ return nil, err+ }++ return &base.PseudoTable{Columns: results}, nil+}++// collectPredicatesInScope walks a boolean expression in the extractor's+// current scope: plain ColumnRefs resolve against q.tableSourcesFrom ++// q.outerTableSources; subquery nodes trigger a scope clone, and the+// subquery's output-column sources plus its own predicate-column set merge+// back into q.predicateColumns.+//+// Errors from nested subquery extraction (ResourceNotFoundError or real+// parser errors) propagate so the caller can surface them via the top-level+// NotFoundError path instead of producing a silently-partial span.+func (q *omniQuerySpanExtractor) collectPredicatesInScope(expr ast.Node) error {+ if expr == nil {+ return nil+ }+ switch v := expr.(type) {+ case *ast.ColumnRef:+ r, err := q.tsqlIsFieldSensitive(v.Database, v.Schema, v.Table, v.Column)+ if err != nil {+ return errors.Wrapf(err, "failed to resolve predicate column %q", v.Column)+ }+ q.predicateColumns, _ = base.MergeSourceColumnSet(q.predicateColumns, r.SourceColumns)+ return nil+ case *ast.FullTextPredicate:+ if v.Columns != nil {+ for _, it := range v.Columns.Items {+ if cr, ok := it.(*ast.ColumnRef); ok {+ if err := q.collectPredicatesInScope(cr); err != nil {+ return err+ }+ }+ }+ }+ if err := q.collectPredicatesInScope(v.Value); err != nil {+ return err+ }+ return q.collectPredicatesInScope(v.LanguageTerm)+ case *ast.SubqueryExpr:+ return q.mergeSubqueryIntoPredicates(v.Query)+ case *ast.ExistsExpr:+ return q.mergeSubqueryIntoPredicates(v.Subquery)+ case *ast.SubqueryComparisonExpr:+ if err := q.collectPredicatesInScope(v.Left); err != nil {+ return err+ }+ return q.mergeSubqueryIntoPredicates(v.Subquery)+ case *ast.InExpr:+ if err := q.collectPredicatesInScope(v.Expr); err != nil {+ return err+ }+ if v.List != nil {+ for _, it := range v.List.Items {+ if err := q.collectPredicatesInScope(it); err != nil {+ return err+ }+ }+ }+ return q.collectPredicatesInScope(v.Subquery)+ case *ast.BinaryExpr:+ if err := q.collectPredicatesInScope(v.Left); err != nil {+ return err+ }+ return q.collectPredicatesInScope(v.Right)+ case *ast.UnaryExpr:+ return q.collectPredicatesInScope(v.Operand)+ case *ast.BetweenExpr:+ if err := q.collectPredicatesInScope(v.Expr); err != nil {+ return err+ }+ if err := q.collectPredicatesInScope(v.Low); err != nil {+ return err+ }+ return q.collectPredicatesInScope(v.High)+ case *ast.LikeExpr:+ if err := q.collectPredicatesInScope(v.Expr); err != nil {+ return err+ }+ if err := q.collectPredicatesInScope(v.Pattern); err != nil {+ return err+ }+ return q.collectPredicatesInScope(v.Escape)+ case *ast.IsExpr:+ return q.collectPredicatesInScope(v.Expr)+ case *ast.CaseExpr:+ if err := q.collectPredicatesInScope(v.Arg); err != nil {+ return err+ }+ if err := q.collectPredicatesInScope(v.ElseExpr); err != nil {+ return err+ }+ if v.WhenList != nil {+ for _, it := range v.WhenList.Items {+ if cw, ok := it.(*ast.CaseWhen); ok {+ if err := q.collectPredicatesInScope(cw.Condition); err != nil {+ return err+ }+ if err := q.collectPredicatesInScope(cw.Result); err != nil {+ return err+ }+ }+ }+ }+ return nil+ case *ast.IifExpr:+ if err := q.collectPredicatesInScope(v.Condition); err != nil {+ return err+ }+ if err := q.collectPredicatesInScope(v.TrueVal); err != nil {+ return err+ }+ return q.collectPredicatesInScope(v.FalseVal)+ case *ast.CoalesceExpr:+ if v.Args != nil {+ for _, it := range v.Args.Items {+ if err := q.collectPredicatesInScope(it); err != nil {+ return err+ }+ }+ }+ return nil+ case *ast.NullifExpr:+ if err := q.collectPredicatesInScope(v.Left); err != nil {+ return err+ }+ return q.collectPredicatesInScope(v.Right)+ case *ast.FuncCallExpr:+ if v.Args != nil {+ for _, it := range v.Args.Items {+ if err := q.collectPredicatesInScope(it); err != nil {+ return err+ }+ }+ }+ return nil+ case *ast.MethodCallExpr:+ if v.Args != nil {+ for _, it := range v.Args.Items {+ if err := q.collectPredicatesInScope(it); err != nil {+ return err+ }+ }+ }+ return nil+ case *ast.CastExpr:+ return q.collectPredicatesInScope(v.Expr)+ case *ast.ConvertExpr:+ if err := q.collectPredicatesInScope(v.Expr); err != nil {+ return err+ }+ return q.collectPredicatesInScope(v.Style)+ case *ast.TryCastExpr:+ return q.collectPredicatesInScope(v.Expr)+ case *ast.TryConvertExpr:+ if err := q.collectPredicatesInScope(v.Expr); err != nil {+ return err+ }+ return q.collectPredicatesInScope(v.Style)+ case *ast.ParenExpr:+ return q.collectPredicatesInScope(v.Expr)+ case *ast.CollateExpr:+ return q.collectPredicatesInScope(v.Expr)+ case *ast.AtTimeZoneExpr:+ if err := q.collectPredicatesInScope(v.Expr); err != nil {+ return err+ }+ return q.collectPredicatesInScope(v.TimeZone)+ default:+ // Literal, VariableRef, StarExpr, etc. — nothing to collect.+ return nil+ }+}++func (q *omniQuerySpanExtractor) mergeSubqueryIntoPredicates(body ast.Node) error {+ sel, ok := body.(*ast.SelectStmt)+ if !ok {+ return nil+ }+ clone := q.cloneForSubquery()+ pseudo, err := clone.extractFromSelectStmt(sel)+ if err != nil {+ return err+ }+ // Subquery output columns become outer-scope predicate columns.+ for _, c := range pseudo.GetQuerySpanResult() {+ q.predicateColumns, _ = base.MergeSourceColumnSet(q.predicateColumns, c.SourceColumns)+ }+ // And the clone's own predicate columns (from its WHERE etc.) propagate up.+ q.predicateColumns, _ = base.MergeSourceColumnSet(q.predicateColumns, clone.predicateColumns)+ return nil+}++func (q *omniQuerySpanExtractor) extractFromSetOp(sel *ast.SelectStmt) (*base.PseudoTable, error) {+ // Omni puts the WITH clause on the leftmost set-op arm (the parser attaches+ // the WITH to the first SELECT expression, not to the outer Op node). Lift+ // it into the current scope so the CTE is visible in BOTH arms, then+ // recurse into a copy of Larg with WithClause cleared to avoid double+ // processing.+ larg := sel.Larg+ if larg != nil && larg.WithClause != nil {+ if err := q.processWithClause(larg.WithClause); err != nil {+ return nil, err+ }+ cp := *larg+ cp.WithClause = nil+ larg = &cp+ }+ left, err := q.extractFromSelectStmt(larg)+ if err != nil {+ return nil, err+ }+ right, err := q.extractFromSelectStmt(sel.Rarg)+ if err != nil {+ return nil, err+ }+ merged, err := unionTableSources(left, right)+ if err != nil {+ return nil, err+ }+ return &base.PseudoTable{Columns: merged}, nil+}++func (q *omniQuerySpanExtractor) processWithClause(w *ast.WithClause) error {+ if w.CTEs == nil {+ return nil+ }+ for _, item := range w.CTEs.Items {+ cte, ok := item.(*ast.CommonTableExpr)+ if !ok {+ continue+ }+ body, ok := cte.Query.(*ast.SelectStmt)+ if !ok {+ return errOmniUnsupported+ }++ cteName := normIdent(cte.Name)+ cols, predicates, err := q.extractCTEBody(cteName, body)+ if err != nil {+ return err+ }+ if cte.Columns != nil && cte.Columns.Len() > 0 {+ if cte.Columns.Len() != len(cols) {+ return errors.Errorf("CTE %q column alias count %d does not match body column count %d", cte.Name, cte.Columns.Len(), len(cols))+ }+ for i, it := range cte.Columns.Items {+ if s, ok := it.(*ast.String); ok {+ cols[i].Name = normIdent(s.Str)+ }+ }+ }+ q.ctes = append(q.ctes, &base.PseudoTable{Name: cteName, Columns: cols})+ q.predicateColumns, _ = base.MergeSourceColumnSet(q.predicateColumns, predicates)+ }+ return nil+}++// extractCTEBody extracts the column set of a CTE body. For recursive CTEs+// (body is a set-op SelectStmt whose recursive arm references the CTE name),+// the extractor iterates until the column sources stabilize.+func (q *omniQuerySpanExtractor) extractCTEBody(cteName string, body *ast.SelectStmt) ([]base.QuerySpanResult, base.SourceColumnSet, error) {+ newClone := func() *omniQuerySpanExtractor {+ return &omniQuerySpanExtractor{+ querySpanExtractor: &querySpanExtractor{+ ctx: q.ctx,+ defaultDatabase: q.defaultDatabase,+ defaultSchema: q.defaultSchema,+ ignoreCaseSensitive: q.ignoreCaseSensitive,+ gCtx: q.gCtx,+ ctes: append([]*base.PseudoTable{}, q.ctes...),+ outerTableSources: nil,+ predicateColumns: make(base.SourceColumnSet),+ },+ source: q.source,+ }+ }++ // Non-set-op CTE: extract directly.+ if body.Op == ast.SetOpNone || (body.Larg == nil && body.Rarg == nil) {+ clone := newClone()+ pseudo, err := clone.extractFromSelectStmt(body)+ if err != nil {+ return nil, nil, err+ }+ return pseudo.GetQuerySpanResult(), clone.predicateColumns, nil+ }++ // Set-op CTE: extract anchor (Larg), register placeholder, then iterate Rarg+ // until the source column sets for the CTE columns stabilize.+ anchorClone := newClone()+ anchorPseudo, err := anchorClone.extractFromSelectStmt(body.Larg)+ if err != nil {+ return nil, nil, err+ }+ cols := append([]base.QuerySpanResult{}, anchorPseudo.GetQuerySpanResult()...)+ predicates := base.SourceColumnSet{}+ predicates, _ = base.MergeSourceColumnSet(predicates, anchorClone.predicateColumns)++ for iter := 0; iter < 16; iter++ {+ placeholder := &base.PseudoTable{Name: cteName, Columns: cols}+ clone := newClone()+ clone.ctes = append(clone.ctes, placeholder)+ rarg, rerr := clone.extractFromSelectStmt(body.Rarg)+ if rerr != nil {+ // Recursive arm failed to resolve (e.g. ResourceNotFoundError from+ // a missing table). Propagate — the top-level getOmniQuerySpan+ // converts ResourceNotFoundError into a span with NotFoundError+ // populated so callers don't see a silently-partial lineage.+ return nil, nil, rerr+ }+ rcols := rarg.GetQuerySpanResult()+ if len(rcols) != len(cols) {+ return nil, nil, errors.Errorf("CTE %q recursive arm returns %d columns, anchor returns %d", cteName, len(rcols), len(cols))+ }+ changed := false+ for i := range rcols {+ merged, anyChange := base.MergeSourceColumnSet(cols[i].SourceColumns, rcols[i].SourceColumns)+ cols[i].SourceColumns = merged+ if anyChange {+ changed = true+ }+ }+ predicates, _ = base.MergeSourceColumnSet(predicates, clone.predicateColumns)+ if !changed {+ break+ }+ }+ return cols, predicates, nil+}++// -------------------- FROM / table source --------------------++// extractTableSource returns the TableSource(s) produced by a single FROM item.+// A JoinClause contributes multiple sources (left + right flattened).+func (q *omniQuerySpanExtractor) extractTableSource(node ast.Node) ([]base.TableSource, error) {+ switch v := node.(type) {+ case *ast.JoinClause:+ return q.extractFromJoin(v)+ case *ast.TableRef:+ ts, err := q.resolveTableRef(v)+ if err != nil {+ return nil, err+ }+ return []base.TableSource{ts}, nil+ case *ast.AliasedTableRef:+ ts, err := q.resolveAliasedTableRef(v)+ if err != nil {+ return nil, err+ }+ return []base.TableSource{ts}, nil+ case *ast.SubqueryExpr:+ ts, err := q.resolveSubqueryAsTable(v, "")+ if err != nil {+ return nil, err+ }+ return []base.TableSource{ts}, nil+ case *ast.ValuesClause:+ ts, err := q.resolveValuesClause(v, "", nil)+ if err != nil {+ return nil, err+ }+ return []base.TableSource{ts}, nil+ case *ast.TableVarRef:+ return q.resolveTableVar(v, v.Alias)+ case *ast.TableVarMethodCallRef:+ return []base.TableSource{&base.PseudoTable{Name: v.Alias, Columns: xmlNodesColumns(v.Columns)}}, nil+ case *ast.PivotExpr:+ return nil, errors.New("pivot is not supported yet")+ case *ast.UnpivotExpr:+ return nil, errors.New("unpivot is not supported yet")+ case *ast.FuncCallExpr:+ return nil, unsupportedTableSourceError(node)+ default:+ return nil, errors.Wrapf(errOmniUnsupported, "FROM item type %T", node)+ }+}++func unsupportedTableSourceError(node ast.Node) error {+ return &base.TypeNotSupportedError{+ Err: errors.Errorf("only full table name in table source item is supported"),+ Type: reflect.TypeOf(node).String(),+ }+}++func (q *omniQuerySpanExtractor) extractFromJoin(j *ast.JoinClause) ([]base.TableSource, error) {+ left, err := q.extractTableSource(j.Left)+ if err != nil {+ return nil, err+ }+ if j.Type == ast.JoinCrossApply || j.Type == ast.JoinOuterApply {+ tableSourcesSnapshot := len(q.tableSourcesFrom)+ q.tableSourcesFrom = append(q.tableSourcesFrom, left...)+ defer func() { q.tableSourcesFrom = q.tableSourcesFrom[:tableSourcesSnapshot] }()+ }+ right, err := q.extractTableSource(j.Right)+ if err != nil {+ return nil, err+ }+ return append(left, right...), nil+}++func (q *omniQuerySpanExtractor) resolveTableRef(t *ast.TableRef) (base.TableSource, error) {+ ts, err := q.tsqlFindTableSchemaByParts(t.Server, t.Database, t.Schema, t.Object)+ if err != nil {+ return nil, err+ }+ if t.Alias != "" {+ ts = &base.PseudoTable{+ Name: normIdent(t.Alias),+ Columns: ts.GetQuerySpanResult(),+ }+ }+ return ts, nil+}++func (q *omniQuerySpanExtractor) resolveAliasedTableRef(at *ast.AliasedTableRef) (base.TableSource, error) {+ var columnAliases []string+ if at.Columns != nil {+ for _, it := range at.Columns.Items {+ if s, ok := it.(*ast.String); ok {+ columnAliases = append(columnAliases, s.Str)+ }+ }+ }+ switch inner := at.Table.(type) {+ case *ast.TableRef:+ ts, err := q.tsqlFindTableSchemaByParts(inner.Server, inner.Database, inner.Schema, inner.Object)+ if err != nil {+ return nil, err+ }+ cols := append([]base.QuerySpanResult{}, ts.GetQuerySpanResult()...)+ if err := applyColumnAliases(cols, columnAliases); err != nil {+ return nil, err+ }+ name := inner.Object+ if at.Alias != "" {+ name = at.Alias+ }+ return &base.PseudoTable{Name: name, Columns: cols}, nil+ case *ast.SubqueryExpr:+ return q.resolveSubqueryAsTable(inner, at.Alias, columnAliases...)+ case *ast.ValuesClause:+ return q.resolveValuesClause(inner, at.Alias, columnAliases)+ case *ast.TableVarRef:+ inners, err := q.resolveTableVar(inner, at.Alias)+ if err != nil {+ return nil, err+ }+ if len(inners) == 1 {+ return inners[0], nil+ }+ return nil, errors.Wrapf(errOmniUnsupported, "AliasedTableRef wrapping TableVarRef with non-single output")+ case *ast.FuncCallExpr:+ return nil, unsupportedTableSourceError(at.Table)+ case *ast.TableVarMethodCallRef:+ cols := xmlNodesColumns(inner.Columns)+ if err := applyColumnAliases(cols, columnAliases); err != nil {+ return nil, err+ }+ return &base.PseudoTable{Name: normIdent(at.Alias), Columns: cols}, nil+ default:+ return nil, errors.Wrapf(errOmniUnsupported, "AliasedTableRef inner %T", at.Table)+ }+}++func (q *omniQuerySpanExtractor) resolveSubqueryAsTable(sq *ast.SubqueryExpr, alias string, columnAliases ...string) (base.TableSource, error) {+ body, ok := sq.Query.(*ast.SelectStmt)+ if !ok {+ return nil, errOmniUnsupported+ }+ clone := q.cloneForSubquery()+ pseudo, err := clone.extractFromSelectStmt(body)+ if err != nil {+ return nil, err+ }+ cols := append([]base.QuerySpanResult{}, pseudo.GetQuerySpanResult()...)+ if err := applyColumnAliases(cols, columnAliases); err != nil {+ return nil, err+ }+ q.predicateColumns, _ = base.MergeSourceColumnSet(q.predicateColumns, clone.predicateColumns)+ return &base.PseudoTable{Name: normIdent(alias), Columns: cols}, nil+}++func (q *omniQuerySpanExtractor) resolveValuesClause(vc *ast.ValuesClause, alias string, columnAliases []string) (base.TableSource, error) {+ name := normIdent(alias)+ if vc.Rows == nil || vc.Rows.Len() == 0 {+ return &base.PseudoTable{Name: name}, nil+ }+ firstRow, ok := vc.Rows.Items[0].(*ast.List)+ if !ok {+ return &base.PseudoTable{Name: name}, nil+ }+ cols := make([]base.QuerySpanResult, 0, firstRow.Len())+ for rowIndex, item := range vc.Rows.Items {+ row, ok := item.(*ast.List)+ if !ok {+ continue+ }+ if row.Len() != firstRow.Len() {+ return nil, errors.Errorf("VALUES row %d has %d columns, first row has %d", rowIndex+1, row.Len(), firstRow.Len())+ }+ for i, it := range row.Items {+ expr, ok := it.(ast.ExprNode)+ if !ok {+ if rowIndex == 0 {+ cols = append(cols, base.QuerySpanResult{})+ }+ continue+ }+ r, err := q.resolveExpression(expr)+ if err != nil {+ return nil, err+ }+ if rowIndex == 0 {+ cols = append(cols, r)+ continue+ }+ cols[i].SourceColumns, _ = base.MergeSourceColumnSet(cols[i].SourceColumns, r.SourceColumns)+ }+ }+ if err := applyColumnAliases(cols, columnAliases); err != nil {+ return nil, err+ }+ return &base.PseudoTable{Name: name, Columns: cols}, nil+}++func (q *omniQuerySpanExtractor) resolveTableVar(tv *ast.TableVarRef, alias string) ([]base.TableSource, error) {+ if tv == nil {+ return nil, nil+ }+ name := tv.Name+ if temp, ok := q.gCtx.TempTables[name]; ok {+ tableName := name+ if alias != "" {+ tableName = alias+ }+ cols := make([]base.QuerySpanResult, 0, len(temp.Columns))+ for _, c := range temp.Columns {+ cols = append(cols, base.QuerySpanResult{+ Name: c,+ SourceColumns: base.SourceColumnSet{+ base.ColumnResource{+ Server: temp.Server,+ Database: temp.Database,+ Schema: temp.Schema,+ Table: temp.Name,+ Column: c,+ }: true,+ },+ IsPlainField: true,+ })+ }+ return []base.TableSource{&base.PseudoTable{Name: tableName, Columns: cols}}, nil+ }+ return nil, &base.ResourceNotFoundError{+ Table: &name,+ Err: errors.Errorf("temp table %s not found", name),+ }+}++// populateOmniTempTables mirrors the ANTLR tempTableColumnDefinitionListener:+// for each VariableDecl of table type, build a PhysicalTable keyed by the+// variable name (including the leading '@') and store it in the shared map.+func populateOmniTempTables(d *ast.DeclareStmt, out map[string]*base.PhysicalTable) {+ if d == nil || d.Variables == nil || out == nil {+ return+ }+ for _, it := range d.Variables.Items {+ v, ok := it.(*ast.VariableDecl)+ if !ok || !v.IsTable || v.TableDef == nil {+ continue+ }+ columns := make([]string, 0, v.TableDef.Len())+ for _, cdef := range v.TableDef.Items {+ if cd, ok := cdef.(*ast.ColumnDef); ok {+ columns = append(columns, cd.Name)+ }+ }+ out[v.Name] = &base.PhysicalTable{+ Name: v.Name,+ Columns: columns,+ }+ }+}++func xmlNodesColumns(cols []string) []base.QuerySpanResult {+ out := make([]base.QuerySpanResult, 0, len(cols))+ for _, c := range cols {+ out = append(out, base.QuerySpanResult{Name: c})+ }+ return out+}++func applyColumnAliases(cols []base.QuerySpanResult, aliases []string) error {+ if len(aliases) == 0 {+ return nil+ }+ if len(aliases) != len(cols) {+ return errors.Errorf("column alias count %d does not match column count %d", len(aliases), len(cols))+ }+ for i, name := range aliases {+ cols[i].Name = normIdent(name)+ }+ return nil+}++func (q *omniQuerySpanExtractor) cloneForSubquery() *omniQuerySpanExtractor {+ clone := &omniQuerySpanExtractor{+ querySpanExtractor: &querySpanExtractor{+ ctx: q.ctx,+ defaultDatabase: q.defaultDatabase,+ defaultSchema: q.defaultSchema,+ ignoreCaseSensitive: q.ignoreCaseSensitive,+ gCtx: q.gCtx,+ ctes: append([]*base.PseudoTable{}, q.ctes...),+ outerTableSources: append(append([]base.TableSource{}, q.outerTableSources...), q.tableSourcesFrom...),+ predicateColumns: make(base.SourceColumnSet),+ },+ source: q.source,+ }+ return clone+}++// -------------------- Target list --------------------++func (q *omniQuerySpanExtractor) extractTargetList(list *ast.List) ([]base.QuerySpanResult, error) {+ if list == nil {+ return nil, nil+ }+ var results []base.QuerySpanResult+ for _, item := range list.Items {+ switch v := item.(type) {+ case *ast.ResTarget:+ if star, ok := v.Val.(*ast.StarExpr); ok {+ fields, err := q.expandStar(star)+ if err != nil {+ return nil, err+ }+ results = append(results, fields...)+ continue+ }+ r, err := q.resolveExpression(v.Val)+ if err != nil {+ return nil, err+ }+ if v.Name != "" {+ r.Name = normIdent(v.Name)+ }+ results = append(results, r)+ case *ast.StarExpr:+ fields, err := q.expandStar(v)+ if err != nil {+ return nil, err+ }+ results = append(results, fields...)+ case *ast.SelectAssign:+ // SELECT @v = expr — no result column produced; just fold the expr+ // for predicate/source tracking (omitted here since it doesn't emit a column).+ continue+ default:+ if expr, ok := item.(ast.ExprNode); ok {+ r, err := q.resolveExpression(expr)+ if err != nil {+ return nil, err+ }+ results = append(results, r)+ continue+ }+ return nil, errors.Wrapf(errOmniUnsupported, "target item %T", item)+ }+ }+ return results, nil+}++func (q *omniQuerySpanExtractor) expandStar(s *ast.StarExpr) ([]base.QuerySpanResult, error) {+ if s.Qualifier == "" {+ var out []base.QuerySpanResult+ for _, ts := range q.tableSourcesFrom {+ out = append(out, ts.GetQuerySpanResult()...)+ }+ return out, nil+ }+ return q.tsqlGetAllFieldsOfTableInFromOrOuterCTE("", "", s.Qualifier)+}++// -------------------- Expression resolver --------------------++// resolveExpression maps an omni ExprNode to a QuerySpanResult. This is the+// omni equivalent of the ANTLR getQuerySpanResultFromExpr.+func (q *omniQuerySpanExtractor) resolveExpression(expr ast.ExprNode) (base.QuerySpanResult, error) {+ return q.resolveExpressionNode(expr)+}++func (q *omniQuerySpanExtractor) resolveExpressionNode(n ast.Node) (base.QuerySpanResult, error) {+ if n == nil {+ return base.QuerySpanResult{SourceColumns: make(base.SourceColumnSet)}, nil+ }+ if r, handled, err := q.resolveSimpleExpressionNode(n); handled {+ return r, err+ }+ switch v := n.(type) {+ case *ast.ColumnRef:+ r, err := q.tsqlIsFieldSensitive(v.Database, v.Schema, v.Table, v.Column)+ if err != nil {+ return base.QuerySpanResult{}, errors.Wrapf(err, "failed to resolve column %q", v.Column)+ }+ r.IsPlainField = true+ return r, nil+ case *ast.AtTimeZoneExpr:+ r, err := q.mergeExprSources(v.Expr, v.TimeZone)+ r.Name = q.sliceName(n)+ return r, err+ case *ast.BinaryExpr:+ r, err := q.mergeExprSources(v.Left, v.Right)+ r.Name = q.sliceName(n)+ return r, err+ case *ast.UnaryExpr:+ r, err := q.resolveExpressionNode(v.Operand)+ if err != nil {+ return r, err+ }+ r.Name = q.sliceName(n)+ r.IsPlainField = false+ return r, nil+ case *ast.BetweenExpr:+ r, err := q.mergeExprSources(v.Expr, v.Low, v.High)+ r.Name = q.sliceName(n)+ return r, err+ case *ast.LikeExpr:+ r, err := q.mergeExprSources(v.Expr, v.Pattern, v.Escape)+ r.Name = q.sliceName(n)+ return r, err+ case *ast.IsExpr:+ r, err := q.resolveExpressionNode(v.Expr)+ if err != nil {+ return r, err+ }+ r.Name = q.sliceName(n)+ r.IsPlainField = false+ return r, nil+ case *ast.InExpr:+ exprs := []ast.ExprNode{v.Expr}+ if v.List != nil {+ for _, it := range v.List.Items {+ if e, ok := it.(ast.ExprNode); ok {+ exprs = append(exprs, e)+ }+ }+ }+ if v.Subquery != nil {+ exprs = append(exprs, v.Subquery)+ }+ r, err := q.mergeExprSources(exprs...)+ r.Name = q.sliceName(n)+ return r, err+ case *ast.CaseExpr:+ exprs := []ast.ExprNode{v.Arg, v.ElseExpr}+ if v.WhenList != nil {+ for _, it := range v.WhenList.Items {+ if cw, ok := it.(*ast.CaseWhen); ok {+ exprs = append(exprs, cw.Condition, cw.Result)+ }+ }+ }+ r, err := q.mergeExprSources(exprs...)+ r.Name = q.sliceName(n)+ return r, err+ case *ast.IifExpr:+ r, err := q.mergeExprSources(v.Condition, v.TrueVal, v.FalseVal)+ r.Name = q.sliceName(n)+ return r, err+ case *ast.CoalesceExpr:+ r, err := q.mergeListSources(v.Args)+ r.Name = q.sliceName(n)+ return r, err+ case *ast.NullifExpr:+ r, err := q.mergeExprSources(v.Left, v.Right)+ r.Name = q.sliceName(n)+ return r, err+ case *ast.FuncCallExpr:+ r, err := q.mergeListSources(v.Args)+ if err != nil {+ return r, err+ }+ // Also merge Over clause partition/order.+ if v.Over != nil {+ sub, err := q.mergeListSources(v.Over.PartitionBy)+ if err != nil {+ return r, err+ }+ r.SourceColumns, _ = base.MergeSourceColumnSet(r.SourceColumns, sub.SourceColumns)+ sub, err = q.mergeOrderBySources(v.Over.OrderBy)+ if err != nil {+ return r, err+ }+ r.SourceColumns, _ = base.MergeSourceColumnSet(r.SourceColumns, sub.SourceColumns)+ }+ if v.Within != nil {+ sub, err := q.mergeOrderBySources(v.Within)+ if err != nil {+ return r, err+ }+ r.SourceColumns, _ = base.MergeSourceColumnSet(r.SourceColumns, sub.SourceColumns)+ }+ r.Name = q.sliceName(n)+ return r, nil+ case *ast.MethodCallExpr:+ r, err := q.mergeListSources(v.Args)+ r.Name = q.sliceName(n)+ return r, err+ case *ast.CastExpr:+ r, err := q.resolveExpressionNode(v.Expr)+ if err != nil {+ return r, err+ }+ r.Name = q.sliceName(n)+ r.IsPlainField = false+ return r, nil+ case *ast.ConvertExpr:+ r, err := q.mergeExprSources(v.Expr, v.Style)+ r.Name = q.sliceName(n)+ return r, err+ case *ast.TryCastExpr:+ r, err := q.resolveExpressionNode(v.Expr)+ if err != nil {+ return r, err+ }+ r.Name = q.sliceName(n)+ r.IsPlainField = false+ return r, nil+ case *ast.TryConvertExpr:+ r, err := q.mergeExprSources(v.Expr, v.Style)+ r.Name = q.sliceName(n)+ return r, err+ case *ast.SubqueryExpr:+ return q.resolveSubqueryExpr(v)+ case *ast.SubqueryComparisonExpr:+ r, err := q.resolveExpressionNode(v.Left)+ if err != nil {+ return r, err+ }+ if body, ok := v.Subquery.(*ast.SelectStmt); ok {+ clone := q.cloneForSubquery()+ sub, subErr := clone.extractFromSelectStmt(body)+ if subErr != nil {+ return r, subErr+ }+ for _, sc := range sub.GetQuerySpanResult() {+ r.SourceColumns, _ = base.MergeSourceColumnSet(r.SourceColumns, sc.SourceColumns)+ }+ q.predicateColumns, _ = base.MergeSourceColumnSet(q.predicateColumns, clone.predicateColumns)+ }+ r.Name = q.sliceName(n)+ r.IsPlainField = false+ return r, nil+ case *ast.ExistsExpr:+ r := base.QuerySpanResult{+ Name: q.sliceName(n),+ SourceColumns: make(base.SourceColumnSet),+ }+ if body, ok := v.Subquery.(*ast.SelectStmt); ok {+ clone := q.cloneForSubquery()+ if _, err := clone.extractFromSelectStmt(body); err != nil {+ return r, err+ }+ q.predicateColumns, _ = base.MergeSourceColumnSet(q.predicateColumns, clone.predicateColumns)+ }+ return r, nil+ case *ast.FullTextPredicate:+ r := base.QuerySpanResult{SourceColumns: make(base.SourceColumnSet)}+ if v.Columns != nil {+ for _, it := range v.Columns.Items {+ if cr, ok := it.(*ast.ColumnRef); ok {+ sub, err := q.tsqlIsFieldSensitive(cr.Database, cr.Schema, cr.Table, cr.Column)+ if err != nil {+ return r, errors.Wrapf(err, "failed to resolve full-text predicate column %q", cr.Column)+ }+ r.SourceColumns, _ = base.MergeSourceColumnSet(r.SourceColumns, sub.SourceColumns)+ }+ }+ }+ if v.Value != nil {+ sub, err := q.resolveExpressionNode(v.Value)+ if err != nil {+ return r, err+ }+ r.SourceColumns, _ = base.MergeSourceColumnSet(r.SourceColumns, sub.SourceColumns)+ }+ r.Name = q.sliceName(n)+ return r, nil+ case *ast.StarExpr:+ // Star in scalar context: treat as empty (already handled at target-list level).+ return base.QuerySpanResult{Name: q.sliceName(n), SourceColumns: make(base.SourceColumnSet)}, nil+ case *ast.GroupingSetsExpr, *ast.RollupExpr, *ast.CubeExpr:+ // Appear in GROUP BY; not typical targets. Fallback to empty.+ return base.QuerySpanResult{Name: q.sliceName(n), SourceColumns: make(base.SourceColumnSet)}, nil+ case *ast.ResTarget:+ r, err := q.resolveExpressionNode(v.Val)+ if err != nil {+ return r, err+ }+ if v.Name != "" {+ r.Name = v.Name+ }+ return r, nil+ case *ast.SelectAssign:+ return q.resolveExpressionNode(v.Value)+ case *ast.CaseWhen:+ return q.mergeExprSources(v.Condition, v.Result)+ case *ast.CurrentOfExpr:+ return base.QuerySpanResult{Name: q.sliceName(n), SourceColumns: make(base.SourceColumnSet)}, nil+ default:+ return base.QuerySpanResult{Name: q.sliceName(n), SourceColumns: make(base.SourceColumnSet)}, nil+ }+}++func (q *omniQuerySpanExtractor) resolveSimpleExpressionNode(n ast.Node) (base.QuerySpanResult, bool, error) {+ switch v := n.(type) {+ case *ast.Literal, *ast.VariableRef, *ast.Boolean:+ return base.QuerySpanResult{Name: q.sliceName(v), SourceColumns: make(base.SourceColumnSet)}, true, nil+ case *ast.ParenExpr:+ r, err := q.resolveExpressionNode(v.Expr)+ return r, true, err+ case *ast.CollateExpr:+ r, err := q.resolveExpressionNode(v.Expr)+ return r, true, err+ default:+ return base.QuerySpanResult{}, false, nil+ }+}++func (q *omniQuerySpanExtractor) resolveSubqueryExpr(sq *ast.SubqueryExpr) (base.QuerySpanResult, error) {+ r := base.QuerySpanResult{+ Name: q.sliceName(sq),+ SourceColumns: make(base.SourceColumnSet),+ }+ body, ok := sq.Query.(*ast.SelectStmt)+ if !ok {+ return r, nil+ }+ clone := q.cloneForSubquery()+ pseudo, err := clone.extractFromSelectStmt(body)+ if err != nil {+ return r, err+ }+ // Scalar subquery contributes its first result column's source columns.+ results := pseudo.GetQuerySpanResult()+ for _, c := range results {+ r.SourceColumns, _ = base.MergeSourceColumnSet(r.SourceColumns, c.SourceColumns)+ }+ q.predicateColumns, _ = base.MergeSourceColumnSet(q.predicateColumns, clone.predicateColumns)+ return r, nil+}++func (q *omniQuerySpanExtractor) mergeExprSources(exprs ...ast.ExprNode) (base.QuerySpanResult, error) {+ r := base.QuerySpanResult{SourceColumns: make(base.SourceColumnSet)}+ for _, e := range exprs {+ if e == nil {+ continue+ }+ sub, err := q.resolveExpressionNode(e)+ if err != nil {+ return r, err+ }+ r.SourceColumns, _ = base.MergeSourceColumnSet(r.SourceColumns, sub.SourceColumns)+ }+ return r, nil+}++func (q *omniQuerySpanExtractor) mergeListSources(list *ast.List) (base.QuerySpanResult, error) {+ r := base.QuerySpanResult{SourceColumns: make(base.SourceColumnSet)}+ if list == nil {+ return r, nil+ }+ for _, it := range list.Items {+ if e, ok := it.(ast.ExprNode); ok {+ sub, err := q.resolveExpressionNode(e)+ if err != nil {+ return r, err+ }+ r.SourceColumns, _ = base.MergeSourceColumnSet(r.SourceColumns, sub.SourceColumns)+ }+ }+ return r, nil+}++func (q *omniQuerySpanExtractor) mergeOrderBySources(list *ast.List) (base.QuerySpanResult, error) {+ r := base.QuerySpanResult{SourceColumns: make(base.SourceColumnSet)}+ if list == nil {+ return r, nil+ }+ for _, it := range list.Items {+ var expr ast.Node+ switch v := it.(type) {+ case *ast.OrderByItem:+ expr = v.Expr+ case ast.ExprNode:+ expr = v+ default:+ return r, errors.Wrapf(errOmniUnsupported, "ORDER BY item type %T", it)+ }+ sub, err := q.resolveExpressionNode(expr)+ if err != nil {+ return r, err+ }+ r.SourceColumns, _ = base.MergeSourceColumnSet(r.SourceColumns, sub.SourceColumns)+ }+ return r, nil+}++// -------------------- Helpers --------------------++// sliceName returns the whitespace-stripped source text covering a node's Loc.+// Mirrors ANTLR's ctx.GetText() convention for naming expression results.+// For *SubqueryExpr (and similar paren-wrapped nodes) the Loc spans the+// surrounding parens; we strip a matching leading `(` / trailing `)` pair to+// match the ANTLR subquery.getText() output.+func (q *omniQuerySpanExtractor) sliceName(n ast.Node) string {+ loc := omniNodeLoc(n)+ if loc.Start < 0 || loc.End < 0 || loc.Start >= len(q.source) {+ return ""+ }+ end := loc.End+ if end > len(q.source) {+ end = len(q.source)+ }+ text := stripSpaces(q.source[loc.Start:end])+ switch n.(type) {+ case *ast.SubqueryExpr, *ast.ExistsExpr, *ast.SubqueryComparisonExpr:+ if len(text) >= 2 && text[0] == '(' && text[len(text)-1] == ')' {+ text = text[1 : len(text)-1]+ }+ default:+ }+ return text+}++func stripSpaces(s string) string {+ var b strings.Builder+ b.Grow(len(s))+ for _, r := range s {+ if !unicode.IsSpace(r) {+ b.WriteRune(r)+ }+ }+ return b.String()+}++// normIdent lowercases a SQL-text-sourced identifier (table alias, column+// alias, CTE name, ResTarget name). Matches the legacy ANTLR+// NormalizeTSQLIdentifier behavior which lowercases unconditionally. Does not+// apply to identifiers sourced from metadata (physical table/column names);+// those are compared case-insensitively but stored as-is.+func normIdent(s string) string {+ return strings.ToLower(s)+}++// omniNodeLoc extracts the Loc field from an ast.Node via type switching.+// Not every node has a Loc (the Node interface doesn't expose it), so we+// enumerate the types we care about. Returns zero Loc otherwise.+func omniNodeLoc(n ast.Node) ast.Loc {+ if n == nil {+ return ast.NoLoc()+ }+ if loc, ok := omniExpressionNodeLoc(n); ok {+ return loc+ }+ switch v := n.(type) {+ case *ast.SelectStmt:+ return v.Loc+ case *ast.TableRef:+ return v.Loc+ case *ast.ResTarget:+ return v.Loc+ case *ast.SelectAssign:+ return v.Loc+ default:+ return ast.NoLoc()+ }+}++func omniExpressionNodeLoc(n ast.Node) (ast.Loc, bool) {+ switch v := n.(type) {+ case *ast.ColumnRef:+ return v.Loc, true+ case *ast.Literal:+ return v.Loc, true+ case *ast.VariableRef:+ return v.Loc, true+ case *ast.StarExpr:+ return v.Loc, true+ case *ast.BinaryExpr:+ return v.Loc, true+ case *ast.UnaryExpr:+ return v.Loc, true+ case *ast.BetweenExpr:+ return v.Loc, true+ case *ast.LikeExpr:+ return v.Loc, true+ case *ast.IsExpr:+ return v.Loc, true+ case *ast.InExpr:+ return v.Loc, true+ case *ast.CaseExpr:+ return v.Loc, true+ case *ast.CaseWhen:+ return v.Loc, true+ case *ast.IifExpr:+ return v.Loc, true+ case *ast.CoalesceExpr:+ return v.Loc, true+ case *ast.NullifExpr:+ return v.Loc, true+ case *ast.FuncCallExpr:+ return v.Loc, true+ case *ast.MethodCallExpr:+ return v.Loc, true+ case *ast.CastExpr:+ return v.Loc, true+ case *ast.ConvertExpr:+ return v.Loc, true+ case *ast.TryCastExpr:+ return v.Loc, true+ case *ast.TryConvertExpr:+ return v.Loc, true+ case *ast.SubqueryExpr:+ return v.Loc, true+ case *ast.SubqueryComparisonExpr:+ return v.Loc, true+ case *ast.ExistsExpr:+ return v.Loc, true+ case *ast.FullTextPredicate:+ return v.Loc, true+ case *ast.ParenExpr:+ return v.Loc, true+ case *ast.CollateExpr:+ return v.Loc, true+ case *ast.AtTimeZoneExpr:+ return v.Loc, true+ default:+ return ast.NoLoc(), false+ }+}++// -------------------- Access tables --------------------++// collectOmniAccessTables walks the AST and records every table-source+// position reference. Mirrors the ANTLR accessTableListener.+func collectOmniAccessTables(root ast.Node, currentDatabase, currentSchema string) base.SourceColumnSet {+ out := make(base.SourceColumnSet)+ walkAccessTables(root, currentDatabase, currentSchema, out)+ return out+}++func walkAccessTables(n ast.Node, db, schema string, out base.SourceColumnSet) {+ if n == nil {+ return+ }+ switch v := n.(type) {+ case *ast.SelectStmt:+ if v.WithClause != nil {+ walkAccessTables(v.WithClause, db, schema, out)+ }+ if v.FromClause != nil {+ for _, it := range v.FromClause.Items {+ walkAccessTables(it, db, schema, out)+ }+ }+ if v.WhereClause != nil {+ walkAccessTables(v.WhereClause, db, schema, out)+ }+ if v.HavingClause != nil {+ walkAccessTables(v.HavingClause, db, schema, out)+ }+ if v.TargetList != nil {+ for _, it := range v.TargetList.Items {+ walkAccessTables(it, db, schema, out)+ }+ }+ if v.Larg != nil {+ walkAccessTables(v.Larg, db, schema, out)+ }+ if v.Rarg != nil {+ walkAccessTables(v.Rarg, db, schema, out)+ }+ case *ast.WithClause:+ if v.CTEs != nil {+ for _, it := range v.CTEs.Items {+ if c, ok := it.(*ast.CommonTableExpr); ok {+ walkAccessTables(c.Query, db, schema, out)+ }+ }+ }+ case *ast.TableRef:+ addAccessTable(v, db, schema, out)+ case *ast.AliasedTableRef:+ walkAccessTables(v.Table, db, schema, out)+ case *ast.JoinClause:+ walkAccessTables(v.Left, db, schema, out)+ walkAccessTables(v.Right, db, schema, out)+ if v.Condition != nil {+ walkAccessTables(v.Condition, db, schema, out)+ }+ case *ast.SubqueryExpr:+ walkAccessTables(v.Query, db, schema, out)+ case *ast.PivotExpr:+ walkAccessTables(v.Source, db, schema, out)+ case *ast.UnpivotExpr:+ walkAccessTables(v.Source, db, schema, out)+ case *ast.ExistsExpr:+ walkAccessTables(v.Subquery, db, schema, out)+ case *ast.SubqueryComparisonExpr:+ walkAccessTables(v.Left, db, schema, out)+ walkAccessTables(v.Subquery, db, schema, out)+ case *ast.InExpr:+ walkAccessTables(v.Expr, db, schema, out)+ if v.List != nil {+ for _, it := range v.List.Items {+ walkAccessTables(it, db, schema, out)+ }+ }+ walkAccessTables(v.Subquery, db, schema, out)+ case *ast.BinaryExpr:+ walkAccessTables(v.Left, db, schema, out)+ walkAccessTables(v.Right, db, schema, out)+ case *ast.UnaryExpr:+ walkAccessTables(v.Operand, db, schema, out)+ case *ast.BetweenExpr:+ walkAccessTables(v.Expr, db, schema, out)+ walkAccessTables(v.Low, db, schema, out)+ walkAccessTables(v.High, db, schema, out)+ case *ast.LikeExpr:+ walkAccessTables(v.Expr, db, schema, out)+ walkAccessTables(v.Pattern, db, schema, out)+ case *ast.IsExpr:+ walkAccessTables(v.Expr, db, schema, out)+ case *ast.CaseExpr:+ walkAccessTables(v.Arg, db, schema, out)+ walkAccessTables(v.ElseExpr, db, schema, out)+ if v.WhenList != nil {+ for _, it := range v.WhenList.Items {+ walkAccessTables(it, db, schema, out)+ }+ }+ case *ast.CaseWhen:+ walkAccessTables(v.Condition, db, schema, out)+ walkAccessTables(v.Result, db, schema, out)+ case *ast.ParenExpr:+ walkAccessTables(v.Expr, db, schema, out)+ case *ast.CastExpr:+ walkAccessTables(v.Expr, db, schema, out)+ case *ast.ConvertExpr:+ walkAccessTables(v.Expr, db, schema, out)+ case *ast.TryCastExpr:+ walkAccessTables(v.Expr, db, schema, out)+ case *ast.TryConvertExpr:+ walkAccessTables(v.Expr, db, schema, out)+ case *ast.FuncCallExpr:+ if v.Args != nil {+ for _, it := range v.Args.Items {+ walkAccessTables(it, db, schema, out)+ }+ }+ case *ast.ResTarget:+ walkAccessTables(v.Val, db, schema, out)+ case *ast.ValuesClause:+ // No table access.+ case *ast.TableVarRef, *ast.TableVarMethodCallRef:+ // Temp-table var: no physical access.+ default:+ }+}++func addAccessTable(t *ast.TableRef, defDB, defSchema string, out base.SourceColumnSet) {+ if t == nil || strings.HasPrefix(t.Object, "#") {+ return+ }+ database := defDB+ if t.Database != "" {+ database = t.Database+ }+ schema := defSchema+ if t.Schema != "" {+ schema = t.Schema+ }+ out[base.ColumnResource{+ Server: t.Server,+ Database: database,+ Schema: schema,+ Table: t.Object,+ }] = true+}diff --git a/backend/plugin/parser/tsql/query_span_extractor_omni_test.go b/backend/plugin/parser/tsql/query_span_extractor_omni_test.gonew file mode 100644index 0000000000..dee6b3dffc--- /dev/null+++ b/backend/plugin/parser/tsql/query_span_extractor_omni_test.go@@ -0,0 +1,474 @@+package tsql++import (+ "cmp"+ "context"+ "errors"+ "slices"+ "testing"++ "github.com/stretchr/testify/require"++ storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/plugin/parser/base"+)++// omniTestMetadata is the default mock catalog used by these tests.+// db.dbo has tables t(a,b,c), t1(a,b,c), t2(a,b), and a view vw.+var omniTestMetadata = []*storepb.DatabaseSchemaMetadata{+ {+ Name: "db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "dbo",+ Tables: []*storepb.TableMetadata{+ {Name: "t", Columns: []*storepb.ColumnMetadata{{Name: "a"}, {Name: "b"}, {Name: "c"}}},+ {Name: "t1", Columns: []*storepb.ColumnMetadata{{Name: "a"}, {Name: "b"}, {Name: "c"}}},+ {Name: "t2", Columns: []*storepb.ColumnMetadata{{Name: "a"}, {Name: "b"}}},+ },+ Views: []*storepb.ViewMetadata{+ {Name: "vw", Definition: "CREATE VIEW [dbo].[vw] AS SELECT a, b FROM t"},+ },+ },+ },+ },+ {+ Name: "db2",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "dbo",+ Tables: []*storepb.TableMetadata{+ {Name: "t2", Columns: []*storepb.ColumnMetadata{{Name: "x"}, {Name: "y"}}},+ },+ },+ },+ },+}++// newOmniTestExtractor builds an extractor wired to the default mock catalog.+// Case-insensitive matching is on, matching T-SQL's default collation semantics.+func newOmniTestExtractor(t *testing.T, defaultDatabase string) *omniQuerySpanExtractor {+ t.Helper()+ getter, lister := buildMockDatabaseMetadataGetter(omniTestMetadata)+ gCtx := base.GetQuerySpanContext{+ GetDatabaseMetadataFunc: getter,+ ListDatabaseNamesFunc: lister,+ TempTables: make(map[string]*base.PhysicalTable),+ }+ return newOmniQuerySpanExtractor(defaultDatabase, "dbo", gCtx, true)+}++type expectedColumn struct {+ name string+ sources []base.ColumnResource // empty means isPlainField=false / no sources+}++func sortedSources(set base.SourceColumnSet) []base.ColumnResource {+ out := make([]base.ColumnResource, 0, len(set))+ for k := range set {+ out = append(out, k)+ }+ slices.SortFunc(out, func(a, b base.ColumnResource) int { return cmp.Compare(a.String(), b.String()) })+ return out+}++func TestOmniQuerySpan_SupportedShapes(t *testing.T) {+ type testCase struct {+ name string+ sql string+ defaultDatabase string+ wantResults []expectedColumn+ wantPredicate []base.ColumnResource+ wantAccessTables []base.ColumnResource+ }++ src := func(db, schema, table, col string) base.ColumnResource {+ return base.ColumnResource{Database: db, Schema: schema, Table: table, Column: col}+ }+ access := func(db, schema, table string) base.ColumnResource {+ return base.ColumnResource{Database: db, Schema: schema, Table: table}+ }++ cases := []testCase{+ {+ name: "bare_column",+ sql: "SELECT a FROM t",+ defaultDatabase: "db",+ wantResults: []expectedColumn{+ {"a", []base.ColumnResource{src("db", "dbo", "t", "a")}},+ },+ wantAccessTables: []base.ColumnResource{access("db", "dbo", "t")},+ },+ {+ name: "qualified_column",+ sql: "SELECT t.a FROM t",+ defaultDatabase: "db",+ wantResults: []expectedColumn{+ {"a", []base.ColumnResource{src("db", "dbo", "t", "a")}},+ },+ wantAccessTables: []base.ColumnResource{access("db", "dbo", "t")},+ },+ {+ name: "alias_as",+ sql: "SELECT a AS x FROM t",+ defaultDatabase: "db",+ wantResults: []expectedColumn{+ {"x", []base.ColumnResource{src("db", "dbo", "t", "a")}},+ },+ wantAccessTables: []base.ColumnResource{access("db", "dbo", "t")},+ },+ {+ name: "multi_columns",+ sql: "SELECT a, b FROM t",+ defaultDatabase: "db",+ wantResults: []expectedColumn{+ {"a", []base.ColumnResource{src("db", "dbo", "t", "a")}},+ {"b", []base.ColumnResource{src("db", "dbo", "t", "b")}},+ },+ wantAccessTables: []base.ColumnResource{access("db", "dbo", "t")},+ },+ {+ name: "star_unqualified",+ sql: "SELECT * FROM t",+ defaultDatabase: "db",+ wantResults: []expectedColumn{+ {"a", []base.ColumnResource{src("db", "dbo", "t", "a")}},+ {"b", []base.ColumnResource{src("db", "dbo", "t", "b")}},+ {"c", []base.ColumnResource{src("db", "dbo", "t", "c")}},+ },+ wantAccessTables: []base.ColumnResource{access("db", "dbo", "t")},+ },+ {+ name: "table_alias_qualified",+ sql: "SELECT x.a FROM t AS x",+ defaultDatabase: "db",+ wantResults: []expectedColumn{+ {"a", []base.ColumnResource{src("db", "dbo", "t", "a")}},+ },+ wantAccessTables: []base.ColumnResource{access("db", "dbo", "t")},+ },+ {+ name: "table_alias_star",+ sql: "SELECT x.* FROM t AS x",+ defaultDatabase: "db",+ wantResults: []expectedColumn{+ {"a", []base.ColumnResource{src("db", "dbo", "t", "a")}},+ {"b", []base.ColumnResource{src("db", "dbo", "t", "b")}},+ {"c", []base.ColumnResource{src("db", "dbo", "t", "c")}},+ },+ wantAccessTables: []base.ColumnResource{access("db", "dbo", "t")},+ },+ {+ name: "where_predicate_columns",+ sql: "SELECT a FROM t WHERE b > 1 AND c = 2",+ defaultDatabase: "db",+ wantResults: []expectedColumn{+ {"a", []base.ColumnResource{src("db", "dbo", "t", "a")}},+ },+ wantPredicate: []base.ColumnResource{+ src("db", "dbo", "t", "b"),+ src("db", "dbo", "t", "c"),+ },+ wantAccessTables: []base.ColumnResource{access("db", "dbo", "t")},+ },+ {+ name: "cross_database_table",+ sql: "SELECT x FROM db2.dbo.t2",+ defaultDatabase: "db",+ wantResults: []expectedColumn{+ {"x", []base.ColumnResource{src("db2", "dbo", "t2", "x")}},+ },+ wantAccessTables: []base.ColumnResource{access("db2", "dbo", "t2")},+ },+ {+ name: "explicit_schema",+ sql: "SELECT a FROM dbo.t",+ defaultDatabase: "db",+ wantResults: []expectedColumn{+ {"a", []base.ColumnResource{src("db", "dbo", "t", "a")}},+ },+ wantAccessTables: []base.ColumnResource{access("db", "dbo", "t")},+ },+ {+ name: "star_with_alias",+ sql: "SELECT y.* FROM t2 AS y",+ defaultDatabase: "db",+ wantResults: []expectedColumn{+ {"a", []base.ColumnResource{src("db", "dbo", "t2", "a")}},+ {"b", []base.ColumnResource{src("db", "dbo", "t2", "b")}},+ },+ wantAccessTables: []base.ColumnResource{access("db", "dbo", "t2")},+ },+ }++ for _, tc := range cases {+ t.Run(tc.name, func(t *testing.T) {+ q := newOmniTestExtractor(t, tc.defaultDatabase)+ span, err := q.getOmniQuerySpan(context.Background(), tc.sql)+ require.NoError(t, err, "sql: %s", tc.sql)+ require.NotNil(t, span)++ require.Equalf(t, base.Select, span.Type, "query type")+ require.Lenf(t, span.Results, len(tc.wantResults), "result count; got: %+v", span.Results)+ for i, want := range tc.wantResults {+ require.Equalf(t, want.name, span.Results[i].Name, "result[%d].Name", i)+ gotSources := sortedSources(span.Results[i].SourceColumns)+ wantSources := append([]base.ColumnResource{}, want.sources...)+ slices.SortFunc(wantSources, func(a, b base.ColumnResource) int { return cmp.Compare(a.String(), b.String()) })+ require.ElementsMatchf(t, wantSources, gotSources, "result[%d].SourceColumns", i)+ }++ wantPred := append([]base.ColumnResource{}, tc.wantPredicate...)+ slices.SortFunc(wantPred, func(a, b base.ColumnResource) int { return cmp.Compare(a.String(), b.String()) })+ require.ElementsMatch(t, wantPred, sortedSources(span.PredicateColumns), "PredicateColumns")++ wantAccess := append([]base.ColumnResource{}, tc.wantAccessTables...)+ slices.SortFunc(wantAccess, func(a, b base.ColumnResource) int { return cmp.Compare(a.String(), b.String()) })+ require.ElementsMatch(t, wantAccess, sortedSources(span.SourceColumns), "AccessTables")+ })+ }+}++func TestOmniQuerySpan_NotFound(t *testing.T) {+ q := newOmniTestExtractor(t, "db")+ span, err := q.getOmniQuerySpan(context.Background(), "SELECT a FROM no_such_table")+ require.NoError(t, err)+ require.NotNil(t, span.NotFoundError)+ require.Empty(t, span.Results)+}++// TestOmniQuerySpan_NonSelectQueryType verifies that non-SELECT statements+// still return an empty span with the correct query type (matching the ANTLR+// early-return behavior).+func TestOmniQuerySpan_NonSelectQueryType(t *testing.T) {+ cases := []struct {+ name string+ sql string+ typ base.QueryType+ }{+ {"insert", "INSERT INTO t(a) VALUES (1)", base.DML},+ {"update", "UPDATE t SET a = 1", base.DML},+ {"delete", "DELETE FROM t", base.DML},+ {"create_table", "CREATE TABLE t2 (id INT)", base.DDL},+ {"alter_table", "ALTER TABLE t ADD c INT", base.DDL},+ }+ for _, tc := range cases {+ t.Run(tc.name, func(t *testing.T) {+ q := newOmniTestExtractor(t, "db")+ span, err := q.getOmniQuerySpan(context.Background(), tc.sql)+ require.NoError(t, err)+ require.Equal(t, tc.typ, span.Type)+ require.Empty(t, span.Results)+ })+ }+}++// TestOmniQuerySpan_DeclareTableVariableRoundTrip verifies that a DECLARE @t+// TABLE(...) call populates gCtx.TempTables so a subsequent SELECT from @t+// (in the same session, with the same gCtx) can resolve its columns. This+// matches the pre-migration ANTLR side-effect.+func TestOmniQuerySpan_DeclareTableVariableRoundTrip(t *testing.T) {+ getter, lister := buildMockDatabaseMetadataGetter(omniTestMetadata)+ gCtx := base.GetQuerySpanContext{+ GetDatabaseMetadataFunc: getter,+ ListDatabaseNamesFunc: lister,+ TempTables: make(map[string]*base.PhysicalTable),+ }++ // Step 1: DECLARE populates TempTables.+ q1 := newOmniQuerySpanExtractor("db", "dbo", gCtx, true)+ span1, err := q1.getOmniQuerySpan(context.Background(), "DECLARE @t TABLE(id INT, name NVARCHAR(50))")+ require.NoError(t, err)+ require.Equal(t, base.Select, span1.Type)+ require.Empty(t, span1.Results)+ require.Contains(t, gCtx.TempTables, "@t", "DECLARE should register @t in gCtx.TempTables")+ require.Equal(t, []string{"id", "name"}, gCtx.TempTables["@t"].Columns)++ // Step 2: SELECT from @t resolves via TempTables.+ q2 := newOmniQuerySpanExtractor("db", "dbo", gCtx, true)+ span2, err := q2.getOmniQuerySpan(context.Background(), "SELECT id, name FROM @t")+ require.NoError(t, err)+ require.Equal(t, base.Select, span2.Type)+ require.Len(t, span2.Results, 2)+ require.Equal(t, "id", span2.Results[0].Name)+ require.Equal(t, "name", span2.Results[1].Name)+}++// TestOmniQuerySpan_UnsupportedTableSources verifies that table sources whose+// output shape is not modeled fail explicitly instead of returning partial+// lineage.+func TestOmniQuerySpan_UnsupportedTableSources(t *testing.T) {+ cases := []struct {+ name string+ sql string+ wantTypeNotSupported bool+ }{+ {name: "pivot", sql: "SELECT * FROM t PIVOT (SUM(a) FOR b IN ([1],[2])) AS p"},+ {name: "unpivot", sql: "SELECT * FROM t UNPIVOT (v FOR c IN ([1],[2])) AS u"},+ {name: "cross_apply_tvf", sql: "SELECT * FROM t1 CROSS APPLY fn(t1.a) AS x(v)", wantTypeNotSupported: true},+ {name: "aliased_tvf", sql: "SELECT * FROM fn(t1.a) AS x(v)", wantTypeNotSupported: true},+ }+ for _, tc := range cases {+ t.Run(tc.name, func(t *testing.T) {+ q := newOmniTestExtractor(t, "db")+ _, err := q.getOmniQuerySpan(context.Background(), tc.sql)+ require.Error(t, err)+ if tc.wantTypeNotSupported {+ var typeNotSupported *base.TypeNotSupportedError+ require.True(t, errors.As(err, &typeNotSupported), "expected TypeNotSupportedError, got %T: %v", err, err)+ }+ })+ }+}++// TestOmniQuerySpan_CTEVisibleInSetOpArms verifies that a CTE defined on a+// set-op SELECT root (the outer SelectStmt with Op != None) is visible inside+// both arms. Regression test for the P1 comment where the set-op early+// return fired before processWithClause.+func TestOmniQuerySpan_CTEVisibleInSetOpArms(t *testing.T) {+ q := newOmniTestExtractor(t, "db")+ sql := "WITH cte AS (SELECT a FROM t) SELECT a FROM cte UNION SELECT a FROM cte"+ span, err := q.getOmniQuerySpan(context.Background(), sql)+ require.NoError(t, err)+ require.Nil(t, span.NotFoundError, "CTE should resolve inside both UNION arms")+ require.Len(t, span.Results, 1)+ require.Equal(t, "a", span.Results[0].Name)+}++// TestOmniQuerySpan_SubqueryNotFoundPropagates verifies that a predicate+// subquery referencing a missing table surfaces the ResourceNotFoundError at+// the top-level QuerySpan instead of silently succeeding. Regression test+// for the P1 comment on mergeSubqueryIntoPredicates silently dropping errors.+func TestOmniQuerySpan_SubqueryNotFoundPropagates(t *testing.T) {+ q := newOmniTestExtractor(t, "db")+ sql := "SELECT a FROM t WHERE EXISTS (SELECT 1 FROM no_such_table)"+ span, err := q.getOmniQuerySpan(context.Background(), sql)+ require.NoError(t, err)+ require.NotNil(t, span.NotFoundError, "missing subquery table should surface NotFoundError")+ require.Empty(t, span.Results)+}++// TestOmniQuerySpan_RecursiveCTEArmNotFoundPropagates verifies that a+// recursive-CTE arm that can't resolve (e.g. references a missing table)+// surfaces NotFoundError at the top level instead of silently falling back+// to anchor-only columns.+func TestOmniQuerySpan_RecursiveCTEArmNotFoundPropagates(t *testing.T) {+ q := newOmniTestExtractor(t, "db")+ sql := "WITH rec AS (SELECT a FROM t UNION ALL SELECT a FROM rec JOIN no_such_table nx ON rec.a = nx.a) SELECT * FROM rec"+ span, err := q.getOmniQuerySpan(context.Background(), sql)+ require.NoError(t, err)+ require.NotNil(t, span.NotFoundError, "recursive arm hitting a missing table should surface NotFoundError")+ require.Empty(t, span.Results)+}++// TestOmniQuerySpan_SubqueryComparisonNotFoundPropagates verifies that a+// subquery-comparison expression (x > ANY/ALL (SELECT ...)) whose subquery+// references a missing table surfaces NotFoundError.+func TestOmniQuerySpan_SubqueryComparisonNotFoundPropagates(t *testing.T) {+ q := newOmniTestExtractor(t, "db")+ sql := "SELECT a FROM t WHERE a > ANY (SELECT x FROM no_such_table)"+ span, err := q.getOmniQuerySpan(context.Background(), sql)+ require.NoError(t, err)+ require.NotNil(t, span.NotFoundError, "missing subquery table should surface NotFoundError via SubqueryComparisonExpr")+ require.Empty(t, span.Results)+}++func TestOmniQuerySpan_ApplyRightSideCanReferenceLeft(t *testing.T) {+ q := newOmniTestExtractor(t, "db")+ sql := "SELECT x FROM t1 CROSS APPLY (VALUES (t1.a)) AS v(x)"+ span, err := q.getOmniQuerySpan(context.Background(), sql)+ require.NoError(t, err)+ require.Len(t, span.Results, 1)+ require.Equal(t, "x", span.Results[0].Name)+ require.ElementsMatch(t, []base.ColumnResource{+ {Database: "db", Schema: "dbo", Table: "t1", Column: "a"},+ }, sortedSources(span.Results[0].SourceColumns))+}++func TestOmniQuerySpan_ValuesClauseMergesAllRows(t *testing.T) {+ q := newOmniTestExtractor(t, "db")+ sql := "SELECT x FROM t1 CROSS APPLY (VALUES (t1.a), (t1.b)) AS v(x)"+ span, err := q.getOmniQuerySpan(context.Background(), sql)+ require.NoError(t, err)+ require.Len(t, span.Results, 1)+ require.Equal(t, "x", span.Results[0].Name)+ require.ElementsMatch(t, []base.ColumnResource{+ {Database: "db", Schema: "dbo", Table: "t1", Column: "a"},+ {Database: "db", Schema: "dbo", Table: "t1", Column: "b"},+ }, sortedSources(span.Results[0].SourceColumns))+}++func TestOmniQuerySpan_WindowOrderBySources(t *testing.T) {+ q := newOmniTestExtractor(t, "db")+ sql := "SELECT SUM(a) OVER (ORDER BY b) FROM t"+ span, err := q.getOmniQuerySpan(context.Background(), sql)+ require.NoError(t, err)+ require.Len(t, span.Results, 1)+ require.ElementsMatch(t, []base.ColumnResource{+ {Database: "db", Schema: "dbo", Table: "t", Column: "a"},+ {Database: "db", Schema: "dbo", Table: "t", Column: "b"},+ }, sortedSources(span.Results[0].SourceColumns))+}++func TestOmniQuerySpan_WithinGroupOrderBySources(t *testing.T) {+ q := newOmniTestExtractor(t, "db")+ sql := "SELECT PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY b) FROM t"+ span, err := q.getOmniQuerySpan(context.Background(), sql)+ require.NoError(t, err)+ require.Len(t, span.Results, 1)+ require.ElementsMatch(t, []base.ColumnResource{+ {Database: "db", Schema: "dbo", Table: "t", Column: "b"},+ }, sortedSources(span.Results[0].SourceColumns))+}++func TestOmniQuerySpan_InListAccessTables(t *testing.T) {+ q := newOmniTestExtractor(t, "db")+ sql := "SELECT a FROM t WHERE a IN (ABS((SELECT x FROM db2.dbo.t2)))"+ span, err := q.getOmniQuerySpan(context.Background(), sql)+ require.NoError(t, err)+ require.ElementsMatch(t, []base.ColumnResource{+ {Database: "db", Schema: "dbo", Table: "t"},+ {Database: "db2", Schema: "dbo", Table: "t2"},+ }, sortedSources(span.SourceColumns))+}++// TestOmniQuerySpan_UnresolvedColumnErrors verifies that an unresolvable+// column reference is surfaced as an error (both from SELECT list and from+// WHERE predicates) rather than producing a silently-partial span. Matches+// the legacy ANTLR extractor's strict behavior.+func TestOmniQuerySpan_UnresolvedColumnErrors(t *testing.T) {+ cases := []struct {+ name string+ sql string+ }{+ {"select_list", "SELECT no_such_col FROM t"},+ {"qualified_select_list", "SELECT t.no_such_col FROM t"},+ {"where_predicate", "SELECT a FROM t WHERE no_such_col = 1"},+ {"window_partition", "SELECT SUM(a) OVER (PARTITION BY no_such_col) FROM t"},+ {"window_order", "SELECT SUM(a) OVER (ORDER BY no_such_col) FROM t"},+ {"within_group_order", "SELECT PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY no_such_col) FROM t"},+ {"full_text_predicate", "SELECT CASE WHEN CONTAINS(no_such_col, 'x') THEN 1 ELSE 0 END FROM t"},+ }+ for _, tc := range cases {+ t.Run(tc.name, func(t *testing.T) {+ q := newOmniTestExtractor(t, "db")+ _, err := q.getOmniQuerySpan(context.Background(), tc.sql)+ require.Errorf(t, err, "unresolved column %q should surface as error", tc.sql)+ })+ }+}++// TestOmniQuerySpan_EmptyAndGo verifies that empty statements and bare GO+// produce a SELECT-typed empty span, matching ANTLR's zero-AST behavior.+func TestOmniQuerySpan_EmptyAndGo(t *testing.T) {+ cases := []string{"", "GO", ";"}+ for _, sql := range cases {+ t.Run(sql, func(t *testing.T) {+ q := newOmniTestExtractor(t, "db")+ span, err := q.getOmniQuerySpan(context.Background(), sql)+ require.NoError(t, err)+ require.Equal(t, base.Select, span.Type)+ require.Empty(t, span.Results)+ })+ }+}diff --git a/backend/plugin/parser/tsql/query_span_omni_probe_test.go b/backend/plugin/parser/tsql/query_span_omni_probe_test.gonew file mode 100644index 0000000000..8ee871d436--- /dev/null+++ b/backend/plugin/parser/tsql/query_span_omni_probe_test.go@@ -0,0 +1,663 @@+package tsql++import (+ "fmt"+ "io"+ "os"+ "slices"+ "strings"+ "testing"++ "github.com/bytebase/omni/mssql/ast"+ "gopkg.in/yaml.v3"+)++// TestOmniMigrationProbe verifies that the omni MSSQL parser can handle every+// statement used by the current query-span ANTLR-based tests, and that the omni+// AST populates the specific fields the planned transliteration depends on.+// This is a discovery test: failures are the point. Run with+//+// go test -v -count=1 github.com/bytebase/bytebase/backend/plugin/parser/tsql -run ^TestOmniMigrationProbe$+func TestOmniMigrationProbe(t *testing.T) {+ t.Run("FixtureParseCoverage", probeFixtureParseCoverage)+ t.Run("StructuralInvariants", probeStructuralInvariants)+}++type fixtureEntry struct {+ Description string `yaml:"description"`+ Statement string `yaml:"statement"`+}++var omniProbeFixturePaths = []string{+ "test-data/query-span/standard.yaml",+ "test-data/query-span/join.yaml",+ "test-data/query-span/case-sensitivity.yaml",+ "test-data/query-span/query_type.yaml",+ "test-data/query-span/predicate.yaml",+ "test-data/query-span/regression.yaml",+}++func probeFixtureParseCoverage(t *testing.T) {+ type failure struct {+ fixture string+ index int+ statement string+ err error+ }++ totals := map[string][2]int{} // fixture -> [total, passed]+ var failures []failure++ for _, path := range omniProbeFixturePaths {+ f, err := os.Open(path)+ if err != nil {+ t.Fatalf("open %s: %v", path, err)+ }+ data, err := io.ReadAll(f)+ _ = f.Close()+ if err != nil {+ t.Fatalf("read %s: %v", path, err)+ }+ var entries []fixtureEntry+ if err := yaml.Unmarshal(data, &entries); err != nil {+ t.Fatalf("yaml %s: %v", path, err)+ }+ total := 0+ passed := 0+ for i, e := range entries {+ if strings.TrimSpace(e.Statement) == "" {+ continue+ }+ total+++ _, err := ParseTSQLOmni(e.Statement)+ if err != nil {+ failures = append(failures, failure{fixture: path, index: i, statement: e.Statement, err: err})+ continue+ }+ passed+++ }+ totals[path] = [2]int{total, passed}+ }++ t.Log("=== Omni parse coverage per fixture ===")+ var keys []string+ for k := range totals {+ keys = append(keys, k)+ }+ slices.Sort(keys)+ totalAll, passedAll := 0, 0+ for _, k := range keys {+ v := totals[k]+ totalAll += v[0]+ passedAll += v[1]+ t.Logf(" %s: %d/%d", k, v[1], v[0])+ }+ t.Logf(" TOTAL: %d/%d", passedAll, totalAll)++ if len(failures) > 0 {+ t.Logf("=== %d parse failures ===", len(failures))+ for _, f := range failures {+ t.Logf(" [%s case %d] err: %v\n SQL: %s", f.fixture, f.index, f.err, firstLine(f.statement))+ }+ t.Errorf("omni failed to parse %d/%d fixture statements", len(failures), totalAll)+ }+}++func firstLine(s string) string {+ s = strings.TrimSpace(s)+ if i := strings.IndexByte(s, '\n'); i >= 0 {+ return s[:i] + " …"+ }+ return s+}++// probeStructuralInvariants runs hand-crafted SQL probes and asserts the+// omni AST populates fields the migration will rely on. A failure means we+// must either change omni or find an alternative in the transliteration.+func probeStructuralInvariants(t *testing.T) {+ type probe struct {+ name string+ sql string+ check func(*testing.T, ast.Node)+ }++ probes := []probe{+ {+ name: "res_target_as_alias",+ sql: "SELECT c AS a FROM t",+ check: func(t *testing.T, n ast.Node) {+ sel := mustSelect(t, n)+ rt := firstOf[*ast.ResTarget](t, sel.TargetList)+ if rt.Name != "a" {+ t.Errorf("want ResTarget.Name=a, got %q", rt.Name)+ }+ },+ },+ {+ name: "res_target_equals_alias",+ sql: "SELECT a = c FROM t",+ check: func(t *testing.T, n ast.Node) {+ sel := mustSelect(t, n)+ if sel.TargetList == nil || sel.TargetList.Len() == 0 {+ t.Fatal("empty TargetList")+ }+ // Document actual shape — omni may lose the alias completely for this form.+ switch v := sel.TargetList.Items[0].(type) {+ case *ast.ResTarget:+ t.Logf("omni parses `a = c` as ResTarget{Name=%q, Val=%T} — migration must recover alias from Val if Name is empty", v.Name, v.Val)+ if v.Name != "a" {+ if be, ok := v.Val.(*ast.BinaryExpr); ok && be.Op == ast.BinOpEq {+ t.Logf(" recovery: Val is BinaryExpr{Op=Eq, Left=%T, Right=%T} — migration can pull alias from Left *ColumnRef.Column", be.Left, be.Right)+ }+ t.Errorf("ResTarget.Name: want a, got %q (parser does not attribute the assignment form)", v.Name)+ }+ case *ast.SelectAssign:+ t.Errorf("omni parses `a = c` as SelectAssign(var=%q) — that's for `SET @a = c`, not column alias", v.Variable)+ default:+ t.Errorf("first TargetList item is %T", v)+ }+ },+ },+ {+ name: "res_target_bare_column",+ sql: "SELECT c FROM t",+ check: func(t *testing.T, n ast.Node) {+ sel := mustSelect(t, n)+ if sel.TargetList == nil || sel.TargetList.Len() == 0 {+ t.Fatal("empty TargetList")+ }+ switch v := sel.TargetList.Items[0].(type) {+ case *ast.ResTarget:+ if v.Name != "" {+ t.Errorf("ResTarget.Name: want empty for bare column, got %q", v.Name)+ }+ if _, ok := v.Val.(*ast.ColumnRef); !ok {+ t.Errorf("ResTarget.Val: want *ColumnRef, got %T", v.Val)+ }+ case *ast.ColumnRef:+ t.Log("note: bare column appears directly (not wrapped in ResTarget)")+ default:+ t.Errorf("first TargetList item is %T", v)+ }+ },+ },+ {+ name: "star_unqualified",+ sql: "SELECT * FROM t",+ check: func(t *testing.T, n ast.Node) {+ sel := mustSelect(t, n)+ star := findFirst[*ast.StarExpr](sel)+ if star == nil {+ t.Fatal("no StarExpr")+ }+ if star.Qualifier != "" {+ t.Errorf("want Qualifier empty, got %q", star.Qualifier)+ }+ },+ },+ {+ name: "star_qualified",+ sql: "SELECT t1.* FROM t1",+ check: func(t *testing.T, n ast.Node) {+ sel := mustSelect(t, n)+ star := findFirst[*ast.StarExpr](sel)+ if star == nil {+ t.Fatal("no StarExpr")+ }+ if star.Qualifier != "t1" {+ t.Errorf("want Qualifier=t1, got %q", star.Qualifier)+ }+ },+ },+ {+ name: "derived_table_no_column_list",+ sql: "SELECT a FROM (SELECT a FROM t) AS x",+ check: func(t *testing.T, n ast.Node) {+ sel := mustSelect(t, n)+ at := findFirst[*ast.AliasedTableRef](sel.FromClause)+ if at == nil {+ t.Fatal("no AliasedTableRef")+ }+ if at.Alias != "x" {+ t.Errorf("Alias: want x, got %q", at.Alias)+ }+ },+ },+ {+ name: "derived_table_column_list",+ sql: "SELECT c1, c2 FROM (SELECT a, b FROM t) AS x(c1, c2)",+ check: func(t *testing.T, n ast.Node) {+ sel := mustSelect(t, n)+ at := findFirst[*ast.AliasedTableRef](sel.FromClause)+ if at == nil {+ t.Fatal("no AliasedTableRef in FROM")+ }+ if at.Alias != "x" {+ t.Errorf("Alias: want x, got %q", at.Alias)+ }+ if at.Columns == nil || at.Columns.Len() != 2 {+ n := 0+ if at.Columns != nil {+ n = at.Columns.Len()+ }+ t.Errorf("Columns len: want 2, got %d", n)+ }+ },+ },+ {+ name: "cte_column_list",+ sql: "WITH cte(c1, c2) AS (SELECT a, b FROM t) SELECT c1 FROM cte",+ check: func(t *testing.T, n ast.Node) {+ sel := mustSelect(t, n)+ if sel.WithClause == nil || sel.WithClause.CTEs == nil || sel.WithClause.CTEs.Len() == 0 {+ t.Fatal("missing WITH clause")+ }+ c, ok := sel.WithClause.CTEs.Items[0].(*ast.CommonTableExpr)+ if !ok {+ t.Fatalf("CTEs.Items[0] is %T, want *CommonTableExpr", sel.WithClause.CTEs.Items[0])+ }+ if !strings.EqualFold(c.Name, "cte") {+ t.Errorf("CTE name: want cte, got %q", c.Name)+ }+ if c.Columns == nil || c.Columns.Len() != 2 {+ n := 0+ if c.Columns != nil {+ n = c.Columns.Len()+ }+ t.Errorf("CTE Columns len: want 2, got %d", n)+ }+ },+ },+ {+ name: "declare_table_variable_columns",+ sql: "DECLARE @t TABLE (id INT, name NVARCHAR(50))",+ check: func(t *testing.T, n ast.Node) {+ d, ok := n.(*ast.DeclareStmt)+ if !ok {+ t.Fatalf("want *DeclareStmt, got %T", n)+ }+ if d.Variables == nil || d.Variables.Len() == 0 {+ t.Fatal("no Variables")+ }+ v, ok := d.Variables.Items[0].(*ast.VariableDecl)+ if !ok {+ t.Fatalf("Variables.Items[0] is %T, want *VariableDecl", d.Variables.Items[0])+ }+ if !v.IsTable {+ t.Error("want IsTable=true")+ }+ if v.TableDef == nil || v.TableDef.Len() != 2 {+ n := 0+ if v.TableDef != nil {+ n = v.TableDef.Len()+ }+ t.Errorf("TableDef len: want 2, got %d", n)+ }+ // Verify ColumnDef has DataType populated.+ if v.TableDef != nil && v.TableDef.Len() > 0 {+ cd, ok := v.TableDef.Items[0].(*ast.ColumnDef)+ if !ok {+ t.Errorf("TableDef.Items[0] is %T, want *ColumnDef", v.TableDef.Items[0])+ } else if cd.DataType == nil {+ t.Error("ColumnDef.DataType is nil")+ } else if !strings.EqualFold(cd.DataType.Name, "int") {+ t.Errorf("DataType.Name: want INT, got %q", cd.DataType.Name)+ }+ }+ },+ },+ {+ name: "union_set_op",+ sql: "SELECT a FROM t1 UNION SELECT a FROM t2",+ check: func(t *testing.T, n ast.Node) {+ sel := mustSelect(t, n)+ if sel.Op != ast.SetOpUnion {+ t.Errorf("want Op=SetOpUnion, got %v", sel.Op)+ }+ if sel.Larg == nil || sel.Rarg == nil {+ t.Error("want non-nil Larg/Rarg")+ }+ },+ },+ {+ name: "union_all_set_op",+ sql: "SELECT a FROM t1 UNION ALL SELECT a FROM t2",+ check: func(t *testing.T, n ast.Node) {+ sel := mustSelect(t, n)+ if sel.Op != ast.SetOpUnion {+ t.Errorf("want Op=SetOpUnion, got %v", sel.Op)+ }+ if !sel.All {+ t.Error("want All=true for UNION ALL")+ }+ },+ },+ {+ name: "intersect_except",+ sql: "SELECT a FROM t1 INTERSECT SELECT a FROM t2 EXCEPT SELECT a FROM t3",+ check: func(t *testing.T, n ast.Node) {+ sel := mustSelect(t, n)+ // Just confirm Op is Intersect or Except at the top and arms exist.+ if sel.Op != ast.SetOpIntersect && sel.Op != ast.SetOpExcept {+ t.Errorf("want Op=Intersect or Except, got %v", sel.Op)+ }+ if sel.Larg == nil || sel.Rarg == nil {+ t.Error("want non-nil Larg/Rarg")+ }+ },+ },+ {+ name: "correlated_subquery_alias",+ sql: "SELECT a FROM outerT AS o WHERE EXISTS (SELECT 1 FROM innerT i WHERE i.a = o.a)",+ check: func(t *testing.T, n ast.Node) {+ sel := mustSelect(t, n)+ if sel.WhereClause == nil {+ t.Fatal("missing WHERE")+ }+ var foundExists, foundOuterRef bool+ ast.Inspect(sel.WhereClause, func(n ast.Node) bool {+ if _, ok := n.(*ast.ExistsExpr); ok {+ foundExists = true+ }+ if cr, ok := n.(*ast.ColumnRef); ok && strings.EqualFold(cr.Table, "o") {+ foundOuterRef = true+ }+ return true+ })+ if !foundExists {+ t.Error("missing ExistsExpr in WHERE")+ }+ if !foundOuterRef {+ t.Error("missing correlated column ref o.a")+ }+ },+ },+ {+ name: "cross_apply_tvf_no_col_list",+ sql: "SELECT * FROM t1 CROSS APPLY fn(t1.a) AS x",+ check: func(t *testing.T, n ast.Node) {+ sel := mustSelect(t, n)+ j := findFirst[*ast.JoinClause](sel.FromClause)+ if j == nil {+ t.Fatal("no JoinClause")+ }+ if j.Type != ast.JoinCrossApply {+ t.Errorf("JoinType: want CrossApply, got %v", j.Type)+ }+ },+ },+ {+ name: "cross_apply_tvf",+ sql: "SELECT * FROM t1 CROSS APPLY fn(t1.a) AS x(v)",+ check: func(t *testing.T, n ast.Node) {+ sel := mustSelect(t, n)+ j := findFirst[*ast.JoinClause](sel.FromClause)+ if j == nil {+ t.Fatal("no JoinClause in FROM")+ }+ if j.Type != ast.JoinCrossApply {+ t.Errorf("JoinType: want CrossApply, got %v", j.Type)+ }+ },+ },+ {+ name: "pivot",+ sql: "SELECT * FROM src PIVOT (SUM(amt) FOR cat IN ([a],[b])) AS p",+ check: func(t *testing.T, n ast.Node) {+ sel := mustSelect(t, n)+ if p := findFirst[*ast.PivotExpr](sel.FromClause); p == nil {+ t.Error("no PivotExpr")+ }+ },+ },+ {+ name: "unpivot",+ sql: "SELECT * FROM src UNPIVOT (v FOR c IN ([x],[y])) AS u",+ check: func(t *testing.T, n ast.Node) {+ sel := mustSelect(t, n)+ if u := findFirst[*ast.UnpivotExpr](sel.FromClause); u == nil {+ t.Error("no UnpivotExpr")+ }+ },+ },+ {+ name: "values_insert",+ sql: "INSERT INTO t(a,b) VALUES (1,2),(3,4)",+ check: func(t *testing.T, n ast.Node) {+ if vc := findFirst[*ast.ValuesClause](n); vc == nil {+ t.Error("no ValuesClause in INSERT — parser may lack VALUES support entirely")+ }+ },+ },+ {+ name: "values_table_constructor",+ sql: "SELECT * FROM (VALUES (1,2),(3,4)) AS v(a,b)",+ check: func(t *testing.T, n ast.Node) {+ sel := mustSelect(t, n)+ if vc := findFirst[*ast.ValuesClause](sel.FromClause); vc == nil {+ t.Error("no ValuesClause")+ }+ },+ },+ {+ name: "contains_predicate",+ sql: "SELECT a FROM t WHERE CONTAINS(b, 'foo')",+ check: func(t *testing.T, n ast.Node) {+ sel := mustSelect(t, n)+ if sel.WhereClause == nil {+ t.Error("WHERE is nil")+ }+ // Omni likely parses CONTAINS as a FuncCallExpr; document actual node shape.+ var kind string+ switch sel.WhereClause.(type) {+ case *ast.FuncCallExpr:+ kind = "FuncCallExpr"+ case *ast.BinaryExpr:+ kind = "BinaryExpr"+ default:+ kind = fmt.Sprintf("%T", sel.WhereClause)+ }+ t.Logf("CONTAINS predicate shape: %s — migration must handle this form instead of ANTLR Freetext_predicateContext", kind)+ },+ },+ {+ name: "freetext_predicate",+ sql: "SELECT a FROM t WHERE FREETEXT(b, 'foo')",+ check: func(t *testing.T, n ast.Node) {+ sel := mustSelect(t, n)+ if sel.WhereClause == nil {+ t.Error("WHERE is nil")+ }+ t.Logf("FREETEXT WHERE node: %T", sel.WhereClause)+ },+ },+ {+ name: "bracketed_identifier",+ sql: "SELECT [col name] FROM [my table]",+ check: func(t *testing.T, n ast.Node) {+ sel := mustSelect(t, n)+ col := findFirst[*ast.ColumnRef](sel)+ if col == nil {+ t.Fatal("no ColumnRef")+ }+ if col.Column != "col name" {+ t.Errorf("Column: want \"col name\" (unquoted), got %q", col.Column)+ }+ tr := findFirst[*ast.TableRef](sel)+ if tr == nil {+ t.Fatal("no TableRef")+ }+ if tr.Object != "my table" {+ t.Errorf("TableRef.Object: want \"my table\", got %q", tr.Object)+ }+ },+ },+ {+ name: "quoted_identifier",+ sql: "SELECT \"col name\" FROM \"my table\"",+ check: func(t *testing.T, n ast.Node) {+ sel := mustSelect(t, n)+ col := findFirst[*ast.ColumnRef](sel)+ if col == nil {+ t.Fatal("no ColumnRef")+ }+ if col.Column != "col name" {+ t.Errorf("Column: want \"col name\" (unquoted), got %q", col.Column)+ }+ },+ },+ {+ name: "cross_database_reference",+ sql: "SELECT c FROM db1.dbo.t",+ check: func(t *testing.T, n ast.Node) {+ sel := mustSelect(t, n)+ tr := findFirst[*ast.TableRef](sel.FromClause)+ if tr == nil {+ t.Fatal("no TableRef in FROM")+ }+ if tr.Database != "db1" || tr.Schema != "dbo" || tr.Object != "t" {+ t.Errorf("TableRef: got db=%q schema=%q obj=%q", tr.Database, tr.Schema, tr.Object)+ }+ },+ },+ {+ name: "linked_server_reference",+ sql: "SELECT c FROM srv1.db1.dbo.t",+ check: func(t *testing.T, n ast.Node) {+ sel := mustSelect(t, n)+ tr := findFirst[*ast.TableRef](sel.FromClause)+ if tr == nil {+ t.Fatal("no TableRef in FROM")+ }+ if tr.Server != "srv1" || tr.Database != "db1" || tr.Schema != "dbo" || tr.Object != "t" {+ t.Errorf("TableRef: got srv=%q db=%q schema=%q obj=%q", tr.Server, tr.Database, tr.Schema, tr.Object)+ }+ },+ },+ {+ name: "table_variable_in_from",+ sql: "DECLARE @t TABLE(id INT); SELECT id FROM @t",+ check: func(t *testing.T, _ ast.Node) {+ // ParseTSQLOmni gives a slice; this probe only checks the first stmt (DECLARE).+ // Walk the full parse to confirm the second has @t as TableVarRef.+ stmts, err := ParseTSQLOmni("DECLARE @t TABLE(id INT); SELECT id FROM @t")+ if err != nil {+ t.Fatalf("parse: %v", err)+ }+ if len(stmts) < 2 {+ t.Fatalf("want 2 stmts, got %d", len(stmts))+ }+ sel, ok := stmts[1].AST.(*ast.SelectStmt)+ if !ok {+ t.Fatalf("stmt[1] is %T, want *SelectStmt", stmts[1].AST)+ }+ tv := findFirst[*ast.TableVarRef](sel.FromClause)+ if tv == nil {+ t.Error("no TableVarRef in SELECT FROM @t")+ } else if tv.Name != "@t" {+ t.Errorf("TableVarRef.Name: want @t, got %q", tv.Name)+ }+ },+ },+ {+ name: "temp_table_hash_prefix",+ sql: "SELECT c FROM #t",+ check: func(t *testing.T, n ast.Node) {+ sel := mustSelect(t, n)+ tr := findFirst[*ast.TableRef](sel.FromClause)+ if tr == nil {+ t.Fatal("no TableRef in FROM")+ }+ if tr.Object != "#t" {+ t.Errorf("TableRef.Object for temp table: got %q — migration must recognize '#' prefix as local temp", tr.Object)+ }+ },+ },+ {+ name: "xml_nodes_method",+ sql: "SELECT T.c.value('@id', 'int') FROM @xml.nodes('/root/item') AS T(c)",+ check: func(t *testing.T, n ast.Node) {+ sel := mustSelect(t, n)+ if m := findFirst[*ast.TableVarMethodCallRef](sel.FromClause); m == nil {+ t.Error("no TableVarMethodCallRef")+ }+ },+ },+ {+ name: "for_xml_clause",+ sql: "SELECT a FROM t FOR XML AUTO",+ check: func(t *testing.T, n ast.Node) {+ sel := mustSelect(t, n)+ if sel.ForClause == nil {+ t.Error("ForClause is nil")+ }+ },+ },+ {+ name: "merge_output",+ sql: "MERGE tgt USING src ON tgt.id = src.id WHEN MATCHED THEN UPDATE SET v = src.v;",+ check: func(t *testing.T, n ast.Node) {+ if _, ok := n.(*ast.MergeStmt); !ok {+ t.Errorf("want *MergeStmt, got %T", n)+ }+ },+ },+ }++ for _, p := range probes {+ t.Run(p.name, func(t *testing.T) {+ stmts, err := ParseTSQLOmni(p.sql)+ if err != nil {+ t.Fatalf("parse err: %v\nSQL: %s", err, p.sql)+ }+ if len(stmts) == 0 || stmts[0].AST == nil {+ t.Fatal("empty parse result")+ }+ p.check(t, stmts[0].AST)+ })+ }+}++func mustSelect(t *testing.T, n ast.Node) *ast.SelectStmt {+ t.Helper()+ sel, ok := n.(*ast.SelectStmt)+ if !ok {+ t.Fatalf("expected *SelectStmt, got %T", n)+ }+ return sel+}++// findFirst walks root (any ast.Node) and returns the first node that type-asserts to T, or nil.+func findFirst[T ast.Node](root ast.Node) T {+ var zero T+ if root == nil {+ return zero+ }+ var found T+ ast.Inspect(root, func(n ast.Node) bool {+ if cast, ok := n.(T); ok {+ // Only take the first match; keep walking is harmless but we stop to save work.+ if any(found) == any(zero) {+ found = cast+ }+ return false+ }+ return true+ })+ return found+}++// firstOf returns the first item in a List that type-asserts to T, or nil.+// Used when callers know the expected wrapper type (e.g. *ResTarget in TargetList).+func firstOf[T ast.Node](t *testing.T, list *ast.List) T {+ t.Helper()+ var zero T+ if list == nil || list.Len() == 0 {+ t.Fatal("list empty")+ }+ cast, ok := list.Items[0].(T)+ if !ok {+ t.Fatalf("first item is %T, want %T", list.Items[0], zero)+ }+ return cast+}diff --git a/backend/plugin/parser/tsql/query_span_predicate.go b/backend/plugin/parser/tsql/query_span_predicate.godeleted file mode 100644index 370e32879e..0000000000--- a/backend/plugin/parser/tsql/query_span_predicate.go+++ /dev/null@@ -1,162 +0,0 @@-package tsql--import (- "github.com/antlr4-go/antlr/v4"- parser "github.com/bytebase/parser/tsql"- "github.com/pkg/errors"-- "github.com/bytebase/bytebase/backend/plugin/parser/base"-)--func (q *querySpanExtractor) extractPredicateColumnFromSearchCondition(- ctx parser.ISearch_conditionContext,-) error {- if ctx == nil {- return nil- }-- if ctx.Predicate() != nil {- if err := q.extractPredicateColumnFromPredicate(ctx.Predicate()); err != nil {- return err- }- }-- for _, s := range ctx.AllSearch_condition() {- if err := q.extractPredicateColumnFromSearchCondition(s); err != nil {- return err- }- }-- return nil-}--func (q *querySpanExtractor) extractPredicateColumnFromPredicate(- ctx parser.IPredicateContext,-) error {- if ctx == nil {- return nil- }-- if ctx.Subquery() != nil {- if err := q.extractPredicateColumnFromSubquery(ctx.Subquery()); err != nil {- return err- }- }-- if ctx.Freetext_predicate() != nil {- if err := q.extractPredicateColumnFromFreetextPredicate(ctx.Freetext_predicate()); err != nil {- return err- }- }-- for _, p := range ctx.AllExpression() {- if err := q.extractPredicateColumnFromExpression(p); err != nil {- return err- }- }-- if ctx.Expression_list_() != nil {- for _, e := range ctx.Expression_list_().AllExpression() {- if err := q.extractPredicateColumnFromExpression(e); err != nil {- return err- }- }- }-- return nil-}--func (q *querySpanExtractor) extractPredicateColumnFromFreetextPredicate(- ctx parser.IFreetext_predicateContext,-) error {- if ctx == nil {- return nil- }-- for _, c := range ctx.AllFull_column_name() {- r, err := q.tsqlIsFullColumnNameSensitive(c)- if err != nil {- return err- }- q.predicateColumns, _ = base.MergeSourceColumnSet(q.predicateColumns, r.SourceColumns)- }-- for _, e := range ctx.AllExpression() {- if err := q.extractPredicateColumnFromExpression(e); err != nil {- return err- }- }-- return nil-}--func (q *querySpanExtractor) extractPredicateColumnFromExpression(- ctx antlr.ParserRuleContext,-) error {- if ctx == nil {- return nil- }-- switch ctx := ctx.(type) {- case parser.IFull_column_nameContext:- r, err := q.tsqlIsFullColumnNameSensitive(ctx)- if err != nil {- return err- }- q.predicateColumns, _ = base.MergeSourceColumnSet(q.predicateColumns, r.SourceColumns)- case parser.ISubqueryContext:- if err := q.extractPredicateColumnFromSubquery(ctx); err != nil {- return err- }- default:- }-- var list []antlr.ParserRuleContext- for _, child := range ctx.GetChildren() {- if child, ok := child.(antlr.ParserRuleContext); ok {- list = append(list, child)- }- }-- for _, c := range list {- if err := q.extractPredicateColumnFromExpression(c); err != nil {- return err- }- }-- return nil-}--func (q *querySpanExtractor) extractPredicateColumnFromSubquery(- ctx parser.ISubqueryContext,-) error {- if ctx == nil {- return nil- }-- // Clone the extractor so the subquery sees the enclosing FROM clause as- // outer table sources. Without this, correlated subqueries like- // `WHERE EXISTS (SELECT 1 FROM t e WHERE e.col = outer.col)` cannot- // resolve the `outer` alias and fail with "no matching column".- cloneExtractor := &querySpanExtractor{- ctx: q.ctx,- defaultDatabase: q.defaultDatabase,- defaultSchema: q.defaultSchema,- ignoreCaseSensitive: q.ignoreCaseSensitive,- gCtx: q.gCtx,- ctes: q.ctes,- outerTableSources: append(q.outerTableSources, q.tableSourcesFrom...),- predicateColumns: make(base.SourceColumnSet),- }- tableSource, err := cloneExtractor.extractTSqlSensitiveFieldsFromSubquery(ctx)- if err != nil {- return errors.Wrapf(err, "failed to get query span for subquery")- }-- q.predicateColumns, _ = base.MergeSourceColumnSet(q.predicateColumns, cloneExtractor.predicateColumns)- for _, r := range tableSource.GetQuerySpanResult() {- q.predicateColumns, _ = base.MergeSourceColumnSet(q.predicateColumns, r.SourceColumns)- }-- return nil-}diff --git a/backend/plugin/parser/tsql/query_span_predicate_omni.go b/backend/plugin/parser/tsql/query_span_predicate_omni.gonew file mode 100644index 0000000000..85b3df91f0--- /dev/null+++ b/backend/plugin/parser/tsql/query_span_predicate_omni.go@@ -0,0 +1,171 @@+package tsql++import "github.com/bytebase/omni/mssql/ast"++// collectOmniPredicateColumnRefs walks an omni expression tree rooted at a+// boolean context (WHERE, HAVING, JOIN ON) and returns every *ast.ColumnRef+// reachable through predicate paths. It mirrors the traversal performed by+// the ANTLR-based helpers in query_span_predicate.go.+//+// Scope it covers:+// - direct column references in the expression+// - FULLTEXT predicate column lists (CONTAINS / FREETEXT)+// - nested subqueries: their WHERE / HAVING / ON / CTE bodies are descended+//+// Scope it does NOT cover (left to the extractor):+// - a subquery's SELECT-list output columns that flow back into the outer+// predicate via IN / scalar subquery / ANY|ALL — those require the+// extractor's table-source resolution.+// - table sources in a FROM clause other than their JOIN ON / derived-table+// WHERE.+func collectOmniPredicateColumnRefs(expr ast.ExprNode) []*ast.ColumnRef {+ var out []*ast.ColumnRef+ walkOmniPredicateExpr(expr, &out)+ return out+}++// collectOmniSelectPredicateColumnRefs returns predicate column refs reachable+// from a SelectStmt's WHERE, HAVING, JOIN ON conditions, set-op arms, and CTE+// bodies.+func collectOmniSelectPredicateColumnRefs(sel *ast.SelectStmt) []*ast.ColumnRef {+ var out []*ast.ColumnRef+ walkOmniSelectPredicates(sel, &out)+ return out+}++func walkOmniPredicateExpr(n ast.Node, out *[]*ast.ColumnRef) {+ if n == nil {+ return+ }+ switch v := n.(type) {+ case *ast.ColumnRef:+ *out = append(*out, v)+ case *ast.FullTextPredicate:+ if v.Columns != nil {+ for _, item := range v.Columns.Items {+ if cr, ok := item.(*ast.ColumnRef); ok {+ *out = append(*out, cr)+ }+ }+ }+ walkOmniPredicateExpr(v.Value, out)+ walkOmniPredicateExpr(v.LanguageTerm, out)+ case *ast.BinaryExpr:+ walkOmniPredicateExpr(v.Left, out)+ walkOmniPredicateExpr(v.Right, out)+ case *ast.UnaryExpr:+ walkOmniPredicateExpr(v.Operand, out)+ case *ast.BetweenExpr:+ walkOmniPredicateExpr(v.Expr, out)+ walkOmniPredicateExpr(v.Low, out)+ walkOmniPredicateExpr(v.High, out)+ case *ast.LikeExpr:+ walkOmniPredicateExpr(v.Expr, out)+ walkOmniPredicateExpr(v.Pattern, out)+ walkOmniPredicateExpr(v.Escape, out)+ case *ast.IsExpr:+ walkOmniPredicateExpr(v.Expr, out)+ case *ast.InExpr:+ walkOmniPredicateExpr(v.Expr, out)+ if v.List != nil {+ for _, item := range v.List.Items {+ if e, ok := item.(ast.ExprNode); ok {+ walkOmniPredicateExpr(e, out)+ }+ }+ }+ walkOmniPredicateExpr(v.Subquery, out)+ case *ast.ExistsExpr:+ if sel, ok := v.Subquery.(*ast.SelectStmt); ok {+ walkOmniSelectPredicates(sel, out)+ }+ case *ast.SubqueryExpr:+ if sel, ok := v.Query.(*ast.SelectStmt); ok {+ walkOmniSelectPredicates(sel, out)+ }+ case *ast.SubqueryComparisonExpr:+ walkOmniPredicateExpr(v.Left, out)+ if sel, ok := v.Subquery.(*ast.SelectStmt); ok {+ walkOmniSelectPredicates(sel, out)+ }+ case *ast.CaseExpr:+ walkOmniPredicateExpr(v.Arg, out)+ if v.WhenList != nil {+ for _, item := range v.WhenList.Items {+ if cw, ok := item.(*ast.CaseWhen); ok {+ walkOmniPredicateExpr(cw.Condition, out)+ walkOmniPredicateExpr(cw.Result, out)+ }+ }+ }+ walkOmniPredicateExpr(v.ElseExpr, out)+ case *ast.IifExpr:+ walkOmniPredicateExpr(v.Condition, out)+ walkOmniPredicateExpr(v.TrueVal, out)+ walkOmniPredicateExpr(v.FalseVal, out)+ case *ast.CoalesceExpr:+ walkOmniArgList(v.Args, out)+ case *ast.NullifExpr:+ walkOmniPredicateExpr(v.Left, out)+ walkOmniPredicateExpr(v.Right, out)+ case *ast.FuncCallExpr:+ walkOmniArgList(v.Args, out)+ case *ast.MethodCallExpr:+ walkOmniArgList(v.Args, out)+ case *ast.CastExpr:+ walkOmniPredicateExpr(v.Expr, out)+ case *ast.ConvertExpr:+ walkOmniPredicateExpr(v.Expr, out)+ walkOmniPredicateExpr(v.Style, out)+ case *ast.TryCastExpr:+ walkOmniPredicateExpr(v.Expr, out)+ case *ast.TryConvertExpr:+ walkOmniPredicateExpr(v.Expr, out)+ walkOmniPredicateExpr(v.Style, out)+ case *ast.ParenExpr:+ walkOmniPredicateExpr(v.Expr, out)+ case *ast.CollateExpr:+ walkOmniPredicateExpr(v.Expr, out)+ case *ast.AtTimeZoneExpr:+ walkOmniPredicateExpr(v.Expr, out)+ walkOmniPredicateExpr(v.TimeZone, out)+ default:+ }+}++func walkOmniArgList(list *ast.List, out *[]*ast.ColumnRef) {+ if list == nil {+ return+ }+ for _, item := range list.Items {+ if e, ok := item.(ast.ExprNode); ok {+ walkOmniPredicateExpr(e, out)+ }+ }+}++func walkOmniSelectPredicates(sel *ast.SelectStmt, out *[]*ast.ColumnRef) {+ if sel == nil {+ return+ }+ if sel.Larg != nil {+ walkOmniSelectPredicates(sel.Larg, out)+ }+ if sel.Rarg != nil {+ walkOmniSelectPredicates(sel.Rarg, out)+ }+ if sel.WithClause != nil && sel.WithClause.CTEs != nil {+ for _, item := range sel.WithClause.CTEs.Items {+ if c, ok := item.(*ast.CommonTableExpr); ok {+ if q, ok := c.Query.(*ast.SelectStmt); ok {+ walkOmniSelectPredicates(q, out)+ }+ }+ }+ }+ walkOmniPredicateExpr(sel.WhereClause, out)+ walkOmniPredicateExpr(sel.HavingClause, out)+ // JOIN ON conditions are intentionally NOT walked — the legacy ANTLR extractor+ // doesn't treat them as predicate columns, and downstream fixtures assume+ // that behavior. Re-enabling them is a separate decision.+}diff --git a/backend/plugin/parser/tsql/query_span_predicate_omni_test.go b/backend/plugin/parser/tsql/query_span_predicate_omni_test.gonew file mode 100644index 0000000000..4784f7c680--- /dev/null+++ b/backend/plugin/parser/tsql/query_span_predicate_omni_test.go@@ -0,0 +1,116 @@+package tsql++import (+ "slices"+ "testing"++ "github.com/bytebase/omni/mssql/ast"+ "github.com/stretchr/testify/require"+)++func TestCollectOmniSelectPredicateColumnRefs(t *testing.T) {+ type testCase struct {+ name string+ sql string+ // want lists the expected column names (ColumnRef.Column) reachable+ // from the SelectStmt's predicate contexts, order-insensitive.+ want []string+ }+ cases := []testCase{+ {"simple_eq", "SELECT * FROM t WHERE a = 1", []string{"a"}},+ {"col_vs_col", "SELECT * FROM t WHERE a > b", []string{"a", "b"}},+ {"and_or", "SELECT * FROM t WHERE a = 1 AND (b = 2 OR c = 3)", []string{"a", "b", "c"}},+ {"not", "SELECT * FROM t WHERE NOT (a > 0)", []string{"a"}},+ {"between", "SELECT * FROM t WHERE a BETWEEN b AND c", []string{"a", "b", "c"}},+ {"like", "SELECT * FROM t WHERE a LIKE b", []string{"a", "b"}},+ {"is_null", "SELECT * FROM t WHERE a IS NULL", []string{"a"}},+ {"in_literal_list", "SELECT * FROM t WHERE a IN (1, 2, 3)", []string{"a"}},+ {"in_subquery", "SELECT * FROM t WHERE a IN (SELECT c FROM t1 WHERE d = 1)", []string{"a", "d"}},+ {"exists", "SELECT * FROM t WHERE EXISTS (SELECT 1 FROM t1 WHERE d = e)", []string{"d", "e"}},+ {"contains_predicate", "SELECT * FROM t WHERE CONTAINS(a, 'foo')", []string{"a"}},+ {"freetext_predicate", "SELECT * FROM t WHERE FREETEXT(a, 'foo')", []string{"a"}},+ {"fulltext_star", "SELECT * FROM t WHERE CONTAINS(*, 'foo')", []string{}},+ {"case_expr", "SELECT * FROM t WHERE CASE WHEN a = 1 THEN b ELSE c END = 1", []string{"a", "b", "c"}},+ {"iif", "SELECT * FROM t WHERE IIF(a = 1, b, c) > 0", []string{"a", "b", "c"}},+ {"coalesce", "SELECT * FROM t WHERE COALESCE(a, b, c) = 0", []string{"a", "b", "c"}},+ {"nullif", "SELECT * FROM t WHERE NULLIF(a, b) IS NOT NULL", []string{"a", "b"}},+ {"cast_expr", "SELECT * FROM t WHERE CAST(a AS INT) > 5", []string{"a"}},+ {"convert_expr", "SELECT * FROM t WHERE CONVERT(INT, a) > 5", []string{"a"}},+ {"try_cast_expr", "SELECT * FROM t WHERE TRY_CAST(a AS INT) > 5", []string{"a"}},+ {"func_args", "SELECT * FROM t WHERE LEN(a) > LEN(b)", []string{"a", "b"}},+ {"paren_expr", "SELECT * FROM t WHERE ((a + b) > (c - d))", []string{"a", "b", "c", "d"}},+ {"collate_expr", "SELECT * FROM t WHERE a COLLATE Latin1_General_CS_AS = 'x'", []string{"a"}},+ // Note: JOIN ON is intentionally NOT collected by this helper (parity+ // with the legacy ANTLR extractor). Only WHERE/HAVING contribute.+ {"join_on", "SELECT * FROM t1 JOIN t2 ON t1.a = t2.b", []string{}},+ {"multi_join_on", "SELECT * FROM t1 JOIN t2 ON t1.a = t2.b JOIN t3 ON t2.c = t3.d", []string{}},+ {"having", "SELECT a, SUM(b) FROM t GROUP BY a HAVING SUM(b) > 10", []string{"b"}},+ {"correlated_subquery", "SELECT * FROM t1 WHERE EXISTS (SELECT 1 FROM t2 WHERE t2.a = t1.b)", []string{"a", "b"}},+ {"subquery_comparison_any", "SELECT * FROM t WHERE a > ANY (SELECT b FROM t1 WHERE c = 1)", []string{"a", "c"}},+ {"subquery_comparison_all", "SELECT * FROM t WHERE a <= ALL (SELECT b FROM t1 WHERE c = 1)", []string{"a", "c"}},+ {"nested_subquery", "SELECT * FROM t WHERE EXISTS (SELECT 1 FROM t1 WHERE EXISTS (SELECT 1 FROM t2 WHERE x = y))", []string{"x", "y"}},+ // The `c` inside MAX(c) lives in the subquery's SELECT list, not its WHERE,+ // so it is intentionally excluded by this helper's contract. Subquery OUTPUT+ // columns become outer predicate columns only after the extractor's+ // table-source resolution.+ {"scalar_subquery_in_where", "SELECT * FROM t WHERE (SELECT MAX(c) FROM t1 WHERE d = 1) > 5", []string{"d"}},+ {"no_predicate", "SELECT * FROM t", []string{}},+ {"set_op_union_both_have_where", "SELECT * FROM t WHERE a = 1 UNION SELECT * FROM t2 WHERE b = 2", []string{"a", "b"}},+ {"cte_body_where", "WITH c AS (SELECT * FROM t WHERE a = 1) SELECT * FROM c WHERE x = 2", []string{"a", "x"}},+ {"where_and_join_on", "SELECT * FROM t1 JOIN t2 ON t1.a = t2.b WHERE t1.c > 0", []string{"c"}},+ {"cross_apply_condition_none", "SELECT * FROM t1 CROSS APPLY fn(t1.a) AS x", []string{}},+ }+ for _, tc := range cases {+ t.Run(tc.name, func(t *testing.T) {+ stmts, err := ParseTSQLOmni(tc.sql)+ require.NoError(t, err, "parse failed for %s", tc.sql)+ require.NotEmpty(t, stmts)+ sel, ok := stmts[0].AST.(*ast.SelectStmt)+ require.Truef(t, ok, "want *SelectStmt, got %T", stmts[0].AST)++ refs := collectOmniSelectPredicateColumnRefs(sel)+ got := make([]string, 0, len(refs))+ for _, r := range refs {+ got = append(got, r.Column)+ }+ slices.Sort(got)+ want := append([]string{}, tc.want...)+ slices.Sort(want)+ require.ElementsMatch(t, want, got, "sql: %s", tc.sql)+ })+ }+}++func TestCollectOmniPredicateColumnRefs_QualifiedRefs(t *testing.T) {+ // Ensure qualified column refs (db.schema.table.col) are returned with+ // all qualifier fields intact so the extractor can resolve them later.+ sql := "SELECT * FROM db1.dbo.t WHERE db1.dbo.t.a = 1"+ stmts, err := ParseTSQLOmni(sql)+ require.NoError(t, err)+ sel, ok := stmts[0].AST.(*ast.SelectStmt)+ require.True(t, ok)+ refs := collectOmniSelectPredicateColumnRefs(sel)+ require.Len(t, refs, 1)+ r := refs[0]+ require.Equal(t, "db1", r.Database)+ require.Equal(t, "dbo", r.Schema)+ require.Equal(t, "t", r.Table)+ require.Equal(t, "a", r.Column)+}++func TestCollectOmniPredicateColumnRefs_Directly(t *testing.T) {+ // Exercise the non-Select entry point: caller hands in a bare ExprNode.+ sql := "SELECT * FROM t WHERE a > b AND c IS NULL"+ stmts, err := ParseTSQLOmni(sql)+ require.NoError(t, err)+ sel, ok := stmts[0].AST.(*ast.SelectStmt)+ require.True(t, ok)++ refs := collectOmniPredicateColumnRefs(sel.WhereClause)+ got := make([]string, 0, len(refs))+ for _, r := range refs {+ got = append(got, r.Column)+ }+ slices.Sort(got)+ require.ElementsMatch(t, []string{"a", "b", "c"}, got)+}diff --git a/docs/plans/2026-04-23-mssql-query-span-omni-migration.md b/docs/plans/2026-04-23-mssql-query-span-omni-migration.mdnew file mode 100644index 0000000000..851f6d7db5--- /dev/null+++ b/docs/plans/2026-04-23-mssql-query-span-omni-migration.md@@ -0,0 +1,330 @@+# MSSQL query_span_extractor omni migration — full plan++Date: 2026-04-23+Owner: jy+Scope: `backend/plugin/parser/tsql/query_span_extractor.go` (3743 LOC) + `query_span_predicate.go` (already has omni counterpart)++## Goal++End state: every code path in MSSQL query-span extraction runs on omni AST. `query_span_extractor.go` and `query_span_predicate.go` deleted. `tsql/omni.go`'s ANTLR fallback removed. No regression on `TestGetQuerySpan` YAML corpus or downstream consumers (advisors, masking, lineage).++**Merge policy**: all phases happen on this branch in isolation. The branch is merged *only after* the full migration is complete and the ANTLR path is gone. No intermediate release, no feature flag on main, no dual-path in production.++Non-goals: migrating non-query-span tsql files (resource_change, backup/restore, completion, diagnose, get_database_metadata). Those are tracked separately.++## Architecture++Single extractor. No dual-parse. Omni produces the AST; extractor consumes it.++```+GetQuerySpan+ → ParseTSQLOmni(stmt) → []omnimssql.Statement+ → pre-pass: collect DECLARE @t TABLE into gCtx.TempTables+ → omniQuerySpanExtractor.run(stmts)+ → classifyQueryType(stmts[0].AST) (already omni)+ → if not SELECT: return span with only Type + access tables+ → collectAccessTables(stmts[0].AST)+ → isMixedQuery check+ → extractFromSelectStmt(sel) → PseudoTable+ → predicate-columns collection (uses existing omni helper + subquery-output merge)+ → return QuerySpan+```++Struct layout stays the same as `querySpanExtractor`: `ctx, defaultDatabase, defaultSchema, ignoreCaseSensitive, gCtx, ctes, outerTableSources, tableSourcesFrom, predicateColumns`. The current Phase-0 `omniQuerySpanExtractor` embeds `*querySpanExtractor` for helper reuse; at cutover we rename the embedded struct to the sole `querySpanExtractor` and drop the wrapper.++## Shared helpers (keep, no parallel copies)++These take strings and are reused as-is:++- `tsqlFindTableSchemaByParts(linkedServer, db, schema, table)` — already extracted Phase 0+- `tsqlIsFieldSensitive(db, schema, table, column)` — pure string+- `tsqlGetAllFieldsOfTableInFromOrOuterCTE(db, schema, table)` — pure string+- `isIdentifierEqual(a, b)` — pure+- `unionTableSources(...TableSource)` — pure+- `isMixedQuery`, `isSystemResource` — pure+- `getColumnsFromCreateView(definition, db, schema)` — recurses into query-span on the view body; once the extractor is omni, this becomes self-hosting++Added during migration:++- `resolveOmniExpression(ast.ExprNode) (base.QuerySpanResult, error)` — the omni equivalent of `getQuerySpanResultFromExpr`+- `extractOmniFromSubquery(sel *ast.SelectStmt) (*base.PseudoTable, error)` — clone-scope descent+- `collectOmniAccessTables(ast.Node) base.SourceColumnSet` — mirrors the ANTLR accessTableListener++## Coverage map (every ANTLR entry point → omni destination)++| ANTLR function | LOC | Omni replacement | Phase |+|---|---:|---|---|+| `getQuerySpan` | ~85 | `getOmniQuerySpan` | 11 (cutover) |+| `tsqlSelectOnlyListener` walk | ~40 | explicit statement-level iteration | 11 |+| `EnterDeclare_statement` (temp table) | ~25 | `preCollectTempTables(stmts)` | 8 |+| `EnterDml_clause` | ~15 | dispatch inside `getOmniQuerySpan` | 11 |+| `extractTSqlSensitiveFieldsFromSelectStatementStandalone` | ~110 | folded into `extractFromSelectStmt` (With + set ops) | 1/5/6 |+| `extractTSqlSensitiveFieldsFromSelectStatement` | ~15 | same | 1 |+| `extractTSqlSensitiveFieldsFromQueryExpression` | ~50 | `extractFromSelectStmt` handles Larg/Rarg | 5 |+| `extractTSqlSensitiveFieldsFromQuerySpecification` | ~75 | `extractFromQuerySpec` | 1/2 |+| `extractTSqlSensitiveFieldsFromTableSources` | ~20 | `extractFromClause` | 2 |+| `extractTSqlSensitiveFieldsFromTableSource` | ~65 | `extractTableSourceWithJoins` | 2 |+| `extractTSqlSensitiveFieldsFromTableSourceItem` | ~75 | `extractTableSourceItem` | 2/3/7 |+| `extractTSqlSensitiveFieldsFromDerivedTable` | ~35 | `extractDerivedTable` | 3 |+| `extractTSqlSensitiveFieldsFromTableValueConstructor` | ~20 | `extractValuesClause` | 3 |+| `extractTSqlSensitiveFieldsFromSubquery` | ~5 | `extractFromSubquery` | 4 |+| `tsqlFindTableSchema` | ~95 | use `tsqlFindTableSchemaByParts` (shared) | 0 ✅ |+| `getColumnsFromCreateView` | ~35 | unchanged (self-hosting after cutover) | 11 |+| `tsqlGetAllFieldsOfTableInFromOrOuterCTE` | ~40 | shared | 0 ✅ |+| `tsqlIsFullColumnNameSensitive` | ~10 | replaced by direct `tsqlIsFieldSensitive` calls from `*ColumnRef` | 1 |+| `tsqlIsFieldSensitive` | ~90 | shared | 0 ✅ |+| `getQuerySpanResultFromExpr` | ~2525 | `resolveOmniExpression` | 1 (largest) |+| `unionTableSources` | ~20 | shared | 0 ✅ |+| `getAccessTables` + listener | ~80 | `collectOmniAccessTables` | 9 |+| `isMixedQuery` + `isSystemResource` | ~30 | shared | 0 ✅ |+| `splitTableNameIntoNormalizedParts` | ~30 | unused after omni (TableRef fields directly) | drop at 11 |+| `normalizeFullTableNameFallback` | ~30 | unused after omni | drop at 11 |+| `unquote` | ~15 | unused (omni already unquotes) | drop at 11 |+| `getSelectBodyFromCreateView` | ~30 | unused once omni parses views directly | drop at 11 |++## Phases++### Phase 0 — Foundation (DONE)++✅ `tsqlFindTableSchemaByParts` extracted+✅ `collectOmniPredicateColumnRefs` + test+✅ `omniQuerySpanExtractor` scaffold: single-table SELECT, WHERE predicates, bare/qualified/`*`/`t.*`/`AS alias` in target list+✅ 25 tests under `TestOmniQuerySpan_SupportedShapes/NotFound/UnsupportedShapes`++### Phase 1 — Expression resolver `resolveOmniExpression`++The largest single piece. Mirrors ANTLR's `getQuerySpanResultFromExpr` (2525 LOC). Each omni ExprNode type gets a case:++| Omni node | Source columns behavior |+|---|---|+| `*ColumnRef` | `tsqlIsFieldSensitive` lookup; IsPlainField=true |+| `*StarExpr` | expand via `tsqlGetAllFieldsOfTableInFromOrOuterCTE`; caller treats as multiple |+| `*Literal`, `*VariableRef`, `*StarExpr` (no qualifier in scalar context) | empty sources |+| `*BinaryExpr`, `*UnaryExpr`, `*BetweenExpr`, `*LikeExpr`, `*IsExpr`, `*InExpr` | merge sources of sub-exprs |+| `*CaseExpr`, `*IifExpr`, `*CoalesceExpr`, `*NullifExpr` | merge all branches |+| `*CastExpr`, `*ConvertExpr`, `*TryCastExpr`, `*TryConvertExpr` | unwrap |+| `*ParenExpr`, `*CollateExpr`, `*AtTimeZoneExpr` | unwrap |+| `*FuncCallExpr` | merge all Args; recurse into OverClause (partition/order exprs contribute) |+| `*MethodCallExpr` | merge Args |+| `*SubqueryExpr` (scalar subquery) | clone extractor with outerTableSources, extract, use first result column's sources; also merge subquery's predicate/source columns into outer's predicateColumns |+| `*SubqueryComparisonExpr` | Left sources + subquery output sources |+| `*ExistsExpr` | subquery's predicate columns (no value column) |+| `*FullTextPredicate` | Columns + Value sources |+| `*ResTarget`, `*SelectAssign` | unwrap Val; Name becomes result Name |+| `*GroupingSetsExpr`, `*RollupExpr`, `*CubeExpr` | merge args (appear in GROUP BY, not SELECT usually) |++Tests: one focused subtest per node type (≈ 30). Includes: arithmetic, nested CASE, CAST chain, COALESCE of columns, function with column args, aggregate with OVER (partition by different col than select), scalar subquery, correlated scalar subquery.++Special attention: IsPlainField flag semantics — true iff the expression is exactly a `*ColumnRef` (or `*ParenExpr` wrapping one). Check ANTLR behavior for edge cases.++### Phase 2 — Multi-table FROM + JOIN + ON++Handlers:++- `extractFromClause(*ast.List) ([]TableSource, error)` — iterate items, each is a table tree (JoinClause, TableRef, AliasedTableRef, etc.). Concatenate into tableSourcesFrom.+- `extractTableSourceWithJoins(node ast.Node) ([]TableSource, error)`+ - `*ast.JoinClause`: recurse Left + Right, concat; ON condition gets extracted to collect predicate columns (needs a separate pre-walk; the predicate helper already covers this in `collectOmniSelectPredicateColumnRefs`)+ - other → `extractTableSourceItem`++Scope note: JOIN `Left` and `Right` contribute to the same scope. Multiple comma-joined items in FromClause also all in same scope.++Tests: INNER, LEFT, RIGHT, FULL, CROSS JOIN; 3-table chain `a JOIN b ON ... JOIN c ON ...`; comma join with mixed JOIN; join with a derived table; USING clause (omni `JoinClause.Using` is a *List).++### Phase 3 — Derived tables + column alias lists++- `*AliasedTableRef` wrapping a `*SubqueryExpr` → derived table: recurse with clone-extractor that inherits tableSourcesFrom as outerTableSources; apply alias as PseudoTable.Name; apply column alias list positionally (rename each column).+- `*AliasedTableRef` wrapping a `*ValuesClause` → VALUES-derived table: each row is a tuple of expressions; column count from first row; Alias name list renames.+- Bare `*ValuesClause` in FromClause → same as above minus alias.+- Column alias list on a `*AliasedTableRef{Table: *TableRef}` → rename physical columns (rare but legal for TVFs).++Length-mismatch check: error mirrors ANTLR's `"number of column alias %d does not match the number of columns %d"`.++Tests: `(SELECT a,b FROM t) AS x`; `(SELECT a,b FROM t) AS x(c1,c2)`; `(VALUES (1,2),(3,4)) AS v(a,b)`; nested derived table; derived table with ORDER BY / GROUP BY inside.++### Phase 4 — Subqueries in expressions + scope management++- Scalar subquery `(SELECT ...)` appearing as ExprNode: resolveOmniExpression calls `extractFromSubquery(sq.Query.(*SelectStmt))` with a cloned extractor whose outerTableSources includes the outer scope's tableSourcesFrom. The scalar's "source" for outer attribution: first result column's SourceColumns. Also merge clone's predicateColumns into outer.+- `*ExistsExpr`: same clone-scope; contributes only predicate columns (no result value).+- `*InExpr.Subquery`, `*SubqueryComparisonExpr.Subquery`: same clone-scope; result's SourceColumns get merged into outer predicateColumns (this is exactly the "subquery output as outer predicate" that the current predicate helper deliberately skips — it gets filled in here).++After this phase, the predicate helper's limitation goes away because scope-cloned extraction captures both direct refs AND subquery output refs.++Tests: correlated subquery (inner references outer alias); nested 2/3-level correlations; EXISTS with predicate on inner + correlation to outer; IN subquery where outer and inner share column names.++### Phase 5 — Set operations++`*ast.SelectStmt.Op` ∈ {Union, Intersect, Except} with Larg/Rarg:++- Recurse into Larg → anchor results+- Recurse into Rarg → new results+- Length must match; for each column, merge SourceColumns. Anchor name wins.+- `All` flag doesn't affect column set, only runtime semantics++Tests: `A UNION B`; `A UNION ALL B`; `A INTERSECT B EXCEPT C` (chained — omni represents as a nested SelectStmt tree); set op inside a derived table.++### Phase 6 — CTE (WITH clause)++Pre-process WithClause before the body:++- For each `*ast.CommonTableExpr`:+ - Clone extractor with empty tableSourcesFrom (CTE can see previous CTEs via q.ctes but not the outer FROM)+ - Extract CTE body (recursion: CTE body is a `*ast.SelectStmt`)+ - Apply column alias list if `.Columns` is non-empty (positional rename)+ - Build a PseudoTable with CTE name + result columns+ - Push to `q.ctes` (order matters: later CTEs can see earlier)++Recursive CTE: not explicitly handled by ANTLR code either; we match that behavior.++Tests: simple CTE; CTE with explicit column list; chain of CTEs where CTE2 uses CTE1; CTE shadowing a physical table name; deeply nested CTE references.++### Phase 7 — Special table sources++- **PIVOT**: `*ast.PivotExpr{Source, AggFunc, ForCol, InValues, Alias}` — result columns = source's non-pivot columns (need to subtract the FOR column) + one column per IN value, each pivoted aggregate. Source columns for pivoted columns = the AggFunc's source columns. This is new semantic work; ANTLR currently returns "not supported yet" — so this is an upgrade from current behavior. Start by emulating ANTLR: return errOmniUnsupported, and add a TODO. Real PIVOT support is a separate follow-up.+- **UNPIVOT**: similar — emulate current ANTLR (unsupported).+- **CROSS APPLY / OUTER APPLY**: `*JoinClause{Type: CrossApply|OuterApply}`. The Right is a table expression (often a TVF or subquery) that can reference Left columns (correlated). Treat as a join where Right is evaluated in a scope that includes Left. Contribution to tableSourcesFrom: both sides.+- **Table-valued function call (TVF)**: `*FuncCallExpr` appearing in FROM position. Look up return signature — complicated, requires schema metadata for user TVFs. For system TVFs (e.g. `OPENJSON`, `STRING_SPLIT`): hardcode return shape. For unknown: `errOmniUnsupported` (ANTLR does the same for most).+- **Temp tables**: `#t` → PseudoTable from gCtx.TempTables; `@t` (TableVarRef) → same lookup.+- **TableVarMethodCallRef** (XML `.nodes()`): emits a table of XML fragments; columns named after the method's alias columns; no physical source.+- **CHANGETABLE**: return PseudoTable (stats only, no sensitive data flow). Matches ANTLR behavior.++Tests: temp table declared + used; @t variable table used; XML nodes method; CROSS APPLY with TVF; OUTER APPLY; `OPENJSON(...)`; CHANGETABLE.++### Phase 8 — Multi-statement pre-pass for temp tables++Current ANTLR runs the listener across ALL statements in `GetQuerySpan`, picking up `DECLARE @t TABLE` from previous statements before processing the final SELECT. Omni needs the same:++- `preCollectTempTables(stmts []omnimssql.Statement)` walks every stmt+- For each `*ast.DeclareStmt` with `VariableDecl.IsTable == true`, extract columns from `VariableDecl.TableDef`, build a PseudoTable, put into `gCtx.TempTables[name]`++Also handle `CREATE TABLE #temp (...)`: omni `*CreateTableStmt` where name starts with `#`. Same pre-collect.++Tests: `DECLARE @t TABLE(a INT, b INT); SELECT a FROM @t`; `CREATE TABLE #tmp (id INT); SELECT id FROM #tmp`.++### Phase 9 — Access tables++`collectOmniAccessTables(root ast.Node) base.SourceColumnSet`:++- `ast.Inspect` to walk the whole AST+- Collect `*ast.TableRef` seen in table-source positions (FromClause, JoinClause, PivotExpr.Source, DerivedTable, CTE body FROM, subquery FROM)+- NOT inside: FuncCallExpr.Name (which happens to be *TableRef for qualified function names — but those are functions, not tables)++Distinguishing is tricky. Approach: rather than inspect-everywhere, walk only through table-source-bearing fields explicitly. Start from SelectStmt, descend into FromClause + subqueries + CTEs. Skip TargetList, WhereClause expressions (unless we enter a subquery's FromClause via a SubqueryExpr).++Parity check: run both ANTLR's `getAccessTables` and omni's `collectOmniAccessTables` on every fixture, assert set-equality.++### Phase 10 — INTO, FOR XML/JSON, OPTION, hints++- `SELECT INTO target_table`: target becomes a write resource; not part of sensitive field extraction but appears in access tables as a target (current ANTLR doesn't specially mark this; we match)+- `FOR XML`, `FOR JSON`: result becomes a single-column text blob; current ANTLR extractor doesn't specially handle this (just runs regular extraction); we match. Possible follow-up: single-column collapse. Note for later.+- `OPTION (...)` / table hints: ignore; they don't affect column flow+- `TABLESAMPLE`: ignore; row-level only++Tests: SELECT INTO #tmp FROM t (temp destination); FOR XML AUTO (ensure no crash, output matches current behavior).++### Phase 11 — Cutover++1. `GetQuerySpan` stops using ANTLR. Entry point switches to `getOmniQuerySpan`.+2. Delete `query_span_predicate.go`.+3. Delete all ANTLR-context-taking methods from `querySpanExtractor` (they're duplicated now as omni versions).+4. Rename `omniQuerySpanExtractor` → `querySpanExtractor`; merge structs.+5. Remove unused ANTLR-only helpers: `normalizeFullTableNameFallback`, `splitTableNameIntoNormalizedParts`, `unquote`, `getSelectBodyFromCreateView`, `NormalizeTSQLIdentifier` (if only used here — grep).+6. `omni.go`'s `AsANTLRAST()` lazy ANTLR fallback: check if any other tsql file still uses it; drop if not.++All 29 YAML fixtures must pass. The probe tests (`TestOmniMigrationProbe`) must still pass. New extractor tests all green. Lint clean. Build clean.++### Phase 12 — Cleanup++- Delete the now-unused `query_span_extractor.go` ANTLR code (once fully replaced)+- Remove `query_span_omni_probe_test.go` if it's now redundant with Phase 1-10 tests (or keep as a regression net — decide on cutover)+- Update `AGENTS.md` / skill docs if they still reference the old file layout+- Open follow-up tickets for items deferred: PIVOT/UNPIVOT result-column inference, TVF metadata, SELECT INTO target marking, FOR XML single-column collapse++## Test strategy++### Existing-fixture parity (gating at cutover only)+`TestGetQuerySpan` YAML corpus (29 cases across 6 files) MUST pass unchanged *after* Phase 11 cutover. Before cutover, the omni extractor is a separate code path that only runs via its own unit tests; ANTLR remains primary. Between phases, we don't need to keep the fixtures green via the omni path — that happens in one shot at cutover.++### Per-phase unit tests+Each phase adds a focused block in `query_span_extractor_omni_test.go`. Target 15-30 cases per phase, grouped by phase name:++- Phase 1: `TestOmniQuerySpan_Expressions` — one subtest per ExprNode type + edge cases+- Phase 2: `TestOmniQuerySpan_Joins`+- Phase 3: `TestOmniQuerySpan_DerivedTables`+- Phase 4: `TestOmniQuerySpan_Subqueries`+- Phase 5: `TestOmniQuerySpan_SetOps`+- Phase 6: `TestOmniQuerySpan_CTE`+- Phase 7: `TestOmniQuerySpan_SpecialTableSources`+- Phase 8: `TestOmniQuerySpan_TempTables`+- Phase 9: `TestOmniQuerySpan_AccessTables`+- Phase 10: `TestOmniQuerySpan_IntoForClauses`++### Differential test (development aid, not CI gate)+Add a test-only helper `diffQuerySpan(ctx, stmt, ...)` that runs both ANTLR and omni paths on the same input and reports structural diffs (result count, names, source column sets, predicate columns, access tables, NotFoundError parity). Run it locally across the full YAML corpus as a progress thermometer: `TestQuerySpan_AntlrOmniParity`. Used to track "how much of the corpus omni currently matches". Expected to show diffs until Phase 10 is complete. At Phase 11, it must show 0 diffs — that's the cutover gate. Delete the test as part of Phase 12 cleanup.++### Oracle test (optional, post-cutover)+Add testcontainer MSSQL. For each YAML fixture, execute the SQL against MSSQL with the metadata we mocked, capture `sys.dm_exec_describe_first_result_set` or similar. Verify our extracted column names match the server's view of the result. Not a blocker for cutover but valuable as a follow-up.++## How the two paths coexist on-branch (no dispatch)++No flag, no env var, no runtime dispatch. The omni extractor lives as a parallel package-internal implementation in `query_span_extractor_omni.go`. `GetQuerySpan` keeps calling the ANTLR path as it does today, unchanged.++Validation of omni progress during development:+1. **Unit tests** (`TestOmniQuerySpan_*`) — direct calls into `newOmniQuerySpanExtractor(...).getOmniQuerySpan()`, bypassing `GetQuerySpan`. This is where every phase's coverage is proven.+2. **Parity harness** (`TestQuerySpan_AntlrOmniParity`, dev-only) — table-driven over the YAML corpus; calls both ANTLR via `GetQuerySpan` and omni directly; reports diffs. Useful as a thermometer; not wired to CI until cutover.++`errOmniUnsupported` remains as a development signal (so unit tests can distinguish "not yet implemented" from "real bug") but is never observable from outside the package. After Phase 10 it should stop appearing for every supported shape, at which point the cutover is safe.++At Phase 11, `GetQuerySpan` is rewritten to call the omni extractor directly (no fallback). `errOmniUnsupported` is deleted along with all ANTLR extraction code.++## Risk register++| # | Risk | Likelihood | Impact | Mitigation |+|---|---|---|---|---|+| R1 | Omni parser chokes on an unusual fixture form | Low (probe says 29/29) | High | Probe test already in place; extend before each phase |+| R2 | Subquery scope bug: outer ref resolving to wrong alias | Medium | High | Mirror ANTLR's clone-extractor pattern byte-for-byte; explicit test cases for 3-level correlated subquery with alias shadowing |+| R3 | IsPlainField flag diverges (ANTLR marks `(col)` as plain but `col+0` as not) | Medium | Medium | Mirror exact ANTLR logic in resolveOmniExpression; diff test will catch |+| R4 | CTE scoping / ordering differs (omni might reorder CTEs) | Low | Medium | Use CTE items in omni order (matches SQL order); test chain of 3 CTEs |+| R5 | Temp table pre-pass misses a form (CREATE TABLE #t with constraints, SELECT INTO #t) | Medium | Medium | Handle both DeclareStmt and CreateTableStmt; test both |+| R6 | Access-tables set not identical to ANTLR's accessTableListener | Medium | Medium | Diff test on every fixture + ad-hoc corpus |+| R7 | View body parsing recursion — omni-in-omni infinite loop if view definition is malformed | Low | Low | Existing code uses `newQ.getQuerySpan(q.ctx, selectBody)`; recursion limit handled by parser; same pattern reused |+| R8 | PIVOT/UNPIVOT regression: current ANTLR returns empty for pivot, omni might produce something different | Low | Low | Emulate ANTLR's current behavior (unsupported) in Phase 7; PIVOT support is a separate follow-up |+| R9 | Linked server (`srv.db.schema.obj`) currently errors; omni preserves Server field, we might accidentally start resolving it | Low | Medium | Keep the `if server != ""` early return in tsqlFindTableSchemaByParts |+| R10 | CONTAINS/FREETEXT now a dedicated `*FullTextPredicate` node (new since 569d6f7); ensure resolveOmniExpression / predicate helpers handle it | Low (already tested) | Medium | Predicate helper already handles it; resolveOmniExpression must include the case |++## Timeline estimate++Rough engineer-days (focused, interruptible):++- Phase 1 (expressions): 2d — the big one, ~30 node types, IsPlainField edge cases+- Phase 2 (joins): 0.5d+- Phase 3 (derived tables + column alias): 0.5d+- Phase 4 (subqueries + scope): 1d — scope cloning is tricky+- Phase 5 (set ops): 0.5d+- Phase 6 (CTE): 0.5d+- Phase 7 (special table sources): 1d — CROSS APPLY with correlated TVF needs care+- Phase 8 (multi-statement pre-pass): 0.5d+- Phase 9 (access tables parity): 0.5d+- Phase 10 (INTO / FOR / hints): 0.25d+- Phase 11 (cutover + cleanup): 0.75d+- Phase 12 (housekeeping): 0.25d++Total: ~8 engineer-days. Parity test fixed after each phase, keeping CI green.++## Open questions++1. **Should `query_span_omni_probe_test.go` stay?** It duplicates some checks. Recommend: keep through cutover for parse-coverage gating; delete in Phase 12 if the per-phase tests cover everything.+2. **PIVOT full support in this migration or later?** Current ANTLR is a no-op ("not supported"). Recommend: keep no-op parity in Phase 7; real PIVOT support = new project.+3. **TVF return-shape metadata**: do we add a gCtx function for it (similar to ListDatabaseNamesFunc)? Recommend: yes, but defer to a separate ticket; the migration uses an empty fallback meanwhile.+4. **Order of `tableSourcesFrom` across comma-join vs JOIN**: ANTLR current code `append`s as it walks left-to-right, JOIN unwraps from inside. Match exactly.+5. **Case-sensitivity flag threading**: The `ignoreCaseSensitive` parameter in `GetQuerySpan` is user-controlled. All identifier comparisons inside omni-extracted data must honor it. Easy (already inherited via embedded `*querySpanExtractor`), just a reminder.++## Definition of done (merge gate)++- [ ] Phases 1-10 each: code + per-phase unit tests + lint clean + `go build` ok (on-branch, incremental)+- [ ] Parity harness `TestQuerySpan_AntlrOmniParity` shows 0 diffs across the full YAML corpus+- [ ] Phase 11 cutover: `GetQuerySpan` calls omni extractor directly; no fallback, no env var; `query_span_predicate.go` deleted; ANTLR-context-taking methods removed+- [ ] `TestGetQuerySpan` (29 fixtures) passes unchanged+- [ ] Phase 12: no reference to `antlr4-go` remains in `query_span*.go`; `omni.go`'s `AsANTLRAST` fallback reviewed and dropped if no other tsql file uses it; parity harness deleted+- [ ] `go build ./backend/bin/server/main.go` passes+- [ ] `golangci-lint run ./backend/plugin/parser/tsql/...` clean+- [ ] Only then: merge branch