Skip to content

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

feat(snowflake): switch plugin/parser, advisors, and db driver to the omni parser (#20567)

h3n4l83a79721aa2c
diff --git a/.gitignore b/.gitignore
index d077ad7c54..bfae23e67a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -49,3 +49,4 @@ scripts/azure-marketplace/*.backup
# superpowers brainstorm sessions (visual companion mockups)
.superpowers/
+.gocache/
diff --git a/backend/plugin/advisor/snowflake/advisor.go b/backend/plugin/advisor/snowflake/advisor.go
index ca4bc8c797..dc10dfaa45 100644
--- a/backend/plugin/advisor/snowflake/advisor.go
+++ b/backend/plugin/advisor/snowflake/advisor.go
@@ -1,11 +1,2 @@
// Package snowflake is the advisor for snowflake database.
package snowflake
-
-// currentConstraintAction is the action of current constraint.
-type currentConstraintAction int
-
-const (
- currentConstraintActionNone currentConstraintAction = iota
- currentConstraintActionAdd
- currentConstraintActionDrop
-)
diff --git a/backend/plugin/advisor/snowflake/base_rule.go b/backend/plugin/advisor/snowflake/base_rule.go
new file mode 100644
index 0000000000..343cc0a4e5
--- /dev/null
+++ b/backend/plugin/advisor/snowflake/base_rule.go
@@ -0,0 +1,31 @@
+package snowflake
+
+import (
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+)
+
+// BaseRule provides the common advice-accumulation plumbing for the omni-AST
+// review rules. Rules embed it to get SetBaseLine/AddAdvice/GetAdviceList.
+// (It is the ANTLR-free survivor of the legacy GenericChecker walker, which
+// was deleted with the ANTLR parser cutover.)
+type BaseRule struct {
+ level storepb.Advice_Status
+ title string
+ adviceList []*storepb.Advice
+ baseLine int
+}
+
+// SetBaseLine sets the base line for the rule.
+func (r *BaseRule) SetBaseLine(baseLine int) {
+ r.baseLine = baseLine
+}
+
+// GetAdviceList returns the accumulated advice.
+func (r *BaseRule) GetAdviceList() []*storepb.Advice {
+ return r.adviceList
+}
+
+// AddAdvice adds a new advice to the list.
+func (r *BaseRule) AddAdvice(advice *storepb.Advice) {
+ r.adviceList = append(r.adviceList, advice)
+}
diff --git a/backend/plugin/advisor/snowflake/generic_checker.go b/backend/plugin/advisor/snowflake/generic_checker.go
deleted file mode 100644
index 8715331a37..0000000000
--- a/backend/plugin/advisor/snowflake/generic_checker.go
+++ /dev/null
@@ -1,201 +0,0 @@
-package snowflake
-
-import (
- "fmt"
- "reflect"
- "strings"
-
- "github.com/antlr4-go/antlr/v4"
- parser "github.com/bytebase/parser/snowflake"
-
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
-)
-
-// Node type constants for consistent node type checking
-const (
- NodeTypeCreateTable = "Create_table"
- NodeTypeAlterTable = "Alter_table"
- NodeTypeDropTable = "Drop_table"
- NodeTypeCreateView = "Create_view"
- NodeTypeDropView = "Drop_view"
- NodeTypeCreateDatabase = "Create_database"
- NodeTypeDropDatabase = "Drop_database"
- NodeTypeCreateSchema = "Create_schema"
- NodeTypeDropSchema = "Drop_schema"
- NodeTypeInsertStatement = "Insert_statement"
- NodeTypeUpdateStatement = "Update_statement"
- NodeTypeDeleteStatement = "Delete_statement"
- NodeTypeSelectStatement = "Select_statement"
- NodeTypeMergeStatement = "Merge_statement"
- NodeTypeCreateFunction = "Create_function"
- NodeTypeDropFunction = "Drop_function"
- NodeTypeCreateProcedure = "Create_procedure"
- NodeTypeDropProcedure = "Drop_procedure"
- NodeTypeCreateSequence = "Create_sequence"
- NodeTypeDropSequence = "Drop_sequence"
- NodeTypeCreateStage = "Create_stage"
- NodeTypeDropStage = "Drop_stage"
- NodeTypeCreateStream = "Create_stream"
- NodeTypeDropStream = "Drop_stream"
- NodeTypeCreateTask = "Create_task"
- NodeTypeDropTask = "Drop_task"
- NodeTypeUseStatement = "Use_statement"
- NodeTypeColumnDeclItemList = "Column_decl_item_list"
- NodeTypeTableColumnAction = "Table_column_action"
- NodeTypeQueryStatement = "Query_statement"
- NodeTypeDmlCommand = "Dml_command"
- NodeTypeDdlCommand = "Ddl_command"
- NodeTypeOtherCommand = "Other_command"
- NodeTypeColumnDecl = "Col_decl"
- NodeTypeColumnList = "Column_list"
- NodeTypeTableName = "Table_name"
- NodeTypeObjectName = "Object_name"
- NodeTypeSelectListElem = "Select_list_elem"
- NodeTypeSelectClause = "Select_clause"
- NodeTypeFromClause = "From_clause"
- NodeTypeWhereClause = "Where_clause"
- NodeTypeJoinClause = "Join_clause"
- NodeTypeSetOperators = "Set_operators"
- NodeTypeID = "Id_"
- NodeTypeFullColDecl = "Full_col_decl"
- NodeTypeColumnName = "Column_name"
- NodeTypeDataType = "Data_type"
- NodeTypeVarcharType = "Varchar"
- NodeTypeStringType = "String"
- NodeTypeNumberType = "Number"
- NodeTypeFloatType = "Float"
- NodeTypeIntType = "Int"
- NodeTypeSmallintType = "Smallint"
- NodeTypeTinyintType = "Tinyint"
- NodeTypeBigintType = "Bigint"
- NodeTypeByteintType = "Byteint"
- NodeTypeIdentifier = "Id_"
- NodeTypeTableConstraints = "Table_constraints"
- NodeTypeOutOfLineConstraint = "Out_of_line_constraint"
- NodeTypeInlineConstraint = "Inline_constraint"
- NodeTypePrimaryKeyClause = "Primary_key_clause"
- NodeTypeForeignKeyClause = "Foreign_key_clause"
- NodeTypeSearchCondition = "Search_condition"
- NodeTypeExpr = "Expr"
- NodeTypePredicate = "Predicate"
- NodeTypePredicatePartLike = "Predicate_part_like"
- NodeTypeExprListInParentheses = "Expr_list_in_parentheses"
- NodeTypeExecuteImmediate = "Execute_immediate"
- NodeTypeGrantPrivileges = "Grant_privileges"
- NodeTypeRevokePrivileges = "Revoke_privileges"
- NodeTypeCreateExternalTable = "Create_external_table"
- NodeTypeDropExternalTable = "Drop_external_table"
- NodeTypeTruncateTable = "Truncate_table"
- NodeTypeSQLCommand = "Sql_command"
-)
-
-// Rule defines the interface for individual SQL validation rules.
-// Each rule implements specific checking logic without embedding the base listener.
-type Rule interface {
- // OnEnter is called when entering a parse tree node
- OnEnter(ctx antlr.ParserRuleContext, nodeType string) error
-
- // OnExit is called when exiting a parse tree node
- OnExit(ctx antlr.ParserRuleContext, nodeType string) error
-
- // Name returns the rule name for logging/debugging
- Name() string
-
- // GetAdviceList returns the accumulated advice from this rule
- GetAdviceList() []*storepb.Advice
-}
-
-// GenericChecker embeds the base Snowflake parser listener and dispatches events to registered rules.
-// This design ensures only one copy of the listener type metadata in the binary.
-type GenericChecker struct {
- *parser.BaseSnowflakeParserListener
-
- rules []Rule
- baseLine int
-}
-
-// NewGenericChecker creates a new instance of GenericChecker with the given rules.
-func NewGenericChecker(rules []Rule) *GenericChecker {
- return &GenericChecker{
- rules: rules,
- }
-}
-
-// SetBaseLine sets the base line number for error reporting.
-func (g *GenericChecker) SetBaseLine(baseLine int) {
- g.baseLine = baseLine
-}
-
-// GetBaseLine returns the current base line number.
-func (g *GenericChecker) GetBaseLine() int {
- return g.baseLine
-}
-
-// EnterEveryRule is called when any rule is entered.
-// It dispatches the event to all registered rules.
-func (g *GenericChecker) EnterEveryRule(ctx antlr.ParserRuleContext) {
- nodeType := g.getNodeType(ctx)
- for _, rule := range g.rules {
- if err := rule.OnEnter(ctx, nodeType); err != nil {
- // Log error if needed
- fmt.Printf("Rule %s error on enter %s: %v\n", rule.Name(), nodeType, err)
- }
- }
-}
-
-// ExitEveryRule is called when any rule is exited.
-// It dispatches the event to all registered rules.
-func (g *GenericChecker) ExitEveryRule(ctx antlr.ParserRuleContext) {
- nodeType := g.getNodeType(ctx)
- for _, rule := range g.rules {
- if err := rule.OnExit(ctx, nodeType); err != nil {
- // Log error if needed
- fmt.Printf("Rule %s error on exit %s: %v\n", rule.Name(), nodeType, err)
- }
- }
-}
-
-// GetAdviceList collects and returns all advice from registered rules.
-func (g *GenericChecker) GetAdviceList() []*storepb.Advice {
- var allAdvice []*storepb.Advice
- for _, rule := range g.rules {
- allAdvice = append(allAdvice, rule.GetAdviceList()...)
- }
- return allAdvice
-}
-
-// getNodeType returns the type name of the parse tree node.
-func (*GenericChecker) getNodeType(ctx antlr.ParserRuleContext) string {
- t := reflect.TypeOf(ctx)
- if t.Kind() == reflect.Pointer {
- t = t.Elem()
- }
- name := t.Name()
- // Remove "Context" suffix if present
- name = strings.TrimSuffix(name, "Context")
- return name
-}
-
-// BaseRule provides common functionality for rules.
-// Other rules can embed this struct to get common behavior.
-type BaseRule struct {
- level storepb.Advice_Status
- title string
- adviceList []*storepb.Advice
- baseLine int
-}
-
-// SetBaseLine sets the base line for the rule.
-func (r *BaseRule) SetBaseLine(baseLine int) {
- r.baseLine = baseLine
-}
-
-// GetAdviceList returns the accumulated advice.
-func (r *BaseRule) GetAdviceList() []*storepb.Advice {
- return r.adviceList
-}
-
-// AddAdvice adds a new advice to the list.
-func (r *BaseRule) AddAdvice(advice *storepb.Advice) {
- r.adviceList = append(r.adviceList, advice)
-}
diff --git a/backend/plugin/advisor/snowflake/rule_column_maximum_varchar_length.go b/backend/plugin/advisor/snowflake/rule_column_maximum_varchar_length.go
index fd1519c315..d155f45f3f 100644
--- a/backend/plugin/advisor/snowflake/rule_column_maximum_varchar_length.go
+++ b/backend/plugin/advisor/snowflake/rule_column_maximum_varchar_length.go
@@ -3,19 +3,18 @@ package snowflake
import (
"context"
"fmt"
- "strconv"
+ "strings"
"github.com/pkg/errors"
"github.com/bytebase/bytebase/backend/plugin/advisor/code"
- "github.com/antlr4-go/antlr/v4"
- parser "github.com/bytebase/parser/snowflake"
+ omniast "github.com/bytebase/omni/snowflake/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
+ snowsqlparser "github.com/bytebase/bytebase/backend/plugin/parser/snowflake"
)
const (
@@ -54,29 +53,28 @@ func (*ColumnMaximumVarcharLengthAdvisor) Check(_ context.Context, checkCtx advi
// Create the rule
rule := NewColumnMaximumVarcharLengthRule(level, checkCtx.Rule.Type.String(), int(numberPayload.Number))
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
for _, stmt := range checkCtx.ParsedStatements {
if stmt.AST == nil {
continue
}
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
+ node, ok := snowsqlparser.GetOmniNode(stmt.AST)
if !ok {
continue
}
rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule.checkStatement(node, stmt.Text)
}
- return checker.GetAdviceList(), nil
+ return rule.GetAdviceList(), nil
}
// ColumnMaximumVarcharLengthRule checks for maximum varchar length.
type ColumnMaximumVarcharLengthRule struct {
BaseRule
maximum int
+ // stmtText is the SQL text of the statement currently being checked; node
+ // Loc offsets are relative to it.
+ stmtText string
}
// NewColumnMaximumVarcharLengthRule creates a new ColumnMaximumVarcharLengthRule.
@@ -95,32 +93,29 @@ func (*ColumnMaximumVarcharLengthRule) Name() string {
return "ColumnMaximumVarcharLengthRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *ColumnMaximumVarcharLengthRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- if nodeType == NodeTypeDataType {
- r.checkDataType(ctx.(*parser.Data_typeContext))
- }
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (*ColumnMaximumVarcharLengthRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- // This rule doesn't need exit processing
- return nil
+// checkStatement walks one statement's omni AST and checks every data type.
+// This mirrors the legacy ANTLR listener that fired on every Data_type context
+// anywhere in the statement.
+func (r *ColumnMaximumVarcharLengthRule) checkStatement(node omniast.Node, stmtText string) {
+ r.stmtText = stmtText
+ omniast.Inspect(node, func(n omniast.Node) bool {
+ if typeName, ok := n.(*omniast.TypeName); ok {
+ r.checkTypeName(typeName)
+ }
+ return true
+ })
}
-func (r *ColumnMaximumVarcharLengthRule) checkDataType(ctx *parser.Data_typeContext) {
- if ctx.VARCHAR() == nil {
+func (r *ColumnMaximumVarcharLengthRule) checkTypeName(typeName *omniast.TypeName) {
+ // The legacy rule only fired on the literal VARCHAR keyword: aliases like
+ // STRING, TEXT, NVARCHAR (all TypeVarchar in omni) did not trigger it.
+ if !strings.EqualFold(typeName.Name, "VARCHAR") {
return
}
length := varcharDefaultLength
- if v := ctx.Data_type_size(); v != nil && v.Num() != nil {
- var err error
- length, err = strconv.Atoi(v.Num().GetText())
- if err != nil {
- return
- }
+ if len(typeName.Params) > 0 {
+ length = typeName.Params[0]
}
if length > r.maximum {
@@ -129,7 +124,19 @@ func (r *ColumnMaximumVarcharLengthRule) checkDataType(ctx *parser.Data_typeCont
Code: code.VarcharLengthExceedsLimit.Int32(),
Title: r.title,
Content: fmt.Sprintf("The maximum varchar length is %d.", r.maximum),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + r.line(typeName.Loc.Start)),
})
}
}
+
+// line converts a byte offset within the current statement text to a 1-based
+// line number, mirroring the ANTLR token line the legacy advisor reported.
+func (r *ColumnMaximumVarcharLengthRule) line(offset int) int {
+ if offset < 0 {
+ return 1
+ }
+ if offset > len(r.stmtText) {
+ offset = len(r.stmtText)
+ }
+ return 1 + strings.Count(r.stmtText[:offset], "\n")
+}
diff --git a/backend/plugin/advisor/snowflake/rule_column_no_null.go b/backend/plugin/advisor/snowflake/rule_column_no_null.go
index 06dcd7ff3d..d28455d28a 100644
--- a/backend/plugin/advisor/snowflake/rule_column_no_null.go
+++ b/backend/plugin/advisor/snowflake/rule_column_no_null.go
@@ -3,15 +3,14 @@ package snowflake
import (
"context"
"fmt"
+ "strings"
- "github.com/antlr4-go/antlr/v4"
- parser "github.com/bytebase/parser/snowflake"
+ omniast "github.com/bytebase/omni/snowflake/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"github.com/bytebase/bytebase/backend/plugin/advisor/code"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
snowsqlparser "github.com/bytebase/bytebase/backend/plugin/parser/snowflake"
)
@@ -37,33 +36,27 @@ func (*ColumnNoNullAdvisor) Check(_ context.Context, checkCtx advisor.Context) (
// Create the rule
rule := NewColumnNoNullRule(level, checkCtx.Rule.Type.String())
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
for _, stmt := range checkCtx.ParsedStatements {
if stmt.AST == nil {
continue
}
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
+ node, ok := snowsqlparser.GetOmniNode(stmt.AST)
if !ok {
continue
}
rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule.checkStatement(node, stmt.Text)
}
- return checker.GetAdviceList(), nil
+ return rule.GetAdviceList(), nil
}
// ColumnNoNullRule checks for column no NULL value.
type ColumnNoNullRule struct {
BaseRule
-
- // currentOriginalTableName is the original table name of the current table.
- currentOriginalTableName string
- // columnNullable is a map from normalized column name to the line number causing the column to be nullable.
- columnNullable map[string]int
+ // stmtText is the SQL text of the statement currently being checked; node
+ // Loc offsets are relative to it.
+ stmtText string
}
// NewColumnNoNullRule creates a new ColumnNoNullRule.
@@ -73,7 +66,6 @@ func NewColumnNoNullRule(level storepb.Advice_Status, title string) *ColumnNoNul
level: level,
title: title,
},
- columnNullable: make(map[string]int),
}
}
@@ -82,188 +74,109 @@ func (*ColumnNoNullRule) Name() string {
return "ColumnNoNullRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *ColumnNoNullRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.enterCreateTable(ctx.(*parser.Create_tableContext))
- case NodeTypeFullColDecl:
- r.enterFullColDecl(ctx.(*parser.Full_col_declContext))
- case NodeTypeOutOfLineConstraint:
- r.enterOutOfLineConstraint(ctx.(*parser.Out_of_line_constraintContext))
- case NodeTypeAlterTable:
- r.enterAlterTable(ctx.(*parser.Alter_tableContext))
- case NodeTypeTableColumnAction:
- r.enterTableColumnAction(ctx.(*parser.Table_column_actionContext))
- case "Alter_table_alter_column":
- r.enterAlterTableAlterColumn(ctx.(*parser.Alter_table_alter_columnContext))
- case "Alter_column_decl":
- r.enterAlterColumnDecl(ctx.(*parser.Alter_column_declContext))
+// checkStatement checks one statement's omni AST node.
+func (r *ColumnNoNullRule) checkStatement(node omniast.Node, stmtText string) {
+ r.stmtText = stmtText
+ switch n := node.(type) {
+ case *omniast.CreateTableStmt:
+ r.checkCreateTable(n)
+ case *omniast.AlterTableStmt:
+ r.checkAlterTable(n)
default:
- // Ignore other node types
- }
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (r *ColumnNoNullRule) OnExit(_ antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.exitCreateTable()
- case NodeTypeAlterTable:
- r.exitAlterTable()
- case "Alter_table_alter_column":
- r.exitAlterTableAlterColumn()
- default:
- // Ignore other node types
- }
- return nil
-}
-
-func (r *ColumnNoNullRule) enterCreateTable(ctx *parser.Create_tableContext) {
- r.currentOriginalTableName = ctx.Object_name().GetText()
-}
-
-func (r *ColumnNoNullRule) exitCreateTable() {
- for normalizedColumnName, columnNullableLine := range r.columnNullable {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.ColumnCannotNull.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Column %s is nullable, which is not allowed.", normalizedColumnName),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + columnNullableLine),
- })
}
- r.currentOriginalTableName = ""
- r.columnNullable = make(map[string]int)
}
-func (r *ColumnNoNullRule) enterFullColDecl(ctx *parser.Full_col_declContext) {
- if r.currentOriginalTableName == "" {
+func (r *ColumnNoNullRule) checkCreateTable(stmt *omniast.CreateTableStmt) {
+ // The legacy advisor only fired on plain CREATE TABLE (the Create_table
+ // context); CREATE TABLE ... AS SELECT / LIKE / CLONE parsed as different
+ // contexts and were never checked.
+ if stmt.AsSelect != nil || stmt.Like != nil || stmt.Clone != nil {
return
}
- normalizedOriginalColumnID := normalizeSnowflakeColumnName(ctx.Col_decl().Column_name())
- r.columnNullable[normalizedOriginalColumnID] = ctx.GetStart().GetLine()
- for _, nullNotNull := range ctx.AllNull_not_null() {
- if nullNotNull.NOT() != nil {
- delete(r.columnNullable, normalizedOriginalColumnID)
- break
+
+ // columnNullable maps the normalized column name to the line number causing
+ // the column to be nullable. Mirrors the legacy add-then-delete walk so
+ // duplicate column names behave identically.
+ columnNullable := make(map[string]int)
+ for _, column := range stmt.Columns {
+ normalizedColumnName := column.Name.Normalize()
+ columnNullable[normalizedColumnName] = r.line(column.Loc.Start)
+ if column.NotNull {
+ delete(columnNullable, normalizedColumnName)
}
- }
- for _, constraint := range ctx.AllInline_constraint() {
- if constraint.Primary_key() != nil {
- delete(r.columnNullable, normalizedOriginalColumnID)
- break
+ if column.InlineConstraint != nil && column.InlineConstraint.Type == omniast.ConstrPrimaryKey {
+ delete(columnNullable, normalizedColumnName)
}
}
-}
-
-func (r *ColumnNoNullRule) enterOutOfLineConstraint(ctx *parser.Out_of_line_constraintContext) {
- if r.currentOriginalTableName == "" {
- return
- }
- if ctx.Primary_key() != nil {
- for _, columnListInParentheses := range ctx.AllColumn_list_in_parentheses() {
- for _, column := range columnListInParentheses.Column_list().AllColumn_name() {
- normalizedOriginalColumnID := normalizeSnowflakeColumnName(column)
- delete(r.columnNullable, normalizedOriginalColumnID)
- }
+ // Out-of-line PRIMARY KEY constraints make their columns non-nullable.
+ for _, constraint := range stmt.Constraints {
+ if constraint.Type != omniast.ConstrPrimaryKey {
+ continue
+ }
+ for _, column := range constraint.Columns {
+ delete(columnNullable, column.Normalize())
}
}
-}
-func (r *ColumnNoNullRule) enterAlterTable(ctx *parser.Alter_tableContext) {
- r.currentOriginalTableName = ctx.Object_name(0).GetText()
+ r.addNullableAdvice(columnNullable)
}
-func (r *ColumnNoNullRule) exitAlterTable() {
- for normalizedColumnName, columnNullableLine := range r.columnNullable {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.ColumnCannotNull.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Column %s is nullable, which is not allowed.", normalizedColumnName),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + columnNullableLine),
- })
- }
- r.currentOriginalTableName = ""
- r.columnNullable = make(map[string]int)
-}
-
-func (r *ColumnNoNullRule) enterTableColumnAction(ctx *parser.Table_column_actionContext) {
- if r.currentOriginalTableName == "" {
- return
- }
- if ctx.ADD() != nil {
- for _, fullColDecl := range ctx.AllFull_col_decl() {
- normalizedNewColumnName := normalizeSnowflakeColumnName(fullColDecl.Col_decl().Column_name())
- hasPK := false
- for _, inlineConstraint := range fullColDecl.AllInline_constraint() {
- if inlineConstraint.Primary_key() != nil {
- hasPK = true
- break
+func (r *ColumnNoNullRule) checkAlterTable(stmt *omniast.AlterTableStmt) {
+ columnNullable := make(map[string]int)
+ for _, action := range stmt.Actions {
+ switch action.Kind {
+ case omniast.AlterTableAddColumn:
+ for _, column := range action.Columns {
+ normalizedColumnName := column.Name.Normalize()
+ columnNullable[normalizedColumnName] = r.line(column.Loc.Start)
+ if column.NotNull {
+ delete(columnNullable, normalizedColumnName)
}
- }
- hasNotNull := false
- for _, nullNotNull := range fullColDecl.AllNull_not_null() {
- if nullNotNull.NOT() != nil {
- hasNotNull = true
- break
+ if column.InlineConstraint != nil && column.InlineConstraint.Type == omniast.ConstrPrimaryKey {
+ delete(columnNullable, normalizedColumnName)
}
}
-
- if !hasPK && !hasNotNull {
- r.columnNullable[normalizedNewColumnName] = fullColDecl.GetStart().GetLine()
+ case omniast.AlterTableAlterColumn:
+ for _, columnAlter := range action.ColumnAlters {
+ if columnAlter.Kind == omniast.ColumnAlterDropNotNull {
+ columnNullable[columnAlter.Column.Normalize()] = r.line(columnAlter.Column.Loc.Start)
+ }
}
- }
- return
- }
- if ctx.Alter_modify() != nil {
- for _, alterColumnClause := range ctx.AllAlter_column_clause() {
- normalizedOriginalColumnName := normalizeSnowflakeColumnName(alterColumnClause.Column_name())
- if alterColumnClause.DROP() != nil && alterColumnClause.NOT() != nil && alterColumnClause.NULL_() != nil {
- r.columnNullable[normalizedOriginalColumnName] = alterColumnClause.GetStart().GetLine()
+ case omniast.AlterTableAddConstraint:
+ // Mirrors the legacy walk where an out-of-line PRIMARY KEY removed
+ // its columns from the nullable set.
+ if action.Constraint != nil && action.Constraint.Type == omniast.ConstrPrimaryKey {
+ for _, column := range action.Constraint.Columns {
+ delete(columnNullable, column.Normalize())
+ }
}
+ default:
}
- return
}
-}
-func (r *ColumnNoNullRule) enterAlterTableAlterColumn(ctx *parser.Alter_table_alter_columnContext) {
- r.currentOriginalTableName = ctx.Object_name().GetText()
+ r.addNullableAdvice(columnNullable)
}
-func (r *ColumnNoNullRule) exitAlterTableAlterColumn() {
- for normalizedColumnName, columnNullableLine := range r.columnNullable {
+func (r *ColumnNoNullRule) addNullableAdvice(columnNullable map[string]int) {
+ for normalizedColumnName, columnNullableLine := range columnNullable {
r.AddAdvice(&storepb.Advice{
Status: r.level,
Code: code.ColumnCannotNull.Int32(),
Title: r.title,
- Content: fmt.Sprintf("After dropping NOT NULL of column %s, it will be nullable, which is not allowed.", normalizedColumnName),
+ Content: fmt.Sprintf("Column %s is nullable, which is not allowed.", normalizedColumnName),
StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + columnNullableLine),
})
}
- r.currentOriginalTableName = ""
- r.columnNullable = make(map[string]int)
-}
-
-func (r *ColumnNoNullRule) enterAlterColumnDecl(ctx *parser.Alter_column_declContext) {
- if r.currentOriginalTableName == "" {
- return
- }
- normalizedNewColumnName := normalizeSnowflakeColumnName(ctx.Column_name())
- if ctx.Alter_column_opts().DROP() != nil && ctx.Alter_column_opts().NOT() != nil && ctx.Alter_column_opts().NULL_() != nil {
- r.columnNullable[normalizedNewColumnName] = ctx.GetStart().GetLine()
- }
}
-func normalizeSnowflakeColumnName(columnName parser.IColumn_nameContext) string {
- if columnName == nil {
- return ""
+// line converts a byte offset within the current statement text to a 1-based
+// line number, mirroring the ANTLR token line the legacy advisor reported.
+func (r *ColumnNoNullRule) line(offset int) int {
+ if offset < 0 {
+ return 1
}
- allIDs := columnName.AllId_()
- if len(allIDs) == 0 {
- return ""
+ if offset > len(r.stmtText) {
+ offset = len(r.stmtText)
}
- return snowsqlparser.NormalizeSnowSQLObjectNamePart(allIDs[len(allIDs)-1])
+ return 1 + strings.Count(r.stmtText[:offset], "\n")
}
diff --git a/backend/plugin/advisor/snowflake/rule_column_require.go b/backend/plugin/advisor/snowflake/rule_column_require.go
index bb1cde4ff5..80ba628e65 100644
--- a/backend/plugin/advisor/snowflake/rule_column_require.go
+++ b/backend/plugin/advisor/snowflake/rule_column_require.go
@@ -5,18 +5,17 @@ import (
"context"
"fmt"
"slices"
+ "strings"
"github.com/pkg/errors"
"github.com/bytebase/bytebase/backend/plugin/advisor/code"
- "github.com/antlr4-go/antlr/v4"
- parser "github.com/bytebase/parser/snowflake"
+ omniast "github.com/bytebase/omni/snowflake/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
snowsqlparser "github.com/bytebase/bytebase/backend/plugin/parser/snowflake"
)
@@ -49,22 +48,20 @@ func (*ColumnRequireAdvisor) Check(_ context.Context, checkCtx advisor.Context)
}
rule := NewColumnRequireRule(level, checkCtx.Rule.Type.String(), requireColumns)
- checker := NewGenericChecker([]Rule{rule})
for _, stmt := range checkCtx.ParsedStatements {
if stmt.AST == nil {
continue
}
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
+ node, ok := snowsqlparser.GetOmniNode(stmt.AST)
if !ok {
continue
}
rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule.checkStatement(node, stmt.Text)
}
- return checker.GetAdviceList(), nil
+ return rule.GetAdviceList(), nil
}
// ColumnRequireRule checks for required columns.
@@ -72,13 +69,9 @@ type ColumnRequireRule struct {
BaseRule
// requireColumns is the required columns, the key is the normalized column name.
requireColumns map[string]any
-
- // The following variables should be clean up when ENTER some statement.
- //
- // currentMissingColumn is the missing column, the key is the normalized column name.
- currentMissingColumn map[string]any
- // currentOriginalTableName is the original table name, should be reset when QUIT some statement.
- currentOriginalTableName string
+ // stmtText is the SQL text of the statement currently being checked; node
+ // Loc offsets are relative to it.
+ stmtText string
}
// NewColumnRequireRule creates a new ColumnRequireRule.
@@ -97,137 +90,108 @@ func (*ColumnRequireRule) Name() string {
return "ColumnRequireRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *ColumnRequireRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.enterCreateTable(ctx.(*parser.Create_tableContext))
- case NodeTypeColumnDeclItemList:
- r.enterColumnDeclItemList(ctx.(*parser.Column_decl_item_listContext))
- case NodeTypeAlterTable:
- r.enterAlterTable(ctx.(*parser.Alter_tableContext))
- case NodeTypeTableColumnAction:
- r.enterTableColumnAction(ctx.(*parser.Table_column_actionContext))
+// checkStatement checks one statement's omni AST node.
+func (r *ColumnRequireRule) checkStatement(node omniast.Node, stmtText string) {
+ r.stmtText = stmtText
+ switch n := node.(type) {
+ case *omniast.CreateTableStmt:
+ r.checkCreateTable(n)
+ case *omniast.AlterTableStmt:
+ r.checkAlterTable(n)
default:
- // Ignore other node types
}
- return nil
}
-// OnExit is called when exiting a parse tree node.
-func (r *ColumnRequireRule) OnExit(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.exitCreateTable(ctx.(*parser.Create_tableContext))
- case NodeTypeAlterTable:
- r.exitAlterTable(ctx.(*parser.Alter_tableContext))
- default:
- // Ignore other node types
+func (r *ColumnRequireRule) checkCreateTable(stmt *omniast.CreateTableStmt) {
+ // The legacy advisor only fired on plain CREATE TABLE (the Create_table
+ // context); CREATE TABLE ... AS SELECT / LIKE / CLONE parsed as different
+ // contexts and were never checked.
+ if stmt.AsSelect != nil || stmt.Like != nil || stmt.Clone != nil {
+ return
}
- return nil
-}
-func (r *ColumnRequireRule) enterCreateTable(ctx *parser.Create_tableContext) {
- r.currentOriginalTableName = ctx.Object_name().GetText()
- r.currentMissingColumn = make(map[string]any)
+ missingColumns := make(map[string]any)
for column := range r.requireColumns {
- r.currentMissingColumn[column] = true
+ missingColumns[column] = true
}
-}
-
-func (r *ColumnRequireRule) enterColumnDeclItemList(ctx *parser.Column_decl_item_listContext) {
- if r.currentOriginalTableName == "" {
+ for _, column := range stmt.Columns {
+ delete(missingColumns, column.Name.Normalize())
+ }
+ if len(missingColumns) == 0 {
return
}
- allColumnDeclItems := ctx.AllColumn_decl_item()
- for _, columnDeclItem := range allColumnDeclItems {
- if fullColDecl := columnDeclItem.Full_col_decl(); fullColDecl != nil {
- normalizedColumnName := normalizeSnowflakeColumnNameForRequire(fullColDecl.Col_decl().Column_name())
- delete(r.currentMissingColumn, normalizedColumnName)
+
+ // The legacy advisor reported the stop line of the column declaration list,
+ // i.e. the line where its last item (column or out-of-line constraint) ends.
+ line := r.line(stmt.Loc.Start)
+ lastItemEnd := -1
+ for _, column := range stmt.Columns {
+ if column.Loc.End > lastItemEnd {
+ lastItemEnd = column.Loc.End
}
}
-}
-
-func (r *ColumnRequireRule) exitCreateTable(ctx *parser.Create_tableContext) {
- columnNames := make([]string, 0, len(r.currentMissingColumn))
- for column := range r.currentMissingColumn {
- columnNames = append(columnNames, column)
+ for _, constraint := range stmt.Constraints {
+ if constraint.Loc.End > lastItemEnd {
+ lastItemEnd = constraint.Loc.End
+ }
}
- if len(columnNames) == 0 {
- return
+ if lastItemEnd > 0 {
+ line = r.line(lastItemEnd - 1)
}
- slices.Sort(columnNames)
- for _, column := range columnNames {
- line := ctx.GetStart().GetLine()
- if createTableClause := ctx.Create_table_clause(); createTableClause != nil {
- if columnDeclItemListParen := createTableClause.Column_decl_item_list_paren(); columnDeclItemListParen != nil {
- if columnDeclItemList := columnDeclItemListParen.Column_decl_item_list(); columnDeclItemList != nil && columnDeclItemList.GetStop() != nil {
- line = columnDeclItemList.GetStop().GetLine()
- }
+ r.addMissingColumnAdvice(missingColumns, stmt.Name.String(), line)
+}
+
+func (r *ColumnRequireRule) checkAlterTable(stmt *omniast.AlterTableStmt) {
+ missingColumns := make(map[string]any)
+ actionLine := 0
+ for _, action := range stmt.Actions {
+ if action.Kind != omniast.AlterTableDropColumn {
+ continue
+ }
+ if actionLine == 0 {
+ actionLine = r.line(action.Loc.Start)
+ }
+ for _, column := range action.DropColumnNames {
+ normalizedColumnName := column.Normalize()
+ if _, ok := r.requireColumns[normalizedColumnName]; ok {
+ missingColumns[normalizedColumnName] = true
}
}
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.NoRequiredColumn.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Table %s missing required column %q", r.currentOriginalTableName, column),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + line),
- })
}
- r.currentOriginalTableName = ""
- r.currentMissingColumn = nil
-}
-
-func (r *ColumnRequireRule) enterAlterTable(ctx *parser.Alter_tableContext) {
- r.currentOriginalTableName = ctx.Object_name(0).GetText()
- r.currentMissingColumn = make(map[string]any)
-}
-
-func (r *ColumnRequireRule) enterTableColumnAction(ctx *parser.Table_column_actionContext) {
- if r.currentOriginalTableName == "" || ctx.DROP() == nil || ctx.Alter_modify() != nil {
+ if len(missingColumns) == 0 {
return
}
- for _, columnName := range ctx.Column_list().AllColumn_name() {
- originalColumName := columnName.GetText()
- normalizedColumnName := snowsqlparser.ExtractSnowSQLOrdinaryIdentifier(originalColumName)
- if _, ok := r.requireColumns[normalizedColumnName]; ok {
- r.currentMissingColumn[normalizedColumnName] = true
- }
- }
-}
-
-func normalizeSnowflakeColumnNameForRequire(columnName parser.IColumn_nameContext) string {
- if columnName == nil {
- return ""
- }
- allIDs := columnName.AllId_()
- if len(allIDs) == 0 {
- return ""
- }
- return snowsqlparser.NormalizeSnowSQLObjectNamePart(allIDs[len(allIDs)-1])
+ r.addMissingColumnAdvice(missingColumns, stmt.Name.String(), actionLine)
}
-func (r *ColumnRequireRule) exitAlterTable(ctx *parser.Alter_tableContext) {
- columnNames := make([]string, 0, len(r.currentMissingColumn))
- for column := range r.currentMissingColumn {
+func (r *ColumnRequireRule) addMissingColumnAdvice(missingColumns map[string]any, originalTableName string, line int) {
+ columnNames := make([]string, 0, len(missingColumns))
+ for column := range missingColumns {
columnNames = append(columnNames, column)
}
- if len(columnNames) == 0 {
- return
- }
-
slices.Sort(columnNames)
+
for _, column := range columnNames {
r.AddAdvice(&storepb.Advice{
Status: r.level,
Code: code.NoRequiredColumn.Int32(),
Title: r.title,
- Content: fmt.Sprintf("Table %s missing required column %q", r.currentOriginalTableName, column),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.Table_column_action().GetStart().GetLine()),
+ Content: fmt.Sprintf("Table %s missing required column %q", originalTableName, column),
+ StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + line),
})
}
- r.currentOriginalTableName = ""
- r.currentMissingColumn = nil
+}
+
+// line converts a byte offset within the current statement text to a 1-based
+// line number, mirroring the ANTLR token line the legacy advisor reported.
+func (r *ColumnRequireRule) line(offset int) int {
+ if offset < 0 {
+ return 1
+ }
+ if offset > len(r.stmtText) {
+ offset = len(r.stmtText)
+ }
+ return 1 + strings.Count(r.stmtText[:offset], "\n")
}
diff --git a/backend/plugin/advisor/snowflake/rule_migration_compatibility.go b/backend/plugin/advisor/snowflake/rule_migration_compatibility.go
index 2b1ad1b047..3f8f99cfa7 100644
--- a/backend/plugin/advisor/snowflake/rule_migration_compatibility.go
+++ b/backend/plugin/advisor/snowflake/rule_migration_compatibility.go
@@ -6,14 +6,12 @@ import (
"fmt"
"strings"
- "github.com/antlr4-go/antlr/v4"
- parser "github.com/bytebase/parser/snowflake"
+ omniast "github.com/bytebase/omni/snowflake/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"github.com/bytebase/bytebase/backend/plugin/advisor/code"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
snowsqlparser "github.com/bytebase/bytebase/backend/plugin/parser/snowflake"
)
@@ -36,28 +34,32 @@ func (*MigrationCompatibilityAdvisor) Check(_ context.Context, checkCtx advisor.
return nil, err
}
- rule := NewMigrationCompatibilityRule(level, checkCtx.Rule.Type.String(), checkCtx.CurrentDatabase)
- checker := NewGenericChecker([]Rule{rule})
+ checker := &migrationCompatibilityChecker{
+ level: level,
+ title: checkCtx.Rule.Type.String(),
+ currentDatabase: checkCtx.CurrentDatabase,
+ normalizedNewCreateTableNameMap: make(map[string]bool),
+ normalizedNewCreateSchemaNameMap: make(map[string]bool),
+ normalizedNewCreateDatabaseNameMap: make(map[string]bool),
+ }
for _, stmt := range checkCtx.ParsedStatements {
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
+ node, ok := snowsqlparser.GetOmniNode(stmt.AST)
if !ok {
continue
}
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ checker.checkStmt(node, stmt.Text, stmt.BaseLine())
}
- return checker.GetAdviceList(), nil
+ return checker.adviceList, nil
}
-// MigrationCompatibilityRule checks for migration compatibility issues.
-type MigrationCompatibilityRule struct {
- BaseRule
+// migrationCompatibilityChecker checks for migration compatibility issues.
+type migrationCompatibilityChecker struct {
+ adviceList []*storepb.Advice
+ level storepb.Advice_Status
+ title string
+
// normalizedNewCreateTableNameMap contain the new created table name in normalized format, e.g. "SNOWFLAKE.PUBLIC.TABLE", If there are IF NOT EXISTS, the value will be false.
normalizedNewCreateTableNameMap map[string]bool
// normalizedNewCreateSchemaNameMap contain the new created schema name in normalized format, e.g. "SNOWFLAKE.PUBLIC", If there are IF NOT EXISTS, the value will be false.
@@ -69,156 +71,143 @@ type MigrationCompatibilityRule struct {
currentDatabase string
}
-// NewMigrationCompatibilityRule creates a new MigrationCompatibilityRule.
-func NewMigrationCompatibilityRule(level storepb.Advice_Status, title string, currentDatabase string) *MigrationCompatibilityRule {
- return &MigrationCompatibilityRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- currentDatabase: currentDatabase,
- normalizedNewCreateTableNameMap: make(map[string]bool),
- normalizedNewCreateSchemaNameMap: make(map[string]bool),
- normalizedNewCreateDatabaseNameMap: make(map[string]bool),
- }
-}
-
-// Name returns the rule name.
-func (*MigrationCompatibilityRule) Name() string {
- return "MigrationCompatibilityRule"
-}
-
-// OnEnter is called when entering a parse tree node.
-func (r *MigrationCompatibilityRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.enterCreateTable(ctx.(*parser.Create_tableContext))
- case "Create_table_as_select":
- r.enterCreateTableAsSelect(ctx.(*parser.Create_table_as_selectContext))
- case NodeTypeCreateSchema:
- r.enterCreateSchema(ctx.(*parser.Create_schemaContext))
- case NodeTypeCreateDatabase:
- r.enterCreateDatabase(ctx.(*parser.Create_databaseContext))
- case NodeTypeDropTable:
- r.enterDropTable(ctx.(*parser.Drop_tableContext))
- case NodeTypeDropSchema:
- r.enterDropSchema(ctx.(*parser.Drop_schemaContext))
- case NodeTypeDropDatabase:
- r.enterDropDatabase(ctx.(*parser.Drop_databaseContext))
- case NodeTypeAlterTable:
- r.enterAlterTable(ctx.(*parser.Alter_tableContext))
+// checkStmt dispatches one top-level statement node, mirroring the legacy
+// listener's context cases: CREATE TABLE [AS SELECT] / CREATE SCHEMA /
+// CREATE DATABASE record new objects; DROP TABLE / DROP SCHEMA /
+// DROP DATABASE / ALTER TABLE ... DROP COLUMN produce compatibility advices.
+func (c *migrationCompatibilityChecker) checkStmt(node omniast.Node, text string, baseLine int) {
+ switch stmt := node.(type) {
+ case *omniast.CreateTableStmt:
+ // Covers both CREATE TABLE and CREATE TABLE ... AS SELECT (the legacy
+ // grammar had two contexts; omni folds CTAS into CreateTableStmt).
+ c.normalizedNewCreateTableNameMap[c.normalizeTableName(stmt.Name)] = !stmt.IfNotExists
+ case *omniast.CreateSchemaStmt:
+ c.normalizedNewCreateSchemaNameMap[c.normalizeSchemaName(stmt.Name)] = !stmt.IfNotExists
+ case *omniast.CreateDatabaseStmt:
+ c.normalizedNewCreateDatabaseNameMap[normalizeDatabaseName(stmt.Name)] = !stmt.IfNotExists
+ case *omniast.DropStmt:
+ if stmt.Kind != omniast.DropTable {
+ return
+ }
+ normalizedFullDropTableName := c.normalizeTableName(stmt.Name)
+ mustNewCreate, ok := c.normalizedNewCreateTableNameMap[normalizedFullDropTableName]
+ if ok && mustNewCreate {
+ return
+ }
+ level := c.level
+ if ok && !mustNewCreate {
+ level = storepb.Advice_WARNING
+ }
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: level,
+ Code: code.CompatibilityDropTable.Int32(),
+ Title: c.title,
+ Content: fmt.Sprintf("Drop table %q may cause incompatibility with the existing data and code", normalizedFullDropTableName),
+ StartPosition: common.ConvertANTLRLineToPosition(baseLine + statementLineForOffset(text, stmt.Loc.Start)),
+ })
+ case *omniast.DropSchemaStmt:
+ normalizedFullDropSchemaName := c.normalizeSchemaName(stmt.Name)
+ mustNewCreate, ok := c.normalizedNewCreateSchemaNameMap[normalizedFullDropSchemaName]
+ if ok && mustNewCreate {
+ return
+ }
+ level := c.level
+ if ok && !mustNewCreate {
+ level = storepb.Advice_WARNING
+ }
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: level,
+ Code: code.CompatibilityDropSchema.Int32(),
+ Title: c.title,
+ Content: fmt.Sprintf("Drop schema %q may cause incompatibility with the existing data and code", normalizedFullDropSchemaName),
+ StartPosition: common.ConvertANTLRLineToPosition(baseLine + statementLineForOffset(text, stmt.Loc.Start)),
+ })
+ case *omniast.DropDatabaseStmt:
+ normalizedFullDropDatabaseName := normalizeDatabaseName(stmt.Name)
+ mustNewCreate, ok := c.normalizedNewCreateDatabaseNameMap[normalizedFullDropDatabaseName]
+ if ok && mustNewCreate {
+ return
+ }
+ level := c.level
+ if ok && !mustNewCreate {
+ level = storepb.Advice_WARNING
+ }
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: level,
+ Code: code.CompatibilityDropDatabase.Int32(),
+ Title: c.title,
+ Content: fmt.Sprintf("Drop database %q may cause incompatibility with the existing data and code", normalizedFullDropDatabaseName),
+ StartPosition: common.ConvertANTLRLineToPosition(baseLine + statementLineForOffset(text, stmt.Loc.Start)),
+ })
+ case *omniast.AlterTableStmt:
+ for _, action := range stmt.Actions {
+ if action == nil || action.Kind != omniast.AlterTableDropColumn {
+ continue
+ }
+ normalizedAllColumnNames := make([]string, 0, len(action.DropColumnNames))
+ for _, columnName := range action.DropColumnNames {
+ if columnName.IsEmpty() {
+ continue
+ }
+ normalizedAllColumnNames = append(normalizedAllColumnNames, fmt.Sprintf("%q", columnName.Normalize()))
+ }
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: c.level,
+ Code: code.CompatibilityDropColumn.Int32(),
+ Title: c.title,
+ Content: fmt.Sprintf("Drop column %s may cause incompatibility with the existing data and code", strings.Join(normalizedAllColumnNames, ",")),
+ StartPosition: common.ConvertANTLRLineToPosition(baseLine + statementLineForOffset(text, stmt.Loc.Start)),
+ })
+ }
default:
- // Other node types
+ // Other statement types
}
- return nil
}
-// OnExit is called when exiting a parse tree node.
-func (*MigrationCompatibilityRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- // This rule doesn't need exit processing
- return nil
-}
-
-func (r *MigrationCompatibilityRule) enterCreateTable(ctx *parser.Create_tableContext) {
- normalizedFullTableName := snowsqlparser.NormalizeSnowSQLObjectName(ctx.Object_name(), r.currentDatabase, "PUBLIC")
- r.normalizedNewCreateTableNameMap[normalizedFullTableName] = ctx.If_not_exists() == nil
-}
-
-func (r *MigrationCompatibilityRule) enterCreateTableAsSelect(ctx *parser.Create_table_as_selectContext) {
- normalizedFullTableName := snowsqlparser.NormalizeSnowSQLObjectName(ctx.Object_name(), r.currentDatabase, "PUBLIC")
- r.normalizedNewCreateTableNameMap[normalizedFullTableName] = ctx.If_not_exists() == nil
-}
-
-func (r *MigrationCompatibilityRule) enterCreateSchema(ctx *parser.Create_schemaContext) {
- normalizedFullSchemaName := snowsqlparser.NormalizeSnowSQLSchemaName(ctx.Schema_name(), r.currentDatabase)
- r.normalizedNewCreateSchemaNameMap[normalizedFullSchemaName] = ctx.If_not_exists() == nil
-}
-
-func (r *MigrationCompatibilityRule) enterCreateDatabase(ctx *parser.Create_databaseContext) {
- normalizedFullDatabaseName := snowsqlparser.NormalizeSnowSQLObjectNamePart(ctx.Id_())
- r.normalizedNewCreateDatabaseNameMap[normalizedFullDatabaseName] = ctx.If_not_exists() == nil
-}
-
-func (r *MigrationCompatibilityRule) enterDropTable(ctx *parser.Drop_tableContext) {
- normalizedFullDropTableName := snowsqlparser.NormalizeSnowSQLObjectName(ctx.Object_name(), r.currentDatabase, "PUBLIC")
- mustNewCreate, ok := r.normalizedNewCreateTableNameMap[normalizedFullDropTableName]
- if ok && mustNewCreate {
- return
- }
- level := r.level
- if ok && !mustNewCreate {
- level = storepb.Advice_WARNING
- }
- r.AddAdvice(&storepb.Advice{
- Status: level,
- Code: code.CompatibilityDropTable.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Drop table %q may cause incompatibility with the existing data and code", normalizedFullDropTableName),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
- })
-}
-
-func (r *MigrationCompatibilityRule) enterDropSchema(ctx *parser.Drop_schemaContext) {
- normalizedFullDropSchemaName := snowsqlparser.NormalizeSnowSQLSchemaName(ctx.Schema_name(), r.currentDatabase)
- mustNewCreate, ok := r.normalizedNewCreateSchemaNameMap[normalizedFullDropSchemaName]
- if ok && mustNewCreate {
- return
- }
- level := r.level
- if ok && !mustNewCreate {
- level = storepb.Advice_WARNING
+// normalizeTableName mirrors the legacy NormalizeSnowSQLObjectName(name,
+// currentDatabase, "PUBLIC") for omni object names: each present part is
+// folded per Snowflake rules (unquoted uppercased, quoted verbatim), missing
+// database/schema parts fall back to the current database and PUBLIC.
+func (c *migrationCompatibilityChecker) normalizeTableName(name *omniast.ObjectName) string {
+ database := c.currentDatabase
+ schema := "PUBLIC"
+ var table string
+ if name != nil {
+ if d := name.Database.Normalize(); d != "" {
+ database = d
+ }
+ if s := name.Schema.Normalize(); s != "" {
+ schema = s
+ }
+ table = name.Name.Normalize()
}
- r.AddAdvice(&storepb.Advice{
- Status: level,
- Code: code.CompatibilityDropSchema.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Drop schema %q may cause incompatibility with the existing data and code", normalizedFullDropSchemaName),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
- })
-}
-
-func (r *MigrationCompatibilityRule) enterDropDatabase(ctx *parser.Drop_databaseContext) {
- normalizedFullDropDatabaseName := snowsqlparser.NormalizeSnowSQLObjectNamePart(ctx.Id_())
- mustNewCreate, ok := r.normalizedNewCreateDatabaseNameMap[normalizedFullDropDatabaseName]
- if ok && mustNewCreate {
- return
+ parts := []string{database, schema}
+ if table != "" {
+ parts = append(parts, table)
}
- level := r.level
- if ok && !mustNewCreate {
- level = storepb.Advice_WARNING
+ return strings.Join(parts, ".")
+}
+
+// normalizeSchemaName mirrors the legacy NormalizeSnowSQLSchemaName(name,
+// currentDatabase) for omni object names. A 2-part schema name parses as
+// Schema.Name (database.schema); a 1-part name parses as just Name (schema).
+func (c *migrationCompatibilityChecker) normalizeSchemaName(name *omniast.ObjectName) string {
+ database := c.currentDatabase
+ var schema string
+ if name != nil {
+ if d := name.Schema.Normalize(); d != "" {
+ database = d
+ }
+ schema = name.Name.Normalize()
}
- r.AddAdvice(&storepb.Advice{
- Status: level,
- Code: code.CompatibilityDropDatabase.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Drop database %q may cause incompatibility with the existing data and code", normalizedFullDropDatabaseName),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
- })
+ return strings.Join([]string{database, schema}, ".")
}
-func (r *MigrationCompatibilityRule) enterAlterTable(ctx *parser.Alter_tableContext) {
- tableColumnAction := ctx.Table_column_action()
- if tableColumnAction == nil {
- return
- }
- dropColumn := tableColumnAction.DROP()
- if dropColumn == nil {
- return
- }
- allColumnName := tableColumnAction.Column_list().AllColumn_name()
- normalizedAllColumnNames := make([]string, 0, len(allColumnName))
- for _, columnName := range allColumnName {
- allIDs := columnName.AllId_()
- if len(allIDs) == 0 {
- continue
- }
- normalizedAllColumnNames = append(normalizedAllColumnNames, fmt.Sprintf("%q", snowsqlparser.NormalizeSnowSQLObjectNamePart(allIDs[len(allIDs)-1])))
+// normalizeDatabaseName mirrors the legacy NormalizeSnowSQLObjectNamePart on
+// the single identifier of CREATE/DROP DATABASE.
+func normalizeDatabaseName(name *omniast.ObjectName) string {
+ if name == nil {
+ return ""
}
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.CompatibilityDropColumn.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Drop column %s may cause incompatibility with the existing data and code", strings.Join(normalizedAllColumnNames, ",")),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
- })
+ return name.Name.Normalize()
}
diff --git a/backend/plugin/advisor/snowflake/rule_naming_identifier_case.go b/backend/plugin/advisor/snowflake/rule_naming_identifier_case.go
index d6156981e1..c2502c005a 100644
--- a/backend/plugin/advisor/snowflake/rule_naming_identifier_case.go
+++ b/backend/plugin/advisor/snowflake/rule_naming_identifier_case.go
@@ -6,14 +6,12 @@ import (
"fmt"
"strings"
- "github.com/antlr4-go/antlr/v4"
- parser "github.com/bytebase/parser/snowflake"
+ snowflakeast "github.com/bytebase/omni/snowflake/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"github.com/bytebase/bytebase/backend/plugin/advisor/code"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
snowsqlparser "github.com/bytebase/bytebase/backend/plugin/parser/snowflake"
)
@@ -36,125 +34,127 @@ func (*NamingIdentifierCaseAdvisor) Check(_ context.Context, checkCtx advisor.Co
return nil, err
}
- rule := NewNamingIdentifierCaseRule(level, checkCtx.Rule.Type.String())
- checker := NewGenericChecker([]Rule{rule})
+ checker := &namingIdentifierCaseChecker{
+ level: level,
+ title: checkCtx.Rule.Type.String(),
+ adviceList: []*storepb.Advice{},
+ }
for _, stmt := range checkCtx.ParsedStatements {
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
+ node, ok := snowsqlparser.GetOmniNode(stmt.AST)
if !ok {
continue
}
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ checker.text = stmt.Text
+ checker.baseLine = stmt.BaseLine()
+ checker.checkStmt(node)
}
- return checker.GetAdviceList(), nil
+ return checker.adviceList, nil
}
-// NamingIdentifierCaseRule checks for identifier case.
-type NamingIdentifierCaseRule struct {
- BaseRule
- // currentOriginalTableName is the original table name in the statement.
- currentOriginalTableName string
+type namingIdentifierCaseChecker struct {
+ adviceList []*storepb.Advice
+ level storepb.Advice_Status
+ title string
+ // text is the SQL text of the statement currently being checked.
+ text string
+ // baseLine is the 0-based line of the current statement in the whole script.
+ baseLine int
}
-// NewNamingIdentifierCaseRule creates a new NamingIdentifierCaseRule.
-func NewNamingIdentifierCaseRule(level storepb.Advice_Status, title string) *NamingIdentifierCaseRule {
- return &NamingIdentifierCaseRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- }
-}
-
-// Name returns the rule name.
-func (*NamingIdentifierCaseRule) Name() string {
- return "NamingIdentifierCaseRule"
-}
-
-// OnEnter is called when entering a parse tree node.
-func (r *NamingIdentifierCaseRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.currentOriginalTableName = ctx.(*parser.Create_tableContext).Object_name().GetText()
- case "Create_table_as_select":
- r.currentOriginalTableName = ctx.(*parser.Create_table_as_selectContext).Object_name().GetText()
- case NodeTypeColumnDeclItemList:
- r.enterColumnDeclItemList(ctx.(*parser.Column_decl_item_listContext))
- case NodeTypeAlterTable:
- r.enterAlterTable(ctx.(*parser.Alter_tableContext))
- default:
- // Other node types
- }
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (r *NamingIdentifierCaseRule) OnExit(_ antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable, "Create_table_as_select":
- r.currentOriginalTableName = ""
+func (c *namingIdentifierCaseChecker) checkStmt(node snowflakeast.Node) {
+ switch n := node.(type) {
+ case *snowflakeast.CreateTableStmt:
+ c.checkCreateTable(n)
+ case *snowflakeast.AlterTableStmt:
+ c.checkAlterTable(n)
default:
- // Other node types
}
- return nil
}
-func (r *NamingIdentifierCaseRule) enterColumnDeclItemList(ctx *parser.Column_decl_item_listContext) {
- if r.currentOriginalTableName == "" {
+// checkCreateTable checks the declared column names of CREATE TABLE and
+// CREATE TABLE ... AS SELECT: a column whose canonical (folded) name is not
+// upper case — i.e. a double-quoted identifier containing lower-case
+// characters — is reported. Mirroring the legacy listener, every advice is
+// anchored on the start of the column declaration list, so all violations in
+// one statement share that line.
+func (c *namingIdentifierCaseChecker) checkCreateTable(stmt *snowflakeast.CreateTableStmt) {
+ if len(stmt.Columns) == 0 {
return
}
- allItems := ctx.AllColumn_decl_item()
- if len(allItems) == 0 {
- return
+ listPosition := c.columnDeclListPosition(stmt)
+ for _, column := range stmt.Columns {
+ // Snowflake folding: unquoted identifiers are stored upper case, quoted
+ // identifiers verbatim — so only quoted lower-case names can violate.
+ originalColName := column.Name.Normalize()
+ if strings.ToUpper(originalColName) != originalColName {
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: c.level,
+ Code: code.NamingCaseMismatch.Int32(),
+ Title: c.title,
+ Content: fmt.Sprintf("Identifier %q should be upper case", originalColName),
+ StartPosition: listPosition,
+ })
+ }
}
+}
- for _, item := range allItems {
- if fullColDecl := item.Full_col_decl(); fullColDecl != nil {
- allIDs := fullColDecl.Col_decl().Column_name().AllId_()
- if len(allIDs) == 0 {
- continue
- }
- originalID := allIDs[len(allIDs)-1]
- originalColName := snowsqlparser.NormalizeSnowSQLObjectNamePart(originalID)
- if strings.ToUpper(originalColName) != originalColName {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.NamingCaseMismatch.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Identifier %q should be upper case", originalColName),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
- })
- }
+// checkAlterTable checks ALTER TABLE ... RENAME COLUMN old TO new: the new
+// column name must fold to upper case. Other ALTER TABLE actions are not
+// checked, matching the legacy listener.
+func (c *namingIdentifierCaseChecker) checkAlterTable(stmt *snowflakeast.AlterTableStmt) {
+ for _, action := range stmt.Actions {
+ if action.Kind != snowflakeast.AlterTableRenameColumn {
+ continue
+ }
+ renameToColName := action.NewColName.Normalize()
+ if strings.ToUpper(renameToColName) != renameToColName {
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: c.level,
+ Code: code.NamingCaseMismatch.Int32(),
+ Title: c.title,
+ Content: fmt.Sprintf("Identifier %q should be upper case", renameToColName),
+ StartPosition: c.positionAtOffset(action.NewColName.Loc.Start),
+ })
}
}
}
-func (r *NamingIdentifierCaseRule) enterAlterTable(ctx *parser.Alter_tableContext) {
- if ctx.Table_column_action() == nil || ctx.Table_column_action().RENAME() == nil {
- return
+// columnDeclListPosition returns the position of the first item of the column
+// declaration list — columns, out-of-line constraints and inline indexes in
+// source order — mirroring the legacy rule's anchor on the ANTLR
+// Column_decl_item_list start token.
+func (c *namingIdentifierCaseChecker) columnDeclListPosition(stmt *snowflakeast.CreateTableStmt) *storepb.Position {
+ start := -1
+ merge := func(loc snowflakeast.Loc) {
+ if loc.Start >= 0 && (start < 0 || loc.Start < start) {
+ start = loc.Start
+ }
}
- r.currentOriginalTableName = ctx.Object_name(0).GetText()
- renameColumnName := ctx.Table_column_action().Column_name(1)
- allIDs := renameColumnName.AllId_()
- if len(allIDs) == 0 {
- return
+ for _, column := range stmt.Columns {
+ merge(column.Loc)
+ }
+ for _, constraint := range stmt.Constraints {
+ merge(constraint.Loc)
+ }
+ for _, index := range stmt.Indexes {
+ merge(index.Loc)
}
- renameToID := allIDs[len(allIDs)-1]
- renameToColName := snowsqlparser.NormalizeSnowSQLObjectNamePart(renameToID)
- if strings.ToUpper(renameToColName) != renameToColName {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.NamingCaseMismatch.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Identifier %q should be upper case", renameToColName),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + renameToID.GetStart().GetLine()),
- })
+ return c.positionAtOffset(start)
+}
+
+// positionAtOffset converts a byte offset within the current statement text to
+// an advice position, reproducing the legacy baseLine + ANTLR-line arithmetic.
+// A negative (unknown) offset degrades to the statement's first line.
+func (c *namingIdentifierCaseChecker) positionAtOffset(offset int) *storepb.Position {
+ line := 1
+ if offset > 0 {
+ if offset > len(c.text) {
+ offset = len(c.text)
+ }
+ line += strings.Count(c.text[:offset], "\n")
}
+ return common.ConvertANTLRLineToPosition(c.baseLine + line)
}
diff --git a/backend/plugin/advisor/snowflake/rule_naming_identifier_no_keyword.go b/backend/plugin/advisor/snowflake/rule_naming_identifier_no_keyword.go
index 412ab53655..0c484b5175 100644
--- a/backend/plugin/advisor/snowflake/rule_naming_identifier_no_keyword.go
+++ b/backend/plugin/advisor/snowflake/rule_naming_identifier_no_keyword.go
@@ -4,16 +4,14 @@ package snowflake
import (
"context"
"fmt"
+ "strings"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/antlr4-go/antlr/v4"
- parser "github.com/bytebase/parser/snowflake"
+ snowflakeast "github.com/bytebase/omni/snowflake/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/code"
snowsqlparser "github.com/bytebase/bytebase/backend/plugin/parser/snowflake"
)
@@ -36,125 +34,124 @@ func (*NamingIdentifierNoKeywordAdvisor) Check(_ context.Context, checkCtx advis
return nil, err
}
- rule := NewNamingIdentifierNoKeywordRule(level, checkCtx.Rule.Type.String())
- checker := NewGenericChecker([]Rule{rule})
+ checker := &namingIdentifierNoKeywordChecker{
+ level: level,
+ title: checkCtx.Rule.Type.String(),
+ adviceList: []*storepb.Advice{},
+ }
for _, stmt := range checkCtx.ParsedStatements {
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
+ node, ok := snowsqlparser.GetOmniNode(stmt.AST)
if !ok {
continue
}
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
- }
-
- return checker.GetAdviceList(), nil
-}
-
-// NamingIdentifierNoKeywordRule checks for identifier naming without keywords.
-type NamingIdentifierNoKeywordRule struct {
- BaseRule
- // currentOriginalTableName is the original table name in the statement.
- currentOriginalTableName string
-}
-
-// NewNamingIdentifierNoKeywordRule creates a new NamingIdentifierNoKeywordRule.
-func NewNamingIdentifierNoKeywordRule(level storepb.Advice_Status, title string) *NamingIdentifierNoKeywordRule {
- return &NamingIdentifierNoKeywordRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
+ checker.text = stmt.Text
+ checker.baseLine = stmt.BaseLine()
+ checker.checkStmt(node)
}
-}
-// Name returns the rule name.
-func (*NamingIdentifierNoKeywordRule) Name() string {
- return "NamingIdentifierNoKeywordRule"
+ return checker.adviceList, nil
}
-// OnEnter is called when entering a parse tree node.
-func (r *NamingIdentifierNoKeywordRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.currentOriginalTableName = ctx.(*parser.Create_tableContext).Object_name().GetText()
- case "Create_table_as_select":
- r.currentOriginalTableName = ctx.(*parser.Create_table_as_selectContext).Object_name().GetText()
- case NodeTypeColumnDeclItemList:
- r.enterColumnDeclItemList(ctx.(*parser.Column_decl_item_listContext))
- case NodeTypeAlterTable:
- r.enterAlterTable(ctx.(*parser.Alter_tableContext))
- default:
- // Ignore other node types
- }
- return nil
+type namingIdentifierNoKeywordChecker struct {
+ adviceList []*storepb.Advice
+ level storepb.Advice_Status
+ title string
+ // text is the SQL text of the statement currently being checked.
+ text string
+ // baseLine is the 0-based line of the current statement in the whole script.
+ baseLine int
}
-// OnExit is called when exiting a parse tree node.
-func (r *NamingIdentifierNoKeywordRule) OnExit(_ antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable, "Create_table_as_select":
- r.currentOriginalTableName = ""
+func (c *namingIdentifierNoKeywordChecker) checkStmt(node snowflakeast.Node) {
+ switch n := node.(type) {
+ case *snowflakeast.CreateTableStmt:
+ c.checkCreateTable(n)
+ case *snowflakeast.AlterTableStmt:
+ c.checkAlterTable(n)
default:
- // Ignore other node types
}
- return nil
}
-func (r *NamingIdentifierNoKeywordRule) enterColumnDeclItemList(ctx *parser.Column_decl_item_listContext) {
- if r.currentOriginalTableName == "" {
+// checkCreateTable checks the declared column names of CREATE TABLE and
+// CREATE TABLE ... AS SELECT: a column whose canonical (folded) name is a
+// Snowflake keyword is reported. Mirroring the legacy listener, the advice
+// content carries the identifier as written in the source (quotes preserved)
+// and every advice is anchored on the start of the column declaration list.
+func (c *namingIdentifierNoKeywordChecker) checkCreateTable(stmt *snowflakeast.CreateTableStmt) {
+ if len(stmt.Columns) == 0 {
return
}
- allItems := ctx.AllColumn_decl_item()
- if len(allItems) == 0 {
- return
+ listPosition := c.columnDeclListPosition(stmt)
+ for _, column := range stmt.Columns {
+ originalColName := column.Name.Normalize()
+ if snowsqlparser.IsSnowflakeKeyword(originalColName, false) {
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: c.level,
+ Code: code.NameIsKeywordIdentifier.Int32(),
+ Title: c.title,
+ Content: fmt.Sprintf("Identifier %s is a keyword and should be avoided", column.Name.String()),
+ StartPosition: listPosition,
+ })
+ }
}
+}
- for _, item := range allItems {
- if fullColDecl := item.Full_col_decl(); fullColDecl != nil {
- allIDs := fullColDecl.Col_decl().Column_name().AllId_()
- if len(allIDs) == 0 {
- continue
- }
- originalID := allIDs[len(allIDs)-1]
- originalColName := snowsqlparser.NormalizeSnowSQLObjectNamePart(originalID)
- if snowsqlparser.IsSnowflakeKeyword(originalColName, false) {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.NameIsKeywordIdentifier.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Identifier %s is a keyword and should be avoided", originalID.GetText()),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
- })
- }
+// checkAlterTable checks ALTER TABLE ... RENAME COLUMN old TO new: the new
+// column name must not be a Snowflake keyword. Other ALTER TABLE actions are
+// not checked, matching the legacy listener.
+func (c *namingIdentifierNoKeywordChecker) checkAlterTable(stmt *snowflakeast.AlterTableStmt) {
+ for _, action := range stmt.Actions {
+ if action.Kind != snowflakeast.AlterTableRenameColumn {
+ continue
+ }
+ renameToColName := action.NewColName.Normalize()
+ if snowsqlparser.IsSnowflakeKeyword(renameToColName, false) {
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: c.level,
+ Code: code.NameIsKeywordIdentifier.Int32(),
+ Title: c.title,
+ Content: fmt.Sprintf("Identifier %s is a keyword and should be avoided", action.NewColName.String()),
+ StartPosition: c.positionAtOffset(action.NewColName.Loc.Start),
+ })
}
}
}
-func (r *NamingIdentifierNoKeywordRule) enterAlterTable(ctx *parser.Alter_tableContext) {
- if ctx.Table_column_action() == nil || ctx.Table_column_action().RENAME() == nil {
- return
+// columnDeclListPosition returns the position of the first item of the column
+// declaration list — columns, out-of-line constraints and inline indexes in
+// source order — mirroring the legacy rule's anchor on the ANTLR
+// Column_decl_item_list start token.
+func (c *namingIdentifierNoKeywordChecker) columnDeclListPosition(stmt *snowflakeast.CreateTableStmt) *storepb.Position {
+ start := -1
+ merge := func(loc snowflakeast.Loc) {
+ if loc.Start >= 0 && (start < 0 || loc.Start < start) {
+ start = loc.Start
+ }
}
- r.currentOriginalTableName = ctx.Object_name(0).GetText()
- renameColumnName := ctx.Table_column_action().Column_name(1)
- allIDs := renameColumnName.AllId_()
- if len(allIDs) == 0 {
- return
+ for _, column := range stmt.Columns {
+ merge(column.Loc)
+ }
+ for _, constraint := range stmt.Constraints {
+ merge(constraint.Loc)
+ }
+ for _, index := range stmt.Indexes {
+ merge(index.Loc)
}
- renameToID := allIDs[len(allIDs)-1]
- renameToColName := snowsqlparser.NormalizeSnowSQLObjectNamePart(renameToID)
- if snowsqlparser.IsSnowflakeKeyword(renameToColName, false) {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.NameIsKeywordIdentifier.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Identifier %s is a keyword and should be avoided", renameToID.GetText()),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + renameToID.GetStart().GetLine()),
- })
+ return c.positionAtOffset(start)
+}
+
+// positionAtOffset converts a byte offset within the current statement text to
+// an advice position, reproducing the legacy baseLine + ANTLR-line arithmetic.
+// A negative (unknown) offset degrades to the statement's first line.
+func (c *namingIdentifierNoKeywordChecker) positionAtOffset(offset int) *storepb.Position {
+ line := 1
+ if offset > 0 {
+ if offset > len(c.text) {
+ offset = len(c.text)
+ }
+ line += strings.Count(c.text[:offset], "\n")
}
+ return common.ConvertANTLRLineToPosition(c.baseLine + line)
}
diff --git a/backend/plugin/advisor/snowflake/rule_naming_table.go b/backend/plugin/advisor/snowflake/rule_naming_table.go
index 1a5c824be8..7ddb2c4826 100644
--- a/backend/plugin/advisor/snowflake/rule_naming_table.go
+++ b/backend/plugin/advisor/snowflake/rule_naming_table.go
@@ -4,19 +4,17 @@ package snowflake
import (
"context"
"fmt"
- "log/slog"
"regexp"
"strings"
- "github.com/antlr4-go/antlr/v4"
- parser "github.com/bytebase/parser/snowflake"
+ snowflakeast "github.com/bytebase/omni/snowflake/ast"
"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/advisor"
"github.com/bytebase/bytebase/backend/plugin/advisor/code"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
+ snowsqlparser "github.com/bytebase/bytebase/backend/plugin/parser/snowflake"
)
var (
@@ -52,122 +50,104 @@ func (*NamingTableAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([
maxLength = advisor.DefaultNameLengthLimit
}
- rule := NewNamingTableRule(level, checkCtx.Rule.Type.String(), format, maxLength)
- checker := NewGenericChecker([]Rule{rule})
+ checker := &namingTableChecker{
+ level: level,
+ title: checkCtx.Rule.Type.String(),
+ format: format,
+ maxLength: maxLength,
+ adviceList: []*storepb.Advice{},
+ }
for _, stmt := range checkCtx.ParsedStatements {
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
+ node, ok := snowsqlparser.GetOmniNode(stmt.AST)
if !ok {
continue
}
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ checker.text = stmt.Text
+ checker.baseLine = stmt.BaseLine()
+ checker.checkStmt(node)
}
- return checker.GetAdviceList(), nil
-}
-
-// NamingTableRule checks for table naming convention.
-type NamingTableRule struct {
- BaseRule
- format *regexp.Regexp
- maxLength int
-}
-
-// NewNamingTableRule creates a new NamingTableRule.
-func NewNamingTableRule(level storepb.Advice_Status, title string, format *regexp.Regexp, maxLength int) *NamingTableRule {
- return &NamingTableRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- format: format,
- maxLength: maxLength,
- }
+ return checker.adviceList, nil
}
-// Name returns the rule name.
-func (*NamingTableRule) Name() string {
- return "NamingTableRule"
+type namingTableChecker struct {
+ adviceList []*storepb.Advice
+ level storepb.Advice_Status
+ title string
+ format *regexp.Regexp
+ maxLength int
+ // text is the SQL text of the statement currently being checked.
+ text string
+ // baseLine is the 0-based line of the current statement in the whole script.
+ baseLine int
}
-// OnEnter is called when entering a parse tree node.
-func (r *NamingTableRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.enterCreateTable(ctx.(*parser.Create_tableContext))
- case NodeTypeAlterTable:
- r.enterAlterTable(ctx.(*parser.Alter_tableContext))
+func (c *namingTableChecker) checkStmt(node snowflakeast.Node) {
+ switch n := node.(type) {
+ case *snowflakeast.CreateTableStmt:
+ // The legacy listener only fired on the plain CREATE TABLE grammar rule;
+ // CREATE TABLE ... AS SELECT / LIKE / CLONE were separate rules it did
+ // not subscribe to. Mirror that scope exactly.
+ if n.AsSelect != nil || n.Like != nil || n.Clone != nil {
+ return
+ }
+ if n.Name == nil {
+ return
+ }
+ c.checkTableName(n.Name.Name, n.Loc)
+ case *snowflakeast.AlterTableStmt:
+ for _, action := range n.Actions {
+ if action.Kind != snowflakeast.AlterTableRename || action.NewName == nil {
+ continue
+ }
+ c.checkTableName(action.NewName.Name, n.Loc)
+ }
default:
- // Ignore other node types
}
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (*NamingTableRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- // This rule doesn't need exit processing
- return nil
}
-func (r *NamingTableRule) enterCreateTable(ctx *parser.Create_tableContext) {
- objectName := ctx.Object_name().GetO().GetText()
+// checkTableName applies the format and max-length checks to the table part of
+// an object name, anchoring the advice on the statement start (stmtLoc), as
+// the legacy listener did. The legacy rule took the raw source text of the
+// name and stripped only the surrounding double quotes — no case folding and
+// no unescaping of doubled quotes — which Ident.String() (the re-quoted
+// source form) reproduces exactly.
+func (c *namingTableChecker) checkTableName(name snowflakeast.Ident, stmtLoc snowflakeast.Loc) {
+ objectName := name.String()
tableName := strings.TrimPrefix(strings.TrimSuffix(objectName, `"`), `"`)
+ position := c.positionAtOffset(stmtLoc.Start)
- if !r.format.MatchString(tableName) {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ if !c.format.MatchString(tableName) {
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: c.level,
Code: code.NamingTableConventionMismatch.Int32(),
- Title: r.title,
- Content: fmt.Sprintf(`"%s" mismatches table naming convention, naming format should be %q`, tableName, r.format),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ Title: c.title,
+ Content: fmt.Sprintf(`"%s" mismatches table naming convention, naming format should be %q`, tableName, c.format),
+ StartPosition: position,
})
}
- if r.maxLength > 0 && len(tableName) > r.maxLength {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ if c.maxLength > 0 && len(tableName) > c.maxLength {
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: c.level,
Code: code.NamingTableConventionMismatch.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("\"%s\" mismatches table naming convention, its length should be within %d characters", tableName, r.maxLength),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ Title: c.title,
+ Content: fmt.Sprintf("\"%s\" mismatches table naming convention, its length should be within %d characters", tableName, c.maxLength),
+ StartPosition: position,
})
}
}
-func (r *NamingTableRule) enterAlterTable(ctx *parser.Alter_tableContext) {
- if ctx.RENAME() == nil {
- return
- }
-
- allObjectNames := ctx.AllObject_name()
- if len(allObjectNames) != 2 {
- slog.Warn("Unexpected number of object names in alter table rename statement", slog.Int("objectNameCount", len(allObjectNames)))
- return
- }
-
- newObjectName := allObjectNames[1].GetO().GetText()
- tableName := strings.TrimPrefix(strings.TrimSuffix(newObjectName, `"`), `"`)
-
- if !r.format.MatchString(tableName) {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.NamingTableConventionMismatch.Int32(),
- Title: r.title,
- Content: fmt.Sprintf(`"%s" mismatches table naming convention, naming format should be %q`, tableName, r.format),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
- })
- }
- if r.maxLength > 0 && len(tableName) > r.maxLength {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.NamingTableConventionMismatch.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("\"%s\" mismatches table naming convention, its length should be within %d characters", tableName, r.maxLength),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
- })
+// positionAtOffset converts a byte offset within the current statement text to
+// an advice position, reproducing the legacy baseLine + ANTLR-line arithmetic.
+// A negative (unknown) offset degrades to the statement's first line.
+func (c *namingTableChecker) positionAtOffset(offset int) *storepb.Position {
+ line := 1
+ if offset > 0 {
+ if offset > len(c.text) {
+ offset = len(c.text)
+ }
+ line += strings.Count(c.text[:offset], "\n")
}
+ return common.ConvertANTLRLineToPosition(c.baseLine + line)
}
diff --git a/backend/plugin/advisor/snowflake/rule_naming_table_no_keyword.go b/backend/plugin/advisor/snowflake/rule_naming_table_no_keyword.go
index 80d31b8490..9b70a5d453 100644
--- a/backend/plugin/advisor/snowflake/rule_naming_table_no_keyword.go
+++ b/backend/plugin/advisor/snowflake/rule_naming_table_no_keyword.go
@@ -5,14 +5,11 @@ import (
"context"
"fmt"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/antlr4-go/antlr/v4"
- parser "github.com/bytebase/parser/snowflake"
+ snowflakeast "github.com/bytebase/omni/snowflake/ast"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/code"
snowsqlparser "github.com/bytebase/bytebase/backend/plugin/parser/snowflake"
)
@@ -35,87 +32,63 @@ func (*NamingTableNoKeywordAdvisor) Check(_ context.Context, checkCtx advisor.Co
return nil, err
}
- rule := NewNamingTableNoKeywordRule(level, checkCtx.Rule.Type.String())
- checker := NewGenericChecker([]Rule{rule})
+ checker := &namingTableNoKeywordChecker{
+ level: level,
+ title: checkCtx.Rule.Type.String(),
+ adviceList: []*storepb.Advice{},
+ }
for _, stmt := range checkCtx.ParsedStatements {
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
+ node, ok := snowsqlparser.GetOmniNode(stmt.AST)
if !ok {
continue
}
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ checker.checkStmt(node)
}
- return checker.GetAdviceList(), nil
+ return checker.adviceList, nil
}
-// NamingTableNoKeywordRule checks for table naming without keywords.
-type NamingTableNoKeywordRule struct {
- BaseRule
+type namingTableNoKeywordChecker struct {
+ adviceList []*storepb.Advice
+ level storepb.Advice_Status
+ title string
}
-// NewNamingTableNoKeywordRule creates a new NamingTableNoKeywordRule.
-func NewNamingTableNoKeywordRule(level storepb.Advice_Status, title string) *NamingTableNoKeywordRule {
- return &NamingTableNoKeywordRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- }
-}
-
-// Name returns the rule name.
-func (*NamingTableNoKeywordRule) Name() string {
- return "NamingTableNoKeywordRule"
-}
-
-// OnEnter is called when entering a parse tree node.
-func (r *NamingTableNoKeywordRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.enterCreateTable(ctx.(*parser.Create_tableContext))
- case NodeTypeAlterTable:
- r.enterAlterTable(ctx.(*parser.Alter_tableContext))
+func (c *namingTableNoKeywordChecker) checkStmt(node snowflakeast.Node) {
+ switch n := node.(type) {
+ case *snowflakeast.CreateTableStmt:
+ // The legacy listener only fired on the plain CREATE TABLE grammar rule;
+ // CREATE TABLE ... AS SELECT / LIKE / CLONE were separate rules it did
+ // not subscribe to. Mirror that scope exactly.
+ if n.AsSelect != nil || n.Like != nil || n.Clone != nil {
+ return
+ }
+ if n.Name == nil {
+ return
+ }
+ c.checkTableName(n.Name.Name)
+ case *snowflakeast.AlterTableStmt:
+ for _, action := range n.Actions {
+ if action.Kind != snowflakeast.AlterTableRename || action.NewName == nil {
+ continue
+ }
+ c.checkTableName(action.NewName.Name)
+ }
default:
- // Ignore other node types
- }
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (*NamingTableNoKeywordRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- // This rule doesn't need exit processing
- return nil
-}
-
-func (r *NamingTableNoKeywordRule) enterCreateTable(ctx *parser.Create_tableContext) {
- tableName := snowsqlparser.NormalizeSnowSQLObjectNamePart(ctx.Object_name().GetO())
- if snowsqlparser.IsSnowflakeKeyword(tableName, false) {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.NameIsKeywordIdentifier.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Table name %q is a keyword identifier and should be avoided.", tableName),
- })
}
}
-func (r *NamingTableNoKeywordRule) enterAlterTable(ctx *parser.Alter_tableContext) {
- if ctx.RENAME() == nil {
- return
- }
-
- tableName := snowsqlparser.NormalizeSnowSQLObjectNamePart(ctx.Object_name(1).GetO())
+// checkTableName reports the table part of an object name when its canonical
+// (folded) form is a Snowflake keyword. The legacy listener emitted this
+// advice without a start position; keep that shape for identical output.
+func (c *namingTableNoKeywordChecker) checkTableName(name snowflakeast.Ident) {
+ tableName := name.Normalize()
if snowsqlparser.IsSnowflakeKeyword(tableName, false) {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: c.level,
Code: code.NameIsKeywordIdentifier.Int32(),
- Title: r.title,
+ Title: c.title,
Content: fmt.Sprintf("Table name %q is a keyword identifier and should be avoided.", tableName),
})
}
diff --git a/backend/plugin/advisor/snowflake/rule_select_no_select_all.go b/backend/plugin/advisor/snowflake/rule_select_no_select_all.go
index 0645679dfe..28c059d301 100644
--- a/backend/plugin/advisor/snowflake/rule_select_no_select_all.go
+++ b/backend/plugin/advisor/snowflake/rule_select_no_select_all.go
@@ -3,15 +3,16 @@ package snowflake
import (
"context"
+ "slices"
+ "strings"
- "github.com/antlr4-go/antlr/v4"
- parser "github.com/bytebase/parser/snowflake"
+ omniast "github.com/bytebase/omni/snowflake/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"github.com/bytebase/bytebase/backend/plugin/advisor/code"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
+ snowsqlparser "github.com/bytebase/bytebase/backend/plugin/parser/snowflake"
)
var (
@@ -33,67 +34,81 @@ func (*SelectNoSelectAllAdvisor) Check(_ context.Context, checkCtx advisor.Conte
return nil, err
}
- rule := NewSelectNoSelectAllRule(level, checkCtx.Rule.Type.String())
- checker := NewGenericChecker([]Rule{rule})
+ checker := &selectNoSelectAllChecker{
+ level: level,
+ title: checkCtx.Rule.Type.String(),
+ }
for _, stmt := range checkCtx.ParsedStatements {
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
+ node, ok := snowsqlparser.GetOmniNode(stmt.AST)
if !ok {
continue
}
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ checker.checkStmt(node, stmt.Text, stmt.BaseLine())
}
- return checker.GetAdviceList(), nil
-}
-
-// SelectNoSelectAllRule checks for no select all.
-type SelectNoSelectAllRule struct {
- BaseRule
+ return checker.adviceList, nil
}
-// NewSelectNoSelectAllRule creates a new SelectNoSelectAllRule.
-func NewSelectNoSelectAllRule(level storepb.Advice_Status, title string) *SelectNoSelectAllRule {
- return &SelectNoSelectAllRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- }
-}
-
-// Name returns the rule name.
-func (*SelectNoSelectAllRule) Name() string {
- return "SelectNoSelectAllRule"
+type selectNoSelectAllChecker struct {
+ adviceList []*storepb.Advice
+ level storepb.Advice_Status
+ title string
}
-// OnEnter is called when entering a parse tree node.
-func (r *SelectNoSelectAllRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- if nodeType == NodeTypeSelectListElem {
- r.enterSelectListElem(ctx.(*parser.Select_list_elemContext))
- }
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (*SelectNoSelectAllRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- // This rule doesn't need exit processing
- return nil
-}
+// checkStmt flags every `*` / `qualifier.*` SELECT-list item anywhere in the
+// statement (top-level query, subqueries, CTEs, INSERT ... SELECT, ...),
+// mirroring the legacy listener that fired on every Select_list_elem holding a
+// column_elem_star. Stars inside expressions (e.g. COUNT(*)) are not
+// SELECT-list stars and are not flagged, matching the legacy behavior.
+func (c *selectNoSelectAllChecker) checkStmt(node omniast.Node, text string, baseLine int) {
+ // Collect the star-token offsets first and emit advices in text order so
+ // the advice order matches the legacy depth-first listener walk.
+ var starOffsets []int
+ omniast.Inspect(node, func(n omniast.Node) bool {
+ selectStmt, ok := n.(*omniast.SelectStmt)
+ if !ok {
+ return true
+ }
+ for _, target := range selectStmt.Targets {
+ if target == nil || !target.Star {
+ continue
+ }
+ // Anchor on the `*` token itself (the legacy rule used the STAR
+ // token's line). For both `*` and `qualifier.*` the star is the
+ // last byte of the StarExpr.
+ offset := target.Loc.Start
+ if star, ok := target.Expr.(*omniast.StarExpr); ok && star.Loc.IsValid() {
+ offset = star.Loc.End - 1
+ }
+ starOffsets = append(starOffsets, offset)
+ }
+ return true
+ })
+ slices.Sort(starOffsets)
-func (r *SelectNoSelectAllRule) enterSelectListElem(ctx *parser.Select_list_elemContext) {
- if v := ctx.Column_elem_star(); v != nil {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ for _, offset := range starOffsets {
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: c.level,
Code: code.StatementSelectAll.Int32(),
- Title: r.title,
+ Title: c.title,
Content: "Avoid using SELECT *.",
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + v.STAR().GetSymbol().GetLine()),
+ StartPosition: common.ConvertANTLRLineToPosition(baseLine + statementLineForOffset(text, offset)),
})
}
}
+
+// statementLineForOffset converts a byte offset within a statement's text into
+// the 1-based line number of that offset — the same line ANTLR assigned to a
+// token at that position when the legacy listeners parsed the identical
+// per-statement text. Add the statement's BaseLine() to obtain the line in the
+// whole script.
+func statementLineForOffset(text string, offset int) int {
+ if offset < 0 {
+ offset = 0
+ }
+ if offset > len(text) {
+ offset = len(text)
+ }
+ return strings.Count(text[:offset], "\n") + 1
+}
diff --git a/backend/plugin/advisor/snowflake/rule_table_drop_naming_convention.go b/backend/plugin/advisor/snowflake/rule_table_drop_naming_convention.go
index 398d491794..54456a0302 100644
--- a/backend/plugin/advisor/snowflake/rule_table_drop_naming_convention.go
+++ b/backend/plugin/advisor/snowflake/rule_table_drop_naming_convention.go
@@ -5,16 +5,15 @@ import (
"context"
"fmt"
"regexp"
+ "strings"
- "github.com/antlr4-go/antlr/v4"
- parser "github.com/bytebase/parser/snowflake"
+ snowflakeast "github.com/bytebase/omni/snowflake/ast"
"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/advisor"
"github.com/bytebase/bytebase/backend/plugin/advisor/code"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
snowsqlparser "github.com/bytebase/bytebase/backend/plugin/parser/snowflake"
)
@@ -47,70 +46,69 @@ func (*TableDropNamingConventionAdvisor) Check(_ context.Context, checkCtx advis
return nil, errors.Wrapf(err, "failed to compile regex format %q", namingPayload.Format)
}
- rule := NewTableDropNamingConventionRule(level, checkCtx.Rule.Type.String(), format)
- checker := NewGenericChecker([]Rule{rule})
+ checker := &tableDropNamingConventionChecker{
+ level: level,
+ title: checkCtx.Rule.Type.String(),
+ format: format,
+ adviceList: []*storepb.Advice{},
+ }
for _, stmt := range checkCtx.ParsedStatements {
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
+ node, ok := snowsqlparser.GetOmniNode(stmt.AST)
if !ok {
continue
}
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ checker.text = stmt.Text
+ checker.baseLine = stmt.BaseLine()
+ checker.checkStmt(node)
}
- return checker.GetAdviceList(), nil
+ return checker.adviceList, nil
}
-// TableDropNamingConventionRule checks for table drop naming convention.
-type TableDropNamingConventionRule struct {
- BaseRule
- format *regexp.Regexp
+type tableDropNamingConventionChecker struct {
+ adviceList []*storepb.Advice
+ level storepb.Advice_Status
+ title string
+ format *regexp.Regexp
+ // text is the SQL text of the statement currently being checked.
+ text string
+ // baseLine is the 0-based line of the current statement in the whole script.
+ baseLine int
}
-// NewTableDropNamingConventionRule creates a new TableDropNamingConventionRule.
-func NewTableDropNamingConventionRule(level storepb.Advice_Status, title string, format *regexp.Regexp) *TableDropNamingConventionRule {
- return &TableDropNamingConventionRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- format: format,
+// checkStmt checks DROP TABLE statements: the canonical (folded) table name
+// must match the configured naming format. Other DROP kinds (VIEW, DYNAMIC
+// TABLE, ...) are not checked, matching the legacy listener which only
+// subscribed to the plain DROP TABLE grammar rule.
+func (c *tableDropNamingConventionChecker) checkStmt(node snowflakeast.Node) {
+ dropStmt, ok := node.(*snowflakeast.DropStmt)
+ if !ok || dropStmt.Kind != snowflakeast.DropTable || dropStmt.Name == nil {
+ return
}
-}
-// Name returns the rule name.
-func (*TableDropNamingConventionRule) Name() string {
- return "TableDropNamingConventionRule"
-}
-
-// OnEnter is called when entering a parse tree node.
-func (r *TableDropNamingConventionRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- if nodeType == NodeTypeDropTable {
- r.enterDropTable(ctx.(*parser.Drop_tableContext))
+ normalizedObjectName := dropStmt.Name.Name.Normalize()
+ if !c.format.MatchString(normalizedObjectName) {
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: c.level,
+ Code: code.TableDropNamingConventionMismatch.Int32(),
+ Title: c.title,
+ Content: fmt.Sprintf("%q mismatches drop table naming convention, naming format should be %q", normalizedObjectName, c.format),
+ StartPosition: c.positionAtOffset(dropStmt.Name.Name.Loc.Start),
+ })
}
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (*TableDropNamingConventionRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- // This rule doesn't need exit processing
- return nil
}
-func (r *TableDropNamingConventionRule) enterDropTable(ctx *parser.Drop_tableContext) {
- normalizedObjectName := snowsqlparser.NormalizeSnowSQLObjectNamePart(ctx.Object_name().GetO())
- if !r.format.MatchString(normalizedObjectName) {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.TableDropNamingConventionMismatch.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("%q mismatches drop table naming convention, naming format should be %q", normalizedObjectName, r.format),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.Object_name().GetO().GetStart().GetLine()),
- })
+// positionAtOffset converts a byte offset within the current statement text to
+// an advice position, reproducing the legacy baseLine + ANTLR-line arithmetic.
+// A negative (unknown) offset degrades to the statement's first line.
+func (c *tableDropNamingConventionChecker) positionAtOffset(offset int) *storepb.Position {
+ line := 1
+ if offset > 0 {
+ if offset > len(c.text) {
+ offset = len(c.text)
+ }
+ line += strings.Count(c.text[:offset], "\n")
}
+ return common.ConvertANTLRLineToPosition(c.baseLine + line)
}
diff --git a/backend/plugin/advisor/snowflake/rule_table_no_foreign_key.go b/backend/plugin/advisor/snowflake/rule_table_no_foreign_key.go
index 9615505cdd..080c5b75e7 100644
--- a/backend/plugin/advisor/snowflake/rule_table_no_foreign_key.go
+++ b/backend/plugin/advisor/snowflake/rule_table_no_foreign_key.go
@@ -4,15 +4,14 @@ package snowflake
import (
"context"
"fmt"
+ "strings"
- "github.com/antlr4-go/antlr/v4"
- parser "github.com/bytebase/parser/snowflake"
+ omniast "github.com/bytebase/omni/snowflake/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"github.com/bytebase/bytebase/backend/plugin/advisor/code"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
snowsqlparser "github.com/bytebase/bytebase/backend/plugin/parser/snowflake"
)
@@ -36,41 +35,38 @@ func (*TableNoForeignKeyAdvisor) Check(_ context.Context, checkCtx advisor.Conte
}
rule := NewTableNoForeignKeyRule(level, checkCtx.Rule.Type.String())
- checker := NewGenericChecker([]Rule{rule})
for _, stmt := range checkCtx.ParsedStatements {
if stmt.AST == nil {
continue
}
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
+ node, ok := snowsqlparser.GetOmniNode(stmt.AST)
if !ok {
continue
}
rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule.checkStatement(node, stmt.Text)
}
- return checker.GetAdviceList(), nil
+ return rule.GetAdviceList(), nil
}
// TableNoForeignKeyRule checks for table disallow foreign key.
type TableNoForeignKeyRule struct {
BaseRule
- currentConstraintAction currentConstraintAction
- // currentNormalizedTableName is the current table name, and it is normalized.
- // It should be set then entering create_table, alter_table and so on,
- // and should be reset then exiting them.
- currentNormalizedTableName string
// tableForeignKeyTimes is a map of normalized table name to the times of FOREIGN KEY.
tableForeignKeyTimes map[string]int
// tableOriginalName is a map of normalized table name to original table name.
- // The key of the tableOriginalName is the superset of the key of the tableHasForeignKey.
+ // The key of the tableOriginalName is the superset of the key of the tableForeignKeyTimes.
tableOriginalName map[string]string
// tableLine is a map of normalized table name to the line number of the table.
- // The key of the tableLine is the superset of the key of the tableHasForeignKey.
+ // The key of the tableLine is the superset of the key of the tableForeignKeyTimes.
tableLine map[string]int
+
+ // stmtText is the SQL text of the statement currently being checked; node
+ // Loc offsets are relative to it.
+ stmtText string
}
// NewTableNoForeignKeyRule creates a new TableNoForeignKeyRule.
@@ -80,11 +76,9 @@ func NewTableNoForeignKeyRule(level storepb.Advice_Status, title string) *TableN
level: level,
title: title,
},
- currentConstraintAction: currentConstraintActionNone,
- currentNormalizedTableName: "",
- tableForeignKeyTimes: make(map[string]int),
- tableOriginalName: make(map[string]string),
- tableLine: make(map[string]int),
+ tableForeignKeyTimes: make(map[string]int),
+ tableOriginalName: make(map[string]string),
+ tableLine: make(map[string]int),
}
}
@@ -93,38 +87,6 @@ func (*TableNoForeignKeyRule) Name() string {
return "TableNoForeignKeyRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *TableNoForeignKeyRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.enterCreateTable(ctx.(*parser.Create_tableContext))
- case NodeTypeInlineConstraint:
- r.enterInlineConstraint(ctx.(*parser.Inline_constraintContext))
- case NodeTypeOutOfLineConstraint:
- r.enterOutOfLineConstraint(ctx.(*parser.Out_of_line_constraintContext))
- case "Constraint_action":
- r.enterConstraintAction(ctx.(*parser.Constraint_actionContext))
- case NodeTypeAlterTable:
- r.enterAlterTable(ctx.(*parser.Alter_tableContext))
- default:
- // Ignore other node types
- }
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (r *TableNoForeignKeyRule) OnExit(_ antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.exitCreateTable()
- case NodeTypeAlterTable:
- r.exitAlterTable()
- default:
- // Ignore other node types
- }
- return nil
-}
-
// GetAdviceList returns the accumulated advice list, generating final advice for tables with FK.
func (r *TableNoForeignKeyRule) GetAdviceList() []*storepb.Advice {
for tableName, times := range r.tableForeignKeyTimes {
@@ -141,74 +103,120 @@ func (r *TableNoForeignKeyRule) GetAdviceList() []*storepb.Advice {
return r.adviceList
}
-func (r *TableNoForeignKeyRule) enterCreateTable(ctx *parser.Create_tableContext) {
- originalTableName := ctx.Object_name()
- normalizedTableName := snowsqlparser.NormalizeSnowSQLObjectName(originalTableName, "", "PUBLIC")
-
- r.tableForeignKeyTimes[normalizedTableName] = 0
- r.tableOriginalName[normalizedTableName] = originalTableName.GetText()
- r.tableLine[normalizedTableName] = r.baseLine + ctx.GetStart().GetLine()
- r.currentNormalizedTableName = normalizedTableName
- r.currentConstraintAction = currentConstraintActionAdd
-}
-
-func (r *TableNoForeignKeyRule) exitCreateTable() {
- r.currentNormalizedTableName = ""
- r.currentConstraintAction = currentConstraintActionNone
+// checkStatement checks one statement's omni AST node.
+func (r *TableNoForeignKeyRule) checkStatement(node omniast.Node, stmtText string) {
+ r.stmtText = stmtText
+ switch n := node.(type) {
+ case *omniast.CreateTableStmt:
+ r.checkCreateTable(n)
+ case *omniast.AlterTableStmt:
+ r.checkAlterTable(n)
+ default:
+ }
}
-func (r *TableNoForeignKeyRule) enterInlineConstraint(ctx *parser.Inline_constraintContext) {
- if ctx.REFERENCES() == nil || r.currentNormalizedTableName == "" {
+func (r *TableNoForeignKeyRule) checkCreateTable(stmt *omniast.CreateTableStmt) {
+ // The legacy advisor only fired on plain CREATE TABLE (the Create_table
+ // context); CREATE TABLE ... AS SELECT / LIKE / CLONE parsed as different
+ // contexts and were never checked.
+ if stmt.AsSelect != nil || stmt.Like != nil || stmt.Clone != nil {
return
}
- r.tableForeignKeyTimes[r.currentNormalizedTableName]++
-}
-func (r *TableNoForeignKeyRule) enterOutOfLineConstraint(ctx *parser.Out_of_line_constraintContext) {
- if ctx.REFERENCES() == nil || r.currentNormalizedTableName == "" || r.currentConstraintAction == currentConstraintActionNone {
- return
+ normalizedTableName := r.normalizedTableName(stmt.Name)
+
+ r.tableForeignKeyTimes[normalizedTableName] = 0
+ r.tableOriginalName[normalizedTableName] = stmt.Name.String()
+ r.tableLine[normalizedTableName] = r.baseLine + r.line(stmt.Loc.Start)
+
+ // Inline REFERENCES constraints count but keep the table line.
+ for _, column := range stmt.Columns {
+ if column.InlineConstraint != nil && column.InlineConstraint.Type == omniast.ConstrForeignKey {
+ r.tableForeignKeyTimes[normalizedTableName]++
+ }
}
- switch r.currentConstraintAction {
- case currentConstraintActionAdd:
- r.tableForeignKeyTimes[r.currentNormalizedTableName]++
- r.tableLine[r.currentNormalizedTableName] = r.baseLine + ctx.GetStart().GetLine()
- case currentConstraintActionDrop:
- if times, ok := r.tableForeignKeyTimes[r.currentNormalizedTableName]; ok && times > 0 {
- r.tableForeignKeyTimes[r.currentNormalizedTableName]--
+ // Out-of-line FOREIGN KEY constraints count and move the line to the
+ // constraint, matching the legacy advisor.
+ for _, constraint := range stmt.Constraints {
+ if constraint.Type == omniast.ConstrForeignKey {
+ r.tableForeignKeyTimes[normalizedTableName]++
+ r.tableLine[normalizedTableName] = r.baseLine + r.line(constraint.Loc.Start)
}
- default:
- // Other constraint actions
}
}
-func (r *TableNoForeignKeyRule) enterConstraintAction(ctx *parser.Constraint_actionContext) {
- if r.currentNormalizedTableName == "" {
+func (r *TableNoForeignKeyRule) checkAlterTable(stmt *omniast.AlterTableStmt) {
+ // The legacy advisor only tracked ALTER TABLE statements with a constraint
+ // action (ADD/DROP/RENAME CONSTRAINT); column actions were ignored.
+ if !alterTableHasConstraintActionForFk(stmt) {
return
}
- if ctx.DROP() != nil && ctx.Foreign_key() != nil {
- if times, ok := r.tableForeignKeyTimes[r.currentNormalizedTableName]; ok && times > 0 {
- r.tableForeignKeyTimes[r.currentNormalizedTableName]--
+
+ normalizedTableName := r.normalizedTableName(stmt.Name)
+ r.tableOriginalName[normalizedTableName] = stmt.Name.String()
+
+ for _, action := range stmt.Actions {
+ switch action.Kind {
+ case omniast.AlterTableAddConstraint:
+ if action.Constraint != nil && action.Constraint.Type == omniast.ConstrForeignKey {
+ r.tableForeignKeyTimes[normalizedTableName]++
+ r.tableLine[normalizedTableName] = r.baseLine + r.line(action.Constraint.Loc.Start)
+ }
+ case omniast.AlterTableDropConstraint:
+ // Only an explicit DROP FOREIGN KEY decrements; DROP CONSTRAINT
+ // <name> does not reveal the constraint type, matching the legacy
+ // advisor.
+ if action.DropForeignKey {
+ if times, ok := r.tableForeignKeyTimes[normalizedTableName]; ok && times > 0 {
+ r.tableForeignKeyTimes[normalizedTableName]--
+ }
+ }
+ default:
}
- return
- }
- if ctx.ADD() != nil {
- r.currentConstraintAction = currentConstraintActionAdd
- return
}
}
-func (r *TableNoForeignKeyRule) enterAlterTable(ctx *parser.Alter_tableContext) {
- if ctx.Constraint_action() == nil {
- return
+func alterTableHasConstraintActionForFk(stmt *omniast.AlterTableStmt) bool {
+ for _, action := range stmt.Actions {
+ switch action.Kind {
+ case omniast.AlterTableAddConstraint, omniast.AlterTableDropConstraint, omniast.AlterTableRenameConstraint:
+ return true
+ default:
+ }
}
- originalTableName := ctx.Object_name(0)
- normalizedTableName := snowsqlparser.NormalizeSnowSQLObjectName(originalTableName, "", "PUBLIC")
+ return false
+}
- r.currentNormalizedTableName = normalizedTableName
- r.tableOriginalName[normalizedTableName] = originalTableName.GetText()
+// normalizedTableName normalizes an object name into the same
+// "database.schema.table" key the legacy advisor produced via
+// NormalizeSnowSQLObjectName(name, "", "PUBLIC").
+func (*TableNoForeignKeyRule) normalizedTableName(name *omniast.ObjectName) string {
+ if name == nil {
+ return ""
+ }
+ database := ""
+ if d := name.Database.Normalize(); d != "" {
+ database = d
+ }
+ schema := "PUBLIC"
+ if s := name.Schema.Normalize(); s != "" {
+ schema = s
+ }
+ parts := []string{database, schema}
+ if o := name.Name.Normalize(); o != "" {
+ parts = append(parts, o)
+ }
+ return strings.Join(parts, ".")
}
-func (r *TableNoForeignKeyRule) exitAlterTable() {
- r.currentNormalizedTableName = ""
- r.currentConstraintAction = currentConstraintActionNone
+// line converts a byte offset within the current statement text to a 1-based
+// line number, mirroring the ANTLR token line the legacy advisor reported.
+func (r *TableNoForeignKeyRule) line(offset int) int {
+ if offset < 0 {
+ return 1
+ }
+ if offset > len(r.stmtText) {
+ offset = len(r.stmtText)
+ }
+ return 1 + strings.Count(r.stmtText[:offset], "\n")
}
diff --git a/backend/plugin/advisor/snowflake/rule_table_require_pk.go b/backend/plugin/advisor/snowflake/rule_table_require_pk.go
index 93bb4a81b1..a463fde451 100644
--- a/backend/plugin/advisor/snowflake/rule_table_require_pk.go
+++ b/backend/plugin/advisor/snowflake/rule_table_require_pk.go
@@ -4,15 +4,14 @@ package snowflake
import (
"context"
"fmt"
+ "strings"
- "github.com/antlr4-go/antlr/v4"
- parser "github.com/bytebase/parser/snowflake"
+ omniast "github.com/bytebase/omni/snowflake/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"github.com/bytebase/bytebase/backend/plugin/advisor/code"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
snowsqlparser "github.com/bytebase/bytebase/backend/plugin/parser/snowflake"
)
@@ -36,32 +35,25 @@ func (*TableRequirePkAdvisor) Check(_ context.Context, checkCtx advisor.Context)
}
rule := NewTableRequirePkRule(level, checkCtx.Rule.Type.String())
- checker := NewGenericChecker([]Rule{rule})
for _, stmt := range checkCtx.ParsedStatements {
if stmt.AST == nil {
continue
}
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
+ node, ok := snowsqlparser.GetOmniNode(stmt.AST)
if !ok {
continue
}
rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule.checkStatement(node, stmt.Text)
}
- return checker.GetAdviceList(), nil
+ return rule.GetAdviceList(), nil
}
// TableRequirePkRule checks for table require primary key.
type TableRequirePkRule struct {
BaseRule
- currentConstraintAction currentConstraintAction
- // currentNormalizedTableName is the current table name, and it is normalized.
- // It should be set then entering create_table, alter_table and so on,
- // and should be reset then exiting them.
- currentNormalizedTableName string
// tableHasPrimaryKey is a map of normalized table name to whether the table has primary key.
tableHasPrimaryKey map[string]bool
@@ -71,6 +63,10 @@ type TableRequirePkRule struct {
// tableLine is a map of normalized table name to the line number of the table.
// The key of the tableLine is the superset of the key of the tableHasPrimaryKey.
tableLine map[string]int
+
+ // stmtText is the SQL text of the statement currently being checked; node
+ // Loc offsets are relative to it.
+ stmtText string
}
// NewTableRequirePkRule creates a new TableRequirePkRule.
@@ -80,11 +76,9 @@ func NewTableRequirePkRule(level storepb.Advice_Status, title string) *TableRequ
level: level,
title: title,
},
- currentConstraintAction: currentConstraintActionNone,
- currentNormalizedTableName: "",
- tableHasPrimaryKey: make(map[string]bool),
- tableOriginalName: make(map[string]string),
- tableLine: make(map[string]int),
+ tableHasPrimaryKey: make(map[string]bool),
+ tableOriginalName: make(map[string]string),
+ tableLine: make(map[string]int),
}
}
@@ -93,47 +87,12 @@ func (*TableRequirePkRule) Name() string {
return "TableRequirePkRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *TableRequirePkRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.enterCreateTable(ctx.(*parser.Create_tableContext))
- case NodeTypeDropTable:
- r.enterDropTable(ctx.(*parser.Drop_tableContext))
- case NodeTypeInlineConstraint:
- r.enterInlineConstraint(ctx.(*parser.Inline_constraintContext))
- case NodeTypeOutOfLineConstraint:
- r.enterOutOfLineConstraint(ctx.(*parser.Out_of_line_constraintContext))
- case "Constraint_action":
- r.enterConstraintAction(ctx.(*parser.Constraint_actionContext))
- case NodeTypeAlterTable:
- r.enterAlterTable(ctx.(*parser.Alter_tableContext))
- default:
- // Ignore other node types
- }
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (r *TableRequirePkRule) OnExit(_ antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.exitCreateTable()
- case NodeTypeAlterTable:
- r.exitAlterTable()
- default:
- // Other node types
- }
- return nil
-}
-
// GetAdviceList returns the accumulated advice list, generating final advice for tables without PK.
func (r *TableRequirePkRule) GetAdviceList() []*storepb.Advice {
for tableName, has := range r.tableHasPrimaryKey {
if !has {
- // Directly append to adviceList instead of using AddAdvice because
- // tableLine already stores absolute line numbers (baseLine + line at time of encounter).
- // Using AddAdvice would incorrectly add the current baseLine again.
+ // tableLine already stores absolute line numbers (baseLine + line at
+ // time of encounter), so append directly without re-adding baseLine.
r.adviceList = append(r.adviceList, &storepb.Advice{
Status: r.level,
Code: code.TableNoPK.Int32(),
@@ -146,82 +105,129 @@ func (r *TableRequirePkRule) GetAdviceList() []*storepb.Advice {
return r.adviceList
}
-func (r *TableRequirePkRule) enterCreateTable(ctx *parser.Create_tableContext) {
- originalTableName := ctx.Object_name()
- normalizedTableName := snowsqlparser.NormalizeSnowSQLObjectName(originalTableName, "", "PUBLIC")
+// checkStatement checks one statement's omni AST node.
+func (r *TableRequirePkRule) checkStatement(node omniast.Node, stmtText string) {
+ r.stmtText = stmtText
+ switch n := node.(type) {
+ case *omniast.CreateTableStmt:
+ r.checkCreateTable(n)
+ case *omniast.DropStmt:
+ r.checkDropStmt(n)
+ case *omniast.AlterTableStmt:
+ r.checkAlterTable(n)
+ default:
+ }
+}
+
+func (r *TableRequirePkRule) checkCreateTable(stmt *omniast.CreateTableStmt) {
+ // The legacy advisor only fired on plain CREATE TABLE (the Create_table
+ // context); CREATE TABLE ... AS SELECT / LIKE / CLONE parsed as different
+ // contexts and were never checked.
+ if stmt.AsSelect != nil || stmt.Like != nil || stmt.Clone != nil {
+ return
+ }
+
+ normalizedTableName := r.normalizedTableName(stmt.Name)
r.tableHasPrimaryKey[normalizedTableName] = false
- r.tableOriginalName[normalizedTableName] = originalTableName.GetText()
- r.tableLine[normalizedTableName] = r.baseLine + ctx.GetStart().GetLine()
- r.currentNormalizedTableName = normalizedTableName
- r.currentConstraintAction = currentConstraintActionAdd
+ r.tableOriginalName[normalizedTableName] = stmt.Name.String()
+ r.tableLine[normalizedTableName] = r.baseLine + r.line(stmt.Loc.Start)
+
+ for _, column := range stmt.Columns {
+ if column.InlineConstraint != nil && column.InlineConstraint.Type == omniast.ConstrPrimaryKey {
+ r.tableHasPrimaryKey[normalizedTableName] = true
+ }
+ }
+ for _, constraint := range stmt.Constraints {
+ if constraint.Type == omniast.ConstrPrimaryKey {
+ r.tableHasPrimaryKey[normalizedTableName] = true
+ }
+ }
}
-func (r *TableRequirePkRule) enterDropTable(ctx *parser.Drop_tableContext) {
- originalTableName := ctx.Object_name()
- normalizedTableName := snowsqlparser.NormalizeSnowSQLObjectName(originalTableName, "", "PUBLIC")
+func (r *TableRequirePkRule) checkDropStmt(stmt *omniast.DropStmt) {
+ if stmt.Kind != omniast.DropTable {
+ return
+ }
+ normalizedTableName := r.normalizedTableName(stmt.Name)
delete(r.tableHasPrimaryKey, normalizedTableName)
delete(r.tableOriginalName, normalizedTableName)
delete(r.tableLine, normalizedTableName)
}
-func (r *TableRequirePkRule) exitCreateTable() {
- r.currentNormalizedTableName = ""
- r.currentConstraintAction = currentConstraintActionNone
-}
-
-func (r *TableRequirePkRule) enterInlineConstraint(ctx *parser.Inline_constraintContext) {
- if ctx.Primary_key() == nil || r.currentNormalizedTableName == "" {
+func (r *TableRequirePkRule) checkAlterTable(stmt *omniast.AlterTableStmt) {
+ // The legacy advisor only tracked ALTER TABLE statements with a constraint
+ // action (ADD/DROP/RENAME CONSTRAINT); column actions were ignored.
+ if !alterTableHasConstraintActionForPk(stmt) {
return
}
- r.tableHasPrimaryKey[r.currentNormalizedTableName] = true
-}
-func (r *TableRequirePkRule) enterOutOfLineConstraint(ctx *parser.Out_of_line_constraintContext) {
- if ctx.Primary_key() == nil || r.currentNormalizedTableName == "" || r.currentConstraintAction == currentConstraintActionNone {
- return
- }
- switch r.currentConstraintAction {
- case currentConstraintActionAdd:
- r.tableHasPrimaryKey[r.currentNormalizedTableName] = true
- case currentConstraintActionDrop:
- r.tableHasPrimaryKey[r.currentNormalizedTableName] = false
- r.tableLine[r.currentNormalizedTableName] = r.baseLine + ctx.GetStart().GetLine()
- default:
- // No action for other constraint actions
+ normalizedTableName := r.normalizedTableName(stmt.Name)
+ r.tableOriginalName[normalizedTableName] = stmt.Name.String()
+
+ for _, action := range stmt.Actions {
+ switch action.Kind {
+ case omniast.AlterTableAddConstraint:
+ if action.Constraint != nil && action.Constraint.Type == omniast.ConstrPrimaryKey {
+ r.tableHasPrimaryKey[normalizedTableName] = true
+ }
+ case omniast.AlterTableDropConstraint:
+ // Only an explicit DROP PRIMARY KEY flips the flag; DROP CONSTRAINT
+ // <name> does not reveal the constraint type, matching the legacy
+ // advisor.
+ if action.IsPrimaryKey {
+ if _, ok := r.tableHasPrimaryKey[normalizedTableName]; ok {
+ r.tableHasPrimaryKey[normalizedTableName] = false
+ r.tableLine[normalizedTableName] = r.baseLine + r.line(action.Loc.Start)
+ }
+ }
+ default:
+ }
}
}
-func (r *TableRequirePkRule) enterConstraintAction(ctx *parser.Constraint_actionContext) {
- if r.currentNormalizedTableName == "" {
- return
- }
- if ctx.DROP() != nil && (ctx.Primary_key() != nil || ctx.PRIMARY() != nil) {
- if _, ok := r.tableHasPrimaryKey[r.currentNormalizedTableName]; ok {
- r.tableHasPrimaryKey[r.currentNormalizedTableName] = false
- r.tableLine[r.currentNormalizedTableName] = r.baseLine + ctx.GetStart().GetLine()
+func alterTableHasConstraintActionForPk(stmt *omniast.AlterTableStmt) bool {
+ for _, action := range stmt.Actions {
+ switch action.Kind {
+ case omniast.AlterTableAddConstraint, omniast.AlterTableDropConstraint, omniast.AlterTableRenameConstraint:
+ return true
+ default:
}
- return
- }
- if ctx.ADD() != nil {
- r.currentConstraintAction = currentConstraintActionAdd
- return
}
+ return false
}
-func (r *TableRequirePkRule) enterAlterTable(ctx *parser.Alter_tableContext) {
- if ctx.Constraint_action() == nil {
- return
+// normalizedTableName normalizes an object name into the same
+// "database.schema.table" key the legacy advisor produced via
+// NormalizeSnowSQLObjectName(name, "", "PUBLIC").
+func (*TableRequirePkRule) normalizedTableName(name *omniast.ObjectName) string {
+ if name == nil {
+ return ""
}
- originalTableName := ctx.Object_name(0)
- normalizedTableName := snowsqlparser.NormalizeSnowSQLObjectName(originalTableName, "", "PUBLIC")
-
- r.currentNormalizedTableName = normalizedTableName
- r.tableOriginalName[normalizedTableName] = originalTableName.GetText()
+ database := ""
+ if d := name.Database.Normalize(); d != "" {
+ database = d
+ }
+ schema := "PUBLIC"
+ if s := name.Schema.Normalize(); s != "" {
+ schema = s
+ }
+ parts := []string{database, schema}
+ if o := name.Name.Normalize(); o != "" {
+ parts = append(parts, o)
+ }
+ return strings.Join(parts, ".")
}
-func (r *TableRequirePkRule) exitAlterTable() {
- r.currentNormalizedTableName = ""
- r.currentConstraintAction = currentConstraintActionNone
+// line converts a byte offset within the current statement text to a 1-based
+// line number, mirroring the ANTLR token line the legacy advisor reported.
+func (r *TableRequirePkRule) line(offset int) int {
+ if offset < 0 {
+ return 1
+ }
+ if offset > len(r.stmtText) {
+ offset = len(r.stmtText)
+ }
+ return 1 + strings.Count(r.stmtText[:offset], "\n")
}
diff --git a/backend/plugin/advisor/snowflake/rule_where_require_select.go b/backend/plugin/advisor/snowflake/rule_where_require_select.go
index 9f67acb3a4..80c28f2a8c 100644
--- a/backend/plugin/advisor/snowflake/rule_where_require_select.go
+++ b/backend/plugin/advisor/snowflake/rule_where_require_select.go
@@ -3,15 +3,16 @@ package snowflake
import (
"context"
+ "slices"
+ "strings"
- "github.com/antlr4-go/antlr/v4"
- parser "github.com/bytebase/parser/snowflake"
+ omniast "github.com/bytebase/omni/snowflake/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"github.com/bytebase/bytebase/backend/plugin/advisor/code"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
+ snowsqlparser "github.com/bytebase/bytebase/backend/plugin/parser/snowflake"
)
var (
@@ -33,75 +34,103 @@ func (*WhereRequireForSelectAdvisor) Check(_ context.Context, checkCtx advisor.C
return nil, err
}
- rule := NewWhereRequireForSelectRule(level, checkCtx.Rule.Type.String())
- checker := NewGenericChecker([]Rule{rule})
+ checker := &whereRequireForSelectChecker{
+ level: level,
+ title: checkCtx.Rule.Type.String(),
+ }
for _, stmt := range checkCtx.ParsedStatements {
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
+ node, ok := snowsqlparser.GetOmniNode(stmt.AST)
if !ok {
continue
}
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ checker.checkStmt(node, stmt.Text, stmt.BaseLine())
}
- return checker.GetAdviceList(), nil
+ return checker.adviceList, nil
}
-// WhereRequireForSelectRule checks for WHERE clause requirement in SELECT statements.
-type WhereRequireForSelectRule struct {
- BaseRule
-}
-
-// NewWhereRequireForSelectRule creates a new WhereRequireForSelectRule.
-func NewWhereRequireForSelectRule(level storepb.Advice_Status, title string) *WhereRequireForSelectRule {
- return &WhereRequireForSelectRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- }
+type whereRequireForSelectChecker struct {
+ adviceList []*storepb.Advice
+ level storepb.Advice_Status
+ title string
}
-// Name returns the rule name.
-func (*WhereRequireForSelectRule) Name() string {
- return "WhereRequireForSelectRule"
-}
+// checkStmt flags every SELECT with a FROM clause but no WHERE clause anywhere
+// in the statement — including CTE bodies, derived tables, and subqueries —
+// mirroring the legacy listener that fired on every Select_statement context.
+// SELECTs without a FROM clause (e.g. SELECT 1) are allowed.
+func (c *whereRequireForSelectChecker) checkStmt(node omniast.Node, text string, baseLine int) {
+ // Collect anchor offsets first and emit advices in text order so the
+ // advice order matches the legacy depth-first listener walk (a CTE body
+ // is reported before the main SELECT that follows it).
+ var offsets []int
+ omniast.Inspect(node, func(n omniast.Node) bool {
+ selectStmt, ok := n.(*omniast.SelectStmt)
+ if !ok {
+ return true
+ }
+ // Allow SELECT queries without a FROM clause to proceed, e.g. SELECT 1.
+ if selectStmt.Where == nil && selectStmt.From != nil {
+ offsets = append(offsets, selectBodyStart(selectStmt, text))
+ }
+ return true
+ })
+ slices.Sort(offsets)
-// OnEnter is called when entering a parse tree node.
-func (r *WhereRequireForSelectRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- if nodeType == NodeTypeSelectStatement {
- r.enterSelectStatement(ctx.(*parser.Select_statementContext))
+ for _, offset := range offsets {
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: c.level,
+ Code: code.StatementNoWhere.Int32(),
+ Title: c.title,
+ Content: "WHERE clause is required for SELECT statement.",
+ StartPosition: common.ConvertANTLRLineToPosition(baseLine + statementLineForOffset(text, offset)),
+ })
}
- return nil
}
-// OnExit is called when exiting a parse tree node.
-func (*WhereRequireForSelectRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- // This rule doesn't need exit processing
- return nil
+// selectBodyStart returns the byte offset of the SELECT keyword that opens the
+// body of selectStmt within text. For a plain SELECT that is Loc.Start; for a
+// WITH ... SELECT the omni parser extends Loc.Start back over the CTE list,
+// while the legacy ANTLR Select_statement context started at the body SELECT
+// itself — so skip past the last CTE's closing paren and any trivia to land on
+// the SELECT keyword, reproducing the legacy line exactly.
+func selectBodyStart(selectStmt *omniast.SelectStmt, text string) int {
+ start := selectStmt.Loc.Start
+ if n := len(selectStmt.With); n > 0 {
+ last := selectStmt.With[n-1]
+ if last != nil && last.Loc.End >= 0 && last.Loc.End <= len(text) {
+ if offset, ok := skipSpaceAndComments(text, last.Loc.End); ok {
+ start = offset
+ }
+ }
+ }
+ return start
}
-func (r *WhereRequireForSelectRule) enterSelectStatement(ctx *parser.Select_statementContext) {
- if ctx == nil {
- return
- }
- optional := ctx.Select_optional_clauses()
- if optional == nil {
- return
- }
- // Allow SELECT queries without a FROM clause to proceed, e.g. SELECT 1.
- if optional.Where_clause() == nil && optional.From_clause() != nil {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.StatementNoWhere.Int32(),
- Title: r.title,
- Content: "WHERE clause is required for SELECT statement.",
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
- })
+// skipSpaceAndComments advances offset past whitespace and SQL comments
+// (`--` and `//` line comments, `/* */` block comments) and returns the offset
+// of the next token, or false when only trivia remains.
+func skipSpaceAndComments(text string, offset int) (int, bool) {
+ for offset < len(text) {
+ switch c := text[offset]; {
+ case c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\v' || c == '\f':
+ offset++
+ case strings.HasPrefix(text[offset:], "--") || strings.HasPrefix(text[offset:], "//"):
+ next := strings.IndexByte(text[offset:], '\n')
+ if next < 0 {
+ return 0, false
+ }
+ offset += next + 1
+ case strings.HasPrefix(text[offset:], "/*"):
+ end := strings.Index(text[offset+2:], "*/")
+ if end < 0 {
+ return 0, false
+ }
+ offset += 2 + end + 2
+ default:
+ return offset, true
+ }
}
+ return 0, false
}
diff --git a/backend/plugin/advisor/snowflake/rule_where_require_update_delete.go b/backend/plugin/advisor/snowflake/rule_where_require_update_delete.go
index 30f3abac99..5fc5d30a17 100644
--- a/backend/plugin/advisor/snowflake/rule_where_require_update_delete.go
+++ b/backend/plugin/advisor/snowflake/rule_where_require_update_delete.go
@@ -3,15 +3,15 @@ package snowflake
import (
"context"
+ "fmt"
- "github.com/antlr4-go/antlr/v4"
- parser "github.com/bytebase/parser/snowflake"
+ omniast "github.com/bytebase/omni/snowflake/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"github.com/bytebase/bytebase/backend/plugin/advisor/code"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
+ snowsqlparser "github.com/bytebase/bytebase/backend/plugin/parser/snowflake"
)
var (
@@ -33,84 +33,56 @@ func (*WhereRequireForUpdateDeleteAdvisor) Check(_ context.Context, checkCtx adv
return nil, err
}
- rule := NewWhereRequireForUpdateDeleteRule(level, checkCtx.Rule.Type.String())
- checker := NewGenericChecker([]Rule{rule})
+ checker := &whereRequireForUpdateDeleteChecker{
+ level: level,
+ title: checkCtx.Rule.Type.String(),
+ }
for _, stmt := range checkCtx.ParsedStatements {
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
+ node, ok := snowsqlparser.GetOmniNode(stmt.AST)
if !ok {
continue
}
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
- }
-
- return checker.GetAdviceList(), nil
-}
-
-// WhereRequireForUpdateDeleteRule checks for WHERE clause requirement in UPDATE and DELETE statements.
-type WhereRequireForUpdateDeleteRule struct {
- BaseRule
-}
-
-// NewWhereRequireForUpdateDeleteRule creates a new WhereRequireForUpdateDeleteRule.
-func NewWhereRequireForUpdateDeleteRule(level storepb.Advice_Status, title string) *WhereRequireForUpdateDeleteRule {
- return &WhereRequireForUpdateDeleteRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
+ checker.checkStmt(node, stmt.Text, stmt.BaseLine())
}
-}
-
-// Name returns the rule name.
-func (*WhereRequireForUpdateDeleteRule) Name() string {
- return "WhereRequireForUpdateDeleteRule"
-}
-// OnEnter is called when entering a parse tree node.
-func (r *WhereRequireForUpdateDeleteRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeUpdateStatement:
- r.enterUpdateStatement(ctx.(*parser.Update_statementContext))
- case NodeTypeDeleteStatement:
- r.enterDeleteStatement(ctx.(*parser.Delete_statementContext))
- default:
- // Ignore other node types
- }
- return nil
+ return checker.adviceList, nil
}
-// OnExit is called when exiting a parse tree node.
-func (*WhereRequireForUpdateDeleteRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- // This rule doesn't need exit processing
- return nil
+type whereRequireForUpdateDeleteChecker struct {
+ adviceList []*storepb.Advice
+ level storepb.Advice_Status
+ title string
}
-func (r *WhereRequireForUpdateDeleteRule) enterUpdateStatement(ctx *parser.Update_statementContext) {
- if ctx.WHERE() == nil {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.StatementNoWhere.Int32(),
- Title: r.title,
- Content: "WHERE clause is required for UPDATE statement.",
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
- })
- }
+// checkStmt flags UPDATE and DELETE statements without a WHERE clause,
+// mirroring the legacy listener that fired on Update_statement and
+// Delete_statement contexts. MERGE WHEN ... THEN UPDATE/DELETE actions are
+// dedicated merge nodes in both ASTs and are not flagged.
+func (c *whereRequireForUpdateDeleteChecker) checkStmt(node omniast.Node, text string, baseLine int) {
+ omniast.Inspect(node, func(n omniast.Node) bool {
+ switch stmt := n.(type) {
+ case *omniast.UpdateStmt:
+ if stmt.Where == nil {
+ c.addAdvice("UPDATE", text, baseLine, stmt.Loc.Start)
+ }
+ case *omniast.DeleteStmt:
+ if stmt.Where == nil {
+ c.addAdvice("DELETE", text, baseLine, stmt.Loc.Start)
+ }
+ default:
+ // Ignore other node types
+ }
+ return true
+ })
}
-func (r *WhereRequireForUpdateDeleteRule) enterDeleteStatement(ctx *parser.Delete_statementContext) {
- if ctx.WHERE() == nil {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.StatementNoWhere.Int32(),
- Title: r.title,
- Content: "WHERE clause is required for DELETE statement.",
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
- })
- }
+func (c *whereRequireForUpdateDeleteChecker) addAdvice(statementType, text string, baseLine, offset int) {
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: c.level,
+ Code: code.StatementNoWhere.Int32(),
+ Title: c.title,
+ Content: fmt.Sprintf("WHERE clause is required for %s statement.", statementType),
+ StartPosition: common.ConvertANTLRLineToPosition(baseLine + statementLineForOffset(text, offset)),
+ })
}
diff --git a/backend/plugin/advisor/snowflake/test/schema_backward_compatibility.yaml b/backend/plugin/advisor/snowflake/test/schema_backward_compatibility.yaml
index 351f43e7fe..3762cf4671 100644
--- a/backend/plugin/advisor/snowflake/test/schema_backward_compatibility.yaml
+++ b/backend/plugin/advisor/snowflake/test/schema_backward_compatibility.yaml
@@ -59,3 +59,12 @@
CREATE TABLE T(ID INT);
DROP TABLE T;
changeType: 1
+# DIVERGENCE LOCK (omni migration): legacy fired CompatibilityDropTable here only
+# because its ANTLR listener never subscribed to the Create_table_like context —
+# an accidental gap, not intent. omni folds CREATE TABLE ... LIKE into
+# CreateTableStmt, so a LIKE-created table dropped in the SAME batch is treated
+# like any other in-batch creation (no advice), matching the rule's intent and
+# the behavior both versions already had for plain CREATE + DROP.
+- statement: CREATE TABLE SCRATCH LIKE SOURCE_TABLE;DROP TABLE SCRATCH;
+ changeType: 1
+ want: null
diff --git a/backend/plugin/advisor/snowflake/test/statement_select_no_select_all.yaml b/backend/plugin/advisor/snowflake/test/statement_select_no_select_all.yaml
index d8fe22afba..6992fe800f 100644
--- a/backend/plugin/advisor/snowflake/test/statement_select_no_select_all.yaml
+++ b/backend/plugin/advisor/snowflake/test/statement_select_no_select_all.yaml
@@ -33,3 +33,16 @@
line: 1
column: 0
endposition: null
+# Lock: CTE bodies ARE visited (omni walker post-#294 covers SelectStmt.With);
+# a SELECT * inside a CTE body flags even when the outer SELECT is star-free.
+- statement: WITH c AS (SELECT * FROM t) SELECT a FROM c;
+ changeType: 1
+ want:
+ - status: 2
+ code: 203
+ title: STATEMENT_SELECT_NO_SELECT_ALL
+ content: Avoid using SELECT *.
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
diff --git a/backend/plugin/advisor/snowflake/test/statement_where_require_select.yaml b/backend/plugin/advisor/snowflake/test/statement_where_require_select.yaml
index 7067ea6aed..9a93e83d17 100644
--- a/backend/plugin/advisor/snowflake/test/statement_where_require_select.yaml
+++ b/backend/plugin/advisor/snowflake/test/statement_where_require_select.yaml
@@ -68,3 +68,16 @@
endposition: null
- statement: SELECT 1;
changeType: 1
+# Lock: CTE bodies ARE visited (omni walker post-#294 covers SelectStmt.With);
+# the inner WHERE-less CTE SELECT flags even when the outer SELECT has WHERE.
+- statement: WITH c AS (SELECT x FROM t) SELECT a FROM c WHERE id = 1;
+ changeType: 1
+ want:
+ - status: 2
+ code: 202
+ title: STATEMENT_WHERE_REQUIRE_SELECT
+ content: WHERE clause is required for SELECT statement.
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
diff --git a/backend/plugin/db/snowflake/query.go b/backend/plugin/db/snowflake/query.go
index 173c10fe18..2d32289c93 100644
--- a/backend/plugin/db/snowflake/query.go
+++ b/backend/plugin/db/snowflake/query.go
@@ -4,17 +4,19 @@ import (
"fmt"
"log/slog"
"strconv"
+ "strings"
- "github.com/antlr4-go/antlr/v4"
+ omniast "github.com/bytebase/omni/snowflake/ast"
+ omniparser "github.com/bytebase/omni/snowflake/parser"
"github.com/pkg/errors"
- snowsql "github.com/bytebase/parser/snowflake"
-
"github.com/bytebase/bytebase/backend/common/log"
"github.com/bytebase/bytebase/backend/plugin/db/util"
- snowparser "github.com/bytebase/bytebase/backend/plugin/parser/snowflake"
+ "github.com/bytebase/bytebase/backend/utils"
)
+const stmtErrFmt = "statement: %s"
+
func getStatementWithResultLimit(statement string, limit int) string {
stmt, err := getStatementWithResultLimitInline(statement, limit)
if err != nil {
@@ -24,132 +26,124 @@ func getStatementWithResultLimit(statement string, limit int) string {
return stmt
}
+// getStatementWithResultLimitInline rewrites the single statement so that its
+// outermost query returns at most limitCount rows. The rewrite splices the
+// replacement into the original statement text via omni AST byte offsets
+// (ast.Loc), preserving the original formatting. The output shape mirrors the
+// legacy ANTLR TokenStreamRewriter: the statement trimmed of trailing
+// spaces/semicolons, plus a final "\n;".
+//
+// Mirroring the legacy rewriter, only the outermost query's right-most select
+// is touched:
+// - LIMIT <n> → n becomes min(n, limitCount) (n == 0 → limitCount)
+// - OFFSET/FETCH <n> → the fetch count is replaced the same way
+// - TOP <n> → replaced the same way
+// - no limiting clause → " LIMIT <limitCount>" is appended at the end of
+// that select (inside any wrapping parentheses)
+//
+// Non-query statements are returned unchanged (the legacy listener never
+// fired on them). Any error makes the caller fall back to wrapping the
+// statement in SELECT * FROM (...) LIMIT n.
func getStatementWithResultLimitInline(singleStatement string, limitCount int) (string, error) {
- results, err := snowparser.ParseSnowSQL(singleStatement)
+ trimmed := strings.TrimRightFunc(singleStatement, utils.IsSpaceOrSemicolon)
+
+ file, err := omniparser.Parse(trimmed)
if err != nil {
- return "", err
+ return "", errors.Wrapf(err, stmtErrFmt, singleStatement)
}
-
- if len(results) != 1 {
- return "", errors.Errorf("expected exactly 1 statement, got %d", len(results))
+ if len(file.Stmts) != 1 {
+ return "", errors.Errorf("expected exactly 1 statement, got %d", len(file.Stmts))
}
- result := results[0]
-
- listener := &snowsqlRewriter{
- limitCount: limitCount,
- outerMostQuery: true,
+ sel := findRightMostSelect(file.Stmts[0])
+ if sel == nil {
+ // Not a query statement; return the normalized text unchanged.
+ return trimmed + "\n;", nil
}
- listener.rewriter = *antlr.NewTokenStreamRewriter(result.Tokens)
- antlr.ParseTreeWalkerDefault.Walk(listener, result.Tree)
- if listener.err != nil {
- return "", errors.Wrapf(listener.err, "statement: %s", singleStatement)
+ // LIMIT <n> [OFFSET <m>]: replace n.
+ if sel.Limit != nil {
+ spliced, err := spliceLimitNumber(trimmed, omniast.NodeLoc(sel.Limit), limitCount)
+ if err != nil {
+ return "", errors.Wrapf(err, stmtErrFmt, singleStatement)
+ }
+ return spliced + "\n;", nil
}
- return listener.rewriter.GetTextDefault(), nil
-}
-
-type snowsqlRewriter struct {
- *snowsql.BaseSnowflakeParserListener
-
- rewriter antlr.TokenStreamRewriter
- err error
- outerMostQuery bool
- limitCount int
-}
-
-func (r *snowsqlRewriter) EnterQuery_statement(ctx *snowsql.Query_statementContext) {
- if !r.outerMostQuery {
- return
+ // [OFFSET <m>] FETCH [FIRST|NEXT] <n> ...: replace the fetch count n.
+ if sel.Fetch != nil {
+ spliced, err := spliceLimitNumber(trimmed, omniast.NodeLoc(sel.Fetch.Count), limitCount)
+ if err != nil {
+ return "", errors.Wrapf(err, stmtErrFmt, singleStatement)
+ }
+ return spliced + "\n;", nil
}
- r.outerMostQuery = false
- // if has set_operators, use last select_statement of set_operator.
- var selectCtx *snowsql.Select_statementContext
- if len(ctx.AllSet_operators()) > 0 {
- setOperator := ctx.Set_operators(len(ctx.AllSet_operators()) - 1)
- selectCtx = findRightMostSelectStatement(setOperator.Select_statement_in_parentheses())
- }
- // otherwise, ues outermost direct select_statement.
- if selectCtx == nil {
- selectCtx = findRightMostSelectStatement(ctx.Select_statement_in_parentheses())
- }
- if selectCtx == nil {
- return
+ // A bare OFFSET without LIMIT/FETCH cannot take an appended LIMIT clause
+ // (LIMIT must precede OFFSET); the legacy parser rejected this shape, so
+ // keep routing it to the caller's fallback.
+ if sel.Offset != nil {
+ return "", errors.Errorf("statement has OFFSET without LIMIT/FETCH: %s", singleStatement)
}
- limitClause := selectCtx.Limit_clause()
- if limitClause != nil {
- if limitClause.LIMIT() != nil {
- if len(limitClause.AllNum()) > 0 {
- firstNumber := limitClause.Num(0)
- userLimitText := firstNumber.GetText()
- limit, _ := strconv.Atoi(userLimitText)
- if limit == 0 || r.limitCount < limit {
- limit = r.limitCount
- }
- r.rewriter.ReplaceDefault(firstNumber.GetStart().GetTokenIndex(), firstNumber.GetStop().GetTokenIndex(), fmt.Sprintf("%d", limit))
- }
- } else {
- var num snowsql.INumContext
- if limitClause.OFFSET() != nil {
- if len(limitClause.AllNum()) > 1 {
- num = limitClause.Num(1)
- }
- } else {
- if len(limitClause.AllNum()) > 0 {
- num = limitClause.Num(0)
- }
- }
- if num != nil {
- userLimitText := num.GetText()
- limit, _ := strconv.Atoi(userLimitText)
- if limit == 0 || r.limitCount < limit {
- limit = r.limitCount
- }
- r.rewriter.ReplaceDefault(num.GetStart().GetTokenIndex(), num.GetStop().GetTokenIndex(), fmt.Sprintf("%d", limit))
- }
+ // TOP <n>: replace n.
+ if sel.Top != nil {
+ spliced, err := spliceLimitNumber(trimmed, omniast.NodeLoc(sel.Top), limitCount)
+ if err != nil {
+ return "", errors.Wrapf(err, stmtErrFmt, singleStatement)
}
- return
+ return spliced + "\n;", nil
}
- selectTopClause := selectCtx.Select_top_clause()
- if selectTopClause != nil && selectTopClause.Select_list_top() != nil && selectTopClause.Select_list_top().Top_clause() != nil {
- topClause := selectTopClause.Select_list_top().Top_clause()
- number := topClause.Num()
- userLimitText := number.GetText()
- limit, _ := strconv.Atoi(userLimitText)
- if limit == 0 || r.limitCount < limit {
- limit = r.limitCount
- }
- r.rewriter.ReplaceDefault(number.GetStart().GetTokenIndex(), number.GetStop().GetTokenIndex(), fmt.Sprintf("%d", limit))
- return
+ // No limiting clause: append after the end of the select.
+ end := sel.Loc.End
+ if !sel.Loc.IsValid() || end > len(trimmed) {
+ return "", errors.Errorf("invalid select location %v for statement: %s", sel.Loc, singleStatement)
}
-
- // Append after select_optional_clauses.
- r.rewriter.InsertAfterDefault(selectCtx.Select_optional_clauses().GetStop().GetTokenIndex(), fmt.Sprintf(" LIMIT %d", r.limitCount))
+ return trimmed[:end] + fmt.Sprintf(" LIMIT %d", limitCount) + trimmed[end:] + "\n;", nil
}
-func findRightMostSelectStatement(ctx snowsql.ISelect_statement_in_parenthesesContext) *snowsql.Select_statementContext {
- if ctx == nil {
- return nil
+// spliceLimitNumber replaces the number at loc within text by
+// min(userLimit, limitCount), where a user limit of 0 means "no limit" and
+// becomes limitCount — the exact arithmetic of the legacy rewriter. A
+// non-integer at loc is an error (the legacy grammar only accepted integer
+// tokens there; anything else fell back).
+func spliceLimitNumber(text string, loc omniast.Loc, limitCount int) (string, error) {
+ if !loc.IsValid() || loc.End > len(text) || loc.Start > loc.End {
+ return "", errors.Errorf("invalid limit number location %v", loc)
}
-
- if setOperator := ctx.Set_operators(); setOperator != nil {
- if selectCtx := findRightMostSelectStatement(setOperator.Select_statement_in_parentheses()); selectCtx != nil {
- return selectCtx
- }
+ userLimit, err := strconv.Atoi(text[loc.Start:loc.End])
+ if err != nil {
+ return "", errors.Wrapf(err, "non-integer limit count %q", text[loc.Start:loc.End])
}
- if nested := ctx.Select_statement_in_parentheses(); nested != nil {
- if selectCtx := findRightMostSelectStatement(nested); selectCtx != nil {
- return selectCtx
- }
+ limit := userLimit
+ if limit == 0 || limitCount < limit {
+ limit = limitCount
}
- if selectStatement := ctx.Select_statement(); selectStatement != nil {
- if selectCtx, ok := selectStatement.(*snowsql.Select_statementContext); ok {
- return selectCtx
+ return text[:loc.Start] + strconv.Itoa(limit) + text[loc.End:], nil
+}
+
+// findRightMostSelect returns the right-most SELECT of the outermost query:
+// for set operations the last operand's select (recursing through nested set
+// operations and parenthesized selects, which omni flattens), for the
+// result-pipe operator (->>) the consuming query's select. Returns nil for
+// non-query statements.
+func findRightMostSelect(node omniast.Node) *omniast.SelectStmt {
+ switch n := node.(type) {
+ case *omniast.SelectStmt:
+ return n
+ case *omniast.SetOperationStmt:
+ return findRightMostSelect(n.Right)
+ case *omniast.ResultScanStmt:
+ return findRightMostSelect(n.Query)
+ case *omniast.ShowStmt:
+ // SHOW ... ->> <query>: omni stores the piped statement on ShowStmt.Pipe;
+ // the row limit applies to that trailing query.
+ if n.Pipe != nil {
+ return findRightMostSelect(n.Pipe)
}
+ return nil
+ default:
+ return nil
}
- return nil
}
diff --git a/backend/plugin/db/snowflake/test-data/test_limit.yaml b/backend/plugin/db/snowflake/test-data/test_limit.yaml
index 950cbc6383..ffe5f16e76 100644
--- a/backend/plugin/db/snowflake/test-data/test_limit.yaml
+++ b/backend/plugin/db/snowflake/test-data/test_limit.yaml
@@ -149,3 +149,8 @@
;
# antlr parser does not support ILIKE, EXCLUDE, RENAME
+- stmt: SHOW TABLES ->> SELECT * FROM $1;
+ limit: 100
+ want: |-
+ SHOW TABLES ->> SELECT * FROM $1 LIMIT 100
+ ;
diff --git a/backend/plugin/parser/snowflake/diagnose.go b/backend/plugin/parser/snowflake/diagnose.go
index cfbc1a38ff..682c083c48 100644
--- a/backend/plugin/parser/snowflake/diagnose.go
+++ b/backend/plugin/parser/snowflake/diagnose.go
@@ -2,11 +2,8 @@ package snowflake
import (
"context"
- "strings"
- "unicode"
- "github.com/antlr4-go/antlr/v4"
- parser "github.com/bytebase/parser/snowflake"
+ "github.com/bytebase/omni/snowflake/diagnostics"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
@@ -16,53 +13,51 @@ func init() {
base.RegisterDiagnoseFunc(storepb.Engine_SNOWFLAKE, Diagnose)
}
+// Diagnose returns syntax diagnostics for the given Snowflake statement. It
+// surfaces genuine lex/parse errors from the omni best-effort parser.
+//
+// The omni Snowflake parser emits a "<NAME> statement parsing is not yet
+// supported" / "unknown or unsupported statement starting with <token>" stub
+// error from a handful of dispatch fallthroughs. Unlike Trino (where DESCRIBE
+// INPUT / DESCRIBE OUTPUT are valid statements that still hit a stub), every
+// such Snowflake stub fires ONLY on genuinely malformed input: an unrecognized
+// CREATE/ALTER/DROP object keyword, or an unknown leading keyword. All valid
+// object forms (CREATE TABLE/VIEW/WAREHOUSE/SEQUENCE/STREAM/TASK/FUNCTION/...,
+// ALTER WAREHOUSE/DATABASE/USER/..., DROP <obj>/UNDROP <obj>) parse cleanly, so
+// these stubs represent true syntax errors and are intentionally passed through
+// (no suppression filter is needed here). When omni grows coverage for a new
+// object the stub simply stops firing for it.
func Diagnose(_ context.Context, _ base.DiagnoseContext, statement string) ([]base.Diagnostic, error) {
- diagnostics := make([]base.Diagnostic, 0)
- syntaxError := parseSnowflakeStatement(statement)
- if syntaxError != nil {
- diagnostics = append(diagnostics, base.ConvertSyntaxErrorToDiagnostic(syntaxError, statement))
+ diags := diagnostics.Analyze(statement)
+ out := make([]base.Diagnostic, 0, len(diags))
+ // Re-derive line:column from the byte offset so the column is rune-based
+ // (the position that base.ConvertSyntaxErrorToDiagnostic expects). omni's
+ // own Range.Column is byte-based, so we deliberately use Offset instead to
+ // keep multibyte input positions correct — mirroring the Trino diagnose.go.
+ mapper := base.NewByteOffsetPositionMapper(statement)
+ for _, d := range diags {
+ syntaxErr := &base.SyntaxError{
+ Position: mapper.Position(d.Range.Start.Offset),
+ Message: d.Message,
+ }
+ out = append(out, base.ConvertSyntaxErrorToDiagnostic(syntaxErr, statement))
}
-
- return diagnostics, nil
+ return out, nil
}
-func parseSnowflakeStatement(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.NewSnowflakeLexer(inputStream)
- stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
-
- p := parser.NewSnowflakeParser(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,
+// parseSnowflakeSQL parses statement with the omni best-effort parser and
+// returns the first syntax error as a *base.SyntaxError (nil when the input is
+// syntactically valid or empty). It mirrors the Trino parseTrinoSQL helper and
+// preserves the "first syntax error" shape the legacy implementation exposed.
+func parseSnowflakeSQL(statement string) *base.SyntaxError {
+ diags := diagnostics.Analyze(statement)
+ if len(diags) == 0 {
+ return nil
}
- p.RemoveErrorListeners()
- p.AddErrorListener(parserErrorListener)
-
- p.BuildParseTrees = false
-
- _ = p.Snowflake_file()
-
- if lexerErrorListener.Err != nil {
- return lexerErrorListener.Err
- }
-
- if parserErrorListener.Err != nil {
- return parserErrorListener.Err
+ d := diags[0]
+ mapper := base.NewByteOffsetPositionMapper(statement)
+ return &base.SyntaxError{
+ Position: mapper.Position(d.Range.Start.Offset),
+ Message: d.Message,
}
- return nil
}
diff --git a/backend/plugin/parser/snowflake/diagnose_test.go b/backend/plugin/parser/snowflake/diagnose_test.go
new file mode 100644
index 0000000000..f0c37a8d55
--- /dev/null
+++ b/backend/plugin/parser/snowflake/diagnose_test.go
@@ -0,0 +1,143 @@
+package snowflake
+
+import (
+ "context"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+
+ "github.com/bytebase/bytebase/backend/plugin/parser/base"
+)
+
+func TestDiagnose(t *testing.T) {
+ tests := []struct {
+ name string
+ sql string
+ expectErr bool
+ }{
+ {
+ name: "Valid SELECT statement",
+ sql: "SELECT * FROM users",
+ expectErr: false,
+ },
+ {
+ name: "Valid CREATE TABLE statement",
+ sql: "CREATE TABLE users (id INT, name VARCHAR)",
+ expectErr: false,
+ },
+ {
+ name: "Valid CREATE WAREHOUSE statement",
+ sql: "CREATE WAREHOUSE my_wh WITH WAREHOUSE_SIZE = 'XSMALL'",
+ expectErr: false,
+ },
+ {
+ name: "Missing select list",
+ sql: "SELECT FROM users",
+ expectErr: true,
+ },
+ {
+ // Unrecognized CREATE object keyword (TABLEE) — omni reports a
+ // "CREATE statement parsing is not yet supported" stub here, which is
+ // still a genuine syntax error (the object keyword is invalid).
+ name: "Invalid CREATE object",
+ sql: "CREATE TABLEE users (id INT)",
+ expectErr: true,
+ },
+ {
+ name: "Truncated statement (end of input)",
+ sql: "SELECT * FROM t WHERE",
+ expectErr: true,
+ },
+ {
+ name: "Unknown leading keyword",
+ sql: "FOO BAR BAZ",
+ expectErr: true,
+ },
+ {
+ // Resolved divergence: omni's strict parse (post-#303) now rejects
+ // unconsumed trailing tokens, matching the legacy ANTLR behavior —
+ // the stray "FFROM" token produces a syntax diagnostic.
+ name: "Trailing garbage token after valid prefix",
+ sql: "SELECT * FFROM users",
+ expectErr: true,
+ },
+ {
+ // DIVERGENCE (omni vs legacy ANTLR): empty / whitespace-only input is
+ // zero statements with no error. The legacy path appended a ';' and
+ // reported a syntax error on the lone ';'; omni correctly treats an
+ // empty buffer as not-an-error.
+ name: "Empty statement",
+ sql: "",
+ expectErr: false,
+ },
+ {
+ name: "Whitespace-only statement",
+ sql: " \n\t ",
+ expectErr: false,
+ },
+ {
+ // DIVERGENCE (omni vs legacy ANTLR): a lone or doubled ';' is an empty
+ // statement (zero statements), not a syntax error, under omni.
+ name: "Lone semicolons",
+ sql: ";;",
+ expectErr: false,
+ },
+ {
+ name: "Valid multi-statement script",
+ sql: "SELECT 1; SELECT 2;",
+ expectErr: false,
+ },
+ }
+
+ ctx := context.Background()
+ diagnoseCtx := base.DiagnoseContext{}
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ diagnostics, err := Diagnose(ctx, diagnoseCtx, tt.sql)
+ require.NoError(t, err, "Diagnose should not return an error")
+
+ if tt.expectErr {
+ assert.NotEmpty(t, diagnostics, "Expected diagnostics for invalid SQL")
+ } else {
+ assert.Empty(t, diagnostics, "Expected no diagnostics for valid SQL")
+ }
+ })
+ }
+}
+
+func TestParseSnowflakeSQL(t *testing.T) {
+ tests := []struct {
+ name string
+ sql string
+ expectErr bool
+ }{
+ {
+ name: "Valid statement with semicolon",
+ sql: "SELECT * FROM users;",
+ expectErr: false,
+ },
+ {
+ name: "Valid statement without semicolon",
+ sql: "SELECT * FROM users",
+ expectErr: false,
+ },
+ {
+ name: "Invalid statement (missing select list)",
+ sql: "SELECT FROM users",
+ expectErr: true,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ err := parseSnowflakeSQL(tt.sql)
+ if tt.expectErr {
+ assert.NotNil(t, err, "Expected syntax error")
+ } else {
+ assert.Nil(t, err, "Expected no syntax error")
+ }
+ })
+ }
+}
diff --git a/backend/plugin/parser/snowflake/omni_ast.go b/backend/plugin/parser/snowflake/omni_ast.go
new file mode 100644
index 0000000000..4bdd3d042e
--- /dev/null
+++ b/backend/plugin/parser/snowflake/omni_ast.go
@@ -0,0 +1,88 @@
+package snowflake
+
+import (
+ "errors"
+ "fmt"
+
+ omniast "github.com/bytebase/omni/snowflake/ast"
+ omniparser "github.com/bytebase/omni/snowflake/parser"
+
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/plugin/parser/base"
+)
+
+var _ base.AST = (*OmniAST)(nil)
+
+// OmniAST wraps an omni AST node and implements the base.AST interface.
+// It mirrors redshift's OmniAST.
+type OmniAST struct {
+ // Node is the omni AST node for this statement (e.g. *ast.SelectStmt).
+ // It is always non-nil for a successfully parsed non-empty statement.
+ Node omniast.Node
+ // Text is the original SQL text of this statement.
+ Text string
+ // StartPosition is the 1-based position where this statement starts.
+ StartPosition *storepb.Position
+}
+
+// ASTStartPosition implements base.AST.
+func (a *OmniAST) ASTStartPosition() *storepb.Position {
+ return a.StartPosition
+}
+
+// GetOmniNode extracts the omni AST node from a base.AST interface.
+// It returns false when a is not a snowflake OmniAST (or carries no node).
+func GetOmniNode(a base.AST) (omniast.Node, bool) {
+ if a == nil {
+ return nil, false
+ }
+ omniAST, ok := a.(*OmniAST)
+ if !ok || omniAST.Node == nil {
+ return nil, false
+ }
+ return omniAST.Node, true
+}
+
+// parseOmniStatementNode parses ONE statement's text with the omni parser and
+// returns its single top-level statement node. Unlike the dual-AST transition
+// (where omni was best-effort behind the legacy gatekeeper), a parse failure
+// is now a hard error: the omni parser is the only parser, so a statement it
+// rejects must fail the batch the way a legacy ANTLR syntax error did.
+func parseOmniStatementNode(text string) (omniast.Node, error) {
+ file, err := parseSnowflakeAST(text)
+ if err != nil {
+ return nil, err
+ }
+ if file == nil || len(file.Stmts) == 0 {
+ return nil, errors.New("statement yielded no parse tree")
+ }
+ return file.Stmts[0], nil
+}
+
+// convertOmniParseError converts an omni *parser.ParseError into the bare
+// *base.SyntaxError shape the legacy ANTLR error listener produced (callers
+// such as the sheet manager type-assert on *base.SyntaxError to surface
+// syntax-error advices). The error position is computed inside the statement
+// text (rune-based columns) and offset by the statement's start position in
+// the original multi-statement input — mirroring redshift's convertOmniError.
+func convertOmniParseError(err error, stmt base.Statement) error {
+ var parseErr *omniparser.ParseError
+ if !errors.As(err, &parseErr) {
+ return err
+ }
+
+ mapper := base.NewByteOffsetPositionMapper(stmt.Text)
+ position := mapper.Position(parseErr.Loc.Start)
+ if stmt.Start != nil {
+ localLine := position.Line
+ position.Line += stmt.Start.Line - 1
+ if localLine == 1 && stmt.Start.Column > 0 {
+ position.Column += stmt.Start.Column - 1
+ }
+ }
+ return &base.SyntaxError{
+ Position: position,
+ Message: fmt.Sprintf("Syntax error at line %d:%d \n%s", position.Line, position.Column, parseErr.Msg),
+ RawMessage: parseErr.Msg,
+ }
+}
diff --git a/backend/plugin/parser/snowflake/omni_ast_test.go b/backend/plugin/parser/snowflake/omni_ast_test.go
new file mode 100644
index 0000000000..5efc27363b
--- /dev/null
+++ b/backend/plugin/parser/snowflake/omni_ast_test.go
@@ -0,0 +1,80 @@
+package snowflake
+
+import (
+ "testing"
+
+ omniast "github.com/bytebase/omni/snowflake/ast"
+ "github.com/stretchr/testify/require"
+
+ "github.com/bytebase/bytebase/backend/plugin/parser/base"
+)
+
+// TestParseSnowflakeStatementsOmniAST locks the omni-only contract: every
+// non-empty statement carries an OmniAST wrapping a non-nil omni node
+// (reachable via GetOmniNode); the legacy ANTLR tree is gone, so
+// base.GetANTLRAST reports false.
+func TestParseSnowflakeStatementsOmniAST(t *testing.T) {
+ parsed, err := parseSnowflakeStatements("CREATE TABLE t1(id INT);\nSELECT id FROM t1;")
+ require.NoError(t, err)
+ require.Len(t, parsed, 2)
+
+ for i, ps := range parsed {
+ omniAST, ok := ps.AST.(*OmniAST)
+ require.True(t, ok, "stmt %d: AST must be *OmniAST", i)
+ require.Equal(t, ps.Text, omniAST.Text, "stmt %d", i)
+
+ // The omni node must be reachable through GetOmniNode.
+ node, ok := GetOmniNode(ps.AST)
+ require.True(t, ok, "stmt %d: GetOmniNode must work", i)
+ require.NotNil(t, node, "stmt %d", i)
+
+ // No legacy ANTLR tree rides along anymore.
+ _, ok = base.GetANTLRAST(ps.AST)
+ require.False(t, ok, "stmt %d: GetANTLRAST must report false", i)
+
+ // ASTStartPosition keeps the legacy shape (line-based, BaseLine()+1),
+ // so position reporting is unchanged by the cutover.
+ require.EqualValues(t, ps.BaseLine()+1, ps.AST.ASTStartPosition().GetLine(), "stmt %d", i)
+ }
+
+ node0, _ := GetOmniNode(parsed[0].AST)
+ require.IsType(t, (*omniast.CreateTableStmt)(nil), node0)
+ node1, _ := GetOmniNode(parsed[1].AST)
+ require.IsType(t, (*omniast.SelectStmt)(nil), node1)
+}
+
+// TestParseSnowflakeStatementsEmptyAndError locks the surrounding behavior:
+// empty/comment-only inputs produce no statements (omni-based SplitSQL drops
+// empty segments), and an omni parse failure fails the whole batch exactly as
+// the legacy ANTLR parser did.
+func TestParseSnowflakeStatementsEmptyAndError(t *testing.T) {
+ // Comments and bare semicolons yield no statements and no error.
+ parsed, err := parseSnowflakeStatements(";; -- only a comment\n;")
+ require.NoError(t, err)
+ require.Empty(t, parsed)
+
+ // Comment-only prefix does not shift the statement pairing.
+ parsed, err = parseSnowflakeStatements("-- header\nSELECT 1;;SELECT 2;")
+ require.NoError(t, err)
+ require.Len(t, parsed, 2)
+ for i, ps := range parsed {
+ _, ok := GetOmniNode(ps.AST)
+ require.True(t, ok, "stmt %d", i)
+ }
+
+ // Parse failure keeps the legacy error behavior: the whole batch fails.
+ _, err = parseSnowflakeStatements("SELECT 1;\nSELECT * FROM;")
+ require.Error(t, err)
+}
+
+// TestGetOmniNodeEdgeCases covers the accessor's non-OmniAST inputs.
+func TestGetOmniNodeEdgeCases(t *testing.T) {
+ node, ok := GetOmniNode(nil)
+ require.False(t, ok)
+ require.Nil(t, node)
+
+ // A plain legacy ANTLRAST is not an OmniAST.
+ node, ok = GetOmniNode(&base.ANTLRAST{})
+ require.False(t, ok)
+ require.Nil(t, node)
+}
diff --git a/backend/plugin/parser/snowflake/query.go b/backend/plugin/parser/snowflake/query.go
index 55b9b782b2..1843fc760f 100644
--- a/backend/plugin/parser/snowflake/query.go
+++ b/backend/plugin/parser/snowflake/query.go
@@ -1,8 +1,10 @@
package snowflake
import (
- "github.com/antlr4-go/antlr/v4"
- parser "github.com/bytebase/parser/snowflake"
+ "strings"
+
+ "github.com/bytebase/omni/snowflake/ast"
+ "github.com/bytebase/omni/snowflake/parser"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
@@ -12,53 +14,98 @@ func init() {
base.RegisterQueryValidator(storepb.Engine_SNOWFLAKE, validateQuery)
}
-// validateQuery validates the SQL statement for SQL editor.
+// validateQuery validates the SQL statement for the SQL editor, which only
+// permits read-only queries.
+//
+// It returns (allReadOnly, allReturnData, error):
+// - allReadOnly: every statement can run in read-only mode;
+// - allReturnData: every statement returns data;
+// - error: a syntax error if a statement is invalid.
+//
+// This mirrors the legacy ANTLR queryValidateListener exactly. The read-only
+// set is {SELECT, SHOW, DESCRIBE, SET, EXPLAIN}; a SET statement is read-only
+// but does NOT return data (legacy set hasExecute=true → returnsData=false).
+// Every other statement (USE, INSERT/UPDATE/DELETE/MERGE, CALL, COPY, COMMENT,
+// TRUNCATE, GRANT/REVOKE, CREATE/ALTER/DROP, ...) is not read-only.
+//
+// EXPLAIN is special-cased because omni's parser does not yet support it
+// (parser.Parse returns an error). The legacy listener accepted any
+// other_command.explain as read-only and data-returning without inspecting the
+// inner statement; we preserve that via a lexical EXPLAIN check.
func validateQuery(statement string) (bool, bool, error) {
- parseResults, err := ParseSnowSQL(statement)
+ // Split into top-level statements with the omni splitter so EXPLAIN (which
+ // omni cannot parse) can be classified per-segment before parsing.
+ stmts, err := SplitSQL(statement)
if err != nil {
return false, false, err
}
- l := &queryValidateListener{
- valid: true,
- }
- for _, parseResult := range parseResults {
- antlr.ParseTreeWalkerDefault.Walk(l, parseResult.Tree)
- if !l.valid {
+
+ returnsData := true
+ for _, stmt := range stmts {
+ if stmt.Empty || strings.TrimSpace(stmt.Text) == "" {
+ continue
+ }
+
+ file, perr := parser.Parse(stmt.Text)
+ if perr != nil {
+ return false, false, perr
+ }
+ if file == nil || len(file.Stmts) == 0 {
+ // A segment that parses to no statement (e.g. comment-only) is neither
+ // read-only nor a write; treat it as a no-op like the legacy walker did.
+ continue
+ }
+
+ readOnly, isSet := classifyForEditor(getQueryType(file.Stmts[0]), file.Stmts[0])
+ if !readOnly {
return false, false, nil
}
+ if isSet {
+ returnsData = false
+ }
}
- return true, !l.hasExecute, nil
-}
-type queryValidateListener struct {
- *parser.BaseSnowflakeParserListener
-
- valid bool
- hasExecute bool
+ return true, returnsData, nil
}
-func (l *queryValidateListener) EnterSql_command(ctx *parser.Sql_commandContext) {
- if !l.valid {
- return
+// classifyForEditor maps a statement's base.QueryType (plus its node, to single
+// out SET) onto the SQL-editor read-only decision, mirroring the legacy
+// queryValidateListener.EnterSql_command.
+//
+// Returns (readOnly, isSet):
+// - readOnly: the statement may run in the read-only SQL editor;
+// - isSet: the statement is a SET (read-only but not data-returning).
+//
+// The read-only set is SELECT (base.Select for SELECT/set-op/USE/SET) and
+// SHOW/DESCRIBE (base.SelectInfoSchema). However getQueryType maps USE to
+// base.Select while the legacy editor listener REJECTED USE (use_command was
+// not in its accepted dml/other/describe/show set), so USE is excluded here.
+func classifyForEditor(qt base.QueryType, node ast.Node) (bool, bool) {
+ if sh, ok := node.(*ast.ShowStmt); ok && sh.Pipe != nil {
+ // SHOW ... ->> <stmt>: SHOW itself is read-only metadata; the verdict is
+ // the piped statement's (a non-read-only pipe must not classify as a query).
+ return classifyForEditor(getQueryType(sh.Pipe), sh.Pipe)
}
- if ctx.Dml_command() == nil && ctx.Other_command() == nil && ctx.Describe_command() == nil && ctx.Show_command() == nil {
- l.valid = false
- return
+ if rs, ok := node.(*ast.ResultScanStmt); ok {
+ // stmt ->> query is read-only only if BOTH the source statement and the
+ // trailing query are; it returns the trailing query's data.
+ srcOK, _ := classifyForEditor(getQueryType(rs.Source), rs.Source)
+ queryOK, querySet := classifyForEditor(getQueryType(rs.Query), rs.Query)
+ return srcOK && queryOK, querySet
}
- if dml := ctx.Dml_command(); dml != nil {
- if dml.Query_statement() == nil {
- l.valid = false
- return
- }
+ if _, ok := node.(*ast.UseStmt); ok {
+ // Legacy validateQuery rejected USE even though getQueryType classifies it
+ // read-only; preserve the stricter editor behavior.
+ return false, false
}
- if other := ctx.Other_command(); other != nil {
- if other.Set() != nil {
- l.hasExecute = true
- return
- }
- if other.Explain() == nil {
- l.valid = false
- return
- }
+ if _, ok := node.(*ast.SetStmt); ok {
+ // SET is read-only but does not return data.
+ return true, true
+ }
+ switch qt {
+ case base.Select, base.SelectInfoSchema, base.Explain:
+ return true, false
+ default:
+ return false, false
}
}
diff --git a/backend/plugin/parser/snowflake/query_span_extractor.go b/backend/plugin/parser/snowflake/query_span_extractor.go
index 81900d90e1..adbc4d1ae7 100644
--- a/backend/plugin/parser/snowflake/query_span_extractor.go
+++ b/backend/plugin/parser/snowflake/query_span_extractor.go
@@ -2,21 +2,32 @@ package snowflake
import (
"context"
+ "regexp"
"strconv"
+ "strings"
+ "unicode/utf8"
- "github.com/antlr4-go/antlr/v4"
- parser "github.com/bytebase/parser/snowflake"
+ "github.com/bytebase/omni/snowflake/ast"
"github.com/pkg/errors"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
)
+// querySpanExtractor walks an omni Snowflake AST and produces a base.QuerySpan
+// describing the column-level lineage of a single SELECT/set-operation query.
+//
+// It is the omni-AST port of the legacy ANTLR listener-based extractor: the
+// bytebase-side machinery (base.PseudoTable / base.PhysicalTable / getField /
+// findTableSchema, which resolve a column reference back to its physical source
+// column via the database metadata getter) is preserved unchanged; only the
+// tree-walking was rewritten from ANTLR parse-tree contexts onto omni's
+// hand-written ast.Node type-switches.
type querySpanExtractor struct {
ctx context.Context
defaultDatbase string
defaultSchema string
- // https://docs.com/en/sql-reference/identifiers-syntax
+ // https://docs.snowflake.com/en/sql-reference/identifiers-syntax
ignoreCaseSensitive bool
gCtx base.GetQuerySpanContext
@@ -27,6 +38,17 @@ type querySpanExtractor struct {
// tableSourcesFrom is used to record the table sources from the query.
tableSourcesFrom []base.TableSource
+
+ // statementText is the single statement being extracted; expression result
+ // names slice it by node Loc (the legacy extractor used ctx.GetText()).
+ statementText string
+
+ // joinMemberSources records the individual relations inside JOIN trees of
+ // the current FROM scope. tableSourcesFrom holds one (unnamed, merged)
+ // pseudo-table per FROM item, so qualified references like T2.B or T2.*
+ // inside a join could not resolve by table name; lookups fall back to this
+ // list. Scoped/truncated together with tableSourcesFrom.
+ joinMemberSources []base.TableSource
}
func newQuerySpanExtractor(defaultDatabase, defaultSchema string, gCtx base.GetQuerySpanContext, ignoreCaseSensitive bool) *querySpanExtractor {
@@ -45,23 +67,22 @@ func newQuerySpanExtractor(defaultDatabase, defaultSchema string, gCtx base.GetQ
func (q *querySpanExtractor) getQuerySpan(ctx context.Context, statement string) (*base.QuerySpan, error) {
q.ctx = ctx
+ q.statementText = statement
- parseResults, err := ParseSnowSQL(statement)
+ file, err := parseSnowflakeAST(statement)
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
- if tree == nil {
+ if file == nil || len(file.Stmts) == 0 {
return nil, nil
}
+ if len(file.Stmts) != 1 {
+ return nil, errors.Errorf("expected exactly 1 statement, got %d", len(file.Stmts))
+ }
- accessTables := getAccessTables(q.defaultDatbase, q.defaultSchema, tree)
+ node := file.Stmts[0]
+
+ accessTables := getAccessTables(q.defaultDatbase, q.defaultSchema, node)
// We do not support simultaneous access to the system table and the user table
// because we do not synchronize the schema of the system table.
// This causes an error (NOT_FOUND) when using querySpanExtractor.findTableSchema.
@@ -70,31 +91,39 @@ func (q *querySpanExtractor) getQuerySpan(ctx context.Context, statement string)
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
+
+ queryType := getQueryType(node)
+ // A SELECT/set-op that touches only system tables is reclassified as
+ // SelectInfoSchema, matching the legacy queryTypeListener.allSystems branch.
+ if queryType == base.Select && allSystems {
+ queryType = base.SelectInfoSchema
}
- if queryTypeListener.result != base.Select {
+
+ if queryType != base.Select {
return &base.QuerySpan{
- Type: queryTypeListener.result,
+ Type: queryType,
SourceColumns: base.SourceColumnSet{},
Results: []base.QuerySpanResult{},
}, nil
}
- // We assumes the caller had handled the statement type case,
- // so we only need to handle the determined statement type here.
- // In order to decrease the maintenance cost, we use listener
- // to handlet the select statement precisely.
- listener := &selectOnlyListener{
- q: q,
+ // USE and SET are session commands that getQueryType classifies as
+ // base.Select (legacy queryTypeListener parity), but they are not query
+ // expressions and produce no result columns. The legacy selectOnlyListener
+ // simply never fired for them, so the legacy extractor returned a span with
+ // empty results and no error; mirror that here instead of falling into
+ // extractPseudoTableFromQueryNode's "unsupported query node type" error.
+ switch node.(type) {
+ case *ast.UseStmt, *ast.SetStmt:
+ return &base.QuerySpan{
+ Type: queryType,
+ SourceColumns: accessTables,
+ Results: []base.QuerySpanResult{},
+ }, nil
+ default:
}
- antlr.ParseTreeWalkerDefault.Walk(listener, tree)
- err = listener.err
+
+ result, err := q.extractPseudoTableFromQueryNode(node)
if err != nil {
var resourceNotFound *base.ResourceNotFoundError
if errors.As(err, &resourceNotFound) {
@@ -104,139 +133,194 @@ func (q *querySpanExtractor) getQuerySpan(ctx context.Context, statement string)
NotFoundError: resourceNotFound,
}, nil
}
-
return nil, err
}
return &base.QuerySpan{
- Type: queryTypeListener.result,
+ Type: queryType,
SourceColumns: accessTables,
- Results: listener.result,
+ Results: result.GetQuerySpanResult(),
}, nil
}
-type selectOnlyListener struct {
- *parser.BaseSnowflakeParserListener
-
- q *querySpanExtractor
- result []base.QuerySpanResult
- err error
-}
-
-func (l *selectOnlyListener) EnterDml_command(ctx *parser.Dml_commandContext) {
- if l.err != nil {
- return
- }
-
- if ctx.Query_statement() == nil {
- return
- }
-
- parent := ctx.GetParent()
- if parent == nil {
- return
- }
-
- if _, ok := parent.(*parser.Sql_commandContext); !ok {
- return
- }
-
- result, err := l.q.extractPseudoTableFromQueryStatement(ctx.Query_statement())
- if err != nil {
- l.err = err
- l.result = make([]base.QuerySpanResult, 0)
- return
+// extractPseudoTableFromQueryNode extracts the result columns of a query
+// expression node: a *ast.SelectStmt (optionally carrying a WITH clause) or a
+// *ast.SetOperationStmt.
+func (q *querySpanExtractor) extractPseudoTableFromQueryNode(node ast.Node) (*base.PseudoTable, error) {
+ switch n := node.(type) {
+ case *ast.SelectStmt:
+ // A WITH clause on this SELECT introduces CTEs visible to the SELECT body
+ // and to later CTEs; push them, then pop on the way out so sibling scopes
+ // don't see them. The legacy extractor handled WITH at the
+ // query_statement level; omni hangs WITH off the SelectStmt itself.
+ if len(n.With) > 0 {
+ originalCTECount := len(q.ctes)
+ if err := q.extractCTEFromWith(n.With, isRecursiveWith(n.With)); err != nil {
+ return nil, err
+ }
+ defer func() {
+ q.ctes = q.ctes[:originalCTECount]
+ }()
+ }
+ return q.extractPseudoTableFromSelectStmt(n)
+ case *ast.SetOperationStmt:
+ // A WITH clause preceding a set operation is attached by omni to the
+ // LEFTMOST SelectStmt, but its CTEs are visible to EVERY operand
+ // (legacy handled WITH at the query_statement level). Hoist them over
+ // the whole set-op extraction; the leftmost SelectStmt pushes them
+ // again during its own extraction, which is harmless shadowing.
+ if leftmost := leftmostSelect(n); leftmost != nil && len(leftmost.With) > 0 {
+ originalCTECount := len(q.ctes)
+ if err := q.extractCTEFromWith(leftmost.With, isRecursiveWith(leftmost.With)); err != nil {
+ return nil, err
+ }
+ defer func() {
+ q.ctes = q.ctes[:originalCTECount]
+ }()
+ }
+ return q.extractPseudoTableFromSetOperation(n)
+ case *ast.ResultScanStmt:
+ // FAIL CLOSED: a result-pipe (->>) query reads the previous statement's
+ // result set, which has no resolvable schema here; resolving the trailing
+ // SELECT against $1 would produce wrong lineage. Same posture as PIVOT.
+ return nil, errors.New("result-pipe (->>) statements are not supported for query span extraction yet")
+ case *ast.ShowStmt:
+ // Only reachable for SHOW ... ->> <query> (a plain SHOW classifies
+ // SelectInfoSchema and returns before extraction). Same fail-closed
+ // posture as ResultScanStmt: $1's schema is not resolvable.
+ return nil, errors.New("result-pipe (->>) statements are not supported for query span extraction yet")
+ default:
+ return nil, errors.Errorf("unsupported query node type %T", node)
}
- l.result = result.GetQuerySpanResult()
}
-func (q *querySpanExtractor) extractPseudoTableFromQueryStatement(ctx parser.IQuery_statementContext) (*base.PseudoTable, error) {
- if err := q.extractCTEFromWithExpression(ctx.With_expression()); err != nil {
- return nil, err
- }
-
- selectStatementInParentheses := ctx.Select_statement_in_parentheses()
- result, err := q.extractPseudoTableFromSelectStatementInParentheses(selectStatementInParentheses)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to extract sensitive fields of the query statement near line %d", selectStatementInParentheses.GetStart().GetLine())
+// leftmostSelect descends a set-operation chain's left spine to the SelectStmt
+// that textually leads the statement (where omni attaches a leading WITH).
+func leftmostSelect(node ast.Node) *ast.SelectStmt {
+ switch n := node.(type) {
+ case *ast.SelectStmt:
+ return n
+ case *ast.SetOperationStmt:
+ return leftmostSelect(n.Left)
+ default:
+ return nil
}
+}
- allSetOperators := ctx.AllSet_operators()
- for i, setOperator := range allSetOperators {
- // For UNION operator, the number of the columns in the result set is the same, and will use the left part's column name.
- // So we only need to extract the sensitive fields of the right part.
- right, err := q.extractPseudoTableFromSetOperator(setOperator)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to extract the %d set operator near line %d", i+1, setOperator.GetStart().GetLine())
- }
- if err := mergeSetOperatorColumns(result, right, selectStatementInParentheses.GetStart().GetLine(), i+1, setOperator.GetStart().GetLine()); err != nil {
- return nil, err
+// isRecursiveWith reports whether any CTE in the WITH list carries the RECURSIVE
+// flag. Snowflake applies RECURSIVE to the whole WITH list, so a single
+// recursive CTE makes the list recursive (mirroring the legacy
+// withExpression.RECURSIVE() check).
+func isRecursiveWith(ctes []*ast.CTE) bool {
+ for _, cte := range ctes {
+ if cte.Recursive {
+ return true
}
}
- return result, nil
+ return false
}
-func (q *querySpanExtractor) extractCTEFromWithExpression(withExpression parser.IWith_expressionContext) error {
- if withExpression == nil {
- return nil
- }
-
- for _, commonTableExpression := range withExpression.AllCommon_table_expression() {
- normalizedCTEName := NormalizeSnowSQLObjectNamePart(commonTableExpression.Id_())
- isRecursiveCTE := withExpression.RECURSIVE() != nil || hasSetOperatorInSelectStatementInParentheses(commonTableExpression.Select_statement_in_parentheses())
+func (q *querySpanExtractor) extractCTEFromWith(ctes []*ast.CTE, recursiveWith bool) error {
+ for _, cte := range ctes {
+ normalizedCTEName := normalizeSnowflakeIdentifier(cte.Name)
+ isRecursiveCTE := recursiveWith || cte.Recursive || queryNodeHasSetOperation(cte.Query)
+ // UNION [ALL] BY NAME merges columns by name, which the recursive
+ // fixed-point merge below does not model (it merges positionally and
+ // would attribute A's lineage to B for swapped columns). Route BY NAME
+ // set-op bodies through the plain extraction path, which handles BY
+ // NAME correctly; a genuinely self-referencing BY NAME CTE then fails
+ // resolution explicitly instead of producing wrong lineage. (Legacy
+ // ANTLR could not parse BY NAME at all, so there is no legacy behavior
+ // to preserve here.)
+ if isRecursiveCTE && setOpChainHasByName(cte.Query) {
+ isRecursiveCTE = false
+ }
if isRecursiveCTE {
- if err := q.extractRecursiveCTE(normalizedCTEName, commonTableExpression); err != nil {
+ if err := q.extractRecursiveCTE(normalizedCTEName, cte); err != nil {
return err
}
continue
}
- pseudoTable, err := q.extractPseudoTableFromSelectStatementInParentheses(commonTableExpression.Select_statement_in_parentheses())
+ pseudoTable, err := q.extractPseudoTableFromQueryNode(cte.Query)
if err != nil {
- return errors.Wrapf(err, "failed to extract sensitive fields of the CTE %q near line %d", normalizedCTEName, commonTableExpression.GetStart().GetLine())
+ return errors.Wrapf(err, "failed to extract sensitive fields of the CTE %q", normalizedCTEName)
}
- pseudoTable, err = applyCTEColumnList(normalizedCTEName, pseudoTable, commonTableExpression.Column_list())
+ pseudoTable, err = applyCTEColumnList(normalizedCTEName, pseudoTable, cte.Columns)
if err != nil {
- return errors.Wrapf(err, "failed to extract sensitive fields of the CTE %q near line %d", normalizedCTEName, commonTableExpression.GetStart().GetLine())
+ return errors.Wrapf(err, "failed to extract sensitive fields of the CTE %q", normalizedCTEName)
}
q.ctes = append(q.ctes, pseudoTable)
}
return nil
}
-func hasSetOperatorInSelectStatementInParentheses(ctx parser.ISelect_statement_in_parenthesesContext) bool {
- if ctx == nil {
+// queryNodeHasSetOperation reports whether a CTE body is (or wraps) a
+// set-operation, which Snowflake treats as recursive-capable. omni nests
+// set-ops as *ast.SetOperationStmt; a leading WITH-bearing SELECT whose body is
+// a set-op is not representable here (omni hangs WITH on the SelectStmt), so a
+// plain check of the top node suffices.
+func queryNodeHasSetOperation(node ast.Node) bool {
+ _, ok := node.(*ast.SetOperationStmt)
+ return ok
+}
+
+// setOpChainHasByName reports whether any set operation in a (possibly nested)
+// set-operation chain uses the BY NAME column-matching mode.
+func setOpChainHasByName(node ast.Node) bool {
+ setOp, ok := node.(*ast.SetOperationStmt)
+ if !ok {
return false
}
- if ctx.Set_operators() != nil {
+ if setOp.ByName {
return true
}
- return hasSetOperatorInSelectStatementInParentheses(ctx.Select_statement_in_parentheses())
+ return setOpChainHasByName(setOp.Left) || setOpChainHasByName(setOp.Right)
}
-func (q *querySpanExtractor) extractRecursiveCTE(cteName string, commonTableExpression parser.ICommon_table_expressionContext) error {
- anchorTableSource, err := q.extractAnchorPseudoTableFromSelectStatementInParentheses(commonTableExpression.Select_statement_in_parentheses())
+func (q *querySpanExtractor) extractRecursiveCTE(cteName string, cte *ast.CTE) error {
+ setOp, ok := cte.Query.(*ast.SetOperationStmt)
+ if !ok {
+ // A recursive CTE whose body is not a set-operation has no recursive term
+ // to iterate; treat it as a plain CTE (extract its single SELECT body).
+ pseudoTable, err := q.extractPseudoTableFromQueryNode(cte.Query)
+ if err != nil {
+ return errors.Wrapf(err, "failed to extract sensitive fields of recursive CTE %q", cteName)
+ }
+ pseudoTable, err = applyCTEColumnList(cteName, pseudoTable, cte.Columns)
+ if err != nil {
+ return err
+ }
+ q.ctes = append(q.ctes, pseudoTable)
+ return nil
+ }
+
+ // Anchor term: the left branch of the set-operation. It must be resolvable
+ // without the CTE itself being visible yet.
+ anchorTableSource, err := q.extractPseudoTableFromQueryNode(setOp.Left)
if err != nil {
- return errors.Wrapf(err, "failed to extract sensitive fields of the anchor clause of recursive CTE %q near line %d", cteName, commonTableExpression.GetStart().GetLine())
+ return errors.Wrapf(err, "failed to extract sensitive fields of the anchor clause of recursive CTE %q", cteName)
}
- anchorTableSource, err = applyCTEColumnList(cteName, anchorTableSource, commonTableExpression.Column_list())
+ anchorTableSource, err = applyCTEColumnList(cteName, anchorTableSource, cte.Columns)
if err != nil {
- return errors.Wrapf(err, "failed to extract sensitive fields of the anchor clause of recursive CTE %q near line %d", cteName, commonTableExpression.GetStart().GetLine())
+ return errors.Wrapf(err, "failed to extract sensitive fields of the anchor clause of recursive CTE %q", cteName)
}
q.ctes = append(q.ctes, anchorTableSource)
+ // Iterate the recursive term (the right branch) until the lineage of the CTE
+ // columns reaches a fixed point, mirroring the legacy loop.
for {
- recursivePartTableSource, err := q.extractPseudoTableFromSelectStatementInParentheses(commonTableExpression.Select_statement_in_parentheses())
+ recursivePartTableSource, err := q.extractPseudoTableFromQueryNode(setOp.Right)
if err != nil {
- return errors.Wrapf(err, "failed to extract sensitive fields of the recursive clause of recursive CTE %q near line %d", cteName, commonTableExpression.GetStart().GetLine())
+ return errors.Wrapf(err, "failed to extract sensitive fields of the recursive clause of recursive CTE %q", cteName)
}
- recursivePartTableSource, err = applyCTEColumnList(cteName, recursivePartTableSource, commonTableExpression.Column_list())
+ recursivePartTableSource, err = applyCTEColumnList(cteName, recursivePartTableSource, cte.Columns)
if err != nil {
- return errors.Wrapf(err, "failed to extract sensitive fields of the recursive clause of recursive CTE %q near line %d", cteName, commonTableExpression.GetStart().GetLine())
+ return errors.Wrapf(err, "failed to extract sensitive fields of the recursive clause of recursive CTE %q", cteName)
}
currentCTE := q.ctes[len(q.ctes)-1]
- mergedColumns, changed, err := mergeQuerySpanResults(currentCTE.GetQuerySpanResult(), recursivePartTableSource.GetQuerySpanResult(), cteName, commonTableExpression.GetStart().GetLine())
+ mergedColumns, changed, err := mergeQuerySpanResults(currentCTE.GetQuerySpanResult(), recursivePartTableSource.GetQuerySpanResult(), cteName)
if err != nil {
return err
}
@@ -251,33 +335,7 @@ func (q *querySpanExtractor) extractRecursiveCTE(cteName string, commonTableExpr
return nil
}
-func (q *querySpanExtractor) extractAnchorPseudoTableFromSelectStatementInParentheses(ctx parser.ISelect_statement_in_parenthesesContext) (*base.PseudoTable, error) {
- if ctx == nil {
- return nil, errors.Errorf("empty select statement in parentheses")
- }
- if nested := ctx.Select_statement_in_parentheses(); nested != nil {
- return q.extractAnchorPseudoTableFromSelectStatementInParentheses(nested)
- }
- if selectStatement := ctx.Select_statement(); selectStatement != nil {
- return q.extractPseudoTableFromSelectStatement(selectStatement)
- }
- if withExpression := ctx.With_expression(); withExpression != nil {
- originalCTECount := len(q.ctes)
- if err := q.extractCTEFromWithExpression(withExpression); err != nil {
- return nil, err
- }
- defer func() {
- q.ctes = q.ctes[:originalCTECount]
- }()
- if setOperator := ctx.Set_operators(); setOperator != nil {
- return q.extractPseudoTableFromSetOperator(setOperator)
- }
- return nil, errors.Errorf("failed to extract anchor statement near line %d", ctx.GetStart().GetLine())
- }
- return nil, errors.Errorf("failed to extract anchor statement near line %d", ctx.GetStart().GetLine())
-}
-
-func applyCTEColumnList(cteName string, pseudoTable *base.PseudoTable, columnList parser.IColumn_listContext) (*base.PseudoTable, error) {
+func applyCTEColumnList(cteName string, pseudoTable *base.PseudoTable, columns []ast.Ident) (*base.PseudoTable, error) {
if pseudoTable == nil {
pseudoTable = &base.PseudoTable{
Name: cteName,
@@ -291,21 +349,21 @@ func applyCTEColumnList(cteName string, pseudoTable *base.PseudoTable, columnLis
}
copy(result.Columns, pseudoTable.GetQuerySpanResult())
- if columnList == nil {
+ if len(columns) == 0 {
return result, nil
}
- if len(columnList.AllColumn_name()) != len(result.GetQuerySpanResult()) {
- return nil, errors.Errorf("the number of columns in the CTE %q near line %d returns %d fields, but the column list returns %d fields", cteName, columnList.GetStart().GetLine(), len(result.GetQuerySpanResult()), len(columnList.AllColumn_name()))
+ if len(columns) != len(result.GetQuerySpanResult()) {
+ return nil, errors.Errorf("the number of columns in the CTE %q returns %d fields, but the column list returns %d fields", cteName, len(result.GetQuerySpanResult()), len(columns))
}
- for i, columnName := range columnList.AllColumn_name() {
- result.Columns[i].Name = normalizeSnowSQLColumnName(columnName)
+ for i, columnName := range columns {
+ result.Columns[i].Name = normalizeSnowflakeIdentifier(columnName)
}
return result, nil
}
-func mergeQuerySpanResults(currentColumns, newColumns []base.QuerySpanResult, cteName string, line int) ([]base.QuerySpanResult, bool, error) {
+func mergeQuerySpanResults(currentColumns, newColumns []base.QuerySpanResult, cteName string) ([]base.QuerySpanResult, bool, error) {
if len(currentColumns) != len(newColumns) {
- return nil, false, errors.Errorf("recursive clause returns %d fields, but anchor clause returns %d fields in recursive CTE %q near line %d", len(newColumns), len(currentColumns), cteName, line)
+ return nil, false, errors.Errorf("recursive clause returns %d fields, but anchor clause returns %d fields in recursive CTE %q", len(newColumns), len(currentColumns), cteName)
}
mergedColumns := make([]base.QuerySpanResult, len(currentColumns))
@@ -319,86 +377,102 @@ func mergeQuerySpanResults(currentColumns, newColumns []base.QuerySpanResult, ct
return mergedColumns, changed, nil
}
-func mergeSetOperatorColumns(left, right *base.PseudoTable, leftLine, setOperatorIndex, rightLine int) error {
- if left == nil || right == nil {
- return nil
+// extractPseudoTableFromSetOperation extracts result columns from a
+// set-operation query (UNION/INTERSECT/EXCEPT). omni nests chained set-ops
+// left-associatively, so the left branch may itself be a *ast.SetOperationStmt.
+// The left branch's column names are kept; each right branch is merged into the
+// left's columns — positionally for plain set operators (the executor requires
+// matching column counts), or by column name for UNION [ALL] BY NAME.
+func (q *querySpanExtractor) extractPseudoTableFromSetOperation(setOp *ast.SetOperationStmt) (*base.PseudoTable, error) {
+ left, err := q.extractPseudoTableFromQueryNode(setOp.Left)
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to extract the left part of the set operation")
}
- leftColumns := left.GetQuerySpanResult()
- rightColumns := right.GetQuerySpanResult()
- if len(leftColumns) != len(rightColumns) {
- return errors.Errorf("the number of columns in the query statement nearly line %d returns %d fields, but %d set operator near line %d returns %d fields", leftLine, len(leftColumns), setOperatorIndex, rightLine, len(rightColumns))
+ right, err := q.extractPseudoTableFromQueryNode(setOp.Right)
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to extract the right part of the set operation")
}
- for i := range rightColumns {
- left.Columns[i].SourceColumns, _ = base.MergeSourceColumnSet(leftColumns[i].SourceColumns, rightColumns[i].SourceColumns)
+ if left == nil {
+ return right, nil
}
- return nil
-}
-
-func (q *querySpanExtractor) extractPseudoTableFromSetOperator(ctx parser.ISet_operatorsContext) (*base.PseudoTable, error) {
- return q.extractPseudoTableFromSelectStatementInParentheses(ctx.Select_statement_in_parentheses())
+ if right == nil {
+ return left, nil
+ }
+ if err := mergeSetOperatorColumns(left, right, setOp.ByName); err != nil {
+ return nil, err
+ }
+ return left, nil
}
-func (q *querySpanExtractor) extractPseudoTableFromSelectStatementInParentheses(ctx parser.ISelect_statement_in_parenthesesContext) (*base.PseudoTable, error) {
- if ctx == nil {
- return nil, nil
+func mergeSetOperatorColumns(left, right *base.PseudoTable, byName bool) error {
+ if left == nil || right == nil {
+ return nil
}
+ leftColumns := left.GetQuerySpanResult()
+ rightColumns := right.GetQuerySpanResult()
- var result *base.PseudoTable
- var err error
- if selectStatement := ctx.Select_statement(); selectStatement != nil {
- result, err = q.extractPseudoTableFromSelectStatement(selectStatement)
- if err != nil {
- return nil, err
- }
- } else if nested := ctx.Select_statement_in_parentheses(); nested != nil {
- result, err = q.extractPseudoTableFromSelectStatementInParentheses(nested)
- if err != nil {
- return nil, err
+ if byName {
+ // UNION [ALL] BY NAME (Snowflake-specific) aligns the branches by
+ // case-insensitive column name instead of by position, and the column
+ // counts may differ (columns missing on one side are NULL-filled by the
+ // engine). Mirror omni's own snowflake/analysis by-name merge: keep the
+ // left columns in order, merging in the lineage of the same-named right
+ // column, then append the right-only columns.
+ rightByName := make(map[string]base.QuerySpanResult, len(rightColumns))
+ for _, rightColumn := range rightColumns {
+ rightByName[strings.ToUpper(rightColumn.Name)] = rightColumn
+ }
+ seen := make(map[string]bool, len(leftColumns))
+ merged := make([]base.QuerySpanResult, 0, len(leftColumns))
+ for _, leftColumn := range leftColumns {
+ key := strings.ToUpper(leftColumn.Name)
+ seen[key] = true
+ if rightColumn, ok := rightByName[key]; ok {
+ leftColumn.SourceColumns, _ = base.MergeSourceColumnSet(leftColumn.SourceColumns, rightColumn.SourceColumns)
+ }
+ merged = append(merged, leftColumn)
}
- } else if withExpression := ctx.With_expression(); withExpression != nil {
- originalCTECount := len(q.ctes)
- if err := q.extractCTEFromWithExpression(withExpression); err != nil {
- return nil, err
+ for _, rightColumn := range rightColumns {
+ if !seen[strings.ToUpper(rightColumn.Name)] {
+ merged = append(merged, rightColumn)
+ }
}
- defer func() {
- q.ctes = q.ctes[:originalCTECount]
- }()
+ left.Columns = merged
+ return nil
}
- if setOperator := ctx.Set_operators(); setOperator != nil {
- right, err := q.extractPseudoTableFromSetOperator(setOperator)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to extract set operator near line %d", setOperator.GetStart().GetLine())
- }
- if result == nil {
- return right, nil
- }
- if err := mergeSetOperatorColumns(result, right, ctx.GetStart().GetLine(), 1, setOperator.GetStart().GetLine()); err != nil {
- return nil, err
- }
+ if len(leftColumns) != len(rightColumns) {
+ return errors.Errorf("the number of columns in the left part of the set operation returns %d fields, but the right part returns %d fields", len(leftColumns), len(rightColumns))
}
-
- if result == nil {
- return nil, errors.Errorf("failed to extract select statement near line %d", ctx.GetStart().GetLine())
+ for i := range rightColumns {
+ left.Columns[i].SourceColumns, _ = base.MergeSourceColumnSet(leftColumns[i].SourceColumns, rightColumns[i].SourceColumns)
}
- return result, nil
+ return nil
}
-func (q *querySpanExtractor) extractPseudoTableFromSelectStatement(ctx parser.ISelect_statementContext) (*base.PseudoTable, error) {
+func (q *querySpanExtractor) extractPseudoTableFromSelectStmt(ctx *ast.SelectStmt) (*base.PseudoTable, error) {
if ctx == nil {
return nil, nil
}
- if ctx.Select_optional_clauses() != nil && ctx.Select_optional_clauses().From_clause() != nil {
- tableSourcesFrom, err := q.extractTableSourceFromFromClause(ctx.Select_optional_clauses().From_clause())
- if err != nil {
- return nil, err
- }
+ if len(ctx.From) > 0 {
+ // Snapshot BEFORE extracting the FROM scope: extractTableSourceFromFrom
+ // appends JOIN member relations to q.joinMemberSources as it walks join
+ // trees, so a later snapshot would bake those into the restore point and
+ // leak them into sibling/outer scopes.
originalFromFieldsLength := len(q.tableSourcesFrom)
- q.tableSourcesFrom = append(q.tableSourcesFrom, tableSourcesFrom)
+ originalJoinMemberLength := len(q.joinMemberSources)
defer func() {
q.tableSourcesFrom = q.tableSourcesFrom[:originalFromFieldsLength]
+ q.joinMemberSources = q.joinMemberSources[:originalJoinMemberLength]
}()
+ tableSourceFrom, err := q.extractTableSourceFromFrom(ctx.From)
+ if err != nil {
+ return nil, err
+ }
+ if tableSourceFrom != nil {
+ q.tableSourcesFrom = append(q.tableSourcesFrom, tableSourceFrom)
+ }
}
result := &base.PseudoTable{
@@ -406,711 +480,461 @@ func (q *querySpanExtractor) extractPseudoTableFromSelectStatement(ctx parser.IS
Columns: make([]base.QuerySpanResult, 0),
}
- var selectList parser.ISelect_listContext
- if ctx.Select_clause() != nil {
- selectList = ctx.Select_clause().Select_list_no_top().Select_list()
- } else if ctx.Select_top_clause() != nil {
- selectList = ctx.Select_top_clause().Select_list_top().Select_list()
- }
- if selectList == nil {
- return result, nil
- }
- for _, iSelectListElem := range selectList.AllSelect_list_elem() {
- if columnElemStar := iSelectListElem.Column_elem_star(); columnElemStar != nil {
- normalizedDatabaseName, normalizedSchemaName, normalizedTableName := normalizedObjectNameOrAlias(columnElemStar.Object_name_or_alias())
+ for _, target := range ctx.Targets {
+ if target == nil {
+ continue
+ }
+ if target.Star {
+ // SELECT * or SELECT t.* [EXCLUDE (cols)].
+ var normalizedDatabaseName, normalizedSchemaName, normalizedTableName string
+ if star, ok := target.Expr.(*ast.StarExpr); ok && star != nil && star.Qualifier != nil {
+ normalizedDatabaseName, normalizedSchemaName, normalizedTableName = normalizeSnowflakeObjectName(star.Qualifier, "", "")
+ }
left, err := q.getAllFieldsOfTableInFromOrOuterCTE(normalizedDatabaseName, normalizedSchemaName, normalizedTableName)
if err != nil {
- return nil, errors.Wrapf(err, "failed to extract sensitive fields of the query statement near line %d", ctx.GetStart().GetLine())
- }
- left = filterExcludedColumns(left, iSelectListElem.Exclude_clause())
- result.Columns = append(result.Columns, left...)
- } else if columnElem := iSelectListElem.Column_elem(); columnElem != nil {
- var normalizedDatabaseName, normalizedSchemaName, normalizedTableName, normalizedColumnName string
- if v := columnElem.Object_name_or_alias(); v != nil {
- normalizedDatabaseName, normalizedSchemaName, normalizedTableName = normalizedObjectNameOrAlias(v)
- }
- if columnName := columnElem.Column_name(); columnName != nil {
- ids := columnName.AllId_()
- if normalizedTableName == "" && len(ids) == 2 {
- normalizedTableName = NormalizeSnowSQLObjectNamePart(ids[0])
- }
- normalizedColumnName = normalizeSnowSQLColumnName(columnName)
- querySpanResult, err := q.getField(normalizedDatabaseName, normalizedSchemaName, normalizedTableName, normalizedColumnName)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to check whether the column %q is sensitive near line %d", normalizedColumnName, columnName.GetStart().GetLine())
- }
- result.Columns = append(result.Columns, querySpanResult)
- } else if columnElem.DOLLAR() != nil {
- columnPosition, err := strconv.Atoi(columnElem.Column_position().Num().GetText())
- if err != nil {
- return nil, errors.Wrapf(err, "failed to parse column position %q to integer near line %d", columnElem.Column_position().Num().GetText(), columnElem.Column_position().Num().GetStart().GetLine())
- }
- if columnPosition < 1 {
- return nil, errors.Errorf("column position %d is invalid because it is less than 1 near line %d", columnPosition, columnElem.Column_position().Num().GetStart().GetLine())
- }
- left, err := q.getAllFieldsOfTableInFromOrOuterCTE(normalizedDatabaseName, normalizedSchemaName, normalizedTableName)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to extract sensitive fields of the query statement near line %d", ctx.GetStart().GetLine())
- }
- if columnPosition > len(left) {
- return nil, errors.Errorf("column position is invalid because want to try get the %d column near line %d, but FROM clause only returns %d columns for %q.%q.%q", columnPosition, columnElem.Column_position().Num().GetStart().GetLine(), len(left), normalizedDatabaseName, normalizedSchemaName, normalizedTableName)
- }
- result.Columns = append(result.Columns, left[columnPosition-1])
+ return nil, errors.Wrapf(err, "failed to extract sensitive fields of the query statement")
}
- if asAlias := iSelectListElem.As_alias(); asAlias != nil && len(result.Columns) > 0 {
- result.Columns[len(result.Columns)-1].Name = NormalizeSnowSQLObjectNamePart(asAlias.Alias().Id_())
- }
- } else if expressionElem := iSelectListElem.Expression_elem(); expressionElem != nil {
- if v := expressionElem.Expr(); v != nil {
- columnName, querySpanResult, err := q.extractQuerySpanResultResultFromExpr(v)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to check whether the expression %q is sensitive near line %d", v.GetText(), v.GetStart().GetLine())
- }
- result.Columns = append(result.Columns, base.QuerySpanResult{
- Name: columnName,
- SourceColumns: querySpanResult.SourceColumns,
- })
- } else if v := expressionElem.Predicate(); v != nil {
- columnName, querySpanResult, err := q.extractQuerySpanResultResultFromExpr(v)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to check whether the expression %q is sensitive near line %d", v.GetText(), v.GetStart().GetLine())
- }
- result.Columns = append(result.Columns, base.QuerySpanResult{
- Name: columnName,
- SourceColumns: querySpanResult.SourceColumns,
- })
+ left = filterIlikeColumns(left, target.Ilike)
+ left = filterExcludedColumns(left, target.Exclude)
+ left, err = q.applyStarReplaces(left, target.Replace)
+ if err != nil {
+ return nil, err
}
+ left = applyStarRenames(left, target.Rename)
+ result.Columns = append(result.Columns, left...)
+ continue
+ }
- if asAlias := iSelectListElem.As_alias(); asAlias != nil && len(result.Columns) > 0 {
- result.Columns[len(result.Columns)-1].Name = NormalizeSnowSQLObjectNamePart(asAlias.Alias().Id_())
- }
+ // Non-star target: a column reference resolves to a single source column;
+ // any other expression collects every column it references.
+ columnName, querySpanResult, err := q.extractQuerySpanResultFromTargetExpr(target.Expr)
+ if err != nil {
+ return nil, err
}
+ if !target.Alias.IsEmpty() {
+ columnName = normalizeSnowflakeIdentifier(target.Alias)
+ }
+ result.Columns = append(result.Columns, base.QuerySpanResult{
+ Name: columnName,
+ SourceColumns: querySpanResult.SourceColumns,
+ IsPlainField: querySpanResult.IsPlainField,
+ })
}
return result, nil
}
-func normalizedObjectNameOrAlias(ctx parser.IObject_name_or_aliasContext) (string, string, string) {
- if ctx == nil {
- return "", "", ""
- }
- if objectName := ctx.Object_name(); objectName != nil {
- return normalizedObjectName(objectName, "", "")
- }
- if alias := ctx.Alias(); alias != nil {
- return "", "", NormalizeSnowSQLObjectNamePart(alias.Id_())
- }
- return "", "", ""
-}
-
-func normalizeSnowSQLColumnName(columnName parser.IColumn_nameContext) string {
- if columnName == nil {
- return ""
- }
- ids := columnName.AllId_()
- if len(ids) == 0 {
- return ""
- }
- return NormalizeSnowSQLObjectNamePart(ids[len(ids)-1])
-}
-
-func filterExcludedColumns(columns []base.QuerySpanResult, excludeClause parser.IExclude_clauseContext) []base.QuerySpanResult {
- if excludeClause == nil {
- return columns
+// extractQuerySpanResultFromTargetExpr resolves one non-star SELECT-list
+// expression into its output name and source columns. A bare column reference
+// (the IsPlainField case) is resolved against the FROM scope back to its
+// physical source column; any compound expression instead unions the source
+// columns of every column reference inside it (IsPlainField=false).
+func (q *querySpanExtractor) extractQuerySpanResultFromTargetExpr(expr ast.Node) (string, base.QuerySpanResult, error) {
+ if colRef, ok := expr.(*ast.ColumnRef); ok {
+ normalizedDatabaseName, normalizedSchemaName, normalizedTableName, normalizedColumnName := normalizeColumnRef(colRef)
+ querySpanResult, err := q.getField(normalizedDatabaseName, normalizedSchemaName, normalizedTableName, normalizedColumnName)
+ if err != nil {
+ return "", base.QuerySpanResult{}, errors.Wrapf(err, "failed to check whether the column %q is sensitive", normalizedColumnName)
+ }
+ return querySpanResult.Name, querySpanResult, nil
}
- excludedColumns := make(map[string]bool)
- if columnName := excludeClause.Column_name(); columnName != nil {
- excludedColumns[normalizeSnowSQLColumnName(columnName)] = true
- }
- if columnList := excludeClause.Column_list_in_parentheses(); columnList != nil {
- if list := columnList.Column_list(); list != nil {
- for _, columnName := range list.AllColumn_name() {
- excludedColumns[normalizeSnowSQLColumnName(columnName)] = true
- }
+ // Positional column reference $N (optionally qualified, d.$1): the legacy
+ // extractor resolved it to the N-th column of the FROM scope (or of the
+ // qualified relation), erroring on out-of-range positions. Session
+ // variables ($var, Positional=false) fall through to the generic path like
+ // any other opaque expression.
+ if dollar, ok := expr.(*ast.DollarRef); ok && dollar.Positional {
+ querySpanResult, err := q.resolvePositionalRef(dollar)
+ if err != nil {
+ return "", base.QuerySpanResult{}, err
}
- }
- if len(excludedColumns) == 0 {
- return columns
+ return querySpanResult.Name, querySpanResult, nil
}
- var result []base.QuerySpanResult
- for _, column := range columns {
- if !excludedColumns[column.Name] {
- result = append(result, column)
- }
+ name := q.exprDisplayName(expr)
+ sourceColumns, err := q.collectSourceColumnsFromExpr(expr)
+ if err != nil {
+ return "", base.QuerySpanResult{}, err
}
- return result
+ return name, base.QuerySpanResult{
+ Name: name,
+ SourceColumns: sourceColumns,
+ IsPlainField: false,
+ }, nil
}
-// The closure of the IExprContext.
-func (q *querySpanExtractor) extractQuerySpanResultResultFromExpr(ctx antlr.RuleContext) (string, base.QuerySpanResult, error) {
- switch ctx := ctx.(type) {
- case *parser.ExprContext:
- if v := ctx.Primitive_expression(); v != nil {
- return q.extractQuerySpanResultResultFromExpr(v)
- }
- if v := ctx.Function_call(); v != nil {
- return q.extractQuerySpanResultResultFromExpr(v)
- }
-
- querySpanResult := base.QuerySpanResult{
- Name: ctx.GetText(),
- SourceColumns: make(base.SourceColumnSet),
- }
- for _, expr := range ctx.AllExpr() {
- _, maskingAttributes, err := q.extractQuerySpanResultResultFromExpr(expr)
- if err != nil {
- return "", base.QuerySpanResult{}, errors.Wrapf(err, "failed to check whether the expression %q is sensitive near line %d", expr.GetText(), expr.GetStart().GetLine())
- }
- querySpanResult.SourceColumns, _ = base.MergeSourceColumnSet(querySpanResult.SourceColumns, maskingAttributes.SourceColumns)
- }
- if v := ctx.Subquery(); v != nil {
- _, maskingAttributes, err := q.extractQuerySpanResultResultFromExpr(v)
- if err != nil {
- return "", base.QuerySpanResult{}, errors.Wrapf(err, "failed to check whether the expression %q is sensitive near line %d", v.GetText(), v.GetStart().GetLine())
- }
- querySpanResult.SourceColumns, _ = base.MergeSourceColumnSet(querySpanResult.SourceColumns, maskingAttributes.SourceColumns)
- }
-
- if v := ctx.Case_expression(); v != nil {
- return q.extractQuerySpanResultResultFromExpr(v)
- }
- if v := ctx.Iff_expr(); v != nil {
- return q.extractQuerySpanResultResultFromExpr(v)
- }
- if v := ctx.Bracket_expression(); v != nil {
- return q.extractQuerySpanResultResultFromExpr(v)
- }
- if v := ctx.Arr_literal(); v != nil {
- return q.extractQuerySpanResultResultFromExpr(v)
- }
- if v := ctx.Json_literal(); v != nil {
- return q.extractQuerySpanResultResultFromExpr(v)
- }
+// collectSourceColumnsFromExpr walks an arbitrary SELECT-list / predicate
+// expression and unions the source columns of every column reference it
+// contains. Column references are resolved against the current FROM scope;
+// subqueries nested in the expression contribute their own source columns. This
+// is the omni-AST replacement for the legacy extractQuerySpanResultResultFromExpr
+// type-switch over the dozens of ANTLR expression contexts.
+func (q *querySpanExtractor) collectSourceColumnsFromExpr(expr ast.Node) (base.SourceColumnSet, error) {
+ result := make(base.SourceColumnSet)
+ if expr == nil {
+ return result, nil
+ }
- if v := ctx.Try_cast_expr(); v != nil {
- return q.extractQuerySpanResultResultFromExpr(v)
- }
- if v := ctx.Object_name(); v != nil {
- return q.extractQuerySpanResultResultFromExpr(v)
- }
- if v := ctx.Trim_expression(); v != nil {
- return q.extractQuerySpanResultResultFromExpr(v)
- }
- if v := ctx.Expr_list(); v != nil {
- _, maskingAttributes, err := q.extractQuerySpanResultResultFromExpr(v)
- if err != nil {
- return "", base.QuerySpanResult{}, errors.Wrapf(err, "failed to check whether the expression %q is sensitive near line %d", v.GetText(), v.GetStart().GetLine())
- }
- querySpanResult.SourceColumns, _ = base.MergeSourceColumnSet(querySpanResult.SourceColumns, maskingAttributes.SourceColumns)
- }
- return ctx.GetText(), querySpanResult, nil
- case *parser.Full_column_nameContext:
- normalizedDatabaseName, normalizedSchemaName, normalizedTableName, normalizedColumnName := normalizedFullColumnName(ctx)
- querySpanResult, err := q.getField(normalizedDatabaseName, normalizedSchemaName, normalizedTableName, normalizedColumnName)
- if err != nil {
- return "", base.QuerySpanResult{}, errors.Wrapf(err, "failed to check whether the column %q is sensitive near line %d", normalizedColumnName, ctx.GetStart().GetLine())
- }
- return querySpanResult.Name, querySpanResult, nil
- case *parser.Object_nameContext:
- normalizedDatabaseName, normalizedSchemaName, normalizedTableName := normalizedObjectName(ctx, q.defaultDatbase, q.defaultSchema)
- fieldInfo, err := q.getField(normalizedDatabaseName, normalizedSchemaName, normalizedTableName, "")
- if err != nil {
- return "", base.QuerySpanResult{}, errors.Wrapf(err, "failed to check whether the object %q is sensitive near line %d", normalizedTableName, ctx.GetStart().GetLine())
- }
- return fieldInfo.Name, fieldInfo, nil
- case *parser.Trim_expressionContext:
- if v := ctx.Expr(); v != nil {
- _, maskingAttributes, err := q.extractQuerySpanResultResultFromExpr(v)
- if err != nil {
- return "", base.QuerySpanResult{}, errors.Wrapf(err, "failed to check whether the expression %q is sensitive near line %d", v.GetText(), v.GetStart().GetLine())
+ var walkErr error
+ // walk is the ast.Inspect callback. It is a named closure so the manual
+ // recursions below — for sub-trees that omni's GENERATED walker does not
+ // descend into because they are non-Node embedded structs — can re-enter
+ // the same traversal. Collecting into a set keeps the manual recursion
+ // idempotent: it stays correct (merely redundant) if the upstream walker
+ // later learns to visit these children itself.
+ var walk func(node ast.Node) bool
+ walk = func(node ast.Node) bool {
+ if node == nil || walkErr != nil {
+ return false
+ }
+ switch n := node.(type) {
+ case *ast.DollarRef:
+ // Positional $N inside a compound expression (e.g. TO_DATE($1),
+ // $1 + 1) contributes the N-th FROM-scope column's lineage. Session
+ // variables ($var) carry no column lineage.
+ if !n.Positional {
+ return true
}
- return ctx.GetText(), maskingAttributes, nil
- }
- return "", base.QuerySpanResult{}, errors.Errorf("never reach here")
- case *parser.Try_cast_exprContext:
- if v := ctx.Expr(); v != nil {
- _, maskingAttributes, err := q.extractQuerySpanResultResultFromExpr(v)
+ querySpanResult, err := q.resolvePositionalRef(n)
if err != nil {
- return "", base.QuerySpanResult{}, errors.Wrapf(err, "failed to check whether the expression %q is sensitive near line %d", v.GetText(), v.GetStart().GetLine())
+ walkErr = err
+ return false
}
- return ctx.GetText(), maskingAttributes, nil
- }
- return "", base.QuerySpanResult{}, errors.Errorf("never reach here")
- case *parser.Json_literalContext:
- querySpanResult := base.QuerySpanResult{
- Name: ctx.GetText(),
- SourceColumns: make(base.SourceColumnSet),
- }
- if v := ctx.AllKv_pair(); len(v) > 0 {
- for _, kvPair := range v {
- _, maskingAttributes, err := q.extractQuerySpanResultResultFromExpr(kvPair)
- if err != nil {
- return "", base.QuerySpanResult{}, errors.Wrapf(err, "failed to check whether the expression %q is sensitive near line %d", kvPair.GetText(), kvPair.GetStart().GetLine())
- }
- querySpanResult.SourceColumns, _ = base.MergeSourceColumnSet(querySpanResult.SourceColumns, maskingAttributes.SourceColumns)
- }
- }
- return ctx.GetText(), querySpanResult, nil
- case *parser.Kv_pairContext:
- if v := ctx.Value(); v != nil {
- _, maskingAttributes, err := q.extractQuerySpanResultResultFromExpr(v)
+ result, _ = base.MergeSourceColumnSet(result, querySpanResult.SourceColumns)
+ case *ast.ColumnRef:
+ normalizedDatabaseName, normalizedSchemaName, normalizedTableName, normalizedColumnName := normalizeColumnRef(n)
+ querySpanResult, err := q.getField(normalizedDatabaseName, normalizedSchemaName, normalizedTableName, normalizedColumnName)
if err != nil {
- return "", base.QuerySpanResult{}, errors.Wrapf(err, "failed to check whether the expression %q is sensitive near line %d", v.GetText(), v.GetStart().GetLine())
- }
- return ctx.GetText(), maskingAttributes, nil
- }
- return "", base.QuerySpanResult{}, errors.Errorf("never reach here")
- case *parser.Arr_literalContext:
- querySpanResult := base.QuerySpanResult{
- Name: ctx.GetText(),
- SourceColumns: make(base.SourceColumnSet),
- }
- if v := ctx.AllValue(); len(v) > 0 {
- for _, value := range v {
- _, maskingAttributes, err := q.extractQuerySpanResultResultFromExpr(value)
- if err != nil {
- return "", base.QuerySpanResult{}, errors.Wrapf(err, "failed to check whether the expression %q is sensitive near line %d", value.GetText(), value.GetStart().GetLine())
- }
- querySpanResult.SourceColumns, _ = base.MergeSourceColumnSet(querySpanResult.SourceColumns, maskingAttributes.SourceColumns)
+ walkErr = errors.Wrapf(err, "failed to check whether the column %q is sensitive", normalizedColumnName)
+ return false
}
- }
- return ctx.GetText(), querySpanResult, nil
- case *parser.ValueContext:
- return q.extractQuerySpanResultResultFromExpr(ctx.Expr())
- case *parser.Bracket_expressionContext:
- querySpanResult := base.QuerySpanResult{
- Name: ctx.GetText(),
- SourceColumns: make(base.SourceColumnSet),
- }
- if v := ctx.Expr_list(); v != nil {
- _, maskingAttributes, err := q.extractQuerySpanResultResultFromExpr(v)
+ result, _ = base.MergeSourceColumnSet(result, querySpanResult.SourceColumns)
+ return false
+ case *ast.SubqueryExpr:
+ sub, err := q.extractPseudoTableFromQueryNode(n.Query)
if err != nil {
- return "", base.QuerySpanResult{}, errors.Wrapf(err, "failed to check whether the expression %q is sensitive near line %d", v.GetText(), v.GetStart().GetLine())
+ walkErr = err
+ return false
}
- querySpanResult.SourceColumns, _ = base.MergeSourceColumnSet(querySpanResult.SourceColumns, maskingAttributes.SourceColumns)
- return ctx.GetText(), querySpanResult, nil
- }
- if v := ctx.Subquery(); v != nil {
- _, maskingAttributes, err := q.extractQuerySpanResultResultFromExpr(v)
- if err != nil {
- return "", base.QuerySpanResult{}, errors.Wrapf(err, "failed to check whether the expression %q is sensitive near line %d", v.GetText(), v.GetStart().GetLine())
+ for _, field := range sub.GetQuerySpanResult() {
+ result, _ = base.MergeSourceColumnSet(result, field.SourceColumns)
}
- querySpanResult.SourceColumns, _ = base.MergeSourceColumnSet(querySpanResult.SourceColumns, maskingAttributes.SourceColumns)
- return ctx.GetText(), querySpanResult, nil
- }
- return "", base.QuerySpanResult{}, errors.Errorf("never reach here")
- case *parser.Iff_exprContext:
- querySpanResult := base.QuerySpanResult{
- Name: ctx.GetText(),
- SourceColumns: make(base.SourceColumnSet),
- }
- if v := ctx.Search_condition(); v != nil {
- _, maskingAttributes, err := q.extractQuerySpanResultResultFromExpr(ctx.Search_condition())
+ return false
+ case *ast.ExistsExpr:
+ sub, err := q.extractPseudoTableFromQueryNode(n.Query)
if err != nil {
- return "", base.QuerySpanResult{}, errors.Wrapf(err, "failed to check whether the expression %q is sensitive near line %d", v.GetText(), v.GetStart().GetLine())
+ walkErr = err
+ return false
}
- querySpanResult.SourceColumns, _ = base.MergeSourceColumnSet(querySpanResult.SourceColumns, maskingAttributes.SourceColumns)
- for _, expr := range ctx.AllExpr() {
- _, finalAttributes, err := q.extractQuerySpanResultResultFromExpr(expr)
- if err != nil {
- return "", base.QuerySpanResult{}, errors.Wrapf(err, "failed to check whether the expression %q is sensitive near line %d", expr.GetText(), expr.GetStart().GetLine())
- }
- querySpanResult.SourceColumns, _ = base.MergeSourceColumnSet(querySpanResult.SourceColumns, finalAttributes.SourceColumns)
+ for _, field := range sub.GetQuerySpanResult() {
+ result, _ = base.MergeSourceColumnSet(result, field.SourceColumns)
}
- return ctx.GetText(), querySpanResult, nil
- }
- return "", base.QuerySpanResult{}, errors.Errorf("never reach here")
- case *parser.Case_expressionContext:
- querySpanResult := base.QuerySpanResult{
- Name: ctx.GetText(),
- SourceColumns: make(base.SourceColumnSet),
- }
- for _, expr := range ctx.AllExpr() {
- _, maskingAttributes, err := q.extractQuerySpanResultResultFromExpr(expr)
- if err != nil {
- return "", base.QuerySpanResult{}, errors.Wrapf(err, "failed to check whether the expression %q is sensitive near line %d", expr.GetText(), expr.GetStart().GetLine())
- }
- querySpanResult.SourceColumns, _ = base.MergeSourceColumnSet(querySpanResult.SourceColumns, maskingAttributes.SourceColumns)
- }
- if v := ctx.AllSwitch_section(); len(v) > 0 {
- for _, switchSection := range v {
- _, maskingAttributes, err := q.extractQuerySpanResultResultFromExpr(switchSection)
- if err != nil {
- return "", base.QuerySpanResult{}, errors.Wrapf(err, "failed to check whether the expression %q is sensitive near line %d", switchSection.GetText(), switchSection.GetStart().GetLine())
+ return false
+ case *ast.StarExpr:
+ // COUNT(*) and similar: a star with no resolvable column contributes no
+ // specific source column (matching the legacy aggregate STAR branch).
+ return false
+ case *ast.CaseExpr:
+ // omni's generated walker only visits CaseExpr.Operand and
+ // CaseExpr.Else; the WHEN branches are []*WhenClause — a non-Node
+ // embedded struct — so without this manual recursion
+ // `CASE WHEN A > 0 THEN B END` would contribute no lineage at all.
+ for _, when := range n.Whens {
+ if when == nil {
+ continue
}
- querySpanResult.SourceColumns, _ = base.MergeSourceColumnSet(querySpanResult.SourceColumns, maskingAttributes.SourceColumns)
+ ast.Inspect(when.Cond, walk)
+ ast.Inspect(when.Result, walk)
}
- return ctx.GetText(), querySpanResult, nil
- }
- if v := ctx.AllSwitch_search_condition_section(); len(v) > 0 {
- for _, switchSearchConditionSection := range v {
- _, maskingAttributes, err := q.extractQuerySpanResultResultFromExpr(switchSearchConditionSection)
- if err != nil {
- return "", base.QuerySpanResult{}, errors.Wrapf(err, "failed to check whether the expression %q is sensitive near line %d", switchSearchConditionSection.GetText(), switchSearchConditionSection.GetStart().GetLine())
+ // Continue the normal traversal for Operand and Else.
+ return true
+ case *ast.FuncCallExpr:
+ // omni's generated walker only visits FuncCallExpr.Args; the
+ // WITHIN GROUP (ORDER BY ...) items and the OVER (...) window
+ // specification are non-Node embedded structs, so
+ // `ROW_NUMBER() OVER (ORDER BY A)` would otherwise contribute no
+ // lineage at all.
+ for _, item := range n.OrderBy {
+ if item != nil {
+ ast.Inspect(item.Expr, walk)
}
- querySpanResult.SourceColumns, _ = base.MergeSourceColumnSet(querySpanResult.SourceColumns, maskingAttributes.SourceColumns)
}
- return ctx.GetText(), querySpanResult, nil
- }
- return "", base.QuerySpanResult{}, errors.Errorf("never reach here")
- case *parser.Switch_sectionContext:
- querySpanResult := base.QuerySpanResult{
- Name: ctx.GetText(),
- SourceColumns: make(base.SourceColumnSet),
- }
- if v := ctx.AllExpr(); len(v) > 0 {
- for _, expr := range v {
- _, maskingAttributes, err := q.extractQuerySpanResultResultFromExpr(expr)
- if err != nil {
- return "", base.QuerySpanResult{}, errors.Wrapf(err, "failed to check whether the expression %q is sensitive near line %d", expr.GetText(), expr.GetStart().GetLine())
+ if n.Over != nil {
+ for _, partition := range n.Over.PartitionBy {
+ ast.Inspect(partition, walk)
}
- querySpanResult.SourceColumns, _ = base.MergeSourceColumnSet(querySpanResult.SourceColumns, maskingAttributes.SourceColumns)
- }
- return ctx.GetText(), querySpanResult, nil
- }
- return "", base.QuerySpanResult{}, errors.Errorf("never reach here")
- case *parser.Switch_search_condition_sectionContext:
- querySpanResult := base.QuerySpanResult{
- Name: ctx.GetText(),
- SourceColumns: make(base.SourceColumnSet),
- }
- if v := ctx.Search_condition(); v != nil {
- _, maskingAttributes, err := q.extractQuerySpanResultResultFromExpr(v)
- if err != nil {
- return "", base.QuerySpanResult{}, errors.Wrapf(err, "failed to check whether the expression %q is sensitive near line %d", v.GetText(), v.GetStart().GetLine())
- }
- querySpanResult.SourceColumns, _ = base.MergeSourceColumnSet(querySpanResult.SourceColumns, maskingAttributes.SourceColumns)
- _, maskingAttributes, err = q.extractQuerySpanResultResultFromExpr(ctx.Expr())
- if err != nil {
- return "", base.QuerySpanResult{}, errors.Wrapf(err, "failed to check whether the expression %q is sensitive near line %d", ctx.Expr().GetText(), ctx.Expr().GetStart().GetLine())
- }
- querySpanResult.SourceColumns, _ = base.MergeSourceColumnSet(querySpanResult.SourceColumns, maskingAttributes.SourceColumns)
- return ctx.GetText(), querySpanResult, nil
- }
- return "", base.QuerySpanResult{}, errors.Errorf("never reach here")
- case *parser.Search_conditionContext:
- querySpanResult := base.QuerySpanResult{
- Name: ctx.GetText(),
- SourceColumns: make(base.SourceColumnSet),
- }
- if v := ctx.Predicate(); v != nil {
- _, maskingAttributes, err := q.extractQuerySpanResultResultFromExpr(v)
- if err != nil {
- return "", base.QuerySpanResult{}, errors.Wrapf(err, "failed to check whether the expression %q is sensitive near line %d", ctx.Predicate().GetText(), ctx.Predicate().GetStart().GetLine())
- }
- querySpanResult.SourceColumns, _ = base.MergeSourceColumnSet(querySpanResult.SourceColumns, maskingAttributes.SourceColumns)
- }
- if v := ctx.AllSearch_condition(); len(v) > 0 {
- for _, searchCondition := range v {
- _, maskingAttributes, err := q.extractQuerySpanResultResultFromExpr(searchCondition)
- if err != nil {
- return "", base.QuerySpanResult{}, errors.Wrapf(err, "failed to check whether the expression %q is sensitive near line %d", searchCondition.GetText(), searchCondition.GetStart().GetLine())
+ for _, item := range n.Over.OrderBy {
+ if item != nil {
+ ast.Inspect(item.Expr, walk)
+ }
}
- querySpanResult.SourceColumns, _ = base.MergeSourceColumnSet(querySpanResult.SourceColumns, maskingAttributes.SourceColumns)
- }
- return ctx.GetText(), querySpanResult, nil
- }
- return "", base.QuerySpanResult{}, errors.Errorf("never reach here")
- case *parser.PredicateContext:
- querySpanResult := base.QuerySpanResult{
- Name: ctx.GetText(),
- SourceColumns: make(base.SourceColumnSet),
- }
- if v := ctx.AllExpr(); len(v) > 0 {
- for _, expr := range v {
- _, maskingAttributes, err := q.extractQuerySpanResultResultFromExpr(expr)
- if err != nil {
- return "", base.QuerySpanResult{}, errors.Wrapf(err, "failed to check whether the expression %q is sensitive near line %d", expr.GetText(), expr.GetStart().GetLine())
+ if n.Over.Frame != nil {
+ ast.Inspect(n.Over.Frame.Start.Offset, walk)
+ ast.Inspect(n.Over.Frame.End.Offset, walk)
}
- querySpanResult.SourceColumns, _ = base.MergeSourceColumnSet(querySpanResult.SourceColumns, maskingAttributes.SourceColumns)
- }
- }
- if v := ctx.Subquery(); v != nil {
- _, maskingAttributes, err := q.extractQuerySpanResultResultFromExpr(v)
- if err != nil {
- return "", base.QuerySpanResult{}, errors.Wrapf(err, "failed to check whether the expression %q is sensitive near line %d", v.GetText(), v.GetStart().GetLine())
}
- querySpanResult.SourceColumns, _ = base.MergeSourceColumnSet(querySpanResult.SourceColumns, maskingAttributes.SourceColumns)
- }
- if v := ctx.Expr_list(); v != nil {
- _, maskingAttributes, err := q.extractQuerySpanResultResultFromExpr(v)
- if err != nil {
- return "", base.QuerySpanResult{}, errors.Wrapf(err, "failed to check whether the expression %q is sensitive near line %d", v.GetText(), v.GetStart().GetLine())
- }
- querySpanResult.SourceColumns, _ = base.MergeSourceColumnSet(querySpanResult.SourceColumns, maskingAttributes.SourceColumns)
- }
- return ctx.GetText(), querySpanResult, nil
- case *parser.SubqueryContext:
- fields, err := q.extractPseudoTableFromQueryStatement(ctx.Query_statement())
- if err != nil {
- return "", base.QuerySpanResult{}, errors.Wrapf(err, "failed to check whether the expression %q is sensitive near line %d", ctx.GetText(), ctx.GetStart().GetLine())
- }
- querySpanResult := base.QuerySpanResult{
- Name: ctx.GetText(),
- SourceColumns: make(base.SourceColumnSet),
- }
- for _, querySpanResult := range fields.GetQuerySpanResult() {
- querySpanResult.SourceColumns, _ = base.MergeSourceColumnSet(querySpanResult.SourceColumns, querySpanResult.SourceColumns)
+ // Continue the normal traversal for Args.
+ return true
+ default:
}
- return ctx.GetText(), querySpanResult, nil
- case *parser.Primitive_expressionContext:
- if v := ctx.AllId_(); len(v) > 0 {
- return q.extractQuerySpanResultResultFromExpr(v[0])
- }
- return ctx.GetText(), base.QuerySpanResult{
- Name: ctx.GetText(),
- }, nil
- case *parser.Function_callContext:
- if v := ctx.Ranking_windowed_function(); v != nil {
- return q.extractQuerySpanResultResultFromExpr(v)
- }
- if v := ctx.Aggregate_function(); v != nil {
- return q.extractQuerySpanResultResultFromExpr(v)
- }
- if v := ctx.Object_name(); v != nil {
- return v.GetText(), base.QuerySpanResult{
- Name: ctx.GetText(),
- }, nil
- }
- if v := ctx.Expr_list(); v != nil {
- _, maskingAttributes, err := q.extractQuerySpanResultResultFromExpr(v)
- if err != nil {
- return "", base.QuerySpanResult{}, errors.Wrapf(err, "failed to check whether the expression %q is sensitive near line %d", v.GetText(), v.GetStart().GetLine())
- }
- return ctx.GetText(), maskingAttributes, nil
- }
- if v := ctx.AllExpr(); len(v) > 0 {
- _, maskingAttributes, err := q.extractQuerySpanResultResultFromExpr(v[0])
- if err != nil {
- return "", base.QuerySpanResult{}, errors.Wrapf(err, "failed to check whether the expression %q is sensitive near line %d", v[0].GetText(), v[0].GetStart().GetLine())
- }
- return ctx.GetText(), maskingAttributes, nil
- }
- return "", base.QuerySpanResult{}, errors.Errorf("never reach here")
- case *parser.Aggregate_functionContext:
- if v := ctx.Expr_list(); v != nil {
- _, maskingAttributes, err := q.extractQuerySpanResultResultFromExpr(v)
- if err != nil {
- return "", base.QuerySpanResult{}, errors.Wrapf(err, "failed to check whether the expression %q is sensitive near line %d", v.GetText(), v.GetStart().GetLine())
- }
- return ctx.GetText(), maskingAttributes, nil
- }
- if ctx.STAR() != nil {
- return ctx.GetText(), base.QuerySpanResult{}, nil
- }
- if v := ctx.Expr(); v != nil {
- querySpanResult := base.QuerySpanResult{
- Name: ctx.GetText(),
- SourceColumns: make(base.SourceColumnSet),
- }
- _, maskingAttributes, err := q.extractQuerySpanResultResultFromExpr(v)
- if err != nil {
- return "", base.QuerySpanResult{}, errors.Wrapf(err, "failed to check whether the expression %q is sensitive near line %d", v.GetText(), v.GetStart().GetLine())
- }
- querySpanResult.SourceColumns, _ = base.MergeSourceColumnSet(querySpanResult.SourceColumns, maskingAttributes.SourceColumns)
- _, maskingAttributes, err = q.extractQuerySpanResultResultFromExpr(ctx.Order_by_clause())
- if err != nil {
- return "", base.QuerySpanResult{}, errors.Wrapf(err, "failed to check whether the expression %q is sensitive near line %d", ctx.Order_by_clause().GetText(), ctx.Order_by_clause().GetStart().GetLine())
- }
- querySpanResult.SourceColumns, _ = base.MergeSourceColumnSet(querySpanResult.SourceColumns, maskingAttributes.SourceColumns)
- return ctx.GetText(), querySpanResult, nil
- }
- return "", base.QuerySpanResult{}, errors.Errorf("never reach here")
- case *parser.Ranking_windowed_functionContext:
- querySpanResult := base.QuerySpanResult{
- Name: ctx.GetText(),
- SourceColumns: make(base.SourceColumnSet),
- }
- if v := ctx.AllExpr(); len(v) > 0 {
- _, maskingAttributes, err := q.extractQuerySpanResultResultFromExpr(v[0])
- if err != nil {
- return "", base.QuerySpanResult{}, errors.Wrapf(err, "failed to check whether the expression %q is sensitive near line %d", v[0].GetText(), v[0].GetStart().GetLine())
- }
- querySpanResult.SourceColumns, _ = base.MergeSourceColumnSet(querySpanResult.SourceColumns, maskingAttributes.SourceColumns)
- }
- if v := ctx.Over_clause(); v != nil {
- _, maskingAttributes, err := q.extractQuerySpanResultResultFromExpr(v)
- if err != nil {
- return "", base.QuerySpanResult{}, errors.Wrapf(err, "failed to check whether the expression %q is sensitive near line %d", v.GetText(), v.GetStart().GetLine())
- }
- querySpanResult.SourceColumns, _ = base.MergeSourceColumnSet(querySpanResult.SourceColumns, maskingAttributes.SourceColumns)
- return ctx.GetText(), querySpanResult, nil
- }
- return "", base.QuerySpanResult{}, errors.Errorf("never reach here")
- case *parser.Over_clauseContext:
- querySpanResult := base.QuerySpanResult{
- Name: ctx.GetText(),
- SourceColumns: make(base.SourceColumnSet),
- }
- if v := ctx.Partition_by(); v != nil {
- _, maskingAttributes, err := q.extractQuerySpanResultResultFromExpr(v)
- if err != nil {
- return "", base.QuerySpanResult{}, errors.Wrapf(err, "failed to check whether the expression %q is sensitive near line %d", v.GetText(), v.GetStart().GetLine())
- }
- querySpanResult.SourceColumns, _ = base.MergeSourceColumnSet(querySpanResult.SourceColumns, maskingAttributes.SourceColumns)
- return ctx.GetText(), querySpanResult, nil
- }
- if v := ctx.Order_by_expr(); v != nil {
- _, maskingAttributes, err := q.extractQuerySpanResultResultFromExpr(v)
- if err != nil {
- return "", base.QuerySpanResult{}, errors.Wrapf(err, "failed to check whether the expression %q is sensitive near line %d", v.GetText(), v.GetStart().GetLine())
- }
- querySpanResult.SourceColumns, _ = base.MergeSourceColumnSet(querySpanResult.SourceColumns, maskingAttributes.SourceColumns)
- return ctx.GetText(), querySpanResult, nil
- }
- return "", base.QuerySpanResult{}, errors.Errorf("never reach here")
- case *parser.Partition_byContext:
- _, maskingAttributes, err := q.extractQuerySpanResultResultFromExpr(ctx.Expr_list())
- if err != nil {
- return "", base.QuerySpanResult{}, errors.Wrapf(err, "failed to check whether the expression %q is sensitive near line %d", ctx.Expr_list().GetText(), ctx.Expr_list().GetStart().GetLine())
- }
- return ctx.GetText(), maskingAttributes, nil
- case *parser.Order_by_exprContext:
- _, maskingAttributes, err := q.extractQuerySpanResultResultFromExpr(ctx.Expr_list_sorted())
- if err != nil {
- return "", base.QuerySpanResult{}, errors.Wrapf(err, "failed to check whether the expression %q is sensitive near line %d", ctx.Expr_list_sorted().GetText(), ctx.Expr_list_sorted().GetStart().GetLine())
- }
- return ctx.GetText(), maskingAttributes, nil
- case *parser.Expr_listContext:
- querySpanResult := base.QuerySpanResult{
- Name: ctx.GetText(),
- SourceColumns: make(base.SourceColumnSet),
- }
- allExpr := ctx.AllExpr()
- for _, expr := range allExpr {
- _, maskingAttributes, err := q.extractQuerySpanResultResultFromExpr(expr)
- if err != nil {
- return "", base.QuerySpanResult{}, errors.Wrapf(err, "failed to check whether the expression %q is sensitive near line %d", expr.GetText(), expr.GetStart().GetLine())
- }
- querySpanResult.SourceColumns, _ = base.MergeSourceColumnSet(querySpanResult.SourceColumns, maskingAttributes.SourceColumns)
- }
- return ctx.GetText(), querySpanResult, nil
- case *parser.Expr_list_sortedContext:
- querySpanResult := base.QuerySpanResult{
- Name: ctx.GetText(),
- SourceColumns: make(base.SourceColumnSet),
- }
- allExpr := ctx.AllExpr()
- for _, expr := range allExpr {
- _, maskingAttributes, err := q.extractQuerySpanResultResultFromExpr(expr)
- if err != nil {
- return "", base.QuerySpanResult{}, errors.Wrapf(err, "failed to check whether the expression %q is sensitive near line %d", expr.GetText(), expr.GetStart().GetLine())
- }
- querySpanResult.SourceColumns, _ = base.MergeSourceColumnSet(querySpanResult.SourceColumns, maskingAttributes.SourceColumns)
- }
- return ctx.GetText(), querySpanResult, nil
- case *parser.Id_Context:
- normalizedColumnName := NormalizeSnowSQLObjectNamePart(ctx)
- fieldInfo, err := q.getField("", "", "", normalizedColumnName)
- if err != nil {
- return "", base.QuerySpanResult{}, errors.Wrapf(err, "failed to check whether the column %q is sensitive near line %d", normalizedColumnName, ctx.GetStart().GetLine())
- }
- return fieldInfo.Name, fieldInfo, nil
+ return true
+ }
+ ast.Inspect(expr, walk)
+ if walkErr != nil {
+ return nil, walkErr
}
- return "", base.QuerySpanResult{}, errors.Errorf("never reach here")
+ return result, nil
}
-func (q *querySpanExtractor) extractTableSourceFromFromClause(ctx parser.IFrom_clauseContext) (base.TableSource, error) {
- if ctx == nil {
- return nil, nil
- }
+// exprDisplayName picks the output column name for an unaliased non-star target,
+// matching the legacy behavior where the result name fell back to the
+// expression's source text. For a bare column reference the trailing part is
+// used; for anything else omni's source range is unavailable here, so the name
+// is left empty (callers that have an AS alias override it anyway).
+func (q *querySpanExtractor) exprDisplayName(expr ast.Node) string {
+ switch e := expr.(type) {
+ case *ast.ColumnRef:
+ if len(e.Parts) > 0 {
+ return normalizeSnowflakeIdentifier(e.Parts[len(e.Parts)-1])
+ }
+ case *ast.ParenExpr:
+ return q.exprDisplayName(e.Expr)
+ case *ast.CastExpr:
+ return q.exprDisplayName(e.Expr)
+ case *ast.CollateExpr:
+ return q.exprDisplayName(e.Expr)
+ default:
+ }
+ // Fall back to the expression's verbatim source text (the legacy extractor
+ // used ctx.GetText()): omni nodes carry byte Locs into the statement.
+ if loc := ast.NodeLoc(expr); loc.End > loc.Start && loc.Start >= 0 && loc.End <= len(q.statementText) {
+ if name := strings.TrimSpace(q.statementText[loc.Start:loc.End]); utf8.ValidString(name) {
+ return name
+ }
+ }
+ return ""
+}
- return q.extractTableSourceFromTableSources(ctx.Table_sources())
+// normalizeColumnRef splits an omni ColumnRef (1-4 parts) into its normalized
+// (database, schema, table, column) components. A 1-part ref is a bare column;
+// 2-part is table.column; 3-part is schema.table.column; 4-part is
+// database.schema.table.column.
+func normalizeColumnRef(ref *ast.ColumnRef) (database, schema, table, column string) {
+ if ref == nil {
+ return "", "", "", ""
+ }
+ parts := ref.Parts
+ switch len(parts) {
+ case 1:
+ return "", "", "", normalizeSnowflakeIdentifier(parts[0])
+ case 2:
+ return "", "", normalizeSnowflakeIdentifier(parts[0]), normalizeSnowflakeIdentifier(parts[1])
+ case 3:
+ return "", normalizeSnowflakeIdentifier(parts[0]), normalizeSnowflakeIdentifier(parts[1]), normalizeSnowflakeIdentifier(parts[2])
+ case 4:
+ return normalizeSnowflakeIdentifier(parts[0]), normalizeSnowflakeIdentifier(parts[1]), normalizeSnowflakeIdentifier(parts[2]), normalizeSnowflakeIdentifier(parts[3])
+ default:
+ if len(parts) == 0 {
+ return "", "", "", ""
+ }
+ return "", "", "", normalizeSnowflakeIdentifier(parts[len(parts)-1])
+ }
}
-func (q *querySpanExtractor) extractTableSourceFromTableSources(ctx parser.ITable_sourcesContext) (base.TableSource, error) {
- if ctx == nil {
- return nil, nil
+func filterExcludedColumns(columns []base.QuerySpanResult, exclude []ast.Ident) []base.QuerySpanResult {
+ if len(exclude) == 0 {
+ return columns
+ }
+
+ excludedColumns := make(map[string]bool, len(exclude))
+ for _, columnName := range exclude {
+ excludedColumns[normalizeSnowflakeIdentifier(columnName)] = true
}
- allTableSources := ctx.AllTable_source()
+ var result []base.QuerySpanResult
+ for _, column := range columns {
+ if !excludedColumns[column.Name] {
+ result = append(result, column)
+ }
+ }
+ return result
+}
+
+// extractTableSourceFromFrom resolves a FROM clause (a list of *ast.TableRef and
+// *ast.JoinExpr items) into a single combined table source exposing every column
+// it provides. Multiple top-level FROM items are an implicit CROSS JOIN, so
+// their columns are concatenated into one pseudo-table (matching the legacy
+// extractTableSourceFromTableSources). A single FROM item is returned as-is so
+// its alias/name is preserved for qualified column resolution; two or more items
+// collapse into an unnamed pseudo-table.
+func (q *querySpanExtractor) extractTableSourceFromFrom(froms []ast.Node) (base.TableSource, error) {
var result base.TableSource
- // If there are multiple table sources, the default join type is CROSS JOIN.
- for _, tableSource := range allTableSources {
- candidatesTableSource, err := q.extractTableSourceFromTableSource(tableSource)
+ merged := false
+ for _, item := range froms {
+ tableSource, err := q.extractTableSourceFromItem(item)
if err != nil {
return nil, err
}
+ if tableSource == nil {
+ continue
+ }
if result == nil {
- result = candidatesTableSource
- } else {
- pseudoTable := &base.PseudoTable{
- Name: "",
- Columns: append(result.GetQuerySpanResult(), candidatesTableSource.GetQuerySpanResult()...),
- }
- result = pseudoTable
+ result = tableSource
+ continue
+ }
+ // Comma-list FROM items merge into one unnamed scope for positional/star
+ // purposes; record each named item in joinMemberSources so qualified
+ // references (alias.col, table.col) keep resolving — the same fallback
+ // JOIN trees use.
+ if !merged {
+ q.joinMemberSources = append(q.joinMemberSources, result)
+ merged = true
+ }
+ q.joinMemberSources = append(q.joinMemberSources, tableSource)
+ result = &base.PseudoTable{
+ Name: "",
+ Columns: append(result.GetQuerySpanResult(), tableSource.GetQuerySpanResult()...),
}
}
return result, nil
}
-func (q *querySpanExtractor) extractTableSourceFromTableSource(ctx parser.ITable_sourceContext) (base.TableSource, error) {
- if ctx == nil {
+func (q *querySpanExtractor) extractTableSourceFromItem(item ast.Node) (base.TableSource, error) {
+ switch n := item.(type) {
+ case *ast.TableRef:
+ return q.extractTableSourceFromTableRef(n)
+ case *ast.JoinExpr:
+ return q.extractTableSourceFromJoin(n)
+ default:
return nil, nil
}
- return q.extractTableSOurceFromTableSourceItemJoined(ctx.Table_source_item_joined())
}
-func (q *querySpanExtractor) extractTableSOurceFromTableSourceItemJoined(ctx parser.ITable_source_item_joinedContext) (base.TableSource, error) {
- if ctx == nil {
+func (q *querySpanExtractor) extractTableSourceFromJoin(join *ast.JoinExpr) (base.TableSource, error) {
+ if join == nil {
return nil, nil
}
-
- var left base.TableSource
- var err error
- if ctx.Object_ref() != nil {
- left, err = q.extractTableSourceFromObjectRef(ctx.Object_ref())
- if err != nil {
- return nil, errors.Wrapf(err, "failed to extract sensitive fields of the left part of the object ref near line %d", ctx.Object_ref().GetStart().GetLine())
- }
+ left, err := q.extractTableSourceFromItem(join.Left)
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to extract sensitive fields of the left part of the join")
}
-
- if ctx.Table_source_item_joined() != nil {
- left, err = q.extractTableSOurceFromTableSourceItemJoined(ctx.Table_source_item_joined())
- if err != nil {
- return nil, errors.Wrapf(err, "failed to extract sensitive fields of the left part of the table source item joined near line %d", ctx.Table_source_item_joined().GetStart().GetLine())
- }
+ if left != nil {
+ q.joinMemberSources = append(q.joinMemberSources, left)
+ }
+ right, err := q.extractTableSourceFromItem(join.Right)
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to extract sensitive fields of the right part of the join")
+ }
+ if right != nil {
+ q.joinMemberSources = append(q.joinMemberSources, right)
}
- for i, joinClause := range ctx.AllJoin_clause() {
- left, err = q.extractPseudoTableFromJoinClause(joinClause, left)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to extract sensitive fields of the left part of the #%d join clause near line %d", i+1, joinClause.GetStart().GetLine())
- }
+ var leftColumns, rightColumns []base.QuerySpanResult
+ if left != nil {
+ leftColumns = left.GetQuerySpanResult()
+ }
+ if right != nil {
+ rightColumns = right.GetQuerySpanResult()
}
- return left, nil
-}
+ // Snowflake has 6 join types: INNER, LEFT OUTER, RIGHT OUTER, FULL OUTER,
+ // CROSS, and NATURAL. NATURAL JOIN and JOIN ... USING collapse the shared
+ // join keys to a single output column; getting the collapse wrong is worse
+ // than wrong lineage, because a duplicated shared column shifts every later
+ // positional mask by one.
-func (q *querySpanExtractor) extractPseudoTableFromJoinClause(ctx parser.IJoin_clauseContext, left base.TableSource) (*base.PseudoTable, error) {
- if ctx == nil {
- return nil, nil
+ if join.Natural {
+ // NATURAL JOIN: each shared column appears once, carrying the lineage of
+ // BOTH sides (the legacy extractor collapsed but kept only the left
+ // side's lineage). Left columns keep their order, then the right-only
+ // columns follow.
+ rightIndexByName := make(map[string]int, len(rightColumns))
+ for i, rightColumn := range rightColumns {
+ if _, ok := rightIndexByName[rightColumn.Name]; !ok {
+ rightIndexByName[rightColumn.Name] = i
+ }
+ }
+ sharedNames := make(map[string]bool)
+ var result []base.QuerySpanResult
+ for _, leftColumn := range leftColumns {
+ if i, ok := rightIndexByName[leftColumn.Name]; ok {
+ sharedNames[leftColumn.Name] = true
+ leftColumn.SourceColumns, _ = base.MergeSourceColumnSet(leftColumn.SourceColumns, rightColumns[i].SourceColumns)
+ }
+ result = append(result, leftColumn)
+ }
+ for _, rightColumn := range rightColumns {
+ if !sharedNames[rightColumn.Name] {
+ result = append(result, rightColumn)
+ }
+ }
+ return &base.PseudoTable{
+ Name: "",
+ Columns: result,
+ }, nil
}
- // Snowflake has 6 types of join:
- // INNER JOIN, LEFT OUTER JOIN, RIGHT OUTER JOIN, FULL OUTER JOIN, CROSS JOIN, and NATURAL JOIN.
- // Only the result(column num) of NATURAL JOIN may be reduced.
- right, err := q.extractTableSourceFromObjectRef(ctx.Object_ref())
- if err != nil {
- return nil, errors.Wrapf(err, "failed to extract sensitive fields of the right part of the JOIN near line %d", ctx.Object_ref().GetStart().GetLine())
- }
- if ctx.NATURAL() != nil {
- // We should remove all the duplicate columns in the result set.
- // For example, if the left part has columns [a, b, c], and the right part has columns [a, b, d],
- // then the result set of NATURAL JOIN should be [a, b, c, d].
- rightMap := make(map[string]bool)
- for _, rightColumn := range right.GetQuerySpanResult() {
- rightMap[rightColumn.Name] = true
+ if len(join.Using) > 0 {
+ // JOIN ... USING (cols): per
+ // https://docs.snowflake.com/en/sql-reference/constructs/join the output
+ // contains the USING columns first (in the order specified, each ONCE,
+ // carrying the merged lineage of both sides), then the left table
+ // columns not in USING, then the right table columns not in USING.
+ // The legacy extractor had no USING handling at all and duplicated the
+ // shared columns.
+ usingNames := make([]string, 0, len(join.Using))
+ usingSet := make(map[string]bool, len(join.Using))
+ for _, col := range join.Using {
+ name := normalizeSnowflakeIdentifier(col)
+ if usingSet[name] {
+ // Defensive: a duplicated USING column is invalid SQL; keep one.
+ continue
+ }
+ usingSet[name] = true
+ usingNames = append(usingNames, name)
}
+
var result []base.QuerySpanResult
- for _, leftColumn := range left.GetQuerySpanResult() {
- delete(rightMap, leftColumn.Name)
- result = append(result, leftColumn)
+ for _, name := range usingNames {
+ var merged *base.QuerySpanResult
+ for _, leftColumn := range leftColumns {
+ if leftColumn.Name == name {
+ columnCopy := leftColumn
+ merged = &columnCopy
+ break
+ }
+ }
+ for _, rightColumn := range rightColumns {
+ if rightColumn.Name == name {
+ if merged == nil {
+ columnCopy := rightColumn
+ merged = &columnCopy
+ } else {
+ merged.SourceColumns, _ = base.MergeSourceColumnSet(merged.SourceColumns, rightColumn.SourceColumns)
+ }
+ break
+ }
+ }
+ if merged == nil {
+ // The USING column resolves on neither side (invalid SQL or an
+ // unresolvable source); emit an empty-lineage column to keep the
+ // positional column count stable.
+ merged = &base.QuerySpanResult{
+ Name: name,
+ SourceColumns: make(base.SourceColumnSet),
+ IsPlainField: false,
+ }
+ }
+ result = append(result, *merged)
}
- for _, rightColumn := range right.GetQuerySpanResult() {
- if _, ok := rightMap[rightColumn.Name]; ok {
+ for _, leftColumn := range leftColumns {
+ if !usingSet[leftColumn.Name] {
+ result = append(result, leftColumn)
+ }
+ }
+ for _, rightColumn := range rightColumns {
+ if !usingSet[rightColumn.Name] {
result = append(result, rightColumn)
}
}
@@ -1120,152 +944,73 @@ func (q *querySpanExtractor) extractPseudoTableFromJoinClause(ctx parser.IJoin_c
}, nil
}
- // For other types of join, we should keep all the columns for the left part and the right part.
var result []base.QuerySpanResult
- result = append(result, left.GetQuerySpanResult()...)
- result = append(result, right.GetQuerySpanResult()...)
+ result = append(result, leftColumns...)
+ result = append(result, rightColumns...)
return &base.PseudoTable{
Name: "",
Columns: result,
}, nil
}
-func (q *querySpanExtractor) extractTableSourceFromObjectRef(ctx parser.IObject_refContext) (base.TableSource, error) {
- if ctx == nil {
+func (q *querySpanExtractor) extractTableSourceFromTableRef(ref *ast.TableRef) (base.TableSource, error) {
+ if ref == nil {
return nil, nil
}
- var result []base.QuerySpanResult
+ // FAIL CLOSED on PIVOT/UNPIVOT: the pivoted relation's projection (dynamic
+ // pivot columns) is not modeled here yet, so resolving the bare base table
+ // would silently produce wrong lineage/positions for masking. Returning an
+ // error makes GetQuerySpan degrade explicitly instead. (omni models the
+ // clause on TableRef since #300; lineage support is a follow-up.)
+ if ref.Pivot != nil || ref.Unpivot != nil || ref.Nested != nil {
+ return nil, errors.New("PIVOT/UNPIVOT table sources are not supported for query span extraction yet")
+ }
- if objectName := ctx.Object_name(); objectName != nil {
- _, tableSource, err := q.findTableSchema(objectName, q.defaultDatbase, q.defaultSchema)
+ var result []base.QuerySpanResult
+ // underlying keeps the resolved (named) table source so an unaliased
+ // reference retains its identity — qualified references (T2.B, T2.*) must
+ // resolve by the original table name.
+ var underlying base.TableSource
+
+ switch {
+ case ref.Name != nil:
+ _, tableSource, err := q.findTableSchema(ref.Name, q.defaultDatbase, q.defaultSchema)
if err != nil {
return nil, err
}
+ underlying = tableSource
result = append(result, tableSource.GetQuerySpanResult()...)
- }
-
- // TODO(zp): Handle the value clause.
- if ctx.Values_table() != nil {
- return nil, nil
- }
-
- // TODO(zp): In data-warehouse, define a function to return multiple rows is widespread, we should parse the
- // function definition to extract the sensitive fields.
- if ctx.TABLE() != nil {
- return nil, nil
- }
-
- if ctx.Subquery() != nil {
- tableSource, err := q.extractPseudoTableFromQueryStatement(ctx.Subquery().Query_statement())
+ case ref.Subquery != nil:
+ tableSource, err := q.extractPseudoTableFromQueryNode(ref.Subquery)
if err != nil {
- return nil, errors.Wrapf(err, "failed to extract sensitive fields of subquery near line %d", ctx.Subquery().GetStart().GetLine())
+ return nil, errors.Wrapf(err, "failed to extract sensitive fields of subquery in FROM")
}
result = append(result, tableSource.GetQuerySpanResult()...)
- }
-
- // TODO(zp): Handle the flatten table.
- if ctx.Flatten_table() != nil {
- return nil, nil
- }
-
- if v := ctx.Pivot_unpivot(); v != nil {
- if v.PIVOT() != nil {
- pivotColumnName := v.AllId_()[1]
- normalizedPivotColumnName := NormalizeSnowSQLObjectNamePart(pivotColumnName)
- pivotColumnIndex := -1
- for i, field := range result {
- if field.Name == normalizedPivotColumnName {
- pivotColumnIndex = i
- break
- }
- }
- if pivotColumnIndex == -1 {
- return nil, errors.Errorf(`pivot column %s is not found from field list %+v`, normalizedPivotColumnName, result)
- }
- pivotColumnInOriginalResult := result[pivotColumnIndex]
- result = append(result[:pivotColumnIndex], result[pivotColumnIndex+1:]...)
-
- valueColumnName := v.AllId_()[2]
- normalizedValueColumnName := NormalizeSnowSQLObjectNamePart(valueColumnName)
- valueColumnIndex := -1
- for i, field := range result {
- if field.Name == normalizedValueColumnName {
- valueColumnIndex = i
- break
- }
- }
- if valueColumnIndex == -1 {
- return nil, errors.Errorf(`value column %s is not found from field list %+v`, normalizedValueColumnName, result)
- }
- result = append(result[:valueColumnIndex], result[valueColumnIndex+1:]...)
-
- if v.Pivot_in_clause() == nil {
- return nil, errors.Errorf("pivot in clause is missing near line %d", v.GetStart().GetLine())
- }
- for _, literal := range v.Pivot_in_clause().AllLiteral() {
- result = append(result, base.QuerySpanResult{
- Name: literal.GetText(),
- SourceColumns: pivotColumnInOriginalResult.SourceColumns,
- })
- }
- } else if v.UNPIVOT() != nil {
- var strippedColumnIndices []int
- var strippedColumnInOriginalResult []base.QuerySpanResult
- if v.Aliased_column_list() == nil {
- return nil, errors.Errorf("aliased column list is missing for unpivot near line %d", v.GetStart().GetLine())
- }
- for idx, columnName := range v.Aliased_column_list().AllColumn_name() {
- normalizedColumnName := normalizeSnowSQLColumnName(columnName)
- for i, field := range result {
- if field.Name == normalizedColumnName {
- strippedColumnIndices = append(strippedColumnIndices, i)
- strippedColumnInOriginalResult = append(strippedColumnInOriginalResult, field)
- break
- }
- }
- if len(strippedColumnIndices) != idx+1 {
- return nil, errors.Errorf(`column %s is not found from field list %+v`, normalizedColumnName, result)
- }
- result = append(result[:strippedColumnIndices[idx]], result[strippedColumnIndices[idx]+1:]...)
- }
-
- sourceColumns := make(base.SourceColumnSet)
- for _, field := range strippedColumnInOriginalResult {
- sourceColumns, _ = base.MergeSourceColumnSet(sourceColumns, field.SourceColumns)
- }
-
- valueColumnName := v.Id_(0)
- normalizedValueColumnName := NormalizeSnowSQLObjectNamePart(valueColumnName)
-
- nameColumnName := normalizeSnowSQLColumnName(v.Column_name())
-
- result = append(result, base.QuerySpanResult{
- Name: nameColumnName,
- SourceColumns: make(base.SourceColumnSet),
- }, base.QuerySpanResult{
- Name: normalizedValueColumnName,
- SourceColumns: sourceColumns,
- })
- }
- }
-
- // If the as alias is not nil, we should use the alias name to replace the original table name.
- if ctx.Pivot_unpivot() != nil && ctx.Pivot_unpivot().As_alias() != nil {
- id := ctx.Pivot_unpivot().As_alias().Alias().Id_()
- aliasName := NormalizeSnowSQLObjectNamePart(id)
+ case ref.FuncCall != nil:
+ // NOTE(zp): In data-warehouse, defining a table function that returns
+ // multiple rows is widespread; we should parse the function definition to
+ // extract the sensitive fields. For now, a table function exposes no known
+ // columns (mirroring the legacy TABLE()/flatten handling).
+ result = nil
+ default:
+ result = nil
+ }
+
+ // If the table reference carries an alias, expose its columns under the alias
+ // name so qualified column references (alias.column) resolve to it.
+ if !ref.Alias.IsEmpty() {
+ aliasName := normalizeSnowflakeIdentifier(ref.Alias)
return &base.PseudoTable{
Name: aliasName,
Columns: result,
}, nil
}
- if ctx.As_alias() != nil {
- id := ctx.As_alias().Alias().Id_()
- aliasName := NormalizeSnowSQLObjectNamePart(id)
- return &base.PseudoTable{
- Name: aliasName,
- Columns: result,
- }, nil
+
+ // No alias: keep the resolved table source (with its real name) so
+ // qualified lookups by the original table name still match.
+ if underlying != nil {
+ return underlying, nil
}
return &base.PseudoTable{
@@ -1274,17 +1019,20 @@ func (q *querySpanExtractor) extractTableSourceFromObjectRef(ctx parser.IObject_
}, nil
}
-func (q *querySpanExtractor) findTableSchema(objectName parser.IObject_nameContext, normalizedFallbackDatabaseName, normalizedFallbackSchemaName string) (string, base.TableSource, error) {
- normalizedDatabaseName, normalizedSchemaName, normalizedTableName := normalizedObjectName(objectName, "", "")
- // For snowflake, we should find the table schema in ctes by ascending order.
+func (q *querySpanExtractor) findTableSchema(objectName *ast.ObjectName, normalizedFallbackDatabaseName, normalizedFallbackSchemaName string) (string, base.TableSource, error) {
+ normalizedDatabaseName, normalizedSchemaName, normalizedTableName := normalizeSnowflakeObjectName(objectName, "", "")
+ // Scan CTEs newest-first so an inner scope's CTE shadows an outer one with
+ // the same name (q.ctes is a stack of scopes; duplicate names within one
+ // WITH list are a Snowflake error, so this only affects cross-scope shadowing).
if normalizedDatabaseName == "" && normalizedSchemaName == "" {
- for _, cte := range q.ctes {
+ for i := len(q.ctes) - 1; i >= 0; i-- {
+ cte := q.ctes[i]
if normalizedTableName == cte.Name {
return normalizedDatabaseName, cte, nil
}
}
}
- normalizedDatabaseName, normalizedSchemaName, normalizedTableName = normalizedObjectName(objectName, normalizedFallbackDatabaseName, normalizedFallbackSchemaName)
+ normalizedDatabaseName, normalizedSchemaName, normalizedTableName = normalizeSnowflakeObjectName(objectName, normalizedFallbackDatabaseName, normalizedFallbackSchemaName)
allDatabases, err := q.gCtx.ListDatabaseNamesFunc(q.ctx, q.gCtx.InstanceID)
if err != nil {
return "", nil, errors.Wrapf(err, "failed to get all databases")
@@ -1363,7 +1111,9 @@ func (q *querySpanExtractor) getAllFieldsOfTableInFromOrOuterCTE(normalizedDatab
var result []base.QuerySpanResult
if mask&maskDatabaseName == 0 && mask&maskSchemaName == 0 && mask&maskTableName != 0 {
- for _, cte := range q.ctes {
+ // Newest-first: inner-scope CTEs shadow outer ones (see findTableSchema).
+ for i := len(q.ctes) - 1; i >= 0; i-- {
+ cte := q.ctes[i]
if normalizedTableName == cte.Name {
result = append(result, cte.GetQuerySpanResult()...)
return result, nil
@@ -1385,6 +1135,25 @@ func (q *querySpanExtractor) getAllFieldsOfTableInFromOrOuterCTE(normalizedDatab
return result, nil
}
+ // Qualified reference that did not match a FROM item: it may name a
+ // relation inside a JOIN tree (tableSourcesFrom holds only the merged,
+ // unnamed join pseudo-table).
+ if mask&maskTableName != 0 {
+ for _, tableSource := range q.joinMemberSources {
+ if mask&maskDatabaseName != 0 && normalizedDatabaseName != tableSource.GetDatabaseName() {
+ continue
+ }
+ if mask&maskSchemaName != 0 && normalizedSchemaName != tableSource.GetSchemaName() {
+ continue
+ }
+ if normalizedTableName != tableSource.GetTableName() {
+ continue
+ }
+ result = append(result, tableSource.GetQuerySpanResult()...)
+ return result, nil
+ }
+ }
+
return nil, errors.Errorf(`no matching table %q.%q.%q`, normalizedDatabaseName, normalizedSchemaName, normalizedTableName)
}
@@ -1456,66 +1225,138 @@ func (q *querySpanExtractor) getField(normalizedDatabaseName, normalizedSchemaNa
return field, nil
}
}
+ // Qualified reference into a JOIN tree: tableSourcesFrom holds only the
+ // merged, unnamed join pseudo-table, so match the member relations.
+ if mask&maskTableName != 0 {
+ for _, tableSource := range q.joinMemberSources {
+ if mask&maskDatabaseName != 0 && normalizedDatabaseName != tableSource.GetDatabaseName() {
+ continue
+ }
+ if mask&maskSchemaName != 0 && normalizedSchemaName != tableSource.GetSchemaName() {
+ continue
+ }
+ if normalizedTableName != tableSource.GetTableName() {
+ continue
+ }
+ for _, field := range tableSource.GetQuerySpanResult() {
+ if mask&maskColumnName != 0 && normalizedColumnName != field.Name {
+ continue
+ }
+ return field, nil
+ }
+ }
+ }
+
return base.QuerySpanResult{}, errors.Errorf(`no matching column %q.%q.%q.%q`, normalizedDatabaseName, normalizedSchemaName, normalizedTableName, normalizedColumnName)
}
-func normalizedFullColumnName(ctx parser.IFull_column_nameContext) (normalizedDatabaseName, normalizedSchemaName, normalizedTableName, normalizedColumnName string) {
- if ctx.GetDb_name() != nil {
- normalizedDatabaseName = NormalizeSnowSQLObjectNamePart(ctx.GetDb_name())
- }
- if ctx.GetSchema() != nil {
- normalizedSchemaName = NormalizeSnowSQLObjectNamePart(ctx.GetSchema())
- }
- if ctx.GetTab_name() != nil {
- normalizedTableName = NormalizeSnowSQLObjectNamePart(ctx.GetTab_name())
- }
- if ctx.GetCol_name() != nil {
- normalizedColumnName = NormalizeSnowSQLObjectNamePart(ctx.GetCol_name())
- }
- return normalizedDatabaseName, normalizedSchemaName, normalizedTableName, normalizedColumnName
+// getAccessTables extracts the list of resources from the SELECT statement, and
+// normalizes the object names with the NON-EMPTY currentNormalizedDatabase and
+// currentNormalizedSchema. It collects every *ast.TableRef with a table name —
+// including CTE references and subquery tables — mirroring the legacy
+// accessTablesListener, which added every object_ref it visited.
+//
+// It uses a hand-written recursion as belt-and-suspenders: it predates the
+// omni walker-coverage fix, when the generated walker skipped a SelectStmt's
+// WITH clause, SELECT targets, GROUP BY, ORDER BY, and FETCH. The walker now
+// descends into all of those (the SQL-review advisors rely on plain
+// ast.Inspect reaching CTE bodies and SELECT-list subqueries, locked by yaml
+// cases), so this recursion is no longer load-bearing for coverage; it is kept
+// because it is correct, tested, and independent of walker regressions.
+func getAccessTables(currentNormalizedDatabase, currentNormalizedSchema string, node ast.Node) base.SourceColumnSet {
+ resourceMap := make(base.SourceColumnSet)
+ collectAccessTables(node, currentNormalizedDatabase, currentNormalizedSchema, resourceMap)
+ return resourceMap
}
-func normalizedObjectName(objectName parser.IObject_nameContext, normalizedFallbackDatabaseName, normalizedFallbackSchemaName string) (string, string, string) {
- // TODO(zp): unify here with NormalizeObjectName in backend/plugin/parser/sql/snowsql.go
- var parts []string
- if objectName == nil {
- return "", "", ""
+// collectAccessTables recursively records every table reference reachable from
+// node into resourceMap. It explicitly descends into the SelectStmt sub-parts
+// (WITH/targets/GROUP BY/ORDER BY/FETCH; redundant with the fixed omni walker,
+// see getAccessTables); for everything else it falls back to ast.Inspect.
+func collectAccessTables(node ast.Node, defaultDatabase, defaultSchema string, resourceMap base.SourceColumnSet) {
+ if node == nil {
+ return
}
- database := normalizedFallbackDatabaseName
- if d := objectName.GetD(); d != nil {
- normalizedD := NormalizeSnowSQLObjectNamePart(d)
- if normalizedD != "" {
- database = normalizedD
+ switch n := node.(type) {
+ case *ast.File:
+ for _, stmt := range n.Stmts {
+ collectAccessTables(stmt, defaultDatabase, defaultSchema, resourceMap)
}
- }
- parts = append(parts, database)
-
- schema := normalizedFallbackSchemaName
- if s := objectName.GetS(); s != nil {
- normalizedS := NormalizeSnowSQLObjectNamePart(s)
- if normalizedS != "" {
- schema = normalizedS
+ return
+ case *ast.SetOperationStmt:
+ collectAccessTables(n.Left, defaultDatabase, defaultSchema, resourceMap)
+ collectAccessTables(n.Right, defaultDatabase, defaultSchema, resourceMap)
+ return
+ case *ast.SelectStmt:
+ for _, cte := range n.With {
+ collectAccessTables(cte.Query, defaultDatabase, defaultSchema, resourceMap)
}
- }
- parts = append(parts, schema)
-
- normalizedO := NormalizeSnowSQLObjectNamePart(objectName.GetO())
- parts = append(parts, normalizedO)
-
- return parts[0], parts[1], parts[2]
-}
-
-// getAccessTables extracts the list of resources from the SELECT statement, and normalizes the object names with the NON-EMPTY currentNormalizedDatabase and currentNormalizedSchema.
-func getAccessTables(currentNormalizedDatabase string, currentNormalizedSchema string, tree antlr.Tree) base.SourceColumnSet {
- l := &accessTablesListener{
- currentDatabase: currentNormalizedDatabase,
- currentSchema: currentNormalizedSchema,
- resourceMap: make(base.SourceColumnSet),
+ for _, item := range n.From {
+ collectAccessTables(item, defaultDatabase, defaultSchema, resourceMap)
+ }
+ for _, target := range n.Targets {
+ if target != nil {
+ collectAccessTables(target.Expr, defaultDatabase, defaultSchema, resourceMap)
+ }
+ }
+ collectAccessTables(n.Where, defaultDatabase, defaultSchema, resourceMap)
+ if n.GroupBy != nil {
+ for _, item := range n.GroupBy.Items {
+ collectAccessTables(item, defaultDatabase, defaultSchema, resourceMap)
+ }
+ }
+ collectAccessTables(n.Having, defaultDatabase, defaultSchema, resourceMap)
+ collectAccessTables(n.Qualify, defaultDatabase, defaultSchema, resourceMap)
+ for _, item := range n.OrderBy {
+ if item != nil {
+ collectAccessTables(item.Expr, defaultDatabase, defaultSchema, resourceMap)
+ }
+ }
+ return
+ case *ast.JoinExpr:
+ collectAccessTables(n.Left, defaultDatabase, defaultSchema, resourceMap)
+ collectAccessTables(n.Right, defaultDatabase, defaultSchema, resourceMap)
+ collectAccessTables(n.On, defaultDatabase, defaultSchema, resourceMap)
+ return
+ case *ast.TableRef:
+ if n.Name != nil {
+ database, schema, table := normalizeSnowflakeObjectName(n.Name, defaultDatabase, defaultSchema)
+ resourceMap[base.ColumnResource{
+ Database: database,
+ Schema: schema,
+ Table: table,
+ }] = true
+ }
+ if n.Subquery != nil {
+ collectAccessTables(n.Subquery, defaultDatabase, defaultSchema, resourceMap)
+ }
+ if n.FuncCall != nil {
+ collectAccessTables(n.FuncCall, defaultDatabase, defaultSchema, resourceMap)
+ }
+ return
+ case *ast.SubqueryExpr:
+ collectAccessTables(n.Query, defaultDatabase, defaultSchema, resourceMap)
+ return
+ case *ast.ExistsExpr:
+ collectAccessTables(n.Query, defaultDatabase, defaultSchema, resourceMap)
+ return
}
- antlr.ParseTreeWalkerDefault.Walk(l, tree)
-
- return l.resourceMap
+ // For any other expression node, walk its children with ast.Inspect (which
+ // fully traverses non-SelectStmt sub-trees) and recurse into the
+ // query-bearing nodes it surfaces. This catches table refs nested inside
+ // arbitrary expressions (e.g. a scalar subquery inside a CASE in WHERE).
+ ast.Inspect(node, func(child ast.Node) bool {
+ if child == nil || child == node {
+ return true
+ }
+ switch child.(type) {
+ case *ast.SelectStmt, *ast.SetOperationStmt, *ast.TableRef, *ast.JoinExpr, *ast.SubqueryExpr, *ast.ExistsExpr:
+ collectAccessTables(child, defaultDatabase, defaultSchema, resourceMap)
+ return false
+ }
+ return true
+ })
}
// isMixedQuery checks whether the query accesses the user table and system table at the same time.
@@ -1541,49 +1382,131 @@ func isSystemResource(base.ColumnResource, bool) bool {
return false
}
-type accessTablesListener struct {
- *parser.BaseSnowflakeParserListener
-
- currentDatabase string
- currentSchema string
- resourceMap base.SourceColumnSet
+// resolvePositionalRef resolves a positional column reference $N (optionally
+// qualified, d.$1) to the N-th column of the FROM scope (or of the qualified
+// relation), mirroring the legacy DOLLAR/Column_position branch: errors on a
+// non-numeric, < 1, or out-of-range position.
+func (q *querySpanExtractor) resolvePositionalRef(dollar *ast.DollarRef) (base.QuerySpanResult, error) {
+ position, err := strconv.Atoi(dollar.Name)
+ if err != nil {
+ return base.QuerySpanResult{}, errors.Wrapf(err, "failed to parse column position %q to integer", dollar.Name)
+ }
+ if position < 1 {
+ return base.QuerySpanResult{}, errors.Errorf("column position %d is invalid because it is less than 1", position)
+ }
+ var normalizedDatabaseName, normalizedSchemaName, normalizedTableName string
+ if dollar.Qualifier != nil {
+ normalizedDatabaseName, normalizedSchemaName, normalizedTableName = normalizeSnowflakeObjectName(dollar.Qualifier, "", "")
+ }
+ left, err := q.getAllFieldsOfTableInFromOrOuterCTE(normalizedDatabaseName, normalizedSchemaName, normalizedTableName)
+ if err != nil {
+ return base.QuerySpanResult{}, errors.Wrapf(err, "failed to resolve positional column reference $%s", dollar.Name)
+ }
+ if position > len(left) {
+ return base.QuerySpanResult{}, errors.Errorf("column position $%d is invalid: the FROM clause only returns %d columns", position, len(left))
+ }
+ return left[position-1], nil
}
-func (l *accessTablesListener) EnterObject_ref(ctx *parser.Object_refContext) {
- objectName := ctx.Object_name()
- if objectName == nil {
- return
+// applyStarRenames applies the `SELECT * RENAME (col AS alias, ...)` transform
+// to a star expansion: the matching result columns keep their lineage but take
+// the alias as their output name (Snowflake returns the renamed column).
+// Matching uses normalized identifiers, like the rest of the resolver.
+func applyStarRenames(columns []base.QuerySpanResult, renames []ast.StarRename) []base.QuerySpanResult {
+ if len(renames) == 0 {
+ return columns
}
-
- database := l.currentDatabase
- if d := objectName.GetD(); d != nil {
- normalizedD := NormalizeSnowSQLObjectNamePart(d)
- if normalizedD != "" {
- database = normalizedD
+ aliasByColumn := make(map[string]string, len(renames))
+ for _, r := range renames {
+ aliasByColumn[normalizeSnowflakeIdentifier(r.Col)] = normalizeSnowflakeIdentifier(r.Alias)
+ }
+ // Copy before renaming: columns may be the FROM-scope table source's
+ // internal slice (getAllFieldsOfTableInFromOrOuterCTE returns it directly,
+ // and base.PseudoTable documents that callers must copy before modifying);
+ // mutating it in place would corrupt the scope for later references.
+ renamed := make([]base.QuerySpanResult, len(columns))
+ copy(renamed, columns)
+ for i := range renamed {
+ if alias, ok := aliasByColumn[renamed[i].Name]; ok {
+ renamed[i].Name = alias
}
}
+ return renamed
+}
- schema := l.currentSchema
- if s := objectName.GetS(); s != nil {
- normalizedS := NormalizeSnowSQLObjectNamePart(s)
- if normalizedS != "" {
- schema = normalizedS
+// filterIlikeColumns applies the `SELECT * ILIKE '<pattern>'` transform: only
+// columns whose name matches the pattern (case-insensitive; % = any run,
+// _ = any single character) survive the star expansion. Applied FIRST, before
+// EXCLUDE/REPLACE/RENAME, per the documented transform order. The positional
+// masker depends on the span matching the actual result-set shape, so this
+// must genuinely filter (not over-attribute).
+func filterIlikeColumns(columns []base.QuerySpanResult, pattern *ast.Literal) []base.QuerySpanResult {
+ if pattern == nil {
+ return columns
+ }
+ matcher := ilikeMatcher(pattern.Value)
+ filtered := make([]base.QuerySpanResult, 0, len(columns))
+ for _, column := range columns {
+ if matcher(column.Name) {
+ filtered = append(filtered, column)
}
}
+ return filtered
+}
- var table string
- if o := objectName.GetO(); o != nil {
- normalizedO := NormalizeSnowSQLObjectNamePart(o)
- if normalizedO != "" {
- table = normalizedO
- }
+// ilikeMatcher compiles a Snowflake ILIKE pattern into a case-insensitive
+// matcher over the whole name (% -> any run, _ -> any single rune). Go regexp
+// (?i) covers ASCII case-insensitivity; it does not replicate Snowflake SQL
+// collation for non-ASCII identifiers (low risk: column names are near-always
+// ASCII).
+func ilikeMatcher(pattern string) func(string) bool {
+ var sb strings.Builder
+ sb.WriteString(`(?is)\A`)
+ for _, r := range pattern {
+ switch r {
+ case '%':
+ sb.WriteString(`.*`)
+ case '_':
+ sb.WriteString(`.`)
+ default:
+ sb.WriteString(regexp.QuoteMeta(string(r)))
+ }
+ }
+ sb.WriteString(`\z`)
+ re, err := regexp.Compile(sb.String())
+ if err != nil {
+ // A pattern that fails to compile matches nothing (fail closed).
+ return func(string) bool { return false }
}
+ return re.MatchString
+}
- l.resourceMap[base.ColumnResource{
- Server: "",
- Database: database,
- Schema: schema,
- Table: table,
- Column: "",
- }] = true
+// applyStarReplaces applies the `SELECT * REPLACE (expr AS col, ...)` transform
+// to a star expansion: the matching result column keeps its position and name
+// but its lineage becomes the replacement EXPRESSION's source columns
+// (Snowflake returns expr in place of the original column value). Copy-on-write
+// like applyStarRenames — the input may be a table source's internal slice.
+func (q *querySpanExtractor) applyStarReplaces(columns []base.QuerySpanResult, replaces []ast.StarReplace) ([]base.QuerySpanResult, error) {
+ if len(replaces) == 0 {
+ return columns, nil
+ }
+ exprByColumn := make(map[string]ast.Node, len(replaces))
+ for _, r := range replaces {
+ exprByColumn[normalizeSnowflakeIdentifier(r.Col)] = r.Expr
+ }
+ replaced := make([]base.QuerySpanResult, len(columns))
+ copy(replaced, columns)
+ for i := range replaced {
+ expr, ok := exprByColumn[replaced[i].Name]
+ if !ok {
+ continue
+ }
+ sourceColumns, err := q.collectSourceColumnsFromExpr(expr)
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to resolve star REPLACE expression for column %q", replaced[i].Name)
+ }
+ replaced[i].SourceColumns = sourceColumns
+ replaced[i].IsPlainField = false
+ }
+ return replaced, nil
}
diff --git a/backend/plugin/parser/snowflake/query_span_test.go b/backend/plugin/parser/snowflake/query_span_test.go
index da87e13b98..55438d79a3 100644
--- a/backend/plugin/parser/snowflake/query_span_test.go
+++ b/backend/plugin/parser/snowflake/query_span_test.go
@@ -35,7 +35,16 @@ func TestGetQuerySpan(t *testing.T) {
"test-data/query-span/standard.yaml",
"test-data/query-span/subquery.yaml",
"test-data/query-span/set-operator.yaml",
- "test-data/query-span/pivot.yaml",
+ // TODO: re-enable once pivot lineage is implemented on the omni AST.
+ // omni models PIVOT/UNPIVOT on ast.TableRef (Pivot/Unpivot/Nested), but
+ // this extractor does not compute the pivoted projection yet — it FAILS
+ // CLOSED instead (see extractTableSourceFromTableRef and
+ // TestGetQuerySpan_PivotFailsClosed): GetQuerySpan returns an explicit
+ // error for pivoted table sources rather than silently resolving the
+ // bare base table with wrong lineage. The expected lineage assertions
+ // are preserved in pivot.yaml for when pivot projection lands. Tracked
+ // in the migration ledger.
+ // "test-data/query-span/pivot.yaml",
"test-data/query-span/cte.yaml",
}
)
@@ -96,3 +105,40 @@ func buildMockDatabaseMetadataGetter(databaseMetadata []*storepb.DatabaseSchemaM
return names, nil
}
}
+
+// TestGetQuerySpan_PivotFailsClosed locks the PIVOT/UNPIVOT fail-closed
+// behavior: until pivot projection lineage is implemented, GetQuerySpan must
+// return an explicit error for pivoted table sources — never silently resolve
+// the bare base table (which would yield wrong lineage/positions for masking).
+func TestGetQuerySpan_PivotFailsClosed(t *testing.T) {
+ a := require.New(t)
+ metadata := &storepb.DatabaseSchemaMetadata{Name: "DB1"}
+ databaseMetadataGetter, databaseNameLister := buildMockDatabaseMetadataGetter([]*storepb.DatabaseSchemaMetadata{metadata})
+ for _, sql := range []string{
+ `SELECT * FROM monthly_sales PIVOT (SUM(amount) FOR month IN ('JAN', 'FEB')) AS p;`,
+ `SELECT * FROM monthly_sales UNPIVOT (sales FOR month IN (jan, feb)) AS u;`,
+ } {
+ _, err := GetQuerySpan(context.TODO(), base.GetQuerySpanContext{
+ GetDatabaseMetadataFunc: databaseMetadataGetter,
+ ListDatabaseNamesFunc: databaseNameLister,
+ }, base.Statement{Text: sql}, "DB1", "PUBLIC", false)
+ a.Errorf(err, "expected fail-closed error, statement: %s", sql)
+ a.Containsf(err.Error(), "PIVOT", "statement: %s", sql)
+ }
+}
+
+// TestGetQuerySpan_ShowPipeFailsClosed locks the fail-closed posture for
+// SHOW result-pipes: the trailing query reads $1 (unresolvable schema), so
+// span extraction errors explicitly instead of resolving wrong lineage or
+// passing as metadata-only.
+func TestGetQuerySpan_ShowPipeFailsClosed(t *testing.T) {
+ a := require.New(t)
+ metadata := &storepb.DatabaseSchemaMetadata{Name: "DB1"}
+ databaseMetadataGetter, databaseNameLister := buildMockDatabaseMetadataGetter([]*storepb.DatabaseSchemaMetadata{metadata})
+ _, err := GetQuerySpan(context.TODO(), base.GetQuerySpanContext{
+ GetDatabaseMetadataFunc: databaseMetadataGetter,
+ ListDatabaseNamesFunc: databaseNameLister,
+ }, base.Statement{Text: `SHOW TABLES ->> SELECT * FROM SENSITIVE_T;`}, "DB1", "PUBLIC", false)
+ a.Error(err)
+ a.Contains(err.Error(), "result-pipe")
+}
diff --git a/backend/plugin/parser/snowflake/query_test.go b/backend/plugin/parser/snowflake/query_test.go
index d42067ae5e..65ab8730ae 100644
--- a/backend/plugin/parser/snowflake/query_test.go
+++ b/backend/plugin/parser/snowflake/query_test.go
@@ -4,6 +4,10 @@ import (
"testing"
"github.com/stretchr/testify/require"
+
+ "github.com/bytebase/omni/snowflake/parser"
+
+ "github.com/bytebase/bytebase/backend/plugin/parser/base"
)
func TestValidateSQLForEditor(t *testing.T) {
@@ -18,6 +22,24 @@ func TestValidateSQLForEditor(t *testing.T) {
valid: true,
gotAllQuery: true,
},
+ {
+ // Result-pipe: read-only source + trailing SELECT over $1 is a query.
+ statement: "SHOW TABLES ->> SELECT * FROM $1;",
+ valid: true,
+ gotAllQuery: true,
+ },
+ {
+ // Result-pipe with a non-read-only source must not classify as a query.
+ statement: "CALL P() ->> SELECT * FROM $1;",
+ valid: false,
+ gotAllQuery: false,
+ },
+ {
+ // A non-read-only statement piped FROM a SHOW must not classify as a query.
+ statement: "SHOW TABLES ->> DELETE FROM T1;",
+ valid: false,
+ gotAllQuery: false,
+ },
{
statement: "DESC TABLE bytebase;",
valid: true,
@@ -81,3 +103,66 @@ func TestValidateSQLForEditor(t *testing.T) {
}
}
}
+
+// TestGetQueryType_CallExecuteAreDML locks the legacy classification of
+// CALL / EXECUTE IMMEDIATE / EXECUTE TASK as DML (procedures can mutate data;
+// the DDL fallback would map them to the wrong ACL bucket).
+func TestGetQueryType_CallExecuteAreDML(t *testing.T) {
+ for _, sql := range []string{
+ "CALL P(1);",
+ "EXECUTE IMMEDIATE 'SELECT 1';",
+ "EXECUTE TASK T1;",
+ } {
+ file, err := parser.Parse(sql)
+ if err != nil {
+ t.Fatalf("parse %q: %v", sql, err)
+ }
+ if got := getQueryType(file.Stmts[0]); got != base.DML {
+ t.Fatalf("getQueryType(%q) = %v, want base.DML", sql, got)
+ }
+ }
+}
+
+// TestGetQueryType_UnmappedStaysUnknown locks the fail-closed fallback: parsed
+// statements the classifier does not deliberately map (legacy left them
+// QueryTypeUnknown, which the SQL service denies) must NOT default to DDL.
+func TestGetQueryType_UnmappedStaysUnknown(t *testing.T) {
+ for _, tc := range []struct {
+ sql string
+ want base.QueryType
+ }{
+ {"TRUNCATE TABLE T1;", base.QueryTypeUnknown},
+ {"GRANT SELECT ON TABLE T1 TO ROLE R1;", base.QueryTypeUnknown},
+ {"CREATE TABLE T1 (A INT);", base.DDL},
+ {"ALTER TABLE T1 ADD COLUMN B INT;", base.DDL},
+ {"DROP TABLE T1;", base.DDL},
+ } {
+ file, err := parser.Parse(tc.sql)
+ if err != nil {
+ t.Fatalf("parse %q: %v", tc.sql, err)
+ }
+ if got := getQueryType(file.Stmts[0]); got != tc.want {
+ t.Fatalf("getQueryType(%q) = %v, want %v", tc.sql, got, tc.want)
+ }
+ }
+}
+
+// TestGetQueryType_ShowPipeClassifiesByQuery locks that SHOW ... ->> <query>
+// classifies as the piped query's type (so the trailing SELECT is
+// permission-checked and masked as a query, not as info-schema metadata).
+func TestGetQueryType_ShowPipeClassifiesByQuery(t *testing.T) {
+ file, err := parser.Parse("SHOW TABLES ->> SELECT * FROM SENSITIVE_T;")
+ if err != nil {
+ t.Fatal(err)
+ }
+ if got := getQueryType(file.Stmts[0]); got != base.Select {
+ t.Fatalf("got %v, want base.Select", got)
+ }
+ file, err = parser.Parse("SHOW TABLES;")
+ if err != nil {
+ t.Fatal(err)
+ }
+ if got := getQueryType(file.Stmts[0]); got != base.SelectInfoSchema {
+ t.Fatalf("got %v, want base.SelectInfoSchema", got)
+ }
+}
diff --git a/backend/plugin/parser/snowflake/query_type.go b/backend/plugin/parser/snowflake/query_type.go
index 04927e88b2..bee7545f3c 100644
--- a/backend/plugin/parser/snowflake/query_type.go
+++ b/backend/plugin/parser/snowflake/query_type.go
@@ -1,78 +1,114 @@
package snowflake
import (
- parser "github.com/bytebase/parser/snowflake"
+ "strings"
+
+ "github.com/bytebase/omni/snowflake/ast"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
)
-type queryTypeListener struct {
- *parser.BaseSnowflakeParserListener
+// getQueryType classifies a single parsed Snowflake statement into a
+// base.QueryType. It mirrors the legacy ANTLR listener's
+// getQueryTypeForBatch/getQueryTypeForDmlCommand/getQueryTypeForOtherCommand
+// (which switched on the sql_command / dml_command / other_command grammar
+// branches) onto a type-switch over omni's AST nodes.
+//
+// Mapping (legacy branch → omni node → base.QueryType):
+// - dml_command query_statement (SELECT / set-op) → *ast.SelectStmt,
+// *ast.SetOperationStmt → base.Select
+// - dml_command (INSERT/UPDATE/DELETE/MERGE) → *ast.InsertStmt,
+// *ast.InsertMultiStmt, *ast.UpdateStmt,
+// *ast.DeleteStmt, *ast.MergeStmt → base.DML
+// - show_command → *ast.ShowStmt
+// describe_command → *ast.DescribeStmt → base.SelectInfoSchema
+// - use_command → *ast.UseStmt → base.Select
+// - other_command set → *ast.SetStmt → base.Select
+// - other_command copy_into_table → *ast.CopyIntoTableStmt → base.DML
+// - other_command comment → *ast.CommentStmt → base.DDL
+// - ddl_command (CREATE/ALTER/DROP/UNDROP/...) → the *ast.*Stmt set → base.DDL
+//
+// CALL / EXECUTE IMMEDIATE / EXECUTE TASK classify base.DML and EXPLAIN
+// classifies base.Explain, matching the legacy other_command branches.
+//
+// CREATE/ALTER/DROP/UNDROP statements classify base.DDL (the legacy
+// ddl_command branch), matched by node-tag prefix. Everything else (the
+// other_command forms the legacy listener left as base.QueryTypeUnknown —
+// TRUNCATE, GRANT/REVOKE, COMMIT/ROLLBACK, PUT/GET, LIST/REMOVE, UNSET,
+// scripting blocks, ...) stays base.QueryTypeUnknown, which the SQL service
+// DENIES before execution: fail closed, exactly as legacy. A blanket DDL
+// fallback would let bb.sql.ddl holders run account/stage administration
+// commands that were never deliberately mapped.
+//
+// A nil node yields base.QueryTypeUnknown.
+func getQueryType(node ast.Node) base.QueryType {
+ if node == nil {
+ return base.QueryTypeUnknown
+ }
+ switch n := node.(type) {
+ // SELECT / set operations.
+ case *ast.SelectStmt, *ast.SetOperationStmt:
+ return base.Select
- allSystems bool
- result base.QueryType
- err error
-}
+ // DML: INSERT (single + multi-table), UPDATE, DELETE, MERGE. The legacy
+ // listener also classified CALL / EXECUTE IMMEDIATE / EXECUTE TASK as DML
+ // (other_command branches) — a stored procedure can mutate data, so DML is
+ // the right ACL bucket, not the DDL fallback.
+ case *ast.InsertStmt, *ast.InsertMultiStmt,
+ *ast.UpdateStmt, *ast.DeleteStmt, *ast.MergeStmt,
+ *ast.CallStmt, *ast.ExecuteImmediateStmt, *ast.ExecuteTaskStmt:
+ return base.DML
-func (l *queryTypeListener) EnterBatch(ctx *parser.BatchContext) {
- if l.err != nil {
- return
- }
+ // SHOW / DESCRIBE read system metadata.
+ case *ast.ResultScanStmt:
+ // stmt ->> query: the result shape is the trailing query's (typically a
+ // SELECT over $1). Read-only-ness of the SOURCE is enforced separately
+ // in classifyForEditor.
+ return getQueryType(n.Query)
+ case *ast.ExplainStmt:
+ // EXPLAIN is read-only and data-returning regardless of the inner
+ // statement (legacy other_command.explain never recursed into it).
+ return base.Explain
+ case *ast.ShowStmt:
+ // A SHOW with a result-pipe (SHOW ... ->> <query>) produces the piped
+ // query's result — classify by it, so the trailing SELECT is
+ // permission-checked/masked as a query instead of hiding behind
+ // info-schema-only access.
+ if n.Pipe != nil {
+ return getQueryType(n.Pipe)
+ }
+ return base.SelectInfoSchema
+ case *ast.DescribeStmt:
+ return base.SelectInfoSchema
- l.result, l.err = l.getQueryTypeForBatch(ctx)
-}
+ // USE is a session-scoped no-op for read purposes; the legacy listener
+ // classified use_command as base.Select.
+ case *ast.UseStmt:
+ return base.Select
-func (l *queryTypeListener) getQueryTypeForBatch(batch parser.IBatchContext) (base.QueryType, error) {
- sqlCommands := batch.AllSql_command()
- if len(sqlCommands) == 0 {
- return base.QueryTypeUnknown, nil
- }
- sqlCommand := sqlCommands[0]
- switch {
- case sqlCommand.Ddl_command() != nil:
- return base.DDL, nil
- case sqlCommand.Dml_command() != nil:
- return l.getQueryTypeForDmlCommand(sqlCommand.Dml_command())
- case sqlCommand.Show_command() != nil:
- return base.SelectInfoSchema, nil
- case sqlCommand.Use_command() != nil:
- return base.Select, nil
- case sqlCommand.Describe_command() != nil:
- return base.SelectInfoSchema, nil
- case sqlCommand.Other_command() != nil:
- return l.getQueryTypeForOtherCommand(sqlCommand.Other_command())
- default:
- return base.QueryTypeUnknown, nil
+ // SET (session variable) was classified base.Select by the legacy listener.
+ case *ast.SetStmt:
+ return base.Select
+
+ // COPY INTO <table> loads data, so the legacy listener classified it DML.
+ case *ast.CopyIntoTableStmt:
+ return base.DML
+
+ // COMMENT ON ... is DDL.
+ case *ast.CommentStmt:
+ return base.DDL
}
-}
-func (l *queryTypeListener) getQueryTypeForDmlCommand(dmlCommand parser.IDml_commandContext) (base.QueryType, error) {
- switch {
- case dmlCommand.Query_statement() != nil:
- if l.allSystems {
- return base.SelectInfoSchema, nil
+ // CREATE / ALTER / DROP / UNDROP — the legacy ddl_command set — classify by
+ // node-tag prefix (every omni DDL node is named Create*/Alter*/Drop*/Undrop*).
+ tag := node.Tag().String()
+ for _, prefix := range []string{"Create", "Alter", "Drop", "Undrop"} {
+ if strings.HasPrefix(tag, prefix) {
+ return base.DDL
}
- return base.Select, nil
- default:
- return base.DML, nil
}
-}
-func (*queryTypeListener) getQueryTypeForOtherCommand(otherCommand parser.IOther_commandContext) (base.QueryType, error) {
- switch {
- case otherCommand.Copy_into_table() != nil:
- return base.DML, nil
- case otherCommand.Comment() != nil:
- return base.DDL, nil
- case otherCommand.Set() != nil:
- return base.Select, nil
- case otherCommand.Call() != nil:
- return base.DML, nil
- case otherCommand.Execute_immediate() != nil:
- return base.DML, nil
- case otherCommand.Execute_task() != nil:
- return base.DML, nil
- default:
- return base.QueryTypeUnknown, nil
- }
+ // Anything not deliberately mapped stays Unknown — the SQL service denies
+ // it before execution (fail closed, legacy parity).
+ return base.QueryTypeUnknown
}
diff --git a/backend/plugin/parser/snowflake/snowflake.go b/backend/plugin/parser/snowflake/snowflake.go
index 877102a558..ce7a808458 100644
--- a/backend/plugin/parser/snowflake/snowflake.go
+++ b/backend/plugin/parser/snowflake/snowflake.go
@@ -2,14 +2,12 @@ package snowflake
import (
"strings"
- "unicode"
- "github.com/antlr4-go/antlr/v4"
- parser "github.com/bytebase/parser/snowflake"
+ omniast "github.com/bytebase/omni/snowflake/ast"
+ omniparser "github.com/bytebase/omni/snowflake/parser"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
- "github.com/bytebase/bytebase/backend/utils"
)
func init() {
@@ -18,6 +16,14 @@ func init() {
// parseSnowflakeStatements is the ParseStatementsFunc for Snowflake.
// Returns []ParsedStatement with both text and AST populated.
+//
+// Each non-empty statement is parsed with the omni parser (the legacy ANTLR
+// parser is gone). A parse failure on any statement fails the whole batch
+// with a *base.SyntaxError — exactly the contract the legacy path had, so
+// callers (e.g. the sheet manager) keep surfacing syntax errors unchanged.
+// The omni side is parsed PER STATEMENT (stmt.Text) rather than from the
+// whole input, so positions inside one statement stay relative to it and are
+// offset by the statement's start position afterwards.
func parseSnowflakeStatements(statement string) ([]base.ParsedStatement, error) {
// First split to get Statement with text and positions
stmts, err := SplitSQL(statement)
@@ -25,22 +31,23 @@ func parseSnowflakeStatements(statement string) ([]base.ParsedStatement, error)
return nil, err
}
- // Then parse to get ASTs
- parseResults, err := ParseSnowSQL(statement)
- if err != nil {
- return nil, err
- }
-
- // Combine: Statement provides text/positions, ANTLRAST provides AST
var result []base.ParsedStatement
- astIndex := 0
for _, stmt := range stmts {
ps := base.ParsedStatement{
Statement: stmt,
}
- if !stmt.Empty && astIndex < len(parseResults) {
- ps.AST = parseResults[astIndex]
- astIndex++
+ if !stmt.Empty {
+ node, err := parseOmniStatementNode(stmt.Text)
+ if err != nil {
+ return nil, convertOmniParseError(err, stmt)
+ }
+ ps.AST = &OmniAST{
+ Node: node,
+ Text: stmt.Text,
+ // Keep the legacy ASTStartPosition shape: 1-based line of the
+ // statement start, no column component.
+ StartPosition: &storepb.Position{Line: int32(stmt.BaseLine()) + 1},
+ }
}
result = append(result, ps)
}
@@ -48,176 +55,65 @@ func parseSnowflakeStatements(statement string) ([]base.ParsedStatement, error)
return result, nil
}
-// ParseSnowSQL parses the given SQL and returns a list of ANTLRAST (one per statement).
-// Use the Snowflake parser based on antlr4.
-func ParseSnowSQL(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 := parseSingleSnowSQL(stmt.Text, stmt.BaseLine())
- if err != nil {
- return nil, err
- }
- results = append(results, parseResult)
- }
-
- return results, nil
-}
-
-// parseSingleSnowSQL parses a single Snowflake statement and returns the ANTLRAST.
-func parseSingleSnowSQL(statement string, baseLine int) (*base.ANTLRAST, error) {
- statement = strings.TrimRightFunc(statement, utils.IsSpaceOrSemicolon) + "\n;"
- inputStream := antlr.NewInputStream(statement)
- lexer := parser.NewSnowflakeLexer(inputStream)
- stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
- p := parser.NewSnowflakeParser(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.Snowflake_file()
-
- if lexerErrorListener.Err != nil {
- return nil, lexerErrorListener.Err
- }
-
- if parserErrorListener.Err != nil {
- return nil, parserErrorListener.Err
- }
-
- result := &base.ANTLRAST{
- StartPosition: &storepb.Position{Line: int32(baseLine) + 1},
- Tree: tree,
- Tokens: stream,
- }
-
- return result, nil
-}
-
-// NormalizeSnowSQLObjectNamePart normalizes the object name part.
-func NormalizeSnowSQLObjectNamePart(part parser.IId_Context) string {
- if part == nil {
- return ""
+// IsSnowflakeKeyword returns true if the given string is a snowflake keyword.
+// Follows https://docs.snowflake.com/en/sql-reference/reserved-keywords.
+func IsSnowflakeKeyword(s string, caseSensitive bool) bool {
+ if !caseSensitive {
+ s = strings.ToUpper(s)
}
- return ExtractSnowSQLOrdinaryIdentifier(part.GetText())
+ return snowflakeKeyword[s]
}
-// ExtractSnowSQLOrdinaryIdentifier extracts the ordinary object name from a string. It follows the following rules:
+// ---------------------------------------------------------------------------
+// omni-AST-based normalization helpers
//
-// 1. If there are no double quotes on either side, it will be converted to uppercase.
-//
-// 2. If there are double quotes on both sides, the case will not change, the double quotes on both sides will be removed, and `""` in content will be converted to `"`.
-//
-// Caller MUST ensure the identifier is VALID.
-func ExtractSnowSQLOrdinaryIdentifier(identifier string) string {
- quoted := strings.HasPrefix(identifier, `"`) && strings.HasSuffix(identifier, `"`)
- if quoted {
- identifier = identifier[1 : len(identifier)-1]
- }
- runeObjectName := []rune(identifier)
- var result []rune
- for i := 0; i < len(runeObjectName); i++ {
- newRune := runeObjectName[i]
- if i+1 < len(runeObjectName) && runeObjectName[i] == '"' && runeObjectName[i+1] == '"' && quoted {
- newRune = '"'
- i++
- } else if !quoted {
- newRune = unicode.ToUpper(newRune)
- }
- result = append(result, newRune)
- }
- return string(result)
+// These replace the deleted legacy ANTLR helpers (NormalizeSnowSQLObjectName /
+// NormalizeSnowSQLObjectNamePart / NormalizeSnowSQLSchemaName), operating on
+// omni's hand-written AST nodes (*omniast.ObjectName, omniast.Ident) instead of
+// ANTLR contexts while applying the SAME Snowflake identifier-folding rules
+// (unquoted → uppercase, quoted → verbatim). They are the shared base the
+// query-span extractor and the advisors build on.
+// ---------------------------------------------------------------------------
+
+// parseSnowflakeAST parses a single Snowflake statement with the omni parser and
+// returns the resulting *ast.File. It mirrors trino.parseTrinoSQL: a thin
+// wrapper over omniparser.Parse so call sites share one parse entry point.
+func parseSnowflakeAST(statement string) (*omniast.File, error) {
+ return omniparser.Parse(statement)
}
-// NormalizeSnowSQLSchemaName normalizes the given schema name.
-func NormalizeSnowSQLSchemaName(schemaName parser.ISchema_nameContext, fallbackDatabaseName string) string {
- ids := schemaName.AllId_()
-
- var parts []string
- database := fallbackDatabaseName
- if len(ids) == 2 {
- normalizedD := NormalizeSnowSQLObjectNamePart(ids[0])
- if normalizedD != "" {
- database = normalizedD
- }
- }
- parts = append(parts, database)
-
- var schema string
- if len(ids) == 2 {
- normalizedS := NormalizeSnowSQLObjectNamePart(ids[1])
- if normalizedS != "" {
- schema = normalizedS
- }
- } else {
- normalizedS := NormalizeSnowSQLObjectNamePart(ids[0])
- if normalizedS != "" {
- schema = normalizedS
- }
+// normalizeSnowflakeIdentifier returns the canonical (folded) form of an omni
+// Ident, applying the Snowflake identifier-folding rules: an unquoted identifier
+// is upper-cased; a quoted identifier is returned verbatim (case preserved, the
+// surrounding quotes already stripped by the lexer). It delegates to omni's own
+// Ident.Normalize so the folding stays in lock-step with the parser.
+func normalizeSnowflakeIdentifier(id omniast.Ident) string {
+ if id.IsEmpty() {
+ return ""
}
- parts = append(parts, schema)
- return strings.Join(parts, ".")
+ return id.Normalize()
}
-// NormalizeSnowSQLObjectName normalizes the given object name.
-func NormalizeSnowSQLObjectName(objectName parser.IObject_nameContext, fallbackDatabaseName, fallbackSchemaName string) string {
- var parts []string
-
- database := fallbackDatabaseName
- if d := objectName.GetD(); d != nil {
- normalizedD := NormalizeSnowSQLObjectNamePart(d)
- if normalizedD != "" {
- database = normalizedD
- }
+// normalizeSnowflakeObjectName splits an omni *ObjectName into its normalized
+// (database, schema, table) parts, filling in the supplied fallbacks for any
+// part the name omits. It is the omni-AST analog of the extractor's
+// normalizedObjectName helper (which took parser.IObject_nameContext). An empty
+// part in the source leaves the corresponding fallback in place.
+func normalizeSnowflakeObjectName(objectName *omniast.ObjectName, fallbackDatabaseName, fallbackSchemaName string) (database, schema, table string) {
+ if objectName == nil {
+ return "", "", ""
}
- parts = append(parts, database)
- schema := fallbackSchemaName
- if s := objectName.GetS(); s != nil {
- normalizedS := NormalizeSnowSQLObjectNamePart(s)
- if normalizedS != "" {
- schema = normalizedS
- }
+ database = fallbackDatabaseName
+ if d := normalizeSnowflakeIdentifier(objectName.Database); d != "" {
+ database = d
}
- parts = append(parts, schema)
- if o := objectName.GetO(); o != nil {
- normalizedO := NormalizeSnowSQLObjectNamePart(o)
- if normalizedO != "" {
- parts = append(parts, normalizedO)
- }
+ schema = fallbackSchemaName
+ if s := normalizeSnowflakeIdentifier(objectName.Schema); s != "" {
+ schema = s
}
- return strings.Join(parts, ".")
-}
-// IsSnowflakeKeyword returns true if the given string is a snowflake keyword.
-// Follows https://docs.snowflake.com/en/sql-reference/reserved-keywords.
-func IsSnowflakeKeyword(s string, caseSensitive bool) bool {
- if !caseSensitive {
- s = strings.ToUpper(s)
- }
- return snowflakeKeyword[s]
+ table = normalizeSnowflakeIdentifier(objectName.Name)
+ return database, schema, table
}
diff --git a/backend/plugin/parser/snowflake/snowflake_test.go b/backend/plugin/parser/snowflake/snowflake_test.go
index 8dded327b4..bb5bd1761e 100644
--- a/backend/plugin/parser/snowflake/snowflake_test.go
+++ b/backend/plugin/parser/snowflake/snowflake_test.go
@@ -4,66 +4,51 @@ import (
"testing"
"github.com/stretchr/testify/require"
+
+ "github.com/bytebase/bytebase/backend/plugin/parser/base"
)
-func TestSnowSqlExtractOrdinaryIdentifier(t *testing.T) {
- testCases := []struct {
- description string
- name string
- want string
- }{
- {
- description: "Should convert object name to uppercase if it is not quoted",
- name: `table_name`,
- want: `TABLE_NAME`,
- },
- {
- description: "Should **NOT** convert object name to uppercase if it is quoted",
- name: `"table_name"`,
- want: `table_name`,
- },
- {
- description: `Should convert '""' to '"' if it is quoted`,
- name: `"table_name"""`,
- want: `table_name"`,
- },
- {
- description: `Should be fine with unicode characters`,
- name: `"😈😄"""`,
- want: `😈😄"`,
- },
- }
- for _, tc := range testCases {
- t.Run(tc.name, func(t *testing.T) {
- got := ExtractSnowSQLOrdinaryIdentifier(tc.name)
- require.Equal(t, tc.want, got, tc.description)
- })
+func TestParseSnowflakeStatements(t *testing.T) {
+ stmts, err := parseSnowflakeStatements("SELECT 1;\n\nSELECT 2 FROM t;")
+ require.NoError(t, err)
+ require.Len(t, stmts, 2)
+ for i, stmt := range stmts {
+ require.False(t, stmt.Empty, i)
+ require.NotNil(t, stmt.AST, i)
+ node, ok := GetOmniNode(stmt.AST)
+ require.True(t, ok, i)
+ require.NotNil(t, node, i)
+ // ASTStartPosition keeps the legacy shape: BaseLine()+1, no column.
+ require.Equal(t, int32(stmt.BaseLine())+1, stmt.AST.ASTStartPosition().Line, i)
}
}
-func TestParseSnowSQL(t *testing.T) {
- testCase := []struct {
+func TestParseSnowflakeStatementsSyntaxError(t *testing.T) {
+ testCases := []struct {
sql string
- err string
+ // 1-based line the syntax error is reported on, relative to the whole
+ // multi-statement input.
+ line int32
}{
{
- sql: `SELECT t.a, t.b FRO table_name t;`,
- err: "Syntax error at line 1:1 \nrelated text: SELECT",
+ sql: `SELEC 5;`,
+ line: 1,
},
{
- sql: "SELECT 1;\n SELEC 5;\nSELECT 6;",
- // After standardization, each statement is parsed separately, so the error context
- // only includes the current statement being parsed (not previous statements).
- err: "Syntax error at line 2:4 \nrelated text: \n SELEC",
+ // The error position is offset by the failing statement's start
+ // line in the original input.
+ sql: "SELECT 1;\n SELEC 5;\nSELECT 6;",
+ line: 2,
},
}
- for _, tc := range testCase {
- _, err := ParseSnowSQL(tc.sql)
- if tc.err != "" {
- require.EqualError(t, err, tc.err, tc.sql)
- } else {
- require.NoError(t, err, tc.sql)
- }
+ for _, tc := range testCases {
+ _, err := parseSnowflakeStatements(tc.sql)
+ require.Error(t, err, tc.sql)
+ syntaxErr, ok := err.(*base.SyntaxError)
+ require.True(t, ok, "expected *base.SyntaxError, got %T: %v", err, err)
+ require.NotNil(t, syntaxErr.Position, tc.sql)
+ require.Equal(t, tc.line, syntaxErr.Position.Line, tc.sql)
+ require.Contains(t, syntaxErr.Message, "Syntax error at line", tc.sql)
}
}
diff --git a/backend/plugin/parser/snowflake/split.go b/backend/plugin/parser/snowflake/split.go
index 5fbb47de13..9c0d3bb619 100644
--- a/backend/plugin/parser/snowflake/split.go
+++ b/backend/plugin/parser/snowflake/split.go
@@ -1,8 +1,7 @@
package snowflake
import (
- "github.com/antlr4-go/antlr/v4"
- parser "github.com/bytebase/parser/snowflake"
+ "github.com/bytebase/omni/snowflake/parser"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
@@ -12,11 +11,39 @@ func init() {
base.RegisterSplitterFunc(storepb.Engine_SNOWFLAKE, SplitSQL)
}
-// SplitSQL splits the given SQL statement into multiple SQL statements using ANTLR lexer.
+// SplitSQL splits the input into multiple SQL statements using the omni Snowflake
+// splitter, then converts each Segment into a base.Statement with the position
+// fields bytebase expects. Positions are computed in a single O(n) pass via the
+// shared base.ByteOffsetPositionMapper (segment offsets are queried in
+// increasing order).
func SplitSQL(statement string) ([]base.Statement, error) {
- lexer := parser.NewSnowflakeLexer(antlr.NewInputStream(statement))
- stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
- stream.Fill()
+ segs := parser.Split(statement)
+ if len(segs) == 0 {
+ return nil, nil
+ }
- return base.SplitSQLByLexer(stream, parser.SnowflakeLexerSEMI)
+ mapper := base.NewByteOffsetPositionMapper(statement)
+ stmts := make([]base.Statement, 0, len(segs))
+ for _, seg := range segs {
+ // bytebase historically returns Text including the trailing ';' delimiter
+ // (when present). omni's Segment.Text excludes it, so we re-attach when
+ // the byte after ByteEnd is ';'.
+ text := seg.Text
+ end := seg.ByteEnd
+ if end < len(statement) && statement[end] == ';' {
+ text = statement[seg.ByteStart : end+1]
+ end++
+ }
+ stmts = append(stmts, base.Statement{
+ Text: text,
+ Empty: seg.Empty(),
+ Start: mapper.Position(seg.ByteStart),
+ End: mapper.Position(end),
+ Range: &storepb.Range{
+ Start: int32(seg.ByteStart),
+ End: int32(end),
+ },
+ })
+ }
+ return stmts, nil
}
diff --git a/backend/plugin/parser/snowflake/split_empty_test.go b/backend/plugin/parser/snowflake/split_empty_test.go
new file mode 100644
index 0000000000..6b26c84d0f
--- /dev/null
+++ b/backend/plugin/parser/snowflake/split_empty_test.go
@@ -0,0 +1,37 @@
+package snowflake
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+// TestSplitSQL_EmptyStatementsDropped locks the omni-vs-legacy divergence:
+// the legacy ANTLR splitter emitted bare ";" as a (non-empty!) statement, which
+// the downstream skip guard `stmt.Empty || TrimSpace(Text)==""` would NOT skip
+// (";" is neither empty nor blank) — i.e. it spuriously processed a bare
+// semicolon as a real statement. omni's parser.Split produces no empty segments,
+// so SplitSQL now drops them. Verified that every consumer skips empty/blank
+// statements (trino.go, snowflake.go, redshift, doris, bigquery, tsql, ...), so
+// nothing depends on the dropped statements. omni's behavior also matches MySQL
+// and the merged Trino omni cutover.
+func TestSplitSQL_EmptyStatementsDropped(t *testing.T) {
+ cases := []struct {
+ in string
+ want int
+ }{
+ {"SELECT 1;;SELECT 2;", 2},
+ {"SELECT 1; ;SELECT 2;", 2},
+ {";", 0},
+ {";;", 0},
+ {"SELECT 1;", 1},
+ }
+ for _, c := range cases {
+ got, err := SplitSQL(c.in)
+ require.NoError(t, err, c.in)
+ require.Len(t, got, c.want, "input %q", c.in)
+ for i, st := range got {
+ require.NotEmpty(t, st.Text, "stmt %d of %q should be a real statement", i, c.in)
+ }
+ }
+}
diff --git a/backend/plugin/parser/snowflake/statement_ranges.go b/backend/plugin/parser/snowflake/statement_ranges.go
index f574f16464..3ebe906e98 100644
--- a/backend/plugin/parser/snowflake/statement_ranges.go
+++ b/backend/plugin/parser/snowflake/statement_ranges.go
@@ -2,25 +2,201 @@ package snowflake
import (
"context"
+ "strings"
+ "unicode"
+ "unicode/utf8"
- "github.com/antlr4-go/antlr/v4"
-
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
-
- parser "github.com/bytebase/parser/snowflake"
+ protocol "github.com/bytebase/lsp-protocol"
+ "github.com/bytebase/omni/snowflake/parser"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/plugin/parser/base"
)
func init() {
base.RegisterStatementRangesFunc(storepb.Engine_SNOWFLAKE, GetStatementRanges)
}
+// GetStatementRanges returns the UTF-16 line/character ranges of statements in
+// the input, formatted for the LSP protocol. It is backed by the omni Snowflake
+// splitter (parser.Split), the same splitter SplitSQL uses, so the emitted
+// ranges line up byte-for-byte with the segments SplitSQL returns.
+//
+// Per segment the range:
+// - starts at the first non-whitespace byte (leading whitespace is trimmed,
+// but leading comments are kept inside the range — matching the prior
+// ANTLR helper, which folded hidden-channel comment tokens into the
+// following statement while excluding pure whitespace);
+// - ends just past the trailing ';' delimiter when present (omni's
+// Segment.ByteEnd points AT the ';', so we step over it), consistent with
+// SplitSQL re-attaching the delimiter.
+//
+// omni's splitter drops comment-only / lone-';' chunks entirely (zero
+// segments). The legacy ANTLR helper still emitted a range for trailing
+// comment tokens, so comment-only input and trailing comment tails are
+// re-emitted here to keep editor features (which count statement positions)
+// from losing ground — see commentOnlyRanges and the trailing-tail block.
func GetStatementRanges(_ context.Context, _ base.StatementRangeContext, statement string) ([]base.Range, error) {
- createLexer := func(input antlr.CharStream) antlr.Lexer {
- return parser.NewSnowflakeLexer(input)
+ trimmed := strings.TrimRightFunc(statement, unicode.IsSpace)
+
+ segs := parser.Split(trimmed)
+ if len(segs) == 0 {
+ // No SQL statements — typically comment-only input like `-- note` or
+ // `/* ... */`, which the legacy ANTLR helper emitted a range for.
+ return commentOnlyRanges(trimmed), nil
}
- stream := base.PrepareANTLRTokenStream(statement, createLexer)
- ranges := base.GetANTLRStatementRangesUTF16Position(stream, parser.SnowflakeParserEOF, parser.SnowflakeParserSEMI)
+
+ mapper := newUTF16Mapper(trimmed)
+
+ ranges := make([]base.Range, 0, len(segs))
+ for _, seg := range segs {
+ // Skip pure-whitespace segments (omni filters comment-only chunks, so
+ // any segment that reaches here with no non-whitespace byte is just
+ // whitespace and carries no editor-meaningful position).
+ if !hasNonWhitespace(trimmed[seg.ByteStart:seg.ByteEnd]) {
+ continue
+ }
+ // Trim only leading whitespace — leading comments stay inside the
+ // range to mirror the prior ANTLR helper.
+ start := seg.ByteStart
+ for start < seg.ByteEnd {
+ c := trimmed[start]
+ if c == ' ' || c == '\t' || c == '\n' || c == '\r' {
+ start++
+ continue
+ }
+ break
+ }
+ end := seg.ByteEnd
+ if end < len(trimmed) && trimmed[end] == ';' {
+ end++
+ }
+ ranges = append(ranges, base.Range{
+ Start: mapper.position(start),
+ End: mapper.position(end),
+ })
+ }
+
+ // Trailing comment-only content: omni's Split filters out comment-only
+ // segments, so any non-whitespace bytes between the last segment's end and
+ // the end of the trimmed input would otherwise be lost. The legacy ANTLR
+ // helper emitted a final range for these — replicate that.
+ last := segs[len(segs)-1]
+ tailStart := last.ByteEnd
+ if tailStart < len(trimmed) && trimmed[tailStart] == ';' {
+ tailStart++
+ }
+ for tailStart < len(trimmed) {
+ c := trimmed[tailStart]
+ if c == ' ' || c == '\t' || c == '\n' || c == '\r' {
+ tailStart++
+ continue
+ }
+ break
+ }
+ if tailStart < len(trimmed) {
+ ranges = append(ranges, base.Range{
+ Start: mapper.position(tailStart),
+ End: mapper.position(len(trimmed)),
+ })
+ }
+
return ranges, nil
}
+
+// commentOnlyRanges emits a single Range covering the non-whitespace span of
+// `trimmed`, or nil when nothing in `trimmed` looks like a comment. Used when
+// the splitter finds zero segments — typically that means comment-only input
+// like `-- note` or `/* ... */`, which the legacy ANTLR helper would have
+// emitted a range for. Pure delimiters (e.g. a single `;`) are NOT turned into
+// ranges; the legacy helper dropped single-terminator inputs.
+func commentOnlyRanges(trimmed string) []base.Range {
+ if !containsCommentMarker(trimmed) {
+ return nil
+ }
+ start := 0
+ for start < len(trimmed) {
+ c := trimmed[start]
+ if c == ' ' || c == '\t' || c == '\n' || c == '\r' {
+ start++
+ continue
+ }
+ break
+ }
+ if start >= len(trimmed) {
+ return nil
+ }
+ mapper := newUTF16Mapper(trimmed)
+ return []base.Range{{
+ Start: mapper.position(start),
+ End: mapper.position(len(trimmed)),
+ }}
+}
+
+// containsCommentMarker reports whether s contains one of the SQL comment
+// introducers (`--`, `//`, `/*`). Anything inside a string literal could
+// false-positive, but the splitter only routes us here when there are no
+// segments at all — i.e. no string literals to worry about.
+func containsCommentMarker(s string) bool {
+ for i := 0; i < len(s); i++ {
+ c := s[i]
+ if i+1 < len(s) {
+ if c == '-' && s[i+1] == '-' {
+ return true
+ }
+ if c == '/' && (s[i+1] == '*' || s[i+1] == '/') {
+ return true
+ }
+ }
+ }
+ return false
+}
+
+// hasNonWhitespace reports whether s contains any non-whitespace character.
+func hasNonWhitespace(s string) bool {
+ for i := 0; i < len(s); i++ {
+ c := s[i]
+ if c != ' ' && c != '\t' && c != '\n' && c != '\r' {
+ return true
+ }
+ }
+ return false
+}
+
+// utf16Mapper maps byte offsets in a source string to UTF-16 LSP positions
+// (0-based line, 0-based UTF-16 code-unit column) in a single O(n) precompute.
+type utf16Mapper struct {
+ positions []protocol.Position
+}
+
+func newUTF16Mapper(s string) *utf16Mapper {
+ positions := make([]protocol.Position, len(s)+1)
+ var line, character uint32
+ for i := 0; i < len(s); {
+ positions[i] = protocol.Position{Line: line, Character: character}
+ r, size := utf8.DecodeRuneInString(s[i:])
+ if r == '\n' {
+ line++
+ character = 0
+ } else if r > 0xFFFF {
+ character += 2 // UTF-16 surrogate pair
+ } else {
+ character++
+ }
+ i += size
+ }
+ positions[len(s)] = protocol.Position{Line: line, Character: character}
+ return &utf16Mapper{positions: positions}
+}
+
+// position returns the UTF-16 position at byteOffset. byteOffset is clamped to
+// [0, len(s)]; out-of-range offsets resolve to the nearest boundary.
+func (m *utf16Mapper) position(byteOffset int) protocol.Position {
+ if byteOffset < 0 {
+ byteOffset = 0
+ }
+ if byteOffset >= len(m.positions) {
+ byteOffset = len(m.positions) - 1
+ }
+ return m.positions[byteOffset]
+}
diff --git a/backend/plugin/parser/snowflake/test-data/query-span/set-operator.yaml b/backend/plugin/parser/snowflake/test-data/query-span/set-operator.yaml
index 31ca02e082..f4b6dfc597 100644
--- a/backend/plugin/parser/snowflake/test-data/query-span/set-operator.yaml
+++ b/backend/plugin/parser/snowflake/test-data/query-span/set-operator.yaml
@@ -112,3 +112,210 @@
table: T3
column: ""
predicatecolumns: []
+- description: Union all by name merges columns by name, not by position
+ statement: SELECT A, B FROM T1 UNION ALL BY NAME SELECT B, A FROM T2;
+ defaultDatabase: SNOWFLAKE
+ ignoreCaseSensitive: true
+ metadata: |-
+ {
+ "name": "SNOWFLAKE",
+ "schemas": [
+ {
+ "name": "PUBLIC",
+ "tables": [
+ {
+ "name": "T1",
+ "columns": [
+ {
+ "name": "A"
+ },
+ {
+ "name": "B"
+ }
+ ]
+ },
+ {
+ "name": "T2",
+ "columns": [
+ {
+ "name": "A"
+ },
+ {
+ "name": "B"
+ }
+ ]
+ }
+ ]
+ }
+ ]
+ }
+ querySpan:
+ type: 1
+ results:
+ - name: A
+ sourcecolumns:
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T1
+ column: A
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T2
+ column: A
+ isplainfield: true
+ sourcefieldpaths: []
+ selectasterisk: false
+ - name: B
+ sourcecolumns:
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T1
+ column: B
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T2
+ column: B
+ isplainfield: true
+ sourcefieldpaths: []
+ selectasterisk: false
+ sourcecolumns:
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T1
+ column: ""
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T2
+ column: ""
+ predicatecolumns: []
+- description: UNION ALL BY NAME inside a CTE merges by column name (not positionally)
+ statement: WITH C AS (SELECT A, B FROM T1 UNION ALL BY NAME SELECT B, A FROM T2) SELECT * FROM C;
+ defaultDatabase: SNOWFLAKE
+ ignoreCaseSensitive: true
+ metadata: |-
+ {
+ "name": "SNOWFLAKE",
+ "schemas": [
+ {
+ "name": "PUBLIC",
+ "tables": [
+ {
+ "name": "T1",
+ "columns": [
+ {"name": "A"},
+ {"name": "B"}
+ ]
+ },
+ {
+ "name": "T2",
+ "columns": [
+ {"name": "A"},
+ {"name": "B"}
+ ]
+ }
+ ]
+ }
+ ]
+ }
+ querySpan:
+ type: 1
+ results:
+ - name: A
+ sourcecolumns:
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T1
+ column: A
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T2
+ column: A
+ isplainfield: true
+ sourcefieldpaths: []
+ selectasterisk: false
+ - name: B
+ sourcecolumns:
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T1
+ column: B
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T2
+ column: B
+ isplainfield: true
+ sourcefieldpaths: []
+ selectasterisk: false
+ sourcecolumns:
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: C
+ column: ""
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T1
+ column: ""
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T2
+ column: ""
+ predicatecolumns: []
+- description: WITH CTEs stay visible to every set-operation operand (not just the left)
+ statement: WITH C AS (SELECT A FROM T1) SELECT A FROM C UNION SELECT A FROM C;
+ defaultDatabase: SNOWFLAKE
+ ignoreCaseSensitive: true
+ metadata: |-
+ {
+ "name": "SNOWFLAKE",
+ "schemas": [
+ {
+ "name": "PUBLIC",
+ "tables": [
+ {
+ "name": "T1",
+ "columns": [
+ {"name": "A"}
+ ]
+ }
+ ]
+ }
+ ]
+ }
+ querySpan:
+ type: 1
+ results:
+ - name: A
+ sourcecolumns:
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T1
+ column: A
+ isplainfield: true
+ sourcefieldpaths: []
+ selectasterisk: false
+ sourcecolumns:
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: C
+ column: ""
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T1
+ column: ""
+ predicatecolumns: []
diff --git a/backend/plugin/parser/snowflake/test-data/query-span/standard.yaml b/backend/plugin/parser/snowflake/test-data/query-span/standard.yaml
index 5842286703..0d068da041 100644
--- a/backend/plugin/parser/snowflake/test-data/query-span/standard.yaml
+++ b/backend/plugin/parser/snowflake/test-data/query-span/standard.yaml
@@ -358,6 +358,11 @@
schema: PUBLIC
table: T1
column: A
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T2
+ column: A
isplainfield: true
sourcefieldpaths: []
selectasterisk: false
@@ -368,6 +373,11 @@
schema: PUBLIC
table: T2
column: E
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T3
+ column: E
isplainfield: true
sourcefieldpaths: []
selectasterisk: false
@@ -542,3 +552,705 @@
table: T1
column: ""
predicatecolumns: []
+- description: CASE WHEN branches contribute lineage (omni walker does not descend into WhenClause)
+ statement: SELECT CASE WHEN A > 0 THEN B ELSE C END AS X FROM T1;
+ defaultDatabase: SNOWFLAKE
+ ignoreCaseSensitive: true
+ metadata: |-
+ {
+ "name": "SNOWFLAKE",
+ "schemas": [
+ {
+ "name": "PUBLIC",
+ "tables": [
+ {
+ "name": "T1",
+ "columns": [
+ {
+ "name": "A"
+ },
+ {
+ "name": "B"
+ },
+ {
+ "name": "C"
+ },
+ {
+ "name": "D"
+ }
+ ]
+ }
+ ]
+ }
+ ]
+ }
+ querySpan:
+ type: 1
+ results:
+ - name: X
+ sourcecolumns:
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T1
+ column: A
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T1
+ column: B
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T1
+ column: C
+ isplainfield: false
+ sourcefieldpaths: []
+ selectasterisk: false
+ sourcecolumns:
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T1
+ column: ""
+ predicatecolumns: []
+- description: Window function OVER (PARTITION BY / ORDER BY) contributes lineage (omni walker does not descend into WindowSpec)
+ statement: SELECT ROW_NUMBER() OVER (PARTITION BY C ORDER BY A) AS RN FROM T1;
+ defaultDatabase: SNOWFLAKE
+ ignoreCaseSensitive: true
+ metadata: |-
+ {
+ "name": "SNOWFLAKE",
+ "schemas": [
+ {
+ "name": "PUBLIC",
+ "tables": [
+ {
+ "name": "T1",
+ "columns": [
+ {
+ "name": "A"
+ },
+ {
+ "name": "B"
+ },
+ {
+ "name": "C"
+ },
+ {
+ "name": "D"
+ }
+ ]
+ }
+ ]
+ }
+ ]
+ }
+ querySpan:
+ type: 1
+ results:
+ - name: RN
+ sourcecolumns:
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T1
+ column: A
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T1
+ column: C
+ isplainfield: false
+ sourcefieldpaths: []
+ selectasterisk: false
+ sourcecolumns:
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T1
+ column: ""
+ predicatecolumns: []
+- description: Join using collapses the shared column once with merged lineage, using columns first, then left and right non-shared
+ statement: SELECT * FROM T1 JOIN T2 USING (K);
+ defaultDatabase: SNOWFLAKE
+ ignoreCaseSensitive: true
+ metadata: |-
+ {
+ "name": "SNOWFLAKE",
+ "schemas": [
+ {
+ "name": "PUBLIC",
+ "tables": [
+ {
+ "name": "T1",
+ "columns": [
+ {
+ "name": "A"
+ },
+ {
+ "name": "K"
+ }
+ ]
+ },
+ {
+ "name": "T2",
+ "columns": [
+ {
+ "name": "K"
+ },
+ {
+ "name": "B"
+ }
+ ]
+ }
+ ]
+ }
+ ]
+ }
+ querySpan:
+ type: 1
+ results:
+ - name: K
+ sourcecolumns:
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T1
+ column: K
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T2
+ column: K
+ isplainfield: true
+ sourcefieldpaths: []
+ selectasterisk: false
+ - name: A
+ sourcecolumns:
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T1
+ column: A
+ isplainfield: true
+ sourcefieldpaths: []
+ selectasterisk: false
+ - name: B
+ sourcecolumns:
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T2
+ column: B
+ isplainfield: true
+ sourcefieldpaths: []
+ selectasterisk: false
+ sourcecolumns:
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T1
+ column: ""
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T2
+ column: ""
+ predicatecolumns: []
+- description: Natural join star expansion collapses the shared column once with lineage from both sides
+ statement: SELECT * FROM T1 NATURAL JOIN T2;
+ defaultDatabase: SNOWFLAKE
+ ignoreCaseSensitive: true
+ metadata: |-
+ {
+ "name": "SNOWFLAKE",
+ "schemas": [
+ {
+ "name": "PUBLIC",
+ "tables": [
+ {
+ "name": "T1",
+ "columns": [
+ {
+ "name": "A"
+ },
+ {
+ "name": "K"
+ }
+ ]
+ },
+ {
+ "name": "T2",
+ "columns": [
+ {
+ "name": "K"
+ },
+ {
+ "name": "B"
+ }
+ ]
+ }
+ ]
+ }
+ ]
+ }
+ querySpan:
+ type: 1
+ results:
+ - name: A
+ sourcecolumns:
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T1
+ column: A
+ isplainfield: true
+ sourcefieldpaths: []
+ selectasterisk: false
+ - name: K
+ sourcecolumns:
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T1
+ column: K
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T2
+ column: K
+ isplainfield: true
+ sourcefieldpaths: []
+ selectasterisk: false
+ - name: B
+ sourcecolumns:
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T2
+ column: B
+ isplainfield: true
+ sourcefieldpaths: []
+ selectasterisk: false
+ sourcecolumns:
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T1
+ column: ""
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T2
+ column: ""
+ predicatecolumns: []
+- description: USE command yields an empty span instead of an error
+ statement: USE DATABASE D1;
+ defaultDatabase: SNOWFLAKE
+ ignoreCaseSensitive: true
+ metadata: |-
+ {
+ "name": "SNOWFLAKE"
+ }
+ querySpan:
+ type: 1
+ results: []
+ sourcecolumns: []
+ predicatecolumns: []
+- description: SET command yields an empty span instead of an error
+ statement: SET MY_VAR = 10;
+ defaultDatabase: SNOWFLAKE
+ ignoreCaseSensitive: true
+ metadata: |-
+ {
+ "name": "SNOWFLAKE"
+ }
+ querySpan:
+ type: 1
+ results: []
+ sourcecolumns: []
+ predicatecolumns: []
+- description: Positional column references $1 and qualified T.$2 resolve to the FROM scope (legacy DOLLAR/Column_position parity)
+ statement: SELECT $1, T.$2 FROM T1 T;
+ defaultDatabase: SNOWFLAKE
+ ignoreCaseSensitive: true
+ metadata: |-
+ {
+ "name": "SNOWFLAKE",
+ "schemas": [
+ {
+ "name": "PUBLIC",
+ "tables": [
+ {
+ "name": "T1",
+ "columns": [
+ {"name": "A"},
+ {"name": "B"}
+ ]
+ }
+ ]
+ }
+ ]
+ }
+ querySpan:
+ type: 1
+ results:
+ - name: A
+ sourcecolumns:
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T1
+ column: A
+ isplainfield: true
+ sourcefieldpaths: []
+ selectasterisk: false
+ - name: B
+ sourcecolumns:
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T1
+ column: B
+ isplainfield: true
+ sourcefieldpaths: []
+ selectasterisk: false
+ sourcecolumns:
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T1
+ column: ""
+ predicatecolumns: []
+- description: Positional $1 inside a compound expression contributes the first FROM column's lineage
+ statement: SELECT TO_DATE($1) FROM T1;
+ defaultDatabase: SNOWFLAKE
+ ignoreCaseSensitive: true
+ metadata: |-
+ {
+ "name": "SNOWFLAKE",
+ "schemas": [
+ {
+ "name": "PUBLIC",
+ "tables": [
+ {
+ "name": "T1",
+ "columns": [
+ {"name": "A"},
+ {"name": "B"}
+ ]
+ }
+ ]
+ }
+ ]
+ }
+ querySpan:
+ type: 1
+ results:
+ - name: TO_DATE($1)
+ sourcecolumns:
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T1
+ column: A
+ isplainfield: false
+ sourcefieldpaths: []
+ selectasterisk: false
+ sourcecolumns:
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T1
+ column: ""
+ predicatecolumns: []
+- description: SELECT * RENAME reports the alias as the result column name (lineage unchanged)
+ statement: SELECT * RENAME (B AS MASKED_B) FROM T1;
+ defaultDatabase: SNOWFLAKE
+ ignoreCaseSensitive: true
+ metadata: |-
+ {
+ "name": "SNOWFLAKE",
+ "schemas": [
+ {
+ "name": "PUBLIC",
+ "tables": [
+ {
+ "name": "T1",
+ "columns": [
+ {"name": "A"},
+ {"name": "B"}
+ ]
+ }
+ ]
+ }
+ ]
+ }
+ querySpan:
+ type: 1
+ results:
+ - name: A
+ sourcecolumns:
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T1
+ column: A
+ isplainfield: true
+ sourcefieldpaths: []
+ selectasterisk: false
+ - name: MASKED_B
+ sourcecolumns:
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T1
+ column: B
+ isplainfield: true
+ sourcefieldpaths: []
+ selectasterisk: false
+ sourcecolumns:
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T1
+ column: ""
+ predicatecolumns: []
+- description: SELECT * REPLACE rewires the replaced column's lineage to the replacement expression (cross-column)
+ statement: SELECT * REPLACE (UPPER(B) AS A) FROM T1;
+ defaultDatabase: SNOWFLAKE
+ ignoreCaseSensitive: true
+ metadata: |-
+ {
+ "name": "SNOWFLAKE",
+ "schemas": [
+ {
+ "name": "PUBLIC",
+ "tables": [
+ {
+ "name": "T1",
+ "columns": [
+ {"name": "A"},
+ {"name": "B"}
+ ]
+ }
+ ]
+ }
+ ]
+ }
+ querySpan:
+ type: 1
+ results:
+ - name: A
+ sourcecolumns:
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T1
+ column: B
+ isplainfield: false
+ sourcefieldpaths: []
+ selectasterisk: false
+ - name: B
+ sourcecolumns:
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T1
+ column: B
+ isplainfield: true
+ sourcefieldpaths: []
+ selectasterisk: false
+ sourcecolumns:
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T1
+ column: ""
+ predicatecolumns: []
+- description: SELECT * REPLACE with a qualified cross-table expression over a JOIN
+ statement: SELECT * REPLACE (T2.B AS A) FROM T1 JOIN T2 ON T1.ID = T2.ID;
+ defaultDatabase: SNOWFLAKE
+ ignoreCaseSensitive: true
+ metadata: |-
+ {
+ "name": "SNOWFLAKE",
+ "schemas": [
+ {
+ "name": "PUBLIC",
+ "tables": [
+ {
+ "name": "T1",
+ "columns": [
+ {"name": "ID"},
+ {"name": "A"}
+ ]
+ },
+ {
+ "name": "T2",
+ "columns": [
+ {"name": "ID"},
+ {"name": "B"}
+ ]
+ }
+ ]
+ }
+ ]
+ }
+ querySpan:
+ type: 1
+ results:
+ - name: ID
+ sourcecolumns:
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T1
+ column: ID
+ isplainfield: true
+ sourcefieldpaths: []
+ selectasterisk: false
+ - name: A
+ sourcecolumns:
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T2
+ column: B
+ isplainfield: false
+ sourcefieldpaths: []
+ selectasterisk: false
+ - name: ID
+ sourcecolumns:
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T2
+ column: ID
+ isplainfield: true
+ sourcefieldpaths: []
+ selectasterisk: false
+ - name: B
+ sourcecolumns:
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T2
+ column: B
+ isplainfield: true
+ sourcefieldpaths: []
+ selectasterisk: false
+ sourcecolumns:
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T1
+ column: ""
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T2
+ column: ""
+ predicatecolumns: []
+- description: SELECT * ILIKE filters the star expansion by the pattern (case-insensitive)
+ statement: SELECT * ILIKE '%s%' FROM T1;
+ defaultDatabase: SNOWFLAKE
+ ignoreCaseSensitive: true
+ metadata: |-
+ {
+ "name": "SNOWFLAKE",
+ "schemas": [
+ {
+ "name": "PUBLIC",
+ "tables": [
+ {
+ "name": "T1",
+ "columns": [
+ {"name": "ID"},
+ {"name": "SSN"},
+ {"name": "NAME"}
+ ]
+ }
+ ]
+ }
+ ]
+ }
+ querySpan:
+ type: 1
+ results:
+ - name: SSN
+ sourcecolumns:
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T1
+ column: SSN
+ isplainfield: true
+ sourcefieldpaths: []
+ selectasterisk: false
+ sourcecolumns:
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T1
+ column: ""
+ predicatecolumns: []
+- description: JOIN members in a derived table do not leak into the outer scope (stale-alias lookup must fail, not resolve)
+ statement: SELECT D.A, T3.C FROM (SELECT T1.A FROM T1 JOIN T2 ON T1.ID = T2.ID) D, T3;
+ defaultDatabase: SNOWFLAKE
+ ignoreCaseSensitive: true
+ metadata: |-
+ {
+ "name": "SNOWFLAKE",
+ "schemas": [
+ {
+ "name": "PUBLIC",
+ "tables": [
+ {"name": "T1", "columns": [{"name": "ID"}, {"name": "A"}]},
+ {"name": "T2", "columns": [{"name": "ID"}, {"name": "B"}]},
+ {"name": "T3", "columns": [{"name": "C"}]}
+ ]
+ }
+ ]
+ }
+ querySpan:
+ type: 1
+ results:
+ - name: A
+ sourcecolumns:
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T1
+ column: A
+ isplainfield: true
+ sourcefieldpaths: []
+ selectasterisk: false
+ - name: C
+ sourcecolumns:
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T3
+ column: C
+ isplainfield: true
+ sourcefieldpaths: []
+ selectasterisk: false
+ sourcecolumns:
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T1
+ column: ""
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T2
+ column: ""
+ - server: ""
+ database: SNOWFLAKE
+ schema: PUBLIC
+ table: T3
+ column: ""
+ predicatecolumns: []
diff --git a/go.mod b/go.mod
index d5d1c26377..038695da5c 100644
--- a/go.mod
+++ b/go.mod
@@ -34,7 +34,7 @@ require (
github.com/bmatcuk/doublestar/v4 v4.10.0
github.com/bytebase/gomongo v0.0.0-20260510192859-8526a440b352
github.com/bytebase/lsp-protocol v0.0.0-20250324071136-1586d0c10ff0
- github.com/bytebase/omni v0.0.0-20260610065946-2e66c343ef99
+ github.com/bytebase/omni v0.0.0-20260611091433-2bdffb0599b3
github.com/caarlos0/env/v11 v11.4.1
github.com/cenkalti/backoff/v5 v5.0.3
github.com/cockroachdb/cockroachdb-parser v0.25.2
diff --git a/go.sum b/go.sum
index 82c211a613..b5d8b53716 100644
--- a/go.sum
+++ b/go.sum
@@ -250,8 +250,8 @@ github.com/bytebase/gomongo v0.0.0-20260510192859-8526a440b352 h1:3sC5pdvJ7bNAhR
github.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-20260610065946-2e66c343ef99 h1:cdoFNOOGTdgpUqtdN72Uls0p1RRKujmjC9MvJli1J8E=
-github.com/bytebase/omni v0.0.0-20260610065946-2e66c343ef99/go.mod h1:EVG8nQbNPUnUBGKn6J0niSGOs3jgfKo3TciY22R4bfo=
+github.com/bytebase/omni v0.0.0-20260611091433-2bdffb0599b3 h1:e53I+ahF1zSk7ZrDcRrP5ZttInx6iRB2QaV8RMoa2oU=
+github.com/bytebase/omni v0.0.0-20260611091433-2bdffb0599b3/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=