Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat: cut BigQuery + Spanner parsing to the omni googlesql parser (#20569)
h3n4l5005e28beaed
diff --git a/backend/plugin/parser/bigquery/bigquery.go b/backend/plugin/parser/bigquery/bigquery.godeleted file mode 100644index 2c001eb3b4..0000000000--- a/backend/plugin/parser/bigquery/bigquery.go+++ /dev/null@@ -1,80 +0,0 @@-package bigquery--import (- "strings"-- "github.com/antlr4-go/antlr/v4"-- parser "github.com/bytebase/parser/googlesql"-- storepb "github.com/bytebase/bytebase/backend/generated-go/store"- "github.com/bytebase/bytebase/backend/plugin/parser/base"- "github.com/bytebase/bytebase/backend/utils"-)--// ParseBigQuerySQL parses the given SQL statement by using antlr4. Returns a list of AST and token stream if no error.-func ParseBigQuerySQL(statement string) ([]*base.ANTLRAST, error) {- stmts, err := SplitSQL(statement)- if err != nil {- return nil, err- }-- var result []*base.ANTLRAST- for _, stmt := range stmts {- if stmt.Empty {- continue- }-- ast, err := parseSingleBigQuerySQL(stmt.Text, stmt.BaseLine())- if err != nil {- return nil, err- }- result = append(result, ast)- }-- return result, nil-}--func parseSingleBigQuerySQL(statement string, baseLine int) (*base.ANTLRAST, error) {- statement = strings.TrimRightFunc(statement, utils.IsSpaceOrSemicolon) + "\n;"- inputStream := antlr.NewInputStream(statement)- lexer := parser.NewGoogleSQLLexer(inputStream)- stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)- p := parser.NewGoogleSQLParser(stream)-- // Remove default error listener and add our own error listener.- startPosition := &storepb.Position{Line: int32(baseLine) + 1}- lexer.RemoveErrorListeners()- lexerErrorListener := &base.ParseErrorListener{- Statement: statement,- StartPosition: startPosition,- }- lexer.AddErrorListener(lexerErrorListener)-- p.RemoveErrorListeners()- parserErrorListener := &base.ParseErrorListener{- Statement: statement,- StartPosition: startPosition,- }- p.AddErrorListener(parserErrorListener)-- p.BuildParseTrees = true-- tree := p.Root()-- if lexerErrorListener.Err != nil {- return nil, lexerErrorListener.Err- }-- if parserErrorListener.Err != nil {- return nil, parserErrorListener.Err- }-- ast := &base.ANTLRAST{- StartPosition: startPosition,- Tree: tree,- Tokens: stream,- }-- return ast, nil-}diff --git a/backend/plugin/parser/bigquery/dignose.go b/backend/plugin/parser/bigquery/dignose.godeleted file mode 100644index 506556c015..0000000000--- a/backend/plugin/parser/bigquery/dignose.go+++ /dev/null@@ -1,65 +0,0 @@-package bigquery--import (- "context"- "strings"- "unicode"-- "github.com/antlr4-go/antlr/v4"- parser "github.com/bytebase/parser/googlesql"-- "github.com/bytebase/bytebase/backend/generated-go/store"- "github.com/bytebase/bytebase/backend/plugin/parser/base"-)--func init() {- base.RegisterDiagnoseFunc(store.Engine_BIGQUERY, Diagnose)-}--func Diagnose(_ context.Context, _ base.DiagnoseContext, statement string) ([]base.Diagnostic, error) {- diagnostics := make([]base.Diagnostic, 0)- syntaxError := parseGoogleSQLStatement(statement)- if syntaxError != nil {- diagnostics = append(diagnostics, base.ConvertSyntaxErrorToDiagnostic(syntaxError, statement))- }-- return diagnostics, nil-}--func parseGoogleSQLStatement(statement string) *base.SyntaxError {- trimmedStatement := strings.TrimRightFunc(statement, unicode.IsSpace)- if len(trimmedStatement) > 0 && !strings.HasSuffix(trimmedStatement, ";") {- // Add a semicolon to the end of the statement to allow users to omit the semicolon- // for the last statement in the script.- statement += ";"- }- inputStream := antlr.NewInputStream(statement)- lexer := parser.NewGoogleSQLLexer(inputStream)- stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)-- p := parser.NewGoogleSQLParser(stream)- lexerErrorListener := &base.ParseErrorListener{- Statement: statement,- }- lexer.RemoveErrorListeners()- lexer.AddErrorListener(lexerErrorListener)-- parserErrorListener := &base.ParseErrorListener{- Statement: statement,- }- p.RemoveErrorListeners()- p.AddErrorListener(parserErrorListener)-- p.BuildParseTrees = false-- _ = p.Root()-- if lexerErrorListener.Err != nil {- return lexerErrorListener.Err- }-- if parserErrorListener.Err != nil {- return parserErrorListener.Err- }- return nil-}diff --git a/backend/plugin/parser/bigquery/plugin.go b/backend/plugin/parser/bigquery/plugin.gonew file mode 100644index 0000000000..1c2b0d4e7f--- /dev/null+++ b/backend/plugin/parser/bigquery/plugin.go@@ -0,0 +1,34 @@+// Package bigquery registers the BigQuery engine handlers, backed by the shared+// omni-based GoogleSQL implementation (backend/plugin/parser/googlesql) with the+// BigQuery dialect configuration. See that package for the behavior contract and+// the masking notes; the differential corpus (test-data/query-span/standard.yaml,+// recorded from the legacy ANTLR resolver) and the leak-pin tests in this package+// pin the BigQuery-specific behavior.+package bigquery++import (+ "github.com/bytebase/omni/googlesql/analysis"++ storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/plugin/parser/googlesql"+)++// config carries the BigQuery dialect deltas: the written dataset is the+// DATABASE (default fill-in, single unnamed metadata schema), system queries+// keep their table-level resources, SET classifies as Select (the legacy+// listener's "treat SAFE SET as select"), and set-operation star-merge names+// are upper-cased (the legacy BigQuery extractor's rendering).+var config = googlesql.Config{+ Dialect: analysis.DialectBigQuery,+ SetStatementIsSelect: true,+ UppercaseStarMergeName: true,+}++var handlers = googlesql.Register(storepb.Engine_BIGQUERY, config)++// SplitSQL is the registered splitter, re-exported for the tests in this package.+var SplitSQL = handlers.SplitSQL++// GetQuerySpan is the registered query-span handler, re-exported for the tests+// in this package.+var GetQuerySpan = handlers.GetQuerySpandiff --git a/backend/plugin/parser/bigquery/query_span.go b/backend/plugin/parser/bigquery/query_span.godeleted file mode 100644index 43db3423c6..0000000000--- a/backend/plugin/parser/bigquery/query_span.go+++ /dev/null@@ -1,28 +0,0 @@-package bigquery--import (- "context"-- storepb "github.com/bytebase/bytebase/backend/generated-go/store"- "github.com/bytebase/bytebase/backend/plugin/parser/base"-)--func init() {- base.RegisterGetQuerySpan(storepb.Engine_BIGQUERY, GetQuerySpan)-}--// GetQuerySpan returns the query span for the given statement.-func GetQuerySpan(- ctx context.Context,- gCtx base.GetQuerySpanContext,- stmt base.Statement,- database, _ string,- ignoreCaseSensitive bool,-) (*base.QuerySpan, error) {- q := newQuerySpanExtractor(database, gCtx, ignoreCaseSensitive)- querySpan, err := q.getQuerySpan(ctx, stmt.Text)- if err != nil {- return nil, err- }- return querySpan, nil-}diff --git a/backend/plugin/parser/bigquery/query_span_extractor.go b/backend/plugin/parser/bigquery/query_span_extractor.godeleted file mode 100644index 979fbe1b90..0000000000--- a/backend/plugin/parser/bigquery/query_span_extractor.go+++ /dev/null@@ -1,1269 +0,0 @@-package bigquery--import (- "context"- "slices"- "strings"-- "github.com/antlr4-go/antlr/v4"- parser "github.com/bytebase/parser/googlesql"-- "github.com/pkg/errors"-- "github.com/bytebase/bytebase/backend/plugin/parser/base"-)--type querySpanExtractor struct {- ctx context.Context- defaultDatabase string-- gCtx base.GetQuerySpanContext-- ctes []*base.PseudoTable-- // outerTableSources is the table sources from the outer query span.- // it's used to resolve the column name in the correlated sub-query.- outerTableSources []base.TableSource-- // tableSourceFrom is the table sources from the from clause.- tableSourceFrom []base.TableSource-}--func newQuerySpanExtractor(defaultDatabase string, gCtx base.GetQuerySpanContext, _ bool) *querySpanExtractor {- return &querySpanExtractor{- defaultDatabase: defaultDatabase,- gCtx: gCtx,- }-}--func (q *querySpanExtractor) getQuerySpan(ctx context.Context, stmt string) (*base.QuerySpan, error) {- parseResults, err := ParseBigQuerySQL(stmt)- if err != nil {- return nil, err- }- if len(parseResults) == 0 {- return &base.QuerySpan{- SourceColumns: base.SourceColumnSet{},- Results: []base.QuerySpanResult{},- }, nil- }- if len(parseResults) != 1 {- return nil, errors.Errorf("expecting only one statement to get query span, but got %d", len(parseResults))- }- tree := parseResults[0].Tree- q.ctx = ctx- accessTables, err := getAccessTables(q.defaultDatabase, tree)- if err != nil {- return nil, err- }- allSystems, mixed := isMixedQuery(accessTables)- if mixed {- return nil, base.MixUserSystemTablesError- }-- queryTypeListener := &queryTypeListener{- allSystems: allSystems,- result: base.QueryTypeUnknown,- }- antlr.ParseTreeWalkerDefault.Walk(queryTypeListener, tree)- if queryTypeListener.err != nil {- return nil, queryTypeListener.err- }- if queryTypeListener.result != base.Select {- return &base.QuerySpan{- Type: queryTypeListener.result,- SourceColumns: base.SourceColumnSet{},- Results: []base.QuerySpanResult{},- }, nil- }-- // TODO(zp): statement type check.- querySpanResult, err := q.getQuerySpanResult(tree.(*parser.RootContext).Stmts().AllUnterminated_sql_statement()[0].Sql_statement_body())- if err != nil {- return nil, err- }-- return &base.QuerySpan{- Type: base.Select,- Results: querySpanResult,- SourceColumns: accessTables,- }, nil-}--func (q *querySpanExtractor) getQuerySpanResult(tree parser.ISql_statement_bodyContext) ([]base.QuerySpanResult, error) {- if tree.Query_statement() == nil {- return nil, errors.Errorf("unsupported non-query statement")- }-- tableSource, err := q.extractTableSourceFromQuery(tree.Query_statement().Query())- if err != nil {- return nil, err- }-- return tableSource.GetQuerySpanResult(), nil-}--func (q *querySpanExtractor) extractTableSourceFromQuery(query parser.IQueryContext) (base.TableSource, error) {- queryWithoutPipe := query.Query_without_pipe_operators()- if queryWithoutPipe == nil {- return nil, errors.Errorf("unsupported query with pipe operators")- }- return q.extractTableSourceFromQueryWithoutPipe(queryWithoutPipe)-}--func (q *querySpanExtractor) extractTableSourceFromQueryWithoutPipe(queryWithoutPipe parser.IQuery_without_pipe_operatorsContext) (base.TableSource, error) {- originalCTELength := len(q.ctes)- defer func() {- q.ctes = q.ctes[:originalCTELength]- }()- if queryWithoutPipe.With_clause() != nil {- if err := q.recordCTE(queryWithoutPipe.With_clause()); err != nil {- return nil, err- }- }- return q.extractTableSourceFromQueryPrimaryOrSetOperation(queryWithoutPipe.Query_primary_or_set_operation())-}--func (q *querySpanExtractor) recordRecursiveCTE(aliasedQuery parser.IAliased_queryContext) error {- cteName := unquoteIdentifierByRule(aliasedQuery.Identifier())- query := aliasedQuery.Parenthesized_query().Query().Query_without_pipe_operators()- if query.With_clause() != nil {- return errors.Errorf("WITH is not allowed inside WITH RECURSIVE")- }- if query.Query_primary_or_set_operation().Query_set_operation() == nil {- return q.recordNonRecursiveCTE(aliasedQuery)- }- prefix := query.Query_primary_or_set_operation().Query_set_operation().Query_set_operation_prefix()- anchor, err := q.extractTableSourceFromQueryPrimary(prefix.Query_primary())- if err != nil {- return err- }- // XXX(zp): How about two union?- recursiveItem := prefix.AllQuery_set_operation_item()[0]- tempCte := &base.PseudoTable{- Name: cteName,- Columns: anchor.GetQuerySpanResult(),- }- q.ctes = append(q.ctes, tempCte)- originalSize := len(q.ctes)- for {- originalSize := len(q.ctes)- recursivePartTableSource, err := q.extractTableSourceFromQueryPrimary(recursiveItem.Query_primary())- if err != nil {- return err- }- anchorQuerySpanResults := q.ctes[originalSize-1].GetQuerySpanResult()- recursivePartQuerySpanResults := recursivePartTableSource.GetQuerySpanResult()- if len(anchorQuerySpanResults) != len(recursivePartQuerySpanResults) {- return errors.Errorf("recursive cte %s clause returns %d fields, but anchor clause returns %d fields", cteName, len(anchorQuerySpanResults), len(recursivePartQuerySpanResults))- }- changed := false- for i := range anchorQuerySpanResults {- var hasChange bool- anchorQuerySpanResults[i].SourceColumns, hasChange = base.MergeSourceColumnSet(anchorQuerySpanResults[i].SourceColumns, recursivePartQuerySpanResults[i].SourceColumns)- changed = changed || hasChange- }- tempCte := &base.PseudoTable{- Name: cteName,- Columns: anchorQuerySpanResults,- }- q.ctes = q.ctes[:originalSize-1]- if !changed {- break- }- q.ctes = append(q.ctes, tempCte)- }- q.ctes = q.ctes[:originalSize-1]- q.ctes = append(q.ctes, tempCte)- return nil-}--func (q *querySpanExtractor) recordNonRecursiveCTE(aliasedQuery parser.IAliased_queryContext) error {- cteName := unquoteIdentifierByRule(aliasedQuery.Identifier())- query := aliasedQuery.Parenthesized_query().Query()- tableSource, err := q.extractNormalCTE(query)- if err != nil {- return err- }- q.ctes = append(q.ctes, &base.PseudoTable{- Name: cteName,- Columns: tableSource.GetQuerySpanResult(),- })- return nil-}--func (q *querySpanExtractor) recordCTE(withClause parser.IWith_clauseContext) error {- allAliasedQuery := withClause.AllAliased_query()- recursive := withClause.RECURSIVE_SYMBOL() != nil- for _, aliasedQuery := range allAliasedQuery {- // TODO(zp): Actually, BigQuery do not rely on the RECURSIVE keyword, instead, it detects the recursive CTE- // by the reference of the CTE itself in the CTE body. Also, check other engines.- if recursive {- if err := q.recordRecursiveCTE(aliasedQuery); err != nil {- return err- }- } else {- if err := q.recordNonRecursiveCTE(aliasedQuery); err != nil {- return err- }- }- }- return nil-}--func (q *querySpanExtractor) extractNormalCTE(query parser.IQueryContext) (base.TableSource, error) {- querySpanExtractor := &querySpanExtractor{- ctx: q.ctx,- defaultDatabase: q.defaultDatabase,- gCtx: q.gCtx,- ctes: q.ctes,- }- tableSource, err := querySpanExtractor.extractTableSourceFromQuery(query)- if err != nil {- return nil, err- }- return tableSource, nil-}--func (q *querySpanExtractor) extractTableSourceFromQueryPrimaryOrSetOperation(queryPrimaryOrSetOperation parser.IQuery_primary_or_set_operationContext) (base.TableSource, error) {- if queryPrimaryOrSetOperation.Query_primary() != nil {- return q.extractTableSourceFromQueryPrimary(queryPrimaryOrSetOperation.Query_primary())- }- if queryPrimaryOrSetOperation.Query_set_operation() != nil {- return q.extractTableSourceFromQuerySetOperation(queryPrimaryOrSetOperation.Query_set_operation())- }- return nil, nil-}--func (q *querySpanExtractor) extractTableSourceFromQuerySetOperation(querySetOperation parser.IQuery_set_operationContext) (base.TableSource, error) {- tableSource, err := q.extractTableSourceFromQueryPrimary(querySetOperation.Query_set_operation_prefix().Query_primary())- if err != nil {- return nil, err- }- leftSpanResults := tableSource- for _, item := range querySetOperation.Query_set_operation_prefix().AllQuery_set_operation_item() {- newQ := &querySpanExtractor{- ctx: q.ctx,- defaultDatabase: q.defaultDatabase,- gCtx: q.gCtx,- ctes: q.ctes,- }- rightSpanResults, err := newQ.extractTableSourceFromQueryPrimary(item.Query_primary())- if err != nil {- return nil, err- }- leftQuerySpanResult, rightQuerySpanResult := leftSpanResults.GetQuerySpanResult(), rightSpanResults.GetQuerySpanResult()- if len(leftQuerySpanResult) != len(rightQuerySpanResult) {- return nil, errors.Errorf("left select has %d columns, but right select has %d columns", len(leftQuerySpanResult), len(rightQuerySpanResult))- }- var result []base.QuerySpanResult- for i, leftSpanResult := range leftQuerySpanResult {- rightSpanResult := rightQuerySpanResult[i]- newResourceColumns, _ := base.MergeSourceColumnSet(leftSpanResult.SourceColumns, rightSpanResult.SourceColumns)- result = append(result, base.QuerySpanResult{- Name: leftSpanResult.Name,- SourceColumns: newResourceColumns,- })- }- leftSpanResults = &base.PseudoTable{- Name: "",- Columns: result,- }- // FIXME(zp): Consider UNION alias.- }- return leftSpanResults, nil-}--func (q *querySpanExtractor) extractTableSourceFromQueryPrimary(queryPrimary parser.IQuery_primaryContext) (base.TableSource, error) {- if queryPrimary.Select_() != nil {- return q.extractTableSourceFromSelect(queryPrimary.Select_())- }- if parenthesizedQuery := queryPrimary.Parenthesized_query(); parenthesizedQuery != nil {- // Table subquery shares the ctes of outer query. On the contrary,- // the subquery should not effect the outer query.- // https://cloud.google.com/bigquery/docs/reference/standard-sql/subqueries#correlated_subquery_concepts- originalCtesLength := len(q.ctes)- originalTableSourceFrom := len(q.tableSourceFrom)- defer func() {- q.ctes = q.ctes[:originalCtesLength]- q.tableSourceFrom = q.tableSourceFrom[:originalTableSourceFrom]- }()- subqueryExtractor := &querySpanExtractor{- ctx: q.ctx,- gCtx: q.gCtx,- defaultDatabase: q.defaultDatabase,- ctes: q.ctes,- outerTableSources: q.outerTableSources,- tableSourceFrom: q.tableSourceFrom,- }- tableSource, err := subqueryExtractor.extractTableSourceFromParenthesizedQuery(parenthesizedQuery)- if err != nil {- return nil, err- }-- var alias string- if v := queryPrimary.Opt_as_alias_with_required_as(); v != nil {- if v.Identifier() != nil {- alias = unquoteIdentifierByRule(v.Identifier())- }- }- if alias != "" {- return &base.PseudoTable{- Name: alias,- Columns: tableSource.GetQuerySpanResult(),- }, nil- }-- return tableSource, nil- }- return nil, nil-}--func (q *querySpanExtractor) extractTableSourceFromSelect(selectCtx parser.ISelectContext) (base.TableSource, error) {- var fromFields []base.QuerySpanResult- var resultFields []base.QuerySpanResult- if selectCtx.From_clause() != nil {- tableSource, err := q.extractTableSourceFromFromClause(selectCtx.From_clause())- if err != nil {- return nil, err- }- fromFields = tableSource.GetQuerySpanResult()- }-- itemList := selectCtx.Select_clause().Select_list().AllSelect_list_item()- for _, item := range itemList {- switch {- case item.Select_column_star() != nil:- fields := append([]base.QuerySpanResult{}, fromFields...)- fields, err := q.starModify(fields, item.Select_column_star().Star_modifiers())- if err != nil {- return nil, err- }- resultFields = append(resultFields, fields...)- case item.Select_column_dot_star() != nil:- v := item.Select_column_dot_star()- wildFields, err := q.extractWildFromExpr(v.Expression_higher_prec_than_and())- if err != nil {- return nil, err- }- fields, err := q.starModify(wildFields, item.Select_column_dot_star().Star_modifiers())- if err != nil {- return nil, err- }- resultFields = append(resultFields, fields...)- case item.Select_column_expr() != nil:- v := item.Select_column_expr()- var expression parser.IExpressionContext- if v.Expression() != nil {- expression = v.Expression()- } else if v.Select_column_expr_with_as_alias() != nil {- expression = v.Select_column_expr_with_as_alias().Expression()- }-- var alias parser.IIdentifierContext- if v.Select_column_expr_with_as_alias() != nil {- alias = v.Select_column_expr_with_as_alias().Identifier()- } else if v.Identifier() != nil {- alias = v.Identifier()- }-- name, sourceColumnSet, err := q.extractSourceColumnSetFromExpr(expression)- if err != nil {- return nil, err- }- aliasName := strings.ToUpper(name)- if alias != nil {- aliasName = strings.ToUpper(unquoteIdentifierByRule(alias))- }- resultFields = append(resultFields, base.QuerySpanResult{- Name: aliasName,- SourceColumns: sourceColumnSet,- })- default:- // Skip unrecognized select list item types- }- }- return &base.PseudoTable{- Name: "",- Columns: resultFields,- }, nil-}--func (q *querySpanExtractor) extractTableSourceFromParenthesizedQuery(parenthesizedQuery parser.IParenthesized_queryContext) (base.TableSource, error) {- return q.extractTableSourceFromQuery(parenthesizedQuery.Query())-}--func (q *querySpanExtractor) extractSourceColumnSetFromExpr(ctx antlr.ParserRuleContext) (string, base.SourceColumnSet, error) {- if ctx == nil {- return "", make(base.SourceColumnSet), nil- }-- // BigQuery support project the field from json, for example, SELECT a.b.c FROM ..., the AST looks like:- // /- // └── expression- // └── expression_higher_prec_than_and- // ├── expression_higher_prec_than_and- // │ ├── expression_higher_prec_than_and- // │ │ ├── expression_higher_prec_than_and- // │ │ ├── DOT_SYMBOL- // │ │ └── identifier(a)- // │ ├── DOT_SYMBOL- // │ └── identifier(b)- // ├── DOT_SYMBOL- // └── identifier(c)- // We use DFS algorithm here to find the tallest [expression_higher_prec_than_and DOT_SYMBOL identifier] subtree and- // treat it as identifier.- var name string- switch ctx := ctx.(type) {- case *parser.Parenthesized_queryContext:- baseSet := make(base.SourceColumnSet)- subqueryExtractor := &querySpanExtractor{- ctx: q.ctx,- defaultDatabase: q.defaultDatabase,- gCtx: q.gCtx,- ctes: q.ctes,- outerTableSources: append(q.outerTableSources, q.tableSourceFrom...),- tableSourceFrom: []base.TableSource{},- }- tableSource, err := subqueryExtractor.extractTableSourceFromParenthesizedQuery(ctx)- if err != nil {- return "", nil, err- }- spanResult := tableSource.GetQuerySpanResult()- for _, field := range spanResult {- baseSet, _ = base.MergeSourceColumnSet(field.SourceColumns, field.SourceColumns)- }- return "", baseSet, nil- case *parser.Expression_higher_prec_than_andContext:- baseSet := make(base.SourceColumnSet)- possibleColumnResources := getPossibleColumnResources(ctx)- for _, columnResources := range possibleColumnResources {- l := len(columnResources)- if l == 1 {- sourceColumnSet, err := q.getFieldColumnSource("", "", columnResources[0])- if err != nil {- return "", nil, err- }- baseSet, _ = base.MergeSourceColumnSet(baseSet, sourceColumnSet)- name = columnResources[0]- }- if l >= 2 {- // a.b, a can be the table name or the field name.- sourceColumnSet, err := q.getFieldColumnSource("", "", columnResources[0])- if err != nil {- sourceColumnSet, err = q.getFieldColumnSource("", columnResources[0], columnResources[1])- if err != nil {- return "", nil, err- }- baseSet, _ = base.MergeSourceColumnSet(baseSet, sourceColumnSet)- name = columnResources[1]- } else {- baseSet, _ = base.MergeSourceColumnSet(baseSet, sourceColumnSet)- name = columnResources[0]- }- }- }- return name, baseSet, nil- default:- }-- baseSet := make(base.SourceColumnSet)- children := ctx.GetChildren()- for _, child := range children {- child, ok := child.(antlr.ParserRuleContext)- if !ok {- continue- }- fieldName, sourceColumnSet, err := q.extractSourceColumnSetFromExpr(child)- if err != nil {- return "", nil, err- }- name = fieldName- baseSet, _ = base.MergeSourceColumnSet(baseSet, sourceColumnSet)- }- if len(children) > 1 {- name = ""- }-- return name, baseSet, nil-}--func (q *querySpanExtractor) starModify(fields []base.QuerySpanResult, starModifier parser.IStar_modifiersContext) ([]base.QuerySpanResult, error) {- if starModifier == nil {- return fields, nil- }-- type fieldItem struct {- id int- field base.QuerySpanResult- }- var fieldItems []fieldItem- for i, field := range fields {- fieldItems = append(fieldItems, fieldItem{- id: i,- field: field,- })- }- fieldItemMap := make(map[string]fieldItem)- for _, fieldItem := range fieldItems {- fieldItemMap[fieldItem.field.Name] = fieldItem- }-- if except := starModifier.Star_except_list(); except != nil {- allIdentifiers := except.AllIdentifier()- for _, identifier := range allIdentifiers {- identifierNormalized := unquoteIdentifierByRule(identifier)- if _, ok := fieldItemMap[identifierNormalized]; !ok {- return nil, errors.Errorf("field %s does not exist in the select clause", identifierNormalized)- }- delete(fieldItemMap, identifierNormalized)- }- }-- if replace := starModifier.Star_replace_list(); replace != nil {- allReplaceItems := replace.AllStar_replace_item()- for _, replaceItem := range allReplaceItems {- _, set, err := q.extractSourceColumnSetFromExpr(replaceItem.Expression())- if err != nil {- return nil, err- }- asIdentifier := unquoteIdentifierByRule(replaceItem.Identifier())- querySpanResult := base.QuerySpanResult{- Name: asIdentifier,- SourceColumns: set,- }- if _, ok := fieldItemMap[asIdentifier]; !ok {- return nil, errors.Errorf("field %s does not exist in the select clause", asIdentifier)- }- fieldItemMap[asIdentifier] = fieldItem{- id: fieldItemMap[asIdentifier].id,- field: querySpanResult,- }- }- }-- fieldItems = nil- for _, fieldItem := range fieldItemMap {- fieldItems = append(fieldItems, fieldItem)- }- slices.SortFunc(fieldItems, func(i, j fieldItem) int {- if i.id < j.id {- return -1- }- if i.id > j.id {- return 1- }- return 0- })-- var result []base.QuerySpanResult- for _, fieldItem := range fieldItems {- result = append(result, fieldItem.field)- }- return result, nil-}--func (q *querySpanExtractor) extractWildFromExpr(ctx antlr.ParserRuleContext) ([]base.QuerySpanResult, error) {- if ctx == nil {- return []base.QuerySpanResult{}, nil- }-- // BigQuery support project the field from json, for example, SELECT a.b.c FROM ..., the AST looks like:- // /- // └── expression- // └── expression_higher_prec_than_and- // ├── expression_higher_prec_than_and- // │ ├── expression_higher_prec_than_and- // │ │ ├── expression_higher_prec_than_and- // │ │ ├── DOT_SYMBOL- // │ │ └── identifier(a)- // │ ├── DOT_SYMBOL- // │ └── identifier(b)- // ├── DOT_SYMBOL- // └── identifier(c)- // We use DFS algorithm here to find the tallest [expression_higher_prec_than_and DOT_SYMBOL identifier] subtree and- // treat it as identifier.- switch ctx := ctx.(type) {- case *parser.Parenthesized_queryContext:- subqueryExtractor := &querySpanExtractor{- ctx: q.ctx,- defaultDatabase: q.defaultDatabase,- gCtx: q.gCtx,- ctes: q.ctes,- outerTableSources: append(q.outerTableSources, q.tableSourceFrom...),- tableSourceFrom: []base.TableSource{},- }- tableSource, err := subqueryExtractor.extractTableSourceFromParenthesizedQuery(ctx)- if err != nil {- return nil, err- }- spanResult := tableSource.GetQuerySpanResult()- return spanResult, nil- case *parser.Expression_higher_prec_than_andContext:- possibleColumnResources := getPossibleColumnResources(ctx)- for _, columnResources := range possibleColumnResources {- l := len(columnResources)- if l == 1 {- // a.*, a can be the table name or the field name.- results, ok := q.getAllTableColumnSources("", columnResources[0])- if !ok {- results, err := q.getFieldColumnSource("", "", columnResources[0])- if err != nil {- return nil, err- }- return []base.QuerySpanResult{- {- Name: columnResources[0],- SourceColumns: results,- },- }, nil- }- return results, nil- }- if l >= 2 {- // a.b.*, can be resolved as- // 1. a is the table name, b is the field name.- // 2. a is the field name, b is the field name.- results, err := q.getFieldColumnSource("", columnResources[0], columnResources[1])- if err != nil {- results, err := q.getFieldColumnSource("", "", columnResources[0])- if err != nil {- return nil, err- }- return []base.QuerySpanResult{- {- Name: columnResources[0],- SourceColumns: results,- },- }, nil- }- return []base.QuerySpanResult{- {- Name: columnResources[1],- SourceColumns: results,- },- }, nil- }- }- return nil, nil- default:- return nil, errors.Errorf("unsupported type in wild expr: %T", ctx)- }-}--func (q *querySpanExtractor) getAllTableColumnSources(datasetName, tableName string) ([]base.QuerySpanResult, bool) {- findInTableSource := func(tableSource base.TableSource) ([]base.QuerySpanResult, bool) {- if datasetName != "" && !strings.EqualFold(datasetName, tableSource.GetDatabaseName()) {- return nil, false- }- if tableName != "" && !strings.EqualFold(tableName, tableSource.GetTableName()) {- return nil, false- }-- // If the table name is empty, we should check if there are ambiguous fields,- // but we delegate this responsibility to the db-server, we do the fail-open strategy here.-- return tableSource.GetQuerySpanResult(), true- }-- // One sub-query may have multi-outer schemas and the multi-outer schemas can use the same name, such as:- //- // select (- // select (- // select max(a) > x1.a from t- // )- // from t1 as x1- // limit 1- // )- // from t as x1;- //- // This query has two tables can be called `x1`, and the expression x1.a uses the closer x1 table.- // This is the reason we loop the slice in reversed order.- for i := len(q.outerTableSources) - 1; i >= 0; i-- {- if querySpanResult, ok := findInTableSource(q.outerTableSources[i]); ok {- return querySpanResult, true- }- }-- for i := len(q.tableSourceFrom) - 1; i >= 0; i-- {- if querySpanResult, ok := findInTableSource(q.tableSourceFrom[i]); ok {- return querySpanResult, true- }- }-- return nil, false-}--func (q *querySpanExtractor) getFieldColumnSource(_, tableName, fieldName string) (base.SourceColumnSet, error) {- // Bigquery column name is case-insensitive.- findInTableSource := func(tableSource base.TableSource) (base.SourceColumnSet, bool) {- if tableName != "" && !strings.EqualFold(tableName, tableSource.GetTableName()) {- return nil, false- }- // If the table name is empty, we should check if there are ambiguous fields,- // but we delegate this responsibility to the db-server, we do the fail-open strategy here.-- querySpanResult := tableSource.GetQuerySpanResult()- for _, column := range querySpanResult {- if strings.EqualFold(column.Name, fieldName) {- return column.SourceColumns, true- }- }- return nil, false- }-- // One sub-query may have multi-outer schemas and the multi-outer schemas can use the same name, such as:- //- // select (- // select (- // select max(a) > x1.a from t- // )- // from t1 as x1- // limit 1- // )- // from t as x1;- //- // This query has two tables can be called `x1`, and the expression x1.a uses the closer x1 table.- // This is the reason we loop the slice in reversed order.- for i := len(q.outerTableSources) - 1; i >= 0; i-- {- if sourceColumnSet, ok := findInTableSource(q.outerTableSources[i]); ok {- return sourceColumnSet, nil- }- }-- for i := len(q.tableSourceFrom) - 1; i >= 0; i-- {- if sourceColumnSet, ok := findInTableSource(q.tableSourceFrom[i]); ok {- return sourceColumnSet, nil- }- }-- return nil, &base.ResourceNotFoundError{- Database: nil,- Table: &tableName,- Column: &fieldName,- }-}--func isValidExpressionHigherPrecThanAnd(ctx antlr.ParserRuleContext) bool {- if _, ok := ctx.(*parser.Expression_higher_prec_than_andContext); !ok {- return false- }-- terminalNodes := getAllChildTerminalNode(ctx)- if len(terminalNodes) == 1 && terminalNodes[0].GetSymbol().GetTokenType() == parser.GoogleSQLParserIDENTIFIER {- return true- }- child := ctx.GetChildren()- if len(child) == 3 {- first := child[0]- _, ok := first.(*parser.Expression_higher_prec_than_andContext)- if !ok {- return false- }- if !isValidExpressionHigherPrecThanAnd(first.(*parser.Expression_higher_prec_than_andContext)) {- return false- }- second := child[1]- _, ok = second.(*antlr.TerminalNodeImpl)- if !ok {- return false- }- if second.(*antlr.TerminalNodeImpl).GetSymbol().GetTokenType() != parser.GoogleSQLParserDOT_SYMBOL {- return false- }- third := child[2]- _, ok = third.(*parser.IdentifierContext)- return ok- }- return false-}--func getPossibleColumnResources(ctx antlr.ParserRuleContext) [][]string {- // Traverse the tree to find the tallest subtree which matches the pattern:- // [expression_higher_prec_than_and DOT_SYMBOL identifier]- // The result is the possible column resources.- var path []antlr.Tree- path = append(path, ctx)-- var result [][]string- for len(path) > 0 {- element := path[len(path)-1]- path = path[:len(path)-1]- if _, ok := element.(*parser.Parenthesized_queryContext); ok {- // NOTE: while adding the case in extractSourceColumnSetFromExpr, we should skip the case here.- continue- }- appendChild := true- if element, ok := element.(antlr.ParserRuleContext); ok {- valid := isValidExpressionHigherPrecThanAnd(element)- if valid {- appendChild = false- allTerminalNodes := getAllChildTerminalNode(element)- var columnResources []string- for _, terminalNode := range allTerminalNodes {- if terminalNode.GetSymbol().GetTokenType() == parser.GoogleSQLParserIDENTIFIER {- columnResources = append(columnResources, unquoteIdentifierByText(terminalNode.GetText()))- }- }- result = append(result, columnResources)- }- }-- if appendChild {- for _, child := range element.GetChildren() {- if child, ok := child.(antlr.ParserRuleContext); ok {- path = append(path, child)- }- }- }- }- return result-}--func getAllChildTerminalNode(ctx antlr.ParserRuleContext) []antlr.TerminalNode {- allChilds := ctx.GetChildren()- var result []antlr.TerminalNode- for _, child := range allChilds {- if childTerminal, ok := child.(antlr.TerminalNode); ok {- result = append(result, childTerminal)- continue- }- if childRuleCtx, ok := child.(antlr.ParserRuleContext); ok {- subResult := getAllChildTerminalNode(childRuleCtx)- result = append(result, subResult...)- }- }- return result-}--type joinType int--const (- crossJoin = iota- innerJoin- fullOuterJoin- leftOuterJoin- rightOuterJoin-)--func (q *querySpanExtractor) extractTableSourceFromFromClause(fromClause parser.IFrom_clauseContext) (base.TableSource, error) {- contents := fromClause.From_clause_contents()- tableSource, err := q.extractTableSourceFromTablePrimary(contents.Table_primary())- q.tableSourceFrom = append(q.tableSourceFrom, tableSource)- if err != nil {- return nil, err- }- var anchor base.TableSource- anchor = &base.PseudoTable{- Name: "",- Columns: tableSource.GetQuerySpanResult(),- }- allSuffixes := contents.AllFrom_clause_contents_suffix()- for _, suffix := range allSuffixes {- joinType := getJoinTypeFromFromClauseContentsSuffix(suffix)- tableSource, err = q.extractTableSourceFromTablePrimary(suffix.Table_primary())- if err != nil {- return nil, err- }- q.tableSourceFrom = append(q.tableSourceFrom, tableSource)- var usingColumns []string- if suffix.On_or_using_clause_list() != nil {- allJoinOnOrUsingClause := suffix.On_or_using_clause_list().AllOn_or_using_clause()- for _, joinOnOrUsingClause := range allJoinOnOrUsingClause {- if usingClause := joinOnOrUsingClause.Using_clause(); usingClause != nil {- identifiers := usingClause.AllIdentifier()- for _, identifier := range identifiers {- usingColumns = append(usingColumns, unquoteIdentifierByRule(identifier))- }- }- }- }- anchor = joinTable(anchor, joinType, usingColumns, tableSource)- }- q.tableSourceFrom = append(q.tableSourceFrom, anchor)- return anchor, nil-}--func joinTable(anchor base.TableSource, tp joinType, usingColumns []string, tableSource base.TableSource) base.TableSource {- var resultField []base.QuerySpanResult- switch tp {- case crossJoin, innerJoin, fullOuterJoin, leftOuterJoin, rightOuterJoin:- using := make(map[string]bool)- for _, usingColumn := range usingColumns {- using[usingColumn] = true- }- var lFields []base.QuerySpanResult- var rFields []base.QuerySpanResult- usingMerge := make(map[string][]base.QuerySpanResult)- for _, rField := range tableSource.GetQuerySpanResult() {- if _, ok := using[strings.ToUpper(rField.Name)]; ok {- usingMerge[strings.ToUpper(rField.Name)] = append(usingMerge[strings.ToUpper(rField.Name)], rField)- } else {- rFields = append(rFields, rField)- }- }- lFields = append(lFields, anchor.GetQuerySpanResult()...)-- for _, lField := range lFields {- columnSet := lField.SourceColumns- if _, ok := using[strings.ToUpper(lField.Name)]; ok {- mergeItems := usingMerge[strings.ToUpper(lField.Name)]- for _, mergeItem := range mergeItems {- columnSet, _ = base.MergeSourceColumnSet(columnSet, mergeItem.SourceColumns)- }- }- resultField = append(resultField, base.QuerySpanResult{- Name: lField.Name,- SourceColumns: columnSet,- })- }-- resultField = append(resultField, rFields...)- default:- // For unhandled join types, combine all fields from both sources- resultField = append(resultField, anchor.GetQuerySpanResult()...)- resultField = append(resultField, tableSource.GetQuerySpanResult()...)- }- return &base.PseudoTable{- Name: "",- Columns: resultField,- }-}--func getJoinTypeFromJoinType(joinType parser.IJoin_typeContext) joinType {- if joinType == nil {- return innerJoin- }- switch {- case joinType.CROSS_SYMBOL() != nil:- return crossJoin- case joinType.INNER_SYMBOL() != nil:- return innerJoin- case joinType.LEFT_SYMBOL() != nil:- return leftOuterJoin- case joinType.RIGHT_SYMBOL() != nil:- return rightOuterJoin- default:- return crossJoin- }-}--func getJoinTypeFromJoinItem(joinItem parser.IJoin_itemContext) joinType {- return getJoinTypeFromJoinType(joinItem.Join_type())-}--func getJoinTypeFromFromClauseContentsSuffix(fromClauseContentsSuffix parser.IFrom_clause_contents_suffixContext) joinType {- if fromClauseContentsSuffix.COMMA_SYMBOL() != nil {- return crossJoin- }- if fromClauseContentsSuffix.JOIN_SYMBOL() != nil {- return getJoinTypeFromJoinType(fromClauseContentsSuffix.Join_type())- }- return crossJoin-}--func (q *querySpanExtractor) extractTableSourceFromTablePrimary(tablePrimary parser.ITable_primaryContext) (base.TableSource, error) {- if tablePrimary.Tvf_with_suffixes() != nil {- // We do not support table value function because we do not have the returnning columns information.- return nil, errors.Errorf("unsupported table value function: %s", tablePrimary.GetText())- }- if tablePrimary.Table_path_expression() != nil {- return q.extractTableSourceFromTablePathExpression(tablePrimary.Table_path_expression())- }- if subquery := tablePrimary.Table_subquery(); subquery != nil {- return q.extractTableSourceFromTableSubquery(subquery)- }- if join := tablePrimary.Join(); join != nil {- anchor, err := q.extractTableSourceFromTablePrimary(join.Table_primary())- if err != nil {- return nil, err- }- q.tableSourceFrom = append(q.tableSourceFrom, anchor)- for _, item := range join.AllJoin_item() {- joinType := getJoinTypeFromJoinItem(item)- tableSource, err := q.extractTableSourceFromTablePrimary(item.Table_primary())- if err != nil {- return nil, err- }- q.tableSourceFrom = append(q.tableSourceFrom, tableSource)- var usingColumns []string- if item.On_or_using_clause_list() != nil {- allJoinOnOrUsingClause := item.On_or_using_clause_list().AllOn_or_using_clause()- for _, joinOnOrUsingClause := range allJoinOnOrUsingClause {- if usingClause := joinOnOrUsingClause.Using_clause(); usingClause != nil {- identifiers := usingClause.AllIdentifier()- for _, identifier := range identifiers {- usingColumns = append(usingColumns, unquoteIdentifierByRule(identifier))- }- }- }- }- anchor = joinTable(anchor, joinType, usingColumns, tableSource)- }- return anchor, nil- }- if tablePrimary.Table_primary() != nil {- return q.extractTableSourceFromTablePrimary(tablePrimary.Table_primary())- }-- return nil, nil-}--func (q *querySpanExtractor) extractTableSourceFromTableSubquery(subquery parser.ITable_subqueryContext) (base.TableSource, error) {- parenthesizedQuery := subquery.Parenthesized_query()-- // Table subquery shares the ctes of outer query. On the contrary,- // the subquery should not effect the outer query.- // https://cloud.google.com/bigquery/docs/reference/standard-sql/subqueries#correlated_subquery_concepts- originalCtesLength := len(q.ctes)- originalTableSourceFrom := len(q.tableSourceFrom)- defer func() {- q.ctes = q.ctes[:originalCtesLength]- q.tableSourceFrom = q.tableSourceFrom[:originalTableSourceFrom]- }()- subqueryExtractor := &querySpanExtractor{- ctx: q.ctx,- defaultDatabase: q.defaultDatabase,- gCtx: q.gCtx,- ctes: q.ctes,- outerTableSources: q.outerTableSources,- tableSourceFrom: q.tableSourceFrom,- }- tableSource, err := subqueryExtractor.extractTableSourceFromParenthesizedQuery(parenthesizedQuery)- if err != nil {- return nil, err- }-- var alias string- if v := subquery.Opt_pivot_or_unpivot_clause_and_alias(); v != nil {- if v.Identifier() != nil {- alias = unquoteIdentifierByRule(v.Identifier())- }- }- if alias != "" {- return &base.PseudoTable{- Name: alias,- Columns: tableSource.GetQuerySpanResult(),- }, nil- }-- return tableSource, nil-}--func (q *querySpanExtractor) extractTableSourceFromTablePathExpression(tablePathExpression parser.ITable_path_expressionContext) (base.TableSource, error) {- tablePathExprBase := tablePathExpression.Table_path_expression_base()- if tablePathExprBase.Unnest_expression() != nil {- // We do not support unnest expression because we do not have the returnning columns information.- return nil, errors.Errorf("unsupported unnest expression: %s", tablePathExprBase.GetText())- }- var tableName string- var datasetName string-- if slashedOrDashedPathExpression := tablePathExprBase.Maybe_slashed_or_dashed_path_expression(); slashedOrDashedPathExpression != nil {- if slashedOrDashedPathExpression.Maybe_dashed_path_expression() != nil {- if maybeDashedPathExpr := slashedOrDashedPathExpression.Maybe_dashed_path_expression(); maybeDashedPathExpr != nil {- // TODO(zp): support dashed path expression, for example, REGION-us- if maybeDashedPathExpr.Dashed_path_expression() != nil {- return nil, errors.Errorf("unsupported dashed path expression: %s", tablePathExprBase.GetText())- }- // REFACTOR(zp): refactor the code to extract table name and dataset name.- allIdentifiers := maybeDashedPathExpr.Path_expression().AllIdentifier()- if len(allIdentifiers) > 0 {- tableName = unquoteIdentifierByRule(allIdentifiers[len(allIdentifiers)-1])- if len(allIdentifiers) > 1 {- datasetName = unquoteIdentifierByRule(allIdentifiers[len(allIdentifiers)-2])- }- }- }- if slashedOrDashedPathExpression.Slashed_path_expression() != nil {- return nil, errors.Errorf("unsupported slashed path expression: %s", tablePathExprBase.GetText())- }- }- }-- tableSource, err := q.findTableSchema(datasetName, tableName)- if err != nil {- return nil, err- }-- if o := tablePathExpression.Opt_pivot_or_unpivot_clause_and_alias(); o != nil {- if o.Identifier() != nil {- tableSource = &base.PseudoTable{- Name: unquoteIdentifierByRule(o.Identifier()),- Columns: tableSource.GetQuerySpanResult(),- }- }- }- return tableSource, nil-}--func (q *querySpanExtractor) findTableSchema(datasetName string, tableName string) (base.TableSource, error) {- // https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical#case_sensitivity- // Dataset and table names are case-sensitive unless the is_case_insensitive option is set to TRUE.- if datasetName == "" {- for i := len(q.ctes) - 1; i >= 0; i-- {- table := q.ctes[i]- if strings.EqualFold(table.Name, tableName) {- return table, nil- }- }- }-- if datasetName == "" {- datasetName = q.defaultDatabase- }-- _, databaseMetadata, err := q.gCtx.GetDatabaseMetadataFunc(q.ctx, q.gCtx.InstanceID, datasetName)- if err != nil {- return nil, err- }- if databaseMetadata == nil {- return nil, errors.Errorf("dataset %q not found", datasetName)- }-- schema := databaseMetadata.GetSchemaMetadata("")- if schema == nil {- return nil, errors.Errorf("table %q not found", tableName)- }-- tables := schema.ListTableNames()- var originalTableName string- for _, t := range tables {- if strings.EqualFold(t, tableName) {- originalTableName = t- break- }- }- if originalTableName == "" {- return nil, errors.Errorf("table %q not found", tableName)- }- table := schema.GetTable(originalTableName)- if table == nil {- return nil, errors.Errorf("table %q not found", tableName)- }-- var columns []string- for _, column := range table.GetProto().GetColumns() {- columns = append(columns, column.Name)- }- return &base.PhysicalTable{- Server: "",- Database: datasetName,- Schema: "",- Name: table.GetProto().Name,- Columns: columns,- }, nil-}--func getAccessTables(defaultDatabase string, tree antlr.Tree) (base.SourceColumnSet, error) {- l := newAccessTableListener(defaultDatabase)- result := make(base.SourceColumnSet)- antlr.ParseTreeWalkerDefault.Walk(l, tree)- if l.err != nil {- return nil, l.err- }- result, _ = base.MergeSourceColumnSet(result, l.sourceColumnSet)- return result, nil-}--type accessTableListener struct {- *parser.BaseGoogleSQLParserListener-- currentDatabase string- sourceColumnSet base.SourceColumnSet- err error-}--func newAccessTableListener(currentDatabase string) *accessTableListener {- return &accessTableListener{- currentDatabase: currentDatabase,- sourceColumnSet: make(base.SourceColumnSet),- }-}--func (l *accessTableListener) EnterTable_path_expression(ctx *parser.Table_path_expressionContext) {- if l.err != nil {- return- }- // TODO(zp): Handle other unusual table path expression.- exprBase := ctx.Table_path_expression_base()- slashedOrDashedPathExpr := exprBase.Maybe_slashed_or_dashed_path_expression()- if slashedOrDashedPathExpr == nil {- l.err = errors.Errorf("unsupported table path expression: %s", ctx.GetText())- return- }-- dashedPathExpr := slashedOrDashedPathExpr.Maybe_dashed_path_expression()- if dashedPathExpr == nil {- l.err = errors.Errorf("unsupported slashed table path expression: %s", ctx.GetText())- return- }-- pathExpr := dashedPathExpr.Path_expression()- if pathExpr == nil {- l.err = errors.Errorf("unsupported dashed table path expression: %s", ctx.GetText())- }-- // Table name syntax: https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical- // Most of the time, the syntax can be [project_id.][dataset_id.]table_id.- // One difference is that the user access INFORMATION_SCHEMA in dataset,- // the syntax would be [project_id.]([region_id.]|[dataset_id.])INFORMATION_SCHEMA.VIEW_NAME.- // In this case, we treat the INFORMATION_SCHEMA as schema name.- allIdentifiers := pathExpr.AllIdentifier()- if len(allIdentifiers) == 0 {- return- }-- columnSource := base.ColumnResource{- Database: l.currentDatabase,- }- lastIdentifier := allIdentifiers[len(allIdentifiers)-1]- tableName := unquoteIdentifierByRule(lastIdentifier)- columnSource.Table = tableName-- if len(allIdentifiers) >= 2 {- identifier := unquoteIdentifierByRule(allIdentifiers[len(allIdentifiers)-2])- if strings.EqualFold(identifier, "INFORMATION_SCHEMA") {- columnSource.Schema = identifier- } else {- columnSource.Database = identifier- }- }-- l.sourceColumnSet[columnSource] = true-}--func unquoteIdentifierByRule(identifier parser.IIdentifierContext) string {- if len(identifier.GetText()) >= 3 && strings.HasPrefix(identifier.GetText(), "`") && strings.HasSuffix(identifier.GetText(), "`") {- return identifier.GetText()[1 : len(identifier.GetText())-1]- }- return identifier.GetText()-}--func unquoteIdentifierByText(identifier string) string {- if len(identifier) >= 3 && strings.HasPrefix(identifier, "`") && strings.HasSuffix(identifier, "`") {- return identifier[1 : len(identifier)-1]- }- return identifier-}--func isMixedQuery(m base.SourceColumnSet) (bool, bool) {- hasSystem, hasUser := false, false- for table := range m {- if isSystemResource(table) {- hasSystem = true- } else {- hasUser = true- }- }-- if hasSystem && hasUser {- return false, true- }-- return !hasUser && hasSystem, false-}--func isSystemResource(resource base.ColumnResource) bool {- return strings.EqualFold(resource.Schema, "INFORMATION_SCHEMA")-}diff --git a/backend/plugin/parser/bigquery/query_span_leak_pins_test.go b/backend/plugin/parser/bigquery/query_span_leak_pins_test.gonew file mode 100644index 0000000000..5e9082a3b0--- /dev/null+++ b/backend/plugin/parser/bigquery/query_span_leak_pins_test.go@@ -0,0 +1,119 @@+package bigquery++import (+ "testing"++ "github.com/stretchr/testify/require"++ storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/plugin/parser/base"+ "github.com/bytebase/bytebase/backend/plugin/parser/googlesql/googlesqltest"+)++// These pins cover masking-leak shapes that CANNOT live in the legacy-recorded+// differential corpus (test-data/query-span/standard.yaml), because the legacy+// ANTLR resolver either errors on them (fail-closed) or produces lineage that is+// itself a known leak. For each, omni must produce CORRECT lineage — the+// structural rule for shapes legacy cannot resolve: correct lineage or a closed+// failure, never silently empty/misaligned attribution (bytebase's masker is+// positional and fail-open).++func leakPinSpan(t *testing.T, statement string, tables []*storepb.TableMetadata) []base.QuerySpanResult {+ t.Helper()+ span, err := googlesqltest.GetSpan(t, storepb.Engine_BIGQUERY, GetQuerySpan, statement, "ds1",+ googlesqltest.DefaultSchemaTables(tables...))+ require.NoError(t, err, "statement: %s", statement)+ return span.Results+}++// TestLeakPin_LowercaseUsingCoalesce: `SELECT * FROM a JOIN b USING (k)` with a+// lowercase key. Real BigQuery coalesces USING keys case-insensitively (3 output+// columns), but the LEGACY resolver only coalesced upper-case-written keys — its+// lowercase output was 4 uncoalesced columns, shifting every later position+// against the executed result (a masking leak legacy shipped with). omni+// coalesces case-insensitively: one key column whose lineage reads BOTH sides,+// then the non-key columns, positionally aligned with the real output.+func TestLeakPin_LowercaseUsingCoalesce(t *testing.T) {+ results := leakPinSpan(t, "SELECT * FROM a JOIN b USING (k);", []*storepb.TableMetadata{+ {Name: "a", Columns: []*storepb.ColumnMetadata{{Name: "k"}, {Name: "x"}}},+ {Name: "b", Columns: []*storepb.ColumnMetadata{{Name: "k"}, {Name: "y"}}},+ })+ require.Len(t, results, 3, "USING key must be coalesced: real output is [k, x, y]")+ require.Equal(t, []string{"a.k", "b.k"}, googlesqltest.SourcesOf(results[0]), "coalesced key reads both sides")+ require.Equal(t, []string{"a.x"}, googlesqltest.SourcesOf(results[1]))+ require.Equal(t, []string{"b.y"}, googlesqltest.SourcesOf(results[2]))+}++// TestLeakPin_UnnestLineage: `SELECT elem FROM victim, UNNEST(victim.secret_tokens)+// AS elem` — the UNNEST output column's lineage is the unnested array column. The+// legacy resolver ERRORED on UNNEST in FROM (fail-closed); omni resolves it, so it+// must attribute correctly — silently-empty lineage would return the sensitive+// array elements unmasked.+func TestLeakPin_UnnestLineage(t *testing.T) {+ results := leakPinSpan(t, "SELECT elem FROM victim, UNNEST(victim.secret_tokens) AS elem", []*storepb.TableMetadata{+ {Name: "victim", Columns: []*storepb.ColumnMetadata{{Name: "id"}, {Name: "secret_tokens"}}},+ })+ require.Len(t, results, 1)+ require.Equal(t, []string{"victim.secret_tokens"}, googlesqltest.SourcesOf(results[0]),+ "UNNEST element column must carry the array column's lineage")+}++// TestLeakPin_UnnestWithOffsetNoLineage: the WITH OFFSET companion column is+// positional metadata, not data — empty lineage is correct for it, while the+// element column keeps the array lineage.+func TestLeakPin_UnnestWithOffsetNoLineage(t *testing.T) {+ results := leakPinSpan(t, "SELECT elem, pos FROM victim, UNNEST(victim.secret_tokens) AS elem WITH OFFSET AS pos", []*storepb.TableMetadata{+ {Name: "victim", Columns: []*storepb.ColumnMetadata{{Name: "id"}, {Name: "secret_tokens"}}},+ })+ require.Len(t, results, 2)+ require.Equal(t, []string{"victim.secret_tokens"}, googlesqltest.SourcesOf(results[0]))+ require.Empty(t, googlesqltest.SourcesOf(results[1]), "OFFSET is positional, carries no data lineage")+}++// TestLeakPin_ByNameMerge: `UNION ALL BY NAME` aligns set-op arms by column NAME,+// not ordinal. The legacy parser syntax-errors on BY NAME (fail-closed); omni+// parses it, so the analysis must name-merge — an ordinal merge would attribute+// the right arm's sensitive column to the wrong output position (verified leak+// pre-fix: ID <- rt.b_secret).+func TestLeakPin_ByNameMerge(t *testing.T) {+ results := leakPinSpan(t, "SELECT id, label FROM lt UNION ALL BY NAME SELECT b_secret AS label, id FROM rt", []*storepb.TableMetadata{+ {Name: "lt", Columns: []*storepb.ColumnMetadata{{Name: "id"}, {Name: "label"}}},+ {Name: "rt", Columns: []*storepb.ColumnMetadata{{Name: "id"}, {Name: "b_secret"}}},+ })+ require.Len(t, results, 2)+ require.Equal(t, []string{"lt.id", "rt.id"}, googlesqltest.SourcesOf(results[0]),+ "id column merges both arms' id lineage by NAME")+ require.Equal(t, []string{"lt.label", "rt.b_secret"}, googlesqltest.SourcesOf(results[1]),+ "label column carries the right arm's b_secret lineage (it is aliased AS label)")+}++// TestLeakPin_ByNameStarArmReversedOrder: a BY NAME set-op whose left arm is a+// STAR takes the deferred SetOpMerge consumer path (metadata expansion first).+// The consumer must name-merge there too — pre-fix it ordinal-merged, which+// mis-attributes whenever the arms' column orders differ (round-3 gate finding).+func TestLeakPin_ByNameStarArmReversedOrder(t *testing.T) {+ results := leakPinSpan(t, "SELECT * FROM pub UNION ALL BY NAME SELECT secret AS label, id FROM priv", []*storepb.TableMetadata{+ {Name: "pub", Columns: []*storepb.ColumnMetadata{{Name: "id"}, {Name: "label"}}},+ {Name: "priv", Columns: []*storepb.ColumnMetadata{{Name: "id"}, {Name: "secret"}}},+ })+ require.Len(t, results, 2)+ require.Equal(t, []string{"priv.id", "pub.id"}, googlesqltest.SourcesOf(results[0]),+ "id merges by NAME despite the right arm listing it second")+ require.Equal(t, []string{"priv.secret", "pub.label"}, googlesqltest.SourcesOf(results[1]),+ "label carries priv.secret (aliased AS label) despite it being first in the right arm")+}++// TestLeakPin_ByNameOnMatchColumns: `BY NAME ON (cols)` outputs ONLY the listed+// columns, in list order. Pre-fix the consumer emitted the full ordinal merge+// (wrong arity), shifting the positional masker off every real output column —+// the real single `label` column would have received `id`'s masker.+func TestLeakPin_ByNameOnMatchColumns(t *testing.T) {+ results := leakPinSpan(t, "SELECT * FROM pub UNION ALL BY NAME ON (label) SELECT id, secret AS label FROM priv", []*storepb.TableMetadata{+ {Name: "pub", Columns: []*storepb.ColumnMetadata{{Name: "id"}, {Name: "label"}}},+ {Name: "priv", Columns: []*storepb.ColumnMetadata{{Name: "id"}, {Name: "secret"}}},+ })+ require.Len(t, results, 1, "ON (label) restricts the output to exactly the listed column")+ require.Equal(t, "label", results[0].Name)+ require.Equal(t, []string{"priv.secret", "pub.label"}, googlesqltest.SourcesOf(results[0]),+ "the single output column carries both arms' label lineage")+}diff --git a/backend/plugin/parser/bigquery/query_span_test.go b/backend/plugin/parser/bigquery/query_span_test.goindex c7cbfd0691..7aa47b4fa7 100644--- a/backend/plugin/parser/bigquery/query_span_test.go+++ b/backend/plugin/parser/bigquery/query_span_test.go@@ -1,153 +1,17 @@package bigqueryimport (- "context"- "io"- "os""testing"- "github.com/antlr4-go/antlr/v4"- "github.com/pkg/errors"- "github.com/stretchr/testify/require"- "gopkg.in/yaml.v3"-- parser "github.com/bytebase/parser/googlesql"-- "github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/common/yamltest"storepb "github.com/bytebase/bytebase/backend/generated-go/store"- "github.com/bytebase/bytebase/backend/plugin/parser/base"- "github.com/bytebase/bytebase/backend/store/model"+ "github.com/bytebase/bytebase/backend/plugin/parser/googlesql/googlesqltest")+// TestGetQuerySpan runs the BigQuery query-span differential corpus. The+// goldens were RECORDED FROM the legacy ANTLR resolver — they are the+// masking-parity bar and must be reproduced, never re-recorded against this+// implementation (record=true exists for legacy-worktree recording only).func TestGetQuerySpan(t *testing.T) {- type testCase struct {- Description string `yaml:"description,omitempty"`- Statement string `yaml:"statement,omitempty"`- DefaultDatabase string `yaml:"defaultDatabase,omitempty"`- IgnoreCaseSensitve bool `yaml:"ignoreCaseSensitive,omitempty"`- // Metadata is the protojson encoded storepb.DatabaseSchemaMetadata,- // if it's empty, we will use the defaultDatabaseMetadata.- Metadata string `yaml:"metadata,omitempty"`- QuerySpan *base.YamlQuerySpan `yaml:"querySpan,omitempty"`- }-- var (- record = false- testDataPaths = []string{- "test-data/query-span/standard.yaml",- }- )-- a := require.New(t)- for _, testDataPath := range testDataPaths {- testDataPath := testDataPath-- yamlFile, err := os.Open(testDataPath)- a.NoError(err)-- var testCases []testCase- byteValue, err := io.ReadAll(yamlFile)- a.NoError(err)- a.NoError(yamlFile.Close())- a.NoError(yaml.Unmarshal(byteValue, &testCases))-- for i, tc := range testCases {- metadata := &storepb.DatabaseSchemaMetadata{}- a.NoErrorf(common.ProtojsonUnmarshaler.Unmarshal([]byte(tc.Metadata), metadata), "cases %d", i+1)- databaseMetadataGetter, databaseNameLister := buildMockDatabaseMetadataGetter([]*storepb.DatabaseSchemaMetadata{metadata})- result, err := GetQuerySpan(context.TODO(), base.GetQuerySpanContext{- GetDatabaseMetadataFunc: databaseMetadataGetter,- ListDatabaseNamesFunc: databaseNameLister,- }, base.Statement{Text: tc.Statement}, tc.DefaultDatabase, "dbo", tc.IgnoreCaseSensitve)- a.NoErrorf(err, "statement: %s", tc.Statement)- resultYaml := result.ToYaml()- if record {- testCases[i].QuerySpan = resultYaml- } else {- a.Equalf(tc.QuerySpan, resultYaml, "statement: %s", tc.Statement)- }- }-- if record {- yamltest.Record(t, testDataPath, testCases)- }- }-}--func buildMockDatabaseMetadataGetter(databaseMetadata []*storepb.DatabaseSchemaMetadata) (base.GetDatabaseMetadataFunc, base.ListDatabaseNamesFunc) {- return func(_ context.Context, _, databaseName string) (string, *model.DatabaseMetadata, error) {- m := make(map[string]*model.DatabaseMetadata)- for _, metadata := range databaseMetadata {- m[metadata.Name] = model.NewDatabaseMetadata(metadata, nil, nil, storepb.Engine_BIGQUERY, true /* isObjectCaseSensitive */)- }-- if databaseMetadata, ok := m[databaseName]; ok {- return "", databaseMetadata, nil- }-- return "", nil, errors.Errorf("database %q not found", databaseName)- }, func(_ context.Context, _ string) ([]string, error) {- var names []string- for _, metadata := range databaseMetadata {- names = append(names, metadata.Name)- }- return names, nil- }-}--func TestGetPossibleColumnResources(t *testing.T) {- testCases := []struct {- inputExpr string- want [][]string- }{- // Skip the subquery.- {- inputExpr: "(SELECT a FROM t)",- want: nil,- },- {- inputExpr: "a",- want: [][]string{{"a"}},- },- {- inputExpr: "a.b",- want: [][]string{{"a", "b"}},- },- {- inputExpr: "function_return_json(a.b).c.d",- want: [][]string{{"a", "b"}},- },- {- inputExpr: "a.b + c.d",- want: [][]string{{"c", "d"}, {"a", "b"}},- },- }-- for _, tc := range testCases {- inputStream := antlr.NewInputStream(tc.inputExpr)- lexer := parser.NewGoogleSQLLexer(inputStream)- stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)- p := parser.NewGoogleSQLParser(stream)- // Remove default error listener and add our own error listener.- lexer.RemoveErrorListeners()- lexerErrorListener := &base.ParseErrorListener{- Statement: tc.inputExpr,- }- lexer.AddErrorListener(lexerErrorListener)-- p.RemoveErrorListeners()- parserErrorListener := &base.ParseErrorListener{- Statement: tc.inputExpr,- }- p.AddErrorListener(parserErrorListener)-- p.BuildParseTrees = true-- tree := p.Expression()- require.Nil(t, lexerErrorListener.Err, "input: %s", tc.inputExpr)- require.Nil(t, parserErrorListener.Err, "input: %s", tc.inputExpr)- got := getPossibleColumnResources(tree)- require.Equal(t, tc.want, got, "input: %s", tc.inputExpr)- }+ googlesqltest.RunQuerySpanCorpus(t, storepb.Engine_BIGQUERY, GetQuerySpan,+ "test-data/query-span/standard.yaml", false /* record */)}diff --git a/backend/plugin/parser/bigquery/query_type.go b/backend/plugin/parser/bigquery/query_type.godeleted file mode 100644index a208405e9b..0000000000--- a/backend/plugin/parser/bigquery/query_type.go+++ /dev/null@@ -1,52 +0,0 @@-package bigquery--import (- parser "github.com/bytebase/parser/googlesql"- "github.com/pkg/errors"-- "github.com/bytebase/bytebase/backend/plugin/parser/base"-)--type queryTypeListener struct {- *parser.BaseGoogleSQLParserListener-- allSystems bool- result base.QueryType- err error-}--func (l *queryTypeListener) EnterStmts(ctx *parser.StmtsContext) {- // Assume that the stmts contains only one unterminated statement.- if len(ctx.AllUnterminated_sql_statement()) != 1 {- l.err = errors.Errorf("expecting 1 unterminated sql statement, but got %d", len(ctx.AllUnterminated_sql_statement()))- return- }- unterminatedStatement := ctx.AllUnterminated_sql_statement()[0]- l.result = l.getQueryTypeForUnterminatedSQLStatement(unterminatedStatement)-}--func (l *queryTypeListener) getQueryTypeForUnterminatedSQLStatement(u parser.IUnterminated_sql_statementContext) base.QueryType {- body := u.Sql_statement_body()- switch {- case body.Query_statement() != nil:- if l.allSystems {- return base.SelectInfoSchema- }- return base.Select- case body.Alter_statement() != nil, body.Create_constant_statement() != nil, body.Create_connection_statement() != nil, body.Create_database_statement() != nil,- body.Create_function_statement() != nil, body.Create_procedure_statement() != nil, body.Create_index_statement() != nil, body.Create_privilege_restriction_statement() != nil, body.Create_row_access_policy_statement() != nil,- body.Create_external_table_statement() != nil, body.Create_external_table_function_statement() != nil, body.Create_model_statement() != nil, body.Create_property_graph_statement() != nil,- body.Create_schema_statement() != nil, body.Create_external_schema_statement() != nil, body.Create_snapshot_statement() != nil, body.Create_table_function_statement() != nil, body.Create_table_statement() != nil,- body.Create_view_statement() != nil, body.Create_entity_statement() != nil, body.Rename_statement() != nil, body.Drop_all_row_access_policies_statement() != nil, body.Drop_statement() != nil, body.Undrop_statement() != nil:- return base.DDL- case body.Dml_statement() != nil, body.Merge_statement() != nil, body.Call_statement() != nil:- return base.DML- case body.Explain_statement() != nil:- return base.Explain- // Treat SAFE SET as select statement.- case body.Set_statement() != nil:- return base.Select- default:- return base.QueryTypeUnknown- }-}diff --git a/backend/plugin/parser/bigquery/split.go b/backend/plugin/parser/bigquery/split.godeleted file mode 100644index f296bc7512..0000000000--- a/backend/plugin/parser/bigquery/split.go+++ /dev/null@@ -1,20 +0,0 @@-package bigquery--import (- "github.com/antlr4-go/antlr/v4"- parser "github.com/bytebase/parser/googlesql"-- storepb "github.com/bytebase/bytebase/backend/generated-go/store"- "github.com/bytebase/bytebase/backend/plugin/parser/base"-)--func init() {- base.RegisterSplitterFunc(storepb.Engine_BIGQUERY, SplitSQL)-}--func SplitSQL(statement string) ([]base.Statement, error) {- lexer := parser.NewGoogleSQLLexer(antlr.NewInputStream(statement))- stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)- stream.Fill()- return base.SplitSQLByLexer(stream, parser.GoogleSQLLexerSEMI_SYMBOL)-}diff --git a/backend/plugin/parser/bigquery/test-data/query-span/standard.yaml b/backend/plugin/parser/bigquery/test-data/query-span/standard.yamlindex 8260539526..ed6f863945 100644--- a/backend/plugin/parser/bigquery/test-data/query-span/standard.yaml+++ b/backend/plugin/parser/bigquery/test-data/query-span/standard.yaml@@ -26,25 +26,25 @@querySpan:type: 1results:- - name: NAME- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: people- column: ID- isplainfield: false- sourcefieldpaths: []- selectasterisk: false+ - name: NAME+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: people+ column: ID+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: falsesourcecolumns:- - server: ""- database: ds1- schema: ""- table: people- column: ""+ - server: ""+ database: ds1+ schema: ""+ table: people+ column: ""predicatecolumns: []-- description: Set operation- statement: WITH CTE_1 AS (SELECT * FROM people), CTE_2 AS (SELECT * FROM address) SELECT * FROM CTE_1 UNION ALL SELECT * FROM CTE_2;+- description: 'Set operation (subset-projecting CTEs, then SELECT * UNION ALL): each CTE projects a subset of a wider base table, so a SELECT * over the CTE reproduces only the projected columns, then the set-op position-merges'+ statement: WITH CTE_1 AS (SELECT ID, NAME FROM people), CTE_2 AS (SELECT PEOPLE_ID, ADDRESS FROM address) SELECT * FROM CTE_1 UNION ALL SELECT * FROM CTE_2;defaultDatabase: ds1metadata: |-{@@ -61,6 +61,9 @@},{"name": "NAME"+ },+ {+ "name": "SSN"}]},@@ -72,6 +75,9 @@},{"name": "ADDRESS"+ },+ {+ "name": "ZIP"}]}@@ -82,57 +88,57 @@querySpan:type: 1results:- - name: ID- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: ADDRESS- column: PEOPLE_ID- - server: ""- database: ds1- schema: ""- table: PEOPLE- column: ID- isplainfield: false- sourcefieldpaths: []- selectasterisk: false- - name: NAME- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: ADDRESS- column: ADDRESS- - server: ""- database: ds1- schema: ""- table: PEOPLE- column: NAME- isplainfield: false- sourcefieldpaths: []- selectasterisk: false- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: CTE_1- column: ""- - server: ""- database: ds1- schema: ""- table: CTE_2- column: ""- - server: ""- database: ds1- schema: ""- table: address- column: ""- - server: ""- database: ds1- schema: ""- table: people- column: ""+ - name: ID+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: ADDRESS+ column: PEOPLE_ID+ - server: ""+ database: ds1+ schema: ""+ table: PEOPLE+ column: ID+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: NAME+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: ADDRESS+ column: ADDRESS+ - server: ""+ database: ds1+ schema: ""+ table: PEOPLE+ column: NAME+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: CTE_1+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: CTE_2+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: address+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: people+ column: ""predicatecolumns: []- description: Parenthesized joinstatement: SELECT ID, NAME, ADDRESS FROM (people JOIN address ON people.ID = address.PEOPLE_ID USING (ID));@@ -176,52 +182,52 @@querySpan:type: 1results:- - name: ID- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: ADDRESS- column: ID- - server: ""- database: ds1- schema: ""- table: PEOPLE- column: ID- isplainfield: false- sourcefieldpaths: []- selectasterisk: false- - name: NAME- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: PEOPLE- column: NAME- isplainfield: false- sourcefieldpaths: []- selectasterisk: false- - name: ADDRESS- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: ADDRESS- column: ADDRESS- isplainfield: false- sourcefieldpaths: []- selectasterisk: false- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: address- column: ""- - server: ""- database: ds1- schema: ""- table: people- column: ""+ - name: ID+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: ADDRESS+ column: ID+ - server: ""+ database: ds1+ schema: ""+ table: PEOPLE+ column: ID+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: NAME+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: PEOPLE+ column: NAME+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: ADDRESS+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: ADDRESS+ column: ADDRESS+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: address+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: people+ column: ""predicatecolumns: []- description: Join usingstatement: SELECT ID, NAME, ADDRESS FROM people JOIN address ON people.ID = address.PEOPLE_ID USING (ID);@@ -265,52 +271,52 @@querySpan:type: 1results:- - name: ID- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: ADDRESS- column: ID- - server: ""- database: ds1- schema: ""- table: PEOPLE- column: ID- isplainfield: false- sourcefieldpaths: []- selectasterisk: false- - name: NAME- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: PEOPLE- column: NAME- isplainfield: false- sourcefieldpaths: []- selectasterisk: false- - name: ADDRESS- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: ADDRESS- column: ADDRESS- isplainfield: false- sourcefieldpaths: []- selectasterisk: false- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: address- column: ""- - server: ""- database: ds1- schema: ""- table: people- column: ""+ - name: ID+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: ADDRESS+ column: ID+ - server: ""+ database: ds1+ schema: ""+ table: PEOPLE+ column: ID+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: NAME+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: PEOPLE+ column: NAME+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: ADDRESS+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: ADDRESS+ column: ADDRESS+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: address+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: people+ column: ""predicatecolumns: []- description: Simple joinstatement: SELECT ID, NAME, ADDRESS FROM people JOIN address ON people.ID = address.PEOPLE_ID;@@ -351,47 +357,47 @@querySpan:type: 1results:- - name: ID- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: PEOPLE- column: ID- isplainfield: false- sourcefieldpaths: []- selectasterisk: false- - name: NAME- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: PEOPLE- column: NAME- isplainfield: false- sourcefieldpaths: []- selectasterisk: false- - name: ADDRESS- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: ADDRESS- column: ADDRESS- isplainfield: false- sourcefieldpaths: []- selectasterisk: false- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: address- column: ""- - server: ""- database: ds1- schema: ""- table: people- column: ""+ - name: ID+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: PEOPLE+ column: ID+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: NAME+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: PEOPLE+ column: NAME+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: ADDRESS+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: ADDRESS+ column: ADDRESS+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: address+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: people+ column: ""predicatecolumns: []- description: Recursive ctestatement: |-@@ -430,74 +436,74 @@querySpan:type: 1results:- - name: C1- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: T1- column: a- - server: ""- database: ds1- schema: ""- table: T1- column: b- isplainfield: false- sourcefieldpaths: []- selectasterisk: false- - name: C2- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: T1- column: a- - server: ""- database: ds1- schema: ""- table: T1- column: b- isplainfield: false- sourcefieldpaths: []- selectasterisk: false- - name: C3- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: T1- column: a- - server: ""- database: ds1- schema: ""- table: T1- column: b- - server: ""- database: ds1- schema: ""- table: T1- column: c- isplainfield: false- sourcefieldpaths: []- selectasterisk: false- - name: "N"- sourcecolumns: []- isplainfield: false- sourcefieldpaths: []- selectasterisk: false- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: CTE_01- column: ""- - server: ""- database: ds1- schema: ""- table: T1- column: ""- predicatecolumns: []-- description: Simple normal cte+ - name: C1+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: T1+ column: a+ - server: ""+ database: ds1+ schema: ""+ table: T1+ column: b+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: C2+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: T1+ column: a+ - server: ""+ database: ds1+ schema: ""+ table: T1+ column: b+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: C3+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: T1+ column: a+ - server: ""+ database: ds1+ schema: ""+ table: T1+ column: b+ - server: ""+ database: ds1+ schema: ""+ table: T1+ column: c+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: "N"+ sourcecolumns: []+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: CTE_01+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: T1+ column: ""+ predicatecolumns: []+- description: Simple normal cte (CTE projects a SUBSET of a wider base table; SELECT * over the CTE must reproduce only ID, NAME — not the base SSN)statement: WITH CTE_1 AS (SELECT ID, NAME FROM people) SELECT * FROM CTE_1;defaultDatabase: ds1metadata: |-@@ -515,6 +521,9 @@},{"name": "NAME"+ },+ {+ "name": "SSN"}]}@@ -525,37 +534,37 @@querySpan:type: 1results:- - name: ID- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: people- column: ID- isplainfield: false- sourcefieldpaths: []- selectasterisk: false- - name: NAME- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: people- column: NAME- isplainfield: false- sourcefieldpaths: []- selectasterisk: false+ - name: ID+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: people+ column: ID+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: NAME+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: people+ column: NAME+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: falsesourcecolumns:- - server: ""- database: ds1- schema: ""- table: CTE_1- column: ""- - server: ""- database: ds1- schema: ""- table: people- column: ""+ - server: ""+ database: ds1+ schema: ""+ table: CTE_1+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: people+ column: ""predicatecolumns: []- description: Correlated subquery in the target field clause.statement: SELECT city, (SELECT COUNT(*) FROM paintings p WHERE g.id = p.gallery_id) AS total_paintings FROM galleries g;@@ -602,32 +611,32 @@querySpan:type: 1results:- - name: CITY- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: GALLERIES- column: CITY- isplainfield: false- sourcefieldpaths: []- selectasterisk: false- - name: TOTAL_PAINTINGS- sourcecolumns: []- isplainfield: false- sourcefieldpaths: []- selectasterisk: false+ - name: CITY+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: GALLERIES+ column: CITY+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: TOTAL_PAINTINGS+ sourcecolumns: []+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: falsesourcecolumns:- - server: ""- database: ds1- schema: ""- table: galleries- column: ""- - server: ""- database: ds1- schema: ""- table: paintings- column: ""+ - server: ""+ database: ds1+ schema: ""+ table: galleries+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: paintings+ column: ""predicatecolumns: []- description: Expression subquerystatement: SELECT (SELECT NAME FROM people), ID FROM people;@@ -657,29 +666,29 @@querySpan:type: 1results:- - name: ""- sourcecolumns: []- isplainfield: false- sourcefieldpaths: []- selectasterisk: false- - name: ID- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: people- column: ID- isplainfield: false- sourcefieldpaths: []- selectasterisk: false+ - name: ""+ sourcecolumns: []+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: ID+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: people+ column: ID+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: falsesourcecolumns:- - server: ""- database: ds1- schema: ""- table: people- column: ""+ - server: ""+ database: ds1+ schema: ""+ table: people+ column: ""predicatecolumns: []-- description: Table subquery+- description: 'Table subquery (derived * over a base-star CTE, plus a qualified result.ID): result.ID resolves through the derived relation and the CTE A to people.ID, while the sibling * reproduces every base column'statement: WITH A AS (SELECT * FROM people) SELECT result.ID, * FROM (SELECT * FROM A) result;defaultDatabase: ds1metadata: |-@@ -697,6 +706,9 @@},{"name": "NAME"+ },+ {+ "name": "SSN"}]}@@ -707,47 +719,57 @@querySpan:type: 1results:- - name: ID- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: people- column: ID- isplainfield: false- sourcefieldpaths: []- selectasterisk: false- - name: ID- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: people- column: ID- isplainfield: true- sourcefieldpaths: []- selectasterisk: false- - name: NAME- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: people- column: NAME- isplainfield: true- sourcefieldpaths: []- selectasterisk: false- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: A- column: ""- - server: ""- database: ds1- schema: ""- table: people- column: ""+ - name: ID+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: people+ column: ID+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: ID+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: people+ column: ID+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: false+ - name: NAME+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: people+ column: NAME+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: false+ - name: SSN+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: people+ column: SSN+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: A+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: people+ column: ""predicatecolumns: []- description: Wild projecterstatement: SELECT s.* FROM people s;@@ -777,32 +799,32 @@querySpan:type: 1results:- - name: ID- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: people- column: ID- isplainfield: true- sourcefieldpaths: []- selectasterisk: false- - name: NAME- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: people- column: NAME- isplainfield: true- sourcefieldpaths: []- selectasterisk: false+ - name: ID+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: people+ column: ID+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: false+ - name: NAME+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: people+ column: NAME+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: falsesourcecolumns:- - server: ""- database: ds1- schema: ""- table: people- column: ""+ - server: ""+ database: ds1+ schema: ""+ table: people+ column: ""predicatecolumns: []- description: Simple projecterstatement: SELECT ID, NAME FROM people;@@ -832,32 +854,32 @@querySpan:type: 1results:- - name: ID- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: people- column: ID- isplainfield: false- sourcefieldpaths: []- selectasterisk: false- - name: NAME- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: people- column: NAME- isplainfield: false- sourcefieldpaths: []- selectasterisk: false+ - name: ID+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: people+ column: ID+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: NAME+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: people+ column: NAME+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: falsesourcecolumns:- - server: ""- database: ds1- schema: ""- table: people- column: ""+ - server: ""+ database: ds1+ schema: ""+ table: people+ column: ""predicatecolumns: []- description: Expression in target fieldstatement: SELECT ID+1, ID+NAME, foo(bar(ID), NAME) FROM people;@@ -887,52 +909,52 @@querySpan:type: 1results:- - name: ID- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: people- column: ID- isplainfield: false- sourcefieldpaths: []- selectasterisk: false- - name: ID- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: people- column: ID- - server: ""- database: ds1- schema: ""- table: people- column: NAME- isplainfield: false- sourcefieldpaths: []- selectasterisk: false- - name: ID- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: people- column: ID- - server: ""- database: ds1- schema: ""- table: people- column: NAME- isplainfield: false- sourcefieldpaths: []- selectasterisk: false- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: people- column: ""+ - name: ID+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: people+ column: ID+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: ID+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: people+ column: ID+ - server: ""+ database: ds1+ schema: ""+ table: people+ column: NAME+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: ID+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: people+ column: ID+ - server: ""+ database: ds1+ schema: ""+ table: people+ column: NAME+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: people+ column: ""predicatecolumns: []- description: Simple select asterisk statementstatement: SELECT * FROM people;@@ -962,30 +984,2082 @@querySpan:type: 1results:- - name: ID- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: people- column: ID- isplainfield: true- sourcefieldpaths: []- selectasterisk: false- - name: NAME- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: people- column: NAME- isplainfield: true- sourcefieldpaths: []- selectasterisk: false+ - name: ID+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: people+ column: ID+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: false+ - name: NAME+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: people+ column: NAME+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: people+ column: ""+ predicatecolumns: []+- description: 'set-op: 2-arm equal-width UNION ALL explicit columns'+ statement: SELECT a, b FROM t1 UNION ALL SELECT c, d FROM t2;+ defaultDatabase: ds1+ metadata: |-+ {+ "name": "ds1",+ "schemas": [+ {+ "name": "",+ "tables": [+ { "name": "t1", "columns": [ { "name": "a" }, { "name": "b" } ] },+ { "name": "t2", "columns": [ { "name": "c" }, { "name": "d" } ] }+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: A+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t1+ column: a+ - server: ""+ database: ds1+ schema: ""+ table: t2+ column: c+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: B+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t1+ column: b+ - server: ""+ database: ds1+ schema: ""+ table: t2+ column: d+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t1+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: t2+ column: ""+ predicatecolumns: []+- description: 'set-op: 3-way UNION ALL'+ statement: SELECT a FROM t1 UNION ALL SELECT b FROM t2 UNION ALL SELECT c FROM t3;+ defaultDatabase: ds1+ metadata: |-+ {+ "name": "ds1",+ "schemas": [+ {+ "name": "",+ "tables": [+ { "name": "t1", "columns": [ { "name": "a" } ] },+ { "name": "t2", "columns": [ { "name": "b" } ] },+ { "name": "t3", "columns": [ { "name": "c" } ] }+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: A+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t1+ column: a+ - server: ""+ database: ds1+ schema: ""+ table: t2+ column: b+ - server: ""+ database: ds1+ schema: ""+ table: t3+ column: c+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t1+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: t2+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: t3+ column: ""+ predicatecolumns: []+- description: 'set-op: arm with SELECT * (t2 has 2 cols)'+ statement: SELECT a, b FROM t1 UNION ALL SELECT * FROM t2;+ defaultDatabase: ds1+ metadata: |-+ {+ "name": "ds1",+ "schemas": [+ {+ "name": "",+ "tables": [+ { "name": "t1", "columns": [ { "name": "a" }, { "name": "b" } ] },+ { "name": "t2", "columns": [ { "name": "c" }, { "name": "d" } ] }+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: A+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t1+ column: a+ - server: ""+ database: ds1+ schema: ""+ table: t2+ column: c+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: B+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t1+ column: b+ - server: ""+ database: ds1+ schema: ""+ table: t2+ column: d+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t1+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: t2+ column: ""+ predicatecolumns: []+- description: 'set-op: UNION DISTINCT'+ statement: SELECT a, b FROM t1 UNION DISTINCT SELECT c, d FROM t2;+ defaultDatabase: ds1+ metadata: |-+ {+ "name": "ds1",+ "schemas": [+ {+ "name": "",+ "tables": [+ { "name": "t1", "columns": [ { "name": "a" }, { "name": "b" } ] },+ { "name": "t2", "columns": [ { "name": "c" }, { "name": "d" } ] }+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: A+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t1+ column: a+ - server: ""+ database: ds1+ schema: ""+ table: t2+ column: c+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: B+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t1+ column: b+ - server: ""+ database: ds1+ schema: ""+ table: t2+ column: d+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t1+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: t2+ column: ""+ predicatecolumns: []+- description: 'set-op: INTERSECT DISTINCT'+ statement: SELECT a, b FROM t1 INTERSECT DISTINCT SELECT c, d FROM t2;+ defaultDatabase: ds1+ metadata: |-+ {+ "name": "ds1",+ "schemas": [+ {+ "name": "",+ "tables": [+ { "name": "t1", "columns": [ { "name": "a" }, { "name": "b" } ] },+ { "name": "t2", "columns": [ { "name": "c" }, { "name": "d" } ] }+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: A+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t1+ column: a+ - server: ""+ database: ds1+ schema: ""+ table: t2+ column: c+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: B+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t1+ column: b+ - server: ""+ database: ds1+ schema: ""+ table: t2+ column: d+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t1+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: t2+ column: ""+ predicatecolumns: []+- description: 'set-op: EXCEPT DISTINCT'+ statement: SELECT a, b FROM t1 EXCEPT DISTINCT SELECT c, d FROM t2;+ defaultDatabase: ds1+ metadata: |-+ {+ "name": "ds1",+ "schemas": [+ {+ "name": "",+ "tables": [+ { "name": "t1", "columns": [ { "name": "a" }, { "name": "b" } ] },+ { "name": "t2", "columns": [ { "name": "c" }, { "name": "d" } ] }+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: A+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t1+ column: a+ - server: ""+ database: ds1+ schema: ""+ table: t2+ column: c+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: B+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t1+ column: b+ - server: ""+ database: ds1+ schema: ""+ table: t2+ column: d+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t1+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: t2+ column: ""+ predicatecolumns: []+- description: 'set-op: arms with different column NAMES (first-select-name rule)'+ statement: SELECT id AS uid, name FROM t1 UNION ALL SELECT pid, lbl FROM t2;+ defaultDatabase: ds1+ metadata: |-+ {+ "name": "ds1",+ "schemas": [+ {+ "name": "",+ "tables": [+ { "name": "t1", "columns": [ { "name": "id" }, { "name": "name" } ] },+ { "name": "t2", "columns": [ { "name": "pid" }, { "name": "lbl" } ] }+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: UID+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t1+ column: id+ - server: ""+ database: ds1+ schema: ""+ table: t2+ column: pid+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: NAME+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t1+ column: name+ - server: ""+ database: ds1+ schema: ""+ table: t2+ column: lbl+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t1+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: t2+ column: ""+ predicatecolumns: []+- description: 'join: JOIN ... ON (unqualified cols resolve across both sides by name)'+ statement: SELECT id, lbl FROM a JOIN b ON a.id = b.aid;+ defaultDatabase: ds1+ metadata: |-+ {+ "name": "ds1",+ "schemas": [+ {+ "name": "",+ "tables": [+ { "name": "a", "columns": [ { "name": "id" }, { "name": "x" } ] },+ { "name": "b", "columns": [ { "name": "aid" }, { "name": "lbl" } ] }+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: ID+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: a+ column: id+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: LBL+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: b+ column: lbl+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: a+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: b+ column: ""+ predicatecolumns: []+- description: 'join: JOIN ... USING(K) explicit columns (the coalesced K reads from both sides; X from a, Y from b)'+ statement: SELECT K, X, Y FROM a JOIN b USING (K);+ defaultDatabase: ds1+ metadata: |-+ {+ "name": "ds1",+ "schemas": [+ {+ "name": "",+ "tables": [+ { "name": "a", "columns": [ { "name": "K" }, { "name": "X" } ] },+ { "name": "b", "columns": [ { "name": "K" }, { "name": "Y" } ] }+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: K+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: a+ column: K+ - server: ""+ database: ds1+ schema: ""+ table: b+ column: K+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: X+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: a+ column: X+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: "Y"+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: b+ column: "Y"+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: a+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: b+ column: ""+ predicatecolumns: []+- description: 'join: self-join FROM t x JOIN t y ON ...'+ statement: SELECT x.id, y.parent_id FROM t x JOIN t y ON x.parent_id = y.id;+ defaultDatabase: ds1+ metadata: |-+ {+ "name": "ds1",+ "schemas": [+ {+ "name": "",+ "tables": [+ { "name": "t", "columns": [ { "name": "id" }, { "name": "parent_id" } ] }+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: ID+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t+ column: id+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: PARENT_ID+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t+ column: parent_id+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t+ column: ""+ predicatecolumns: []+- description: 'join: 3-table join (unqualified cols resolve across all three sides by name)'+ statement: SELECT id, lbl, nt FROM a JOIN b ON a.id = b.aid JOIN c ON b.aid = c.bid;+ defaultDatabase: ds1+ metadata: |-+ {+ "name": "ds1",+ "schemas": [+ {+ "name": "",+ "tables": [+ { "name": "a", "columns": [ { "name": "id" } ] },+ { "name": "b", "columns": [ { "name": "aid" }, { "name": "lbl" } ] },+ { "name": "c", "columns": [ { "name": "bid" }, { "name": "nt" } ] }+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: ID+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: a+ column: id+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: LBL+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: b+ column: lbl+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: NT+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: c+ column: nt+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: a+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: b+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: c+ column: ""+ predicatecolumns: []+- description: 'cte: simple'+ statement: WITH c AS (SELECT id, name FROM users) SELECT id, name FROM c;+ defaultDatabase: ds1+ metadata: |-+ {+ "name": "ds1",+ "schemas": [+ {+ "name": "",+ "tables": [+ { "name": "users", "columns": [ { "name": "id" }, { "name": "name" }, { "name": "email" } ] }+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: ID+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: users+ column: id+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: NAME+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: users+ column: name+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: c+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: users+ column: ""+ predicatecolumns: []+- description: 'cte: projecting a SUBSET then SELECT *'+ statement: WITH c AS (SELECT email FROM users) SELECT * FROM c;+ defaultDatabase: ds1+ metadata: |-+ {+ "name": "ds1",+ "schemas": [+ {+ "name": "",+ "tables": [+ { "name": "users", "columns": [ { "name": "id" }, { "name": "name" }, { "name": "email" } ] }+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: EMAIL+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: users+ column: email+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: c+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: users+ column: ""+ predicatecolumns: []+- description: 'cte: recursive'+ statement: |-+ WITH RECURSIVE c AS (+ SELECT n FROM seed+ UNION ALL+ SELECT n + 1 FROM c WHERE n < 10+ )+ SELECT n FROM c;+ defaultDatabase: ds1+ metadata: |-+ {+ "name": "ds1",+ "schemas": [+ {+ "name": "",+ "tables": [+ { "name": "seed", "columns": [ { "name": "n" } ] }+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: "N"+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: seed+ column: "n"+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: c+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: seed+ column: ""+ predicatecolumns: []+- description: 'cte: nested CTEs (outer + inner WITH)'+ statement: WITH outer_cte AS (WITH inner_cte AS (SELECT id, name FROM users) SELECT id, name FROM inner_cte) SELECT id, name FROM outer_cte;+ defaultDatabase: ds1+ metadata: |-+ {+ "name": "ds1",+ "schemas": [+ {+ "name": "",+ "tables": [+ { "name": "users", "columns": [ { "name": "id" }, { "name": "name" } ] }+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: ID+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: users+ column: id+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: NAME+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: users+ column: name+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: inner_cte+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: outer_cte+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: users+ column: ""+ predicatecolumns: []+- description: 'cte: referenced twice (self-join of a CTE)'+ statement: WITH c AS (SELECT id, parent_id FROM nodes) SELECT a.id, b.parent_id FROM c a JOIN c b ON a.parent_id = b.id;+ defaultDatabase: ds1+ metadata: |-+ {+ "name": "ds1",+ "schemas": [+ {+ "name": "",+ "tables": [+ { "name": "nodes", "columns": [ { "name": "id" }, { "name": "parent_id" } ] }+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: ID+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: nodes+ column: id+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: PARENT_ID+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: nodes+ column: parent_id+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: c+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: nodes+ column: ""+ predicatecolumns: []+- description: 'derived: SELECT * FROM (SELECT ...) d'+ statement: SELECT * FROM (SELECT id, name FROM users) d;+ defaultDatabase: ds1+ metadata: |-+ {+ "name": "ds1",+ "schemas": [+ {+ "name": "",+ "tables": [+ { "name": "users", "columns": [ { "name": "id" }, { "name": "name" }, { "name": "email" } ] }+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: ID+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: users+ column: id+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: NAME+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: users+ column: name+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: users+ column: ""+ predicatecolumns: []+- description: 'derived: SELECT d.col FROM (SELECT a AS col FROM t) d'+ statement: SELECT d.col FROM (SELECT a AS col FROM t) d;+ defaultDatabase: ds1+ metadata: |-+ {+ "name": "ds1",+ "schemas": [+ {+ "name": "",+ "tables": [+ { "name": "t", "columns": [ { "name": "a" }, { "name": "b" } ] }+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: COL+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t+ column: a+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t+ column: ""+ predicatecolumns: []+- description: 'derived: projecting a subset/expression'+ statement: SELECT total FROM (SELECT price + tax AS total FROM orders) d;+ defaultDatabase: ds1+ metadata: |-+ {+ "name": "ds1",+ "schemas": [+ {+ "name": "",+ "tables": [+ { "name": "orders", "columns": [ { "name": "price" }, { "name": "tax" }, { "name": "qty" } ] }+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: TOTAL+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: orders+ column: price+ - server: ""+ database: ds1+ schema: ""+ table: orders+ column: tax+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: orders+ column: ""+ predicatecolumns: []+- description: scalar subquery in SELECT list+ statement: SELECT (SELECT x FROM t2) AS c FROM t1;+ defaultDatabase: ds1+ metadata: |-+ {+ "name": "ds1",+ "schemas": [+ {+ "name": "",+ "tables": [+ { "name": "t1", "columns": [ { "name": "id" } ] },+ { "name": "t2", "columns": [ { "name": "x" } ] }+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: C+ sourcecolumns: []+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t1+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: t2+ column: ""+ predicatecolumns: []+- description: 'struct: SELECT s.field FROM t (STRUCT-typed column s)'+ statement: SELECT s.field FROM t;+ defaultDatabase: ds1+ metadata: |-+ {+ "name": "ds1",+ "schemas": [+ {+ "name": "",+ "tables": [+ { "name": "t", "columns": [ { "name": "s" }, { "name": "id" } ] }+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: S+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t+ column: s+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t+ column: ""+ predicatecolumns: []+- description: 'struct: nested field s.a.b'+ statement: SELECT s.a.b FROM t;+ defaultDatabase: ds1+ metadata: |-+ {+ "name": "ds1",+ "schemas": [+ {+ "name": "",+ "tables": [+ { "name": "t", "columns": [ { "name": "s" }, { "name": "id" } ] }+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: S+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t+ column: s+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t+ column: ""+ predicatecolumns: []+- description: 'qualified star: SELECT t.* FROM t'+ statement: SELECT t.* FROM t;+ defaultDatabase: ds1+ metadata: |-+ {+ "name": "ds1",+ "schemas": [+ {+ "name": "",+ "tables": [+ { "name": "t", "columns": [ { "name": "id" }, { "name": "name" } ] }+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: id+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t+ column: id+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: false+ - name: name+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t+ column: name+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t+ column: ""+ predicatecolumns: []+- description: 'dataset-qualified table: SELECT id, name FROM ds1.t (legacy errors on dataset-qualified STAR ds1.t.*, so use explicit cols)'+ statement: SELECT id, name FROM ds1.t;+ defaultDatabase: ds1+ metadata: |-+ {+ "name": "ds1",+ "schemas": [+ {+ "name": "",+ "tables": [+ { "name": "t", "columns": [ { "name": "id" }, { "name": "name" } ] }+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: ID+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t+ column: id+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: NAME+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t+ column: name+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t+ column: ""+ predicatecolumns: []+- description: 'qualified star: alias star SELECT x.* FROM t x'+ statement: SELECT x.* FROM t x;+ defaultDatabase: ds1+ metadata: |-+ {+ "name": "ds1",+ "schemas": [+ {+ "name": "",+ "tables": [+ { "name": "t", "columns": [ { "name": "id" }, { "name": "name" } ] }+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: id+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t+ column: id+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: false+ - name: name+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t+ column: name+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t+ column: ""+ predicatecolumns: []+- description: 'combo: SELECT * EXCEPT(X) FROM a JOIN b USING(K) — uppercase key so the legacy resolver coalesces (its lowercase USING never coalesces, a known legacy case-fold leak; omni coalesces case-insensitively — pinned by a unit test)'+ statement: SELECT * EXCEPT (X) FROM a JOIN b USING (K);+ defaultDatabase: ds1+ metadata: |-+ {+ "name": "ds1",+ "schemas": [+ {+ "name": "",+ "tables": [+ { "name": "a", "columns": [ { "name": "K" }, { "name": "X" } ] },+ { "name": "b", "columns": [ { "name": "K" }, { "name": "Y" } ] }+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: K+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: a+ column: K+ - server: ""+ database: ds1+ schema: ""+ table: b+ column: K+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: "Y"+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: b+ column: "Y"+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: a+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: b+ column: ""+ predicatecolumns: []+- description: 'combo: SELECT * FROM cte UNION ALL SELECT * FROM othercte (each CTE projects a subset of a wider base; the * over each reproduces {id,name}, then the set-op position-merges)'+ statement: WITH cte AS (SELECT id, name FROM users), othercte AS (SELECT id, name FROM members) SELECT * FROM cte UNION ALL SELECT * FROM othercte;+ defaultDatabase: ds1+ metadata: |-+ {+ "name": "ds1",+ "schemas": [+ {+ "name": "",+ "tables": [+ { "name": "users", "columns": [ { "name": "id" }, { "name": "name" }, { "name": "email" } ] },+ { "name": "members", "columns": [ { "name": "id" }, { "name": "name" }, { "name": "ssn" } ] }+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: ID+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: members+ column: id+ - server: ""+ database: ds1+ schema: ""+ table: users+ column: id+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: NAME+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: members+ column: name+ - server: ""+ database: ds1+ schema: ""+ table: users+ column: name+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: cte+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: members+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: othercte+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: users+ column: ""+ predicatecolumns: []+- description: 'join: LEFT OUTER JOIN ... ON'+ statement: SELECT id, lbl FROM a LEFT OUTER JOIN b ON a.id = b.aid;+ defaultDatabase: ds1+ metadata: |-+ {+ "name": "ds1",+ "schemas": [+ {+ "name": "",+ "tables": [+ { "name": "a", "columns": [ { "name": "id" } ] },+ { "name": "b", "columns": [ { "name": "aid" }, { "name": "lbl" } ] }+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: ID+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: a+ column: id+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: LBL+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: b+ column: lbl+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: a+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: b+ column: ""+ predicatecolumns: []+- description: 'join: CROSS JOIN (comma) emits both sides'+ statement: SELECT id, lbl FROM a, b;+ defaultDatabase: ds1+ metadata: |-+ {+ "name": "ds1",+ "schemas": [+ {+ "name": "",+ "tables": [+ { "name": "a", "columns": [ { "name": "id" } ] },+ { "name": "b", "columns": [ { "name": "lbl" } ] }+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: ID+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: a+ column: id+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: LBL+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: b+ column: lbl+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: a+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: b+ column: ""+ predicatecolumns: []+- description: 'asterisk over join: SELECT * FROM a JOIN b ON (no coalescing)'+ statement: SELECT * FROM a JOIN b ON a.id = b.aid;+ defaultDatabase: ds1+ metadata: |-+ {+ "name": "ds1",+ "schemas": [+ {+ "name": "",+ "tables": [+ { "name": "a", "columns": [ { "name": "id" }, { "name": "x" } ] },+ { "name": "b", "columns": [ { "name": "aid" }, { "name": "y" } ] }+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: id+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: a+ column: id+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: x+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: a+ column: x+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: aid+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: b+ column: aid+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: false+ - name: "y"+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: b+ column: "y"+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: a+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: b+ column: ""+ predicatecolumns: []+- description: 'derived: SELECT a FROM (SELECT a,b,c FROM t) — outer projects one column of a 3-column derived relation'+ statement: SELECT a FROM (SELECT a, b, c FROM t) d;+ defaultDatabase: ds1+ metadata: |-+ {+ "name": "ds1",+ "schemas": [+ {+ "name": "",+ "tables": [+ { "name": "t", "columns": [ { "name": "a" }, { "name": "b" }, { "name": "c" }, { "name": "d" } ] }+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: A+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t+ column: a+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t+ column: ""+ predicatecolumns: []+- description: 'derived: SELECT d.* over a subset-projecting derived relation (reproduces the derived projection a,b — not the base d2)'+ statement: SELECT d.* FROM (SELECT a, b FROM t) d;+ defaultDatabase: ds1+ metadata: |-+ {+ "name": "ds1",+ "schemas": [+ {+ "name": "",+ "tables": [+ { "name": "t", "columns": [ { "name": "a" }, { "name": "b" }, { "name": "c" } ] }+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: A+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t+ column: a+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: B+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t+ column: b+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t+ column: ""+ predicatecolumns: []+- description: 'cte: subset CTE referenced with explicit columns AND a star sibling (WITH c AS (SELECT a,b FROM t) SELECT c.a, * FROM c)'+ statement: WITH c AS (SELECT a, b FROM wide) SELECT c.a, * FROM c;+ defaultDatabase: ds1+ metadata: |-+ {+ "name": "ds1",+ "schemas": [+ {+ "name": "",+ "tables": [+ { "name": "wide", "columns": [ { "name": "a" }, { "name": "b" }, { "name": "c" }, { "name": "d" } ] }+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: A+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: wide+ column: a+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: A+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: wide+ column: a+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: B+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: wide+ column: b+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: c+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: wide+ column: ""+ predicatecolumns: []+- description: 'leak-harden set-op base-star x base-star: SELECT * FROM t1 UNION ALL SELECT * FROM t2 (one output col position-merges t1.pub + t2.sec; t2.sec is the sensitive non-first arm — must NOT surface as a separate output column)'+ statement: SELECT * FROM t1 UNION ALL SELECT * FROM t2;+ defaultDatabase: ds1+ metadata: |-+ {+ "name": "ds1",+ "schemas": [+ {+ "name": "",+ "tables": [+ { "name": "t1", "columns": [ { "name": "pub" } ] },+ { "name": "t2", "columns": [ { "name": "sec" } ] }+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: pub+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t1+ column: pub+ - server: ""+ database: ds1+ schema: ""+ table: t2+ column: sec+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t1+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: t2+ column: ""+ predicatecolumns: []+- description: 'leak-harden set-op base-star x base-star (2 cols each): SELECT * FROM t1 UNION ALL SELECT * FROM t2 (both expand to 2 cols then position-merge; t2.sec is the sensitive 2nd-position arm)'+ statement: SELECT * FROM t1 UNION ALL SELECT * FROM t2;+ defaultDatabase: ds1+ metadata: |-+ {+ "name": "ds1",+ "schemas": [+ {+ "name": "",+ "tables": [+ { "name": "t1", "columns": [ { "name": "x1" }, { "name": "y1" } ] },+ { "name": "t2", "columns": [ { "name": "x2" }, { "name": "sec" } ] }+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: x1+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t1+ column: x1+ - server: ""+ database: ds1+ schema: ""+ table: t2+ column: x2+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: y1+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t1+ column: y1+ - server: ""+ database: ds1+ schema: ""+ table: t2+ column: sec+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t1+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: t2+ column: ""+ predicatecolumns: []+- description: 'leak-harden set-op nested star-merge: SELECT a FROM t1 UNION ALL SELECT * FROM t2 UNION ALL SELECT c FROM t3 (the inner star-merge arm t2 must survive the OUTER merge; t2.sec is the sensitive middle arm)'+ statement: SELECT a FROM t1 UNION ALL SELECT * FROM t2 UNION ALL SELECT c FROM t3;+ defaultDatabase: ds1+ metadata: |-+ {+ "name": "ds1",+ "schemas": [+ {+ "name": "",+ "tables": [+ { "name": "t1", "columns": [ { "name": "a" } ] },+ { "name": "t2", "columns": [ { "name": "sec" } ] },+ { "name": "t3", "columns": [ { "name": "c" } ] }+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: A+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t1+ column: a+ - server: ""+ database: ds1+ schema: ""+ table: t2+ column: sec+ - server: ""+ database: ds1+ schema: ""+ table: t3+ column: c+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t1+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: t2+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: t3+ column: ""+ predicatecolumns: []+- description: 'leak-harden set-op EXCEPT-star arm: SELECT * EXCEPT(dropme) FROM t1 UNION ALL SELECT rcol FROM t2 (the EXCEPT starGroup on the left arm must survive the merge — dropme excluded, surviving keep position-merges with t2.rcol)'+ statement: SELECT * EXCEPT (dropme) FROM t1 UNION ALL SELECT rcol FROM t2;+ defaultDatabase: ds1+ metadata: |-+ {+ "name": "ds1",+ "schemas": [+ {+ "name": "",+ "tables": [+ { "name": "t1", "columns": [ { "name": "keep" }, { "name": "dropme" } ] },+ { "name": "t2", "columns": [ { "name": "rcol" } ] }+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: keep+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t1+ column: keep+ - server: ""+ database: ds1+ schema: ""+ table: t2+ column: rcol+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t1+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: t2+ column: ""+ predicatecolumns: []+- description: 'leak-harden set-op REPLACE-star arm: SELECT t1.* REPLACE(t2.sec AS keep) FROM t1 JOIN t2 ON t1.keep=t2.jid UNION ALL SELECT rcol FROM t3 (the REPLACE starGroup must survive the merge; output keep position-merges the REPLACE lineage t2.sec with t3.rcol — t2.sec sensitive)'+ statement: SELECT t1.* REPLACE (t2.sec AS keep) FROM t1 JOIN t2 ON t1.keep = t2.jid UNION ALL SELECT rcol FROM t3;+ defaultDatabase: ds1+ metadata: |-+ {+ "name": "ds1",+ "schemas": [+ {+ "name": "",+ "tables": [+ { "name": "t1", "columns": [ { "name": "keep" } ] },+ { "name": "t2", "columns": [ { "name": "sec" }, { "name": "jid" } ] },+ { "name": "t3", "columns": [ { "name": "rcol" } ] }+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: keep+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t2+ column: sec+ - server: ""+ database: ds1+ schema: ""+ table: t3+ column: rcol+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t1+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: t2+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: t3+ column: ""+ predicatecolumns: []+- description: 'leak-harden recursive base-star arm: WITH RECURSIVE r AS (SELECT * FROM seed UNION ALL SELECT * FROM r) SELECT * FROM r (base-star recursive arm; the fixpoint must publish seed lineage exactly once — NOT duplicate it into 4 cols; seed.sec sensitive)'+ statement: WITH RECURSIVE r AS (SELECT * FROM seed UNION ALL SELECT * FROM r) SELECT * FROM r;+ defaultDatabase: ds1+ metadata: |-+ {+ "name": "ds1",+ "schemas": [+ {+ "name": "",+ "tables": [+ { "name": "seed", "columns": [ { "name": "id" }, { "name": "sec" } ] }+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: id+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: seed+ column: id+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: false+ - name: sec+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: seed+ column: sec+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: r+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: seed+ column: ""+ predicatecolumns: []+- description: 'leak-harden set-op base-star x base-star UNION DISTINCT: SELECT * FROM t1 UNION DISTINCT SELECT * FROM t2 (DISTINCT variant of base-star merge; t2.sec sensitive)'+ statement: SELECT * FROM t1 UNION DISTINCT SELECT * FROM t2;+ defaultDatabase: ds1+ metadata: |-+ {+ "name": "ds1",+ "schemas": [+ {+ "name": "",+ "tables": [+ { "name": "t1", "columns": [ { "name": "pub" } ] },+ { "name": "t2", "columns": [ { "name": "sec" } ] }+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: pub+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: t1+ column: pub+ - server: ""+ database: ds1+ schema: ""+ table: t2+ column: sec+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: falsesourcecolumns:- - server: ""- database: ds1- schema: ""- table: people- column: ""+ - server: ""+ database: ds1+ schema: ""+ table: t1+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: t2+ column: ""predicatecolumns: []diff --git a/backend/plugin/parser/googlesql/config.go b/backend/plugin/parser/googlesql/config.gonew file mode 100644index 0000000000..ea380a9bcf--- /dev/null+++ b/backend/plugin/parser/googlesql/config.go@@ -0,0 +1,70 @@+// Package googlesql holds the omni-backed implementation shared by the two+// GoogleSQL engine plugins (bigquery and spanner): the query-span extractor,+// the splitter, the diagnostics adapter, and the query-type mapping. One+// omni grammar serves both dialects; the per-engine packages register the+// base.* handlers with a Config carrying the few dialect deltas.+//+// Masking note: the query-span extractor is masking-critical (bytebase's+// masker applies per-output-column maskers positionally and fails OPEN, so+// under-attributed lineage returns sensitive columns unmasked). Keeping ONE+// implementation here — rather than a copy per engine — is deliberate: the+// behavior was validated against legacy-resolver differential corpora plus+// leak-pin tests in both engine packages, and a single code path cannot drift+// between engines.+package googlesql++import (+ "github.com/bytebase/omni/googlesql/analysis"+)++// Config carries the dialect deltas between the BigQuery and Spanner plugins.+// Every flag mirrors a documented legacy-extractor behavior difference; see the+// per-engine packages for the values and the corpus/leak-pin tests pinning them.+type Config struct {+ // Dialect selects omni's per-dialect analysis behavior (system schemas,+ // name-part bucketing, default join type).+ Dialect analysis.Dialect++ // IgnoreWrittenDatabase pins every resource's Database to the default+ // database. Spanner: the legacy resolver silently ignored the db part of a+ // 3-part db.schema.table and recorded the session database. BigQuery keeps+ // the written dataset (Database) with default fill-in.+ IgnoreWrittenDatabase bool++ // SchemaScopedMetadata looks tables up under the metadata schema named by+ // the reference (case-SENSITIVE, "" = default schema) within the default+ // database — the Spanner named-schema model. BigQuery instead resolves the+ // written dataset as the DATABASE and uses its single unnamed schema.+ SchemaScopedMetadata bool++ // SystemOnlyEmptySpan early-returns an EMPTY SelectInfoSchema span (no+ // source columns, no results, no metadata access) for a query reading ONLY+ // system tables — the legacy spanner extractor's exact behavior. BigQuery+ // instead keeps the system tables as table-level resources.+ SystemOnlyEmptySpan bool++ // SetStatementIsSelect classifies a SET statement as base.Select — BOTH+ // legacy queryTypeListeners' "treat SAFE SET as select" case (omni+ // classifies SET as Unknown, which the new-ACL access check would reject as+ // a disallowed query type).+ SetStatementIsSelect bool++ // UppercaseStarMergeName upper-cases a set-operation star-merge output name+ // (the legacy BigQuery extractor's rendering). The legacy spanner extractor+ // passed a LEFT-star-derived name through in the star column's metadata+ // case instead.+ UppercaseStarMergeName bool++ // CanonicalBaseFieldName renders a base-table FIELD passthrough (the JOIN+ // ... USING coalesced key, marked BaseFieldName by omni) in the field's+ // METADATA case — the legacy spanner resolver named it after the left+ // PhysicalTable's field, not the written USING token.+ CanonicalBaseFieldName bool++ // ContiguousSplitText makes each split statement's Text run contiguously+ // from the END of the previous statement (inter-statement whitespace and+ // comments attach to the FOLLOWING statement) — the legacy spanner+ // parse-tree splitter's convention. BigQuery starts each statement's Text+ // at its own first token.+ ContiguousSplitText bool+}diff --git a/backend/plugin/parser/googlesql/diagnose.go b/backend/plugin/parser/googlesql/diagnose.gonew file mode 100644index 0000000000..cd87c61f0c--- /dev/null+++ b/backend/plugin/parser/googlesql/diagnose.go@@ -0,0 +1,46 @@+package googlesql++import (+ "strings"++ "github.com/bytebase/omni/googlesql/diagnostics"++ "github.com/bytebase/bytebase/backend/plugin/parser/base"+)++// Diagnose returns syntax diagnostics for the given GoogleSQL statement,+// surfacing lex/parse errors from the omni parser via the omni diagnostics+// analyzer. Mirrors the Trino cutover (#20517).+//+// omni's Analyze emits a "<X> statement parsing is not yet supported" stub+// diagnostic for the handful of valid-but-not-yet-modeled statement forms (e.g.+// IMPORT MODULE, EXPORT METADATA) — recognized statement-leading keywords whose+// body the omni parser doesn't build yet. The legacy ANTLR parser parsed them+// without error, so flagging them would be a false positive on valid SQL; only+// those stubs are suppressed and every genuine lex/parse error (including+// "unknown or unsupported statement …") passes through.+func Diagnose(statement string) []base.Diagnostic {+ diags := diagnostics.Analyze(statement)+ out := make([]base.Diagnostic, 0, len(diags))+ mapper := base.NewByteOffsetPositionMapper(statement)+ for _, d := range diags {+ if isValidButUnimplementedStub(d.Message) {+ continue+ }+ syntaxErr := &base.SyntaxError{+ Position: mapper.Position(d.Range.Start.Offset),+ Message: d.Message,+ }+ out = append(out, base.ConvertSyntaxErrorToDiagnostic(syntaxErr, statement))+ }+ return out+}++// isValidButUnimplementedStub reports whether msg is an omni "not yet supported"+// stub for a statement form that is valid GoogleSQL (so it must not be flagged+// as a syntax error). The genuine-unknown case ("unknown or unsupported+// statement starting with …") and all lexer errors are intentionally NOT matched+// here.+func isValidButUnimplementedStub(msg string) bool {+ return strings.HasSuffix(msg, "statement parsing is not yet supported")+}diff --git a/backend/plugin/parser/googlesql/googlesqltest/googlesqltest.go b/backend/plugin/parser/googlesql/googlesqltest/googlesqltest.gonew file mode 100644index 0000000000..2646e3a6f4--- /dev/null+++ b/backend/plugin/parser/googlesql/googlesqltest/googlesqltest.go@@ -0,0 +1,126 @@+// Package googlesqltest holds the test harness shared by the bigquery and+// spanner engine packages: the query-span differential-corpus runner (the+// corpora are recorded FROM the legacy ANTLR resolvers and must be reproduced,+// never re-recorded against the new implementation) and the mock metadata+// plumbing the leak-pin tests use.+package googlesqltest++import (+ "context"+ "io"+ "os"+ "slices"+ "testing"++ "github.com/pkg/errors"+ "github.com/stretchr/testify/require"+ "gopkg.in/yaml.v3"++ "github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/common/yamltest"+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/plugin/parser/base"+ "github.com/bytebase/bytebase/backend/store/model"+)++// RunQuerySpanCorpus runs the legacy-recorded query-span differential corpus at+// testDataPath against getQuerySpan. With record=true it re-records the corpus+// from the CURRENT implementation — only ever do that from a legacy worktree;+// the goldens are the legacy resolvers' outputs and are the masking-parity bar.+func RunQuerySpanCorpus(t *testing.T, engine storepb.Engine, getQuerySpan base.GetQuerySpanFunc, testDataPath string, record bool) {+ type testCase struct {+ Description string `yaml:"description,omitempty"`+ Statement string `yaml:"statement,omitempty"`+ DefaultDatabase string `yaml:"defaultDatabase,omitempty"`+ IgnoreCaseSensitve bool `yaml:"ignoreCaseSensitive,omitempty"`+ // Metadata is the protojson encoded storepb.DatabaseSchemaMetadata.+ Metadata string `yaml:"metadata,omitempty"`+ QuerySpan *base.YamlQuerySpan `yaml:"querySpan,omitempty"`+ }++ a := require.New(t)+ yamlFile, err := os.Open(testDataPath)+ a.NoError(err)++ var testCases []testCase+ byteValue, err := io.ReadAll(yamlFile)+ a.NoError(err)+ a.NoError(yamlFile.Close())+ a.NoError(yaml.Unmarshal(byteValue, &testCases))++ for i, tc := range testCases {+ metadata := &storepb.DatabaseSchemaMetadata{}+ a.NoErrorf(common.ProtojsonUnmarshaler.Unmarshal([]byte(tc.Metadata), metadata), "cases %d", i+1)+ getter, lister := BuildMockDatabaseMetadataGetter(engine, []*storepb.DatabaseSchemaMetadata{metadata})+ result, err := getQuerySpan(context.TODO(), base.GetQuerySpanContext{+ GetDatabaseMetadataFunc: getter,+ ListDatabaseNamesFunc: lister,+ }, base.Statement{Text: tc.Statement}, tc.DefaultDatabase, "dbo", tc.IgnoreCaseSensitve)+ a.NoErrorf(err, "statement: %s", tc.Statement)+ resultYaml := result.ToYaml()+ if record {+ testCases[i].QuerySpan = resultYaml+ } else {+ a.Equalf(tc.QuerySpan, resultYaml, "statement: %s", tc.Statement)+ }+ }++ if record {+ yamltest.Record(t, testDataPath, testCases)+ }+}++// BuildMockDatabaseMetadataGetter builds the metadata getter/lister pair the+// query-span tests use, keyed by database name.+func BuildMockDatabaseMetadataGetter(engine storepb.Engine, databaseMetadata []*storepb.DatabaseSchemaMetadata) (base.GetDatabaseMetadataFunc, base.ListDatabaseNamesFunc) {+ return func(_ context.Context, _, databaseName string) (string, *model.DatabaseMetadata, error) {+ m := make(map[string]*model.DatabaseMetadata)+ for _, metadata := range databaseMetadata {+ m[metadata.Name] = model.NewDatabaseMetadata(metadata, nil, nil, engine, true /* isObjectCaseSensitive */)+ }++ if databaseMetadata, ok := m[databaseName]; ok {+ return "", databaseMetadata, nil+ }++ return "", nil, errors.Errorf("database %q not found", databaseName)+ }, func(_ context.Context, _ string) ([]string, error) {+ var names []string+ for _, metadata := range databaseMetadata {+ names = append(names, metadata.Name)+ }+ return names, nil+ }+}++// GetSpan evaluates one statement's query span against the given metadata —+// the leak-pin tests' entry point.+func GetSpan(t *testing.T, engine storepb.Engine, getQuerySpan base.GetQuerySpanFunc, statement, defaultDatabase string, schemas []*storepb.SchemaMetadata) (*base.QuerySpan, error) {+ t.Helper()+ meta := &storepb.DatabaseSchemaMetadata{Name: defaultDatabase, Schemas: schemas}+ getter, lister := BuildMockDatabaseMetadataGetter(engine, []*storepb.DatabaseSchemaMetadata{meta})+ return getQuerySpan(context.TODO(), base.GetQuerySpanContext{+ GetDatabaseMetadataFunc: getter,+ ListDatabaseNamesFunc: lister,+ }, base.Statement{Text: statement}, defaultDatabase, "", false)+}++// DefaultSchemaTables wraps tables into a single default ("") schema.+func DefaultSchemaTables(tables ...*storepb.TableMetadata) []*storepb.SchemaMetadata {+ return []*storepb.SchemaMetadata{{Name: "", Tables: tables}}+}++// SourcesOf renders a result's source columns as sorted "[schema.]table.column"+// strings for compact assertions.+func SourcesOf(r base.QuerySpanResult) []string {+ out := make([]string, 0, len(r.SourceColumns))+ for c := range r.SourceColumns {+ name := c.Table + "." + c.Column+ if c.Schema != "" {+ name = c.Schema + "." + name+ }+ out = append(out, name)+ }+ slices.Sort(out)+ return out+}diff --git a/backend/plugin/parser/googlesql/query_span_extractor.go b/backend/plugin/parser/googlesql/query_span_extractor.gonew file mode 100644index 0000000000..10d1146ccd--- /dev/null+++ b/backend/plugin/parser/googlesql/query_span_extractor.go@@ -0,0 +1,850 @@+package googlesql++import (+ "context"+ "slices"+ "strings"++ "github.com/bytebase/omni/googlesql/analysis"+ "github.com/pkg/errors"++ "github.com/bytebase/bytebase/backend/plugin/parser/base"+ "github.com/bytebase/bytebase/backend/store/model"+)++// QuerySpanExtractor analyses a single GoogleSQL statement and produces a+// base.QuerySpan: the set of base tables it accesses, the per-output column+// lineage (expanded against catalog metadata when available), and its statement+// classification.+//+// The extractor delegates structural lineage to omni's analysis.GetQuerySpan+// (per Config.Dialect), then applies the bytebase-specific post-processing the+// legacy plugins performed: the dialect name-model mapping onto+// base.ColumnResource, the user/system table handling, metadata star expansion,+// and the legacy result naming. omni resolves relation projections+// (CTE/derived/recursive bodies, set-operation merges with star markers,+// SELECT * EXCEPT/REPLACE, JOIN USING coalescing, UNNEST element lineage)+// before handing over; the one piece left to this metadata-aware consumer is+// enumerating a PHYSICAL table's columns (StarSegment.BaseTable), which omni+// cannot do catalog-free. Validated against the legacy-resolver differential+// corpora in the engine packages (test-data/query-span/standard.yaml, recorded+// FROM the legacy ANTLR resolvers) plus their leak-pin unit tests for shapes+// the legacy resolvers could not record.+type QuerySpanExtractor struct {+ cfg Config+ defaultDatabase string++ gCtx base.GetQuerySpanContext++ // metaCache memoises database metadata lookups within a single span.+ metaCache map[string]*model.DatabaseMetadata+}++// NewQuerySpanExtractor creates a query span extractor for one statement+// evaluation. The legacy 3-arg constructor's ignoreCaseSensitive flag is+// dropped: both legacy extractors ignored it (schema names match+// case-sensitively under the spanner model, table names fall back to a+// case-folded match — exactly the legacy findTableSchema behavior).+func NewQuerySpanExtractor(cfg Config, defaultDatabase string, gCtx base.GetQuerySpanContext) *QuerySpanExtractor {+ return &QuerySpanExtractor{+ cfg: cfg,+ defaultDatabase: defaultDatabase,+ gCtx: gCtx,+ metaCache: make(map[string]*model.DatabaseMetadata),+ }+}++// GetQuerySpan extracts the query span for a single GoogleSQL statement.+func (q *QuerySpanExtractor) GetQuerySpan(ctx context.Context, statement string) (*base.QuerySpan, error) {+ // query-span analysis expects exactly one non-empty statement (matching the+ // legacy behaviour, which parsed via the engine Parse wrapper and rejected+ // anything but exactly one).+ single, count, err := q.singleNonEmptyStatement(statement)+ if err != nil {+ return nil, err+ }+ switch {+ case count == 0:+ return &base.QuerySpan{+ SourceColumns: base.SourceColumnSet{},+ Results: []base.QuerySpanResult{},+ }, nil+ case count > 1:+ return nil, errors.Errorf("expecting only one statement to get query span, but got %d", count)+ }++ omniSpan, err := analysis.GetQuerySpan(single, q.cfg.Dialect)+ if err != nil {+ return nil, err+ }++ // Reject a statement that reads from both user and system+ // (INFORMATION_SCHEMA / SPANNER_SYS) tables, matching the legacy+ // isMixedQuery → MixUserSystemTablesError.+ allSystems, mixed := classifyAccess(omniSpan)+ if mixed {+ return nil, base.MixUserSystemTablesError+ }+ if allSystems && q.cfg.SystemOnlyEmptySpan {+ // A query reading exclusively from system tables EARLY-RETURNS an EMPTY+ // SelectInfoSchema span — no source columns, no results, no metadata+ // access. This is the legacy spanner extractor's exact behavior; system+ // metadata is never masked, and resolving these pseudo-tables would only+ // risk a spurious ResourceNotFoundError.+ return &base.QuerySpan{+ Type: base.SelectInfoSchema,+ SourceColumns: base.SourceColumnSet{},+ Results: []base.QuerySpanResult{},+ }, nil+ }++ queryType := MapQueryType(omniSpan.Type)+ if queryType == base.QueryTypeUnknown && q.cfg.SetStatementIsSelect && IsSetStatement(single) {+ // Legacy-spanner special case: a SET statement classifies as Select+ // ("treat SAFE SET as select"); omni classifies it Unknown.+ queryType = base.Select+ }+ // A query reading exclusively from system tables is SelectInfoSchema. omni's+ // classifier already promotes this, but recompute from the access-table+ // system flags too so the result is robust if the two ever disagree (the+ // legacy extractors derived allSystems from the access tables, not the+ // classifier).+ if queryType == base.Select && allSystems {+ queryType = base.SelectInfoSchema+ }++ // Top-level SourceColumns is the set of accessed tables at the TABLE level+ // (Column empty) — the legacy spans report accessed tables, not+ // column-expanded source columns. Uses the table name AS WRITTEN in the+ // query (the legacy accessTableListener recorded the written identifier),+ // with the dialect defaults filled in.+ accessTables := q.toTableResources(omniSpan)++ // Only a read-only query produces per-column results; DML/DDL/Explain/+ // Unknown return no per-column lineage — exactly the legacy behaviour+ // (it returned an empty span for any non-Select). SelectInfoSchema is a+ // read so it is treated like Select here.+ if queryType != base.Select && queryType != base.SelectInfoSchema {+ return &base.QuerySpan{+ Type: queryType,+ SourceColumns: base.SourceColumnSet{},+ Results: []base.QuerySpanResult{},+ }, nil+ }++ // Resolve relation aliases (e.g. "g" in "FROM galleries g") back to physical+ // table names so aliased column references match the expanded source columns.+ aliasMap := q.buildAliasMap(ctx, omniSpan)++ // Expand each accessed table to its columns via metadata: the membership set+ // used for lineage matching. The per-column resources use the+ // METADATA-canonical table name, matching the legacy findTableSchema/+ // PhysicalTable, which is why result lineage carries metadata casing while+ // the table-level SourceColumns above carry the written casing.+ fullSourceColumns, notFound, err := q.expandTablesToColumns(ctx, omniSpan)+ if err != nil {+ return nil, err+ }++ results := q.expandColumnInfos(ctx, omniSpan.Results, fullSourceColumns, aliasMap)++ span := &base.QuerySpan{+ Type: queryType,+ SourceColumns: accessTables,+ // PredicateColumns is intentionally left empty: neither legacy GoogleSQL+ // extractor populated predicate-column lineage (the recorded corpora have+ // predicatecolumns: [] on every case). omni DOES collect predicate+ // columns; surfacing them would diverge from the legacy contract the+ // masking layer was calibrated against and is a deliberate follow-up.+ Results: results,+ }+ if notFound != nil {+ span.NotFoundError = notFound+ }+ return span, nil+}++// singleNonEmptyStatement splits the input and returns its single non-empty+// statement's text together with the non-empty count.+func (q *QuerySpanExtractor) singleNonEmptyStatement(statement string) (string, int, error) {+ stmts, err := SplitSQL(statement, q.cfg)+ if err != nil {+ return "", 0, err+ }+ single, count := "", 0+ for _, s := range stmts {+ if !s.Empty && strings.TrimSpace(s.Text) != "" {+ single = s.Text+ count+++ }+ }+ return single, count, nil+}++// classifyAccess reports (allSystems, mixed) over the span's access tables,+// using omni's per-dialect IsSystem flag (BigQuery: INFORMATION_SCHEMA;+// Spanner: INFORMATION_SCHEMA and SPANNER_SYS). It reproduces the legacy+// isMixedQuery: mixed when both a system and a user table are present;+// allSystems when at least one system table is present and no user table is.+func classifyAccess(span *analysis.QuerySpan) (allSystems, mixed bool) {+ if span == nil {+ return false, false+ }+ hasSystem, hasUser := false, false+ for _, t := range span.AccessTables {+ if t.IsSystem {+ hasSystem = true+ } else {+ hasUser = true+ }+ }+ if hasSystem && hasUser {+ return false, true+ }+ return hasSystem && !hasUser, false+}++// resourceDatabase resolves the Database field for a resource: the written+// database/dataset with default fill-in (BigQuery), or always the default+// database (Spanner — the legacy resolver silently ignored the db part of a+// 3-part db.schema.table reference).+func (q *QuerySpanExtractor) resourceDatabase(written string) string {+ if q.cfg.IgnoreWrittenDatabase || written == "" {+ return q.defaultDatabase+ }+ return written+}++// toTableResources converts omni AccessTables into table-level+// base.ColumnResource keys (Column empty), applying the dialect name model.+// The table and schema names are kept AS WRITTEN (matching the legacy+// accessTableListener); the project/catalog qualifier is dropped (the legacy+// listeners never recorded it).+func (q *QuerySpanExtractor) toTableResources(span *analysis.QuerySpan) base.SourceColumnSet {+ out := base.SourceColumnSet{}+ if span == nil {+ return out+ }+ // CTE references are not physical tables, but the legacy access-table+ // listeners recorded every FROM table path — CTE references included — so+ // union them into the table-level SourceColumns.+ for _, group := range [][]analysis.TableAccess{span.AccessTables, span.CTEReferences} {+ for _, t := range group {+ out[base.ColumnResource{+ Database: q.resourceDatabase(t.Database),+ Schema: t.Schema,+ Table: t.Table,+ }] = true+ }+ }+ return out+}++// expandTablesToColumns expands each accessed table into one resource per column+// from the table's metadata, returning the membership set used for lineage+// matching.+//+// A table whose metadata does not EXIST is kept as a single table-level+// resource so the read is still recorded, and the first ResourceNotFoundError+// encountered is returned (non-fatally) for the caller to attach to the span —+// the masking layer rejects spans carrying NotFoundError. Any OTHER metadata+// error (a store outage, a canceled context) is returned FATALLY: falling+// through would leave result columns with silently-empty lineage and no+// NotFoundError, and the fail-open positional masker would return sensitive+// data unmasked after a transient infrastructure failure.+// Each per-column resource uses the METADATA-canonical table/column names (the+// legacy findTableSchema returned a PhysicalTable keyed on the proto name), so+// result lineage carries metadata casing.+func (q *QuerySpanExtractor) expandTablesToColumns(ctx context.Context, span *analysis.QuerySpan) (base.SourceColumnSet, *base.ResourceNotFoundError, error) {+ set := make(base.SourceColumnSet)+ var firstNotFound *base.ResourceNotFoundError+ if span == nil {+ return set, nil, nil+ }+ for _, t := range span.AccessTables {+ db := q.resourceDatabase(t.Database)+ if t.IsSystem {+ // System pseudo-objects are not Bytebase-tracked tables; their+ // metadata is never masked. Resolving them via the metadata getter+ // would yield a ResourceNotFoundError that sql_service turns into a+ // hard "failed to mask" rejection of an otherwise successful result,+ // so keep the table-level resource and skip the lookup. (A system-only+ // query already early-returned under SystemOnlyEmptySpan, and a mixed+ // user/system query was rejected.)+ set[base.ColumnResource{Database: db, Schema: t.Schema, Table: t.Table}] = true+ continue+ }+ canonicalTable, columns, err := q.tableColumns(ctx, t.Database, t.Schema, t.Table)+ if err != nil {+ var notFound *base.ResourceNotFoundError+ if !errors.As(err, ¬Found) {+ return nil, nil, err+ }+ if firstNotFound == nil {+ firstNotFound = notFound+ }+ // Keep the table-level resource for the missing object.+ set[base.ColumnResource{Database: db, Schema: t.Schema, Table: t.Table}] = true+ continue+ }+ if len(columns) == 0 {+ set[base.ColumnResource{Database: db, Schema: t.Schema, Table: canonicalTable}] = true+ continue+ }+ for _, col := range columns {+ set[base.ColumnResource{Database: db, Schema: t.Schema, Table: canonicalTable, Column: col}] = true+ }+ }+ return set, firstNotFound, nil+}++// expandColumnInfos expands a list of omni ColumnInfo (one query body's resolved+// projection) into base.QuerySpanResult. It is called for the top-level results+// and RECURSIVELY by buildSetOpMergeResults for each arm of a deferred set-op+// merge. Each ColumnInfo is one of: a star item (StarSegments, expanded + its+// EXCEPT/REPLACE applied), a per-position base-star set-op merge (StarMerge), a+// whole deferred set-op merge (SetOpMerge, both arms expanded then position-+// merged), or a concrete output column.+func (q *QuerySpanExtractor) expandColumnInfos(ctx context.Context, infos []analysis.ColumnInfo, fullSourceColumns base.SourceColumnSet, aliasMap map[string][]string) []base.QuerySpanResult {+ results := make([]base.QuerySpanResult, 0, len(infos))+ for _, r := range infos {+ switch {+ case r.StarSegments != nil:+ // A `*` / `rel.*` / CTE-or-derived star: omni resolved its ordered+ // expansion into segments (a base table to enumerate, or an+ // already-resolved concrete column). Expand them, then apply the star's+ // EXCEPT/REPLACE modifiers with the legacy name-collision last-wins dedup.+ results = append(results, q.expandStarSegments(ctx, r, fullSourceColumns, aliasMap)...)+ case r.SetOpMerge != nil:+ // A whole deferred set-operation merge whose arms carry un-enumerable+ // stars: expand each arm fully (recursively) and merge them, reproducing+ // the legacy "fully resolve each arm, then zip".+ results = append(results, q.buildSetOpMergeResults(ctx, r.SetOpMerge, fullSourceColumns, aliasMap)...)+ case r.StarMerge != nil:+ // A set-operation merge position whose other arm is a base-table star:+ // union the concrete arm's lineage with that table's Index-th column.+ results = append(results, q.buildStarMergeResult(ctx, r, fullSourceColumns, aliasMap))+ default:+ sourceColumns := base.SourceColumnSet{}+ for _, ref := range r.SourceColumns {+ addMatchingColumns(sourceColumns, fullSourceColumns, ref.Column, ref.Table, aliasMap)+ }+ name := resultName(r)+ // A base-table FIELD passthrough (the JOIN ... USING coalesced key)+ // keeps the field's METADATA case under the spanner naming model — the+ // legacy resolver named it after the left PhysicalTable's field, not+ // the written/upper-cased token.+ if q.cfg.CanonicalBaseFieldName && r.BaseFieldName && len(r.SourceColumns) > 0 {+ if canon := q.canonicalColumnCase(ctx, r.SourceColumns[0], r.Name); canon != "" {+ name = canon+ }+ }+ results = append(results, base.QuerySpanResult{+ Name: name,+ SourceColumns: sourceColumns,+ // IsPlainField is carried from omni: a `SELECT *` / `rel.*` expansion+ // column over a base table is plain; an explicit select-list item, a+ // set-op merge, and a join-left column are not (omni already applied+ // these rules), matching base.PhysicalTable.GetQuerySpanResult vs the+ // rewrapped PseudoTable columns.+ IsPlainField: r.IsPlain,+ })+ }+ }+ return results+}++// buildSetOpMergeResults expands a deferred set-operation merge: each arm is+// expanded fully (a base-table star against metadata, an EXCEPT/REPLACE star with+// its modifiers, a nested merge — recursively via expandColumnInfos), then the+// two expanded column lists are merged. This reproduces the legacy+// extractTableSourceFromQuerySetOperation: output NAME comes from the LEFT arm+// (the first-select-name rule), SourceColumns are the union of both arms at that+// position, and the merged column is never a plain field. omni emits this only+// when a per-position StarMerge cannot express the merge (a base-star UNION+// base-star, or an EXCEPT/REPLACE star arm in a set operation) — the arity is+// known only here, after metadata expansion.+//+// Width handling mirrors masking safety, not the legacy hard error: legacy+// rejects an unequal-width set operation outright (so the query never masks),+// while omni — best-effort over a possibly-already-rejected statement — merges up+// to the LONGER arm so no arm's column is dropped (a dropped sensitive column+// would under-attribute). The shorter arm simply contributes nothing past its+// length. Equal-width arms (the only valid set operations) merge exactly.+func (q *QuerySpanExtractor) buildSetOpMergeResults(ctx context.Context, m *analysis.SetOpMergeInfo, fullSourceColumns base.SourceColumnSet, aliasMap map[string][]string) []base.QuerySpanResult {+ left := q.expandColumnInfos(ctx, m.Left, fullSourceColumns, aliasMap)+ right := q.expandColumnInfos(ctx, m.Right, fullSourceColumns, aliasMap)+ if m.ByName {+ // BY NAME / CORRESPONDING aligns the arms by output column NAME, not+ // ordinal — an ordinal merge here mis-attributes whenever the arms'+ // column orders differ, and ignoring MatchColumns emits the wrong arity+ // (BY NAME ON (cols) outputs ONLY the listed columns), shifting the+ // positional masker off every real output column.+ return mergeExpandedByName(left, right, m.MatchColumns)+ }+ n := len(left)+ if len(right) > n {+ n = len(right)+ }+ merged := make([]base.QuerySpanResult, 0, n)+ for i := 0; i < n; i++ {+ sourceColumns := base.SourceColumnSet{}+ name := ""+ if i < len(left) {+ name = left[i].Name+ for sc := range left[i].SourceColumns {+ sourceColumns[sc] = true+ }+ }+ if i < len(right) {+ if name == "" {+ name = right[i].Name+ }+ for sc := range right[i].SourceColumns {+ sourceColumns[sc] = true+ }+ }+ merged = append(merged, base.QuerySpanResult{+ Name: name,+ SourceColumns: sourceColumns,+ IsPlainField: false,+ })+ }+ return merged+}++// mergeExpandedByName merges two expanded set-operation arms by output column+// NAME (case-insensitive — GoogleSQL column names are), mirroring the resolver's+// mergeProjectionsByName for arms that needed metadata expansion first. With+// matchColumns (BY NAME ON (cols)) the output is EXACTLY those columns in list+// order; otherwise the left arm's names in order plus right-only names appended+// (over-inclusion is masking-safe: a trailing extra never shifts earlier+// positions). Each output column's lineage is the union of BOTH arms'+// same-named columns.+func mergeExpandedByName(left, right []base.QuerySpanResult, matchColumns []string) []base.QuerySpanResult {+ namedSources := func(arm []base.QuerySpanResult, name string) base.SourceColumnSet {+ out := base.SourceColumnSet{}+ for _, r := range arm {+ if strings.EqualFold(r.Name, name) {+ for sc := range r.SourceColumns {+ out[sc] = true+ }+ }+ }+ return out+ }+ mergedFor := func(name string) base.QuerySpanResult {+ sources := namedSources(left, name)+ for sc := range namedSources(right, name) {+ sources[sc] = true+ }+ return base.QuerySpanResult{Name: name, SourceColumns: sources, IsPlainField: false}+ }+ if len(matchColumns) > 0 {+ out := make([]base.QuerySpanResult, 0, len(matchColumns))+ for _, name := range matchColumns {+ out = append(out, mergedFor(name))+ }+ return out+ }+ out := make([]base.QuerySpanResult, 0, len(left)+len(right))+ for _, l := range left {+ out = append(out, mergedFor(l.Name))+ }+ for _, r := range right {+ if !containsResultName(left, r.Name) {+ out = append(out, mergedFor(r.Name))+ }+ }+ return out+}++// containsResultName reports whether any result in list has the given output+// name under case-insensitive comparison.+func containsResultName(list []base.QuerySpanResult, name string) bool {+ for _, r := range list {+ if strings.EqualFold(r.Name, name) {+ return true+ }+ }+ return false+}++// expandStarSegments expands one star item's resolved segments (in order) into+// per-column results, then applies the star's EXCEPT/REPLACE modifiers. A base+// segment enumerates its physical table's columns via metadata (each a plain+// field per the segment's Plain); a concrete segment is emitted directly with its+// resolved lineage. EXCEPT/REPLACE then act on the expanded column list keyed by+// output name with last-wins on a name collision — the legacy starModify+// semantics (two FROM relations contributing a like-named column keep the later+// one before EXCEPT removes a name).+func (q *QuerySpanExtractor) expandStarSegments(ctx context.Context, r analysis.ColumnInfo, fullSourceColumns base.SourceColumnSet, aliasMap map[string][]string) []base.QuerySpanResult {+ var expanded []base.QuerySpanResult+ for _, seg := range r.StarSegments {+ if seg.BaseTable != nil {+ expanded = append(expanded, q.expandBaseTableColumns(ctx, *seg.BaseTable, seg.Plain, seg.ExceptColumns)...)+ continue+ }+ // A concrete segment (a CTE/derived/explicit column resolved by omni).+ sourceColumns := base.SourceColumnSet{}+ for _, ref := range seg.Sources {+ addMatchingColumns(sourceColumns, fullSourceColumns, ref.Column, ref.Table, aliasMap)+ }+ // A base-table FIELD passthrough inside a star (the JOIN ... USING+ // coalesced key) keeps the field's METADATA case under the spanner+ // naming model.+ name := seg.Name+ if q.cfg.CanonicalBaseFieldName && seg.BaseFieldName && len(seg.Sources) > 0 {+ if canon := q.canonicalColumnCase(ctx, seg.Sources[0], name); canon != "" {+ name = canon+ }+ }+ expanded = append(expanded, base.QuerySpanResult{+ Name: name,+ SourceColumns: sourceColumns,+ IsPlainField: seg.Plain,+ })+ }+ return applyStarModifiers(expanded, r, fullSourceColumns, aliasMap)+}++// expandBaseTableColumns enumerates a physical base table's columns from metadata+// into per-column plain-field results, each lineage'd to that column. A table+// whose metadata cannot be resolved yields no columns (the access is still+// recorded at the table level by expandTablesToColumns / toTableResources, and a+// ResourceNotFoundError is surfaced there). except lists column names to SKIP+// (case-insensitive): a JOIN ... USING key is projected once as a coalesced+// concrete segment ahead of the side stars, so each side's star must not expand+// it again — re-expanding would shift every later position and misalign the+// positional masker.+func (q *QuerySpanExtractor) expandBaseTableColumns(ctx context.Context, rel analysis.ColumnRef, plain bool, except []string) []base.QuerySpanResult {+ canonicalTable, columns, err := q.tableColumns(ctx, rel.Database, rel.Schema, rel.Table)+ if err != nil || len(columns) == 0 {+ return nil+ }+ db := q.resourceDatabase(rel.Database)+ out := make([]base.QuerySpanResult, 0, len(columns))+ for _, col := range columns {+ if containsFold(except, col) {+ continue+ }+ res := base.ColumnResource{Database: db, Schema: rel.Schema, Table: canonicalTable, Column: col}+ out = append(out, base.QuerySpanResult{+ Name: col,+ SourceColumns: base.SourceColumnSet{res: true},+ IsPlainField: plain,+ })+ }+ return out+}++// canonicalColumnCase returns the metadata-canonical spelling of column in the+// table named by ref (case-folded match), or "" when the table or column cannot+// be resolved.+func (q *QuerySpanExtractor) canonicalColumnCase(ctx context.Context, ref analysis.ColumnRef, column string) string {+ _, columns, err := q.tableColumns(ctx, ref.Database, ref.Schema, ref.Table)+ if err != nil {+ return ""+ }+ for _, col := range columns {+ if strings.EqualFold(col, column) {+ return col+ }+ }+ return ""+}++// applyStarModifiers applies a star item's EXCEPT/REPLACE modifiers to its+// expanded column list, reproducing the legacy starModify: results are keyed by+// output name (case-insensitive), last-wins on a collision; an EXCEPT name drops+// its column entirely; a REPLACE (expr AS name) re-points the named column's+// lineage to the replacement expression's sources and clears its plain-field+// flag. Original order is preserved. When the star has no modifiers the expanded+// list is returned unchanged.+func applyStarModifiers(expanded []base.QuerySpanResult, r analysis.ColumnInfo, fullSourceColumns base.SourceColumnSet, aliasMap map[string][]string) []base.QuerySpanResult {+ if len(r.StarExcept) == 0 && len(r.StarReplace) == 0 {+ return expanded+ }+ type item struct {+ id int+ field base.QuerySpanResult+ }+ // Key by output name; a name collision keeps the LAST occurrence (legacy+ // fieldItemMap overwrite), but the surviving entry keeps its FIRST ordinal so+ // the output order is stable.+ order := map[string]int{}+ byName := map[string]item{}+ next := 0+ for _, f := range expanded {+ key := strings.ToLower(f.Name)+ if _, ok := order[key]; !ok {+ order[key] = next+ next+++ }+ byName[key] = item{id: order[key], field: f}+ }+ for _, name := range r.StarExcept {+ delete(byName, strings.ToLower(name))+ }+ for _, rep := range r.StarReplace {+ key := strings.ToLower(rep.Name)+ if _, ok := byName[key]; !ok {+ continue+ }+ set := base.SourceColumnSet{}+ for _, ref := range rep.Sources {+ addMatchingColumns(set, fullSourceColumns, ref.Column, ref.Table, aliasMap)+ }+ byName[key] = item{+ id: byName[key].id,+ field: base.QuerySpanResult{Name: rep.Name, SourceColumns: set, IsPlainField: false},+ }+ }+ items := make([]item, 0, len(byName))+ for _, it := range byName {+ items = append(items, it)+ }+ slices.SortFunc(items, func(a, b item) int { return a.id - b.id })+ out := make([]base.QuerySpanResult, 0, len(items))+ for _, it := range items {+ out = append(out, it.field)+ }+ return out+}++// buildStarMergeResult builds a set-operation merge position whose other arm is a+// base-table star: the concrete arm's lineage (r.SourceColumns) is unioned with+// the star table's Index-th column (expanded from metadata). When the star arm is+// the LEFT one (StarMerge.LeftStar) the output name comes from that column (the+// legacy first-select-name rule); otherwise the concrete arm's name is kept. The+// merged column is never a plain field.+//+// Naming: a concrete-arm output name is uppercased (the legacy expression-name+// rendering, both engines). A LEFT-star-derived name is uppercased under the+// BigQuery model but keeps the star column's metadata case under the spanner+// model (the legacy spanner first-arm rule passed the PhysicalTable field name+// through unchanged).+func (q *QuerySpanExtractor) buildStarMergeResult(ctx context.Context, r analysis.ColumnInfo, fullSourceColumns base.SourceColumnSet, aliasMap map[string][]string) base.QuerySpanResult {+ sourceColumns := base.SourceColumnSet{}+ for _, ref := range r.SourceColumns {+ addMatchingColumns(sourceColumns, fullSourceColumns, ref.Column, ref.Table, aliasMap)+ }+ name := strings.ToUpper(r.Name)+ // A StarMerge always carries a bare single-base-table star (a USING-coalesced+ // or otherwise multi-segment star arm defers to SetOpMerge instead), so there+ // are no except columns to apply here.+ starCols := q.expandBaseTableColumns(ctx, r.StarMerge.Table, false, nil)+ if idx := r.StarMerge.Index; idx >= 0 && idx < len(starCols) {+ for sc := range starCols[idx].SourceColumns {+ sourceColumns[sc] = true+ }+ if r.StarMerge.LeftStar {+ name = starCols[idx].Name+ if q.cfg.UppercaseStarMergeName {+ name = strings.ToUpper(name)+ }+ }+ }+ return base.QuerySpanResult{+ Name: name,+ SourceColumns: sourceColumns,+ IsPlainField: false,+ }+}++// resultName renders an explicit select-item's output column name to match the+// legacy extractors (extractTableSourceFromSelect): the name is UPPER-CASED, and+// for an expression that has no written name of its own omni leaves Name empty —+// in that case the legacy code used the name of the single bare column the+// expression's DFS surfaced (e.g. `ID+1` → "ID"), which is omni's first source+// column. A name-less expression with no column reference (a literal, or an+// opaque scalar subquery) stays "".+func resultName(r analysis.ColumnInfo) string {+ name := r.Name+ if name == "" && len(r.SourceColumns) > 0 {+ name = r.SourceColumns[0].Column+ }+ return strings.ToUpper(name)+}++// addMatchingColumns adds every column in fullSourceColumns whose name matches+// refColumn (case-insensitively — GoogleSQL column names are case-insensitive —+// and whose table matches refTable when refTable is non-empty) to dst. refTable+// may be a relation alias; see tableMatches.+func addMatchingColumns(dst, fullSourceColumns base.SourceColumnSet, refColumn, refTable string, aliasMap map[string][]string) {+ for sc := range fullSourceColumns {+ if !strings.EqualFold(sc.Column, refColumn) {+ continue+ }+ if refTable == "" || tableMatches(sc.Table, refTable, aliasMap) {+ dst[sc] = true+ }+ }+}++// tableMatches reports whether the physical table scTable is named by refTable,+// either directly (its own name) or as a relation alias for it. Alias resolution+// is additive: the written refTable and every physical table the alias maps to+// are all accepted. omni's AccessTables is a flat, scope-less list, so an alias+// reused or shadowed across subqueries yields several candidates; accepting all+// of them over-includes (conservatively masks more) rather than risk an+// under-match that would leave a sensitive column unmasked.+func tableMatches(scTable, refTable string, aliasMap map[string][]string) bool {+ if strings.EqualFold(scTable, refTable) {+ return true+ }+ for _, phys := range aliasMap[strings.ToLower(refTable)] {+ if strings.EqualFold(scTable, phys) {+ return true+ }+ }+ return false+}++// buildAliasMap maps each relation alias (lower-cased) to the metadata-canonical+// physical table names it stands for, so aliased column references resolve back+// to the expanded (metadata-cased) source columns during column matching. Because+// omni's AccessTables is flat (carries no SQL scope), an alias reused in+// different scopes maps to several tables; all are kept so matching can+// over-include rather than under-match. An alias equal to its own table name+// carries no information and is skipped.+func (q *QuerySpanExtractor) buildAliasMap(ctx context.Context, span *analysis.QuerySpan) map[string][]string {+ if span == nil {+ return nil+ }+ out := make(map[string][]string)+ for _, t := range span.AccessTables {+ if t.Alias == "" || strings.EqualFold(t.Alias, t.Table) {+ continue+ }+ table := t.Table+ if !t.IsSystem {+ if canonical, _, err := q.tableColumns(ctx, t.Database, t.Schema, t.Table); err == nil && canonical != "" {+ table = canonical+ }+ }+ key := strings.ToLower(t.Alias)+ if !containsFold(out[key], table) {+ out[key] = append(out[key], table)+ }+ }+ return out+}++// containsFold reports whether list contains s under case-insensitive comparison.+func containsFold(list []string, s string) bool {+ return slices.ContainsFunc(list, func(e string) bool { return strings.EqualFold(e, s) })+}++// namedColumns is the table-or-view subset of the metadata model the column+// lookup needs: both expose a canonical name and an ordered column-name list.+type namedColumns struct {+ canonical string+ columns []string+}++// tableColumns returns the metadata-canonical table name and the column names of+// the given table or view, per the dialect metadata model:+// - spanner (SchemaScopedMetadata): the schema named by the reference+// (case-SENSITIVE; "" is the default schema) within the default database;+// - bigquery: the written dataset resolved as the DATABASE (default fill-in)+// with its single unnamed schema.+//+// Table names fall back to a case-folded match in both models — exactly the+// legacy findTableSchema behavior. Tables shadow views on a name collision (the+// legacy lookup tried tables first).+func (q *QuerySpanExtractor) tableColumns(ctx context.Context, writtenDatabase, schema, table string) (string, []string, error) {+ lookupDatabase := q.defaultDatabase+ lookupSchema := schema+ if !q.cfg.SchemaScopedMetadata {+ lookupDatabase = q.resourceDatabase(writtenDatabase)+ lookupSchema = ""+ }+ metadata, err := q.getDatabaseMetadata(ctx, lookupDatabase)+ if err != nil {+ return "", nil, err+ }++ schemaMeta := metadata.GetSchemaMetadata(lookupSchema)+ if schemaMeta == nil {+ return "", nil, &base.ResourceNotFoundError{+ Database: &lookupDatabase,+ Schema: &lookupSchema,+ }+ }++ for _, lookup := range []func(string) *namedColumns{+ func(name string) *namedColumns {+ meta := schemaMeta.GetTable(name)+ if meta == nil {+ return nil+ }+ out := &namedColumns{canonical: meta.GetProto().GetName()}+ for _, col := range meta.GetProto().GetColumns() {+ out.columns = append(out.columns, col.Name)+ }+ return out+ },+ func(name string) *namedColumns {+ meta := schemaMeta.GetView(name)+ if meta == nil {+ return nil+ }+ out := &namedColumns{canonical: meta.GetName()}+ for _, col := range meta.GetColumns() {+ out.columns = append(out.columns, col.Name)+ }+ return out+ },+ } {+ found := lookup(table)+ if found == nil {+ // Case-folded fallback over the object names (the legacy behavior for+ // table identifiers, which resolve case-insensitively).+ for _, name := range append(schemaMeta.ListTableNames(), schemaMeta.ListViewNames()...) {+ if strings.EqualFold(name, table) {+ if found = lookup(name); found != nil {+ break+ }+ }+ }+ }+ if found != nil {+ return found.canonical, found.columns, nil+ }+ }++ return "", nil, &base.ResourceNotFoundError{+ Database: &lookupDatabase,+ Schema: &lookupSchema,+ Table: &table,+ }+}++// getDatabaseMetadata fetches (and caches) metadata for the given database.+func (q *QuerySpanExtractor) getDatabaseMetadata(ctx context.Context, database string) (*model.DatabaseMetadata, error) {+ if database == "" {+ database = q.defaultDatabase+ }+ if meta, ok := q.metaCache[database]; ok {+ return meta, nil+ }+ if q.gCtx.GetDatabaseMetadataFunc == nil {+ return nil, &base.ResourceNotFoundError{Database: &database}+ }+ switch _, meta, err := q.gCtx.GetDatabaseMetadataFunc(ctx, q.gCtx.InstanceID, database); {+ case err != nil:+ var notFound *base.ResourceNotFoundError+ if errors.As(err, ¬Found) {+ return nil, err+ }+ return nil, errors.Wrapf(err, "failed to get database metadata for database: %s", database)+ case meta == nil:+ empty := &model.DatabaseMetadata{}+ q.metaCache[database] = empty+ return empty, nil+ default:+ q.metaCache[database] = meta+ return meta, nil+ }+}diff --git a/backend/plugin/parser/googlesql/query_type.go b/backend/plugin/parser/googlesql/query_type.gonew file mode 100644index 0000000000..138f05a219--- /dev/null+++ b/backend/plugin/parser/googlesql/query_type.go@@ -0,0 +1,54 @@+package googlesql++import (+ "github.com/bytebase/omni/googlesql/analysis"+ "github.com/bytebase/omni/googlesql/ast"+ "github.com/bytebase/omni/googlesql/parser"++ "github.com/bytebase/bytebase/backend/plugin/parser/base"+)++// ClassifyStatement classifies a single GoogleSQL statement via the omni+// analysis classifier and maps the result onto bytebase's base.QueryType.+// Config.SetStatementIsSelect re-applies the legacy spanner queryTypeListener's+// "treat SAFE SET as select" case (omni classifies SET as Unknown).+func ClassifyStatement(statement string, cfg Config) base.QueryType {+ qt := MapQueryType(analysis.ClassifySQL(statement, cfg.Dialect))+ if qt == base.QueryTypeUnknown && cfg.SetStatementIsSelect && IsSetStatement(statement) {+ return base.Select+ }+ return qt+}++// IsSetStatement reports whether the statement parses to a single SET statement.+func IsSetStatement(statement string) bool {+ file, errs := parser.Parse(statement)+ if len(errs) > 0 || file == nil || len(file.Stmts) != 1 {+ return false+ }+ _, ok := file.Stmts[0].(*ast.SetStmt)+ return ok+}++// MapQueryType maps an omni analysis.QueryType onto bytebase's base.QueryType.+// The two enums are defined to correspond 1:1 (omni's QueryType doc states the+// values mirror base.QueryType so this switch is total); an unexpected value+// falls back to QueryTypeUnknown.+func MapQueryType(t analysis.QueryType) base.QueryType {+ switch t {+ case analysis.Select:+ return base.Select+ case analysis.Explain:+ return base.Explain+ case analysis.SelectInfoSchema:+ return base.SelectInfoSchema+ case analysis.DDL:+ return base.DDL+ case analysis.DML:+ return base.DML+ case analysis.Unknown:+ return base.QueryTypeUnknown+ default:+ return base.QueryTypeUnknown+ }+}diff --git a/backend/plugin/parser/googlesql/register.go b/backend/plugin/parser/googlesql/register.gonew file mode 100644index 0000000000..09c3a7caf9--- /dev/null+++ b/backend/plugin/parser/googlesql/register.go@@ -0,0 +1,36 @@+package googlesql++import (+ "context"++ storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/plugin/parser/base"+)++// Handlers bundles the registered engine handlers Register returns, so the+// engine package can re-export each with its own declaration for its tests+// (the differential corpus and the leak pins drive the exact functions+// production uses).+type Handlers struct {+ SplitSQL base.SplitMultiSQLFunc+ GetQuerySpan base.GetQuerySpanFunc+}++// Register registers the splitter, diagnose, and query-span handlers for one+// GoogleSQL engine with the given dialect configuration.+func Register(engine storepb.Engine, cfg Config) Handlers {+ h := Handlers{+ SplitSQL: func(statement string) ([]base.Statement, error) {+ return SplitSQL(statement, cfg)+ },+ GetQuerySpan: func(ctx context.Context, gCtx base.GetQuerySpanContext, stmt base.Statement, database, _ string, _ bool) (*base.QuerySpan, error) {+ return NewQuerySpanExtractor(cfg, database, gCtx).GetQuerySpan(ctx, stmt.Text)+ },+ }+ base.RegisterSplitterFunc(engine, h.SplitSQL)+ base.RegisterDiagnoseFunc(engine, func(_ context.Context, _ base.DiagnoseContext, statement string) ([]base.Diagnostic, error) {+ return Diagnose(statement), nil+ })+ base.RegisterGetQuerySpan(engine, h.GetQuerySpan)+ return h+}diff --git a/backend/plugin/parser/googlesql/split.go b/backend/plugin/parser/googlesql/split.gonew file mode 100644index 0000000000..2c9b0508ad--- /dev/null+++ b/backend/plugin/parser/googlesql/split.go@@ -0,0 +1,59 @@+package googlesql++import (+ "github.com/bytebase/omni/googlesql/parser"++ storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/plugin/parser/base"+)++// SplitSQL splits the input into multiple SQL statements using the omni+// GoogleSQL splitter (block-aware: BEGIN/END, IF, LOOP, CASE bodies stay+// whole), then converts each Segment into a base.Statement with the position+// fields the legacy splitters produced. Positions are computed in a single O(n)+// pass via the shared base.ByteOffsetPositionMapper (segment offsets are+// queried in increasing order).+//+// Text conventions differ per dialect (Config.ContiguousSplitText): the legacy+// spanner parse-tree splitter ran each statement's Text contiguously from the+// END of the previous statement (inter-statement whitespace and comments attach+// to the FOLLOWING statement), while the legacy bigquery splitter started each+// statement's Text at its own first token. Both included the trailing ';',+// which omni's Segment.Text excludes (ByteEnd points AT it), so it is+// re-attached when present.+func SplitSQL(statement string, cfg Config) ([]base.Statement, error) {+ segs := parser.Split(statement)+ if len(segs) == 0 {+ return nil, nil+ }++ mapper := base.NewByteOffsetPositionMapper(statement)+ stmts := make([]base.Statement, 0, len(segs))+ prevEnd := 0+ for _, seg := range segs {+ end := seg.ByteEnd+ if end < len(statement) && statement[end] == ';' {+ end+++ }+ start := seg.ByteStart+ text := seg.Text+ if cfg.ContiguousSplitText {+ start = prevEnd+ text = statement[prevEnd:end]+ } else if end > seg.ByteEnd {+ text = statement[seg.ByteStart:end]+ }+ stmts = append(stmts, base.Statement{+ Text: text,+ Empty: seg.Empty(),+ Start: mapper.Position(start),+ End: mapper.Position(end),+ Range: &storepb.Range{+ Start: int32(start),+ End: int32(end),+ },+ })+ prevEnd = end+ }+ return stmts, nil+}diff --git a/backend/plugin/parser/spanner/dignose.go b/backend/plugin/parser/spanner/dignose.godeleted file mode 100644index a9d8b89e48..0000000000--- a/backend/plugin/parser/spanner/dignose.go+++ /dev/null@@ -1,68 +0,0 @@-package spanner--import (- "context"- "strings"- "unicode"-- "github.com/antlr4-go/antlr/v4"- parser "github.com/bytebase/parser/googlesql"-- storepb "github.com/bytebase/bytebase/backend/generated-go/store"- "github.com/bytebase/bytebase/backend/plugin/parser/base"-)--func init() {- base.RegisterDiagnoseFunc(storepb.Engine_SPANNER, Diagnose)-}--func Diagnose(_ context.Context, _ base.DiagnoseContext, statement string) ([]base.Diagnostic, error) {- diagnostics := make([]base.Diagnostic, 0)- syntaxError := parseGoogleSQLStatement(statement)- if syntaxError != nil {- diagnostics = append(diagnostics, base.ConvertSyntaxErrorToDiagnostic(syntaxError, statement))- }-- return diagnostics, nil-}--func parseGoogleSQLStatement(statement string) *base.SyntaxError {- trimmedStatement := strings.TrimRightFunc(statement, unicode.IsSpace)- if len(trimmedStatement) > 0 && !strings.HasSuffix(trimmedStatement, ";") {- // Add a semicolon to the end of the statement to allow users to omit the semicolon- // for the last statement in the script.- statement += ";"- }- inputStream := antlr.NewInputStream(statement)- lexer := parser.NewGoogleSQLLexer(inputStream)- stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)-- p := parser.NewGoogleSQLParser(stream)- startPosition := &storepb.Position{Line: 1}- lexerErrorListener := &base.ParseErrorListener{- Statement: statement,- StartPosition: startPosition,- }- lexer.RemoveErrorListeners()- lexer.AddErrorListener(lexerErrorListener)-- parserErrorListener := &base.ParseErrorListener{- Statement: statement,- StartPosition: startPosition,- }- p.RemoveErrorListeners()- p.AddErrorListener(parserErrorListener)-- p.BuildParseTrees = false-- _ = p.Root()-- if lexerErrorListener.Err != nil {- return lexerErrorListener.Err- }-- if parserErrorListener.Err != nil {- return parserErrorListener.Err- }- return nil-}diff --git a/backend/plugin/parser/spanner/plugin.go b/backend/plugin/parser/spanner/plugin.gonew file mode 100644index 0000000000..508dbebd9d--- /dev/null+++ b/backend/plugin/parser/spanner/plugin.go@@ -0,0 +1,40 @@+// Package spanner registers the Spanner engine handlers, backed by the shared+// omni-based GoogleSQL implementation (backend/plugin/parser/googlesql) with the+// Spanner dialect configuration. See that package for the behavior contract and+// the masking notes; the differential corpus (test-data/query-span/standard.yaml,+// recorded from the legacy ANTLR resolver) and the leak-pin tests in this package+// pin the Spanner-specific behavior.+package spanner++import (+ "github.com/bytebase/omni/googlesql/analysis"++ storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/plugin/parser/googlesql"+)++// config carries the Spanner dialect deltas, each mirroring a documented legacy+// extractor behavior: named schemas under ONE database (the db part of a 3-part+// db.schema.table is ignored; schema names match case-sensitively), system-only+// queries (INFORMATION_SCHEMA / SPANNER_SYS) early-return an empty+// SelectInfoSchema span, SET classifies as Select, JOIN USING coalesced keys+// keep their metadata case, and split Text runs contiguously from the previous+// statement's end.+var config = googlesql.Config{+ Dialect: analysis.DialectSpanner,+ IgnoreWrittenDatabase: true,+ SchemaScopedMetadata: true,+ SystemOnlyEmptySpan: true,+ SetStatementIsSelect: true,+ CanonicalBaseFieldName: true,+ ContiguousSplitText: true,+}++var handlers = googlesql.Register(storepb.Engine_SPANNER, config)++// SplitSQL is the registered splitter, re-exported for the tests in this package.+var SplitSQL = handlers.SplitSQL++// GetQuerySpan is the registered query-span handler, re-exported for the tests+// in this package.+var GetQuerySpan = handlers.GetQuerySpandiff --git a/backend/plugin/parser/spanner/query_span.go b/backend/plugin/parser/spanner/query_span.godeleted file mode 100644index e38fa96727..0000000000--- a/backend/plugin/parser/spanner/query_span.go+++ /dev/null@@ -1,28 +0,0 @@-package spanner--import (- "context"-- storepb "github.com/bytebase/bytebase/backend/generated-go/store"- "github.com/bytebase/bytebase/backend/plugin/parser/base"-)--func init() {- base.RegisterGetQuerySpan(storepb.Engine_SPANNER, GetQuerySpan)-}--// GetQuerySpan returns the query span for the given statement.-func GetQuerySpan(- ctx context.Context,- gCtx base.GetQuerySpanContext,- stmt base.Statement,- database, _ string,- ignoreCaseSensitive bool,-) (*base.QuerySpan, error) {- q := newQuerySpanExtractor(database, gCtx, ignoreCaseSensitive)- querySpan, err := q.getQuerySpan(ctx, stmt.Text)- if err != nil {- return nil, err- }- return querySpan, nil-}diff --git a/backend/plugin/parser/spanner/query_span_extractor.go b/backend/plugin/parser/spanner/query_span_extractor.godeleted file mode 100644index 150a4b1e57..0000000000--- a/backend/plugin/parser/spanner/query_span_extractor.go+++ /dev/null@@ -1,1257 +0,0 @@-package spanner--import (- "context"- "slices"- "strings"-- "github.com/antlr4-go/antlr/v4"- parser "github.com/bytebase/parser/googlesql"-- "github.com/pkg/errors"-- "github.com/bytebase/bytebase/backend/plugin/parser/base"-)--type querySpanExtractor struct {- ctx context.Context- defaultDatabase string-- gCtx base.GetQuerySpanContext-- ctes []*base.PseudoTable-- // outerTableSources is the table sources from the outer query span.- // it's used to resolve the column name in the correlated sub-query.- outerTableSources []base.TableSource-- // tableSourceFrom is the table sources from the from clause.- tableSourceFrom []base.TableSource-}--func newQuerySpanExtractor(defaultDatabase string, gCtx base.GetQuerySpanContext, _ bool) *querySpanExtractor {- return &querySpanExtractor{- defaultDatabase: defaultDatabase,- gCtx: gCtx,- }-}--func (q *querySpanExtractor) getQuerySpan(ctx context.Context, stmt string) (*base.QuerySpan, error) {- parseResults, err := ParseSpannerGoogleSQL(stmt)- if err != nil {- return nil, err- }-- if len(parseResults) != 1 {- return nil, errors.Errorf("expected exactly 1 statement, got %d", len(parseResults))- }-- parseResult := parseResults[0]- tree := parseResult.Tree- q.ctx = ctx- accessTables, err := getAccessTables(q.defaultDatabase, tree)- if err != nil {- return nil, err- }- allSystems, mixed := isMixedQuery(accessTables)- if mixed {- return nil, base.MixUserSystemTablesError- }-- queryTypeListener := &queryTypeListener{- allSystems: allSystems,- result: base.QueryTypeUnknown,- }- antlr.ParseTreeWalkerDefault.Walk(queryTypeListener, tree)- if queryTypeListener.err != nil {- return nil, queryTypeListener.err- }- if queryTypeListener.result != base.Select {- return &base.QuerySpan{- Type: queryTypeListener.result,- SourceColumns: base.SourceColumnSet{},- Results: []base.QuerySpanResult{},- }, nil- }-- // TODO(zp): statement type check.- querySpanResult, err := q.getQuerySpanResult(tree.(*parser.RootContext).Stmts().AllUnterminated_sql_statement()[0].Sql_statement_body())- if err != nil {- return nil, err- }-- return &base.QuerySpan{- Type: base.Select,- Results: querySpanResult,- SourceColumns: accessTables,- }, nil-}--func (q *querySpanExtractor) getQuerySpanResult(tree parser.ISql_statement_bodyContext) ([]base.QuerySpanResult, error) {- if tree.Query_statement() == nil {- return nil, errors.Errorf("unsupported non-query statement")- }-- tableSource, err := q.extractTableSourceFromQuery(tree.Query_statement().Query())- if err != nil {- return nil, err- }-- return tableSource.GetQuerySpanResult(), nil-}--func (q *querySpanExtractor) extractTableSourceFromQuery(query parser.IQueryContext) (base.TableSource, error) {- queryWithoutPipe := query.Query_without_pipe_operators()- if queryWithoutPipe == nil {- return nil, errors.Errorf("unsupported query with pipe operators")- }- return q.extractTableSourceFromQueryWithoutPipe(queryWithoutPipe)-}--func (q *querySpanExtractor) extractTableSourceFromQueryWithoutPipe(queryWithoutPipe parser.IQuery_without_pipe_operatorsContext) (base.TableSource, error) {- originalCTELength := len(q.ctes)- defer func() {- q.ctes = q.ctes[:originalCTELength]- }()- if queryWithoutPipe.With_clause() != nil {- if err := q.recordCTE(queryWithoutPipe.With_clause()); err != nil {- return nil, err- }- }- return q.extractTableSourceFromQueryPrimaryOrSetOperation(queryWithoutPipe.Query_primary_or_set_operation())-}--func (q *querySpanExtractor) recordRecursiveCTE(aliasedQuery parser.IAliased_queryContext) error {- cteName := unquoteIdentifierByRule(aliasedQuery.Identifier())- query := aliasedQuery.Parenthesized_query().Query().Query_without_pipe_operators()- if query.With_clause() != nil {- return errors.Errorf("WITH is not allowed inside WITH RECURSIVE")- }- if query.Query_primary_or_set_operation().Query_set_operation() == nil {- return q.recordNonRecursiveCTE(aliasedQuery)- }- prefix := query.Query_primary_or_set_operation().Query_set_operation().Query_set_operation_prefix()- anchor, err := q.extractTableSourceFromQueryPrimary(prefix.Query_primary())- if err != nil {- return err- }- // XXX(zp): How about two union?- recursiveItem := prefix.AllQuery_set_operation_item()[0]- tempCte := &base.PseudoTable{- Name: cteName,- Columns: anchor.GetQuerySpanResult(),- }- q.ctes = append(q.ctes, tempCte)- originalSize := len(q.ctes)- for {- originalSize := len(q.ctes)- recursivePartTableSource, err := q.extractTableSourceFromQueryPrimary(recursiveItem.Query_primary())- if err != nil {- return err- }- anchorQuerySpanResults := q.ctes[originalSize-1].GetQuerySpanResult()- recursivePartQuerySpanResults := recursivePartTableSource.GetQuerySpanResult()- if len(anchorQuerySpanResults) != len(recursivePartQuerySpanResults) {- return errors.Errorf("recursive cte %s clause returns %d fields, but anchor clause returns %d fields", cteName, len(anchorQuerySpanResults), len(recursivePartQuerySpanResults))- }- changed := false- for i := range anchorQuerySpanResults {- var hasChange bool- anchorQuerySpanResults[i].SourceColumns, hasChange = base.MergeSourceColumnSet(anchorQuerySpanResults[i].SourceColumns, recursivePartQuerySpanResults[i].SourceColumns)- changed = changed || hasChange- }- tempCte := &base.PseudoTable{- Name: cteName,- Columns: anchorQuerySpanResults,- }- q.ctes = q.ctes[:originalSize-1]- if !changed {- break- }- q.ctes = append(q.ctes, tempCte)- }- q.ctes = q.ctes[:originalSize-1]- q.ctes = append(q.ctes, tempCte)- return nil-}--func (q *querySpanExtractor) recordNonRecursiveCTE(aliasedQuery parser.IAliased_queryContext) error {- cteName := unquoteIdentifierByRule(aliasedQuery.Identifier())- query := aliasedQuery.Parenthesized_query().Query()- tableSource, err := q.extractNormalCTE(query)- if err != nil {- return err- }- q.ctes = append(q.ctes, &base.PseudoTable{- Name: cteName,- Columns: tableSource.GetQuerySpanResult(),- })- return nil-}--func (q *querySpanExtractor) recordCTE(withClause parser.IWith_clauseContext) error {- allAliasedQuery := withClause.AllAliased_query()- recursive := withClause.RECURSIVE_SYMBOL() != nil- for _, aliasedQuery := range allAliasedQuery {- // TODO(zp): Actually, Spanner do not rely on the RECURSIVE keyword, instead, it detects the recursive CTE- // by the reference of the CTE itself in the CTE body. Also, check other engines.- if recursive {- if err := q.recordRecursiveCTE(aliasedQuery); err != nil {- return err- }- } else {- if err := q.recordNonRecursiveCTE(aliasedQuery); err != nil {- return err- }- }- }- return nil-}--func (q *querySpanExtractor) extractNormalCTE(query parser.IQueryContext) (base.TableSource, error) {- querySpanExtractor := &querySpanExtractor{- ctx: q.ctx,- defaultDatabase: q.defaultDatabase,- gCtx: q.gCtx,- ctes: q.ctes,- }- tableSource, err := querySpanExtractor.extractTableSourceFromQuery(query)- if err != nil {- return nil, err- }- return tableSource, nil-}--func (q *querySpanExtractor) extractTableSourceFromQueryPrimaryOrSetOperation(queryPrimaryOrSetOperation parser.IQuery_primary_or_set_operationContext) (base.TableSource, error) {- if queryPrimaryOrSetOperation.Query_primary() != nil {- return q.extractTableSourceFromQueryPrimary(queryPrimaryOrSetOperation.Query_primary())- }- if queryPrimaryOrSetOperation.Query_set_operation() != nil {- return q.extractTableSourceFromQuerySetOperation(queryPrimaryOrSetOperation.Query_set_operation())- }- return nil, nil-}--func (q *querySpanExtractor) extractTableSourceFromQuerySetOperation(querySetOperation parser.IQuery_set_operationContext) (base.TableSource, error) {- tableSource, err := q.extractTableSourceFromQueryPrimary(querySetOperation.Query_set_operation_prefix().Query_primary())- if err != nil {- return nil, err- }- leftSpanResults := tableSource- for _, item := range querySetOperation.Query_set_operation_prefix().AllQuery_set_operation_item() {- newQ := &querySpanExtractor{- ctx: q.ctx,- defaultDatabase: q.defaultDatabase,- gCtx: q.gCtx,- ctes: q.ctes,- }- rightSpanResults, err := newQ.extractTableSourceFromQueryPrimary(item.Query_primary())- if err != nil {- return nil, err- }- leftQuerySpanResult, rightQuerySpanResult := leftSpanResults.GetQuerySpanResult(), rightSpanResults.GetQuerySpanResult()- if len(leftQuerySpanResult) != len(rightQuerySpanResult) {- return nil, errors.Errorf("left select has %d columns, but right select has %d columns", len(leftQuerySpanResult), len(rightQuerySpanResult))- }- var result []base.QuerySpanResult- for i, leftSpanResult := range leftQuerySpanResult {- rightSpanResult := rightQuerySpanResult[i]- newResourceColumns, _ := base.MergeSourceColumnSet(leftSpanResult.SourceColumns, rightSpanResult.SourceColumns)- result = append(result, base.QuerySpanResult{- Name: leftSpanResult.Name,- SourceColumns: newResourceColumns,- })- }- leftSpanResults = &base.PseudoTable{- Name: "",- Columns: result,- }- // FIXME(zp): Consider UNION alias.- }- return leftSpanResults, nil-}--func (q *querySpanExtractor) extractTableSourceFromQueryPrimary(queryPrimary parser.IQuery_primaryContext) (base.TableSource, error) {- if queryPrimary.Select_() != nil {- return q.extractTableSourceFromSelect(queryPrimary.Select_())- }- if parenthesizedQuery := queryPrimary.Parenthesized_query(); parenthesizedQuery != nil {- // Table subquery shares the ctes of outer query. On the contrary,- // the subquery should not effect the outer query.- // https://cloud.google.com/bigquery/docs/reference/standard-sql/subqueries#correlated_subquery_concepts- originalCtesLength := len(q.ctes)- originalTableSourceFrom := len(q.tableSourceFrom)- defer func() {- q.ctes = q.ctes[:originalCtesLength]- q.tableSourceFrom = q.tableSourceFrom[:originalTableSourceFrom]- }()- subqueryExtractor := &querySpanExtractor{- ctx: q.ctx,- gCtx: q.gCtx,- defaultDatabase: q.defaultDatabase,- ctes: q.ctes,- outerTableSources: q.outerTableSources,- tableSourceFrom: q.tableSourceFrom,- }- tableSource, err := subqueryExtractor.extractTableSourceFromParenthesizedQuery(parenthesizedQuery)- if err != nil {- return nil, err- }-- var alias string- if v := queryPrimary.Opt_as_alias_with_required_as(); v != nil {- if v.Identifier() != nil {- alias = unquoteIdentifierByRule(v.Identifier())- }- }- if alias != "" {- return &base.PseudoTable{- Name: alias,- Columns: tableSource.GetQuerySpanResult(),- }, nil- }-- return tableSource, nil- }- return nil, nil-}--func (q *querySpanExtractor) extractTableSourceFromSelect(selectCtx parser.ISelectContext) (base.TableSource, error) {- var fromFields []base.QuerySpanResult- var resultFields []base.QuerySpanResult- if selectCtx.From_clause() != nil {- tableSource, err := q.extractTableSourceFromFromClause(selectCtx.From_clause())- if err != nil {- return nil, err- }- fromFields = tableSource.GetQuerySpanResult()- }-- itemList := selectCtx.Select_clause().Select_list().AllSelect_list_item()- for _, item := range itemList {- switch {- case item.Select_column_star() != nil:- fields := append([]base.QuerySpanResult{}, fromFields...)- fields, err := q.starModify(fields, item.Select_column_star().Star_modifiers())- if err != nil {- return nil, err- }- resultFields = append(resultFields, fields...)- case item.Select_column_dot_star() != nil:- v := item.Select_column_dot_star()- wildFields, err := q.extractWildFromExpr(v.Expression_higher_prec_than_and())- if err != nil {- return nil, err- }- fields, err := q.starModify(wildFields, item.Select_column_dot_star().Star_modifiers())- if err != nil {- return nil, err- }- resultFields = append(resultFields, fields...)- case item.Select_column_expr() != nil:- v := item.Select_column_expr()- var expression parser.IExpressionContext- if v.Expression() != nil {- expression = v.Expression()- } else if v.Select_column_expr_with_as_alias() != nil {- expression = v.Select_column_expr_with_as_alias().Expression()- }-- var alias parser.IIdentifierContext- if v.Select_column_expr_with_as_alias() != nil {- alias = v.Select_column_expr_with_as_alias().Identifier()- } else if v.Identifier() != nil {- alias = v.Identifier()- }-- name, sourceColumnSet, err := q.extractSourceColumnSetFromExpr(expression)- if err != nil {- return nil, err- }- aliasName := strings.ToUpper(name)- if alias != nil {- aliasName = strings.ToUpper(unquoteIdentifierByRule(alias))- }- resultFields = append(resultFields, base.QuerySpanResult{- Name: aliasName,- SourceColumns: sourceColumnSet,- })- default:- // Ignore other select list item types- }- }- return &base.PseudoTable{- Name: "",- Columns: resultFields,- }, nil-}--func (q *querySpanExtractor) extractTableSourceFromParenthesizedQuery(parenthesizedQuery parser.IParenthesized_queryContext) (base.TableSource, error) {- return q.extractTableSourceFromQuery(parenthesizedQuery.Query())-}--func (q *querySpanExtractor) extractSourceColumnSetFromExpr(ctx antlr.ParserRuleContext) (string, base.SourceColumnSet, error) {- if ctx == nil {- return "", make(base.SourceColumnSet), nil- }-- // BigQuery support project the field from json, for example, SELECT a.b.c FROM ..., the AST looks like:- // /- // └── expression- // └── expression_higher_prec_than_and- // ├── expression_higher_prec_than_and- // │ ├── expression_higher_prec_than_and- // │ │ ├── expression_higher_prec_than_and- // │ │ ├── DOT_SYMBOL- // │ │ └── identifier(a)- // │ ├── DOT_SYMBOL- // │ └── identifier(b)- // ├── DOT_SYMBOL- // └── identifier(c)- // We use DFS algorithm here to find the tallest [expression_higher_prec_than_and DOT_SYMBOL identifier] subtree and- // treat it as identifier.- var name string- switch ctx := ctx.(type) {- case *parser.Parenthesized_queryContext:- baseSet := make(base.SourceColumnSet)- subqueryExtractor := &querySpanExtractor{- ctx: q.ctx,- defaultDatabase: q.defaultDatabase,- gCtx: q.gCtx,- ctes: q.ctes,- outerTableSources: append(q.outerTableSources, q.tableSourceFrom...),- tableSourceFrom: []base.TableSource{},- }- tableSource, err := subqueryExtractor.extractTableSourceFromParenthesizedQuery(ctx)- if err != nil {- return "", nil, err- }- spanResult := tableSource.GetQuerySpanResult()- for _, field := range spanResult {- baseSet, _ = base.MergeSourceColumnSet(field.SourceColumns, field.SourceColumns)- }- return "", baseSet, nil- case *parser.Expression_higher_prec_than_andContext:- baseSet := make(base.SourceColumnSet)- possibleColumnResources := getPossibleColumnResources(ctx)- for _, columnResources := range possibleColumnResources {- l := len(columnResources)- if l == 1 {- sourceColumnSet, err := q.getFieldColumnSource("", columnResources[0])- if err != nil {- return "", nil, err- }- baseSet, _ = base.MergeSourceColumnSet(baseSet, sourceColumnSet)- name = columnResources[0]- }- if l >= 2 {- // a.b, a can be the table name or the field name.- sourceColumnSet, err := q.getFieldColumnSource("", columnResources[0])- if err != nil {- sourceColumnSet, err = q.getFieldColumnSource(columnResources[0], columnResources[1])- if err != nil {- return "", nil, err- }- baseSet, _ = base.MergeSourceColumnSet(baseSet, sourceColumnSet)- name = columnResources[1]- } else {- baseSet, _ = base.MergeSourceColumnSet(baseSet, sourceColumnSet)- name = columnResources[0]- }- }- }- return name, baseSet, nil- default:- }-- baseSet := make(base.SourceColumnSet)- children := ctx.GetChildren()- for _, child := range children {- child, ok := child.(antlr.ParserRuleContext)- if !ok {- continue- }- fieldName, sourceColumnSet, err := q.extractSourceColumnSetFromExpr(child)- if err != nil {- return "", nil, err- }- name = fieldName- baseSet, _ = base.MergeSourceColumnSet(baseSet, sourceColumnSet)- }- if len(children) > 1 {- name = ""- }-- return name, baseSet, nil-}--func (q *querySpanExtractor) starModify(fields []base.QuerySpanResult, starModifier parser.IStar_modifiersContext) ([]base.QuerySpanResult, error) {- if starModifier == nil {- return fields, nil- }-- type fieldItem struct {- id int- field base.QuerySpanResult- }- var fieldItems []fieldItem- for i, field := range fields {- fieldItems = append(fieldItems, fieldItem{- id: i,- field: field,- })- }- fieldItemMap := make(map[string]fieldItem)- for _, fieldItem := range fieldItems {- fieldItemMap[fieldItem.field.Name] = fieldItem- }-- if except := starModifier.Star_except_list(); except != nil {- allIdentifiers := except.AllIdentifier()- for _, identifier := range allIdentifiers {- identifierNormalized := unquoteIdentifierByRule(identifier)- if _, ok := fieldItemMap[identifierNormalized]; !ok {- return nil, errors.Errorf("field %s does not exist in the select clause", identifierNormalized)- }- delete(fieldItemMap, identifierNormalized)- }- }-- if replace := starModifier.Star_replace_list(); replace != nil {- allReplaceItems := replace.AllStar_replace_item()- for _, replaceItem := range allReplaceItems {- _, set, err := q.extractSourceColumnSetFromExpr(replaceItem.Expression())- if err != nil {- return nil, err- }- asIdentifier := unquoteIdentifierByRule(replaceItem.Identifier())- querySpanResult := base.QuerySpanResult{- Name: asIdentifier,- SourceColumns: set,- }- if _, ok := fieldItemMap[asIdentifier]; !ok {- return nil, errors.Errorf("field %s does not exist in the select clause", asIdentifier)- }- fieldItemMap[asIdentifier] = fieldItem{- id: fieldItemMap[asIdentifier].id,- field: querySpanResult,- }- }- }-- fieldItems = nil- for _, fieldItem := range fieldItemMap {- fieldItems = append(fieldItems, fieldItem)- }- slices.SortFunc(fieldItems, func(x, y fieldItem) int {- if x.id < y.id {- return -1- } else if x.id > y.id {- return 1- }- return 0- })-- var result []base.QuerySpanResult- for _, fieldItem := range fieldItems {- result = append(result, fieldItem.field)- }- return result, nil-}--func (q *querySpanExtractor) extractWildFromExpr(ctx antlr.ParserRuleContext) ([]base.QuerySpanResult, error) {- if ctx == nil {- return []base.QuerySpanResult{}, nil- }-- // BigQuery support project the field from json, for example, SELECT a.b.c FROM ..., the AST looks like:- // /- // └── expression- // └── expression_higher_prec_than_and- // ├── expression_higher_prec_than_and- // │ ├── expression_higher_prec_than_and- // │ │ ├── expression_higher_prec_than_and- // │ │ ├── DOT_SYMBOL- // │ │ └── identifier(a)- // │ ├── DOT_SYMBOL- // │ └── identifier(b)- // ├── DOT_SYMBOL- // └── identifier(c)- // We use DFS algorithm here to find the tallest [expression_higher_prec_than_and DOT_SYMBOL identifier] subtree and- // treat it as identifier.- switch ctx := ctx.(type) {- case *parser.Parenthesized_queryContext:- subqueryExtractor := &querySpanExtractor{- ctx: q.ctx,- defaultDatabase: q.defaultDatabase,- gCtx: q.gCtx,- ctes: q.ctes,- outerTableSources: append(q.outerTableSources, q.tableSourceFrom...),- tableSourceFrom: []base.TableSource{},- }- tableSource, err := subqueryExtractor.extractTableSourceFromParenthesizedQuery(ctx)- if err != nil {- return nil, err- }- spanResult := tableSource.GetQuerySpanResult()- return spanResult, nil- case *parser.Expression_higher_prec_than_andContext:- possibleColumnResources := getPossibleColumnResources(ctx)- for _, columnResources := range possibleColumnResources {- l := len(columnResources)- if l == 1 {- // a.*, a can be the table name or the field name.- results, ok := q.getAllTableColumnSources("", columnResources[0])- if !ok {- results, err := q.getFieldColumnSource("", columnResources[0])- if err != nil {- return nil, err- }- return []base.QuerySpanResult{- {- Name: columnResources[0],- SourceColumns: results,- },- }, nil- }- return results, nil- }- if l >= 2 {- // a.b.*, can be resolved as- // 1. a is the table name, b is the field name.- // 2. a is the field name, b is the field name.- results, err := q.getFieldColumnSource(columnResources[0], columnResources[1])- if err != nil {- results, err := q.getFieldColumnSource("", columnResources[0])- if err != nil {- return nil, err- }- return []base.QuerySpanResult{- {- Name: columnResources[0],- SourceColumns: results,- },- }, nil- }- return []base.QuerySpanResult{- {- Name: columnResources[1],- SourceColumns: results,- },- }, nil- }- }- return nil, nil- default:- return nil, errors.Errorf("unsupported type in wild expr: %T", ctx)- }-}--func (q *querySpanExtractor) getAllTableColumnSources(datasetName, tableName string) ([]base.QuerySpanResult, bool) {- findInTableSource := func(tableSource base.TableSource) ([]base.QuerySpanResult, bool) {- if datasetName != "" && !strings.EqualFold(datasetName, tableSource.GetDatabaseName()) {- return nil, false- }- if tableName != "" && !strings.EqualFold(tableName, tableSource.GetTableName()) {- return nil, false- }-- // If the table name is empty, we should check if there are ambiguous fields,- // but we delegate this responsibility to the db-server, we do the fail-open strategy here.-- return tableSource.GetQuerySpanResult(), true- }-- // One sub-query may have multi-outer schemas and the multi-outer schemas can use the same name, such as:- //- // select (- // select (- // select max(a) > x1.a from t- // )- // from t1 as x1- // limit 1- // )- // from t as x1;- //- // This query has two tables can be called `x1`, and the expression x1.a uses the closer x1 table.- // This is the reason we loop the slice in reversed order.- for i := len(q.outerTableSources) - 1; i >= 0; i-- {- if querySpanResult, ok := findInTableSource(q.outerTableSources[i]); ok {- return querySpanResult, true- }- }-- for i := len(q.tableSourceFrom) - 1; i >= 0; i-- {- if querySpanResult, ok := findInTableSource(q.tableSourceFrom[i]); ok {- return querySpanResult, true- }- }-- return nil, false-}--func (q *querySpanExtractor) getFieldColumnSource(tableName, fieldName string) (base.SourceColumnSet, error) {- findInTableSource := func(tableSource base.TableSource) (base.SourceColumnSet, bool) {- if tableName != "" && !strings.EqualFold(tableName, tableSource.GetTableName()) {- return nil, false- }- // If the table name is empty, we should check if there are ambiguous fields,- // but we delegate this responsibility to the db-server, we do the fail-open strategy here.-- querySpanResult := tableSource.GetQuerySpanResult()- for _, field := range querySpanResult {- if strings.EqualFold(field.Name, fieldName) {- return field.SourceColumns, true- }- }- return nil, false- }-- // One sub-query may have multi-outer schemas and the multi-outer schemas can use the same name, such as:- //- // select (- // select (- // select max(a) > x1.a from t- // )- // from t1 as x1- // limit 1- // )- // from t as x1;- //- // This query has two tables can be called `x1`, and the expression x1.a uses the closer x1 table.- // This is the reason we loop the slice in reversed order.-- for i := len(q.outerTableSources) - 1; i >= 0; i-- {- if sourceColumnSet, ok := findInTableSource(q.outerTableSources[i]); ok {- return sourceColumnSet, nil- }- }-- for _, tableSource := range q.tableSourceFrom {- if sourceColumnSet, ok := findInTableSource(tableSource); ok {- return sourceColumnSet, nil- }- }-- return nil, &base.ResourceNotFoundError{- Schema: nil,- Table: &tableName,- Column: &fieldName,- }-}--func isValidExpressionHigherPrecThanAnd(ctx antlr.ParserRuleContext) bool {- if _, ok := ctx.(*parser.Expression_higher_prec_than_andContext); !ok {- return false- }-- terminalNodes := getAllChildTerminalNode(ctx)- if len(terminalNodes) == 1 && terminalNodes[0].GetSymbol().GetTokenType() == parser.GoogleSQLParserIDENTIFIER {- return true- }- child := ctx.GetChildren()- if len(child) == 3 {- first := child[0]- _, ok := first.(*parser.Expression_higher_prec_than_andContext)- if !ok {- return false- }- if !isValidExpressionHigherPrecThanAnd(first.(*parser.Expression_higher_prec_than_andContext)) {- return false- }- second := child[1]- _, ok = second.(*antlr.TerminalNodeImpl)- if !ok {- return false- }- if second.(*antlr.TerminalNodeImpl).GetSymbol().GetTokenType() != parser.GoogleSQLParserDOT_SYMBOL {- return false- }- third := child[2]- _, ok = third.(*parser.IdentifierContext)- return ok- }- return false-}--func getPossibleColumnResources(ctx antlr.ParserRuleContext) [][]string {- // Traverse the tree to find the tallest subtree which matches the pattern:- // [expression_higher_prec_than_and DOT_SYMBOL identifier]- // The result is the possible column resources.- var path []antlr.Tree- path = append(path, ctx)-- var result [][]string- for len(path) > 0 {- element := path[len(path)-1]- path = path[:len(path)-1]- if _, ok := element.(*parser.Parenthesized_queryContext); ok {- // NOTE: while adding the case in extractSourceColumnSetFromExpr, we should skip the case here.- continue- }- appendChild := true- if element, ok := element.(antlr.ParserRuleContext); ok {- valid := isValidExpressionHigherPrecThanAnd(element)- if valid {- appendChild = false- allTerminalNodes := getAllChildTerminalNode(element)- var columnResources []string- for _, terminalNode := range allTerminalNodes {- if terminalNode.GetSymbol().GetTokenType() == parser.GoogleSQLParserIDENTIFIER {- columnResources = append(columnResources, unquoteIdentifierByText(terminalNode.GetText()))- }- }- result = append(result, columnResources)- }- }-- if appendChild {- for _, child := range element.GetChildren() {- if child, ok := child.(antlr.ParserRuleContext); ok {- path = append(path, child)- }- }- }- }- return result-}--func getAllChildTerminalNode(ctx antlr.ParserRuleContext) []antlr.TerminalNode {- allChilds := ctx.GetChildren()- var result []antlr.TerminalNode- for _, child := range allChilds {- if childTerminal, ok := child.(antlr.TerminalNode); ok {- result = append(result, childTerminal)- continue- }- if childRuleCtx, ok := child.(antlr.ParserRuleContext); ok {- subResult := getAllChildTerminalNode(childRuleCtx)- result = append(result, subResult...)- }- }- return result-}--type joinType int--const (- crossJoin = iota- innerJoin- fullOuterJoin- leftOuterJoin- rightOuterJoin-)--func (q *querySpanExtractor) extractTableSourceFromFromClause(fromClause parser.IFrom_clauseContext) (base.TableSource, error) {- contents := fromClause.From_clause_contents()- tableSource, err := q.extractTableSourceFromTablePrimary(contents.Table_primary())- q.tableSourceFrom = append(q.tableSourceFrom, tableSource)- if err != nil {- return nil, err- }- var anchor base.TableSource- anchor = &base.PseudoTable{- Name: "",- Columns: tableSource.GetQuerySpanResult(),- }- allSuffixes := contents.AllFrom_clause_contents_suffix()- for _, suffix := range allSuffixes {- joinType := getJoinTypeFromFromClauseContentsSuffix(suffix)- tableSource, err = q.extractTableSourceFromTablePrimary(suffix.Table_primary())- if err != nil {- return nil, err- }- q.tableSourceFrom = append(q.tableSourceFrom, tableSource)- var usingColumns []string- if suffix.On_or_using_clause_list() != nil {- allJoinOnOrUsingClause := suffix.On_or_using_clause_list().AllOn_or_using_clause()- for _, joinOnOrUsingClause := range allJoinOnOrUsingClause {- if usingClause := joinOnOrUsingClause.Using_clause(); usingClause != nil {- identifiers := usingClause.AllIdentifier()- for _, identifier := range identifiers {- usingColumns = append(usingColumns, unquoteIdentifierByRule(identifier))- }- }- }- }- anchor = joinTable(anchor, joinType, usingColumns, tableSource)- }- q.tableSourceFrom = append(q.tableSourceFrom, anchor)- return anchor, nil-}--func joinTable(anchor base.TableSource, tp joinType, usingColumns []string, tableSource base.TableSource) base.TableSource {- var resultField []base.QuerySpanResult- switch tp {- case crossJoin, innerJoin, fullOuterJoin, leftOuterJoin, rightOuterJoin:- using := make(map[string]bool)- for _, usingColumn := range usingColumns {- using[usingColumn] = true- }- var lFields []base.QuerySpanResult- var rFields []base.QuerySpanResult- usingMerge := make(map[string][]base.QuerySpanResult)- for _, rField := range tableSource.GetQuerySpanResult() {- if _, ok := using[strings.ToUpper(rField.Name)]; ok {- usingMerge[strings.ToUpper(rField.Name)] = append(usingMerge[strings.ToUpper(rField.Name)], rField)- } else {- rFields = append(rFields, rField)- }- }- lFields = append(lFields, anchor.GetQuerySpanResult()...)-- for _, lField := range lFields {- columnSet := lField.SourceColumns- if _, ok := using[strings.ToUpper(lField.Name)]; ok {- mergeItems := usingMerge[strings.ToUpper(lField.Name)]- for _, mergeItem := range mergeItems {- columnSet, _ = base.MergeSourceColumnSet(columnSet, mergeItem.SourceColumns)- }- }- resultField = append(resultField, base.QuerySpanResult{- Name: lField.Name,- SourceColumns: columnSet,- })- }-- resultField = append(resultField, rFields...)- default:- // No special handling for other join types- }- return &base.PseudoTable{- Name: "",- Columns: resultField,- }-}--func getJoinTypeFromJoinType(joinType parser.IJoin_typeContext) joinType {- if joinType == nil {- return innerJoin- }- switch {- case joinType.CROSS_SYMBOL() != nil:- return crossJoin- case joinType.INNER_SYMBOL() != nil:- return innerJoin- case joinType.LEFT_SYMBOL() != nil:- return leftOuterJoin- case joinType.RIGHT_SYMBOL() != nil:- return rightOuterJoin- default:- return innerJoin- }-}--func getJoinTypeFromJoinItem(joinItem parser.IJoin_itemContext) joinType {- return getJoinTypeFromJoinType(joinItem.Join_type())-}--func getJoinTypeFromFromClauseContentsSuffix(fromClauseContentsSuffix parser.IFrom_clause_contents_suffixContext) joinType {- if fromClauseContentsSuffix.COMMA_SYMBOL() != nil {- return crossJoin- }- if fromClauseContentsSuffix.JOIN_SYMBOL() != nil {- return getJoinTypeFromJoinType(fromClauseContentsSuffix.Join_type())- }- return crossJoin-}--func (q *querySpanExtractor) extractTableSourceFromTablePrimary(tablePrimary parser.ITable_primaryContext) (base.TableSource, error) {- if tablePrimary.Tvf_with_suffixes() != nil {- // We do not support table value function because we do not have the returnning columns information.- return nil, errors.Errorf("unsupported table value function: %s", tablePrimary.GetText())- }- if tablePrimary.Table_path_expression() != nil {- return q.extractTableSourceFromTablePathExpression(tablePrimary.Table_path_expression())- }- if subquery := tablePrimary.Table_subquery(); subquery != nil {- return q.extractTableSourceFromTableSubquery(subquery)- }- if join := tablePrimary.Join(); join != nil {- anchor, err := q.extractTableSourceFromTablePrimary(join.Table_primary())- if err != nil {- return nil, err- }- q.tableSourceFrom = append(q.tableSourceFrom, anchor)- for _, item := range join.AllJoin_item() {- joinType := getJoinTypeFromJoinItem(item)- tableSource, err := q.extractTableSourceFromTablePrimary(item.Table_primary())- if err != nil {- return nil, err- }- q.tableSourceFrom = append(q.tableSourceFrom, tableSource)- var usingColumns []string- if item.On_or_using_clause_list() != nil {- allJoinOnOrUsingClause := item.On_or_using_clause_list().AllOn_or_using_clause()- for _, joinOnOrUsingClause := range allJoinOnOrUsingClause {- if usingClause := joinOnOrUsingClause.Using_clause(); usingClause != nil {- identifiers := usingClause.AllIdentifier()- for _, identifier := range identifiers {- usingColumns = append(usingColumns, unquoteIdentifierByRule(identifier))- }- }- }- }- anchor = joinTable(anchor, joinType, usingColumns, tableSource)- }- return anchor, nil- }- if tablePrimary.Table_primary() != nil {- return q.extractTableSourceFromTablePrimary(tablePrimary.Table_primary())- }-- return nil, nil-}--func (q *querySpanExtractor) extractTableSourceFromTableSubquery(subquery parser.ITable_subqueryContext) (base.TableSource, error) {- parenthesizedQuery := subquery.Parenthesized_query()-- // Table subquery shares the ctes of outer query. On the contrary,- // the subquery should not effect the outer query.- // https://cloud.google.com/bigquery/docs/reference/standard-sql/subqueries#correlated_subquery_concepts- originalCtesLength := len(q.ctes)- originalTableSourceFrom := len(q.tableSourceFrom)- defer func() {- q.ctes = q.ctes[:originalCtesLength]- q.tableSourceFrom = q.tableSourceFrom[:originalTableSourceFrom]- }()- subqueryExtractor := &querySpanExtractor{- ctx: q.ctx,- defaultDatabase: q.defaultDatabase,- gCtx: q.gCtx,- ctes: q.ctes,- outerTableSources: q.outerTableSources,- tableSourceFrom: q.tableSourceFrom,- }- tableSource, err := subqueryExtractor.extractTableSourceFromParenthesizedQuery(parenthesizedQuery)- if err != nil {- return nil, err- }-- var alias string- if v := subquery.Opt_pivot_or_unpivot_clause_and_alias(); v != nil {- if v.Identifier() != nil {- alias = unquoteIdentifierByRule(v.Identifier())- }- }- if alias != "" {- return &base.PseudoTable{- Name: alias,- Columns: tableSource.GetQuerySpanResult(),- }, nil- }-- return tableSource, nil-}--func (q *querySpanExtractor) extractTableSourceFromTablePathExpression(tablePathExpression parser.ITable_path_expressionContext) (base.TableSource, error) {- tablePathExprBase := tablePathExpression.Table_path_expression_base()- if tablePathExprBase.Unnest_expression() != nil {- // We do not support unnest expression because we do not have the returnning columns information.- return nil, errors.Errorf("unsupported unnest expression: %s", tablePathExprBase.GetText())- }- var tableName string- var datasetName string-- if slashedOrDashedPathExpression := tablePathExprBase.Maybe_slashed_or_dashed_path_expression(); slashedOrDashedPathExpression != nil {- if slashedOrDashedPathExpression.Maybe_dashed_path_expression() != nil {- if maybeDashedPathExpr := slashedOrDashedPathExpression.Maybe_dashed_path_expression(); maybeDashedPathExpr != nil {- // TODO(zp): support dashed path expression, for example, REGION-us- if maybeDashedPathExpr.Dashed_path_expression() != nil {- return nil, errors.Errorf("unsupported dashed path expression: %s", tablePathExprBase.GetText())- }- // REFACTOR(zp): refactor the code to extract table name and dataset name.- allIdentifiers := maybeDashedPathExpr.Path_expression().AllIdentifier()- if len(allIdentifiers) > 0 {- tableName = unquoteIdentifierByRule(allIdentifiers[len(allIdentifiers)-1])- if len(allIdentifiers) > 1 {- datasetName = unquoteIdentifierByRule(allIdentifiers[len(allIdentifiers)-2])- }- }- }- if slashedOrDashedPathExpression.Slashed_path_expression() != nil {- return nil, errors.Errorf("unsupported slashed path expression: %s", tablePathExprBase.GetText())- }- }- }-- tableSource, err := q.findTableSchema(datasetName, tableName)- if err != nil {- return nil, err- }-- if o := tablePathExpression.Opt_pivot_or_unpivot_clause_and_alias(); o != nil {- if o.Identifier() != nil {- tableSource = &base.PseudoTable{- Name: unquoteIdentifierByRule(o.Identifier()),- Columns: tableSource.GetQuerySpanResult(),- }- }- }- return tableSource, nil-}--func (q *querySpanExtractor) findTableSchema(schemaName string, tableName string) (base.TableSource, error) {- // https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical#case_sensitivity- // Dataset and table names are case-sensitive unless the is_case_insensitive option is set to TRUE.- if schemaName == "" {- for i := len(q.ctes) - 1; i >= 0; i-- {- table := q.ctes[i]- if strings.EqualFold(table.Name, tableName) {- return table, nil- }- }- }-- _, databaseMetadata, err := q.gCtx.GetDatabaseMetadataFunc(q.ctx, q.gCtx.InstanceID, q.defaultDatabase)- if err != nil {- return nil, err- }- if databaseMetadata == nil {- return nil, errors.Errorf("database %q not found", q.defaultDatabase)- }-- schema := databaseMetadata.GetSchemaMetadata(schemaName)- if schema == nil {- return nil, &base.ResourceNotFoundError{- Database: &q.defaultDatabase,- Schema: &schemaName,- }- }- tables := schema.ListTableNames()- var originalTableName string- for _, t := range tables {- if strings.EqualFold(t, tableName) {- originalTableName = t- break- }- }- if originalTableName == "" {- return nil, errors.Errorf("table %q not found", tableName)- }- table := schema.GetTable(originalTableName)- if table == nil {- return nil, errors.Errorf("table %q not found", tableName)- }-- var columns []string- for _, column := range table.GetProto().GetColumns() {- columns = append(columns, column.Name)- }- return &base.PhysicalTable{- Server: "",- Database: q.defaultDatabase,- Schema: schemaName,- Name: table.GetProto().Name,- Columns: columns,- }, nil-}--func getAccessTables(defaultDatabase string, tree antlr.Tree) (base.SourceColumnSet, error) {- l := newAccessTableListener(defaultDatabase)- result := make(base.SourceColumnSet)- antlr.ParseTreeWalkerDefault.Walk(l, tree)- if l.err != nil {- return nil, l.err- }- result, _ = base.MergeSourceColumnSet(result, l.sourceColumnSet)- return result, nil-}--type accessTableListener struct {- *parser.BaseGoogleSQLParserListener-- currentDatabase string- sourceColumnSet base.SourceColumnSet- err error-}--func newAccessTableListener(currentDatabase string) *accessTableListener {- return &accessTableListener{- currentDatabase: currentDatabase,- sourceColumnSet: make(base.SourceColumnSet),- }-}--func (l *accessTableListener) EnterTable_path_expression(ctx *parser.Table_path_expressionContext) {- if l.err != nil {- return- }- // TODO(zp): Handle other unusual table path expression.- exprBase := ctx.Table_path_expression_base()- slashedOrDashedPathExpr := exprBase.Maybe_slashed_or_dashed_path_expression()- if slashedOrDashedPathExpr == nil {- l.err = errors.Errorf("unsupported table path expression: %s", ctx.GetText())- return- }-- dashedPathExpr := slashedOrDashedPathExpr.Maybe_dashed_path_expression()- if dashedPathExpr == nil {- l.err = errors.Errorf("unsupported slashed table path expression: %s", ctx.GetText())- return- }-- pathExpr := dashedPathExpr.Path_expression()- if pathExpr == nil {- l.err = errors.Errorf("unsupported dashed table path expression: %s", ctx.GetText())- }-- // Table name syntax: https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical- // Most of the time, the syntax can be [project_id.][dataset_id.]table_id.- // One difference is that the user access INFORMATION_SCHEMA in dataset,- // the syntax would be [project_id.]([region_id.]|[dataset_id.])INFORMATION_SCHEMA.VIEW_NAME.- // In this case, we treat the INFORMATION_SCHEMA as schema name.- allIdentifiers := pathExpr.AllIdentifier()- if len(allIdentifiers) == 0 {- return- }-- columnSource := base.ColumnResource{- Database: l.currentDatabase,- }- lastIdentifier := allIdentifiers[len(allIdentifiers)-1]- tableName := unquoteIdentifierByRule(lastIdentifier)- columnSource.Table = tableName-- if len(allIdentifiers) >= 2 {- identifier := unquoteIdentifierByRule(allIdentifiers[len(allIdentifiers)-2])- columnSource.Schema = identifier- }-- l.sourceColumnSet[columnSource] = true-}--func unquoteIdentifierByRule(identifier parser.IIdentifierContext) string {- if len(identifier.GetText()) >= 3 && strings.HasPrefix(identifier.GetText(), "`") && strings.HasSuffix(identifier.GetText(), "`") {- return identifier.GetText()[1 : len(identifier.GetText())-1]- }- return identifier.GetText()-}--func unquoteIdentifierByText(identifier string) string {- if len(identifier) >= 3 && strings.HasPrefix(identifier, "`") && strings.HasSuffix(identifier, "`") {- return identifier[1 : len(identifier)-1]- }- return identifier-}--func isMixedQuery(m base.SourceColumnSet) (bool, bool) {- hasSystem, hasUser := false, false- for table := range m {- if isSystemResource(table) {- hasSystem = true- } else {- hasUser = true- }- }-- if hasSystem && hasUser {- return false, true- }-- return !hasUser && hasSystem, false-}--func isSystemResource(resource base.ColumnResource) bool {- return strings.EqualFold(resource.Schema, "INFORMATION_SCHEMA") || strings.EqualFold(resource.Schema, "SPANNER_SYS")-}diff --git a/backend/plugin/parser/spanner/query_span_leak_pins_test.go b/backend/plugin/parser/spanner/query_span_leak_pins_test.gonew file mode 100644index 0000000000..cb35021714--- /dev/null+++ b/backend/plugin/parser/spanner/query_span_leak_pins_test.go@@ -0,0 +1,113 @@+package spanner++import (+ "testing"++ "github.com/stretchr/testify/require"++ storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/plugin/parser/base"+ "github.com/bytebase/bytebase/backend/plugin/parser/googlesql/googlesqltest"+)++// These pins cover masking-leak shapes that CANNOT live in the legacy-recorded+// differential corpus, because the legacy ANTLR resolver either errors on them+// (fail-closed) or produces lineage that is itself a known leak. The Spanner+// set differs from BigQuery's where the legacy resolvers differed: it adds the+// named-schema and system-schema shapes.++// TestLeakPin_SpannerLowercaseUsingCoalesce: legacy spanner only coalesced a+// fully-upper-case-written USING token (its key map was keyed on the raw+// statement text but probed with the upper-cased field name); the lowercase+// output kept BOTH key columns — a positional masking leak. omni coalesces+// case-insensitively, with the key keeping the field's metadata case.+func TestLeakPin_SpannerLowercaseUsingCoalesce(t *testing.T) {+ span, err := googlesqltest.GetSpan(t, storepb.Engine_SPANNER, GetQuerySpan,+ "SELECT * FROM users JOIN logins USING (id);", "db",+ googlesqltest.DefaultSchemaTables(+ &storepb.TableMetadata{Name: "users", Columns: []*storepb.ColumnMetadata{{Name: "id"}, {Name: "email"}}},+ &storepb.TableMetadata{Name: "logins", Columns: []*storepb.ColumnMetadata{{Name: "id"}, {Name: "secret"}}},+ ))+ require.NoError(t, err)+ require.Len(t, span.Results, 3, "USING key must be coalesced: real output is [id, email, secret]")+ require.Equal(t, "id", span.Results[0].Name, "the coalesced key keeps the field's metadata case")+ require.Equal(t, []string{"logins.id", "users.id"}, googlesqltest.SourcesOf(span.Results[0]), "coalesced key reads both sides")+ require.Equal(t, []string{"users.email"}, googlesqltest.SourcesOf(span.Results[1]))+ require.Equal(t, []string{"logins.secret"}, googlesqltest.SourcesOf(span.Results[2]))+}++// TestLeakPin_SpannerUnnestLineage: legacy spanner ERRORED on UNNEST in FROM+// ("unsupported table path expression" — fail-closed); omni resolves it and+// must attribute the element to the array column.+func TestLeakPin_SpannerUnnestLineage(t *testing.T) {+ span, err := googlesqltest.GetSpan(t, storepb.Engine_SPANNER, GetQuerySpan,+ "SELECT elem FROM victim, UNNEST(victim.secret_tokens) AS elem", "db",+ googlesqltest.DefaultSchemaTables(+ &storepb.TableMetadata{Name: "victim", Columns: []*storepb.ColumnMetadata{{Name: "id"}, {Name: "secret_tokens"}}},+ ))+ require.NoError(t, err)+ require.Len(t, span.Results, 1)+ require.Equal(t, []string{"victim.secret_tokens"}, googlesqltest.SourcesOf(span.Results[0]),+ "UNNEST element column must carry the array column's lineage")+}++// TestLeakPin_SpannerByNameMerge: legacy spanner syntax-errors on BY NAME+// (fail-closed); omni parses it and must merge by column NAME, not ordinal.+func TestLeakPin_SpannerByNameMerge(t *testing.T) {+ span, err := googlesqltest.GetSpan(t, storepb.Engine_SPANNER, GetQuerySpan,+ "SELECT id, label FROM lt UNION ALL BY NAME SELECT b_secret AS label, id FROM rt", "db",+ googlesqltest.DefaultSchemaTables(+ &storepb.TableMetadata{Name: "lt", Columns: []*storepb.ColumnMetadata{{Name: "id"}, {Name: "label"}}},+ &storepb.TableMetadata{Name: "rt", Columns: []*storepb.ColumnMetadata{{Name: "id"}, {Name: "b_secret"}}},+ ))+ require.NoError(t, err)+ require.Len(t, span.Results, 2)+ require.Equal(t, []string{"lt.id", "rt.id"}, googlesqltest.SourcesOf(span.Results[0]))+ require.Equal(t, []string{"lt.label", "rt.b_secret"}, googlesqltest.SourcesOf(span.Results[1]),+ "label column carries the right arm's b_secret lineage (it is aliased AS label)")+}++// TestLeakPin_SchemaQualifiedStar: `SELECT analytics.events.* FROM+// analytics.events` — the legacy resolver ERRORED on a schema-qualified dot-star+// ("resource not found: column: analytics" — fail-closed); omni resolves it, so+// the star must expand the named schema's table with schema-qualified lineage.+func TestLeakPin_SchemaQualifiedStar(t *testing.T) {+ span, err := googlesqltest.GetSpan(t, storepb.Engine_SPANNER, GetQuerySpan,+ "SELECT analytics.events.* FROM analytics.events", "db",+ []*storepb.SchemaMetadata{+ {Name: ""},+ {Name: "analytics", Tables: []*storepb.TableMetadata{+ {Name: "events", Columns: []*storepb.ColumnMetadata{{Name: "id"}, {Name: "payload"}}},+ }},+ })+ require.NoError(t, err)+ require.Len(t, span.Results, 2, "the schema-qualified star expands events' two columns")+ require.Equal(t, []string{"analytics.events.id"}, googlesqltest.SourcesOf(span.Results[0]))+ require.Equal(t, []string{"analytics.events.payload"}, googlesqltest.SourcesOf(span.Results[1]))+}++// TestLeakPin_MixedUserSystemRejected: mixing user and system tables must keep+// failing closed (the legacy MixUserSystemTablesError), and a system-only query+// must early-return an EMPTY SelectInfoSchema span (the legacy spanner+// extractor's exact behavior; SPANNER_SYS included).+func TestLeakPin_MixedUserSystemRejected(t *testing.T) {+ userTables := googlesqltest.DefaultSchemaTables(+ &storepb.TableMetadata{Name: "users", Columns: []*storepb.ColumnMetadata{{Name: "id"}}},+ )+ _, err := googlesqltest.GetSpan(t, storepb.Engine_SPANNER, GetQuerySpan,+ "SELECT * FROM users JOIN INFORMATION_SCHEMA.TABLES ON TRUE", "db", userTables)+ require.ErrorIs(t, err, base.MixUserSystemTablesError)++ span, err := googlesqltest.GetSpan(t, storepb.Engine_SPANNER, GetQuerySpan,+ "SELECT * FROM INFORMATION_SCHEMA.TABLES", "db", googlesqltest.DefaultSchemaTables())+ require.NoError(t, err)+ require.Equal(t, base.SelectInfoSchema, span.Type)+ require.Empty(t, span.Results, "system-only query early-returns an empty span (legacy behavior)")+ require.Empty(t, span.SourceColumns)++ span, err = googlesqltest.GetSpan(t, storepb.Engine_SPANNER, GetQuerySpan,+ "SELECT * FROM SPANNER_SYS.QUERY_STATS_TOP_MINUTE", "db", googlesqltest.DefaultSchemaTables())+ require.NoError(t, err)+ require.Equal(t, base.SelectInfoSchema, span.Type, "SPANNER_SYS is a system schema for the Spanner dialect")+ require.Empty(t, span.Results)+}diff --git a/backend/plugin/parser/spanner/query_span_test.go b/backend/plugin/parser/spanner/query_span_test.goindex 863f00d7e9..80f1917502 100644--- a/backend/plugin/parser/spanner/query_span_test.go+++ b/backend/plugin/parser/spanner/query_span_test.go@@ -1,153 +1,17 @@package spannerimport (- "context"- "io"- "os""testing"- "github.com/antlr4-go/antlr/v4"- "github.com/pkg/errors"- "github.com/stretchr/testify/require"- "gopkg.in/yaml.v3"-- parser "github.com/bytebase/parser/googlesql"-- "github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/common/yamltest"storepb "github.com/bytebase/bytebase/backend/generated-go/store"- "github.com/bytebase/bytebase/backend/plugin/parser/base"- "github.com/bytebase/bytebase/backend/store/model"+ "github.com/bytebase/bytebase/backend/plugin/parser/googlesql/googlesqltest")+// TestGetQuerySpan runs the Spanner query-span differential corpus. The+// goldens were RECORDED FROM the legacy ANTLR resolver — they are the+// masking-parity bar and must be reproduced, never re-recorded against this+// implementation (record=true exists for legacy-worktree recording only).func TestGetQuerySpan(t *testing.T) {- type testCase struct {- Description string `yaml:"description,omitempty"`- Statement string `yaml:"statement,omitempty"`- DefaultDatabase string `yaml:"defaultDatabase,omitempty"`- IgnoreCaseSensitve bool `yaml:"ignoreCaseSensitive,omitempty"`- // Metadata is the protojson encoded storepb.DatabaseSchemaMetadata,- // if it's empty, we will use the defaultDatabaseMetadata.- Metadata string `yaml:"metadata,omitempty"`- QuerySpan *base.YamlQuerySpan `yaml:"querySpan,omitempty"`- }-- var (- record = false- testDataPaths = []string{- "test-data/query-span/standard.yaml",- }- )-- a := require.New(t)- for _, testDataPath := range testDataPaths {- testDataPath := testDataPath-- yamlFile, err := os.Open(testDataPath)- a.NoError(err)-- var testCases []testCase- byteValue, err := io.ReadAll(yamlFile)- a.NoError(err)- a.NoError(yamlFile.Close())- a.NoError(yaml.Unmarshal(byteValue, &testCases))-- for i, tc := range testCases {- metadata := &storepb.DatabaseSchemaMetadata{}- a.NoErrorf(common.ProtojsonUnmarshaler.Unmarshal([]byte(tc.Metadata), metadata), "cases %d", i+1)- databaseMetadataGetter, databaseNameLister := buildMockDatabaseMetadataGetter([]*storepb.DatabaseSchemaMetadata{metadata})- result, err := GetQuerySpan(context.TODO(), base.GetQuerySpanContext{- GetDatabaseMetadataFunc: databaseMetadataGetter,- ListDatabaseNamesFunc: databaseNameLister,- }, base.Statement{Text: tc.Statement}, tc.DefaultDatabase, "dbo", tc.IgnoreCaseSensitve)- a.NoErrorf(err, "statement: %s", tc.Statement)- resultYaml := result.ToYaml()- if record {- testCases[i].QuerySpan = resultYaml- } else {- a.Equalf(tc.QuerySpan, resultYaml, "statement: %s", tc.Statement)- }- }-- if record {- yamltest.Record(t, testDataPath, testCases)- }- }-}--func buildMockDatabaseMetadataGetter(databaseMetadata []*storepb.DatabaseSchemaMetadata) (base.GetDatabaseMetadataFunc, base.ListDatabaseNamesFunc) {- return func(_ context.Context, _, databaseName string) (string, *model.DatabaseMetadata, error) {- m := make(map[string]*model.DatabaseMetadata)- for _, metadata := range databaseMetadata {- m[metadata.Name] = model.NewDatabaseMetadata(metadata, nil, nil, storepb.Engine_SPANNER, true /* isObjectCaseSensitive */)- }-- if databaseMetadata, ok := m[databaseName]; ok {- return "", databaseMetadata, nil- }-- return "", nil, errors.Errorf("database %q not found", databaseName)- }, func(_ context.Context, _ string) ([]string, error) {- var names []string- for _, metadata := range databaseMetadata {- names = append(names, metadata.Name)- }- return names, nil- }-}--func TestGetPossibleColumnResources(t *testing.T) {- testCases := []struct {- inputExpr string- want [][]string- }{- // Skip the subquery.- {- inputExpr: "(SELECT a FROM t)",- want: nil,- },- {- inputExpr: "a",- want: [][]string{{"a"}},- },- {- inputExpr: "a.b",- want: [][]string{{"a", "b"}},- },- {- inputExpr: "function_return_json(a.b).c.d",- want: [][]string{{"a", "b"}},- },- {- inputExpr: "a.b + c.d",- want: [][]string{{"c", "d"}, {"a", "b"}},- },- }-- for _, tc := range testCases {- inputStream := antlr.NewInputStream(tc.inputExpr)- lexer := parser.NewGoogleSQLLexer(inputStream)- stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)- p := parser.NewGoogleSQLParser(stream)- // Remove default error listener and add our own error listener.- lexer.RemoveErrorListeners()- lexerErrorListener := &base.ParseErrorListener{- Statement: tc.inputExpr,- }- lexer.AddErrorListener(lexerErrorListener)-- p.RemoveErrorListeners()- parserErrorListener := &base.ParseErrorListener{- Statement: tc.inputExpr,- }- p.AddErrorListener(parserErrorListener)-- p.BuildParseTrees = true-- tree := p.Expression()- require.Nil(t, lexerErrorListener.Err, "input: %s", tc.inputExpr)- require.Nil(t, parserErrorListener.Err, "input: %s", tc.inputExpr)- got := getPossibleColumnResources(tree)- require.Equal(t, tc.want, got, "input: %s", tc.inputExpr)- }+ googlesqltest.RunQuerySpanCorpus(t, storepb.Engine_SPANNER, GetQuerySpan,+ "test-data/query-span/standard.yaml", false /* record */)}diff --git a/backend/plugin/parser/spanner/query_span_type_test.go b/backend/plugin/parser/spanner/query_span_type_test.goindex afde8b495d..f2198eae95 100644--- a/backend/plugin/parser/spanner/query_span_type_test.go+++ b/backend/plugin/parser/spanner/query_span_type_test.go@@ -8,6 +8,7 @@ import (storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/parser/base"+ "github.com/bytebase/bytebase/backend/plugin/parser/googlesql/googlesqltest")// TestQuerySpanTypeForSelect guards BYT-9627: a SELECT must produce a query@@ -15,7 +16,7 @@ import (// the access check rejects a QueryTypeUnknown span as "disallowed query type",// so a missing Type makes every Spanner SELECT fail in normal (non-admin) mode.func TestQuerySpanTypeForSelect(t *testing.T) {- getter, lister := buildMockDatabaseMetadataGetter([]*storepb.DatabaseSchemaMetadata{{Name: "db"}})+ getter, lister := googlesqltest.BuildMockDatabaseMetadataGetter(storepb.Engine_SPANNER, []*storepb.DatabaseSchemaMetadata{{Name: "db"}})result, err := GetQuerySpan(context.Background(),base.GetQuerySpanContext{diff --git a/backend/plugin/parser/spanner/query_type.go b/backend/plugin/parser/spanner/query_type.godeleted file mode 100644index 321a137e06..0000000000--- a/backend/plugin/parser/spanner/query_type.go+++ /dev/null@@ -1,52 +0,0 @@-package spanner--import (- parser "github.com/bytebase/parser/googlesql"- "github.com/pkg/errors"-- "github.com/bytebase/bytebase/backend/plugin/parser/base"-)--type queryTypeListener struct {- *parser.BaseGoogleSQLParserListener-- allSystems bool- result base.QueryType- err error-}--func (l *queryTypeListener) EnterStmts(ctx *parser.StmtsContext) {- // Assume that the stmts contains only one unterminated statement.- if len(ctx.AllUnterminated_sql_statement()) != 1 {- l.err = errors.Errorf("expecting 1 unterminated sql statement, but got %d", len(ctx.AllUnterminated_sql_statement()))- return- }- unterminatedStatement := ctx.AllUnterminated_sql_statement()[0]- l.result = l.getQueryTypeForUnterminatedSQLStatement(unterminatedStatement)-}--func (l *queryTypeListener) getQueryTypeForUnterminatedSQLStatement(u parser.IUnterminated_sql_statementContext) base.QueryType {- body := u.Sql_statement_body()- switch {- case body.Query_statement() != nil:- if l.allSystems {- return base.SelectInfoSchema- }- return base.Select- case body.Alter_statement() != nil, body.Create_constant_statement() != nil, body.Create_connection_statement() != nil, body.Create_database_statement() != nil,- body.Create_function_statement() != nil, body.Create_procedure_statement() != nil, body.Create_index_statement() != nil, body.Create_privilege_restriction_statement() != nil, body.Create_row_access_policy_statement() != nil,- body.Create_external_table_statement() != nil, body.Create_external_table_function_statement() != nil, body.Create_model_statement() != nil, body.Create_property_graph_statement() != nil,- body.Create_schema_statement() != nil, body.Create_external_schema_statement() != nil, body.Create_snapshot_statement() != nil, body.Create_table_function_statement() != nil, body.Create_table_statement() != nil,- body.Create_view_statement() != nil, body.Create_entity_statement() != nil, body.Rename_statement() != nil, body.Drop_all_row_access_policies_statement() != nil, body.Drop_statement() != nil, body.Undrop_statement() != nil:- return base.DDL- case body.Dml_statement() != nil, body.Merge_statement() != nil, body.Call_statement() != nil:- return base.DML- case body.Explain_statement() != nil:- return base.Explain- // Treat SAFE SET as select statement.- case body.Set_statement() != nil:- return base.Select- default:- return base.QueryTypeUnknown- }-}diff --git a/backend/plugin/parser/spanner/spanner.go b/backend/plugin/parser/spanner/spanner.godeleted file mode 100644index 33234eefd4..0000000000--- a/backend/plugin/parser/spanner/spanner.go+++ /dev/null@@ -1,82 +0,0 @@-package spanner--import (- "strings"-- "github.com/antlr4-go/antlr/v4"-- parser "github.com/bytebase/parser/googlesql"-- storepb "github.com/bytebase/bytebase/backend/generated-go/store"- "github.com/bytebase/bytebase/backend/plugin/parser/base"- "github.com/bytebase/bytebase/backend/utils"-)--// ParseSpannerGoogleSQL parses the given SQL and returns a list of ANTLRAST (one per statement).-// Use the GoogleSQL parser based on antlr4.-func ParseSpannerGoogleSQL(sql string) ([]*base.ANTLRAST, error) {- stmts, err := SplitSQL(sql)- if err != nil {- return nil, err- }-- var results []*base.ANTLRAST- for _, stmt := range stmts {- if stmt.Empty {- continue- }-- parseResult, err := parseSingleSpannerGoogleSQL(stmt.Text, stmt.BaseLine())- if err != nil {- return nil, err- }- results = append(results, parseResult)- }-- return results, nil-}--// parseSingleSpannerGoogleSQL parses a single Spanner statement and returns the ANTLRAST.-func parseSingleSpannerGoogleSQL(statement string, baseLine int) (*base.ANTLRAST, error) {- statement = strings.TrimRightFunc(statement, utils.IsSpaceOrSemicolon) + "\n;"- inputStream := antlr.NewInputStream(statement)- lexer := parser.NewGoogleSQLLexer(inputStream)- stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)- p := parser.NewGoogleSQLParser(stream)-- // Remove default error listener and add our own error listener.- startPosition := &storepb.Position{Line: int32(baseLine) + 1}- lexer.RemoveErrorListeners()- lexerErrorListener := &base.ParseErrorListener{- Statement: statement,- StartPosition: startPosition,- }- lexer.AddErrorListener(lexerErrorListener)-- p.RemoveErrorListeners()- parserErrorListener := &base.ParseErrorListener{- Statement: statement,- StartPosition: startPosition,- }- p.AddErrorListener(parserErrorListener)-- p.BuildParseTrees = true-- tree := p.Root()-- if lexerErrorListener.Err != nil {- return nil, lexerErrorListener.Err- }-- if parserErrorListener.Err != nil {- return nil, parserErrorListener.Err- }-- result := &base.ANTLRAST{- StartPosition: startPosition,- Tree: tree,- Tokens: stream,- }-- return result, nil-}diff --git a/backend/plugin/parser/spanner/split.go b/backend/plugin/parser/spanner/split.godeleted file mode 100644index f5e475873a..0000000000--- a/backend/plugin/parser/spanner/split.go+++ /dev/null@@ -1,119 +0,0 @@-package spanner--import (- "github.com/antlr4-go/antlr/v4"- parser "github.com/bytebase/parser/googlesql"- "github.com/pkg/errors"-- "github.com/bytebase/bytebase/backend/common"- storepb "github.com/bytebase/bytebase/backend/generated-go/store"- "github.com/bytebase/bytebase/backend/plugin/parser/base"-)--func init() {- base.RegisterSplitterFunc(storepb.Engine_SPANNER, SplitSQL)-}--// SplitSQL splits the given SQL statement into multiple SQL statements using ANTLR parser.-// This handles BEGIN/END blocks, CASE, IF, LOOP, etc. correctly.-func SplitSQL(statement string) ([]base.Statement, error) {- lexer := parser.NewGoogleSQLLexer(antlr.NewInputStream(statement))- stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)-- p := parser.NewGoogleSQLParser(stream)- lexerErrorListener := &base.ParseErrorListener{- Statement: statement,- }- lexer.RemoveErrorListeners()- lexer.AddErrorListener(lexerErrorListener)-- parserErrorListener := &base.ParseErrorListener{- Statement: statement,- }- p.RemoveErrorListeners()- p.AddErrorListener(parserErrorListener)-- p.BuildParseTrees = true-- tree := p.Root()- if lexerErrorListener.Err != nil {- return nil, lexerErrorListener.Err- }-- if parserErrorListener.Err != nil {- return nil, parserErrorListener.Err- }-- if tree == nil || tree.Stmts() == nil {- return nil, errors.New("failed to split multiple statements")- }-- var result []base.Statement- tokens := stream.GetAllTokens()-- // Get all statement-terminating semicolons from the parse tree- allStatements := tree.Stmts().AllUnterminated_sql_statement()- if len(allStatements) == 0 {- return result, nil- }-- byteOffset := 0- start := 0- for i, stmt := range allStatements {- // Find the semicolon after this statement- var endPos int- if i < len(allStatements)-1 {- // Not the last statement - find the semicolon between this and next statement- nextStmt := allStatements[i+1]- nextStmtStart := nextStmt.GetStart().GetTokenIndex()- // Find the semicolon just before the next statement- endPos = nextStmtStart - 1- // Skip back to find the actual semicolon- for endPos >= start && tokens[endPos].GetTokenType() != parser.GoogleSQLLexerSEMI_SYMBOL {- endPos--- }- } else {- // Last statement - may or may not have semicolon- stmtStop := stmt.GetStop().GetTokenIndex()- endPos = stmtStop- // Check if there's a semicolon after the statement- for endPos < len(tokens)-1 {- endPos++- if tokens[endPos].GetTokenType() == parser.GoogleSQLLexerSEMI_SYMBOL {- break- }- }- }-- if endPos < start {- continue- }-- stmtText := stream.GetTextFromTokens(tokens[start], tokens[endPos])- stmtByteLength := len(stmtText)-- // Calculate start position from byte offset (first character of Text)- startLine, startColumn := base.CalculateLineAndColumn(statement, byteOffset)- result = append(result, base.Statement{- Text: stmtText,- Range: &storepb.Range{- Start: int32(byteOffset),- End: int32(byteOffset + stmtByteLength),- },- End: common.ConvertANTLRTokenToExclusiveEndPosition(- int32(tokens[endPos].GetLine()),- int32(tokens[endPos].GetColumn()),- tokens[endPos].GetText(),- ),- Start: &storepb.Position{- Line: int32(startLine + 1),- Column: int32(startColumn + 1),- },- Empty: base.IsEmpty(tokens[start:endPos+1], parser.GoogleSQLLexerSEMI_SYMBOL),- })- byteOffset += stmtByteLength- start = endPos + 1- }-- return result, nil-}diff --git a/backend/plugin/parser/spanner/test-data/query-span/standard.yaml b/backend/plugin/parser/spanner/test-data/query-span/standard.yamlindex 8485d92c8d..2e2de5cd67 100644--- a/backend/plugin/parser/spanner/test-data/query-span/standard.yaml+++ b/backend/plugin/parser/spanner/test-data/query-span/standard.yaml@@ -39,47 +39,47 @@querySpan:type: 1results:- - name: Id- sourcecolumns:- - server: ""- database: maskingdb- schema: MySchema- table: Customers- column: Id- isplainfield: false- sourcefieldpaths: []- selectasterisk: false- - name: Name- sourcecolumns:- - server: ""- database: maskingdb- schema: MySchema- table: Customers- column: Name- isplainfield: false- sourcefieldpaths: []- selectasterisk: false- - name: CustomerId- sourcecolumns:- - server: ""- database: maskingdb- schema: ""- table: Orders- column: CustomerId- isplainfield: true- sourcefieldpaths: []- selectasterisk: false+ - name: Id+ sourcecolumns:+ - server: ""+ database: maskingdb+ schema: MySchema+ table: Customers+ column: Id+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: Name+ sourcecolumns:+ - server: ""+ database: maskingdb+ schema: MySchema+ table: Customers+ column: Name+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: CustomerId+ sourcecolumns:+ - server: ""+ database: maskingdb+ schema: ""+ table: Orders+ column: CustomerId+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: falsesourcecolumns:- - server: ""- database: maskingdb- schema: MySchema- table: Customers- column: ""- - server: ""- database: maskingdb- schema: ""- table: Orders- column: ""+ - server: ""+ database: maskingdb+ schema: MySchema+ table: Customers+ column: ""+ - server: ""+ database: maskingdb+ schema: ""+ table: Orders+ column: ""predicatecolumns: []- description: Except and replacestatement: SELECT * EXCEPT (ID) REPLACE (ID/2 AS NAME) FROM people;@@ -109,22 +109,22 @@querySpan:type: 1results:- - name: NAME- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: people- column: ID- isplainfield: false- sourcefieldpaths: []- selectasterisk: false+ - name: NAME+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: people+ column: ID+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: falsesourcecolumns:- - server: ""- database: ds1- schema: ""- table: people- column: ""+ - server: ""+ database: ds1+ schema: ""+ table: people+ column: ""predicatecolumns: []- description: Set operationstatement: WITH CTE_1 AS (SELECT * FROM people), CTE_2 AS (SELECT * FROM address) SELECT * FROM CTE_1 UNION ALL SELECT * FROM CTE_2;@@ -165,59 +165,59 @@querySpan:type: 1results:- - name: ID- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: ADDRESS- column: PEOPLE_ID- - server: ""- database: ds1- schema: ""- table: PEOPLE- column: ID- isplainfield: false- sourcefieldpaths: []- selectasterisk: false- - name: NAME- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: ADDRESS- column: ADDRESS- - server: ""- database: ds1- schema: ""- table: PEOPLE- column: NAME- isplainfield: false- sourcefieldpaths: []- selectasterisk: false+ - name: ID+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: ADDRESS+ column: PEOPLE_ID+ - server: ""+ database: ds1+ schema: ""+ table: PEOPLE+ column: ID+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: NAME+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: ADDRESS+ column: ADDRESS+ - server: ""+ database: ds1+ schema: ""+ table: PEOPLE+ column: NAME+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: falsesourcecolumns:- - server: ""- database: ds1- schema: ""- table: CTE_1- column: ""- - server: ""- database: ds1- schema: ""- table: CTE_2- column: ""- - server: ""- database: ds1- schema: ""- table: address- column: ""- - server: ""- database: ds1- schema: ""- table: people- column: ""+ - server: ""+ database: ds1+ schema: ""+ table: CTE_1+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: CTE_2+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: address+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: people+ column: ""predicatecolumns: []-- description: Parenthesized join+- description: Parenthesized join (ON+USING legacy over-accept; ADDRESS deliberately has NO ID column so the USING(ID) coalesce is one-sided — with an ADDRESS.ID the legacy resolver under-reports the explicit ID lineage to PEOPLE.ID only while omni correctly merges both sides; reshaped so both agree, mirroring the bigquery sibling)statement: SELECT ID, NAME, ADDRESS FROM (people JOIN address ON people.ID = address.PEOPLE_ID USING (ID));defaultDatabase: ds1metadata: |-@@ -241,9 +241,6 @@{"name": "ADDRESS","columns": [- {- "name": "ID"- },{"name": "PEOPLE_ID"},@@ -259,49 +256,49 @@querySpan:type: 1results:- - name: ID- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: PEOPLE- column: ID- isplainfield: false- sourcefieldpaths: []- selectasterisk: false- - name: NAME- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: PEOPLE- column: NAME- isplainfield: false- sourcefieldpaths: []- selectasterisk: false- - name: ADDRESS- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: ADDRESS- column: ADDRESS- isplainfield: false- sourcefieldpaths: []- selectasterisk: false+ - name: ID+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: PEOPLE+ column: ID+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: NAME+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: PEOPLE+ column: NAME+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: ADDRESS+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: ADDRESS+ column: ADDRESS+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: falsesourcecolumns:- - server: ""- database: ds1- schema: ""- table: address- column: ""- - server: ""- database: ds1- schema: ""- table: people- column: ""+ - server: ""+ database: ds1+ schema: ""+ table: address+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: people+ column: ""predicatecolumns: []-- description: Join using+- description: Join using (ON+USING legacy over-accept; ADDRESS deliberately has NO ID column — see the Parenthesized-join sibling note)statement: SELECT ID, NAME, ADDRESS FROM people JOIN address ON people.ID = address.PEOPLE_ID USING (ID);defaultDatabase: ds1metadata: |-@@ -325,9 +322,6 @@{"name": "ADDRESS","columns": [- {- "name": "ID"- },{"name": "PEOPLE_ID"},@@ -343,47 +337,47 @@querySpan:type: 1results:- - name: ID- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: PEOPLE- column: ID- isplainfield: false- sourcefieldpaths: []- selectasterisk: false- - name: NAME- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: PEOPLE- column: NAME- isplainfield: false- sourcefieldpaths: []- selectasterisk: false- - name: ADDRESS- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: ADDRESS- column: ADDRESS- isplainfield: false- sourcefieldpaths: []- selectasterisk: false+ - name: ID+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: PEOPLE+ column: ID+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: NAME+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: PEOPLE+ column: NAME+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: ADDRESS+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: ADDRESS+ column: ADDRESS+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: falsesourcecolumns:- - server: ""- database: ds1- schema: ""- table: address- column: ""- - server: ""- database: ds1- schema: ""- table: people- column: ""+ - server: ""+ database: ds1+ schema: ""+ table: address+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: people+ column: ""predicatecolumns: []- description: Simple joinstatement: SELECT ID, NAME, ADDRESS FROM people JOIN address ON people.ID = address.PEOPLE_ID;@@ -424,47 +418,47 @@querySpan:type: 1results:- - name: ID- sourcecolumns:- - server: ""- database: maskingdb- schema: ""- table: People- column: Id- isplainfield: false- sourcefieldpaths: []- selectasterisk: false- - name: NAME- sourcecolumns:- - server: ""- database: maskingdb- schema: ""- table: People- column: Name- isplainfield: false- sourcefieldpaths: []- selectasterisk: false- - name: ADDRESS- sourcecolumns:- - server: ""- database: maskingdb- schema: ""- table: Address- column: Address- isplainfield: false- sourcefieldpaths: []- selectasterisk: false+ - name: ID+ sourcecolumns:+ - server: ""+ database: maskingdb+ schema: ""+ table: People+ column: Id+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: NAME+ sourcecolumns:+ - server: ""+ database: maskingdb+ schema: ""+ table: People+ column: Name+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: ADDRESS+ sourcecolumns:+ - server: ""+ database: maskingdb+ schema: ""+ table: Address+ column: Address+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: falsesourcecolumns:- - server: ""- database: maskingdb- schema: ""- table: address- column: ""- - server: ""- database: maskingdb- schema: ""- table: people- column: ""+ - server: ""+ database: maskingdb+ schema: ""+ table: address+ column: ""+ - server: ""+ database: maskingdb+ schema: ""+ table: people+ column: ""predicatecolumns: []- description: Recursive ctestatement: |-@@ -503,72 +497,72 @@querySpan:type: 1results:- - name: C1- sourcecolumns:- - server: ""- database: maskingdb- schema: ""- table: T1- column: a- - server: ""- database: maskingdb- schema: ""- table: T1- column: b- isplainfield: false- sourcefieldpaths: []- selectasterisk: false- - name: C2- sourcecolumns:- - server: ""- database: maskingdb- schema: ""- table: T1- column: a- - server: ""- database: maskingdb- schema: ""- table: T1- column: b- isplainfield: false- sourcefieldpaths: []- selectasterisk: false- - name: C3- sourcecolumns:- - server: ""- database: maskingdb- schema: ""- table: T1- column: a- - server: ""- database: maskingdb- schema: ""- table: T1- column: b- - server: ""- database: maskingdb- schema: ""- table: T1- column: c- isplainfield: false- sourcefieldpaths: []- selectasterisk: false- - name: "N"- sourcecolumns: []- isplainfield: false- sourcefieldpaths: []- selectasterisk: false+ - name: C1+ sourcecolumns:+ - server: ""+ database: maskingdb+ schema: ""+ table: T1+ column: a+ - server: ""+ database: maskingdb+ schema: ""+ table: T1+ column: b+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: C2+ sourcecolumns:+ - server: ""+ database: maskingdb+ schema: ""+ table: T1+ column: a+ - server: ""+ database: maskingdb+ schema: ""+ table: T1+ column: b+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: C3+ sourcecolumns:+ - server: ""+ database: maskingdb+ schema: ""+ table: T1+ column: a+ - server: ""+ database: maskingdb+ schema: ""+ table: T1+ column: b+ - server: ""+ database: maskingdb+ schema: ""+ table: T1+ column: c+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: "N"+ sourcecolumns: []+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: falsesourcecolumns:- - server: ""- database: maskingdb- schema: ""- table: CTE_01- column: ""- - server: ""- database: maskingdb- schema: ""- table: T1- column: ""+ - server: ""+ database: maskingdb+ schema: ""+ table: CTE_01+ column: ""+ - server: ""+ database: maskingdb+ schema: ""+ table: T1+ column: ""predicatecolumns: []- description: Simple normal ctestatement: WITH CTE_1 AS (SELECT Id, Name FROM Customers) SELECT * FROM CTE_1;@@ -598,37 +592,37 @@querySpan:type: 1results:- - name: ID- sourcecolumns:- - server: ""- database: maskingdb- schema: ""- table: Customers- column: Id- isplainfield: false- sourcefieldpaths: []- selectasterisk: false- - name: NAME- sourcecolumns:- - server: ""- database: maskingdb- schema: ""- table: Customers- column: Name- isplainfield: false- sourcefieldpaths: []- selectasterisk: false+ - name: ID+ sourcecolumns:+ - server: ""+ database: maskingdb+ schema: ""+ table: Customers+ column: Id+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: NAME+ sourcecolumns:+ - server: ""+ database: maskingdb+ schema: ""+ table: Customers+ column: Name+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: falsesourcecolumns:- - server: ""- database: maskingdb- schema: ""- table: CTE_1- column: ""- - server: ""- database: maskingdb- schema: ""- table: Customers- column: ""+ - server: ""+ database: maskingdb+ schema: ""+ table: CTE_1+ column: ""+ - server: ""+ database: maskingdb+ schema: ""+ table: Customers+ column: ""predicatecolumns: []- description: Correlated subquery in the target field clause.statement: SELECT city, (SELECT COUNT(*) FROM paintings p WHERE g.id = p.gallery_id) AS total_paintings FROM galleries g;@@ -675,32 +669,32 @@querySpan:type: 1results:- - name: CITY- sourcecolumns:- - server: ""- database: ds1- schema: ""- table: GALLERIES- column: CITY- isplainfield: false- sourcefieldpaths: []- selectasterisk: false- - name: TOTAL_PAINTINGS- sourcecolumns: []- isplainfield: false- sourcefieldpaths: []- selectasterisk: false+ - name: CITY+ sourcecolumns:+ - server: ""+ database: ds1+ schema: ""+ table: GALLERIES+ column: CITY+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: TOTAL_PAINTINGS+ sourcecolumns: []+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: falsesourcecolumns:- - server: ""- database: ds1- schema: ""- table: galleries- column: ""- - server: ""- database: ds1- schema: ""- table: paintings- column: ""+ - server: ""+ database: ds1+ schema: ""+ table: galleries+ column: ""+ - server: ""+ database: ds1+ schema: ""+ table: paintings+ column: ""predicatecolumns: []- description: FIXME(zp)Expression subquerystatement: SELECT (SELECT Name FROM Customers), ID FROM Customers;@@ -730,27 +724,27 @@querySpan:type: 1results:- - name: ""- sourcecolumns: []- isplainfield: false- sourcefieldpaths: []- selectasterisk: false- - name: ID- sourcecolumns:- - server: ""- database: maskingdb- schema: ""- table: Customers- column: Id- isplainfield: false- sourcefieldpaths: []- selectasterisk: false+ - name: ""+ sourcecolumns: []+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: ID+ sourcecolumns:+ - server: ""+ database: maskingdb+ schema: ""+ table: Customers+ column: Id+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: falsesourcecolumns:- - server: ""- database: maskingdb- schema: ""- table: Customers- column: ""+ - server: ""+ database: maskingdb+ schema: ""+ table: Customers+ column: ""predicatecolumns: []- description: Table subquerystatement: WITH A AS (SELECT * FROM Customers) SELECT result.ID, * FROM (SELECT * FROM A) result;@@ -780,47 +774,47 @@querySpan:type: 1results:- - name: ID- sourcecolumns:- - server: ""- database: maskingdb- schema: ""- table: Customers- column: Id- isplainfield: false- sourcefieldpaths: []- selectasterisk: false- - name: Id- sourcecolumns:- - server: ""- database: maskingdb- schema: ""- table: Customers- column: Id- isplainfield: true- sourcefieldpaths: []- selectasterisk: false- - name: Name- sourcecolumns:- - server: ""- database: maskingdb- schema: ""- table: Customers- column: Name- isplainfield: true- sourcefieldpaths: []- selectasterisk: false+ - name: ID+ sourcecolumns:+ - server: ""+ database: maskingdb+ schema: ""+ table: Customers+ column: Id+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: Id+ sourcecolumns:+ - server: ""+ database: maskingdb+ schema: ""+ table: Customers+ column: Id+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: false+ - name: Name+ sourcecolumns:+ - server: ""+ database: maskingdb+ schema: ""+ table: Customers+ column: Name+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: falsesourcecolumns:- - server: ""- database: maskingdb- schema: ""- table: A- column: ""- - server: ""- database: maskingdb- schema: ""- table: Customers- column: ""+ - server: ""+ database: maskingdb+ schema: ""+ table: A+ column: ""+ - server: ""+ database: maskingdb+ schema: ""+ table: Customers+ column: ""predicatecolumns: []- description: Wild projecterstatement: SELECT s.* FROM Customers s;@@ -850,32 +844,32 @@querySpan:type: 1results:- - name: Id- sourcecolumns:- - server: ""- database: maskingdb- schema: ""- table: Customers- column: Id- isplainfield: true- sourcefieldpaths: []- selectasterisk: false- - name: Name- sourcecolumns:- - server: ""- database: maskingdb- schema: ""- table: Customers- column: Name- isplainfield: true- sourcefieldpaths: []- selectasterisk: false+ - name: Id+ sourcecolumns:+ - server: ""+ database: maskingdb+ schema: ""+ table: Customers+ column: Id+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: false+ - name: Name+ sourcecolumns:+ - server: ""+ database: maskingdb+ schema: ""+ table: Customers+ column: Name+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: falsesourcecolumns:- - server: ""- database: maskingdb- schema: ""- table: Customers- column: ""+ - server: ""+ database: maskingdb+ schema: ""+ table: Customers+ column: ""predicatecolumns: []- description: Simple projecterstatement: SELECT Id, Name FROM Customers;@@ -905,32 +899,32 @@querySpan:type: 1results:- - name: ID- sourcecolumns:- - server: ""- database: maskingdb- schema: ""- table: Customers- column: Id- isplainfield: false- sourcefieldpaths: []- selectasterisk: false- - name: NAME- sourcecolumns:- - server: ""- database: maskingdb- schema: ""- table: Customers- column: Name- isplainfield: false- sourcefieldpaths: []- selectasterisk: false+ - name: ID+ sourcecolumns:+ - server: ""+ database: maskingdb+ schema: ""+ table: Customers+ column: Id+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: NAME+ sourcecolumns:+ - server: ""+ database: maskingdb+ schema: ""+ table: Customers+ column: Name+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: falsesourcecolumns:- - server: ""- database: maskingdb- schema: ""- table: Customers- column: ""+ - server: ""+ database: maskingdb+ schema: ""+ table: Customers+ column: ""predicatecolumns: []- description: Expression in target fieldstatement: SELECT Id+1, Id+Name, foo(bar(Id), Name) FROM Customers;@@ -960,52 +954,52 @@querySpan:type: 1results:- - name: ID- sourcecolumns:- - server: ""- database: maskingdb- schema: ""- table: Customers- column: Id- isplainfield: false- sourcefieldpaths: []- selectasterisk: false- - name: ID- sourcecolumns:- - server: ""- database: maskingdb- schema: ""- table: Customers- column: Id- - server: ""- database: maskingdb- schema: ""- table: Customers- column: Name- isplainfield: false- sourcefieldpaths: []- selectasterisk: false- - name: ID- sourcecolumns:- - server: ""- database: maskingdb- schema: ""- table: Customers- column: Id- - server: ""- database: maskingdb- schema: ""- table: Customers- column: Name- isplainfield: false- sourcefieldpaths: []- selectasterisk: false+ - name: ID+ sourcecolumns:+ - server: ""+ database: maskingdb+ schema: ""+ table: Customers+ column: Id+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: ID+ sourcecolumns:+ - server: ""+ database: maskingdb+ schema: ""+ table: Customers+ column: Id+ - server: ""+ database: maskingdb+ schema: ""+ table: Customers+ column: Name+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: ID+ sourcecolumns:+ - server: ""+ database: maskingdb+ schema: ""+ table: Customers+ column: Id+ - server: ""+ database: maskingdb+ schema: ""+ table: Customers+ column: Name+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: falsesourcecolumns:- - server: ""- database: maskingdb- schema: ""- table: Customers- column: ""+ - server: ""+ database: maskingdb+ schema: ""+ table: Customers+ column: ""predicatecolumns: []- description: Simple select asterisk statementstatement: SELECT * FROM Customers;@@ -1035,30 +1029,1966 @@querySpan:type: 1results:- - name: Id- sourcecolumns:- - server: ""- database: maskingdb- schema: ""- table: Customers- column: Id- isplainfield: true- sourcefieldpaths: []- selectasterisk: false- - name: Name- sourcecolumns:- - server: ""- database: maskingdb- schema: ""- table: Customers- column: Name- isplainfield: true- sourcefieldpaths: []- selectasterisk: false+ - name: Id+ sourcecolumns:+ - server: ""+ database: maskingdb+ schema: ""+ table: Customers+ column: Id+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: false+ - name: Name+ sourcecolumns:+ - server: ""+ database: maskingdb+ schema: ""+ table: Customers+ column: Name+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: maskingdb+ schema: ""+ table: Customers+ column: ""+ predicatecolumns: []+- description: Set op union all equal width explicit with sensitive column on second arm+ statement: SELECT id, name FROM users UNION ALL SELECT order_id, card_number FROM orders;+ defaultDatabase: spannerdb+ metadata: |-+ {+ "name": "spannerdb",+ "schemas": [+ {+ "name": "",+ "tables": [+ {"name": "users", "columns": [{"name": "id"}, {"name": "name"}, {"name": "email"}, {"name": "ssn"}]},+ {"name": "orders", "columns": [{"name": "order_id"}, {"name": "user_id"}, {"name": "amount"}, {"name": "card_number"}]}+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: ID+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: order_id+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: id+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: NAME+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: card_number+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: name+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: ""+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: ""+ predicatecolumns: []+- description: Set op union all three way with sensitive column on third arm+ statement: SELECT id FROM users UNION ALL SELECT order_id FROM orders UNION ALL SELECT iban FROM payments;+ defaultDatabase: spannerdb+ metadata: |-+ {+ "name": "spannerdb",+ "schemas": [+ {+ "name": "",+ "tables": [+ {"name": "users", "columns": [{"name": "id"}, {"name": "name"}, {"name": "email"}, {"name": "ssn"}]},+ {"name": "orders", "columns": [{"name": "order_id"}, {"name": "user_id"}, {"name": "amount"}, {"name": "card_number"}]},+ {"name": "payments", "columns": [{"name": "payment_id"}, {"name": "order_id"}, {"name": "paid_amount"}, {"name": "iban"}]}+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: ID+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: order_id+ - server: ""+ database: spannerdb+ schema: ""+ table: payments+ column: iban+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: id+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: ""+ - server: ""+ database: spannerdb+ schema: ""+ table: payments+ column: ""+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: ""+ predicatecolumns: []+- description: Set op union all with select star first arm+ statement: SELECT * FROM users UNION ALL SELECT order_id, user_id, amount, card_number FROM orders;+ defaultDatabase: spannerdb+ metadata: |-+ {+ "name": "spannerdb",+ "schemas": [+ {+ "name": "",+ "tables": [+ {"name": "users", "columns": [{"name": "id"}, {"name": "name"}, {"name": "email"}, {"name": "ssn"}]},+ {"name": "orders", "columns": [{"name": "order_id"}, {"name": "user_id"}, {"name": "amount"}, {"name": "card_number"}]}+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: id+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: order_id+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: id+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: name+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: user_id+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: name+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: email+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: amount+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: email+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: ssn+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: card_number+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: ssn+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: ""+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: ""+ predicatecolumns: []+- description: Set op union distinct+ statement: SELECT name FROM users UNION DISTINCT SELECT card_number FROM orders;+ defaultDatabase: spannerdb+ metadata: |-+ {+ "name": "spannerdb",+ "schemas": [+ {+ "name": "",+ "tables": [+ {"name": "users", "columns": [{"name": "id"}, {"name": "name"}, {"name": "email"}, {"name": "ssn"}]},+ {"name": "orders", "columns": [{"name": "order_id"}, {"name": "user_id"}, {"name": "amount"}, {"name": "card_number"}]}+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: NAME+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: card_number+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: name+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: ""+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: ""+ predicatecolumns: []+- description: Set op intersect distinct+ statement: SELECT email FROM users INTERSECT DISTINCT SELECT iban FROM payments;+ defaultDatabase: spannerdb+ metadata: |-+ {+ "name": "spannerdb",+ "schemas": [+ {+ "name": "",+ "tables": [+ {"name": "users", "columns": [{"name": "id"}, {"name": "name"}, {"name": "email"}, {"name": "ssn"}]},+ {"name": "payments", "columns": [{"name": "payment_id"}, {"name": "order_id"}, {"name": "paid_amount"}, {"name": "iban"}]}+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: EMAIL+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: payments+ column: iban+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: email+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: payments+ column: ""+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: ""+ predicatecolumns: []+- description: Set op except distinct still taints from excluded arm+ statement: SELECT phone FROM profiles EXCEPT DISTINCT SELECT card_number FROM orders;+ defaultDatabase: spannerdb+ metadata: |-+ {+ "name": "spannerdb",+ "schemas": [+ {+ "name": "",+ "tables": [+ {"name": "profiles", "columns": [{"name": "id"}, {"name": "address"}, {"name": "phone"}]},+ {"name": "orders", "columns": [{"name": "order_id"}, {"name": "user_id"}, {"name": "amount"}, {"name": "card_number"}]}+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: PHONE+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: card_number+ - server: ""+ database: spannerdb+ schema: ""+ table: profiles+ column: phone+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: ""+ - server: ""+ database: spannerdb+ schema: ""+ table: profiles+ column: ""+ predicatecolumns: []+- description: Set op different arm names keeps first arm name+ statement: SELECT id AS user_key FROM users UNION ALL SELECT order_id AS order_key FROM orders;+ defaultDatabase: spannerdb+ metadata: |-+ {+ "name": "spannerdb",+ "schemas": [+ {+ "name": "",+ "tables": [+ {"name": "users", "columns": [{"name": "id"}, {"name": "name"}, {"name": "email"}, {"name": "ssn"}]},+ {"name": "orders", "columns": [{"name": "order_id"}, {"name": "user_id"}, {"name": "amount"}, {"name": "card_number"}]}+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: USER_KEY+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: order_id+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: id+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: ""+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: ""+ predicatecolumns: []+- description: Set op of two subset projecting ctes+ statement: WITH safe_users AS (SELECT id, name FROM users), safe_orders AS (SELECT order_id, card_number FROM orders) SELECT * FROM safe_users UNION ALL SELECT * FROM safe_orders;+ defaultDatabase: spannerdb+ metadata: |-+ {+ "name": "spannerdb",+ "schemas": [+ {+ "name": "",+ "tables": [+ {"name": "users", "columns": [{"name": "id"}, {"name": "name"}, {"name": "email"}, {"name": "ssn"}]},+ {"name": "orders", "columns": [{"name": "order_id"}, {"name": "user_id"}, {"name": "amount"}, {"name": "card_number"}]}+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: ID+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: order_id+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: id+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: NAME+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: card_number+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: name+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: ""+ - server: ""+ database: spannerdb+ schema: ""+ table: safe_orders+ column: ""+ - server: ""+ database: spannerdb+ schema: ""+ table: safe_users+ column: ""+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: ""+ predicatecolumns: []+- description: Join on with sensitive columns unprojected+ statement: SELECT u.name, o.amount FROM users u JOIN orders o ON u.id = o.user_id;+ defaultDatabase: spannerdb+ metadata: |-+ {+ "name": "spannerdb",+ "schemas": [+ {+ "name": "",+ "tables": [+ {"name": "users", "columns": [{"name": "id"}, {"name": "name"}, {"name": "email"}, {"name": "ssn"}]},+ {"name": "orders", "columns": [{"name": "order_id"}, {"name": "user_id"}, {"name": "amount"}, {"name": "card_number"}]}+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: NAME+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: name+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: AMOUNT+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: amount+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: ""+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: ""+ predicatecolumns: []+- description: Join using uppercase identifier coalesces+ statement: SELECT * FROM users JOIN logins USING (ID);+ defaultDatabase: spannerdb+ metadata: |-+ {+ "name": "spannerdb",+ "schemas": [+ {+ "name": "",+ "tables": [+ {"name": "users", "columns": [{"name": "id"}, {"name": "name"}, {"name": "email"}, {"name": "ssn"}]},+ {"name": "logins", "columns": [{"name": "id"}, {"name": "device"}, {"name": "ip_address"}]}+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: id+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: logins+ column: id+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: id+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: name+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: name+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: email+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: email+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: ssn+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: ssn+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: device+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: logins+ column: device+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: false+ - name: ip_address+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: logins+ column: ip_address+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: logins+ column: ""+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: ""+ predicatecolumns: []+- description: Self join with aliases+ statement: SELECT a.full_name, b.salary FROM employees a JOIN employees b ON a.manager_id = b.emp_id;+ defaultDatabase: spannerdb+ metadata: |-+ {+ "name": "spannerdb",+ "schemas": [+ {+ "name": "",+ "tables": [+ {"name": "employees", "columns": [{"name": "emp_id"}, {"name": "manager_id"}, {"name": "full_name"}, {"name": "salary"}]}+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: FULL_NAME+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: employees+ column: full_name+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: SALARY+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: employees+ column: salary+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: employees+ column: ""+ predicatecolumns: []+- description: Three table join chain+ statement: SELECT u.name, o.amount, p.iban FROM users u JOIN orders o ON u.id = o.user_id JOIN payments p ON o.order_id = p.order_id;+ defaultDatabase: spannerdb+ metadata: |-+ {+ "name": "spannerdb",+ "schemas": [+ {+ "name": "",+ "tables": [+ {"name": "users", "columns": [{"name": "id"}, {"name": "name"}, {"name": "email"}, {"name": "ssn"}]},+ {"name": "orders", "columns": [{"name": "order_id"}, {"name": "user_id"}, {"name": "amount"}, {"name": "card_number"}]},+ {"name": "payments", "columns": [{"name": "payment_id"}, {"name": "order_id"}, {"name": "paid_amount"}, {"name": "iban"}]}+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: NAME+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: name+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: AMOUNT+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: amount+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: IBAN+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: payments+ column: iban+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: ""+ - server: ""+ database: spannerdb+ schema: ""+ table: payments+ column: ""+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: ""+ predicatecolumns: []+- description: Select star over join+ statement: SELECT * FROM users u JOIN orders o ON u.id = o.user_id;+ defaultDatabase: spannerdb+ metadata: |-+ {+ "name": "spannerdb",+ "schemas": [+ {+ "name": "",+ "tables": [+ {"name": "users", "columns": [{"name": "id"}, {"name": "name"}, {"name": "email"}, {"name": "ssn"}]},+ {"name": "orders", "columns": [{"name": "order_id"}, {"name": "user_id"}, {"name": "amount"}, {"name": "card_number"}]}+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: id+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: id+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: name+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: name+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: email+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: email+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: ssn+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: ssn+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: order_id+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: order_id+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: false+ - name: user_id+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: user_id+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: false+ - name: amount+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: amount+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: false+ - name: card_number+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: card_number+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: ""+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: ""+ predicatecolumns: []+- description: Left join with sensitive column on right side+ statement: SELECT u.name, o.card_number FROM users u LEFT JOIN orders o ON u.id = o.user_id;+ defaultDatabase: spannerdb+ metadata: |-+ {+ "name": "spannerdb",+ "schemas": [+ {+ "name": "",+ "tables": [+ {"name": "users", "columns": [{"name": "id"}, {"name": "name"}, {"name": "email"}, {"name": "ssn"}]},+ {"name": "orders", "columns": [{"name": "order_id"}, {"name": "user_id"}, {"name": "amount"}, {"name": "card_number"}]}+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: NAME+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: name+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: CARD_NUMBER+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: card_number+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: ""+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: ""+ predicatecolumns: []+- description: Comma cross join+ statement: SELECT u.name, l.device FROM users u, logins l;+ defaultDatabase: spannerdb+ metadata: |-+ {+ "name": "spannerdb",+ "schemas": [+ {+ "name": "",+ "tables": [+ {"name": "users", "columns": [{"name": "id"}, {"name": "name"}, {"name": "email"}, {"name": "ssn"}]},+ {"name": "logins", "columns": [{"name": "id"}, {"name": "device"}, {"name": "ip_address"}]}+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: NAME+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: name+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: DEVICE+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: logins+ column: device+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: logins+ column: ""+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: ""+ predicatecolumns: []+- description: Subset projecting cte then select star over it+ statement: WITH safe AS (SELECT id, name FROM users) SELECT * FROM safe;+ defaultDatabase: spannerdb+ metadata: |-+ {+ "name": "spannerdb",+ "schemas": [+ {+ "name": "",+ "tables": [+ {"name": "users", "columns": [{"name": "id"}, {"name": "name"}, {"name": "email"}, {"name": "ssn"}]}+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: ID+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: id+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: NAME+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: name+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: safe+ column: ""+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: ""+ predicatecolumns: []+- description: Recursive cte with join back to cte+ statement: |-+ WITH RECURSIVE ladder AS (+ SELECT emp_id, manager_id, full_name FROM employees+ UNION ALL+ SELECT e.emp_id, e.manager_id, e.full_name FROM employees e JOIN ladder l ON e.manager_id = l.emp_id+ )+ SELECT full_name FROM ladder;+ defaultDatabase: spannerdb+ metadata: |-+ {+ "name": "spannerdb",+ "schemas": [+ {+ "name": "",+ "tables": [+ {"name": "employees", "columns": [{"name": "emp_id"}, {"name": "manager_id"}, {"name": "full_name"}, {"name": "salary"}]}+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: FULL_NAME+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: employees+ column: full_name+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: employees+ column: ""+ - server: ""+ database: spannerdb+ schema: ""+ table: ladder+ column: ""+ predicatecolumns: []+- description: Nested cte inside cte body+ statement: WITH outer_cte AS (WITH inner_cte AS (SELECT id, email FROM users) SELECT id FROM inner_cte) SELECT * FROM outer_cte;+ defaultDatabase: spannerdb+ metadata: |-+ {+ "name": "spannerdb",+ "schemas": [+ {+ "name": "",+ "tables": [+ {"name": "users", "columns": [{"name": "id"}, {"name": "name"}, {"name": "email"}, {"name": "ssn"}]}+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: ID+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: id+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: inner_cte+ column: ""+ - server: ""+ database: spannerdb+ schema: ""+ table: outer_cte+ column: ""+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: ""+ predicatecolumns: []+- description: Cte referenced twice with different aliases+ statement: WITH c AS (SELECT id, name, ssn FROM users) SELECT a.id, b.name FROM c a JOIN c b ON a.id = b.id;+ defaultDatabase: spannerdb+ metadata: |-+ {+ "name": "spannerdb",+ "schemas": [+ {+ "name": "",+ "tables": [+ {"name": "users", "columns": [{"name": "id"}, {"name": "name"}, {"name": "email"}, {"name": "ssn"}]}+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: ID+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: id+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: NAME+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: name+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: c+ column: ""+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: ""+ predicatecolumns: []+- description: Star over join of cte and base table+ statement: WITH safe AS (SELECT id, name FROM users) SELECT * FROM safe s JOIN orders o ON s.id = o.user_id;+ defaultDatabase: spannerdb+ metadata: |-+ {+ "name": "spannerdb",+ "schemas": [+ {+ "name": "",+ "tables": [+ {"name": "users", "columns": [{"name": "id"}, {"name": "name"}, {"name": "email"}, {"name": "ssn"}]},+ {"name": "orders", "columns": [{"name": "order_id"}, {"name": "user_id"}, {"name": "amount"}, {"name": "card_number"}]}+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: ID+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: id+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: NAME+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: name+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: order_id+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: order_id+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: false+ - name: user_id+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: user_id+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: false+ - name: amount+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: amount+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: false+ - name: card_number+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: card_number+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: ""+ - server: ""+ database: spannerdb+ schema: ""+ table: safe+ column: ""+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: ""+ predicatecolumns: []+- description: Derived table select star with subset projection+ statement: SELECT * FROM (SELECT id, name FROM users) d;+ defaultDatabase: spannerdb+ metadata: |-+ {+ "name": "spannerdb",+ "schemas": [+ {+ "name": "",+ "tables": [+ {"name": "users", "columns": [{"name": "id"}, {"name": "name"}, {"name": "email"}, {"name": "ssn"}]}+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: ID+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: id+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: NAME+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: name+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: ""+ predicatecolumns: []+- description: Derived table renamed projection referenced by alias+ statement: SELECT d.alias_col FROM (SELECT email AS alias_col FROM users) d;+ defaultDatabase: spannerdb+ metadata: |-+ {+ "name": "spannerdb",+ "schemas": [+ {+ "name": "",+ "tables": [+ {"name": "users", "columns": [{"name": "id"}, {"name": "name"}, {"name": "email"}, {"name": "ssn"}]}+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: ALIAS_COL+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: email+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: ""+ predicatecolumns: []+- description: Derived table computed projection referenced by alias+ statement: SELECT d.total FROM (SELECT amount + 1 AS total FROM orders) d;+ defaultDatabase: spannerdb+ metadata: |-+ {+ "name": "spannerdb",+ "schemas": [+ {+ "name": "",+ "tables": [+ {"name": "orders", "columns": [{"name": "order_id"}, {"name": "user_id"}, {"name": "amount"}, {"name": "card_number"}]}+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: TOTAL+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: amount+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: ""+ predicatecolumns: []+- description: Derived table dot star+ statement: SELECT d.* FROM (SELECT id, ssn FROM users) d;+ defaultDatabase: spannerdb+ metadata: |-+ {+ "name": "spannerdb",+ "schemas": [+ {+ "name": "",+ "tables": [+ {"name": "users", "columns": [{"name": "id"}, {"name": "name"}, {"name": "email"}, {"name": "ssn"}]}+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: ID+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: id+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: SSN+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: ssn+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: ""+ predicatecolumns: []+- description: Unaliased scalar subquery in select list+ statement: SELECT (SELECT email FROM users), id FROM users;+ defaultDatabase: spannerdb+ metadata: |-+ {+ "name": "spannerdb",+ "schemas": [+ {+ "name": "",+ "tables": [+ {"name": "users", "columns": [{"name": "id"}, {"name": "name"}, {"name": "email"}, {"name": "ssn"}]}+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: ""+ sourcecolumns: []+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: ID+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: id+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: ""+ predicatecolumns: []+- description: Aliased correlated scalar subquery projecting sensitive column+ statement: SELECT o.order_id, (SELECT u.ssn FROM users u WHERE u.id = o.user_id) AS owner_ssn FROM orders o;+ defaultDatabase: spannerdb+ metadata: |-+ {+ "name": "spannerdb",+ "schemas": [+ {+ "name": "",+ "tables": [+ {"name": "users", "columns": [{"name": "id"}, {"name": "name"}, {"name": "email"}, {"name": "ssn"}]},+ {"name": "orders", "columns": [{"name": "order_id"}, {"name": "user_id"}, {"name": "amount"}, {"name": "card_number"}]}+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: ORDER_ID+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: order_id+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: OWNER_SSN+ sourcecolumns: []+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: ""+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: ""+ predicatecolumns: []+- description: Struct field path with table alias prefix+ statement: SELECT p.address.city FROM profiles p;+ defaultDatabase: spannerdb+ metadata: |-+ {+ "name": "spannerdb",+ "schemas": [+ {+ "name": "",+ "tables": [+ {"name": "profiles", "columns": [{"name": "id"}, {"name": "address"}, {"name": "phone"}]}+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: ADDRESS+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: profiles+ column: address+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: profiles+ column: ""+ predicatecolumns: []+- description: Struct field path without table prefix+ statement: SELECT address.city FROM profiles;+ defaultDatabase: spannerdb+ metadata: |-+ {+ "name": "spannerdb",+ "schemas": [+ {+ "name": "",+ "tables": [+ {"name": "profiles", "columns": [{"name": "id"}, {"name": "address"}, {"name": "phone"}]}+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: ADDRESS+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: profiles+ column: address+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: profiles+ column: ""+ predicatecolumns: []+- description: Table name qualified star over join+ statement: SELECT users.* FROM users JOIN orders ON users.id = orders.user_id;+ defaultDatabase: spannerdb+ metadata: |-+ {+ "name": "spannerdb",+ "schemas": [+ {+ "name": "",+ "tables": [+ {"name": "users", "columns": [{"name": "id"}, {"name": "name"}, {"name": "email"}, {"name": "ssn"}]},+ {"name": "orders", "columns": [{"name": "order_id"}, {"name": "user_id"}, {"name": "amount"}, {"name": "card_number"}]}+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: id+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: id+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: false+ - name: name+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: name+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: false+ - name: email+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: email+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: false+ - name: ssn+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: ssn+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: ""+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: ""+ predicatecolumns: []+- description: Named schema table joined with default schema table of same name+ statement: SELECT a.region, b.email FROM analytics.users a JOIN users b ON a.id = b.id;+ defaultDatabase: spannerdb+ metadata: |-+ {+ "name": "spannerdb",+ "schemas": [+ {+ "name": "",+ "tables": [+ {"name": "users", "columns": [{"name": "id"}, {"name": "name"}, {"name": "email"}, {"name": "ssn"}]}+ ]+ },+ {+ "name": "analytics",+ "tables": [+ {"name": "users", "columns": [{"name": "id"}, {"name": "region"}, {"name": "score"}]}+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: REGION+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: analytics+ table: users+ column: region+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: EMAIL+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: email+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: analytics+ table: users+ column: ""+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: ""+ predicatecolumns: []+- description: Select star from named schema table+ statement: SELECT * FROM analytics.events;+ defaultDatabase: spannerdb+ metadata: |-+ {+ "name": "spannerdb",+ "schemas": [+ {+ "name": "",+ "tables": [+ {"name": "users", "columns": [{"name": "id"}, {"name": "name"}, {"name": "email"}, {"name": "ssn"}]}+ ]+ },+ {+ "name": "analytics",+ "tables": [+ {"name": "events", "columns": [{"name": "event_id"}, {"name": "user_id"}, {"name": "payload"}]}+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: event_id+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: analytics+ table: events+ column: event_id+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: false+ - name: user_id+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: analytics+ table: events+ column: user_id+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: false+ - name: payload+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: analytics+ table: events+ column: payload+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: analytics+ table: events+ column: ""+ predicatecolumns: []+- description: Three part database schema table name+ statement: SELECT event_id FROM spannerdb.analytics.events;+ defaultDatabase: spannerdb+ metadata: |-+ {+ "name": "spannerdb",+ "schemas": [+ {+ "name": "",+ "tables": [+ {"name": "users", "columns": [{"name": "id"}, {"name": "name"}, {"name": "email"}, {"name": "ssn"}]}+ ]+ },+ {+ "name": "analytics",+ "tables": [+ {"name": "events", "columns": [{"name": "event_id"}, {"name": "user_id"}, {"name": "payload"}]}+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: EVENT_ID+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: analytics+ table: events+ column: event_id+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: analytics+ table: events+ column: ""+ predicatecolumns: []+- description: Table name qualified column over named schema table+ statement: SELECT events.payload FROM analytics.events;+ defaultDatabase: spannerdb+ metadata: |-+ {+ "name": "spannerdb",+ "schemas": [+ {+ "name": "",+ "tables": [+ {"name": "users", "columns": [{"name": "id"}, {"name": "name"}, {"name": "email"}, {"name": "ssn"}]}+ ]+ },+ {+ "name": "analytics",+ "tables": [+ {"name": "events", "columns": [{"name": "event_id"}, {"name": "user_id"}, {"name": "payload"}]}+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: PAYLOAD+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: analytics+ table: events+ column: payload+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: analytics+ table: events+ column: ""+ predicatecolumns: []+- description: Information schema select is system only+ statement: SELECT table_name FROM INFORMATION_SCHEMA.TABLES;+ defaultDatabase: spannerdb+ metadata: |-+ {+ "name": "spannerdb"+ }+ querySpan:+ type: 3+ results: []+ sourcecolumns: []+ predicatecolumns: []+- description: Lowercase information schema select is system only+ statement: SELECT t.table_name FROM information_schema.tables t;+ defaultDatabase: spannerdb+ metadata: |-+ {+ "name": "spannerdb"+ }+ querySpan:+ type: 3+ results: []+ sourcecolumns: []+ predicatecolumns: []+- description: Spanner sys select is system only+ statement: SELECT * FROM SPANNER_SYS.QUERY_STATS_TOP_MINUTE;+ defaultDatabase: spannerdb+ metadata: |-+ {+ "name": "spannerdb"+ }+ querySpan:+ type: 3+ results: []+ sourcecolumns: []+ predicatecolumns: []+- description: Star except over join with sensitive columns excluded+ statement: SELECT * EXCEPT (ssn, card_number) FROM users u JOIN orders o ON u.id = o.user_id;+ defaultDatabase: spannerdb+ metadata: |-+ {+ "name": "spannerdb",+ "schemas": [+ {+ "name": "",+ "tables": [+ {"name": "users", "columns": [{"name": "id"}, {"name": "name"}, {"name": "email"}, {"name": "ssn"}]},+ {"name": "orders", "columns": [{"name": "order_id"}, {"name": "user_id"}, {"name": "amount"}, {"name": "card_number"}]}+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: id+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: id+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: name+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: name+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: email+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: email+ isplainfield: false+ sourcefieldpaths: []+ selectasterisk: false+ - name: order_id+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: order_id+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: false+ - name: user_id+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: user_id+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: false+ - name: amount+ sourcecolumns:+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: amount+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: falsesourcecolumns:- - server: ""- database: maskingdb- schema: ""- table: Customers- column: ""+ - server: ""+ database: spannerdb+ schema: ""+ table: orders+ column: ""+ - server: ""+ database: spannerdb+ schema: ""+ table: users+ column: ""predicatecolumns: []diff --git a/go.mod b/go.modindex 7f19f82134..d5d1c26377 100644--- a/go.mod+++ b/go.mod@@ -34,7 +34,7 @@ require (github.com/bmatcuk/doublestar/v4 v4.10.0github.com/bytebase/gomongo v0.0.0-20260510192859-8526a440b352github.com/bytebase/lsp-protocol v0.0.0-20250324071136-1586d0c10ff0- github.com/bytebase/omni v0.0.0-20260610065225-2cf04e304cb1+ github.com/bytebase/omni v0.0.0-20260610065946-2e66c343ef99github.com/caarlos0/env/v11 v11.4.1github.com/cenkalti/backoff/v5 v5.0.3github.com/cockroachdb/cockroachdb-parser v0.25.2diff --git a/go.sum b/go.sumindex 1cea68d76e..82c211a613 100644--- a/go.sum+++ b/go.sum@@ -250,8 +250,8 @@ github.com/bytebase/gomongo v0.0.0-20260510192859-8526a440b352 h1:3sC5pdvJ7bNAhRgithub.com/bytebase/gomongo v0.0.0-20260510192859-8526a440b352/go.mod h1:eyWrxE51HCbjGIChvty/mYaNcAroXshnIiR6SIqlxfY=github.com/bytebase/lsp-protocol v0.0.0-20250324071136-1586d0c10ff0 h1:bJ8NSf2K+pccl5m/wuu97fiYjDwpJH6IzURAq/3XA2U=github.com/bytebase/lsp-protocol v0.0.0-20250324071136-1586d0c10ff0/go.mod h1:BEWMfiZtqshdmulKJcIsWb45MUaEvDO4CROcgi8dfFg=-github.com/bytebase/omni v0.0.0-20260610065225-2cf04e304cb1 h1:CsXvtrbHIogzZ7C5iXDnGxbGx+ZiMiooft5KiTs23V0=-github.com/bytebase/omni v0.0.0-20260610065225-2cf04e304cb1/go.mod h1:EVG8nQbNPUnUBGKn6J0niSGOs3jgfKo3TciY22R4bfo=+github.com/bytebase/omni v0.0.0-20260610065946-2e66c343ef99 h1:cdoFNOOGTdgpUqtdN72Uls0p1RRKujmjC9MvJli1J8E=+github.com/bytebase/omni v0.0.0-20260610065946-2e66c343ef99/go.mod h1:EVG8nQbNPUnUBGKn6J0niSGOs3jgfKo3TciY22R4bfo=github.com/bytebase/parser v0.0.0-20260417075056-57b6ef7a2640 h1:g4Jed1/Jv/DHBiQmEgN5ILOQDDBjFlG4N4Lp+3B4aYE=github.com/bytebase/parser v0.0.0-20260417075056-57b6ef7a2640/go.mod h1:jeak/EfutSOAuWKvrFIT2IZunhWprM7oTFBRgZ9RCxo=github.com/bytebase/pgx/v5 v5.0.0-20250212161523-96ff8aed8767 h1:YuR5G3LcxpO5ScSTA+Kdirxg9RW4TRc/v1t7N2Hfmbw=