Skip to content

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

feat(trino): migrate Trino parser plugin from ANTLR to omni (#20517)

h3n4l24080508cfa1
diff --git a/backend/plugin/parser/trino/completion.go b/backend/plugin/parser/trino/completion.go
index 086d35eca0..ae5f7770e1 100644
--- a/backend/plugin/parser/trino/completion.go
+++ b/backend/plugin/parser/trino/completion.go
@@ -2,13 +2,11 @@ package trino
import (
"context"
- "fmt"
- "slices"
"strings"
- "unicode"
+ "unicode/utf8"
- "github.com/antlr4-go/antlr/v4"
- trinoparser "github.com/bytebase/parser/trino"
+ "github.com/bytebase/omni/trino/catalog"
+ "github.com/bytebase/omni/trino/completion"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
@@ -18,1714 +16,195 @@ func init() {
base.RegisterCompleteFunc(storepb.Engine_TRINO, Completion)
}
-var (
- // Check tableRefListener is implementing the TrinoParserListener interface.
- _ trinoparser.TrinoParserListener = &tableRefListener{}
- _ trinoparser.TrinoParserListener = &cteExtractor{}
-
- globalFellowSetsByState = base.NewFollowSetsByState()
- ignoredTokens = map[int]bool{
- // Common EOF
- trinoparser.TrinoParserEOF: true,
-
- // // Whitespace and comments
- // trinoparser.TrinoLexerWS_: true,
- // trinoparser.TrinoLexerSIMPLE_COMMENT_: true,
- // trinoparser.TrinoLexerBRACKETED_COMMENT_: true,
-
- // Identifiers and literals
- trinoparser.TrinoLexerIDENTIFIER_: true,
- trinoparser.TrinoLexerQUOTED_IDENTIFIER_: true,
- trinoparser.TrinoLexerDIGIT_IDENTIFIER_: true,
- trinoparser.TrinoLexerBACKQUOTED_IDENTIFIER_: true,
- trinoparser.TrinoLexerSTRING_: true,
- trinoparser.TrinoLexerUNICODE_STRING_: true,
- trinoparser.TrinoLexerDECIMAL_VALUE_: true,
- trinoparser.TrinoLexerDOUBLE_VALUE_: true,
- trinoparser.TrinoLexerINTEGER_VALUE_: true,
- trinoparser.TrinoLexerBINARY_LITERAL_: true,
-
- // // Type related tokens
- // trinoparser.TrinoLexerDOUBLE_: true,
- // trinoparser.TrinoLexerPRECISION_: true,
-
- // Parameter token
- trinoparser.TrinoLexerQUESTION_MARK_: true,
-
- // Operators and punctuation
- trinoparser.TrinoLexerEQ_: true,
- trinoparser.TrinoLexerNEQ_: true,
- trinoparser.TrinoLexerLT_: true,
- trinoparser.TrinoLexerLTE_: true,
- trinoparser.TrinoLexerGT_: true,
- trinoparser.TrinoLexerGTE_: true,
- trinoparser.TrinoLexerPLUS_: true,
- trinoparser.TrinoLexerMINUS_: true,
- trinoparser.TrinoLexerASTERISK_: true,
- trinoparser.TrinoLexerSLASH_: true,
- trinoparser.TrinoLexerPERCENT_: true,
- trinoparser.TrinoLexerCONCAT_: true,
- trinoparser.TrinoLexerDOT_: true,
- trinoparser.TrinoLexerCOLON_: true,
- trinoparser.TrinoLexerSEMICOLON_: true,
- trinoparser.TrinoLexerCOMMA_: true,
- trinoparser.TrinoLexerLPAREN_: true,
- trinoparser.TrinoLexerRPAREN_: true,
- trinoparser.TrinoLexerLSQUARE_: true,
- trinoparser.TrinoLexerRSQUARE_: true,
- trinoparser.TrinoLexerLCURLY_: true,
- trinoparser.TrinoLexerRCURLY_: true,
- trinoparser.TrinoLexerLCURLYHYPHEN_: true,
- trinoparser.TrinoLexerRCURLYHYPHEN_: true,
- trinoparser.TrinoLexerLARROW_: true,
- trinoparser.TrinoLexerRARROW_: true,
- trinoparser.TrinoLexerRDOUBLEARROW_: true,
- trinoparser.TrinoLexerVBAR_: true,
- trinoparser.TrinoLexerDOLLAR_: true,
- trinoparser.TrinoLexerCARET_: true,
- trinoparser.TrinoLexerAT_: true,
- }
- preferredRules = map[int]bool{
- trinoparser.TrinoParserRULE_identifier: true,
- trinoparser.TrinoParserRULE_qualifiedName: true,
- }
-)
-
-type CompletionMap map[string]base.Candidate
-
-func (m CompletionMap) Insert(entry base.Candidate) {
- m[entry.String()] = entry
-}
-
-func (m CompletionMap) toSlice() []base.Candidate {
- var result []base.Candidate
- for _, candidate := range m {
- result = append(result, candidate)
- }
- slices.SortFunc(result, func(i, j base.Candidate) int {
- if i.Type != j.Type {
- if i.Type < j.Type {
- return -1
- }
- return 1
- }
- if i.Text < j.Text {
- return -1
- }
- if i.Text > j.Text {
- return 1
- }
- return 0
- })
- return result
-}
-
-func (m CompletionMap) insertMetadataCatalogs(c *Completer) {
- if c.defaultCatalog != "" {
- m[c.defaultCatalog] = base.Candidate{
- Type: base.CandidateTypeDatabase,
- Text: c.quotedIdentifierIfNeeded(c.defaultCatalog),
- }
- }
-
- allCatalogs, err := c.catalogNamesLister(c.ctx, c.instanceID)
- if err != nil {
- return
- }
-
- for _, catalog := range allCatalogs {
- if _, ok := m[catalog]; !ok {
- m[catalog] = base.Candidate{
- Type: base.CandidateTypeDatabase,
- Text: c.quotedIdentifierIfNeeded(catalog),
- }
- }
- }
-}
-
-func (m CompletionMap) insertMetadataSchemas(c *Completer, catalog string) {
- anchor := c.defaultCatalog
- if catalog != "" {
- anchor = catalog
- }
- if anchor == "" {
- return
- }
-
- allCatalogNames, err := c.catalogNamesLister(c.ctx, c.instanceID)
- if err != nil {
- return
- }
- for _, catalogName := range allCatalogNames {
- if strings.EqualFold(catalogName, anchor) {
- anchor = catalogName
- break
- }
- }
-
- _, catalogMetadata, err := c.metadataGetter(c.ctx, c.instanceID, anchor)
- if err != nil {
- return
- }
-
- for _, schema := range catalogMetadata.ListSchemaNames() {
- if _, ok := m[schema]; !ok {
- m[schema] = base.Candidate{
- Type: base.CandidateTypeSchema,
- Text: c.quotedIdentifierIfNeeded(schema),
- }
- }
- }
-}
-
-func (m CompletionMap) insertMetadataTables(c *Completer, catalog string, schema string) {
- catalogName, schemaName := c.defaultCatalog, c.defaultSchema
- if catalog != "" {
- catalogName = catalog
- }
- if schema != "" {
- schemaName = schema
- }
- if catalogName == "" || schemaName == "" {
- return
- }
-
- _, catalogMetadata, err := c.metadataGetter(c.ctx, c.instanceID, catalogName)
- if err != nil {
- return
- }
- if catalogMetadata == nil {
- return
- }
- for _, schema := range catalogMetadata.ListSchemaNames() {
- if strings.EqualFold(schema, schemaName) {
- schemaName = schema
- break
- }
- }
-
- schemaMetadata := catalogMetadata.GetSchemaMetadata(schemaName)
- if schemaMetadata == nil {
- return
- }
- for _, table := range schemaMetadata.ListTableNames() {
- if _, ok := m[table]; !ok {
- m[table] = base.Candidate{
- Type: base.CandidateTypeTable,
- Text: c.quotedIdentifierIfNeeded(table),
- }
- }
- }
-}
-
-func (m CompletionMap) insertAllColumns(c *Completer) {
- _, catalogMeta, err := c.metadataGetter(c.ctx, c.instanceID, c.defaultCatalog)
- if err != nil {
- return
- }
- for _, schema := range catalogMeta.ListSchemaNames() {
- schemaMeta := catalogMeta.GetSchemaMetadata(schema)
- if schemaMeta == nil {
- continue
- }
- for _, table := range schemaMeta.ListTableNames() {
- tableMeta := schemaMeta.GetTable(table)
- if tableMeta == nil {
- continue
- }
- for _, column := range tableMeta.GetProto().GetColumns() {
- columnID := getColumnID(c.defaultCatalog, schema, table, column.Name)
- if _, ok := m[columnID]; !ok {
- definition := fmt.Sprintf("%s.%s.%s | %s", c.defaultCatalog, schema, table, column.Type)
- if !column.Nullable {
- definition += ", NOT NULL"
- }
- m[columnID] = base.Candidate{
- Type: base.CandidateTypeColumn,
- Text: c.quotedIdentifierIfNeeded(column.Name),
- Definition: definition,
- Comment: column.Comment,
- Priority: c.getPriority(c.defaultCatalog, schema, table),
- }
- }
- }
- }
- }
-}
-
-func (c *Completer) getPriority(catalog string, schema string, table string) int {
- if catalog == "" {
- catalog = c.defaultCatalog
- }
- if schema == "" {
- schema = c.defaultSchema
- }
- if c.referenceMap == nil {
- return 1
- }
- if c.referenceMap[fmt.Sprintf("%s.%s.%s", catalog, schema, table)] {
- // The higher priority.
- return 0
- }
- return 1
-}
-
-func (m CompletionMap) insertMetadataColumns(c *Completer, catalog string, schema string, table string) {
- catalogName, schemaName, tableName := c.defaultCatalog, c.defaultSchema, ""
- if catalog != "" {
- catalogName = catalog
- }
- if schema != "" {
- schemaName = schema
- }
- if table != "" {
- tableName = table
- }
- if catalogName == "" || schemaName == "" {
- return
- }
- catalogNames, err := c.catalogNamesLister(c.ctx, c.instanceID)
- if err != nil {
- return
- }
- for _, catName := range catalogNames {
- if strings.EqualFold(catName, catalogName) {
- catalogName = catName
- break
- }
- }
- _, catalogMetadata, err := c.metadataGetter(c.ctx, c.instanceID, catalogName)
- if err != nil {
- return
- }
- if catalogMetadata == nil {
- return
- }
- for _, schema := range catalogMetadata.ListSchemaNames() {
- if strings.EqualFold(schema, schemaName) {
- schemaName = schema
- break
- }
- }
- schemaMetadata := catalogMetadata.GetSchemaMetadata(schemaName)
- if schemaMetadata == nil {
- return
- }
- var tableNames []string
- for _, table := range schemaMetadata.ListTableNames() {
- if tableName == "" {
- tableNames = append(tableNames, table)
- } else if strings.EqualFold(table, tableName) {
- tableNames = append(tableNames, table)
- break
- }
- }
- for _, table := range tableNames {
- tableMetadata := schemaMetadata.GetTable(table)
- for _, column := range tableMetadata.GetProto().GetColumns() {
- columnID := getColumnID(catalogName, schemaName, table, column.Name)
- if _, ok := m[columnID]; !ok {
- definition := fmt.Sprintf("%s.%s.%s | %s", catalogName, schemaName, table, column.Type)
- if !column.Nullable {
- definition += ", NOT NULL"
- }
- m[columnID] = base.Candidate{
- Type: base.CandidateTypeColumn,
- Text: c.quotedIdentifierIfNeeded(column.Name),
- Definition: definition,
- Comment: column.Comment,
- Priority: c.getPriority(c.defaultCatalog, schema, table),
- }
- }
- }
- }
-}
-
-func getColumnID(catalogName, schemaName, tableName, columnName string) string {
- return fmt.Sprintf("%s.%s.%s.%s", catalogName, schemaName, tableName, columnName)
-}
-
-func (m CompletionMap) insertCTEs(c *Completer) {
- for _, cte := range c.cteTables {
- if _, ok := m[cte.Table]; !ok {
- m[cte.Table] = base.Candidate{
- Type: base.CandidateTypeTable,
- Text: c.quotedIdentifierIfNeeded(cte.Table),
- }
- }
- }
-}
-
-func (m CompletionMap) insertMetadataViews(c *Completer, catalog string, schema string) {
- catalogName, schemaName := c.defaultCatalog, c.defaultSchema
- if catalog != "" {
- catalogName = catalog
- }
- if schema != "" {
- schemaName = schema
- }
-
- if catalogName == "" || schemaName == "" {
- return
- }
-
- _, catalogMetadata, err := c.metadataGetter(c.ctx, c.instanceID, catalogName)
- if err != nil {
- return
- }
- if catalogMetadata == nil {
- return
- }
-
- schemaNames := catalogMetadata.ListSchemaNames()
-
- var foundMatch bool
- for _, schema := range schemaNames {
- if strings.EqualFold(schema, schemaName) {
- schemaName = schema
- foundMatch = true
- break
- }
- }
-
- if !foundMatch {
- return
- }
-
- schemaMetadata := catalogMetadata.GetSchemaMetadata(schemaName)
- if schemaMetadata == nil {
- return
- }
-
- viewNames := schemaMetadata.ListViewNames()
-
- for _, view := range viewNames {
- if _, ok := m[view]; !ok {
- m[view] = base.Candidate{
- Type: base.CandidateTypeView,
- Text: c.quotedIdentifierIfNeeded(view),
- }
- }
- }
-
- matViewNames := schemaMetadata.ListMaterializedViewNames()
-
- for _, materializeView := range matViewNames {
- if _, ok := m[materializeView]; !ok {
- m[materializeView] = base.Candidate{
- Type: base.CandidateTypeView,
- Text: c.quotedIdentifierIfNeeded(materializeView),
- }
- }
- }
-
- foreignTableNames := schemaMetadata.ListForeignTableNames()
-
- for _, foreignTable := range foreignTableNames {
- if _, ok := m[foreignTable]; !ok {
- m[foreignTable] = base.Candidate{
- Type: base.CandidateTypeView,
- Text: c.quotedIdentifierIfNeeded(foreignTable),
- }
- }
- }
-}
-
-// quotedType is the type of quoted token
-type quotedType int
-
-const (
- quotedTypeNone quotedType = iota
- quotedTypeQuote // ""
- quotedTypeBackQuote // ``
-)
-
-type Completer struct {
- ctx context.Context
- core *base.CodeCompletionCore
- scene base.SceneType
- parser *trinoparser.TrinoParser
- lexer *trinoparser.TrinoLexer
- scanner *base.Scanner
-
- instanceID string
- defaultCatalog string
- defaultSchema string
- metadataGetter base.GetDatabaseMetadataFunc
- catalogNamesLister base.ListDatabaseNamesFunc
-
- noSeparatorRequired map[int]bool
- // referencesStack is a hierarchical stack of table references.
- // We'll update the stack when we encounter a new FROM clauses.
- referencesStack [][]base.TableReference
- // references is the flattened table references.
- // It's helpful to look up the table reference.
- references []base.TableReference
- referenceMap map[string]bool
- cteCache map[int][]*base.VirtualTableReference
- cteTables []*base.VirtualTableReference
- caretTokenIsQuoted quotedType
-}
-
+// Completion returns auto-completion candidates for a Trino statement at the
+// given caret position. It is backed by the omni Trino completer
+// (github.com/bytebase/omni/trino/completion), which tokenizes the input up to
+// the caret, classifies the completion context, and produces candidates from a
+// three-level Trino catalog (catalog -> schema -> table/view -> column) plus a
+// curated keyword set.
+//
+// The bytebase metadata (database -> schema -> table -> column) maps onto the
+// Trino catalog model as catalog=database, so a Company.dbo.Employees object is
+// reachable as the Trino name Company.dbo.Employees.
+//
+// Divergence from the legacy ANTLR (CodeCompletionCore) completer: the omni
+// completer is intentionally NOT a c3 port (see the omni completion package
+// doc). It emits {Text, Type} candidates only — it does not populate the
+// Definition ("catalog.schema.table | type, NOT NULL") or Priority fields the
+// legacy completer attached, and its candidate *set* in column contexts is the
+// in-scope FROM columns (resolved via query-span analysis) rather than the
+// legacy completer's "every column in the default schema, ranked by priority".
+// Those fields are therefore left empty here.
func Completion(ctx context.Context, cCtx base.CompletionContext, statement string, caretLine int, caretOffset int) ([]base.Candidate, error) {
- // Try standard completer first
- completer := NewStandardCompleter(ctx, cCtx, statement, caretLine, caretOffset)
- completer.fetchCommonTableExpression(statement)
- result, err := completer.complete()
-
- if err != nil {
- return nil, err
- }
- if len(result) > 0 {
- return result, nil
- }
-
- // If no results, try tricky completer with skipHeadingSQLWithoutSemicolon
- trickyCompleter := NewTrickyCompleter(ctx, cCtx, statement, caretLine, caretOffset)
- trickyCompleter.fetchCommonTableExpression(statement)
- return trickyCompleter.complete()
-}
-
-func NewStandardCompleter(ctx context.Context, cCtx base.CompletionContext, statement string, caretLine int, caretOffset int) *Completer {
- parser, lexer, scanner := prepareParserAndScanner(statement, caretLine, caretOffset)
- core := base.NewCodeCompletionCore(
- ctx,
- parser,
- ignoredTokens, /* IgnoredTokens */
- preferredRules, /* PreferredRules */
- &globalFellowSetsByState,
- trinoparser.TrinoParserRULE_queryNoWith,
- trinoparser.TrinoParserRULE_query,
- trinoparser.TrinoParserRULE_as_column_alias,
- -1,
- )
-
- return &Completer{
- ctx: ctx,
- core: core,
- scene: cCtx.Scene,
- parser: parser,
- lexer: lexer,
- scanner: scanner,
- instanceID: cCtx.InstanceID,
- defaultCatalog: cCtx.DefaultDatabase,
- defaultSchema: "dbo",
- metadataGetter: cCtx.Metadata,
- catalogNamesLister: cCtx.ListDatabaseNames,
- noSeparatorRequired: map[int]bool{
- trinoparser.TrinoLexerCOMMA_: true,
- trinoparser.TrinoLexerSEMICOLON_: true,
- trinoparser.TrinoLexerDOT_: true,
- trinoparser.TrinoLexerLPAREN_: true,
- trinoparser.TrinoLexerRPAREN_: true,
- trinoparser.TrinoLexerLSQUARE_: true,
- trinoparser.TrinoLexerRSQUARE_: true,
- },
- cteCache: nil,
- }
-}
-
-func NewTrickyCompleter(ctx context.Context, cCtx base.CompletionContext, statement string, caretLine int, caretOffset int) *Completer {
- parser, lexer, scanner := prepareTrickyParserAndScanner(statement, caretLine, caretOffset)
- core := base.NewCodeCompletionCore(
- ctx,
- parser,
- ignoredTokens, /* IgnoredTokens */
- preferredRules, /* PreferredRules */
- &globalFellowSetsByState,
- trinoparser.TrinoParserRULE_queryNoWith,
- trinoparser.TrinoParserRULE_query,
- trinoparser.TrinoParserRULE_as_column_alias,
- -1,
- )
-
- return &Completer{
- ctx: ctx,
- core: core,
- scene: cCtx.Scene,
- parser: parser,
- lexer: lexer,
- scanner: scanner,
- instanceID: cCtx.InstanceID,
- defaultCatalog: cCtx.DefaultDatabase,
- defaultSchema: "dbo",
- metadataGetter: cCtx.Metadata,
- catalogNamesLister: cCtx.ListDatabaseNames,
- noSeparatorRequired: map[int]bool{
- trinoparser.TrinoLexerCOMMA_: true,
- trinoparser.TrinoLexerSEMICOLON_: true,
- trinoparser.TrinoLexerDOT_: true,
- trinoparser.TrinoLexerLPAREN_: true,
- trinoparser.TrinoLexerRPAREN_: true,
- trinoparser.TrinoLexerLSQUARE_: true,
- trinoparser.TrinoLexerRSQUARE_: true,
- },
- cteCache: nil,
- }
-}
-
-func prepareParserAndScanner(statement string, caretLine int, caretOffset int) (*trinoparser.TrinoParser, *trinoparser.TrinoLexer, *base.Scanner) {
- statement, caretLine, caretOffset = skipHeadingSQLs(statement, caretLine, caretOffset)
- input := antlr.NewInputStream(statement)
- lexer := trinoparser.NewTrinoLexer(input)
- stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
- parser := trinoparser.NewTrinoParser(stream)
- parser.RemoveErrorListeners()
- lexer.RemoveErrorListeners()
- scanner := base.NewScanner(stream, true /* fillInput */)
- scanner.SeekPosition(caretLine, caretOffset)
- scanner.Push()
- return parser, lexer, scanner
-}
-
-func prepareTrickyParserAndScanner(statement string, caretLine int, caretOffset int) (*trinoparser.TrinoParser, *trinoparser.TrinoLexer, *base.Scanner) {
- statement, caretLine, caretOffset = skipHeadingSQLs(statement, caretLine, caretOffset)
- statement, caretLine, caretOffset = skipHeadingSQLWithoutSemicolon(statement, caretLine, caretOffset)
- input := antlr.NewInputStream(statement)
- lexer := trinoparser.NewTrinoLexer(input)
- stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
- parser := trinoparser.NewTrinoParser(stream)
- parser.RemoveErrorListeners()
- lexer.RemoveErrorListeners()
- scanner := base.NewScanner(stream, true /* fillInput */)
- scanner.SeekPosition(caretLine, caretOffset)
- scanner.Push()
- return parser, lexer, scanner
-}
-
-func (c *Completer) complete() ([]base.Candidate, error) {
- if c.scanner.IsTokenType(trinoparser.TrinoParserQUOTED_IDENTIFIER_) {
- c.caretTokenIsQuoted = quotedTypeQuote
- }
-
- caretIndex := c.scanner.GetIndex()
-
- // Check if we need to adjust the caret position based on the previous token
- adjustedCaretIndex := caretIndex
- if caretIndex > 0 && !c.noSeparatorRequired[c.scanner.GetPreviousTokenType(true)] {
- adjustedCaretIndex--
- }
-
- c.referencesStack = append([][]base.TableReference{{}}, c.referencesStack...)
- c.parser.Reset()
-
- var context antlr.ParserRuleContext
- if c.scene == base.SceneTypeQuery {
- context = c.parser.SingleStatement()
- } else {
- context = c.parser.Parse()
- }
-
- // For Trino, we need special handling because whitespace tokens are included
- // We add 1 in most cases, but not when:
- // 1. We're at the very beginning (caretIndex == 0)
- // 2. We're right after an identifier that could be a partial keyword
- finalCaretIndex := adjustedCaretIndex + 1
-
- // Special cases where we don't add 1
- if caretIndex == 0 {
- // At the very beginning
- finalCaretIndex = adjustedCaretIndex
- } else if c.scanner.GetTokenType() == antlr.TokenEOF && c.scanner.GetPreviousTokenType(true) == trinoparser.TrinoLexerIDENTIFIER_ {
- // At EOF right after an identifier (potential partial keyword)
- finalCaretIndex = adjustedCaretIndex
- }
+ cat := buildCompletionCatalog(ctx, cCtx, statement)
- candidates := c.core.CollectCandidates(finalCaretIndex, context)
+ byteOffset := caretToByteOffset(statement, caretLine, caretOffset)
+ cands := completion.Complete(statement, byteOffset, cat)
- for ruleName := range candidates.Rules {
- if ruleName == trinoparser.TrinoParserRULE_identifier {
- c.collectLeadingTableReferences(adjustedCaretIndex)
- c.takeReferencesSnapshot()
- c.collectRemainingTableReferences()
- c.takeReferencesSnapshot()
- break
- }
+ result := make([]base.Candidate, 0, len(cands))
+ for _, c := range cands {
+ result = append(result, base.Candidate{
+ Text: c.Text,
+ Type: convertCandidateType(c.Type),
+ })
}
-
- result := c.convertCandidates(candidates)
return result, nil
}
-func (c *Completer) convertCandidates(candidates *base.CandidatesCollection) []base.Candidate {
- keywordEntries := make(CompletionMap)
- functionEntries := make(CompletionMap)
- catalogEntries := make(CompletionMap)
- schemaEntries := make(CompletionMap)
- tableEntries := make(CompletionMap)
- columnEntries := make(CompletionMap)
- viewEntries := make(CompletionMap)
-
- for tokenCandidate, continuous := range candidates.Tokens {
- if tokenCandidate < 0 || tokenCandidate >= len(c.parser.SymbolicNames) {
+// buildCompletionCatalog constructs an omni Trino catalog from the completion
+// context's metadata. Each bytebase database becomes a Trino catalog; its
+// schemas, tables/views and columns are copied in. The session's current
+// catalog/schema are set from the context defaults so unqualified names resolve
+// there. Returns nil when no catalogs are available (omni Complete then offers
+// only keywords and statement-derived CTE names).
+//
+// Every catalog name is registered cheaply so catalog-level completion can list
+// them all, but full schema/table/column metadata is loaded only for catalogs
+// the current statement actually needs (the default catalog and any catalog
+// named in the statement). Trino instances routinely federate many catalogs and
+// the LSP invokes completion on every keystroke, so eagerly loading all of them
+// would fan out into hundreds of metadata fetches and stall completion.
+func buildCompletionCatalog(ctx context.Context, cCtx base.CompletionContext, statement string) *catalog.Catalog {
+ if cCtx.Metadata == nil || cCtx.ListDatabaseNames == nil {
+ return nil
+ }
+ names, err := cCtx.ListDatabaseNames(ctx, cCtx.InstanceID)
+ if err != nil || len(names) == 0 {
+ return nil
+ }
+
+ lowerStmt := strings.ToLower(statement)
+ defaultDB := catalog.Normalize(cCtx.DefaultDatabase)
+
+ cat := catalog.New()
+ for _, dbName := range names {
+ norm := catalog.Normalize(dbName)
+ // Register the catalog name (cheap) so catalog-level completion lists it.
+ cat.EnsureCatalog(norm)
+ if !catalogNeeded(norm, defaultDB, lowerStmt) {
continue
}
-
- candidateText := c.parser.SymbolicNames[tokenCandidate]
- // Remove trailing underscore from keywords in Trino parser
- candidateText = strings.TrimSuffix(candidateText, "_")
-
- for _, continuous := range continuous {
- if continuous < 0 || continuous >= len(c.parser.SymbolicNames) {
- continue
- }
- continuousText := c.parser.SymbolicNames[continuous]
- // Remove trailing underscore from continuous tokens as well
- continuousText = strings.TrimSuffix(continuousText, "_")
- candidateText += " " + continuousText
- }
- keywordEntries.Insert(base.Candidate{
- Type: base.CandidateTypeKeyword,
- Text: candidateText,
- })
- }
-
- for ruleCandidate, ruleStack := range candidates.Rules {
- c.scanner.PopAndRestore()
- c.scanner.Push()
-
- switch ruleCandidate {
- case trinoparser.TrinoParserRULE_qualifiedName:
- // identifier also appears in the qualifiedName rule, handle it separately
- if len(ruleStack) > 0 && ruleStack[len(ruleStack)-1].ID == trinoparser.TrinoParserRULE_identifier {
- continue
- }
- completionContexts := c.determineQualifiedNameContext()
- for _, context := range completionContexts {
- if context.flags&objectFlagShowCatalog != 0 {
- catalogEntries.insertMetadataCatalogs(c)
- }
- if context.flags&objectFlagShowSchema != 0 {
- schemaEntries.insertMetadataSchemas(c, context.catalog)
- }
- if context.flags&objectFlagShowObject != 0 {
- tableEntries.insertMetadataTables(c, context.catalog, context.schema)
- viewEntries.insertMetadataViews(c, context.catalog, context.schema)
- }
- if context.catalog == "" && context.schema == "" && context.flags&objectFlagShowObject != 0 {
- // User did not specify catalog and schema, and wants to complete objects, we should also insert the CTEs
- tableEntries.insertCTEs(c)
- }
- }
- case trinoparser.TrinoParserRULE_identifier:
- completionContexts := c.determineColumnReference()
- for _, context := range completionContexts {
- if context.flags&objectFlagShowCatalog != 0 {
- catalogEntries.insertMetadataCatalogs(c)
- }
- if context.flags&objectFlagShowSchema != 0 {
- schemaEntries.insertMetadataSchemas(c, context.catalog)
- }
- if context.flags&objectFlagShowObject != 0 {
- tableEntries.insertMetadataTables(c, context.catalog, context.schema)
- viewEntries.insertMetadataViews(c, context.catalog, context.schema)
-
- // Add table references from the FROM clause
- for _, reference := range c.references {
- switch reference := reference.(type) {
- case *base.PhysicalTableReference:
- tableName := reference.Table
- if reference.Alias != "" {
- tableName = reference.Alias
- }
- if strings.EqualFold(reference.Database, context.catalog) &&
- strings.EqualFold(reference.Schema, context.schema) {
- if _, ok := tableEntries[tableName]; !ok {
- tableEntries[tableName] = base.Candidate{
- Type: base.CandidateTypeTable,
- Text: c.quotedIdentifierIfNeeded(tableName),
- }
- }
- }
- case *base.VirtualTableReference:
- // We only append the virtual table reference to the completion list
- // when the catalog and schema are all empty.
- if context.catalog == "" && context.schema == "" {
- tableEntries[reference.Table] = base.Candidate{
- Type: base.CandidateTypeTable,
- Text: c.quotedIdentifierIfNeeded(reference.Table),
- }
- }
- default:
- }
- }
- if context.catalog == "" && context.schema == "" {
- // User did not specify catalog and schema, and wants to complete objects,
- // we should also insert the CTEs
- tableEntries.insertCTEs(c)
- }
- }
- if context.flags&objectFlagShowColumn != 0 {
- // Add column aliases from select items
- list := c.fetchSelectItemAliases(ruleStack)
- for _, alias := range list {
- columnEntries.Insert(base.Candidate{
- Type: base.CandidateTypeColumn,
- Text: c.quotedIdentifierIfNeeded(alias),
- })
- }
-
- // Add columns from metadata for the specified table
- columnEntries.insertMetadataColumns(c, context.catalog, context.schema, context.object)
-
- // Add columns from table references in the current query
- for _, reference := range c.references {
- switch reference := reference.(type) {
- case *base.PhysicalTableReference:
- inputCatalogName := context.catalog
- if inputCatalogName == "" {
- inputCatalogName = c.defaultCatalog
- }
- inputSchemaName := context.schema
- if inputSchemaName == "" {
- inputSchemaName = c.defaultSchema
- }
- inputTableName := context.object
-
- referenceCatalogName := reference.Database
- if referenceCatalogName == "" {
- referenceCatalogName = c.defaultCatalog
- }
- referenceSchemaName := reference.Schema
- if referenceSchemaName == "" {
- referenceSchemaName = c.defaultSchema
- }
- referenceTableName := reference.Table
- if reference.Alias != "" {
- referenceTableName = reference.Alias
- }
-
- if strings.EqualFold(referenceCatalogName, inputCatalogName) &&
- strings.EqualFold(referenceSchemaName, inputSchemaName) &&
- strings.EqualFold(referenceTableName, inputTableName) {
- columnEntries.insertMetadataColumns(c, reference.Database, reference.Schema, reference.Table)
- }
- case *base.VirtualTableReference:
- // Reference could be a physical table reference or a virtual table reference
- // If the reference is a virtual table reference and users do not specify
- // the catalog and schema, we should also insert the columns.
- if context.catalog == "" && context.schema == "" {
- for _, column := range reference.Columns {
- if _, ok := columnEntries[column]; !ok {
- columnEntries[column] = base.Candidate{
- Type: base.CandidateTypeColumn,
- Text: c.quotedIdentifierIfNeeded(column),
- }
- }
- }
- }
- default:
- }
- }
-
- // Add columns from CTEs if applicable
- if context.catalog == "" && context.schema == "" {
- for _, cte := range c.cteTables {
- if strings.EqualFold(cte.Table, context.object) {
- for _, column := range cte.Columns {
- if _, ok := columnEntries[column]; !ok {
- columnEntries[column] = base.Candidate{
- Type: base.CandidateTypeColumn,
- Text: c.quotedIdentifierIfNeeded(column),
- }
- }
- }
- }
- }
- }
-
- // If no specific table was referenced, show all columns from the default catalog and schema
- if context.empty() {
- columnEntries.insertAllColumns(c)
- }
- }
- }
- default:
- // Ignore other rule candidates
- }
- }
-
- c.scanner.PopAndRestore()
- var result []base.Candidate
- result = append(result, keywordEntries.toSlice()...)
- result = append(result, functionEntries.toSlice()...)
- result = append(result, catalogEntries.toSlice()...)
- result = append(result, schemaEntries.toSlice()...)
- result = append(result, tableEntries.toSlice()...)
- result = append(result, columnEntries.toSlice()...)
- result = append(result, viewEntries.toSlice()...)
- if result == nil {
- result = []base.Candidate{}
- }
- return result
-}
-
-type objectFlag int
-
-const (
- objectFlagShowCatalog objectFlag = 1 << iota
- objectFlagShowSchema
- objectFlagShowObject
- objectFlagShowColumn
-)
-
-type objectRefContextOption func(*objectRefContext)
-
-func withColumn() objectRefContextOption {
- return func(c *objectRefContext) {
- c.column = ""
- c.flags |= objectFlagShowColumn
- }
-}
-
-func newObjectRefContext(options ...objectRefContextOption) *objectRefContext {
- o := &objectRefContext{
- flags: objectFlagShowCatalog | objectFlagShowSchema | objectFlagShowObject,
- }
- for _, option := range options {
- option(o)
- }
- return o
-}
-
-// objectRefContext provides precise completion context about the object reference,
-// check the flags and the fields to determine what kind of object should be included in the completion list.
-// Caller should call the newObjectRefContext to create a new objectRefContext, and modify it based on function it provides.
-type objectRefContext struct {
- catalog string
- schema string
- object string
-
- // column is optional considering field, for example, it should be not applicable for qualified name rule.
- column string
-
- flags objectFlag
-}
-
-func (o *objectRefContext) empty() bool {
- return o.catalog == "" && o.schema == "" && o.object == "" && o.column == ""
-}
-
-func (o *objectRefContext) clone() *objectRefContext {
- return &objectRefContext{
- catalog: o.catalog,
- schema: o.schema,
- object: o.object,
- column: o.column,
- flags: o.flags,
- }
-}
-
-func (o *objectRefContext) setCatalog(catalog string) *objectRefContext {
- o.catalog = catalog
- o.flags &= ^objectFlagShowCatalog
- return o
-}
-
-func (o *objectRefContext) setSchema(schema string) *objectRefContext {
- o.schema = schema
- o.flags &= ^objectFlagShowSchema
- return o
-}
-
-func (o *objectRefContext) setObject(object string) *objectRefContext {
- o.object = object
- o.flags &= ^objectFlagShowObject
- return o
-}
-
-func (o *objectRefContext) setColumn(column string) *objectRefContext {
- o.column = column
- o.flags &= ^objectFlagShowColumn
- return o
-}
-
-func (c *Completer) determineColumnReference() []*objectRefContext {
- tokenIndex := c.scanner.GetIndex()
- if c.scanner.GetTokenChannel() != antlr.TokenDefaultChannel {
- // Skip to the next non-hidden token.
- c.scanner.Forward(true /* skipHidden */)
- }
-
- // Check current token
- if c.scanner.GetTokenText() != "." && !isIdentifier(c.scanner.GetTokenType()) {
- c.scanner.Backward(true /* skipHidden */)
- }
-
- if tokenIndex > 0 {
- // Go backward until we hit a non-identifier token.
- for {
- curID := isIdentifier(c.scanner.GetTokenType()) && c.scanner.GetPreviousTokenText(false /* skipHidden */) == "."
- curDOT := c.scanner.GetTokenText() == "." && isIdentifier(c.scanner.GetPreviousTokenType(false /* skipHidden */))
- if curID || curDOT {
- c.scanner.Backward(true /* skipHidden */)
- continue
- }
- break
- }
- }
-
- // The scanner is now on the leading identifier (or dot?) if there's no leading id.
- var candidates []string
- var temp string
- var count int
- for {
- count++
- if isIdentifier(c.scanner.GetTokenType()) {
- temp = normalizeIdentifierText(c.scanner.GetTokenText())
- c.scanner.Forward(true /* skipHidden */)
- if !c.scanner.IsTokenType(trinoparser.TrinoParserDOT_) || tokenIndex <= c.scanner.GetIndex() {
- return deriveObjectRefContextsFromCandidates(candidates, true /* includeColumn */)
- }
- candidates = append(candidates, temp)
- }
- c.scanner.Forward(true /* skipHidden */)
- if count > 3 {
- break
+ _, meta, err := cCtx.Metadata(ctx, cCtx.InstanceID, dbName)
+ if err != nil || meta == nil {
+ continue
}
- }
-
- return deriveObjectRefContextsFromCandidates(candidates, true /* includeColumn */)
-}
-
-func (c *Completer) determineQualifiedNameContext() []*objectRefContext {
- tokenIndex := c.scanner.GetIndex()
- if c.scanner.GetTokenChannel() != antlr.TokenDefaultChannel {
- // Skip to the next non-hidden token.
- c.scanner.Forward(true /* skipHidden */)
- }
-
- // Check if we're at a dot or identifier
- if c.scanner.GetTokenText() != "." && !isIdentifier(c.scanner.GetTokenType()) {
- // We are at the end of an incomplete identifier spec. Jump back.
- c.scanner.Backward(true /* skipHidden */)
- }
-
- if tokenIndex > 0 {
- // Go backward until we hit a non-identifier token to find the start of the qualified name.
- for {
- curID := isIdentifier(c.scanner.GetTokenType()) && c.scanner.GetPreviousTokenText(false /* skipHidden */) == "."
- curDOT := c.scanner.GetTokenText() == "." && isIdentifier(c.scanner.GetPreviousTokenType(false /* skipHidden */))
- if curID || curDOT {
- c.scanner.Backward(true /* skipHidden */)
+ database := cat.EnsureCatalog(norm)
+ for _, schemaName := range meta.ListSchemaNames() {
+ schemaMeta := meta.GetSchemaMetadata(schemaName)
+ if schemaMeta == nil {
continue
}
- break
- }
- }
-
- // The scanner is now on the leading identifier (or at a dot if there's no leading id).
- var candidates []string
- var temp string
- var count int
- for {
- count++
- if isIdentifier(c.scanner.GetTokenType()) {
- temp = normalizeIdentifierText(c.scanner.GetTokenText())
- candidates = append(candidates, temp)
- c.scanner.Forward(true /* skipHidden */)
- }
-
- // Check if we've hit the end of the identifier chain or we're past the caret
- if !c.scanner.IsTokenType(trinoparser.TrinoParserDOT_) || tokenIndex <= c.scanner.GetIndex() {
- return deriveObjectRefContextsFromCandidates(candidates, false /* includeColumn */)
- }
-
- // Skip the dot and move to the next token
- c.scanner.Forward(true /* skipHidden */)
- if count > 3 {
- break
- }
- }
-
- return deriveObjectRefContextsFromCandidates(candidates, false /* includeColumn */)
-}
-
-// deriveObjectRefContextsFromCandidates derives the object reference contexts from the candidates.
-// In Trino, a qualified name has the format [catalog_name.][schema_name.][object_name]
-// For example, if the candidates are ["a", "b"], the size is 2,
-// and objectRefContext would be either:
-// - [catalog: "a", schema: "b", object: ""] (interpreting as catalog.schema)
-// - [catalog: "", schema: "a", object: "b"] (interpreting as schema.object)
-func deriveObjectRefContextsFromCandidates(candidates []string, includeColumn bool) []*objectRefContext {
- var options []objectRefContextOption
- if includeColumn {
- options = append(options, withColumn())
- }
- refCtx := newObjectRefContext(options...)
-
- // If we have no candidates, return an empty context
- if len(candidates) == 0 {
- return []*objectRefContext{
- refCtx.clone(),
- }
- }
-
- var results []*objectRefContext
- switch len(candidates) {
- case 1:
- // Single identifier could be:
- // 1. A catalog name
- // 2. A schema name
- // 3. An object name
- // 4. A column name (if includeColumn is true)
- results = append(
- results,
- refCtx.clone().setCatalog(candidates[0]),
- refCtx.clone().setCatalog("").setSchema(candidates[0]),
- refCtx.clone().setCatalog("").setSchema("").setObject(candidates[0]),
- )
- if includeColumn {
- results = append(results, refCtx.clone().setCatalog("").setSchema("").setObject("").setColumn(candidates[0]))
- }
- case 2:
- // Two identifiers could be:
- // 1. catalog.schema
- // 2. schema.object
- // 3. object.column (if includeColumn is true)
- results = append(
- results,
- refCtx.clone().setCatalog(candidates[0]).setSchema(candidates[1]),
- refCtx.clone().setCatalog("").setSchema(candidates[0]).setObject(candidates[1]),
- )
- if includeColumn {
- results = append(results, refCtx.clone().setCatalog("").setSchema("").setObject(candidates[0]).setColumn(candidates[1]))
- }
- case 3:
- // Three identifiers would be catalog.schema.object
- // Or catalog.schema.column if includeColumn is true
- results = append(
- results,
- refCtx.clone().setCatalog(candidates[0]).setSchema(candidates[1]).setObject(candidates[2]),
- )
- if includeColumn {
- results = append(results, refCtx.clone().setCatalog(candidates[0]).setSchema(candidates[1]).setObject("").setColumn(candidates[2]))
- }
- case 4:
- // Four identifiers would be catalog.schema.object.column (if includeColumn is true)
- if includeColumn {
- results = append(results, refCtx.clone().setCatalog(candidates[0]).setSchema(candidates[1]).setObject(candidates[2]).setColumn(candidates[3]))
- }
- default:
- // For cases with more than 4 identifiers, ignore them
- return []*objectRefContext{refCtx.clone()}
- }
-
- // If no results were generated, return the default empty context
- if len(results) == 0 {
- results = append(results, refCtx.clone())
- }
-
- return results
-}
-
-// skipHeadingSQLs skips the SQL statements which before the caret position.
-// caretLine is 1-based and caretOffset is 0-based.
-func skipHeadingSQLs(statement string, caretLine int, caretOffset int) (string, int, int) {
- newCaretLine, newCaretOffset := caretLine, caretOffset
- list, err := SplitSQL(statement)
- if err != nil || notEmptySQLCount(list) <= 1 {
- return statement, caretLine, caretOffset
- }
-
- caretLine-- // Convert to 0-based.
-
- start := 0
- for i, sql := range list {
- // End.Line is 1-based per proto spec, convert to 0-based for comparison with caretLine
- sqlEndLine := int(sql.End.GetLine()) - 1
- sqlEndColumn := int(sql.End.GetColumn())
- if sqlEndLine > caretLine || (sqlEndLine == caretLine && sqlEndColumn >= caretOffset) {
- start = i
- if i == 0 {
- // The caret is in the first SQL statement, so we don't need to skip any SQL statements.
- break
- }
- // End.Line is 1-based per proto spec, convert to 0-based
- previousSQLEndLine := int(list[i-1].End.GetLine()) - 1
- previousSQLEndColumn := int(list[i-1].End.GetColumn())
- newCaretLine = caretLine - previousSQLEndLine + 1 // Convert to 1-based.
- if caretLine == previousSQLEndLine {
- // The caret is in the same line as the last line of the previous SQL statement.
- // End.Column is 1-based exclusive, so (End.Column - 1) gives 0-based start of next statement.
- // newCaretOffset = caretOffset - (previousSQLEndColumn - 1)
- newCaretOffset = caretOffset - previousSQLEndColumn + 1
- }
- break
- }
- }
-
- var buf strings.Builder
- for i := start; i < len(list); i++ {
- if _, err := buf.WriteString(list[i].Text); err != nil {
- return statement, caretLine + 1, caretOffset // Convert back to 1-based on error
- }
- }
-
- return buf.String(), newCaretLine, newCaretOffset
-}
-
-func notEmptySQLCount(list []base.Statement) int {
- count := 0
- for _, sql := range list {
- if !sql.Empty {
- count++
- }
- }
- return count
-}
-
-func (c *Completer) takeReferencesSnapshot() {
- if c.referenceMap == nil {
- c.referenceMap = make(map[string]bool)
- }
- for _, references := range c.referencesStack {
- c.references = append(c.references, references...)
- for _, reference := range references {
- if r, ok := reference.(*base.PhysicalTableReference); ok {
- catalog := r.Database
- if catalog == "" {
- catalog = c.defaultCatalog
+ sc := database.EnsureSchema(catalog.Normalize(schemaName))
+ for _, tableName := range schemaMeta.ListTableNames() {
+ tableMeta := schemaMeta.GetTable(tableName)
+ if tableMeta == nil {
+ continue
}
- schema := r.Schema
- if schema == "" {
- schema = c.defaultSchema
- }
- tableID := fmt.Sprintf("%s.%s.%s", catalog, schema, r.Table)
- c.referenceMap[tableID] = true
- }
- }
- }
-}
-
-func (c *Completer) collectLeadingTableReferences(caretIndex int) {
- c.scanner.Push()
-
- c.scanner.SeekIndex(0)
-
- level := 0
- for {
- found := c.scanner.GetTokenType() == trinoparser.TrinoParserFROM_
- for !found {
- if !c.scanner.Forward(false) || c.scanner.GetIndex() >= caretIndex {
- break
+ sc.AddTable(catalog.Normalize(tableName), columnsOf(tableMeta.GetProto().GetColumns())...)
}
-
- switch c.scanner.GetTokenType() {
- case trinoparser.TrinoParserLPAREN_:
- level++
- c.referencesStack = append([][]base.TableReference{{}}, c.referencesStack...)
- case trinoparser.TrinoParserRPAREN_:
- if level == 0 {
- c.scanner.PopAndRestore()
- return // We cannot go above the initial nesting level.
+ for _, viewName := range schemaMeta.ListViewNames() {
+ viewMeta := schemaMeta.GetView(viewName)
+ if viewMeta == nil {
+ continue
}
- case trinoparser.TrinoParserFROM_:
- found = true
- default:
- // Continue scanning for other tokens
+ sc.AddView(catalog.Normalize(viewName), columnsOf(viewMeta.GetColumns())...)
}
}
- if !found {
- c.scanner.PopAndRestore()
- return // No FROM clause found.
- }
- c.parseTableReferences(c.scanner.GetFollowingText())
- if c.scanner.GetTokenType() == trinoparser.TrinoParserFROM_ {
- c.scanner.Forward(false /* skipHidden */)
- }
}
-}
-
-func (c *Completer) collectRemainingTableReferences() {
- c.scanner.Push()
-
- level := 0
- for {
- found := c.scanner.GetTokenType() == trinoparser.TrinoParserFROM_
- for !found {
- if !c.scanner.Forward(false /* skipHidden */) {
- break
- }
-
- switch c.scanner.GetTokenType() {
- case trinoparser.TrinoParserLPAREN_:
- level++
- c.referencesStack = append([][]base.TableReference{{}}, c.referencesStack...)
- case trinoparser.TrinoParserRPAREN_:
- if level > 0 {
- level--
- }
- case trinoparser.TrinoParserFROM_:
- if level == 0 {
- found = true
- }
- default:
- // Continue scanning for other tokens
- }
- }
-
- if !found {
- c.scanner.PopAndRestore()
- return // No more FROM clause found.
- }
-
- c.parseTableReferences(c.scanner.GetFollowingText())
- if c.scanner.GetTokenType() == trinoparser.TrinoParserFROM_ {
- c.scanner.Forward(false /* skipHidden */)
- }
+ if cCtx.DefaultDatabase != "" {
+ cat.SetCurrentCatalog(catalog.Normalize(cCtx.DefaultDatabase))
}
-}
-
-func (c *Completer) parseTableReferences(fromClause string) {
- input := antlr.NewInputStream(fromClause)
- lexer := trinoparser.NewTrinoLexer(input)
- tokens := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
- parser := trinoparser.NewTrinoParser(tokens)
-
- parser.BuildParseTrees = true
- parser.RemoveErrorListeners()
- tree := parser.Relation()
- listener := &tableRefListener{
- context: c,
- fromClauseMode: true,
+ if cCtx.DefaultSchema != "" {
+ cat.SetCurrentSchema(catalog.Normalize(cCtx.DefaultSchema))
}
- antlr.ParseTreeWalkerDefault.Walk(listener, tree)
+ return cat
}
-type tableRefListener struct {
- *trinoparser.BaseTrinoParserListener
-
- context *Completer
- fromClauseMode bool
- done bool
- level int
-}
-
-func (l *tableRefListener) ExitAliasedRelation(ctx *trinoparser.AliasedRelationContext) {
- if l.done {
- return
+// catalogNeeded reports whether the current completion statement needs this
+// catalog's full schema/table/column metadata loaded: true for the session
+// default catalog and for any catalog whose name appears in the statement text
+// (a qualified reference such as catalog.schema.table). The reference check is a
+// case-insensitive substring, which errs toward loading an occasional extra
+// catalog rather than missing a referenced one.
+func catalogNeeded(normName, defaultDB, lowerStmt string) bool {
+ if normName == "" {
+ return false
}
- if l.level == 0 && len(l.context.referencesStack) != 0 && len(l.context.referencesStack[0]) != 0 {
- if ctx.Identifier() != nil {
- alias := unquote(ctx.Identifier().GetText())
- if physicalTable, ok := l.context.referencesStack[0][len(l.context.referencesStack[0])-1].(*base.PhysicalTableReference); ok {
- physicalTable.Alias = alias
- return
- }
- if virtualTable, ok := l.context.referencesStack[0][len(l.context.referencesStack[0])-1].(*base.VirtualTableReference); ok {
- virtualTable.Table = alias
- }
- }
+ if defaultDB != "" && normName == defaultDB {
+ return true
}
-}
-
-func (l *tableRefListener) ExitColumnAliases(ctx *trinoparser.ColumnAliasesContext) {
- if l.done {
- return
- }
-
- if l.level == 0 && len(l.context.referencesStack) != 0 && len(l.context.referencesStack[0]) != 0 {
- if virtualTable, ok := l.context.referencesStack[0][len(l.context.referencesStack[0])-1].(*base.VirtualTableReference); ok {
- var newColumns []string
- for _, column := range ctx.AllIdentifier() {
- newColumns = append(newColumns, unquote(column.GetText()))
- }
- virtualTable.Columns = newColumns
- }
- }
-}
-
-func (l *tableRefListener) ExitQualifiedName(ctx *trinoparser.QualifiedNameContext) {
- if l.done {
- return
- }
-
- if !l.fromClauseMode || l.level == 0 {
- reference := &base.PhysicalTableReference{}
- catalog, schema, table := normalizeQualifiedNameFallback(ctx, "", "")
- reference.Database = catalog // Using Database field for Catalog in Trino
- reference.Schema = schema
- reference.Table = table
- l.context.referencesStack[0] = append(l.context.referencesStack[0], reference)
+ // A catalog name containing a double quote is rewritten inside a quoted
+ // reference (each " is escaped as ""), so its normalized form is not a literal
+ // substring of the statement and the check below would miss it. Such names are
+ // vanishingly rare; load them unconditionally rather than drop completion. A
+ // double quote is the only identifier character Trino re-spells when quoting —
+ // every other character appears verbatim, so the substring check is reliable.
+ if strings.Contains(normName, `"`) {
+ return true
}
+ return strings.Contains(lowerStmt, strings.ToLower(normName))
}
-func (l *tableRefListener) ExitTableFunctionInvocation(_ *trinoparser.TableFunctionInvocationContext) {
- if l.done {
- return
- }
-
- if !l.fromClauseMode || l.level == 0 {
- reference := &base.VirtualTableReference{}
- l.context.referencesStack[0] = append(l.context.referencesStack[0], reference)
- }
-}
-
-func (l *tableRefListener) ExitSubquery(ctx *trinoparser.SubqueryContext) {
- if l.done {
- return
- }
-
- // In Trino, subqueries must be aliased in an aliasedRelation
- pCtx, ok := ctx.GetParent().(*trinoparser.SubqueryRelationContext)
- if !ok {
- return
- }
-
- // Check for grandparent context which should be an aliasedRelation
- gCtx, ok := pCtx.GetParent().(*trinoparser.AliasedRelationContext)
- if !ok {
- return
- }
-
- var derivedTableName string
- if gCtx.Identifier() != nil {
- derivedTableName = unquote(gCtx.Identifier().GetText())
- } else {
- // If no explicit alias, we can't reference this subquery's columns
- return
- }
-
- reference := &base.VirtualTableReference{
- Table: derivedTableName,
- }
-
- if gCtx.ColumnAliases() == nil {
- // User did not specify the column alias, we should use query span to get the column alias.
- if span, err := GetQuerySpan(
- l.context.ctx,
- base.GetQuerySpanContext{
- InstanceID: l.context.instanceID,
- GetDatabaseMetadataFunc: l.context.metadataGetter,
- ListDatabaseNamesFunc: l.context.catalogNamesLister,
- },
- base.Statement{Text: fmt.Sprintf("SELECT * FROM (%s);", ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx))},
- l.context.defaultCatalog,
- l.context.defaultSchema,
- true,
- ); err == nil && span.NotFoundError == nil {
- for _, column := range span.Results {
- reference.Columns = append(reference.Columns, column.Name)
- }
- }
- } else {
- // If column aliases are specified, use them
- for _, identifier := range gCtx.ColumnAliases().AllIdentifier() {
- reference.Columns = append(reference.Columns, unquote(identifier.GetText()))
- }
- }
- l.context.referencesStack[0] = append(l.context.referencesStack[0], reference)
-}
-
-func (l *tableRefListener) EnterSubqueryRelation(*trinoparser.SubqueryRelationContext) {
- if l.done {
- return
- }
-
- if l.fromClauseMode {
- l.level++
- } else {
- l.context.referencesStack = append([][]base.TableReference{{}}, l.context.referencesStack...)
- }
-}
-
-func (l *tableRefListener) ExitSubqueryRelation(*trinoparser.SubqueryRelationContext) {
- if l.done {
- return
- }
-
- if l.fromClauseMode {
- l.level--
- } else {
- l.context.referencesStack = l.context.referencesStack[1:]
- }
-}
-
-func (c *Completer) fetchCommonTableExpression(statement string) {
- c.cteTables = nil
-
- // Extract CTEs using the cteExtractor
- extractor := &cteExtractor{
- completer: c,
- }
- input := antlr.NewInputStream(statement)
- lexer := trinoparser.NewTrinoLexer(input)
- tokens := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
- parser := trinoparser.NewTrinoParser(tokens)
- parser.BuildParseTrees = true
- parser.RemoveErrorListeners()
- tree := parser.Parse()
- antlr.ParseTreeWalkerDefault.Walk(extractor, tree)
- c.cteTables = extractor.virtualReferences
-}
-
-type cteExtractor struct {
- *trinoparser.BaseTrinoParserListener
-
- completer *Completer
- handled bool
- virtualReferences []*base.VirtualTableReference
-}
-
-func (c *cteExtractor) EnterWith(ctx *trinoparser.WithContext) {
- if c.handled {
- return
- }
- c.handled = true
-
- for _, namedQuery := range ctx.AllNamedQuery() {
- cteName := namedQuery.Identifier().GetText()
- if cteName == "" {
- continue
- }
-
- var columns []string
- if namedQuery.ColumnAliases() != nil {
- for _, columnID := range namedQuery.ColumnAliases().AllIdentifier() {
- columns = append(columns, unquote(columnID.GetText()))
- }
- c.virtualReferences = append(c.virtualReferences, &base.VirtualTableReference{
- Table: unquote(cteName),
- Columns: columns,
- })
+// columnsOf converts storepb columns into omni catalog columns (names
+// normalized).
+func columnsOf(columns []*storepb.ColumnMetadata) []*catalog.Column {
+ out := make([]*catalog.Column, 0, len(columns))
+ for _, c := range columns {
+ if c == nil {
continue
}
-
- // If column aliases are not specified, use query span to get the column names
- cteBody := ctx.GetParser().GetTokenStream().GetTextFromRuleContext(namedQuery.Query())
- statement := fmt.Sprintf("WITH %s AS (%s) SELECT * FROM %s", cteName, cteBody, cteName)
- if span, err := GetQuerySpan(
- c.completer.ctx,
- base.GetQuerySpanContext{
- InstanceID: c.completer.instanceID,
- GetDatabaseMetadataFunc: c.completer.metadataGetter,
- ListDatabaseNamesFunc: c.completer.catalogNamesLister,
- },
- base.Statement{Text: statement},
- c.completer.defaultCatalog,
- c.completer.defaultSchema,
- true,
- ); err == nil && span.NotFoundError == nil {
- var columns []string
- for _, column := range span.Results {
- columns = append(columns, column.Name)
- }
- c.virtualReferences = append(c.virtualReferences, &base.VirtualTableReference{
- Table: unquote(cteName),
- Columns: columns,
- })
- }
- }
-}
-
-func (c *Completer) fetchSelectItemAliases(ruleStack []*base.RuleContext) []string {
- canUseAliases := false
- for i := len(ruleStack) - 1; i >= 0; i-- {
- switch ruleStack[i].ID {
- case trinoparser.TrinoParserRULE_query, trinoparser.TrinoParserRULE_querySpecification:
- if !canUseAliases {
- return nil
- }
- aliasMap := make(map[string]bool)
- for pos := range ruleStack[i].SelectItemAliases {
- if aliasText := c.extractAliasText(pos); len(aliasText) > 0 {
- aliasMap[aliasText] = true
- }
- }
-
- var result []string
- for alias := range aliasMap {
- result = append(result, alias)
- }
- slices.Sort(result)
- return result
- case trinoparser.TrinoParserRULE_groupBy, trinoparser.TrinoParserRULE_sortItem, trinoparser.TrinoParserRULE_booleanExpression:
- // These represent ORDER BY, GROUP BY, and HAVING contexts
- canUseAliases = true
- default:
- // Continue iterating through other rule types
- }
- }
-
- return nil
-}
-
-func (c *Completer) extractAliasText(pos int) string {
- followingText := c.scanner.GetFollowingTextAfter(pos)
- if len(followingText) == 0 {
- return ""
- }
-
- input := antlr.NewInputStream(followingText)
- lexer := trinoparser.NewTrinoLexer(input)
- tokens := antlr.NewCommonTokenStream(lexer, 0)
- parser := trinoparser.NewTrinoParser(tokens)
-
- parser.BuildParseTrees = true
- parser.RemoveErrorListeners()
- // Use the as_column_alias rule for column aliases, matching TSQL's approach
- tree := parser.As_column_alias()
-
- listener := &SelectAliasListener{}
- antlr.ParseTreeWalkerDefault.Walk(listener, tree)
-
- return listener.result
-}
-
-type SelectAliasListener struct {
- *trinoparser.BaseTrinoParserListener
-
- result string
-}
-
-func (l *SelectAliasListener) EnterAs_column_alias(ctx *trinoparser.As_column_aliasContext) {
- l.result = unquote(ctx.Column_alias().GetText())
-}
-
-func (c *Completer) quotedIdentifierIfNeeded(identifier string) string {
- // If we're already in a quoted context, return the identifier as-is
- if c.caretTokenIsQuoted != quotedTypeNone {
- return identifier
- }
-
- // In Trino, certain identifiers need to be quoted:
- // 1. If they contain special characters or spaces
- // 2. If they're case-sensitive
- // 3. If they're reserved keywords
-
- // Check for characters that would require quoting
- needsQuoting := false
- for i, r := range identifier {
- // First character must be a letter or underscore
- if i == 0 && !unicode.IsLetter(r) && r != '_' {
- needsQuoting = true
- break
- }
-
- // Other characters must be letters, numbers, or underscores
- if !unicode.IsLetter(r) && !unicode.IsDigit(r) && r != '_' {
- needsQuoting = true
- break
- }
- }
-
- // Quote the identifier if needed
- if needsQuoting {
- return fmt.Sprintf("\"%s\"", identifier)
- }
-
- return identifier
-}
-
-// Helper functions for handling Trino identifiers
-
-// isIdentifier checks if token is one of the identifier types in Trino
-func isIdentifier(tokenType int) bool {
- return tokenType == trinoparser.TrinoParserIDENTIFIER_ ||
- tokenType == trinoparser.TrinoParserQUOTED_IDENTIFIER_ ||
- tokenType == trinoparser.TrinoParserDIGIT_IDENTIFIER_ ||
- tokenType == trinoparser.TrinoParserBACKQUOTED_IDENTIFIER_
-}
-
-// normalizeIdentifierText normalizes an identifier text by unquoting it if needed
-func normalizeIdentifierText(text string) string {
- return unquote(text)
-}
-
-// unquote removes quotes from identifiers if present
-func unquote(text string) string {
- // Remove double quotes from quoted identifiers
- if len(text) >= 2 && text[0] == '"' && text[len(text)-1] == '"' {
- return text[1 : len(text)-1]
- }
-
- // Remove backticks from backticked identifiers
- if len(text) >= 2 && text[0] == '`' && text[len(text)-1] == '`' {
- return text[1 : len(text)-1]
- }
-
- return text
-}
-
-// normalizeQualifiedNameFallback extracts the catalog, schema, and table names from a QualifiedNameContext
-// In Trino, a qualifiedName can have 1-3 parts: [catalog.]schema.table or just table
-func normalizeQualifiedNameFallback(ctx *trinoparser.QualifiedNameContext, _ string, _ string) (string, string, string) {
- parts := []string{}
-
- // Extract all identifiers from the qualified name
- for _, identifier := range ctx.AllIdentifier() {
- parts = append(parts, unquote(identifier.GetText()))
- }
-
- catalog, schema, table := "", "", ""
-
- // Assign parts based on how many we found
- switch len(parts) {
- case 1:
- // Just a table name
- table = parts[0]
- case 2:
- // schema.table
- schema = parts[0]
- table = parts[1]
- case 3:
- // catalog.schema.table
- catalog = parts[0]
- schema = parts[1]
- table = parts[2]
+ out = append(out, catalog.NewColumn(catalog.Normalize(c.Name), c.Type, c.Nullable))
+ }
+ return out
+}
+
+// convertCandidateType maps an omni completion candidate type onto the bytebase
+// base.CandidateType. A Trino catalog corresponds to a bytebase DATABASE.
+func convertCandidateType(t completion.CandidateType) base.CandidateType {
+ switch t {
+ case completion.CandidateKeyword:
+ return base.CandidateTypeKeyword
+ case completion.CandidateCatalog:
+ return base.CandidateTypeDatabase
+ case completion.CandidateSchema:
+ return base.CandidateTypeSchema
+ case completion.CandidateTable:
+ return base.CandidateTypeTable
+ case completion.CandidateView:
+ return base.CandidateTypeView
+ case completion.CandidateColumn:
+ return base.CandidateTypeColumn
default:
- // Handle other cases (0 or more than 3 parts)
+ return base.CandidateTypeNone
}
-
- return catalog, schema, table
}
-// skipHeadingSQLWithoutSemicolon skips the heading SQL statements that don't end with semicolon,
-// by detecting SELECT at column 0. This is similar to TSQL's implementation.
-func skipHeadingSQLWithoutSemicolon(statement string, caretLine int, caretOffset int) (string, int, int) {
- input := antlr.NewInputStream(statement)
- lexer := trinoparser.NewTrinoLexer(input)
- stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
- lexer.RemoveErrorListeners()
- lexerErrorListener := &base.ParseErrorListener{
- Statement: statement,
- }
- lexer.AddErrorListener(lexerErrorListener)
-
- stream.Fill()
- tokens := stream.GetAllTokens()
- latestSelect := 0
- newCaretLine, newCaretOffset := caretLine, caretOffset
-
- for _, token := range tokens {
- // We want to find SELECT statements that come BEFORE the caret position
- // but we should skip the one we're currently in
- if token.GetLine() > caretLine || (token.GetLine() == caretLine && token.GetColumn() >= caretOffset) {
- break
+// caretToByteOffset converts a (1-based line, 0-based column) caret position
+// into a byte offset into statement. The column is interpreted as a UTF-16
+// code-unit offset (the monaco/LSP convention the caller uses), matching how
+// the completion test derives the caret from a "|" marker.
+func caretToByteOffset(statement string, caretLine, caretColumn int) int {
+ line := 1
+ col := 0 // UTF-16 code units consumed on the current line
+ for i := 0; i < len(statement); {
+ if line == caretLine && col >= caretColumn {
+ return i
}
-
- if token.GetTokenType() == trinoparser.TrinoParserSELECT_ && token.GetColumn() == 0 {
- latestSelect = token.GetTokenIndex()
- newCaretLine = caretLine - token.GetLine() + 1 // convert to 1-based
- // When we're on the same line as the SELECT, we need to adjust the offset
- // by the position where we start extracting the substring
- if token.GetLine() == caretLine {
- // The token's column is where the SELECT starts in the original string
- // We need to subtract this from the caret offset since we're extracting from this position
- newCaretOffset = caretOffset - token.GetColumn()
- } else {
- newCaretOffset = caretOffset
+ r, size := utf8.DecodeRuneInString(statement[i:])
+ if r == '\n' {
+ if line == caretLine {
+ // Caret column is past the end of this line; clamp to line end.
+ return i
}
+ line++
+ col = 0
+ } else if r <= 0xFFFF {
+ col++
+ } else {
+ col += 2 // surrogate pair
}
+ i += size
}
-
- if latestSelect == 0 {
- return statement, caretLine, caretOffset
- }
-
- // Extract the substring starting from the SELECT token
- result := stream.GetTextFromInterval(antlr.NewInterval(latestSelect, stream.Size()))
- return result, newCaretLine, newCaretOffset
+ return len(statement)
}
diff --git a/backend/plugin/parser/trino/completion_test.go b/backend/plugin/parser/trino/completion_test.go
index 99ddb82f5f..7069f996de 100644
--- a/backend/plugin/parser/trino/completion_test.go
+++ b/backend/plugin/parser/trino/completion_test.go
@@ -2,111 +2,227 @@ package trino
import (
"context"
- "io"
- "os"
- "slices"
"strings"
"testing"
"github.com/pkg/errors"
+ "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
- "gopkg.in/yaml.v3"
- "github.com/bytebase/bytebase/backend/common/yamltest"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
"github.com/bytebase/bytebase/backend/store/model"
)
-type candidatesTest struct {
- Description string `yaml:"description"`
- Input string `yaml:"input"`
- Want []base.Candidate `yaml:"want"`
-}
-
-// TestCompletion tests the Transact-SQL auto-completion, all the test cases are stored in the file.
-//
-// - Description: The description of the test case.
+// TestCompletion exercises the omni-backed Trino completer through the public
+// Completion entry point.
//
-// - Input: The input statement with the caret position marked by "|".
+// DIVERGENCE FROM THE LEGACY ANTLR COMPLETER (intentional, documented):
+// The previous Completion was a ~1730-line CodeCompletionCore (c3) port. The
+// omni Trino completer is explicitly NOT a c3 port (see the omni completion
+// package doc) — it is a self-contained, lexer+context completer. Three
+// behavioural differences fall out of that and out of Trino's identifier rules:
//
-// - Want: The expected completion candidates.
+// 1. Identifier case. Trino folds unquoted identifiers to lower case, and the
+// omni catalog stores/returns the normalized (lower-case) form. So a table
+// stored as "Employees" is offered as "employees". The legacy completer
+// preserved the metadata's original case.
+// 2. No Definition / Priority. omni candidates carry only {Text, Type}; the
+// legacy "catalog.schema.table | type, NOT NULL" Definition string and the
+// priority ranking are not produced, so those fields are empty.
+// 3. Candidate set in column / FROM contexts. omni resolves in-scope columns
+// via query-span analysis against the *current* catalog/schema (set here
+// from DefaultDatabase/DefaultSchema). The legacy completer additionally
+// surfaced every column of the default schema ranked by priority; omni does
+// not.
//
-// Our Test suite will determine the caret position - line(0-based) and column(1-based) by the position of the "|", actually,
-// this caret position is as same as the position in the monaco-editor(LSP?).
+// These cases therefore assert the candidate *set* (Text, Type) the omni
+// completer actually returns, rather than the legacy YAML fixture.
func TestCompletion(t *testing.T) {
- tests := []candidatesTest{}
-
- const (
- record = false
- )
- var (
- filepath = "test-data/completion/test_completion.yaml"
- )
+ type want struct {
+ text string
+ typ base.CandidateType
+ }
+ tests := []struct {
+ description string
+ input string // caret marked by "|"
+ // wantPresent candidates that MUST appear.
+ wantPresent []want
+ // wantAbsentText candidate texts that must NOT appear (any type).
+ wantAbsentText []string
+ }{
+ {
+ description: "FROM offers catalogs, schemas, and current-schema tables",
+ input: "SELECT * FROM |",
+ wantPresent: []want{
+ {"company", base.CandidateTypeDatabase},
+ {"school", base.CandidateTypeDatabase},
+ {"dbo", base.CandidateTypeSchema},
+ {"myschema", base.CandidateTypeSchema},
+ {"address", base.CandidateTypeTable},
+ {"employees", base.CandidateTypeTable},
+ },
+ },
+ {
+ description: "Schema-qualified FROM offers that schema's tables",
+ input: "SELECT * FROM dbo.|",
+ wantPresent: []want{
+ {"address", base.CandidateTypeTable},
+ {"employees", base.CandidateTypeTable},
+ },
+ wantAbsentText: []string{"salarylevel"},
+ },
+ {
+ description: "Schema-qualified FROM for a non-current schema",
+ input: "SELECT * FROM myschema.|",
+ wantPresent: []want{
+ {"salarylevel", base.CandidateTypeTable},
+ },
+ },
+ {
+ description: "SELECT column context offers in-scope columns",
+ input: "SELECT | FROM Employees",
+ wantPresent: []want{
+ {"id", base.CandidateTypeColumn},
+ {"name", base.CandidateTypeColumn},
+ },
+ },
+ {
+ description: "WHERE column context offers in-scope columns",
+ input: "SELECT * FROM Employees WHERE |",
+ wantPresent: []want{
+ {"id", base.CandidateTypeColumn},
+ {"name", base.CandidateTypeColumn},
+ },
+ },
+ {
+ description: "Alias-qualified column context",
+ input: "SELECT tableAlias.| FROM Employees AS tableAlias",
+ wantPresent: []want{
+ {"id", base.CandidateTypeColumn},
+ {"name", base.CandidateTypeColumn},
+ },
+ },
+ {
+ description: "CTE name offered as a relation after FROM",
+ input: "WITH MyCTE_01 AS (SELECT * FROM dbo.Employees) SELECT * FROM |",
+ wantPresent: []want{
+ {"mycte_01", base.CandidateTypeTable},
+ },
+ },
+ }
a := require.New(t)
- yamlFile, err := os.Open(filepath)
- a.NoError(err)
-
- byteValue, err := io.ReadAll(yamlFile)
- a.NoError(yamlFile.Close())
- a.NoError(err)
- a.NoError(yaml.Unmarshal(byteValue, &tests))
-
- for i, test := range tests {
- statement, caretLine, caretPosition := getCaretPosition(test.Input)
- getter, lister := buildMockDatabaseMetadataGetterLister()
- results, err := Completion(context.Background(), base.CompletionContext{
- Scene: base.SceneTypeAll,
- DefaultDatabase: "Company",
- Metadata: getter,
- ListDatabaseNames: lister,
- }, statement, caretLine, caretPosition)
- a.NoErrorf(err, "Case %02d: %s", i, test.Description)
- var filteredResult []base.Candidate
- for _, r := range results {
- switch r.Type {
- case base.CandidateTypeKeyword, base.CandidateTypeFunction:
- continue
- default:
- filteredResult = append(filteredResult, r)
- }
- }
- if filteredResult == nil {
- filteredResult = []base.Candidate{}
- }
- slices.SortFunc(filteredResult, func(i, j base.Candidate) int {
- if i.Type != j.Type {
- if i.Type < j.Type {
- return -1
- }
- return 1
+ for _, test := range tests {
+ t.Run(test.description, func(t *testing.T) {
+ statement, caretLine, caretOffset := getCaretPosition(test.input)
+ getter, lister := buildMockDatabaseMetadataGetterLister()
+ results, err := Completion(context.Background(), base.CompletionContext{
+ Scene: base.SceneTypeAll,
+ DefaultDatabase: "Company",
+ DefaultSchema: "dbo",
+ Metadata: getter,
+ ListDatabaseNames: lister,
+ }, statement, caretLine, caretOffset)
+ a.NoErrorf(err, "%s", test.description)
+
+ for _, w := range test.wantPresent {
+ assert.Truef(t, hasCandidate(results, w.text, w.typ),
+ "%s: expected candidate {%s, %s}; got %v", test.description, w.text, w.typ, candidateSummary(results))
}
- if i.Text != j.Text {
- if i.Text < j.Text {
- return -1
+ for _, text := range test.wantAbsentText {
+ for _, r := range results {
+ assert.NotEqualf(t, text, r.Text, "%s: candidate %q should be absent", test.description, text)
}
- return 1
- }
- if i.Definition < j.Definition {
- return -1
}
- if i.Definition > j.Definition {
- return 1
- }
- return 0
})
+ }
+}
- if record {
- tests[i].Want = filteredResult
- } else {
- a.Equalf(test.Want, filteredResult, "Case %02d: %s\nInput: %s", i, test.Description, test.Input)
+// TestCompletionNilMetadata verifies Completion does not panic and returns only
+// keyword/CTE candidates (no object candidates) when no metadata is wired.
+func TestCompletionNilMetadata(t *testing.T) {
+ results, err := Completion(context.Background(), base.CompletionContext{
+ Scene: base.SceneTypeAll,
+ }, "SELECT * FROM ", 1, len("SELECT * FROM "))
+ require.NoError(t, err)
+ for _, r := range results {
+ switch r.Type {
+ case base.CandidateTypeDatabase, base.CandidateTypeSchema, base.CandidateTypeTable, base.CandidateTypeView, base.CandidateTypeColumn:
+ t.Errorf("nil metadata produced object candidate {%s, %s}", r.Text, r.Type)
+ default:
}
}
+}
- if record {
- yamltest.Record(t, filepath, tests)
+// TestCompletionLoadsOnlyNeededCatalogs verifies that completion loads full
+// metadata only for the default catalog and catalogs named in the statement, not
+// for every catalog on the instance. On a Trino instance federating many
+// catalogs, eager loading would fan out a metadata fetch per keystroke.
+func TestCompletionLoadsOnlyNeededCatalogs(t *testing.T) {
+ metas := []*storepb.DatabaseSchemaMetadata{
+ {Name: "Company", Schemas: []*storepb.SchemaMetadata{{Name: "dbo", Tables: []*storepb.TableMetadata{{Name: "Employees", Columns: []*storepb.ColumnMetadata{{Name: "Id", Type: "int"}}}}}}},
+ {Name: "School", Schemas: []*storepb.SchemaMetadata{{Name: "sch", Tables: []*storepb.TableMetadata{{Name: "Student", Columns: []*storepb.ColumnMetadata{{Name: "Id", Type: "int"}}}}}}},
+ {Name: "Warehouse", Schemas: []*storepb.SchemaMetadata{{Name: "wh", Tables: []*storepb.TableMetadata{{Name: "Item", Columns: []*storepb.ColumnMetadata{{Name: "Id", Type: "int"}}}}}}},
+ }
+ loads := map[string]int{}
+ getter := func(_ context.Context, _, databaseName string) (string, *model.DatabaseMetadata, error) {
+ loads[databaseName]++
+ for _, m := range metas {
+ if m.Name == databaseName {
+ return "", model.NewDatabaseMetadata(m, nil, nil, storepb.Engine_MYSQL, false /* isObjectCaseSensitive */), nil
+ }
+ }
+ return "", nil, errors.Errorf("database %q not found", databaseName)
+ }
+ lister := func(context.Context, string) ([]string, error) {
+ return []string{"Company", "School", "Warehouse"}, nil
+ }
+ cCtx := base.CompletionContext{
+ Scene: base.SceneTypeAll,
+ DefaultDatabase: "Company",
+ DefaultSchema: "dbo",
+ Metadata: getter,
+ ListDatabaseNames: lister,
+ }
+
+ // Completing within the default catalog must not load School or Warehouse.
+ stmt := "SELECT * FROM "
+ _, err := Completion(context.Background(), cCtx, stmt, 1, len(stmt))
+ require.NoError(t, err)
+ assert.Zerof(t, loads["School"], "unreferenced catalog School must not be loaded; loads=%v", loads)
+ assert.Zerof(t, loads["Warehouse"], "unreferenced catalog Warehouse must not be loaded; loads=%v", loads)
+ assert.NotZerof(t, loads["Company"], "default catalog Company should be loaded; loads=%v", loads)
+
+ // Naming a catalog in the statement loads it — and still not the others.
+ for k := range loads {
+ delete(loads, k)
+ }
+ stmt = "SELECT * FROM School."
+ _, err = Completion(context.Background(), cCtx, stmt, 1, len(stmt))
+ require.NoError(t, err)
+ assert.NotZerof(t, loads["School"], "referenced catalog School should be loaded; loads=%v", loads)
+ assert.Zerof(t, loads["Warehouse"], "still-unreferenced catalog Warehouse must not be loaded; loads=%v", loads)
+}
+
+func hasCandidate(cands []base.Candidate, text string, typ base.CandidateType) bool {
+ for _, c := range cands {
+ if c.Text == text && c.Type == typ {
+ return true
+ }
+ }
+ return false
+}
+
+func candidateSummary(cands []base.Candidate) []string {
+ out := make([]string, 0, len(cands))
+ for _, c := range cands {
+ if c.Type == base.CandidateTypeKeyword {
+ continue
+ }
+ out = append(out, string(c.Type)+":"+c.Text)
}
+ return out
}
func getCaretPosition(statement string) (string, int, int) {
@@ -131,27 +247,15 @@ var databaseMetadatas = []*storepb.DatabaseSchemaMetadata{
{
Name: "Employees",
Columns: []*storepb.ColumnMetadata{
- {
- Name: "Id",
- Type: "int",
- },
- {
- Name: "Name",
- Type: "varchar",
- },
+ {Name: "Id", Type: "int"},
+ {Name: "Name", Type: "varchar"},
},
},
{
Name: "Address",
Columns: []*storepb.ColumnMetadata{
- {
- Name: "EmployeeId",
- Type: "int",
- },
- {
- Name: "Street",
- Type: "varchar",
- },
+ {Name: "EmployeeId", Type: "int"},
+ {Name: "Street", Type: "varchar"},
},
},
},
@@ -162,14 +266,8 @@ var databaseMetadatas = []*storepb.DatabaseSchemaMetadata{
{
Name: "SalaryLevel",
Columns: []*storepb.ColumnMetadata{
- {
- Name: "Id",
- Type: "int",
- },
- {
- Name: "SalaryUpBound",
- Type: "int",
- },
+ {Name: "Id", Type: "int"},
+ {Name: "SalaryUpBound", Type: "int"},
},
},
},
@@ -185,14 +283,8 @@ var databaseMetadatas = []*storepb.DatabaseSchemaMetadata{
{
Name: "Student",
Columns: []*storepb.ColumnMetadata{
- {
- Name: "Id",
- Type: "int",
- },
- {
- Name: "ParentName",
- Type: "varchar",
- },
+ {Name: "Id", Type: "int"},
+ {Name: "ParentName", Type: "varchar"},
},
},
},
diff --git a/backend/plugin/parser/trino/diagnose.go b/backend/plugin/parser/trino/diagnose.go
index 99a21b58fa..d80a7e4a87 100644
--- a/backend/plugin/parser/trino/diagnose.go
+++ b/backend/plugin/parser/trino/diagnose.go
@@ -3,10 +3,8 @@ package trino
import (
"context"
"strings"
- "unicode"
- "github.com/antlr4-go/antlr/v4"
- parser "github.com/bytebase/parser/trino"
+ "github.com/bytebase/omni/trino/parser"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
@@ -16,60 +14,42 @@ func init() {
base.RegisterDiagnoseFunc(storepb.Engine_TRINO, Diagnose)
}
-// Diagnose diagnoses the SQL statement and returns any syntax errors.
+// Diagnose returns syntax diagnostics for the given Trino statement. It surfaces
+// genuine lex/parse errors from the omni parser.
+//
+// The omni Trino parser emits a "<NAME> statement parsing is not yet supported"
+// stub error from a handful of dispatch fallthroughs. Most of those fire only on
+// genuinely malformed input — every valid CREATE/DROP/ALTER object keyword is
+// implemented, so e.g. "CREATE TABLEE ..." (an unrecognized object) is a true
+// SYNTAX_ERROR (confirmed against the Trino 481 oracle) and we must keep it. The
+// only valid-Trino forms that still hit the stub are DESCRIBE INPUT / DESCRIBE
+// OUTPUT (oracle: NOT_FOUND, i.e. they parse). We suppress ONLY those so the
+// editor does not show a false positive on valid SQL; everything else passes
+// through. When omni implements DESCRIBE INPUT/OUTPUT the stub stops firing and
+// this filter becomes a no-op.
func Diagnose(_ context.Context, _ base.DiagnoseContext, statement string) ([]base.Diagnostic, error) {
- diagnostics := make([]base.Diagnostic, 0)
- syntaxError := parseTrinoStatement(statement)
- if syntaxError != nil {
- diagnostics = append(diagnostics, base.ConvertSyntaxErrorToDiagnostic(syntaxError, statement))
+ diags := parser.Diagnose(statement)
+ out := make([]base.Diagnostic, 0, len(diags))
+ mapper := base.NewByteOffsetPositionMapper(statement)
+ for _, d := range diags {
+ if isValidButUnimplementedStub(d.Msg) {
+ continue
+ }
+ syntaxErr := &base.SyntaxError{
+ Position: mapper.Position(d.Loc.Start),
+ Message: d.Msg,
+ }
+ out = append(out, base.ConvertSyntaxErrorToDiagnostic(syntaxErr, statement))
}
-
- return diagnostics, nil
+ return out, nil
}
-// parseTrinoStatement parses the given SQL and returns any syntax errors.
-func parseTrinoStatement(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
- statement += ";"
- }
-
- // Create lexer and parser
- lexer := parser.NewTrinoLexer(antlr.NewInputStream(statement))
- stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
- p := parser.NewTrinoParser(stream)
-
- // Set up error listeners
- startPosition := &storepb.Position{Line: 1}
- lexerErrorListener := &base.ParseErrorListener{
- Statement: statement,
- StartPosition: startPosition,
- }
- lexer.RemoveErrorListeners()
- lexer.AddErrorListener(lexerErrorListener)
-
- parserErrorListener := &base.ParseErrorListener{
- Statement: statement,
- StartPosition: startPosition,
- }
- p.RemoveErrorListeners()
- p.AddErrorListener(parserErrorListener)
-
- // No need to build parse trees for just syntax checking
- p.BuildParseTrees = false
-
- // Parse the statement
- _ = p.SingleStatement()
-
- // Return any errors
- if lexerErrorListener.Err != nil {
- return lexerErrorListener.Err
- }
-
- if parserErrorListener.Err != nil {
- return parserErrorListener.Err
- }
-
- return nil
+// isValidButUnimplementedStub reports whether msg is an omni "not yet supported"
+// stub error for a statement form that is valid Trino (so it must not be flagged
+// as a syntax error). Today that is only DESCRIBE INPUT / DESCRIBE OUTPUT; the
+// CREATE/DROP/ALTER stubs fire exclusively on unrecognized object keywords,
+// which are genuine syntax errors, so they are intentionally NOT matched here.
+func isValidButUnimplementedStub(msg string) bool {
+ return strings.HasPrefix(msg, "DESCRIBE ") &&
+ strings.HasSuffix(msg, "statement parsing is not yet supported")
}
diff --git a/backend/plugin/parser/trino/diagnose_test.go b/backend/plugin/parser/trino/diagnose_test.go
index c3970907dd..8eda99f4ff 100644
--- a/backend/plugin/parser/trino/diagnose_test.go
+++ b/backend/plugin/parser/trino/diagnose_test.go
@@ -42,9 +42,14 @@ func TestDiagnose(t *testing.T) {
expectErr: true,
},
{
+ // DIVERGENCE: the legacy ANTLR path appended a ';' to empty input and
+ // reported a syntax error on the lone ';'. The omni parser correctly
+ // treats empty / whitespace-only input as zero statements with no
+ // error — an empty editor buffer is not a syntax error. So no
+ // diagnostic is expected here now.
name: "Empty statement",
sql: "",
- expectErr: true,
+ expectErr: false,
},
}
@@ -90,7 +95,7 @@ func TestParseSyntaxError(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
- err := parseTrinoStatement(tt.sql)
+ _, err := parseTrinoSQL(tt.sql)
if tt.expectErr {
assert.NotNil(t, err, "Expected syntax error")
} else {
diff --git a/backend/plugin/parser/trino/parser_test.go b/backend/plugin/parser/trino/parser_test.go
index 9533956925..84faeb1d53 100644
--- a/backend/plugin/parser/trino/parser_test.go
+++ b/backend/plugin/parser/trino/parser_test.go
@@ -1,48 +1,39 @@
package trino
import (
- "context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
)
func TestTrinoParser(t *testing.T) {
- // Simple query parsing test
+ // Simple query parsing test.
sql := "SELECT a.id, b.name FROM table1 a JOIN table2 b ON a.id = b.id"
- results, err := ParseTrino(sql)
+ results, err := parseTrinoSQL(sql)
require.NoError(t, err, "Failed to parse valid SQL")
require.Len(t, results, 1, "Should parse exactly one statement")
- require.NotNil(t, results[0].Tree, "Parse tree should not be nil")
+ require.NotNil(t, results[0].Node(), "Parse tree should not be nil")
- // Verify statement type
- stmtType := GetStatementType(results[0].Tree)
- assert.Equal(t, Select, stmtType, "Statement should be recognized as SELECT")
+ // Verify the statement is read-only.
+ assert.True(t, IsReadOnlyStatement(results[0].Node()), "SELECT statement should be read-only")
- // Verify the statement is read-only
- isReadOnly := IsReadOnlyStatement(results[0].Tree)
- assert.True(t, isReadOnly, "SELECT statement should be read-only")
-
- // Test data-changing statement detection
+ // Test data-changing statement detection.
sqlDML := "INSERT INTO users (id, name) VALUES (1, 'John')"
- resultsDML, err := ParseTrino(sqlDML)
+ resultsDML, err := parseTrinoSQL(sqlDML)
require.NoError(t, err)
require.Len(t, resultsDML, 1)
- isDML := IsDataChangingStatement(resultsDML[0].Tree)
- assert.True(t, isDML, "INSERT statement should be data-changing")
+ assert.True(t, IsDataChangingStatement(resultsDML[0].Node()), "INSERT statement should be data-changing")
- // Test schema-changing statement detection
+ // Test schema-changing statement detection.
sqlDDL := "CREATE TABLE test (id INT, name VARCHAR)"
- resultsDDL, err := ParseTrino(sqlDDL)
+ resultsDDL, err := parseTrinoSQL(sqlDDL)
require.NoError(t, err)
require.Len(t, resultsDDL, 1)
- isDDL := IsSchemaChangingStatement(resultsDDL[0].Tree)
- assert.True(t, isDDL, "CREATE TABLE should be schema-changing")
+ assert.True(t, IsSchemaChangingStatement(resultsDDL[0].Node()), "CREATE TABLE should be schema-changing")
}
func TestTrinoQueryType(t *testing.T) {
@@ -84,7 +75,7 @@ func TestTrinoQueryType(t *testing.T) {
},
{
sql: "EXPLAIN ANALYZE SELECT * FROM users;",
- expected: base.Select, // Special case for EXPLAIN ANALYZE
+ expected: base.Select, // EXPLAIN ANALYZE executes the query, so it is classified as Select.
},
{
sql: "SHOW TABLES;",
@@ -94,111 +85,14 @@ func TestTrinoQueryType(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.sql, func(t *testing.T) {
- results, err := ParseTrino(tc.sql)
- require.NoError(t, err)
- require.Len(t, results, 1)
-
- queryType, _ := getQueryType(results[0].Tree)
+ // queryTypeFromText applies the same system-schema promotion the
+ // query-span path uses, so a SELECT over system.* is SelectInfoSchema.
+ queryType := queryTypeFromText(tc.sql)
assert.Equal(t, tc.expected, queryType)
})
}
}
-func TestGetQuerySpan(t *testing.T) {
- testCases := []struct {
- name string
- sql string
- database string
- schema string
- wantType base.QueryType
- }{
- {
- name: "Simple SELECT",
- sql: "SELECT id, name FROM users;",
- database: "mydb",
- schema: "public",
- wantType: base.Select,
- },
- {
- name: "SELECT with JOIN",
- sql: "SELECT u.id, u.name, o.product FROM users u JOIN orders o ON u.id = o.user_id;",
- database: "mydb",
- schema: "public",
- wantType: base.Select,
- },
- {
- name: "INSERT",
- sql: "INSERT INTO users (id, name) VALUES (1, 'John');",
- database: "mydb",
- schema: "public",
- wantType: base.DML,
- },
- {
- name: "UPDATE",
- sql: "UPDATE users SET name = 'Jane' WHERE id = 1;",
- database: "mydb",
- schema: "public",
- wantType: base.DML,
- },
- {
- name: "DELETE",
- sql: "DELETE FROM users WHERE id = 1;",
- database: "mydb",
- schema: "public",
- wantType: base.DML,
- },
- {
- name: "CREATE TABLE",
- sql: "CREATE TABLE users (id INT, name VARCHAR);",
- database: "mydb",
- schema: "public",
- wantType: base.DDL,
- },
- {
- name: "EXPLAIN",
- sql: "EXPLAIN SELECT * FROM users;",
- database: "mydb",
- schema: "public",
- wantType: base.Explain,
- },
- {
- name: "EXPLAIN ANALYZE",
- sql: "EXPLAIN ANALYZE SELECT * FROM users;",
- database: "mydb",
- schema: "public",
- wantType: base.Select,
- },
- }
-
- for _, tc := range testCases {
- t.Run(tc.name, func(t *testing.T) {
- // Create a mock GetQuerySpanContext without metadata function
- gCtx := base.GetQuerySpanContext{
- InstanceID: "test-instance",
- Engine: storepb.Engine_TRINO,
- // No metadata function provided - mock implementation will handle this
- }
-
- // Special handling for non-SELECT queries that will skip metadata lookup
- if tc.wantType != base.Select && tc.wantType != base.Explain && tc.wantType != base.SelectInfoSchema {
- span, err := GetQuerySpan(context.Background(), gCtx, base.Statement{Text: tc.sql}, tc.database, tc.schema, true)
- if assert.NoError(t, err) {
- assert.Equal(t, tc.wantType, span.Type)
- assert.Empty(t, span.Results, "Results should be empty for non-SELECT queries")
- }
- return
- }
-
- // For SELECT queries, we expect a basic query span with the correct type
- span, err := GetQuerySpan(context.Background(), gCtx, base.Statement{Text: tc.sql}, tc.database, tc.schema, true)
- if assert.NoError(t, err) {
- assert.Equal(t, tc.wantType, span.Type)
- }
- })
- }
-}
-
func TestMain(m *testing.M) {
- // Run the tests and print results to stdout
m.Run()
}
diff --git a/backend/plugin/parser/trino/query.go b/backend/plugin/parser/trino/query.go
index face169e87..af341521c2 100644
--- a/backend/plugin/parser/trino/query.go
+++ b/backend/plugin/parser/trino/query.go
@@ -9,14 +9,19 @@ func init() {
base.RegisterQueryValidator(storepb.Engine_TRINO, validateQuery)
}
-// validateQuery validates if the given SQL statement is valid for SQL editor.
-// We only allow read-only queries in the SQL editor.
-// Returns (canRunInReadOnly, returnsData, error):
-// - canRunInReadOnly: whether all queries can run in read-only mode
-// - returnsData: whether all queries return data
-// - error: parsing error if the statement is invalid
+// validateQuery reports whether the given statement is valid for the SQL editor,
+// which only permits read-only queries.
+//
+// It returns (canRunInReadOnly, returnsData, error):
+// - canRunInReadOnly: every statement can run in read-only mode;
+// - returnsData: every statement returns data;
+// - error: a syntax error if the statement is invalid.
+//
+// EXPLAIN ANALYZE is special-cased exactly as the legacy plugin did: because it
+// executes the inner query, it is only accepted when getQueryType reports it as
+// read-only (base.Select); it then counts as read-only and data-returning.
func validateQuery(statement string) (bool, bool, error) {
- parseResults, err := ParseTrino(statement)
+ parsed, err := parseTrinoSQL(statement)
if err != nil {
return false, false, err
}
@@ -24,29 +29,22 @@ func validateQuery(statement string) (bool, bool, error) {
allReadOnly := true
allReturnData := true
- // Validate each statement
- for _, result := range parseResults {
- queryType, isAnalyze := getQueryType(result.Tree)
+ for _, p := range parsed {
+ queryType, isAnalyze := getQueryType(p.Node())
- // If it's an EXPLAIN ANALYZE, the query will be executed
if isAnalyze {
- // Only allow EXPLAIN ANALYZE for SELECT statements
+ // EXPLAIN ANALYZE runs the query. Only allow it when the type is a
+ // read-only SELECT; in that case it is read-only and returns data.
if queryType != base.Select {
return false, false, nil
}
- // EXPLAIN ANALYZE SELECT is read-only and returns data
continue
}
- // Determine if the statement is read-only
readOnly := queryType == base.Select ||
queryType == base.Explain ||
queryType == base.SelectInfoSchema
-
- // Determine if the statement returns data
- returnsData := queryType == base.Select ||
- queryType == base.Explain ||
- queryType == base.SelectInfoSchema
+ returnsData := readOnly
if !readOnly {
allReadOnly = false
@@ -54,8 +52,6 @@ func validateQuery(statement string) (bool, bool, error) {
if !returnsData {
allReturnData = false
}
-
- // If any statement fails validation, return immediately
if !allReadOnly {
break
}
diff --git a/backend/plugin/parser/trino/query_span.go b/backend/plugin/parser/trino/query_span.go
index bfc21b7b8a..24dabc5cf1 100644
--- a/backend/plugin/parser/trino/query_span.go
+++ b/backend/plugin/parser/trino/query_span.go
@@ -13,8 +13,8 @@ func init() {
base.RegisterGetQuerySpan(storepb.Engine_TRINO, GetQuerySpan)
}
-// GetQuerySpan gets the query span for Trino.
-// This is the entry point registered with the base package.
+// GetQuerySpan gets the query span for a Trino statement. It is the entry point
+// registered with the base package.
func GetQuerySpan(ctx context.Context, gCtx base.GetQuerySpanContext, stmt base.Statement, database, schema string, ignoreCaseSensitive bool) (*base.QuerySpan, error) {
extractor := newQuerySpanExtractor(database, schema, gCtx, ignoreCaseSensitive)
diff --git a/backend/plugin/parser/trino/query_span_extractor.go b/backend/plugin/parser/trino/query_span_extractor.go
index cbb95a3c17..98c6104715 100644
--- a/backend/plugin/parser/trino/query_span_extractor.go
+++ b/backend/plugin/parser/trino/query_span_extractor.go
@@ -4,17 +4,33 @@ import (
"context"
"strings"
- "github.com/antlr4-go/antlr/v4"
- parser "github.com/bytebase/parser/trino"
+ "github.com/bytebase/omni/trino/analysis"
+ "github.com/bytebase/omni/trino/ast"
+ "github.com/bytebase/omni/trino/parser"
"github.com/pkg/errors"
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
"github.com/bytebase/bytebase/backend/store/model"
)
-// querySpanExtractor extracts query spans from Trino statements.
-// This follows the TSQL pattern for consistency.
+// defaultTrinoSchema is the schema assumed for a table reference that omits the
+// schema qualifier. The legacy plugin used "public" for the data-access-control
+// resource format, and the query-span fixtures encode catalog.public.table; we
+// keep that so the resource keys are unchanged across the cutover.
+const defaultTrinoSchema = "public"
+
+// querySpanExtractor analyses a single Trino statement and produces a
+// base.QuerySpan: the set of physical columns it reads (expanded against
+// catalog metadata when available), the predicate columns it filters on, and
+// its statement classification.
+//
+// The extractor delegates table/column lineage to omni's analysis.GetQuerySpan,
+// then applies bytebase-specific post-processing:
+// - default catalog/schema fill-in for bare table references (Trino's
+// catalog.schema.table → base.ColumnResource Database/Schema/Table),
+// - EXPLAIN unwrap so an EXPLAIN <query> still reports the tables it reads,
+// - expansion of each accessed table to its columns via the metadata getter,
+// - mapping omni's PredicateColumns onto the expanded source columns.
type querySpanExtractor struct {
ctx context.Context
@@ -24,25 +40,11 @@ type querySpanExtractor struct {
gCtx base.GetQuerySpanContext
- // CTEs for handling WITH clauses - following TSQL pattern
- ctes []*base.PseudoTable
-
- // Table sources for resolving columns - following TSQL pattern
- tableSourcesFrom []base.TableSource
-
- // Parse results - following TSQL pattern
- sourceColumns base.SourceColumnSet
- predicateColumns base.SourceColumnSet
-
- // Outer table sources for correlated subqueries
- outerTableSources []base.TableSource
-
- // Metadata cache
+ // metaCache memoises database metadata lookups within a single span.
metaCache map[string]*model.DatabaseMetadata
}
// newQuerySpanExtractor creates a new Trino query span extractor.
-// This follows the TSQL constructor pattern.
func newQuerySpanExtractor(defaultDatabase, defaultSchema string, gCtx base.GetQuerySpanContext, ignoreCaseSensitive bool) *querySpanExtractor {
return &querySpanExtractor{
defaultDatabase: defaultDatabase,
@@ -50,252 +52,502 @@ func newQuerySpanExtractor(defaultDatabase, defaultSchema string, gCtx base.GetQ
gCtx: gCtx,
ignoreCaseSensitive: ignoreCaseSensitive,
metaCache: make(map[string]*model.DatabaseMetadata),
- sourceColumns: make(base.SourceColumnSet),
- predicateColumns: make(base.SourceColumnSet),
}
}
-// getQuerySpan extracts the query span for a Trino statement.
-// This method follows the TSQL pattern for consistency.
+// getQuerySpan extracts the query span for a single Trino statement.
func (q *querySpanExtractor) getQuerySpan(ctx context.Context, statement string) (*base.QuerySpan, error) {
q.ctx = ctx
- q.sourceColumns = make(base.SourceColumnSet)
- q.predicateColumns = make(base.SourceColumnSet)
- q.tableSourcesFrom = []base.TableSource{}
- q.ctes = []*base.PseudoTable{}
- // Parse the statement
- parseResults, err := ParseTrino(statement)
+ // Split into top-level statements; query-span analysis expects exactly one
+ // non-empty statement (matching the legacy behaviour).
+ stmts, err := SplitSQL(statement)
if err != nil {
- return nil, errors.Wrapf(err, "failed to parse Trino statement")
+ return nil, err
+ }
+ nonEmpty := 0
+ var single string
+ for _, s := range stmts {
+ if s.Empty || strings.TrimSpace(s.Text) == "" {
+ continue
+ }
+ nonEmpty++
+ single = s.Text
+ }
+ if nonEmpty == 0 {
+ return &base.QuerySpan{
+ SourceColumns: base.SourceColumnSet{},
+ Results: []base.QuerySpanResult{},
+ }, nil
+ }
+ if nonEmpty > 1 {
+ return nil, errors.Errorf("expected exactly 1 statement, got %d", nonEmpty)
}
- // Query span extraction expects exactly one statement
- if len(parseResults) != 1 {
- return nil, errors.Errorf("expected exactly 1 statement, got %d", len(parseResults))
+ // Resolve the statement type up front so non-SELECT statements short-circuit
+ // the way the legacy plugin did. EXPLAIN ANALYZE classifies as base.Select.
+ file, errs := parser.Parse(single)
+ if len(errs) > 0 || file == nil || len(file.Stmts) == 0 {
+ return nil, errors.Errorf("failed to parse Trino statement: %s", single)
+ }
+ node := file.Stmts[0]
+ queryType, _ := getQueryType(node)
+ // Promote a user SELECT/EXPLAIN that references a system schema, matching the
+ // legacy containsSystemSchema heuristic.
+ switch queryType {
+ case base.Select, base.Explain:
+ if containsSystemSchema(single) {
+ queryType = base.SelectInfoSchema
+ }
+ default:
}
- result := parseResults[0]
+ // For statements that don't read columns into a result set (DML, DDL), the
+ // legacy plugin returned a basic span: the accessed tables as source
+ // columns, no per-column results. CREATE VIEW / EXPLAIN over a query still
+ // report the underlying tables.
+ readsResults := queryType == base.Select ||
+ queryType == base.Explain ||
+ queryType == base.SelectInfoSchema
+
+ // Unwrap EXPLAIN so the inner query's tables are visible: omni's span walker
+ // only descends into SELECT/set-op at the top level.
+ spanInput := single
+ if explain, ok := node.(*parser.ExplainStmt); ok && explain.Statement != nil {
+ if loc, ok := nodeSpan(explain.Statement); ok && loc.Start >= 0 && loc.End <= len(single) && loc.Start < loc.End {
+ spanInput = single[loc.Start:loc.End]
+ }
+ }
- if result.Tree == nil {
- return nil, errors.New("failed to parse Trino statement, no parse tree found")
+ omniSpan, err := analysis.GetQuerySpan(spanInput)
+ if err != nil {
+ return nil, err
}
- // Get accessed tables for basic query type determination
- accessTables := q.extractAccessedTables(result.Tree)
- queryType, isExplainAnalyze := getQueryType(result.Tree)
+ tables := q.toTableResources(omniSpan)
- // For non-SELECT queries, return basic span following TSQL pattern
- if queryType != base.Select && queryType != base.Explain && queryType != base.SelectInfoSchema {
+ if !readsResults {
+ // DML/DDL/unknown: report accessed tables as source columns (table-level,
+ // no column expansion), no results — mirroring the legacy basic span.
return &base.QuerySpan{
Type: queryType,
- SourceColumns: accessTables,
+ SourceColumns: tables,
Results: []base.QuerySpanResult{},
}, nil
}
- // Special handling for EXPLAIN following TSQL pattern
- if isExplainAnalyze || queryType == base.Explain {
- return &base.QuerySpan{
- Type: queryType,
- SourceColumns: accessTables,
- Results: []base.QuerySpanResult{},
- }, nil
+ // Resolve relation aliases (e.g. "u" in "FROM users u") back to physical
+ // table names so aliased column references match the expanded source
+ // columns.
+ aliasMap := q.buildAliasMap(omniSpan)
+
+ // Expand each accessed table to its columns via metadata. If metadata is
+ // unavailable (e.g. tests without a getter), fall back to the table-level
+ // resource so callers still see what was read. orderedColumns preserves
+ // FROM-table-then-metadata column order for positional SELECT * masking.
+ //
+ // Trino system pseudo-catalogs (system.*) and information_schema are
+ // detected and skipped per resolved table inside expandTablesToColumns (not
+ // via a query-text substring scan), so a real maskable table is still
+ // expanded even when the same statement mentions "system." in a literal.
+ orderedColumns, fullSourceColumns, notFound := q.expandTablesToColumns(omniSpan)
+
+ results := q.buildResults(omniSpan, fullSourceColumns, orderedColumns, aliasMap)
+ predicateColumns := q.mapPredicateColumns(omniSpan, fullSourceColumns, aliasMap)
+
+ span := &base.QuerySpan{
+ Type: queryType,
+ SourceColumns: fullSourceColumns,
+ PredicateColumns: predicateColumns,
+ Results: results,
+ }
+ if notFound != nil {
+ span.NotFoundError = notFound
}
+ return span, nil
+}
- // Use ANTLR listener to extract source columns and results
- listener := newTrinoQuerySpanListener(q)
- antlr.ParseTreeWalkerDefault.Walk(listener, result.Tree)
+// toTableResources converts omni AccessTables into table-level
+// base.ColumnResource keys (Column empty), applying default catalog/schema.
+func (q *querySpanExtractor) toTableResources(span *analysis.QuerySpan) base.SourceColumnSet {
+ out := base.SourceColumnSet{}
+ if span == nil {
+ return out
+ }
+ for _, t := range span.AccessTables {
+ db, schema := q.resolveCatalogSchema(t.Catalog, t.Schema)
+ out[base.ColumnResource{
+ Database: db,
+ Schema: schema,
+ Table: t.Table,
+ }] = true
+ }
+ return out
+}
- if listener.err != nil {
- var resourceNotFound *base.ResourceNotFoundError
- if errors.As(listener.err, &resourceNotFound) {
- return &base.QuerySpan{
- Type: queryType,
- SourceColumns: accessTables,
- Results: []base.QuerySpanResult{},
- NotFoundError: resourceNotFound,
- }, nil
+// resolveCatalogSchema fills in the default database (catalog) and schema for a
+// table reference that omitted them, mirroring Trino's session-qualified
+// resolution. The schema falls back to defaultTrinoSchema ("public") when no
+// default schema is configured, matching the legacy resource-key format.
+func (q *querySpanExtractor) resolveCatalogSchema(catalog, schema string) (string, string) {
+ db := catalog
+ if db == "" {
+ db = q.defaultDatabase
+ }
+ if schema == "" {
+ schema = q.defaultSchema
+ if schema == "" {
+ schema = defaultTrinoSchema
}
- return nil, listener.err
}
-
- // Expand table references to columns following TSQL pattern
- fullSourceColumns := q.expandTableReferencesToColumns(accessTables)
-
- // Process predicate columns following TSQL pattern
- fullPredicateColumns := q.expandPredicateColumns(fullSourceColumns)
-
- // Expand SELECT * results following TSQL pattern
- expandedResults := q.expandSelectAsteriskResults(listener.results, fullSourceColumns)
-
- return &base.QuerySpan{
- Type: queryType,
- SourceColumns: fullSourceColumns,
- PredicateColumns: fullPredicateColumns,
- Results: expandedResults,
- }, nil
+ return db, schema
}
-// expandPredicateColumns expands predicate columns to their fully qualified forms.
-// This follows the TSQL pattern for consistent predicate handling.
-func (q *querySpanExtractor) expandPredicateColumns(fullSourceColumns base.SourceColumnSet) base.SourceColumnSet {
- fullPredicateColumns := make(base.SourceColumnSet)
-
- for col := range q.predicateColumns {
- if col.Column != "" {
- // Find all fully qualified versions in source columns
- for sourceCol := range fullSourceColumns {
- if sourceCol.Column == col.Column {
- // Match table if specified, otherwise accept any table
- if col.Table == "" || col.Table == sourceCol.Table {
- fullPredicateColumns[sourceCol] = true
- }
- }
+// expandTablesToColumns expands each accessed table into one resource per column
+// from the table's metadata. It returns the columns both as an ordered slice
+// (AccessTables/FROM order, then metadata column order) and as a set. The
+// ordered slice drives positional SELECT * masking, where the order must match
+// the executed result's column order; the set is used for membership tests.
+//
+// A table whose metadata can't be resolved is kept as a single table-level
+// resource so the read is still recorded. The first ResourceNotFoundError
+// encountered is returned (non-fatally) so the caller can attach it to the span,
+// matching the legacy listener behaviour.
+func (q *querySpanExtractor) expandTablesToColumns(span *analysis.QuerySpan) ([]base.ColumnResource, base.SourceColumnSet, *base.ResourceNotFoundError) {
+ ordered := make([]base.ColumnResource, 0)
+ set := make(base.SourceColumnSet)
+ var firstNotFound *base.ResourceNotFoundError
+ // add records a column both in the membership set (deduped, used for lineage
+ // matching) and in the ordered slice (NOT deduped, so a self-join such as
+ // "FROM users u1 JOIN users u2" contributes each instance's columns and the
+ // positional SELECT * masker stays aligned with Trino's 2N output columns).
+ add := func(res base.ColumnResource) {
+ set[res] = true
+ ordered = append(ordered, res)
+ }
+ if span == nil {
+ return ordered, set, nil
+ }
+ for _, t := range span.AccessTables {
+ db, schema := q.resolveCatalogSchema(t.Catalog, t.Schema)
+ if isSystemSchemaTable(db, schema) {
+ // Trino's "system" catalog (system.runtime/metadata/jdbc.*) and the
+ // per-catalog information_schema are pseudo-objects, not
+ // Bytebase-tracked databases. Resolving them via the metadata getter
+ // would yield a ResourceNotFoundError that sql_service turns into a
+ // hard "failed to mask data" rejection of an otherwise-successful
+ // result. System metadata is never masked, so keep the table-level
+ // resource and skip the lookup. Detection keys on the resolved
+ // catalog/schema (not a query-text substring), so a literal
+ // containing "system." cannot suppress masking of a real table.
+ //
+ // Best-effort boundary: a system table contributes one placeholder
+ // here but expands to several columns in a real `SELECT *`. For the
+ // rare `SELECT * FROM <system> JOIN <real>` shape the positional
+ // masker can therefore misalign the trailing real columns. Fully
+ // fixing it needs system-table column metadata (not available); such
+ // queries are already classified SelectInfoSchema. Tracked as a
+ // follow-up rather than blocking the cutover.
+ add(base.ColumnResource{Database: db, Schema: schema, Table: t.Table})
+ continue
+ }
+ columns, err := q.tableColumns(db, schema, t.Table)
+ if err != nil {
+ var notFound *base.ResourceNotFoundError
+ if errors.As(err, &notFound) && firstNotFound == nil {
+ firstNotFound = notFound
}
+ // Keep the table-level resource (NotFound or any other error).
+ add(base.ColumnResource{Database: db, Schema: schema, Table: t.Table})
+ continue
+ }
+ if len(columns) == 0 {
+ add(base.ColumnResource{Database: db, Schema: schema, Table: t.Table})
+ continue
+ }
+ for _, col := range columns {
+ add(base.ColumnResource{Database: db, Schema: schema, Table: t.Table, Column: col})
}
}
+ return ordered, set, firstNotFound
+}
- return fullPredicateColumns
+// isSystemSchemaTable reports whether a resolved table reference targets a Trino
+// system pseudo-object: the built-in "system" catalog (system.runtime.*,
+// system.metadata.*, system.jdbc.*) or the per-catalog "information_schema".
+// These are not Bytebase-tracked databases and their metadata is never masked.
+func isSystemSchemaTable(catalog, schema string) bool {
+ return strings.EqualFold(catalog, "system") || strings.EqualFold(schema, "information_schema")
}
-// expandSelectAsteriskResults expands SELECT * results into individual column results
-// This is similar to how TSQL handles SELECT * queries
-func (q *querySpanExtractor) expandSelectAsteriskResults(results []base.QuerySpanResult, fullSourceColumns base.SourceColumnSet) []base.QuerySpanResult {
- var expandedResults []base.QuerySpanResult
-
- for _, result := range results {
- if result.SelectAsterisk && result.Name == "*" {
- // Use table sources to get columns in the correct order (same as TSQL approach)
- for _, tableSource := range q.tableSourcesFrom {
- tableColumns := tableSource.GetQuerySpanResult()
-
- for _, columnResult := range tableColumns {
- // Only include columns that are in our source columns set
- columnIncluded := false
- for sourceCol := range columnResult.SourceColumns {
- if _, exists := fullSourceColumns[sourceCol]; exists {
- columnIncluded = true
- break
- }
- }
-
- if columnIncluded {
- expandedResults = append(expandedResults, columnResult)
- }
- }
- }
- } else {
- // Keep non-asterisk results as-is
- expandedResults = append(expandedResults, result)
+// buildResults maps omni's per-output-column results onto base.QuerySpanResult.
+// Each result's SourceColumns is the subset of the expanded source columns that
+// share the result's column name (best-effort; omni does not resolve a select
+// item back to its owning relation, so aliasMap translates a relation alias on
+// the reference back to its physical table first).
+//
+// For SELECT *, the expansion uses orderedColumns (FROM-table then metadata
+// column order) rather than ranging the source-column map: the masker applies
+// per-result maskers positionally against the executed result's column order, so
+// a nondeterministic order here could mask the wrong column and leak data.
+func (q *querySpanExtractor) buildResults(span *analysis.QuerySpan, fullSourceColumns base.SourceColumnSet, orderedColumns []base.ColumnResource, aliasMap map[string][]string) []base.QuerySpanResult {
+ if span == nil {
+ return []base.QuerySpanResult{}
+ }
+ results := make([]base.QuerySpanResult, 0, len(span.Results))
+ for _, r := range span.Results {
+ // Unqualified star "*": expand to every source column, in result order.
+ //
+ // Best-effort boundary: omni records a star as a single "*" result over
+ // the flat AccessTables and does not expose the resolved output relation,
+ // so a star over a CTE or derived table that projects a subset/reordering
+ // (e.g. WITH c AS (SELECT email FROM users) SELECT * FROM c) is
+ // indistinguishable here from SELECT * over the base table and expands to
+ // the base table's columns. This matches the prior ANTLR plugin (which
+ // produced no columns for such stars) and is tracked as a follow-up; a
+ // correct fix needs omni's analysis to expose the star's output columns.
+ if r.Name == "*" {
+ results = appendStarColumns(results, orderedColumns, nil)
+ continue
+ }
+ // Qualified star "<rel>.*": omni names the result "<rel>.*" with a single
+ // source ref that is the relation itself. Without this branch it would
+ // fall through to the column-name match below, find no column literally
+ // named "<rel>.*", and leave SourceColumns empty — the masker would then
+ // treat those (possibly sensitive) columns as constants and return them
+ // unmasked. Resolve the ref to the specific relation(s) it names (by
+ // database/schema/table, so same-named tables in different schemas are
+ // distinguished) and expand only those columns, preserving order. The
+ // detection inspects the source-ref shape, not just the display name, so a
+ // column merely aliased to a name ending in ".*" is not mistaken for a
+ // star.
+ if isQualifiedStar(r) {
+ targets := q.qualifiedStarTargets(r.SourceColumns[0], span)
+ results = appendStarColumns(results, orderedColumns, targets)
+ continue
}
+ sourceColumns := base.SourceColumnSet{}
+ for _, ref := range r.SourceColumns {
+ addMatchingColumns(sourceColumns, fullSourceColumns, ref.Column, ref.Table, aliasMap)
+ }
+ results = append(results, base.QuerySpanResult{
+ Name: r.Name,
+ SourceColumns: sourceColumns,
+ IsPlainField: len(r.SourceColumns) == 1,
+ })
}
-
- return expandedResults
+ return results
}
-// expandTableReferencesToColumns expands table references to individual columns
-func (q *querySpanExtractor) expandTableReferencesToColumns(accessTables base.SourceColumnSet) base.SourceColumnSet {
- fullSourceColumns := make(base.SourceColumnSet)
-
- // First, copy all explicitly collected source columns
- for col := range q.sourceColumns {
- fullSourceColumns[col] = true
- }
-
- // Expand table references to columns
- for resource := range accessTables {
- // Check if this is a table reference (has no column set)
- if resource.Column == "" {
- // Get the database metadata to find all columns for this table
- db, schema, table := resource.Database, resource.Schema, resource.Table
- tableMeta, err := q.findTableSchema(db, schema, table)
- if err == nil && tableMeta != nil {
- // Add each column from the table as a source column with full path
- for _, col := range tableMeta.GetProto().GetColumns() {
- colResource := base.ColumnResource{
- Database: db,
- Schema: schema,
- Table: table,
- Column: col.Name,
- }
- fullSourceColumns[colResource] = true
- }
+// appendStarColumns appends one plain-field result per column in orderedColumns,
+// in order (the positional masker maps each result to the executed result column
+// at the same index). When targets is non-nil — a qualified star "<rel>.*" — only
+// columns whose (database, schema, table) key is in targets are appended; a nil
+// targets (unqualified "*") appends every column.
+func appendStarColumns(results []base.QuerySpanResult, orderedColumns []base.ColumnResource, targets map[base.ColumnResource]bool) []base.QuerySpanResult {
+ for _, col := range orderedColumns {
+ if targets != nil {
+ if !targets[base.ColumnResource{Database: col.Database, Schema: col.Schema, Table: col.Table}] {
+ continue
}
- } else {
- // This is already a column reference, keep it
- fullSourceColumns[resource] = true
}
+ results = append(results, base.QuerySpanResult{
+ Name: col.Column,
+ SourceColumns: base.SourceColumnSet{col: true},
+ IsPlainField: true,
+ })
}
-
- return fullSourceColumns
+ return results
}
-// extractAccessedTables analyzes the parse tree to find all table resources accessed
-func (q *querySpanExtractor) extractAccessedTables(tree antlr.Tree) base.SourceColumnSet {
- resources := make(base.SourceColumnSet)
- tableListener := &tableExtractorListener{
- extractor: q,
- resources: resources,
+// qualifiedStarResult reports whether r is a genuine qualified star "<rel>.*"
+// (returning the relation qualifier), as opposed to an ordinary column that
+// happens to be aliased to a name ending in ".*". omni represents a qualified
+// star as a result whose Name is "<rel>.*" and whose single source ref is the
+// relation name itself (Column == <rel>, no catalog/schema/table qualifier); a
+// real column aliased to e.g. "u.*" instead carries the underlying column name
+// in its source ref, so it is handled as a normal column and not star-expanded
+// (which would misalign the positional masker and could leak).
+func isQualifiedStar(r analysis.ColumnInfo) bool {
+ qualifier, ok := qualifiedStarQualifier(r.Name)
+ if !ok {
+ return false
}
-
- antlr.ParseTreeWalkerDefault.Walk(tableListener, tree)
- return resources
+ // omni represents a qualified star "<...>.<rel>.*" as a single source ref
+ // whose Column is the relation name <rel> (any schema/catalog path written
+ // before it lands in the ref's Table/Schema fields). A real column aliased to
+ // a name ending in ".*" instead carries the underlying column name in Column,
+ // which will not equal the qualifier, so it is handled as a normal column and
+ // not star-expanded (avoiding a positional-masker misalignment/leak).
+ //
+ // Matching only on Column == qualifier (not on empty table/schema) is
+ // deliberate: it keeps genuine table/schema/catalog-qualified stars like
+ // public.users.* working. The sole residual ambiguity is the contrived case
+ // of a column literally named like the qualifier and aliased to
+ // "<qualifier>.*", which omni renders identically to a real star.
+ if len(r.SourceColumns) != 1 {
+ return false
+ }
+ return strings.EqualFold(r.SourceColumns[0].Column, qualifier)
}
-// getDatabaseMetadata fetches metadata for the given database.
-func (q *querySpanExtractor) getDatabaseMetadata(database string) (*model.DatabaseMetadata, error) {
- if database == "" {
- database = q.defaultDatabase
+// qualifiedStarTargets resolves the relation(s) named by a qualified-star source
+// ref into a set of (database, schema, table) keys, so the star expands to
+// exactly that relation's columns — distinguishing same-named tables in
+// different schemas or catalogs. omni encodes the written qualifier in the ref
+// with the parts shifted right: Column is the relation/alias, Table is the schema
+// part, and Schema is the catalog part.
+func (q *querySpanExtractor) qualifiedStarTargets(ref analysis.ColumnRef, span *analysis.QuerySpan) map[base.ColumnResource]bool {
+ targets := make(map[base.ColumnResource]bool)
+ // Alias-qualified star (u.*): collect every base table that carries the
+ // alias. A reused/shadowed alias yields several, which over-includes (the
+ // extra columns are ignored positionally) rather than risking a leak.
+ aliasMatched := false
+ for _, t := range span.AccessTables {
+ if t.Alias != "" && strings.EqualFold(t.Alias, ref.Column) {
+ db, schema := q.resolveCatalogSchema(t.Catalog, t.Schema)
+ targets[base.ColumnResource{Database: db, Schema: schema, Table: t.Table}] = true
+ aliasMatched = true
+ }
}
+ if aliasMatched {
+ return targets
+ }
+ // Table-name-qualified star (users.* / public.users.* / cat.sch.users.*):
+ // ref.Column is the table, ref.Table the schema part, ref.Schema the catalog.
+ // Only target a base table actually present in AccessTables, so a qualifier
+ // naming a CTE/derived relation (which omni omits from AccessTables) does not
+ // accidentally target an unrelated physical table of the same name. A
+ // qualified star over a CTE/derived relation thus yields no columns here —
+ // the same best-effort boundary as an unqualified star over such a relation
+ // (omni does not expose the resolved output projection; tracked as a
+ // follow-up).
+ wantDB, wantSchema := q.resolveCatalogSchema(ref.Schema, ref.Table)
+ for _, t := range span.AccessTables {
+ db, schema := q.resolveCatalogSchema(t.Catalog, t.Schema)
+ if strings.EqualFold(db, wantDB) && strings.EqualFold(schema, wantSchema) && strings.EqualFold(t.Table, ref.Column) {
+ targets[base.ColumnResource{Database: db, Schema: schema, Table: t.Table}] = true
+ }
+ }
+ return targets
+}
- // Return cached metadata if available
- if meta, ok := q.metaCache[database]; ok {
- return meta, nil
+// qualifiedStarQualifier returns the relation qualifier of a qualified-star
+// result name like "u.*" or "catalog.schema.t.*" (the rightmost component before
+// the trailing ".*"), and whether name is a qualified star. Column matching keys
+// on the table/alias, so only the last component is returned.
+func qualifiedStarQualifier(name string) (string, bool) {
+ if !strings.HasSuffix(name, ".*") {
+ return "", false
+ }
+ qualifier := strings.TrimSuffix(name, ".*")
+ if i := strings.LastIndex(qualifier, "."); i >= 0 {
+ qualifier = qualifier[i+1:]
}
+ if qualifier == "" {
+ return "", false
+ }
+ return qualifier, true
+}
- // Skip if metadata function not provided (for testing)
- if q.gCtx.GetDatabaseMetadataFunc == nil {
- return nil, &base.ResourceNotFoundError{Database: &database}
+// mapPredicateColumns maps omni's predicate column refs onto the expanded source
+// columns (matching on column name, and table when the ref carries one; a
+// relation alias on the ref is resolved via aliasMap).
+func (*querySpanExtractor) mapPredicateColumns(span *analysis.QuerySpan, fullSourceColumns base.SourceColumnSet, aliasMap map[string][]string) base.SourceColumnSet {
+ out := make(base.SourceColumnSet)
+ if span == nil {
+ return out
}
+ for _, ref := range span.PredicateColumns {
+ addMatchingColumns(out, fullSourceColumns, ref.Column, ref.Table, aliasMap)
+ }
+ return out
+}
- // Fetch metadata using the provided function
- _, meta, err := q.gCtx.GetDatabaseMetadataFunc(q.ctx, q.gCtx.InstanceID, database)
- if err != nil {
- var resourceNotFound *base.ResourceNotFoundError
- if errors.As(err, &resourceNotFound) {
- return nil, err
+// addMatchingColumns adds every column in fullSourceColumns whose name matches
+// refColumn (case-insensitively, and whose table matches refTable when refTable
+// is non-empty) to dst. refTable may be a relation alias; see tableMatches.
+func addMatchingColumns(dst, fullSourceColumns base.SourceColumnSet, refColumn, refTable string, aliasMap map[string][]string) {
+ for sc := range fullSourceColumns {
+ if !strings.EqualFold(sc.Column, refColumn) {
+ continue
+ }
+ if refTable == "" || tableMatches(sc.Table, refTable, aliasMap) {
+ dst[sc] = true
}
- return nil, errors.Wrapf(err, "failed to get database metadata for database: %s", database)
}
+}
- if meta == nil {
- // Return empty metadata for testing purposes
- emptyMeta := &model.DatabaseMetadata{}
- q.metaCache[database] = emptyMeta
- return emptyMeta, nil
+// tableMatches reports whether the physical table scTable is named by refTable,
+// either directly (its own name) or as a relation alias for it. Alias resolution
+// is additive: the written refTable and every physical table the alias maps to
+// are all accepted. omni's AccessTables is a flat, scope-less list, so an alias
+// reused or shadowed across subqueries yields several candidates; accepting all
+// of them over-includes (conservatively masks more) rather than risk an
+// under-match that would leave a sensitive column unmasked.
+func tableMatches(scTable, refTable string, aliasMap map[string][]string) bool {
+ if strings.EqualFold(scTable, refTable) {
+ return true
}
+ for _, phys := range aliasMap[strings.ToLower(refTable)] {
+ if strings.EqualFold(scTable, phys) {
+ return true
+ }
+ }
+ return false
+}
- // Cache and return the metadata
- q.metaCache[database] = meta
- return meta, nil
+// buildAliasMap maps each relation alias (lower-cased) to the physical table
+// names it stands for, so aliased column references resolve back to base tables
+// during column matching. Because omni's AccessTables is flat (carries no SQL
+// scope), an alias reused in different scopes maps to several tables; all are
+// kept so matching can over-include rather than under-match. An alias equal to
+// its own table name carries no information and is skipped.
+func (*querySpanExtractor) buildAliasMap(span *analysis.QuerySpan) map[string][]string {
+ if span == nil {
+ return nil
+ }
+ out := make(map[string][]string)
+ for _, t := range span.AccessTables {
+ if t.Alias == "" || strings.EqualFold(t.Alias, t.Table) {
+ continue
+ }
+ key := strings.ToLower(t.Alias)
+ if !containsFold(out[key], t.Table) {
+ out[key] = append(out[key], t.Table)
+ }
+ }
+ return out
}
-// addSourceColumn adds a column to the tracked source columns.
-func (q *querySpanExtractor) addSourceColumn(col base.ColumnResource) {
- q.sourceColumns[col] = true
+// containsFold reports whether list contains s under case-insensitive
+// comparison.
+func containsFold(list []string, s string) bool {
+ for _, e := range list {
+ if strings.EqualFold(e, s) {
+ return true
+ }
+ }
+ return false
}
-// findTableSchema locates a table or view and returns its metadata.
-func (q *querySpanExtractor) findTableSchema(db, schema, name string) (*model.TableMetadata, error) {
- // Get database metadata
+// tableColumns returns the column names of the given table or view, honouring
+// the case-sensitivity flag for object lookup.
+func (q *querySpanExtractor) tableColumns(db, schema, table string) ([]string, error) {
metadata, err := q.getDatabaseMetadata(db)
if err != nil {
return nil, err
}
- // Get schema metadata
schemaMeta := metadata.GetSchemaMetadata(schema)
+ if schemaMeta == nil && q.ignoreCaseSensitive {
+ for _, name := range metadata.ListSchemaNames() {
+ if strings.EqualFold(name, schema) {
+ schemaMeta = metadata.GetSchemaMetadata(name)
+ break
+ }
+ }
+ }
if schemaMeta == nil {
return nil, &base.ResourceNotFoundError{
Database: &db,
@@ -303,75 +555,90 @@ func (q *querySpanExtractor) findTableSchema(db, schema, name string) (*model.Ta
}
}
- // Look for table
- var tableMeta *model.TableMetadata
- if q.ignoreCaseSensitive {
- for _, tblName := range schemaMeta.ListTableNames() {
- if strings.EqualFold(tblName, name) {
- tableMeta = schemaMeta.GetTable(tblName)
+ // Table first.
+ tableMeta := schemaMeta.GetTable(table)
+ if tableMeta == nil && q.ignoreCaseSensitive {
+ for _, name := range schemaMeta.ListTableNames() {
+ if strings.EqualFold(name, table) {
+ tableMeta = schemaMeta.GetTable(name)
break
}
}
- } else {
- tableMeta = schemaMeta.GetTable(name)
}
-
if tableMeta != nil {
- return tableMeta, nil
+ var cols []string
+ for _, col := range tableMeta.GetProto().GetColumns() {
+ cols = append(cols, col.Name)
+ }
+ return cols, nil
}
- // Look for view
- var viewMeta *storepb.ViewMetadata
- if q.ignoreCaseSensitive {
- for _, viewName := range schemaMeta.ListViewNames() {
- if strings.EqualFold(viewName, name) {
- viewMeta = schemaMeta.GetView(viewName)
+ // Then view.
+ viewMeta := schemaMeta.GetView(table)
+ if viewMeta == nil && q.ignoreCaseSensitive {
+ for _, name := range schemaMeta.ListViewNames() {
+ if strings.EqualFold(name, table) {
+ viewMeta = schemaMeta.GetView(name)
break
}
}
- } else {
- viewMeta = schemaMeta.GetView(name)
}
-
if viewMeta != nil {
- // For views, return a table metadata with columns from the view
- tableMeta := &model.TableMetadata{}
- // In a more complete implementation, we would add columns from the view here
- return tableMeta, nil
+ var cols []string
+ for _, col := range viewMeta.GetColumns() {
+ cols = append(cols, col.Name)
+ }
+ return cols, nil
}
- // Not found
return nil, &base.ResourceNotFoundError{
Database: &db,
Schema: &schema,
- Table: &name,
+ Table: &table,
}
}
-// tableExtractorListener extracts table resources from the parse tree
-type tableExtractorListener struct {
- parser.BaseTrinoParserListener
-
- extractor *querySpanExtractor
- resources base.SourceColumnSet
+// getDatabaseMetadata fetches (and caches) metadata for the given database.
+func (q *querySpanExtractor) getDatabaseMetadata(database string) (*model.DatabaseMetadata, error) {
+ if database == "" {
+ database = q.defaultDatabase
+ }
+ if meta, ok := q.metaCache[database]; ok {
+ return meta, nil
+ }
+ if q.gCtx.GetDatabaseMetadataFunc == nil {
+ return nil, &base.ResourceNotFoundError{Database: &database}
+ }
+ _, meta, err := q.gCtx.GetDatabaseMetadataFunc(q.ctx, q.gCtx.InstanceID, database)
+ if err != nil {
+ var notFound *base.ResourceNotFoundError
+ if errors.As(err, &notFound) {
+ return nil, err
+ }
+ return nil, errors.Wrapf(err, "failed to get database metadata for database: %s", database)
+ }
+ if meta == nil {
+ empty := &model.DatabaseMetadata{}
+ q.metaCache[database] = empty
+ return empty, nil
+ }
+ q.metaCache[database] = meta
+ return meta, nil
}
-// EnterTableName is called when the parser enters a table name
-func (l *tableExtractorListener) EnterTableName(ctx *parser.TableNameContext) {
- if ctx.QualifiedName() == nil {
- return
+// nodeText returns the source substring covered by node n within statement, or
+// "" when the node's location is unusable.
+// nodeSpan returns the source byte range of an omni statement node. Most omni
+// node types expose Span() ast.Loc; *parser.QueryStmt carries its range on a
+// plain Loc field. (ast.NodeLoc only knows ast-package nodes — File/Identifier/
+// QualifiedName — not the parser-package statement nodes, so it cannot be used
+// to unwrap an EXPLAIN's inner statement.)
+func nodeSpan(n ast.Node) (ast.Loc, bool) {
+ if qs, ok := n.(*parser.QueryStmt); ok {
+ return qs.Loc, true
}
-
- db, schema, table := ExtractDatabaseSchemaName(
- ctx.QualifiedName(),
- l.extractor.defaultDatabase,
- l.extractor.defaultSchema,
- )
-
- // Add a resource for the table
- l.resources[base.ColumnResource{
- Database: db,
- Schema: schema,
- Table: table,
- }] = true
+ if sp, ok := n.(interface{ Span() ast.Loc }); ok {
+ return sp.Span(), true
+ }
+ return ast.Loc{}, false
}
diff --git a/backend/plugin/parser/trino/query_span_feature_test.go b/backend/plugin/parser/trino/query_span_feature_test.go
index 6782fffacf..fc04e8e7a8 100644
--- a/backend/plugin/parser/trino/query_span_feature_test.go
+++ b/backend/plugin/parser/trino/query_span_feature_test.go
@@ -1,217 +1,498 @@
package trino
import (
- "strings"
+ "context"
"testing"
"github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
)
-func TestPredicateExtraction(t *testing.T) {
- // Setup a test query with a predicate
- // Create an extractor with context
+// These tests exercise the omni-backed query-span extractor end-to-end through
+// the public extractor API (newQuerySpanExtractor + getQuerySpan). They replace
+// the legacy feature tests that poked at ANTLR-listener internals
+// (newTrinoQuerySpanListener, extractor.sourceColumns, addPredicateColumn,
+// extractor.tableSourcesFrom, …) which no longer exist after the cutover. The
+// behaviour under test — CTE table resolution, predicate-column extraction,
+// UNNEST/LATERAL handling, and Trino's 3-part naming — is preserved.
+
+// withoutMetadata builds an extractor with no metadata getter, so source tables
+// stay at the table level (Column empty); useful for asserting which physical
+// tables a query reads.
+func tablesFromSpan(t *testing.T, sql, defaultDatabase string) base.SourceColumnSet {
+ t.Helper()
+ extractor := newQuerySpanExtractor(defaultDatabase, "public", base.GetQuerySpanContext{}, false)
+ span, err := extractor.getQuerySpan(context.Background(), sql)
+ require.NoError(t, err)
+ return span.SourceColumns
+}
+
+func TestQuerySpan_PredicateExtraction(t *testing.T) {
+ // A WHERE predicate's columns are reported as predicate columns once they
+ // resolve against the (metadata-expanded) source columns.
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Name: "catalog1",
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "users",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "integer"},
+ {Name: "name", Type: "varchar"},
+ },
+ },
+ },
+ },
+ },
+ }
+ getter, lister := buildMockDatabaseMetadataGetter([]*storepb.DatabaseSchemaMetadata{metadata})
gCtx := base.GetQuerySpanContext{
- InstanceID: "test-instance",
- Engine: storepb.Engine_TRINO,
+ GetDatabaseMetadataFunc: getter,
+ ListDatabaseNamesFunc: lister,
+ Engine: storepb.Engine_TRINO,
}
extractor := newQuerySpanExtractor("catalog1", "public", gCtx, false)
+ span, err := extractor.getQuerySpan(context.Background(), "SELECT id, name FROM users WHERE id > 10 AND name LIKE 'A%'")
+ require.NoError(t, err)
- // Create a listener - not used directly but initializes the extractor relationship
- _ = newTrinoQuerySpanListener(extractor)
-
- // Manually add some source columns
- extractor.sourceColumns[base.ColumnResource{
- Database: "catalog1",
- Schema: "public",
- Table: "users",
- Column: "id",
- }] = true
- extractor.sourceColumns[base.ColumnResource{
- Database: "catalog1",
- Schema: "public",
- Table: "users",
- Column: "name",
- }] = true
-
- // Create test boolean expressions for id > 10 and name LIKE 'A%'
- // This is a simple test that doesn't fully parse the tree, but tests the extraction logic
-
- // Just verify our predicate column extraction method works
- idCol := "id"
- nameCol := "name"
-
- // Create a listener to use for the test
- listener := newTrinoQuerySpanListener(extractor)
-
- // Call the method directly with column names
- listener.addPredicateColumn(idCol)
- listener.addPredicateColumn(nameCol)
-
- // Check that predicates were extracted
- assert.Equal(t, 2, len(extractor.predicateColumns), "Expected 2 predicate columns")
-
- var foundIDCol, foundNameCol bool
- for col := range extractor.predicateColumns {
+ var foundID, foundName bool
+ for col := range span.PredicateColumns {
switch col.Column {
case "id":
- foundIDCol = true
+ foundID = true
case "name":
- foundNameCol = true
+ foundName = true
default:
- // Other columns that we don't track in this test
}
}
+ assert.True(t, foundID, "expected 'id' predicate column")
+ assert.True(t, foundName, "expected 'name' predicate column")
+}
- assert.True(t, foundIDCol, "Expected 'id' predicate column")
- assert.True(t, foundNameCol, "Expected 'name' predicate column")
+func TestQuerySpan_CTEHandling(t *testing.T) {
+ // A CTE name is not reported as a source table; the physical table the CTE
+ // reads from is.
+ sources := tablesFromSpan(t, "WITH temp_cte AS (SELECT id FROM users) SELECT id FROM temp_cte", "catalog1")
+
+ assert.Equal(t, 1, len(sources), "expected exactly one physical source table")
+ _, hasUsers := sources[base.ColumnResource{Database: "catalog1", Schema: "public", Table: "users"}]
+ assert.True(t, hasUsers, "expected physical table catalog1.public.users; got %v", sources)
+ _, hasCTE := sources[base.ColumnResource{Database: "catalog1", Schema: "public", Table: "temp_cte"}]
+ assert.False(t, hasCTE, "CTE name temp_cte must not appear as a source table")
}
-func TestCTEHandling(t *testing.T) {
- // Test handling of Common Table Expressions (CTEs)
+func TestQuerySpan_UnnestAndLateral(t *testing.T) {
+ // UNNEST over a column of a base table still reports the base table.
+ unnest := tablesFromSpan(t, "SELECT id, t.name FROM users CROSS JOIN UNNEST(names) AS t(name)", "catalog1")
+ _, hasUsers := unnest[base.ColumnResource{Database: "catalog1", Schema: "public", Table: "users"}]
+ assert.True(t, hasUsers, "UNNEST query should still report base table users; got %v", unnest)
+
+ // A LATERAL derived table that selects only computed expressions reports the
+ // outer base table.
+ lateral := tablesFromSpan(t, "SELECT u.id, t.x FROM users u, LATERAL (SELECT id + 1 AS x) t", "catalog1")
+ _, hasUsersLateral := lateral[base.ColumnResource{Database: "catalog1", Schema: "public", Table: "users"}]
+ assert.True(t, hasUsersLateral, "LATERAL query should report base table users; got %v", lateral)
+}
- // Create a test extractor
+func TestQuerySpan_SelectStarColumnOrder(t *testing.T) {
+ // SELECT * must report result columns in the table's metadata column order,
+ // not Go map order. The masker applies per-result maskers positionally
+ // against the executed result's column order (query_result_masker.go), so a
+ // nondeterministic order here could apply a column's masker to a different
+ // column and leak sensitive data.
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Name: "catalog1",
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "accounts",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "integer"},
+ {Name: "secret", Type: "varchar"},
+ {Name: "name", Type: "varchar"},
+ {Name: "balance", Type: "double"},
+ },
+ },
+ },
+ },
+ },
+ }
+ getter, lister := buildMockDatabaseMetadataGetter([]*storepb.DatabaseSchemaMetadata{metadata})
gCtx := base.GetQuerySpanContext{
- InstanceID: "test-instance",
- Engine: storepb.Engine_TRINO,
+ GetDatabaseMetadataFunc: getter,
+ ListDatabaseNamesFunc: lister,
+ Engine: storepb.Engine_TRINO,
}
- extractor := newQuerySpanExtractor("catalog1", "public", gCtx, false)
-
- // Create a pseudo table for a CTE
- cteName := "temp_cte"
- pseudoTable := base.NewPseudoTable(cteName, nil)
-
- // Add it to the extractor
- extractor.ctes = append(extractor.ctes, pseudoTable)
-
- // Verify we can look up this CTE by name
- var found bool
- for _, cte := range extractor.ctes {
- if cte.Name == cteName {
- found = true
- break
+ want := []string{"id", "secret", "name", "balance"}
+
+ // Repeat to catch nondeterministic map iteration: a single pass can pass by
+ // luck, so assert a stable order across many runs.
+ for i := 0; i < 50; i++ {
+ extractor := newQuerySpanExtractor("catalog1", "public", gCtx, false)
+ span, err := extractor.getQuerySpan(context.Background(), "SELECT * FROM accounts")
+ require.NoError(t, err)
+ var got []string
+ for _, r := range span.Results {
+ got = append(got, r.Name)
}
+ assert.Equal(t, want, got, "SELECT * results must be in metadata column order (iteration %d)", i)
}
+}
- assert.True(t, found, "CTE was not found in extractor")
- assert.Equal(t, 1, len(extractor.ctes), "Expected 1 CTE")
-
- // Test that our CTE lookup in EnterTableName would work
- // Create a listener - not used directly but initializes the extractor relationship
- _ = newTrinoQuerySpanListener(extractor)
-
- // Before recording table sources, check it's empty
- assert.Equal(t, 0, len(extractor.tableSourcesFrom), "Expected no table sources initially")
-
- // Simulate a table lookup that matches our CTE
- for _, cte := range extractor.ctes {
- if strings.EqualFold(cte.Name, cteName) {
- // This is a CTE reference
- extractor.tableSourcesFrom = append(extractor.tableSourcesFrom, cte)
- break
- }
+func TestQuerySpan_AliasedColumnSourceColumns(t *testing.T) {
+ // An aliased, alias-qualified select item (u.email over "users u") must
+ // resolve back to the physical column users.email. Otherwise the per-result
+ // SourceColumns are empty, the masker treats the column as a constant
+ // expression, and a sensitive aliased column is returned unmasked.
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Name: "catalog1",
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "users",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "integer"},
+ {Name: "email", Type: "varchar"},
+ },
+ },
+ },
+ },
+ },
}
-
- // Verify the CTE was added as a table source
- assert.Equal(t, 1, len(extractor.tableSourcesFrom), "CTE should be added as a table source")
- assert.Equal(t, cteName, extractor.tableSourcesFrom[0].GetTableName(), "CTE name mismatch")
+ getter, lister := buildMockDatabaseMetadataGetter([]*storepb.DatabaseSchemaMetadata{metadata})
+ gCtx := base.GetQuerySpanContext{
+ GetDatabaseMetadataFunc: getter,
+ ListDatabaseNamesFunc: lister,
+ Engine: storepb.Engine_TRINO,
+ }
+ extractor := newQuerySpanExtractor("catalog1", "public", gCtx, false)
+ span, err := extractor.getQuerySpan(context.Background(), "SELECT u.email FROM users u")
+ require.NoError(t, err)
+
+ require.Len(t, span.Results, 1)
+ res := span.Results[0]
+ assert.Equal(t, "email", res.Name)
+ _, ok := res.SourceColumns[base.ColumnResource{Database: "catalog1", Schema: "public", Table: "users", Column: "email"}]
+ assert.True(t, ok, "aliased column u.email must resolve to physical users.email; got %v", res.SourceColumns)
}
-func TestUnnestAndLateralSupport(t *testing.T) {
- // Test the core functionality of UNNEST and LATERAL query handling
-
- // Create a test extractor
+func TestQuerySpan_SystemSchemaNoNotFoundError(t *testing.T) {
+ // A system / information_schema query must NOT surface a
+ // ResourceNotFoundError. sql_service turns a span's NotFoundError into a hard
+ // "failed to mask data" rejection of an otherwise-successful result, so
+ // resolving Trino's pseudo-catalogs (which are not Bytebase-tracked
+ // databases) as metadata would fail valid SQL-info queries after execution.
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Name: "mydb",
+ Schemas: []*storepb.SchemaMetadata{{Name: "public"}},
+ }
+ getter, lister := buildMockDatabaseMetadataGetter([]*storepb.DatabaseSchemaMetadata{metadata})
gCtx := base.GetQuerySpanContext{
- InstanceID: "test-instance",
- Engine: storepb.Engine_TRINO,
+ GetDatabaseMetadataFunc: getter,
+ ListDatabaseNamesFunc: lister,
+ Engine: storepb.Engine_TRINO,
}
- extractor := newQuerySpanExtractor("catalog1", "public", gCtx, false)
+ extractor := newQuerySpanExtractor("mydb", "public", gCtx, false)
+
+ for _, stmt := range []string{
+ "SELECT * FROM system.runtime.nodes",
+ "SELECT table_name FROM information_schema.tables",
+ } {
+ span, err := extractor.getQuerySpan(context.Background(), stmt)
+ require.NoError(t, err, "stmt: %s", stmt)
+ assert.Equal(t, base.SelectInfoSchema, span.Type, "stmt %q should classify as SelectInfoSchema", stmt)
+ assert.Nil(t, span.NotFoundError, "system query %q must not set NotFoundError", stmt)
+ }
+}
- // Add some source columns that might come from an UNNEST
- extractor.sourceColumns[base.ColumnResource{
- Database: "catalog1",
- Schema: "public",
- Table: "users",
- Column: "arrays", // Array column being unnested
- }] = true
-
- // Create a listener - not used directly but initializes the extractor relationship
- _ = newTrinoQuerySpanListener(extractor)
-
- // Test creating a derived table for UNNEST results
- results := []base.QuerySpanResult{
- {
- Name: "item",
- IsPlainField: true,
- SourceColumns: make(base.SourceColumnSet),
+func TestQuerySpan_SystemSubstringDoesNotBypassMasking(t *testing.T) {
+ // A real, maskable table must still be column-expanded even when the
+ // statement text contains "system." in a string literal (which trips the
+ // coarse containsSystemSchema classifier). System detection is per resolved
+ // table, so the literal must not suppress lineage for users.email.
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Name: "catalog1",
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "users",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "integer"},
+ {Name: "email", Type: "varchar"},
+ },
+ },
+ },
+ },
},
}
+ getter, lister := buildMockDatabaseMetadataGetter([]*storepb.DatabaseSchemaMetadata{metadata})
+ gCtx := base.GetQuerySpanContext{
+ GetDatabaseMetadataFunc: getter,
+ ListDatabaseNamesFunc: lister,
+ Engine: storepb.Engine_TRINO,
+ }
+ extractor := newQuerySpanExtractor("catalog1", "public", gCtx, false)
+ span, err := extractor.getQuerySpan(context.Background(), "SELECT email FROM users WHERE 'system.' = 'system.'")
+ require.NoError(t, err)
- // Check initial state
- assert.Equal(t, 0, len(extractor.tableSourcesFrom), "Expected no table sources initially")
-
- // Create a pseudo table for the UNNEST operation
- unnestTable := base.NewPseudoTable("unnest", results)
- extractor.tableSourcesFrom = append(extractor.tableSourcesFrom, unnestTable)
-
- // Verify the UNNEST was added as a table source
- assert.Equal(t, 1, len(extractor.tableSourcesFrom), "UNNEST should be added as a table source")
- assert.Equal(t, "unnest", extractor.tableSourcesFrom[0].GetTableName(), "UNNEST name mismatch")
+ require.Len(t, span.Results, 1)
+ _, ok := span.Results[0].SourceColumns[base.ColumnResource{Database: "catalog1", Schema: "public", Table: "users", Column: "email"}]
+ assert.True(t, ok, "users.email must stay masked despite the 'system.' literal; got %v", span.Results[0].SourceColumns)
+}
- // Test the LATERAL subquery handling (similar approach)
- // Create a derived table for LATERAL results
- lateralResults := []base.QuerySpanResult{
- {
- Name: "x",
- IsPlainField: true,
- SourceColumns: make(base.SourceColumnSet),
+func TestQuerySpan_SelfJoinStarColumnCount(t *testing.T) {
+ // SELECT * over a self-join returns one set of columns per table instance
+ // (2N for a 2-way self-join). The positional masker needs a span result per
+ // output column, so the expansion must NOT collapse the duplicate physical
+ // columns or the trailing instance's columns would get no masker.
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Name: "catalog1",
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "users",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "integer"},
+ {Name: "email", Type: "varchar"},
+ },
+ },
+ },
+ },
},
}
+ getter, lister := buildMockDatabaseMetadataGetter([]*storepb.DatabaseSchemaMetadata{metadata})
+ gCtx := base.GetQuerySpanContext{
+ GetDatabaseMetadataFunc: getter,
+ ListDatabaseNamesFunc: lister,
+ Engine: storepb.Engine_TRINO,
+ }
+ extractor := newQuerySpanExtractor("catalog1", "public", gCtx, false)
+ span, err := extractor.getQuerySpan(context.Background(), "SELECT * FROM users u1 JOIN users u2 ON u1.id = u2.id")
+ require.NoError(t, err)
- // Create a pseudo table for the LATERAL subquery
- lateralTable := base.NewPseudoTable("lateral", lateralResults)
-
- // Save current state before adding
- prevCount := len(extractor.tableSourcesFrom)
-
- // Add the lateral table
- extractor.tableSourcesFrom = append(extractor.tableSourcesFrom, lateralTable)
-
- // Verify it was added
- assert.Equal(t, prevCount+1, len(extractor.tableSourcesFrom), "LATERAL should be added as a table source")
- assert.Equal(t, "lateral", extractor.tableSourcesFrom[prevCount].GetTableName(), "LATERAL name mismatch")
+ require.Len(t, span.Results, 4, "self-join SELECT * must yield 2N=4 result columns")
+ var ids, emails int
+ for _, r := range span.Results {
+ switch r.Name {
+ case "id":
+ ids++
+ case "email":
+ emails++
+ default:
+ }
+ }
+ assert.Equal(t, 2, ids, "expected two 'id' result columns (one per instance)")
+ assert.Equal(t, 2, emails, "expected two 'email' result columns (one per instance)")
}
-func TestExtractQualifiedNameParts(t *testing.T) {
- // Test the Trino-specific 3-part naming convention (catalog.schema.table)
-
- // Create the extractor and listener
+func TestQuerySpan_ShadowedAliasStillMasked(t *testing.T) {
+ // An alias reused across scopes ("u" for users outside, for orders inside an
+ // EXISTS subquery) must not drop lineage for the outer u.email. Additive
+ // alias resolution keeps users.email so the sensitive column stays masked.
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Name: "catalog1",
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "users",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "integer"},
+ {Name: "email", Type: "varchar"},
+ },
+ },
+ {
+ Name: "orders",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "integer"},
+ },
+ },
+ },
+ },
+ },
+ }
+ getter, lister := buildMockDatabaseMetadataGetter([]*storepb.DatabaseSchemaMetadata{metadata})
gCtx := base.GetQuerySpanContext{
- InstanceID: "test-instance",
- Engine: storepb.Engine_TRINO,
+ GetDatabaseMetadataFunc: getter,
+ ListDatabaseNamesFunc: lister,
+ Engine: storepb.Engine_TRINO,
}
- extractor := newQuerySpanExtractor("default_catalog", "default_schema", gCtx, false)
+ extractor := newQuerySpanExtractor("catalog1", "public", gCtx, false)
+ span, err := extractor.getQuerySpan(context.Background(), "SELECT u.email FROM users u WHERE EXISTS (SELECT 1 FROM orders u)")
+ require.NoError(t, err)
- // Test extracting database, schema, and table names when fully qualified
- db, schema, table := "catalog1", "schema1", "table1"
+ require.Len(t, span.Results, 1)
+ _, ok := span.Results[0].SourceColumns[base.ColumnResource{Database: "catalog1", Schema: "public", Table: "users", Column: "email"}]
+ assert.True(t, ok, "u.email must resolve to users.email despite the shadowing inner alias; got %v", span.Results[0].SourceColumns)
+}
- // Create a test function that simulates extracting qualified name parts
- extractedDB, extractedSchema, extractedTable := db, schema, table
+func TestQuerySpan_QualifiedStarSourceColumns(t *testing.T) {
+ // A qualified star (u.*) must expand to the aliased relation's columns, each
+ // carrying its physical source column so they stay maskable. omni names the
+ // result "u.*"; only u's table (users) is expanded, not the joined orders.
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Name: "catalog1",
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "users",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "integer"},
+ {Name: "email", Type: "varchar"},
+ },
+ },
+ {
+ Name: "orders",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "integer"},
+ {Name: "amount", Type: "double"},
+ },
+ },
+ },
+ },
+ },
+ }
+ getter, lister := buildMockDatabaseMetadataGetter([]*storepb.DatabaseSchemaMetadata{metadata})
+ gCtx := base.GetQuerySpanContext{
+ GetDatabaseMetadataFunc: getter,
+ ListDatabaseNamesFunc: lister,
+ Engine: storepb.Engine_TRINO,
+ }
+ // Genuine qualified stars in several written forms must all expand to users'
+ // columns (id, email), each mapped to its physical source column, and never
+ // the joined orders. Covers alias (u.*), bare table (users.*) and
+ // schema-qualified (public.users.*) qualifiers.
+ for _, stmt := range []string{
+ "SELECT u.* FROM users u JOIN orders o ON u.id = o.id",
+ "SELECT users.* FROM users JOIN orders o ON users.id = o.id",
+ "SELECT public.users.* FROM users JOIN orders o ON users.id = o.id",
+ } {
+ extractor := newQuerySpanExtractor("catalog1", "public", gCtx, false)
+ span, err := extractor.getQuerySpan(context.Background(), stmt)
+ require.NoError(t, err, "stmt: %s", stmt)
+
+ require.Lenf(t, span.Results, 2, "qualified star must yield exactly 2 result columns; stmt: %s", stmt)
+ assert.Equalf(t, "id", span.Results[0].Name, "stmt: %s", stmt)
+ assert.Equalf(t, "email", span.Results[1].Name, "stmt: %s", stmt)
+ _, ok := span.Results[0].SourceColumns[base.ColumnResource{Database: "catalog1", Schema: "public", Table: "users", Column: "id"}]
+ assert.Truef(t, ok, "result id must map to users.id; stmt: %s got %v", stmt, span.Results[0].SourceColumns)
+ _, ok = span.Results[1].SourceColumns[base.ColumnResource{Database: "catalog1", Schema: "public", Table: "users", Column: "email"}]
+ assert.Truef(t, ok, "result email must map to users.email; stmt: %s got %v", stmt, span.Results[1].SourceColumns)
+ for _, r := range span.Results {
+ for sc := range r.SourceColumns {
+ assert.NotEqualf(t, "orders", sc.Table, "qualified star must not include orders columns; stmt: %s got %v", stmt, r.SourceColumns)
+ }
+ }
+ }
+}
- // Verify the extraction works as expected
- assert.Equal(t, "catalog1", extractedDB, "Database/catalog name should be extracted correctly")
- assert.Equal(t, "schema1", extractedSchema, "Schema name should be extracted correctly")
- assert.Equal(t, "table1", extractedTable, "Table name should be extracted correctly")
+func TestQuerySpan_QualifiedStarDistinguishesSameNamedTables(t *testing.T) {
+ // Two tables named "users" in different schemas. A qualified star s1.users.*
+ // must expand to ONLY s1.users' columns, not s2.users' — matching on the full
+ // (database, schema, table), not just the table name. Otherwise the positional
+ // masker could apply s2's policies to s1's columns and leak.
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Name: "catalog1",
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "s1",
+ Tables: []*storepb.TableMetadata{
+ {Name: "users", Columns: []*storepb.ColumnMetadata{{Name: "id", Type: "integer"}, {Name: "secret", Type: "varchar"}}},
+ },
+ },
+ {
+ Name: "s2",
+ Tables: []*storepb.TableMetadata{
+ {Name: "users", Columns: []*storepb.ColumnMetadata{{Name: "id", Type: "integer"}, {Name: "name", Type: "varchar"}}},
+ },
+ },
+ },
+ }
+ getter, lister := buildMockDatabaseMetadataGetter([]*storepb.DatabaseSchemaMetadata{metadata})
+ gCtx := base.GetQuerySpanContext{
+ GetDatabaseMetadataFunc: getter,
+ ListDatabaseNamesFunc: lister,
+ Engine: storepb.Engine_TRINO,
+ }
+ extractor := newQuerySpanExtractor("catalog1", "s1", gCtx, false)
+ span, err := extractor.getQuerySpan(context.Background(), "SELECT s1.users.* FROM s1.users JOIN s2.users ON s1.users.id = s2.users.id")
+ require.NoError(t, err)
+
+ require.Len(t, span.Results, 2, "s1.users.* must expand to exactly s1.users' two columns")
+ _, hasSecret := span.Results[1].SourceColumns[base.ColumnResource{Database: "catalog1", Schema: "s1", Table: "users", Column: "secret"}]
+ assert.True(t, hasSecret, "expected s1.users.secret; got %v", span.Results[1].SourceColumns)
+ // No column may resolve to schema s2.
+ for _, r := range span.Results {
+ for sc := range r.SourceColumns {
+ assert.NotEqual(t, "s2", sc.Schema, "s1.users.* must not include s2.users columns; got %v", r.SourceColumns)
+ }
+ }
+}
- // Test with default values for partially qualified names
- partialResource := base.ColumnResource{
- Database: extractor.defaultDatabase,
- Schema: extractor.defaultSchema,
+func TestQuerySpan_AliasEndingInStarIsNotExpanded(t *testing.T) {
+ // A column aliased to a quoted identifier that ends in ".*" (SELECT email AS
+ // "u.*") must NOT be treated as a qualified star. It is one output column
+ // (email); star-expanding it would emit several results and misalign the
+ // positional masker, leaking the value. Detection keys on the source-ref
+ // shape, so this resolves to a single email result.
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Name: "catalog1",
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "users",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "integer"},
+ {Name: "email", Type: "varchar"},
+ },
+ },
+ },
+ },
+ },
+ }
+ getter, lister := buildMockDatabaseMetadataGetter([]*storepb.DatabaseSchemaMetadata{metadata})
+ gCtx := base.GetQuerySpanContext{
+ GetDatabaseMetadataFunc: getter,
+ ListDatabaseNamesFunc: lister,
+ Engine: storepb.Engine_TRINO,
}
+ extractor := newQuerySpanExtractor("catalog1", "public", gCtx, false)
+ span, err := extractor.getQuerySpan(context.Background(), `SELECT email AS "u.*" FROM users u`)
+ require.NoError(t, err)
+
+ require.Len(t, span.Results, 1, `SELECT email AS "u.*" is one column, not a star expansion`)
+ _, ok := span.Results[0].SourceColumns[base.ColumnResource{Database: "catalog1", Schema: "public", Table: "users", Column: "email"}]
+ assert.True(t, ok, "the aliased column must still resolve to users.email; got %v", span.Results[0].SourceColumns)
+}
- // Verify defaults are applied
- assert.Equal(t, "default_catalog", partialResource.Database, "Default catalog not applied")
- assert.Equal(t, "default_schema", partialResource.Schema, "Default schema not applied")
+func TestQuerySpan_ThreePartNaming(t *testing.T) {
+ // Trino's catalog.schema.table maps onto Database/Schema/Table, and the
+ // default database/schema are NOT applied when the reference is fully
+ // qualified.
+ sources := tablesFromSpan(t,
+ "SELECT a.id, b.id FROM catalog1.public.users a JOIN catalog2.public.orders b ON a.id = b.user_id",
+ "catalog3")
+
+ _, hasUsers := sources[base.ColumnResource{Database: "catalog1", Schema: "public", Table: "users"}]
+ _, hasOrders := sources[base.ColumnResource{Database: "catalog2", Schema: "public", Table: "orders"}]
+ assert.True(t, hasUsers, "expected catalog1.public.users; got %v", sources)
+ assert.True(t, hasOrders, "expected catalog2.public.orders; got %v", sources)
}
diff --git a/backend/plugin/parser/trino/query_span_listener.go b/backend/plugin/parser/trino/query_span_listener.go
deleted file mode 100644
index 5bf19654dd..0000000000
--- a/backend/plugin/parser/trino/query_span_listener.go
+++ /dev/null
@@ -1,420 +0,0 @@
-// Package trino provides SQL parser for Trino.
-package trino
-
-import (
- "fmt"
- "strings"
-
- "github.com/antlr4-go/antlr/v4"
- parser "github.com/bytebase/parser/trino"
-
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
-)
-
-// trinoQuerySpanListener walks the parse tree to extract query span information.
-type trinoQuerySpanListener struct {
- parser.BaseTrinoParserListener
-
- extractor *querySpanExtractor
- results []base.QuerySpanResult
- err error
-
- // Current CTE being processed
- currentCTE *base.PseudoTable
-}
-
-// newTrinoQuerySpanListener creates a new listener with the given extractor.
-func newTrinoQuerySpanListener(extractor *querySpanExtractor) *trinoQuerySpanListener {
- return &trinoQuerySpanListener{
- extractor: extractor,
- results: []base.QuerySpanResult{},
- }
-}
-
-// EnterQuery processes the top level query and WITH clauses if present.
-func (l *trinoQuerySpanListener) EnterQuery(ctx *parser.QueryContext) {
- if l.err != nil {
- return
- }
-
- if ctx.With() != nil {
- // Process WITH clause
- withCtx := ctx.With()
-
- // Process each named query in the WITH clause
- for _, namedQueryCtx := range withCtx.AllNamedQuery() {
- if namedQueryCtx.Identifier() == nil || namedQueryCtx.Query() == nil {
- continue
- }
-
- // Get the CTE name
- cteName := NormalizeTrinoIdentifier(namedQueryCtx.Identifier().GetText())
-
- // Create a new pseudo table for this CTE
- l.currentCTE = base.NewPseudoTable(cteName, nil)
-
- // Process the CTE query
- queryTree := namedQueryCtx.Query()
- if queryTree != nil {
- // Process subquery to extract columns
- if err := l.extractor.extractPredicateColumnFromSubquery(queryTree); err != nil {
- l.err = err
- return
- }
- }
-
- // Add the CTE to our list
- l.extractor.ctes = append(l.extractor.ctes, l.currentCTE)
- l.currentCTE = nil
- }
- }
-}
-
-// EnterTableName processes table references in the query.
-func (l *trinoQuerySpanListener) EnterTableName(ctx *parser.TableNameContext) {
- if l.err != nil {
- return
- }
-
- if ctx.QualifiedName() == nil {
- return
- }
-
- // Extract database, schema, and table name
- db, schema, table := ExtractDatabaseSchemaName(
- ctx.QualifiedName(),
- l.extractor.defaultDatabase,
- l.extractor.defaultSchema,
- )
-
- // Check if this is a CTE reference
- for _, cte := range l.extractor.ctes {
- if strings.EqualFold(cte.Name, table) {
- // This is a CTE reference
- l.extractor.tableSourcesFrom = append(l.extractor.tableSourcesFrom, cte)
- return
- }
- }
-
- // Add table source to track where columns come from
- // Attempt to find the table in the schema first
- tableMeta, err := l.extractor.findTableSchema(db, schema, table)
- if err != nil {
- // We don't set l.err here because table references might be CTEs or subqueries
-
- // Create physical table without columns as fallback
- physicalTable := &base.PhysicalTable{
- Database: db,
- Schema: schema,
- Name: table,
- Columns: []string{}, // Empty columns
- }
- l.extractor.tableSourcesFrom = append(l.extractor.tableSourcesFrom, physicalTable)
- return
- }
-
- // Create physical table with column names from metadata
- var columnNames []string
- for _, col := range tableMeta.GetProto().GetColumns() {
- columnNames = append(columnNames, col.Name)
- }
-
- physicalTable := &base.PhysicalTable{
- Database: db,
- Schema: schema,
- Name: tableMeta.GetProto().Name,
- Columns: columnNames, // Populate with actual column names
- }
- l.extractor.tableSourcesFrom = append(l.extractor.tableSourcesFrom, physicalTable)
-
- // Add each column from the table as a source column
- for _, col := range tableMeta.GetProto().GetColumns() {
- colResource := base.ColumnResource{
- Database: db,
- Schema: schema,
- Table: table,
- Column: col.Name,
- }
- l.extractor.addSourceColumn(colResource)
- }
-}
-
-// EnterSelectAll handles all cases of SELECT * expressions.
-func (l *trinoQuerySpanListener) EnterSelectAll(_ *parser.SelectAllContext) {
- if l.err != nil {
- return
- }
-
- // Mark that we encountered a SELECT * - we'll expand it later after all tables are processed
- result := base.QuerySpanResult{
- Name: "*",
- SourceColumns: make(base.SourceColumnSet), // Will be populated later
- IsPlainField: false,
- SelectAsterisk: true,
- }
-
- l.results = append(l.results, result)
-}
-
-// EnterSelectSingle processes individual SELECT items.
-func (l *trinoQuerySpanListener) EnterSelectSingle(ctx *parser.SelectSingleContext) {
- if l.err != nil {
- return
- }
-
- var resultName string
- var sourceColumns = make(base.SourceColumnSet)
- isPlainField := false
-
- if ctx.Expression() != nil {
- expr := ctx.Expression()
-
- // Check for simple column references
- columnName := l.extractColumnName(expr)
- if columnName != "" {
- isPlainField = true
- // Find matching source columns
- for col := range l.extractor.sourceColumns {
- if col.Column == columnName {
- sourceColumns[col] = true
- }
- }
- } else {
- // For complex expressions, extract all column references
- l.extractAllColumnReferences(expr, sourceColumns)
- }
-
- resultName = expr.GetText()
- }
-
- // Override with alias if provided
- if ctx.As_column_alias() != nil && ctx.As_column_alias().Column_alias() != nil && ctx.As_column_alias().Column_alias().Identifier() != nil {
- resultName = NormalizeTrinoIdentifier(ctx.As_column_alias().Column_alias().Identifier().GetText())
- }
-
- // Generate name if none found
- if resultName == "" {
- resultName = fmt.Sprintf("_col%d", len(l.results))
- }
-
- result := base.QuerySpanResult{
- Name: resultName,
- SourceColumns: sourceColumns,
- IsPlainField: isPlainField,
- }
-
- l.results = append(l.results, result)
-}
-
-// extractAllColumnReferences gathers all column references from a complex expression.
-// This uses ANTLR tree traversal instead of regex patterns for better accuracy.
-func (l *trinoQuerySpanListener) extractAllColumnReferences(expr parser.IExpressionContext, sourceColumns base.SourceColumnSet) {
- if expr == nil {
- return
- }
-
- // Use ANTLR tree traversal to find column references
- if err := l.extractor.extractPredicateColumnFromExpression(expr); err != nil {
- return // Best effort - don't fail on complex expressions
- }
-
- // Copy predicate columns found to result source columns
- for col := range l.extractor.predicateColumns {
- sourceColumns[col] = true
- }
-}
-
-// EnterQuerySpecification processes query specifications, including WHERE clauses
-func (l *trinoQuerySpanListener) EnterQuerySpecification(ctx *parser.QuerySpecificationContext) {
- if l.err != nil {
- return
- }
-
- // Process WHERE clause using consistent predicate extraction
- if ctx.GetWhere() != nil {
- if err := l.extractor.extractPredicateColumnFromBooleanExpression(ctx.GetWhere()); err != nil {
- l.err = err
- }
- }
-}
-
-// EnterJoinCriteria processes JOIN conditions to extract predicate columns.
-func (l *trinoQuerySpanListener) EnterJoinCriteria(ctx *parser.JoinCriteriaContext) {
- if l.err != nil {
- return
- }
-
- // Use consistent predicate extraction following TSQL pattern
- if err := l.extractor.processJoinPredicate(ctx); err != nil {
- l.err = err
- }
-}
-
-// EnterUnnest processes UNNEST expressions, a Trino-specific feature.
-func (l *trinoQuerySpanListener) EnterUnnest(ctx *parser.UnnestContext) {
- if l.err != nil {
- return
- }
-
- // Process the expressions being unnested
- for _, expr := range ctx.AllExpression() {
- // Extract column references from the UNNEST expression
- columnName := l.extractColumnName(expr)
- if columnName != "" {
- // Add as source column since UNNEST creates a derived table
- for col := range l.extractor.sourceColumns {
- if col.Column == columnName {
- l.extractor.sourceColumns[col] = true
- }
- }
- }
- }
-
- // Create a derived table for the UNNEST
- results := []base.QuerySpanResult{}
- for i, expr := range ctx.AllExpression() {
- // In UNNEST, each column would have a name based on its position
- name := fmt.Sprintf("_unnest%d", i)
-
- // Try to extract source column information
- colSourceColumns := make(base.SourceColumnSet)
- columnName := l.extractColumnName(expr)
- if columnName != "" {
- for col := range l.extractor.sourceColumns {
- if col.Column == columnName {
- colSourceColumns[col] = true
- }
- }
- }
-
- results = append(results, base.QuerySpanResult{
- Name: name,
- SourceColumns: colSourceColumns,
- IsPlainField: true,
- })
- }
-
- // Register as a derived table source
- unnestTable := base.NewPseudoTable("unnest", results)
- l.extractor.tableSourcesFrom = append(l.extractor.tableSourcesFrom, unnestTable)
-}
-
-// EnterLateral processes LATERAL subqueries, a Trino-specific feature.
-func (l *trinoQuerySpanListener) EnterLateral(ctx *parser.LateralContext) {
- if l.err != nil {
- return
- }
-
- // Save outer table sources to allow resolving columns in lateral query
- originalOuterTables := l.extractor.outerTableSources
-
- // For a LATERAL subquery, columns from tables to the left are visible inside
- l.extractor.outerTableSources = append(l.extractor.outerTableSources, l.extractor.tableSourcesFrom...)
-
- // Process the subquery
- if ctx.Query() != nil {
- // Create a new extractor for the subquery
- subExtractor := newQuerySpanExtractor(
- l.extractor.defaultDatabase,
- l.extractor.defaultSchema,
- l.extractor.gCtx,
- l.extractor.ignoreCaseSensitive,
- )
-
- // Copy outer table sources
- subExtractor.outerTableSources = append(subExtractor.outerTableSources, l.extractor.outerTableSources...)
-
- // Copy existing CTEs
- subExtractor.ctes = append(subExtractor.ctes, l.extractor.ctes...)
-
- // Process the subquery with a new listener
- subListener := newTrinoQuerySpanListener(subExtractor)
- antlr.ParseTreeWalkerDefault.Walk(subListener, ctx.Query())
-
- // Create a derived table for the lateral subquery
- lateralTable := base.NewPseudoTable("lateral", subListener.results)
-
- // Add to our table sources
- l.extractor.tableSourcesFrom = append(l.extractor.tableSourcesFrom, lateralTable)
-
- // Add columns from the lateral subquery to our source columns
- for _, result := range subListener.results {
- for col := range result.SourceColumns {
- l.extractor.sourceColumns[col] = true
- }
- }
-
- // Merge predicates from subquery
- for col := range subListener.extractor.predicateColumns {
- l.extractor.predicateColumns[col] = true
- }
- }
-
- // Restore outer table sources
- l.extractor.outerTableSources = originalOuterTables
-}
-
-// EnterQueryNoWith processes queries, including those with set operations (UNION, INTERSECT, EXCEPT)
-func (l *trinoQuerySpanListener) EnterQueryNoWith(_ *parser.QueryNoWithContext) {
- if l.err != nil {
- return
- }
-
- // This method is used to extract information from a query
- // The actual parsing work is done by ANTLR, which will call the
- // appropriate Enter* methods for each node in the parse tree
-
- // We don't need special handling here - the listener will automatically
- // process all parts of the query, including both sides of set operations
-
- // The main logic for predicate column extraction is in processPredicateExpressions
- // and other visitor methods
-}
-
-// addPredicateColumn adds a column to the predicate columns list.
-func (l *trinoQuerySpanListener) addPredicateColumn(columnName string) {
- if columnName == "" {
- return
- }
-
- // Find matching source columns and add them to predicates
- for col := range l.extractor.sourceColumns {
- if col.Column == columnName {
- l.extractor.predicateColumns[col] = true
- }
- }
-}
-
-// extractColumnName attempts to extract a column reference from an expression.
-// Returns the column name if the expression is a simple column reference, or empty string otherwise.
-func (l *trinoQuerySpanListener) extractColumnName(expr parser.IExpressionContext) string {
- if expr == nil {
- return ""
- }
-
- exprText := expr.GetText()
- if exprText == "" {
- return ""
- }
-
- // Handle qualified column names (e.g., table.column)
- parts := strings.Split(exprText, ".")
- if len(parts) > 1 {
- columnName := parts[len(parts)-1]
- // Check if this matches a known column
- for col := range l.extractor.sourceColumns {
- if col.Column == columnName {
- return columnName
- }
- }
- }
-
- // Check if the entire expression is a column name
- for col := range l.extractor.sourceColumns {
- if col.Column == exprText {
- return exprText
- }
- }
-
- return ""
-}
diff --git a/backend/plugin/parser/trino/query_span_predicate.go b/backend/plugin/parser/trino/query_span_predicate.go
deleted file mode 100644
index add91251c9..0000000000
--- a/backend/plugin/parser/trino/query_span_predicate.go
+++ /dev/null
@@ -1,158 +0,0 @@
-package trino
-
-import (
- "github.com/antlr4-go/antlr/v4"
- parser "github.com/bytebase/parser/trino"
- "github.com/pkg/errors"
-
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
-)
-
-// extractPredicateColumnFromBooleanExpression extracts predicate columns from boolean expressions.
-// This follows the TSQL pattern for consistent predicate extraction.
-func (q *querySpanExtractor) extractPredicateColumnFromBooleanExpression(
- ctx parser.IBooleanExpressionContext,
-) error {
- if ctx == nil {
- return nil
- }
-
- // Use generic tree walking to find all expressions
- return q.extractPredicateColumnFromExpression(ctx)
-}
-
-// extractPredicateColumnFromExpression recursively walks the expression tree to find column references.
-// This is the core method following TSQL's recursive pattern but adapted for Trino.
-func (q *querySpanExtractor) extractPredicateColumnFromExpression(
- ctx antlr.ParserRuleContext,
-) error {
- if ctx == nil {
- return nil
- }
-
- switch typedCtx := ctx.(type) {
- case parser.IQualifiedNameContext:
- // Qualified name that might be a column reference
- col := q.extractColumnFromQualifiedName(typedCtx)
- if col != nil {
- q.predicateColumns[*col] = true
- }
-
- case parser.IQueryContext:
- // Subquery in expression
- if err := q.extractPredicateColumnFromSubquery(typedCtx); err != nil {
- return err
- }
- default:
- }
-
- // Recursively process all child nodes following TSQL pattern
- for _, child := range ctx.GetChildren() {
- if childCtx, ok := child.(antlr.ParserRuleContext); ok {
- if err := q.extractPredicateColumnFromExpression(childCtx); err != nil {
- return err
- }
- }
- }
-
- return nil
-}
-
-// extractPredicateColumnFromSubquery processes subqueries within predicates.
-// This follows the TSQL pattern for subquery handling.
-func (q *querySpanExtractor) extractPredicateColumnFromSubquery(
- ctx parser.IQueryContext,
-) error {
- if ctx == nil {
- return nil
- }
-
- // Get subquery text for processing
- subquery := ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx)
-
- // Create new extractor for subquery, following TSQL pattern
- newExtractor := newQuerySpanExtractor(q.defaultDatabase, q.defaultSchema, q.gCtx, q.ignoreCaseSensitive)
- newExtractor.ctes = q.ctes // Preserve CTE context
-
- span, err := newExtractor.getQuerySpan(q.ctx, subquery)
- if err != nil {
- return errors.Wrapf(err, "failed to get query span for subquery: %s", subquery)
- }
-
- // Merge predicate columns from subquery
- q.predicateColumns, _ = base.MergeSourceColumnSet(q.predicateColumns, span.PredicateColumns)
-
- // Add result columns as predicates (they're used in the outer predicate context)
- for _, result := range span.Results {
- q.predicateColumns, _ = base.MergeSourceColumnSet(q.predicateColumns, result.SourceColumns)
- }
-
- return nil
-}
-
-// extractColumnFromQualifiedName extracts a column resource from a qualified name context.
-func (q *querySpanExtractor) extractColumnFromQualifiedName(ctx parser.IQualifiedNameContext) *base.ColumnResource {
- if ctx == nil {
- return nil
- }
-
- db, schema, table := ExtractDatabaseSchemaName(ctx, q.defaultDatabase, q.defaultSchema)
-
- // If we have table qualification, it's likely a column reference
- if table != "" {
- parts := ctx.AllIdentifier()
- if len(parts) > 0 {
- columnName := NormalizeTrinoIdentifier(parts[len(parts)-1].GetText())
-
- // Try to match with source columns
- for col := range q.sourceColumns {
- if col.Column == columnName {
- // Check table match if specified
- if col.Table == "" || col.Table == table {
- return &col
- }
- }
- }
-
- // Create column resource if not found in source columns
- return &base.ColumnResource{
- Database: db,
- Schema: schema,
- Table: table,
- Column: columnName,
- }
- }
- }
-
- return nil
-}
-
-// processJoinPredicate extracts predicate columns from JOIN conditions.
-// This follows the TSQL pattern for join predicate handling.
-func (q *querySpanExtractor) processJoinPredicate(ctx parser.IJoinCriteriaContext) error {
- if ctx == nil {
- return nil
- }
-
- // Process ON clause predicates
- if ctx.ON_() != nil && ctx.BooleanExpression() != nil {
- if err := q.extractPredicateColumnFromBooleanExpression(ctx.BooleanExpression()); err != nil {
- return err
- }
- }
-
- // Process USING clause - columns in USING are predicates by definition
- if ctx.USING_() != nil {
- for _, ident := range ctx.AllIdentifier() {
- columnName := NormalizeTrinoIdentifier(ident.GetText())
- // Find matching source columns and add as predicates
- for col := range q.sourceColumns {
- if col.Column == columnName {
- q.predicateColumns[col] = true
- }
- }
- }
- }
-
- return nil
-}
diff --git a/backend/plugin/parser/trino/query_test.go b/backend/plugin/parser/trino/query_test.go
index cf99a0ab9d..211a4bf6ab 100644
--- a/backend/plugin/parser/trino/query_test.go
+++ b/backend/plugin/parser/trino/query_test.go
@@ -67,8 +67,8 @@ func TestValidateQuery(t *testing.T) {
{
name: "EXPLAIN ANALYZE UPDATE",
sql: "EXPLAIN ANALYZE UPDATE users SET name = 'John' WHERE id = 1",
- wantReadOnly: true, // EXPLAIN ANALYZE is always treated as read-only
- wantData: true, // EXPLAIN ANALYZE always returns data
+ wantReadOnly: false, // EXPLAIN ANALYZE EXECUTES the inner UPDATE, so it is NOT read-only
+ wantData: false,
wantErr: false,
},
{
diff --git a/backend/plugin/parser/trino/query_type.go b/backend/plugin/parser/trino/query_type.go
index 9841edab3a..780a81cf81 100644
--- a/backend/plugin/parser/trino/query_type.go
+++ b/backend/plugin/parser/trino/query_type.go
@@ -3,359 +3,116 @@ package trino
import (
"strings"
- "github.com/antlr4-go/antlr/v4"
- parser "github.com/bytebase/parser/trino"
+ "github.com/bytebase/omni/trino/ast"
+ "github.com/bytebase/omni/trino/parser"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
)
-// StatementType is the type of the SQL statement.
-type StatementType int
-
-// Statement types
-const (
- Unsupported StatementType = iota
- Select
- Explain
- Insert
- Update
- Delete
- Merge
- CreateTable
- CreateView
- AlterTable
- DropTable
- DropView
- CreateSchema
- DropSchema
- RenameTable
- CreateTableAsSelect
- Set
- Show
-)
-
-// queryTypeListener is a parser listener for determining the query type
-type queryTypeListener struct {
- parser.BaseTrinoParserListener
-
- result base.QueryType
- isExplainAnalyze bool
-}
-
-// getQueryType returns the type of the statement.
-func getQueryType(node any) (base.QueryType, bool) {
- stmtCtx, ok := node.(*parser.SingleStatementContext)
- if !ok {
+// getQueryType classifies a single parsed Trino statement.
+//
+// It returns the base.QueryType plus a bool that is true only for
+// EXPLAIN ANALYZE. EXPLAIN ANALYZE actually executes the inner query, so the
+// legacy plugin reported it as base.Select (not base.Explain) with the flag
+// set; we preserve that. A plain EXPLAIN is base.Explain and never executes.
+//
+// allSystems, when true, promotes a user SELECT to base.SelectInfoSchema. The
+// query-span extractor passes this in based on whether every accessed table is
+// a system table; callers that only have the AST pass false and rely on the
+// statement-text system-schema scan (containsSystemSchema) instead.
+//
+// node is an omni AST node (one of the *parser.*Stmt types). A nil node yields
+// (QueryTypeUnknown, false).
+func getQueryType(node ast.Node) (base.QueryType, bool) {
+ if node == nil {
return base.QueryTypeUnknown, false
}
-
- // First check if this is an EXPLAIN statement by examining the statement directly
- if stmt := stmtCtx.Statement(); stmt != nil {
- switch stmt.(type) {
- case *parser.ExplainContext:
- return base.Explain, false
- case *parser.ExplainAnalyzeContext:
- return base.Select, true // EXPLAIN ANALYZE is treated as SELECT since it actually runs the query
+ switch n := node.(type) {
+ case *parser.QueryStmt:
+ return base.Select, false
+ case *parser.ExplainStmt:
+ if n.Analyze {
+ // EXPLAIN ANALYZE EXECUTES the inner statement (oracle-confirmed:
+ // Trino 481 runs it), so its read-only-ness is the inner statement's,
+ // not unconditionally read-only. Report the inner type and flag
+ // isAnalyze so validateQuery rejects EXPLAIN ANALYZE of a non-SELECT
+ // (e.g. EXPLAIN ANALYZE UPDATE, which would otherwise run a write
+ // through the read-only SQL-editor gate).
+ innerType, _ := getQueryType(n.Statement)
+ return innerType, true
}
+ return base.Explain, false
+ case *parser.ShowStmt,
+ *parser.DescribeInputStmt, *parser.DescribeOutputStmt:
+ return base.SelectInfoSchema, false
+ case *parser.SetSessionStmt, *parser.ResetSessionStmt,
+ *parser.SetSessionAuthorizationStmt, *parser.ResetSessionAuthorizationStmt,
+ *parser.SetPathStmt, *parser.SetRoleStmt, *parser.SetTimeZoneStmt,
+ *parser.UseStmt:
+ // Session-state statements were classified read-only (base.Select) by the
+ // legacy listener (EnterSetSession / EnterResetSession). USE is also a
+ // session-scoped no-op for read purposes. Keep them read-only.
+ return base.Select, false
+ case *parser.InsertStmt, *parser.UpdateStmt, *parser.DeleteStmt,
+ *parser.MergeStmt, *parser.TruncateStmt, *parser.CallStmt:
+ return base.DML, false
}
-
- // For other statement types, use the listener
- listener := &queryTypeListener{
- result: base.QueryTypeUnknown,
- }
-
- antlr.ParseTreeWalkerDefault.Walk(listener, stmtCtx)
-
- return listener.result, listener.isExplainAnalyze
-}
-
-// EnterStatementDefault is called when entering a StatementDefaultContext rule.
-func (l *queryTypeListener) EnterStatementDefault(ctx *parser.StatementDefaultContext) {
- // Regular SELECT query
- if containsSystemSchema(ctx.GetText()) {
- l.result = base.SelectInfoSchema
- } else {
- l.result = base.Select
- }
-}
-
-// EnterExplain is called when entering an ExplainContext rule.
-func (l *queryTypeListener) EnterExplain(_ *parser.ExplainContext) {
- l.result = base.Explain
-}
-
-// EnterExplainAnalyze is called when entering an ExplainAnalyzeContext rule.
-func (l *queryTypeListener) EnterExplainAnalyze(ctx *parser.ExplainAnalyzeContext) {
- // For EXPLAIN ANALYZE, we treat it as a SELECT query because it actually executes the query
- l.result = base.Select
- l.isExplainAnalyze = true
-
- // Check if this is an EXPLAIN ANALYZE of a query that accesses system tables
- if containsSystemSchema(ctx.GetText()) {
- l.result = base.SelectInfoSchema
+ // Everything else (CREATE/ALTER/DROP/COMMENT/GRANT/REVOKE/ANALYZE/
+ // transaction control/prepared-statement admin/...) is DDL. This is the safe
+ // upper bound for ACL purposes and matches the legacy listener's
+ // "everything else is DDL" default.
+ return base.DDL, false
+}
+
+// queryTypeFromText parses statement and classifies its single top-level
+// statement, applying the legacy statement-text system-schema promotion: a
+// base.Select that references a Trino system/metadata schema becomes
+// base.SelectInfoSchema. EXPLAIN over such a query is likewise promoted.
+//
+// On parse failure or empty/multi input it returns base.QueryTypeUnknown.
+func queryTypeFromText(statement string) base.QueryType {
+ file, errs := parser.Parse(statement)
+ if len(errs) > 0 || file == nil || len(file.Stmts) == 0 {
+ return base.QueryTypeUnknown
}
-}
-
-// EnterInsertInto is called when entering an InsertIntoContext rule.
-func (l *queryTypeListener) EnterInsertInto(_ *parser.InsertIntoContext) {
- l.result = base.DML
-}
-
-// EnterUpdate is called when entering an UpdateContext rule.
-func (l *queryTypeListener) EnterUpdate(_ *parser.UpdateContext) {
- l.result = base.DML
-}
-
-// EnterDelete is called when entering a DeleteContext rule.
-func (l *queryTypeListener) EnterDelete(_ *parser.DeleteContext) {
- l.result = base.DML
-}
-
-// EnterMerge is called when entering a MergeContext rule.
-func (l *queryTypeListener) EnterMerge(_ *parser.MergeContext) {
- l.result = base.DML
-}
-
-// EnterCall is called when entering a CallContext rule (CALL stored procedure).
-func (l *queryTypeListener) EnterCall(_ *parser.CallContext) {
- l.result = base.DML
-}
-
-// EnterCreateTable is called when entering a CreateTableContext rule.
-func (l *queryTypeListener) EnterCreateTable(_ *parser.CreateTableContext) {
- l.result = base.DDL
-}
-
-// EnterCreateTableAsSelect is called when entering a CreateTableAsSelectContext rule.
-func (l *queryTypeListener) EnterCreateTableAsSelect(_ *parser.CreateTableAsSelectContext) {
- l.result = base.DDL
-}
-
-// EnterCreateView is called when entering a CreateViewContext rule.
-func (l *queryTypeListener) EnterCreateView(_ *parser.CreateViewContext) {
- l.result = base.DDL
-}
-
-// EnterAddColumn is called when entering an AddColumnContext rule.
-func (l *queryTypeListener) EnterAddColumn(_ *parser.AddColumnContext) {
- l.result = base.DDL
-}
-
-// EnterDropColumn is called when entering a DropColumnContext rule.
-func (l *queryTypeListener) EnterDropColumn(_ *parser.DropColumnContext) {
- l.result = base.DDL
-}
-
-// EnterRenameColumn is called when entering a RenameColumnContext rule.
-func (l *queryTypeListener) EnterRenameColumn(_ *parser.RenameColumnContext) {
- l.result = base.DDL
-}
-
-// EnterSetColumnType is called when entering a SetColumnTypeContext rule.
-func (l *queryTypeListener) EnterSetColumnType(_ *parser.SetColumnTypeContext) {
- l.result = base.DDL
-}
-
-// EnterSetTableAuthorization is called when entering a SetTableAuthorizationContext rule.
-func (l *queryTypeListener) EnterSetTableAuthorization(_ *parser.SetTableAuthorizationContext) {
- l.result = base.DDL
-}
-
-// EnterSetTableProperties is called when entering a SetTablePropertiesContext rule.
-func (l *queryTypeListener) EnterSetTableProperties(_ *parser.SetTablePropertiesContext) {
- l.result = base.DDL
-}
-
-// EnterDropTable is called when entering a DropTableContext rule.
-func (l *queryTypeListener) EnterDropTable(_ *parser.DropTableContext) {
- l.result = base.DDL
-}
-
-// EnterDropView is called when entering a DropViewContext rule.
-func (l *queryTypeListener) EnterDropView(_ *parser.DropViewContext) {
- l.result = base.DDL
-}
-
-// EnterCreateSchema is called when entering a CreateSchemaContext rule.
-func (l *queryTypeListener) EnterCreateSchema(_ *parser.CreateSchemaContext) {
- l.result = base.DDL
-}
-
-// EnterDropSchema is called when entering a DropSchemaContext rule.
-func (l *queryTypeListener) EnterDropSchema(_ *parser.DropSchemaContext) {
- l.result = base.DDL
-}
-
-// EnterRenameTable is called when entering a RenameTableContext rule.
-func (l *queryTypeListener) EnterRenameTable(_ *parser.RenameTableContext) {
- l.result = base.DDL
-}
-
-// EnterRenameView is called when entering a RenameViewContext rule.
-func (l *queryTypeListener) EnterRenameView(_ *parser.RenameViewContext) {
- l.result = base.DDL
-}
-
-// EnterRenameSchema is called when entering a RenameSchemaContext rule.
-func (l *queryTypeListener) EnterRenameSchema(_ *parser.RenameSchemaContext) {
- l.result = base.DDL
-}
-
-// EnterShowTables is called when entering a ShowTablesContext rule.
-func (l *queryTypeListener) EnterShowTables(_ *parser.ShowTablesContext) {
- l.result = base.SelectInfoSchema
-}
-
-// EnterShowSchemas is called when entering a ShowSchemasContext rule.
-func (l *queryTypeListener) EnterShowSchemas(_ *parser.ShowSchemasContext) {
- l.result = base.SelectInfoSchema
-}
-
-// EnterShowColumns is called when entering a ShowColumnsContext rule.
-func (l *queryTypeListener) EnterShowColumns(_ *parser.ShowColumnsContext) {
- l.result = base.SelectInfoSchema
-}
-
-// EnterShowCreateTable is called when entering a ShowCreateTableContext rule.
-func (l *queryTypeListener) EnterShowCreateTable(_ *parser.ShowCreateTableContext) {
- l.result = base.SelectInfoSchema
-}
-
-// EnterShowCreateView is called when entering a ShowCreateViewContext rule.
-func (l *queryTypeListener) EnterShowCreateView(_ *parser.ShowCreateViewContext) {
- l.result = base.SelectInfoSchema
-}
-
-// EnterShowFunctions is called when entering a ShowFunctionsContext rule.
-func (l *queryTypeListener) EnterShowFunctions(_ *parser.ShowFunctionsContext) {
- l.result = base.SelectInfoSchema
-}
-
-// EnterShowSession is called when entering a ShowSessionContext rule.
-func (l *queryTypeListener) EnterShowSession(_ *parser.ShowSessionContext) {
- l.result = base.SelectInfoSchema
-}
-
-// EnterShowStats is called when entering a ShowStatsContext rule.
-func (l *queryTypeListener) EnterShowStats(_ *parser.ShowStatsContext) {
- l.result = base.SelectInfoSchema
-}
-
-// EnterSetSession is called when entering a SetSessionContext rule.
-func (l *queryTypeListener) EnterSetSession(_ *parser.SetSessionContext) {
- l.result = base.Select
-}
-
-// EnterResetSession is called when entering a ResetSessionContext rule.
-func (l *queryTypeListener) EnterResetSession(_ *parser.ResetSessionContext) {
- l.result = base.Select
-}
-
-// GetStatementType returns the detailed statement type for query span.
-func GetStatementType(tree any) StatementType {
- stmtCtx, ok := tree.(*parser.SingleStatementContext)
- if !ok {
- return Unsupported
- }
-
- stmt := stmtCtx.Statement()
- if stmt == nil {
- return Unsupported
- }
-
- switch stmt.(type) {
- case *parser.StatementDefaultContext:
- // SELECT statement
- return Select
-
- case *parser.ExplainContext, *parser.ExplainAnalyzeContext:
- return Explain
-
- case *parser.InsertIntoContext:
- return Insert
-
- case *parser.UpdateContext:
- return Update
-
- case *parser.DeleteContext:
- return Delete
-
- case *parser.MergeContext:
- return Merge
-
- case *parser.CreateTableContext:
- return CreateTable
-
- case *parser.CreateTableAsSelectContext:
- return CreateTableAsSelect
-
- case *parser.CreateViewContext:
- return CreateView
-
- // Alter table operations
- case *parser.AddColumnContext, *parser.DropColumnContext,
- *parser.RenameColumnContext, *parser.SetColumnTypeContext,
- *parser.SetTableAuthorizationContext, *parser.SetTablePropertiesContext:
- return AlterTable
-
- case *parser.DropTableContext:
- return DropTable
-
- case *parser.DropViewContext:
- return DropView
-
- case *parser.CreateSchemaContext:
- return CreateSchema
-
- case *parser.DropSchemaContext:
- return DropSchema
-
- case *parser.RenameTableContext, *parser.RenameViewContext:
- return RenameTable
-
- case *parser.RenameSchemaContext:
- return CreateSchema // Using CreateSchema for schema operations
-
- // SHOW statements
- case *parser.ShowTablesContext, *parser.ShowSchemasContext,
- *parser.ShowColumnsContext, *parser.ShowCreateTableContext,
- *parser.ShowCreateViewContext, *parser.ShowFunctionsContext,
- *parser.ShowSessionContext, *parser.ShowStatsContext:
- return Show
-
- case *parser.SetSessionContext, *parser.ResetSessionContext:
- return Set
-
+ qt, _ := getQueryType(file.Stmts[0])
+ switch qt {
+ case base.Select, base.Explain:
+ if containsSystemSchema(statement) {
+ return base.SelectInfoSchema
+ }
default:
- return Unsupported
}
+ return qt
}
-// IsReadOnlyStatement returns whether the statement is read-only.
-func IsReadOnlyStatement(tree any) bool {
- queryType, _ := getQueryType(tree)
+// IsReadOnlyStatement reports whether the given omni AST node is a read-only
+// Trino statement (SELECT family, EXPLAIN, SHOW/DESCRIBE, session-state).
+func IsReadOnlyStatement(node ast.Node) bool {
+ queryType, _ := getQueryType(node)
return queryType == base.Select ||
queryType == base.Explain ||
queryType == base.SelectInfoSchema
}
-// IsDataChangingStatement returns whether the statement changes data.
-func IsDataChangingStatement(tree any) bool {
- queryType, _ := getQueryType(tree)
+// IsDataChangingStatement reports whether the node is a DML statement.
+func IsDataChangingStatement(node ast.Node) bool {
+ queryType, _ := getQueryType(node)
return queryType == base.DML
}
-// IsSchemaChangingStatement returns whether the statement changes schema.
-func IsSchemaChangingStatement(tree any) bool {
- queryType, _ := getQueryType(tree)
+// IsSchemaChangingStatement reports whether the node is a DDL statement.
+func IsSchemaChangingStatement(node ast.Node) bool {
+ queryType, _ := getQueryType(node)
return queryType == base.DDL
}
-// containsSystemSchema checks if a query is accessing system tables/schemas in Trino.
+// containsSystemSchema reports whether a query references a Trino system/metadata
+// schema, via a case-insensitive substring scan. This mirrors the legacy
+// plugin's heuristic (and omni analysis.Classify's refineSelect) so the SELECT →
+// SelectInfoSchema promotion is identical across the legacy and omni stacks.
func containsSystemSchema(sql string) bool {
lowerSQL := strings.ToLower(sql)
-
- // Trino system schemas/catalogs
systemPrefixes := []string{
"system.",
"information_schema.",
@@ -363,12 +120,10 @@ func containsSystemSchema(sql string) bool {
"catalog.",
"metadata.",
}
-
for _, prefix := range systemPrefixes {
if strings.Contains(lowerSQL, prefix) {
return true
}
}
-
return false
}
diff --git a/backend/plugin/parser/trino/query_type_test.go b/backend/plugin/parser/trino/query_type_test.go
index 4968621d9d..24270f8296 100644
--- a/backend/plugin/parser/trino/query_type_test.go
+++ b/backend/plugin/parser/trino/query_type_test.go
@@ -9,180 +9,154 @@ import (
"github.com/bytebase/bytebase/backend/plugin/parser/base"
)
-func TestGetStatementType(t *testing.T) {
+// TestQueryTypeClassification verifies the statement classification (the
+// query_type contract) and the read-only / data-changing / schema-changing
+// helpers against the omni-backed AST. It replaces the legacy
+// TestGetStatementType, which additionally asserted an ANTLR-specific
+// StatementType enum that no longer exists; the user-facing behaviour
+// (base.QueryType + the Is*Statement predicates) is preserved here.
+func TestQueryTypeClassification(t *testing.T) {
tests := []struct {
name string
sql string
- wantType StatementType
wantQueryType base.QueryType
wantIsAnalyze bool
}{
{
name: "SELECT statement",
sql: "SELECT * FROM users",
- wantType: Select,
wantQueryType: base.Select,
wantIsAnalyze: false,
},
{
name: "EXPLAIN statement",
sql: "EXPLAIN SELECT * FROM users",
- wantType: Explain,
wantQueryType: base.Explain,
wantIsAnalyze: false,
},
{
name: "EXPLAIN ANALYZE statement",
sql: "EXPLAIN ANALYZE SELECT * FROM users",
- wantType: Explain,
wantQueryType: base.Select,
wantIsAnalyze: true,
},
{
name: "INSERT statement",
sql: "INSERT INTO users (id, name) VALUES (1, 'John')",
- wantType: Insert,
wantQueryType: base.DML,
wantIsAnalyze: false,
},
{
name: "UPDATE statement",
sql: "UPDATE users SET name = 'John' WHERE id = 1",
- wantType: Update,
wantQueryType: base.DML,
wantIsAnalyze: false,
},
{
name: "DELETE statement",
sql: "DELETE FROM users WHERE id = 1",
- wantType: Delete,
wantQueryType: base.DML,
wantIsAnalyze: false,
},
- // Trino MERGE syntax is complex and varies by version - skipping this test for now
- // {
- // name: "MERGE statement",
- // sql: "MERGE INTO users u USING new_users n ON u.id = n.id WHEN MATCHED THEN UPDATE SET u.name = n.name",
- // wantType: Merge,
- // wantQueryType: base.DML,
- // wantIsAnalyze: false,
- // },
{
name: "CREATE TABLE statement",
sql: "CREATE TABLE users (id INT, name VARCHAR)",
- wantType: CreateTable,
wantQueryType: base.DDL,
wantIsAnalyze: false,
},
{
name: "CREATE TABLE AS SELECT statement",
sql: "CREATE TABLE new_users AS SELECT * FROM users",
- wantType: CreateTableAsSelect,
wantQueryType: base.DDL,
wantIsAnalyze: false,
},
{
name: "CREATE VIEW statement",
sql: "CREATE VIEW user_view AS SELECT id, name FROM users",
- wantType: CreateView,
wantQueryType: base.DDL,
wantIsAnalyze: false,
},
{
name: "ALTER TABLE ADD COLUMN",
sql: "ALTER TABLE users ADD COLUMN email VARCHAR",
- wantType: AlterTable,
wantQueryType: base.DDL,
wantIsAnalyze: false,
},
{
name: "ALTER TABLE DROP COLUMN",
sql: "ALTER TABLE users DROP COLUMN email",
- wantType: AlterTable,
wantQueryType: base.DDL,
wantIsAnalyze: false,
},
{
name: "ALTER TABLE RENAME COLUMN",
sql: "ALTER TABLE users RENAME COLUMN old_name TO new_name",
- wantType: AlterTable,
wantQueryType: base.DDL,
wantIsAnalyze: false,
},
{
name: "ALTER TABLE SET COLUMN TYPE",
sql: "ALTER TABLE users ALTER COLUMN name SET DATA TYPE VARCHAR(100)",
- wantType: AlterTable,
wantQueryType: base.DDL,
wantIsAnalyze: false,
},
{
name: "DROP TABLE statement",
sql: "DROP TABLE users",
- wantType: DropTable,
wantQueryType: base.DDL,
wantIsAnalyze: false,
},
{
name: "DROP VIEW statement",
sql: "DROP VIEW user_view",
- wantType: DropView,
wantQueryType: base.DDL,
wantIsAnalyze: false,
},
{
name: "CREATE SCHEMA statement",
sql: "CREATE SCHEMA new_schema",
- wantType: CreateSchema,
wantQueryType: base.DDL,
wantIsAnalyze: false,
},
{
name: "DROP SCHEMA statement",
sql: "DROP SCHEMA old_schema",
- wantType: DropSchema,
wantQueryType: base.DDL,
wantIsAnalyze: false,
},
{
name: "RENAME TABLE statement",
sql: "ALTER TABLE users RENAME TO new_users",
- wantType: RenameTable,
wantQueryType: base.DDL,
wantIsAnalyze: false,
},
{
name: "RENAME SCHEMA statement",
sql: "ALTER SCHEMA old_schema RENAME TO new_schema",
- wantType: CreateSchema, // Mapped to CreateSchema in the code
wantQueryType: base.DDL,
wantIsAnalyze: false,
},
{
name: "SHOW TABLES statement",
sql: "SHOW TABLES",
- wantType: Show,
wantQueryType: base.SelectInfoSchema,
wantIsAnalyze: false,
},
{
name: "SHOW SCHEMAS statement",
sql: "SHOW SCHEMAS",
- wantType: Show,
wantQueryType: base.SelectInfoSchema,
wantIsAnalyze: false,
},
{
name: "SHOW COLUMNS statement",
sql: "SHOW COLUMNS FROM users",
- wantType: Show,
wantQueryType: base.SelectInfoSchema,
wantIsAnalyze: false,
},
{
name: "SET SESSION statement",
sql: "SET SESSION optimize_hash_generation = true",
- wantType: Set,
wantQueryType: base.Select,
wantIsAnalyze: false,
},
@@ -190,39 +164,31 @@ func TestGetStatementType(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
- // Parse the statement
- results, err := ParseTrino(tt.sql)
+ parsed, err := parseTrinoSQL(tt.sql)
require.NoError(t, err, "Parsing should not fail")
- require.Len(t, results, 1, "Should parse exactly one statement")
+ require.Len(t, parsed, 1, "Should parse exactly one statement")
+ node := parsed[0].Node()
- result := results[0]
-
- // Test GetStatementType
- stmtType := GetStatementType(result.Tree)
- assert.Equal(t, tt.wantType, stmtType, "Statement type mismatch")
-
- // Test getQueryType
- queryType, isAnalyze := getQueryType(result.Tree)
+ queryType, isAnalyze := getQueryType(node)
assert.Equal(t, tt.wantQueryType, queryType, "Query type mismatch")
assert.Equal(t, tt.wantIsAnalyze, isAnalyze, "isAnalyze mismatch")
- // Test utility functions
if tt.wantQueryType == base.Select || tt.wantQueryType == base.Explain || tt.wantQueryType == base.SelectInfoSchema {
- assert.True(t, IsReadOnlyStatement(result.Tree), "Should be read-only")
+ assert.True(t, IsReadOnlyStatement(node), "Should be read-only")
} else {
- assert.False(t, IsReadOnlyStatement(result.Tree), "Should not be read-only")
+ assert.False(t, IsReadOnlyStatement(node), "Should not be read-only")
}
if tt.wantQueryType == base.DML {
- assert.True(t, IsDataChangingStatement(result.Tree), "Should be data-changing")
+ assert.True(t, IsDataChangingStatement(node), "Should be data-changing")
} else {
- assert.False(t, IsDataChangingStatement(result.Tree), "Should not be data-changing")
+ assert.False(t, IsDataChangingStatement(node), "Should not be data-changing")
}
if tt.wantQueryType == base.DDL {
- assert.True(t, IsSchemaChangingStatement(result.Tree), "Should be schema-changing")
+ assert.True(t, IsSchemaChangingStatement(node), "Should be schema-changing")
} else {
- assert.False(t, IsSchemaChangingStatement(result.Tree), "Should not be schema-changing")
+ assert.False(t, IsSchemaChangingStatement(node), "Should not be schema-changing")
}
})
}
diff --git a/backend/plugin/parser/trino/split.go b/backend/plugin/parser/trino/split.go
index 766f8ce675..4aabfd08b0 100644
--- a/backend/plugin/parser/trino/split.go
+++ b/backend/plugin/parser/trino/split.go
@@ -1,127 +1,49 @@
package trino
import (
- "github.com/antlr4-go/antlr/v4"
- trinoparser "github.com/bytebase/parser/trino"
+ "github.com/bytebase/omni/trino/parser"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
- "github.com/bytebase/bytebase/backend/plugin/parser/tokenizer"
)
func init() {
base.RegisterSplitterFunc(storepb.Engine_TRINO, SplitSQL)
}
-// SplitSQL splits the given SQL statement into multiple SQL statements.
-// Following TSQL's pattern, we try parser-based splitting first, then fall back to tokenizer.
+// SplitSQL splits the input into multiple SQL statements using the omni Trino
+// 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) {
- result, err := splitByParser(statement)
- if err != nil {
- // Fall back to tokenizer-based split
- return splitByTokenizer(statement)
+ segs := parser.Split(statement)
+ if len(segs) == 0 {
+ return nil, nil
}
- return result, nil
-}
-
-func splitByTokenizer(statement string) ([]base.Statement, error) {
- t := tokenizer.NewTokenizer(statement)
- return t.SplitStandardMultiSQL()
-}
-
-func splitByParser(statement string) ([]base.Statement, error) {
- input := antlr.NewInputStream(statement)
- lexer := trinoparser.NewTrinoLexer(input)
- stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
- parser := trinoparser.NewTrinoParser(stream)
-
- // Remove default error listener and add our own
- lexer.RemoveErrorListeners()
- lexerErrorListener := &base.ParseErrorListener{
- Statement: statement,
- }
- lexer.AddErrorListener(lexerErrorListener)
-
- parser.RemoveErrorListeners()
- parserErrorListener := &base.ParseErrorListener{
- Statement: statement,
- }
- parser.AddErrorListener(parserErrorListener)
-
- parser.BuildParseTrees = true
- tree := parser.Parse()
-
- if lexerErrorListener.Err != nil {
- return nil, lexerErrorListener.Err
- }
-
- if parserErrorListener.Err != nil {
- return nil, parserErrorListener.Err
- }
-
- var result []base.Statement
- tokens := stream.GetAllTokens()
- byteOffsetStart := 0
-
- // Walk through all statements
- for _, stmts := range tree.AllStatements() {
- if stmts == nil {
- continue
- }
-
- // Get SingleStatement from StatementsContext
- singleStmt := stmts.SingleStatement()
- if singleStmt == nil {
- continue
- }
-
- startToken := singleStmt.GetStart()
- stopToken := singleStmt.GetStop()
- if startToken == nil || stopToken == nil {
- continue
- }
-
- // Find the actual start position
- endIdx := stopToken.GetTokenIndex()
- // Check if there's a semicolon after the statement and include it
- finalEndIdx := endIdx
- if endIdx+1 < len(tokens) && tokens[endIdx+1].GetTokenType() == trinoparser.TrinoLexerSEMICOLON_ {
- finalEndIdx = endIdx + 1
+ 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++
}
-
- // Calculate proper end position (1-based exclusive per proto spec)
- endToken := tokens[finalEndIdx]
-
- // Calculate byte range: include leading whitespace from where previous statement ended
- rangeEnd := endToken.GetStop() + 1 // exclusive end
-
- // Include leading whitespace in text by getting from original statement
- // This ensures Start.Line - 1 == BaseLine for proper position mapping
- text := statement[byteOffsetStart:rangeEnd]
-
- // Calculate start position based on byteOffsetStart (including leading whitespace)
- startLine, startColumn := base.CalculateLineAndColumn(statement, byteOffsetStart)
-
- result = append(result, base.Statement{
- Text: text,
+ stmts = append(stmts, base.Statement{
+ Text: text,
+ Empty: seg.Empty(),
+ Start: mapper.Position(seg.ByteStart),
+ End: mapper.Position(end),
Range: &storepb.Range{
- Start: int32(byteOffsetStart),
- End: int32(rangeEnd),
- },
- Start: &storepb.Position{
- Line: int32(startLine + 1), // 1-based
- Column: int32(startColumn + 1), // 1-based
+ Start: int32(seg.ByteStart),
+ End: int32(end),
},
- End: &storepb.Position{
- Line: int32(endToken.GetLine()), // 1-based
- Column: int32(endToken.GetColumn() + len(endToken.GetText()) + 1), // 1-based exclusive
- },
- Empty: false,
})
-
- byteOffsetStart = rangeEnd
}
-
- return result, nil
+ return stmts, nil
}
diff --git a/backend/plugin/parser/trino/split_test.go b/backend/plugin/parser/trino/split_test.go
index 1718f23b34..86f7f23880 100644
--- a/backend/plugin/parser/trino/split_test.go
+++ b/backend/plugin/parser/trino/split_test.go
@@ -8,8 +8,6 @@ import (
func TestTrinoSplitSQL(t *testing.T) {
base.RunSplitTests(t, "test-data/test_split.yaml", base.SplitTestOptions{
- SplitFunc: SplitSQL,
- LexerSplitFunc: splitByTokenizer,
- ParserSplitFunc: splitByParser,
+ SplitFunc: SplitSQL,
})
}
diff --git a/backend/plugin/parser/trino/test-data/completion/test_completion.yaml b/backend/plugin/parser/trino/test-data/completion/test_completion.yaml
deleted file mode 100644
index 2d576cc969..0000000000
--- a/backend/plugin/parser/trino/test-data/completion/test_completion.yaml
+++ /dev/null
@@ -1,301 +0,0 @@
-- description: Multiple statements
- input: |-
- select count(1) from t1 where id; SELECT * FROM |
- select * from QRTZ_J0B_DETAILS qjd where J0B_NAME like '%gray';
- want:
- - text: Company
- type: DATABASE
- definition: ""
- comment: ""
- priority: 0
- - text: School
- type: DATABASE
- definition: ""
- comment: ""
- priority: 0
- - text: MySchema
- type: SCHEMA
- definition: ""
- comment: ""
- priority: 0
- - text: dbo
- type: SCHEMA
- definition: ""
- comment: ""
- priority: 0
- - text: Address
- type: TABLE
- definition: ""
- comment: ""
- priority: 0
- - text: Employees
- type: TABLE
- definition: ""
- comment: ""
- priority: 0
-- description: Column alias can be used in order by clause
- input: SELECT Id AS IdAlias, Name FROM Employees ORDER BY |;
- want:
- - text: EmployeeId
- type: COLUMN
- definition: Company.dbo.Address | int, NOT NULL
- comment: ""
- priority: 1
- - text: Id
- type: COLUMN
- definition: Company.MySchema.SalaryLevel | int, NOT NULL
- comment: ""
- priority: 1
- - text: Id
- type: COLUMN
- definition: Company.dbo.Employees | int, NOT NULL
- comment: ""
- priority: 0
- - text: IdAlias
- type: COLUMN
- definition: ""
- comment: ""
- priority: 0
- - text: Name
- type: COLUMN
- definition: Company.dbo.Employees | varchar, NOT NULL
- comment: ""
- priority: 0
- - text: SalaryUpBound
- type: COLUMN
- definition: Company.MySchema.SalaryLevel | int, NOT NULL
- comment: ""
- priority: 1
- - text: Street
- type: COLUMN
- definition: Company.dbo.Address | varchar, NOT NULL
- comment: ""
- priority: 1
- - text: Company
- type: DATABASE
- definition: ""
- comment: ""
- priority: 0
- - text: School
- type: DATABASE
- definition: ""
- comment: ""
- priority: 0
- - text: MySchema
- type: SCHEMA
- definition: ""
- comment: ""
- priority: 0
- - text: dbo
- type: SCHEMA
- definition: ""
- comment: ""
- priority: 0
- - text: Address
- type: TABLE
- definition: ""
- comment: ""
- priority: 0
- - text: Employees
- type: TABLE
- definition: ""
- comment: ""
- priority: 0
-- description: Table alias referenced by select item
- input: SELECT tableAlias.| FROM Employees AS tableAlias;
- want:
- - text: Id
- type: COLUMN
- definition: Company.dbo.Employees | int, NOT NULL
- comment: ""
- priority: 0
- - text: Name
- type: COLUMN
- definition: Company.dbo.Employees | varchar, NOT NULL
- comment: ""
- priority: 0
-- description: Full column name
- input: SELECT | FROM Employees;
- want:
- - text: EmployeeId
- type: COLUMN
- definition: Company.dbo.Address | int, NOT NULL
- comment: ""
- priority: 1
- - text: Id
- type: COLUMN
- definition: Company.MySchema.SalaryLevel | int, NOT NULL
- comment: ""
- priority: 1
- - text: Id
- type: COLUMN
- definition: Company.dbo.Employees | int, NOT NULL
- comment: ""
- priority: 0
- - text: Name
- type: COLUMN
- definition: Company.dbo.Employees | varchar, NOT NULL
- comment: ""
- priority: 0
- - text: SalaryUpBound
- type: COLUMN
- definition: Company.MySchema.SalaryLevel | int, NOT NULL
- comment: ""
- priority: 1
- - text: Street
- type: COLUMN
- definition: Company.dbo.Address | varchar, NOT NULL
- comment: ""
- priority: 1
- - text: Company
- type: DATABASE
- definition: ""
- comment: ""
- priority: 0
- - text: School
- type: DATABASE
- definition: ""
- comment: ""
- priority: 0
- - text: MySchema
- type: SCHEMA
- definition: ""
- comment: ""
- priority: 0
- - text: dbo
- type: SCHEMA
- definition: ""
- comment: ""
- priority: 0
- - text: Address
- type: TABLE
- definition: ""
- comment: ""
- priority: 0
- - text: Employees
- type: TABLE
- definition: ""
- comment: ""
- priority: 0
-- description: Asterisk appearred in select item list
- input: WITH MyCTE_01 AS (SELECT * FROM dbo.Employees) SELECT |.* FROM MyCTE_01 JOIN dbo.Address ON MyCTE_01.EmployeeID = dbo.Address.EmployeeID
- want:
- - text: EmployeeId
- type: COLUMN
- definition: Company.dbo.Address | int, NOT NULL
- comment: ""
- priority: 0
- - text: Id
- type: COLUMN
- definition: Company.MySchema.SalaryLevel | int, NOT NULL
- comment: ""
- priority: 1
- - text: Id
- type: COLUMN
- definition: Company.dbo.Employees | int, NOT NULL
- comment: ""
- priority: 0
- - text: Name
- type: COLUMN
- definition: Company.dbo.Employees | varchar, NOT NULL
- comment: ""
- priority: 0
- - text: SalaryUpBound
- type: COLUMN
- definition: Company.MySchema.SalaryLevel | int, NOT NULL
- comment: ""
- priority: 1
- - text: Street
- type: COLUMN
- definition: Company.dbo.Address | varchar, NOT NULL
- comment: ""
- priority: 0
- - text: Company
- type: DATABASE
- definition: ""
- comment: ""
- priority: 0
- - text: School
- type: DATABASE
- definition: ""
- comment: ""
- priority: 0
- - text: MySchema
- type: SCHEMA
- definition: ""
- comment: ""
- priority: 0
- - text: dbo
- type: SCHEMA
- definition: ""
- comment: ""
- priority: 0
- - text: Address
- type: TABLE
- definition: ""
- comment: ""
- priority: 0
- - text: Employees
- type: TABLE
- definition: ""
- comment: ""
- priority: 0
- - text: MyCTE_01
- type: TABLE
- definition: ""
- comment: ""
- priority: 0
-- description: Completion should not consider CTE if no schema specified
- input: WITH MyCTE_01 AS (SELECT * FROM dbo.Employees) SELECT * FROM MySchema.|
- want:
- - text: SalaryLevel
- type: TABLE
- definition: ""
- comment: ""
- priority: 0
-- description: Completion table in specify schema
- input: SELECT * FROM dbo.|
- want:
- - text: Address
- type: TABLE
- definition: ""
- comment: ""
- priority: 0
- - text: Employees
- type: TABLE
- definition: ""
- comment: ""
- priority: 0
-- description: Completion table for simple select
- input: SELECT * FROM |
- want:
- - text: Company
- type: DATABASE
- definition: ""
- comment: ""
- priority: 0
- - text: School
- type: DATABASE
- definition: ""
- comment: ""
- priority: 0
- - text: MySchema
- type: SCHEMA
- definition: ""
- comment: ""
- priority: 0
- - text: dbo
- type: SCHEMA
- definition: ""
- comment: ""
- priority: 0
- - text: Address
- type: TABLE
- definition: ""
- comment: ""
- priority: 0
- - text: Employees
- type: TABLE
- definition: ""
- comment: ""
- priority: 0
\ No newline at end of file
diff --git a/backend/plugin/parser/trino/trino.go b/backend/plugin/parser/trino/trino.go
index c91a3a6569..2985cb7e0c 100644
--- a/backend/plugin/parser/trino/trino.go
+++ b/backend/plugin/parser/trino/trino.go
@@ -1,157 +1,151 @@
-// Package trino provides SQL parser for Trino.
+// Package trino provides a SQL parser adapter for Trino, backed by the omni
+// hand-written Trino parser (github.com/bytebase/omni/trino/*).
package trino
import (
"strings"
- "unicode"
- "github.com/antlr4-go/antlr/v4"
- parser "github.com/bytebase/parser/trino"
- "github.com/pkg/errors"
+ "github.com/bytebase/omni/trino/ast"
+ "github.com/bytebase/omni/trino/parser"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
)
-// ParseTrino parses the given SQL statement and returns a list of ANTLRAST.
-// Each ANTLRAST represents one statement with its AST, tokens, and start position.
-func ParseTrino(sql string) ([]*base.ANTLRAST, error) {
- stmts, err := SplitSQL(sql)
+func init() {
+ base.RegisterParseStatementsFunc(storepb.Engine_TRINO, parseTrinoStatements)
+}
+
+// omniAST wraps an omni Trino AST node so it satisfies the base.AST interface.
+//
+// The concrete statement node types (e.g. *parser.QueryStmt, *parser.InsertStmt)
+// live in the omni trino/parser package and implement ast.Node; parser.Parse
+// returns them inside *ast.File.Stmts.
+type omniAST struct {
+ node ast.Node
+ startPos *storepb.Position
+}
+
+// ASTStartPosition implements base.AST.
+func (a *omniAST) ASTStartPosition() *storepb.Position {
+ return a.startPos
+}
+
+// Node returns the underlying omni AST node. It may be nil if the segment
+// produced no concrete statement (which should not happen for valid SQL).
+func (a *omniAST) Node() ast.Node {
+ return a.node
+}
+
+// parseTrinoStatements is the ParseStatementsFunc registered for Trino. It
+// splits the input into top-level statements and parses each one, returning a
+// []base.ParsedStatement carrying both the statement text/positions and the
+// parsed AST.
+//
+// Each segment is parsed on its own so that any ParseError byte offsets align
+// with the segment text; convertParseError then shifts them into the
+// coordinates of the original multi-statement script. The first parse error
+// encountered is returned, matching the prior behaviour of bailing out on the
+// first syntax error.
+func parseTrinoStatements(statement string) ([]base.ParsedStatement, error) {
+ stmts, err := SplitSQL(statement)
if err != nil {
- return nil, errors.Wrap(err, "failed to split SQL")
+ return nil, err
}
- var results []*base.ANTLRAST
+ result := make([]base.ParsedStatement, 0, len(stmts))
for _, stmt := range stmts {
- if stmt.Empty {
- continue
- }
-
- parseResult, err := parseSingleTrino(stmt.Text, stmt.BaseLine())
+ parsed, err := parseSegment(stmt)
if err != nil {
return nil, err
}
- results = append(results, parseResult)
+ ps := base.ParsedStatement{Statement: stmt}
+ if parsed != nil {
+ ps.AST = parsed
+ }
+ result = append(result, ps)
}
- return results, nil
+ return result, nil
}
-// parseSingleTrino parses a single Trino statement and returns the ANTLRAST.
-func parseSingleTrino(sql string, baseLine int) (*base.ANTLRAST, error) {
- // Add a semicolon if it's missing to allow users to omit the semicolon
- trimmedSQL := strings.TrimRightFunc(sql, unicode.IsSpace)
- if len(trimmedSQL) > 0 && !strings.HasSuffix(trimmedSQL, ";") {
- sql += ";"
- }
-
- // Create lexer and parser
- lexer := parser.NewTrinoLexer(antlr.NewInputStream(sql))
- stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
- p := parser.NewTrinoParser(stream)
-
- // Set up error listeners
- startPosition := &storepb.Position{Line: int32(baseLine) + 1}
- lexerErrorListener := &base.ParseErrorListener{
- Statement: sql,
- StartPosition: startPosition,
+// parseTrinoSQL parses the given SQL using the omni Trino parser and returns one
+// *omniAST per non-empty segment (empty / comment-only segments are skipped).
+//
+// On the first parse error it returns a *base.SyntaxError with the position
+// translated into the coordinates of the original input. This mirrors the
+// historical ParseTrino signature shape used by other files in this package.
+func parseTrinoSQL(statement string) ([]*omniAST, error) {
+ stmts, err := SplitSQL(statement)
+ if err != nil {
+ return nil, err
}
- lexer.RemoveErrorListeners()
- lexer.AddErrorListener(lexerErrorListener)
- parserErrorListener := &base.ParseErrorListener{
- Statement: sql,
- StartPosition: startPosition,
+ var result []*omniAST
+ for _, stmt := range stmts {
+ parsed, err := parseSegment(stmt)
+ if err != nil {
+ return nil, err
+ }
+ if parsed != nil {
+ result = append(result, parsed)
+ }
}
- p.RemoveErrorListeners()
- p.AddErrorListener(parserErrorListener)
-
- p.BuildParseTrees = true
-
- // Parse the statement
- tree := p.SingleStatement()
+ return result, nil
+}
- // Check for errors
- if lexerErrorListener.Err != nil {
- return nil, lexerErrorListener.Err
+// parseSegment parses a single already-split statement segment into an
+// *omniAST. A blank or comment-only segment yields (nil, nil) so callers can
+// decide whether to keep a placeholder (parseTrinoStatements) or skip it
+// (parseTrinoSQL). The first omni parse error is translated into the
+// coordinates of the original input via convertParseError.
+func parseSegment(stmt base.Statement) (*omniAST, error) {
+ if stmt.Empty || strings.TrimSpace(stmt.Text) == "" {
+ return nil, nil
}
-
- if parserErrorListener.Err != nil {
- return nil, parserErrorListener.Err
+ file, errs := parser.Parse(stmt.Text)
+ if len(errs) > 0 {
+ return nil, convertParseError(stmt.Text, &errs[0], stmt.Start)
}
-
- // Return the ANTLR AST
- return &base.ANTLRAST{
- StartPosition: startPosition,
- Tree: tree,
- Tokens: stream,
- }, nil
-}
-
-// NormalizeTrinoIdentifier normalizes the identifier for Trino.
-func NormalizeTrinoIdentifier(ident string) string {
- // Trino identifiers are case-insensitive unless quoted
- if strings.HasPrefix(ident, "\"") && strings.HasSuffix(ident, "\"") {
- // Remove quotes for quoted identifiers
- return ident[1 : len(ident)-1]
+ var node ast.Node
+ if file != nil && len(file.Stmts) > 0 {
+ node = file.Stmts[0]
}
- return strings.ToLower(ident)
+ return &omniAST{node: node, startPos: stmt.Start}, nil
}
-// ExtractQualifiedNameParts extracts the parts of a qualified name.
-func ExtractQualifiedNameParts(ctx parser.IQualifiedNameContext) []string {
- if ctx == nil {
- return nil
- }
-
- var parts []string
- for _, ident := range ctx.AllIdentifier() {
- if ident != nil {
- parts = append(parts, NormalizeTrinoIdentifier(ident.GetText()))
+// convertParseError converts an omni *parser.ParseError to a *base.SyntaxError
+// that sql_service.go recognises for structured error diagnostics. It converts
+// the byte offset in ParseError.Loc to 1-based line and 1-based column
+// (rune-based), matching the storepb.Position convention used across other omni
+// parser adapters.
+//
+// If basePos is non-nil, the computed position is offset by it so that errors
+// from parsing an isolated statement segment are reported in the coordinates of
+// the original multi-statement script.
+func convertParseError(statement string, pe *parser.ParseError, basePos *storepb.Position) *base.SyntaxError {
+ pos := base.NewByteOffsetPositionMapper(statement).Position(pe.Loc.Start)
+ line, col := int(pos.Line), int(pos.Column)
+ if basePos != nil {
+ if line == 1 {
+ col = int(basePos.Column) + col - 1
}
+ line = int(basePos.Line) + line - 1
}
-
- return parts
-}
-
-// ExtractDatabaseSchemaName extracts catalog/database, schema, and table/name parts from qualified name.
-// For data access control features, we need consistent catalog.schema.table.column format
-func ExtractDatabaseSchemaName(ctx parser.IQualifiedNameContext, defaultDatabase, defaultSchema string) (string, string, string) {
- parts := ExtractQualifiedNameParts(ctx)
-
- // Set default schema if it's empty - critical for data access control test format
- if defaultSchema == "" {
- defaultSchema = "public" // Default schema for data access control tests
+ return &base.SyntaxError{
+ Position: &storepb.Position{
+ Line: int32(line),
+ Column: int32(col),
+ },
+ Message: pe.Error(),
+ RawMessage: pe.Msg,
}
+}
- switch len(parts) {
- case 1:
- // Just name (table/column)
- return defaultDatabase, defaultSchema, parts[0]
- case 2:
- // schema.name
- return defaultDatabase, parts[0], parts[1]
- case 3:
- // catalog.schema.name (Trino's model)
- // In Trino, the full 3-part name is catalog.schema.table
- return parts[0], parts[1], parts[2]
- default:
- // Handle more complex cases (rare but possible in Trino)
- if len(parts) > 3 {
- // For very long identifier chains, treat the first part as catalog,
- // second as schema, and join the rest as the object name
- name := strings.Join(parts[2:], "_")
- return parts[0], parts[1], name
- }
-
- if len(parts) == 0 {
- // Empty qualified name, use defaults
- return defaultDatabase, defaultSchema, ""
- }
-
- // Invalid case - should never reach here given Trino's grammar
- // Fallback to taking the last element as the name
- name := parts[len(parts)-1]
- return defaultDatabase, defaultSchema, name
- }
+// NormalizeTrinoIdentifier normalizes a Trino identifier. Trino folds unquoted
+// identifiers to lower case and preserves the case of double-quoted identifiers
+// (stripping the surrounding quotes). This delegates to the omni normalizer so
+// the bytebase adapter and the omni parser agree on identifier folding.
+func NormalizeTrinoIdentifier(ident string) string {
+ return parser.NormalizeTrinoIdentifier(ident)
}
diff --git a/backend/plugin/schema/trino/get_database_definition.go b/backend/plugin/schema/trino/get_database_definition.go
index 6992e23fb9..cead777489 100644
--- a/backend/plugin/schema/trino/get_database_definition.go
+++ b/backend/plugin/schema/trino/get_database_definition.go
@@ -1,8 +1,10 @@
+// Package trino implements the bytebase schema.GetDatabaseDefinition contract
+// for the Trino engine, backed by the omni Trino deparser
+// (github.com/bytebase/omni/trino/deparse).
package trino
import (
- "fmt"
- "strings"
+ "github.com/bytebase/omni/trino/deparse"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/schema"
@@ -12,53 +14,54 @@ func init() {
schema.RegisterGetDatabaseDefinition(storepb.Engine_TRINO, GetDatabaseDefinition)
}
-// GetDatabaseDefinition generates the SQL definition for a Trino database schema
+// GetDatabaseDefinition renders the CREATE TABLE SDL for a Trino database
+// schema. It converts the storepb metadata into the omni deparser's metadata
+// model and delegates to deparse.GetDatabaseDefinition, which emits one CREATE
+// TABLE statement per table in schema-then-table snapshot order.
func GetDatabaseDefinition(_ schema.GetDefinitionContext, metadata *storepb.DatabaseSchemaMetadata) (string, error) {
- var buf strings.Builder
-
- // Process each schema
- for _, schema := range metadata.Schemas {
- // Process tables in the schema
- for _, table := range schema.Tables {
- if err := writeCreateTable(&buf, schema.Name, table); err != nil {
- return "", err
- }
- buf.WriteString("\n\n")
- }
- }
-
- return buf.String(), nil
+ return deparse.GetDatabaseDefinition(convertDatabaseMetadata(metadata))
}
-// writeCreateTable generates CREATE TABLE statement
-func writeCreateTable(buf *strings.Builder, schema string, table *storepb.TableMetadata) error {
- // Begin CREATE TABLE statement
- createTable := fmt.Sprintf("CREATE TABLE IF NOT EXISTS \"%s\".\"%s\" (\n", schema, table.Name)
-
- if _, err := buf.WriteString(createTable); err != nil {
- return err
+// convertDatabaseMetadata maps storepb.DatabaseSchemaMetadata onto the omni
+// deparse metadata model one-to-one, copying only the fields the Trino SDL dump
+// consumes (schemas, tables, and column Name/Type/Nullable).
+func convertDatabaseMetadata(metadata *storepb.DatabaseSchemaMetadata) *deparse.DatabaseSchemaMetadata {
+ if metadata == nil {
+ return nil
}
-
- // Add column definitions
- var columnDefs []string
- for _, column := range table.Columns {
- nullable := ""
- if !column.Nullable {
- nullable = " NOT NULL"
- }
- columnDefs = append(columnDefs, fmt.Sprintf(" \"%s\" %s%s", column.Name, column.Type, nullable))
+ out := &deparse.DatabaseSchemaMetadata{
+ Name: metadata.Name,
+ Schemas: make([]*deparse.SchemaMetadata, 0, len(metadata.Schemas)),
}
-
- if len(columnDefs) > 0 {
- if _, err := buf.WriteString(strings.Join(columnDefs, ",\n")); err != nil {
- return err
+ for _, s := range metadata.Schemas {
+ if s == nil {
+ continue
}
+ schemaMeta := &deparse.SchemaMetadata{
+ Name: s.Name,
+ Tables: make([]*deparse.TableMetadata, 0, len(s.Tables)),
+ }
+ for _, t := range s.Tables {
+ if t == nil {
+ continue
+ }
+ tableMeta := &deparse.TableMetadata{
+ Name: t.Name,
+ Columns: make([]*deparse.ColumnMetadata, 0, len(t.Columns)),
+ }
+ for _, c := range t.Columns {
+ if c == nil {
+ continue
+ }
+ tableMeta.Columns = append(tableMeta.Columns, &deparse.ColumnMetadata{
+ Name: c.Name,
+ Type: c.Type,
+ Nullable: c.Nullable,
+ })
+ }
+ schemaMeta.Tables = append(schemaMeta.Tables, tableMeta)
+ }
+ out.Schemas = append(out.Schemas, schemaMeta)
}
-
- // Close the CREATE TABLE statement
- if _, err := buf.WriteString("\n);"); err != nil {
- return err
- }
-
- return nil
+ return out
}
diff --git a/backend/plugin/schema/trino/get_database_definition_test.go b/backend/plugin/schema/trino/get_database_definition_test.go
index 383d8ae371..d903e6e1d5 100644
--- a/backend/plugin/schema/trino/get_database_definition_test.go
+++ b/backend/plugin/schema/trino/get_database_definition_test.go
@@ -1,7 +1,6 @@
package trino
import (
- "strings"
"testing"
"github.com/stretchr/testify/assert"
@@ -100,6 +99,11 @@ func TestGetDatabaseDefinition(t *testing.T) {
}
}
+// TestWriteCreateTable exercises the single-table CREATE TABLE rendering through
+// the public GetDatabaseDefinition entry point (the omni deparser owns the
+// per-table writer internally now, so it is no longer a directly callable
+// helper). The emitted statement is followed by the standard inter-statement
+// "\n\n" separator the deparser appends.
func TestWriteCreateTable(t *testing.T) {
tests := []struct {
name string
@@ -124,16 +128,26 @@ func TestWriteCreateTable(t *testing.T) {
},
expected: `CREATE TABLE IF NOT EXISTS "schema"."table" (
"col1" integer NOT NULL
-);`,
+);
+
+`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
- var buf strings.Builder
- err := writeCreateTable(&buf, tt.schema, tt.table)
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Name: tt.catalog,
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: tt.schema,
+ Tables: []*storepb.TableMetadata{tt.table},
+ },
+ },
+ }
+ result, err := GetDatabaseDefinition(schema.GetDefinitionContext{}, metadata)
assert.NoError(t, err)
- assert.Equal(t, tt.expected, buf.String())
+ assert.Equal(t, tt.expected, result)
})
}
}