Skip to content

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

chore: tidy up trino ac (#16290)

Vincent Huangbc4226d90a45
diff --git a/backend/plugin/parser/trino/completion.go b/backend/plugin/parser/trino/completion.go
index 794b504cba..b5cf0b3dcd 100644
--- a/backend/plugin/parser/trino/completion.go
+++ b/backend/plugin/parser/trino/completion.go
@@ -25,69 +25,69 @@ var (
globalFellowSetsByState = base.NewFollowSetsByState()
ignoredTokens = map[int]bool{
- // // Common EOF
- // trinoparser.TrinoParserEOF: true,
+ // 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,
+ // 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,
+ // 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, // full_column_name
- trinoparser.TrinoParserRULE_qualifiedName: true, // full_table_name
+ trinoparser.TrinoParserRULE_identifier: true,
+ trinoparser.TrinoParserRULE_qualifiedName: true,
}
)
@@ -111,16 +111,6 @@ func (m CompletionMap) toSlice() []base.Candidate {
return result
}
-// // insertFunctions inserts the built-in functions into the completion map.
-// func (m CompletionMap) insertBuiltinFunctions() {
-// for key := range trinoBuiltinFunctionsMap {
-// m[key] = base.Candidate{
-// Type: base.CandidateTypeFunction,
-// Text: key + "()",
-// }
-// }
-// }
-
func (m CompletionMap) insertMetadataCatalogs(c *Completer) {
if c.defaultCatalog != "" {
m[c.defaultCatalog] = base.Candidate{
@@ -471,6 +461,7 @@ type Completer struct {
}
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()
@@ -482,6 +473,7 @@ func Completion(ctx context.Context, cCtx base.CompletionContext, statement stri
return result, nil
}
+ // If no results, try tricky completer with skipHeadingSQLWithoutSemicolon
trickyCompleter := NewTrickyCompleter(ctx, cCtx, statement, caretLine, caretOffset)
trickyCompleter.fetchCommonTableExpression(statement)
return trickyCompleter.complete()
@@ -497,23 +489,31 @@ func NewStandardCompleter(ctx context.Context, cCtx base.CompletionContext, stat
trinoparser.TrinoParserRULE_queryNoWith,
trinoparser.TrinoParserRULE_query,
trinoparser.TrinoParserRULE_as_column_alias,
- trinoparser.TrinoParserRULE_with,
+ -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: nil,
- cteCache: nil,
+ 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,
}
}
@@ -527,23 +527,31 @@ func NewTrickyCompleter(ctx context.Context, cCtx base.CompletionContext, statem
trinoparser.TrinoParserRULE_queryNoWith,
trinoparser.TrinoParserRULE_query,
trinoparser.TrinoParserRULE_as_column_alias,
- trinoparser.TrinoParserRULE_with,
+ -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: nil,
- cteCache: nil,
+ 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,
}
}
@@ -563,7 +571,7 @@ func prepareParserAndScanner(statement string, caretLine int, caretOffset int) (
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)
+ statement, caretLine, caretOffset = skipHeadingSQLWithoutSemicolon(statement, caretLine, caretOffset)
input := antlr.NewInputStream(statement)
lexer := trinoparser.NewTrinoLexer(input)
stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
@@ -576,38 +584,6 @@ func prepareTrickyParserAndScanner(statement string, caretLine int, caretOffset
return parser, lexer, scanner
}
-// // caretLine is 1-based and caretOffset is 0-based.
-// 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 {
-// if token.GetLine() > caretLine || (token.GetLine() == caretLine && token.GetColumn() >= caretOffset) {
-// break
-// }
-// if token.GetTokenType() == trinoparser.TrinoParserSELECT_ && token.GetColumn() == 0 {
-// latestSelect = token.GetTokenIndex()
-// newCaretLine = caretLine - token.GetLine() + 1 // convert to 1-based.
-// newCaretOffset = caretOffset
-// }
-// }
-
-// if latestSelect == 0 {
-// return statement, caretLine, caretOffset
-// }
-// return stream.GetTextFromInterval(antlr.NewInterval(latestSelect, stream.Size())), newCaretLine, newCaretOffset
-// }
-
func (c *Completer) complete() ([]base.Candidate, error) {
if c.scanner.IsTokenType(trinoparser.TrinoParserQUOTED_IDENTIFIER_) {
c.caretTokenIsQuoted = quotedTypeQuote
@@ -615,13 +591,10 @@ func (c *Completer) complete() ([]base.Candidate, error) {
caretIndex := c.scanner.GetIndex()
- // Initialize noSeparatorRequired to avoid nil map issues
- if c.noSeparatorRequired == nil {
- c.noSeparatorRequired = make(map[int]bool)
- }
-
+ // 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)] {
- caretIndex--
+ adjustedCaretIndex--
}
c.referencesStack = append([][]base.TableReference{{}}, c.referencesStack...)
@@ -634,11 +607,26 @@ func (c *Completer) complete() ([]base.Candidate, error) {
context = c.parser.Parse()
}
- candidates := c.core.CollectCandidates(caretIndex, context)
+ // 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
+ }
+
+ candidates := c.core.CollectCandidates(finalCaretIndex, context)
for ruleName := range candidates.Rules {
if ruleName == trinoparser.TrinoParserRULE_identifier {
- c.collectLeadingTableReferences(caretIndex)
+ c.collectLeadingTableReferences(adjustedCaretIndex)
c.takeReferencesSnapshot()
c.collectRemainingTableReferences()
c.takeReferencesSnapshot()
@@ -665,11 +653,16 @@ func (c *Completer) convertCandidates(candidates *base.CandidatesCollection) ([]
}
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{
@@ -683,12 +676,8 @@ func (c *Completer) convertCandidates(candidates *base.CandidatesCollection) ([]
c.scanner.Push()
switch ruleCandidate {
- // Handle built-in functions when appropriate
- // case trinoparser.TrinoParserRULE_qualifiedName:
- // functionEntries.insertBuiltinFunctions()
-
case trinoparser.TrinoParserRULE_qualifiedName:
- // identifier also appears in the qualifiedName rule, we would handle it in the identifier rule in this case.
+ // identifier also appears in the qualifiedName rule, handle it separately
if len(ruleStack) > 0 && ruleStack[len(ruleStack)-1].ID == trinoparser.TrinoParserRULE_identifier {
continue
}
@@ -852,6 +841,9 @@ func (c *Completer) convertCandidates(candidates *base.CandidatesCollection) ([]
result = append(result, tableEntries.toSlice()...)
result = append(result, columnEntries.toSlice()...)
result = append(result, viewEntries.toSlice()...)
+ if result == nil {
+ result = []base.Candidate{}
+ }
return result, nil
}
@@ -970,15 +962,11 @@ func (c *Completer) determineColumnReference() []*objectRefContext {
temp = normalizeIdentifierText(c.scanner.GetTokenText())
c.scanner.Forward(true /* skipHidden */)
if !c.scanner.IsTokenType(trinoparser.TrinoParserDOT_) || tokenIndex <= c.scanner.GetIndex() {
- // We've reached the end of the identifier chain or we're past where the caret was
- candidates = append(candidates, temp)
return deriveObjectRefContextsFromCandidates(candidates, true /* includeColumn */)
}
candidates = append(candidates, temp)
}
c.scanner.Forward(true /* skipHidden */)
- // In Trino, a fully qualified identifier can have at most 3 parts:
- // catalog.schema.object
if count > 3 {
break
}
@@ -1032,8 +1020,6 @@ func (c *Completer) determineQualifiedNameContext() []*objectRefContext {
// Skip the dot and move to the next token
c.scanner.Forward(true /* skipHidden */)
-
- // In Trino, qualified names have at most 3 parts: catalog.schema.object
if count > 3 {
break
}
@@ -1153,7 +1139,7 @@ func skipHeadingSQLs(statement string, caretLine int, caretOffset int) (string,
var buf strings.Builder
for i := start; i < len(list); i++ {
if _, err := buf.WriteString(list[i].Text); err != nil {
- return statement, caretLine, caretOffset
+ return statement, caretLine + 1, caretOffset // Convert back to 1-based on error
}
}
@@ -1670,3 +1656,51 @@ func normalizeQualifiedNameFallback(ctx *trinoparser.QualifiedNameContext, _ str
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
+ }
+
+ 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
+ }
+ }
+ }
+
+ 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
+}
diff --git a/backend/plugin/parser/trino/completion_test.go b/backend/plugin/parser/trino/completion_test.go
index dd49069673..463ca8282c 100644
--- a/backend/plugin/parser/trino/completion_test.go
+++ b/backend/plugin/parser/trino/completion_test.go
@@ -37,10 +37,10 @@ func TestCompletion(t *testing.T) {
tests := []candidatesTest{}
const (
- record = true
+ record = false
)
var (
- filepath = "test-data/completion/t_cc.yaml"
+ filepath = "test-data/completion/test_completion.yaml"
)
a := require.New(t)
@@ -52,8 +52,8 @@ func TestCompletion(t *testing.T) {
a.NoError(err)
a.NoError(yaml.Unmarshal(byteValue, &tests))
- for i, t := range tests {
- statement, caretLine, caretPosition := getCaretPosition(t.Input)
+ for i, test := range tests {
+ statement, caretLine, caretPosition := getCaretPosition(test.Input)
getter, lister := buildMockDatabaseMetadataGetterLister()
results, err := Completion(context.Background(), base.CompletionContext{
Scene: base.SceneTypeAll,
@@ -61,7 +61,7 @@ func TestCompletion(t *testing.T) {
Metadata: getter,
ListDatabaseNames: lister,
}, statement, caretLine, caretPosition)
- a.NoErrorf(err, "Case %02d: %s", i, t.Description)
+ a.NoErrorf(err, "Case %02d: %s", i, test.Description)
var filteredResult []base.Candidate
for _, r := range results {
switch r.Type {
@@ -71,6 +71,9 @@ func TestCompletion(t *testing.T) {
filteredResult = append(filteredResult, r)
}
}
+ if filteredResult == nil {
+ filteredResult = []base.Candidate{}
+ }
sort.Slice(filteredResult, func(i, j int) bool {
if filteredResult[i].Type != filteredResult[j].Type {
return filteredResult[i].Type < filteredResult[j].Type
@@ -84,7 +87,7 @@ func TestCompletion(t *testing.T) {
if record {
tests[i].Want = filteredResult
} else {
- a.Equalf(t.Want, filteredResult, t.Input, "Case %02d: %s", i, t.Description)
+ a.Equalf(test.Want, filteredResult, "Case %02d: %s\nInput: %s", i, test.Description, test.Input)
}
}
diff --git a/backend/plugin/parser/trino/split.go b/backend/plugin/parser/trino/split.go
index 9464fc70e2..669afffec7 100644
--- a/backend/plugin/parser/trino/split.go
+++ b/backend/plugin/parser/trino/split.go
@@ -3,7 +3,11 @@ package trino
import (
"strings"
+ "github.com/antlr4-go/antlr/v4"
+ trinoparser "github.com/bytebase/trino-parser"
+
"github.com/bytebase/bytebase/backend/plugin/parser/base"
+ "github.com/bytebase/bytebase/backend/plugin/parser/tokenizer"
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
@@ -11,88 +15,187 @@ func init() {
base.RegisterSplitterFunc(storepb.Engine_TRINO, SplitSQL)
}
-// SplitSQL splits the SQL statement into a list of single SQL statements.
+// 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.
func SplitSQL(statement string) ([]base.SingleSQL, error) {
- // Trino uses semicolon (;) as statement delimiter.
- statement = strings.TrimSpace(statement)
- if statement == "" {
- return []base.SingleSQL{}, nil
+ result, err := splitByParser(statement)
+ if err != nil {
+ // Fall back to tokenizer-based split
+ return splitByTokenizer(statement)
+ }
+ return result, nil
+}
+
+func splitByTokenizer(statement string) ([]base.SingleSQL, error) {
+ t := tokenizer.NewTokenizer(statement)
+ list, err := t.SplitStandardMultiSQL()
+ if err != nil {
+ return nil, err
}
- // Tokenize the statement to handle quoted strings and comments properly
- var result []base.SingleSQL
- var currentStmt strings.Builder
- var inSingleQuote, inDoubleQuote, inBlockComment, inLineComment bool
- var lastChar rune
-
- for i, char := range statement {
- // Handle line comments
- if char == '\n' && inLineComment {
- inLineComment = false
+ // The tokenizer doesn't provide accurate position information.
+ // We need to manually calculate positions based on the text.
+ lines := strings.Split(statement, "\n")
+ lineStarts := make([]int, len(lines))
+ pos := 0
+ for i, line := range lines {
+ lineStarts[i] = pos
+ pos += len(line) + 1 // +1 for newline
+ }
+
+ currentPos := 0
+ for i := range list {
+ if list[i].Empty {
+ continue
}
- // Only add character to current statement if not in a line comment
- if !inLineComment {
- currentStmt.WriteRune(char)
+ // Find the start position
+ startIdx := strings.Index(statement[currentPos:], list[i].Text)
+ if startIdx == -1 {
+ continue
}
+ startIdx += currentPos
- // Handle string literals (only if we're not in a comment)
- if !inBlockComment && !inLineComment {
- if char == '\'' && lastChar != '\\' && !inDoubleQuote {
- inSingleQuote = !inSingleQuote
- } else if char == '"' && lastChar != '\\' && !inSingleQuote {
- inDoubleQuote = !inDoubleQuote
+ // Calculate line and column for start
+ startLine, startCol := 0, 0
+ for j, lineStart := range lineStarts {
+ if startIdx < lineStart {
+ break
}
+ startLine = j
+ startCol = startIdx - lineStart
}
- // Handle comments (only if we're not in a string literal)
- if !inSingleQuote && !inDoubleQuote {
- // Line comments
- if char == '-' && lastChar == '-' && !inBlockComment && !inLineComment {
- inLineComment = true
+ // Calculate end position
+ endIdx := startIdx + len(list[i].Text)
+ endLine, endCol := 0, 0
+ for j, lineStart := range lineStarts {
+ if endIdx <= lineStart {
+ break
}
- // Block comments
- if char == '*' && lastChar == '/' && !inLineComment {
- inBlockComment = true
- } else if char == '/' && lastChar == '*' && inBlockComment {
- inBlockComment = false
+ endLine = j
+ endCol = endIdx - lineStart
+ }
+ // If we're exactly at the start of a new line, we're at the end of the previous line
+ if endCol == 0 && endLine > 0 {
+ endLine--
+ if endLine < len(lines) {
+ endCol = len(lines[endLine])
}
}
- // Split at semicolons outside of quotes and comments
- if char == ';' && !inSingleQuote && !inDoubleQuote && !inBlockComment && !inLineComment {
- sql := currentStmt.String()
- result = append(result, base.SingleSQL{
- Text: sql,
- BaseLine: countLines(statement[:i+1-len(sql)]),
- End: &storepb.Position{
- Line: int32(countLines(statement[:i+1])),
- },
- })
- currentStmt.Reset()
+ list[i].Start = &storepb.Position{
+ Line: int32(startLine),
+ Column: int32(startCol),
}
+ list[i].End = &storepb.Position{
+ Line: int32(endLine),
+ Column: int32(endCol),
+ }
+ list[i].BaseLine = startLine
- lastChar = char
+ currentPos = endIdx
}
- // Add the last statement if it's not empty
- if currentStmt.Len() > 0 {
- sql := currentStmt.String()
- if strings.TrimSpace(sql) != "" {
- result = append(result, base.SingleSQL{
- Text: sql,
- BaseLine: countLines(statement[:len(statement)-len(sql)]),
- End: &storepb.Position{
- Line: int32(countLines(statement)),
- },
- })
+ return list, nil
+}
+
+func splitByParser(statement string) ([]base.SingleSQL, 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.SingleSQL
+ tokens := stream.GetAllTokens()
+
+ // 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 (first non-hidden token)
+ startIdx := startToken.GetTokenIndex()
+ endIdx := stopToken.GetTokenIndex()
+
+ // Find the first non-hidden token for accurate start position
+ var firstDefaultToken antlr.Token
+ for i := startIdx; i <= endIdx && i < len(tokens); i++ {
+ if tokens[i].GetChannel() == antlr.TokenDefaultChannel {
+ firstDefaultToken = tokens[i]
+ break
+ }
+ }
+
+ if firstDefaultToken == nil {
+ firstDefaultToken = startToken
+ }
+
+ // 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
}
+
+ // Get the text including any trailing semicolon
+ text := stream.GetTextFromInterval(antlr.NewInterval(startIdx, finalEndIdx))
+
+ // Calculate proper end position
+ endToken := tokens[finalEndIdx]
+ endLine := endToken.GetLine() - 1
+ endColumn := endToken.GetColumn() + len(endToken.GetText())
+
+ result = append(result, base.SingleSQL{
+ Text: text,
+ BaseLine: firstDefaultToken.GetLine() - 1,
+ Start: &storepb.Position{
+ Line: int32(firstDefaultToken.GetLine() - 1),
+ Column: int32(firstDefaultToken.GetColumn()),
+ },
+ End: &storepb.Position{
+ Line: int32(endLine),
+ Column: int32(endColumn),
+ },
+ Empty: false,
+ })
}
return result, nil
}
-
-// countLines counts the number of newlines in a string
-func countLines(s string) int {
- return strings.Count(s, "\n")
-}
diff --git a/backend/plugin/parser/trino/split_test.go b/backend/plugin/parser/trino/split_test.go
index f7a7a01816..9234bf69b1 100644
--- a/backend/plugin/parser/trino/split_test.go
+++ b/backend/plugin/parser/trino/split_test.go
@@ -33,82 +33,78 @@ func TestTrinoSplitMultiSQL(t *testing.T) {
want: resData{
res: []base.SingleSQL{
{
- Text: "SELECT * FROM users;",
- End: &storepb.Position{Line: 0, Column: 19},
+ Text: "SELECT * FROM users;",
+ Start: &storepb.Position{Line: 0, Column: 0},
+ End: &storepb.Position{Line: 0, Column: 20},
},
{
- Text: " SELECT * FROM orders;",
+ Text: "SELECT * FROM orders;",
BaseLine: 0,
Start: &storepb.Position{Line: 0, Column: 21},
- End: &storepb.Position{Line: 0, Column: 41},
+ End: &storepb.Position{Line: 0, Column: 42},
},
},
},
},
{
statement: `
- -- This is a comment
- SELECT
- id,
- name
- FROM users
- WHERE status = 'active';
-
- /* This is a multi-line
- comment */
- SELECT * FROM orders;`,
+ -- This is a comment
+ SELECT
+ id,
+ name
+ FROM users
+ WHERE status = 'active';
+
+ /* This is a multi-line
+ comment */
+ SELECT * FROM orders;`,
want: resData{
res: []base.SingleSQL{
{
- Text: `--
- SELECT
- id,
- name
- FROM users
- WHERE status = 'active';`,
- Start: &storepb.Position{Line: 2, Column: 3},
- End: &storepb.Position{Line: 6, Column: 26},
+ Text: `SELECT
+ id,
+ name
+ FROM users
+ WHERE status = 'active';`,
+ BaseLine: 2,
+ Start: &storepb.Position{Line: 2, Column: 4},
+ End: &storepb.Position{Line: 6, Column: 28},
},
{
- Text: `
-
- /* This is a multi-line
- comment */
- SELECT * FROM orders;`,
- BaseLine: 6,
- Start: &storepb.Position{Line: 10, Column: 3},
- End: &storepb.Position{Line: 10, Column: 23},
+ Text: `SELECT * FROM orders;`,
+ BaseLine: 10,
+ Start: &storepb.Position{Line: 10, Column: 4},
+ End: &storepb.Position{Line: 10, Column: 25},
},
},
},
},
{
statement: `WITH orders_cte AS (
- SELECT * FROM orders
- )
- SELECT u.id, u.name, o.order_id
- FROM users u
- JOIN orders_cte o ON u.id = o.user_id;
-
- SELECT * FROM products;`,
+ SELECT * FROM orders
+ )
+ SELECT u.id, u.name, o.order_id
+ FROM users u
+ JOIN orders_cte o ON u.id = o.user_id;
+
+ SELECT * FROM products;`,
want: resData{
res: []base.SingleSQL{
{
Text: `WITH orders_cte AS (
- SELECT * FROM orders
- )
- SELECT u.id, u.name, o.order_id
- FROM users u
- JOIN orders_cte o ON u.id = o.user_id;`,
- End: &storepb.Position{Line: 5, Column: 41},
+ SELECT * FROM orders
+ )
+ SELECT u.id, u.name, o.order_id
+ FROM users u
+ JOIN orders_cte o ON u.id = o.user_id;`,
+ Start: &storepb.Position{Line: 0, Column: 0},
+ End: &storepb.Position{Line: 5, Column: 42},
},
{
- Text: `
-
- SELECT * FROM products;`,
- BaseLine: 5,
- Start: &storepb.Position{Line: 7, Column: 3},
- End: &storepb.Position{Line: 7, Column: 25},
+ Text: `SELECT * FROM products;`,
+ BaseLine: 7,
+ Start: &storepb.Position{Line: 7, Column: 4},
+ End: &storepb.Position{Line: 7, Column: 27},
},
},
},
diff --git a/backend/plugin/parser/trino/test-data/completion/t_c.yaml b/backend/plugin/parser/trino/test-data/completion/t_c.yaml
deleted file mode 100644
index 16ef2dda37..0000000000
--- a/backend/plugin/parser/trino/test-data/completion/t_c.yaml
+++ /dev/null
@@ -1,277 +0,0 @@
-- input: |-
- select count(1) from users where id = 1; SELECT * FROM |
- select * from orders where order_id like '%123';
- want:
- - text: orders
- type: TABLE
- definition: catalog1.public.orders
- comment: ""
- priority: 0
- - text: users
- type: TABLE
- definition: catalog1.public.users
- comment: ""
- priority: 0
- - text: active_users
- type: VIEW
- definition: catalog1.public.active_users
- comment: ""
- priority: 0
-
-- input: "|"
- want:
- - text: SELECT
- type: KEYWORD
- definition: ""
- comment: ""
- priority: 0
- - text: CREATE
- type: KEYWORD
- definition: ""
- comment: ""
- priority: 0
- - text: INSERT
- type: KEYWORD
- definition: ""
- comment: ""
- priority: 0
- - text: ALTER
- type: KEYWORD
- definition: ""
- comment: ""
- priority: 0
- - text: DROP
- type: KEYWORD
- definition: ""
- comment: ""
- priority: 0
-
-- input: "SEL|"
- want:
- - text: SELECT
- type: KEYWORD
- definition: ""
- comment: ""
- priority: 0
-
-- input: "SELECT * FROM |"
- want:
- - text: orders
- type: TABLE
- definition: catalog1.public.orders
- comment: ""
- priority: 0
- - text: users
- type: TABLE
- definition: catalog1.public.users
- comment: ""
- priority: 0
- - text: active_users
- type: VIEW
- definition: catalog1.public.active_users
- comment: ""
- priority: 0
-
-- input: "SELECT * FROM catalog1.|"
- want:
- - text: public
- type: SCHEMA
- definition: ""
- comment: ""
- priority: 0
- - text: analytics
- type: SCHEMA
- definition: ""
- comment: ""
- priority: 0
-
-- input: "SELECT * FROM catalog1.public.|"
- want:
- - text: orders
- type: TABLE
- definition: ""
- comment: ""
- priority: 0
- - text: users
- type: TABLE
- definition: ""
- comment: ""
- priority: 0
- - text: active_users
- type: VIEW
- definition: ""
- comment: ""
- priority: 0
-
-- input: "SELECT | FROM users"
- want:
- - text: id
- type: COLUMN
- definition: catalog1.public.users | INT
- comment: ""
- priority: 0
- - text: name
- type: COLUMN
- definition: catalog1.public.users | VARCHAR
- comment: ""
- priority: 0
- - text: email
- type: COLUMN
- definition: catalog1.public.users | VARCHAR
- comment: ""
- priority: 0
-
-- input: "SELECT users.| FROM users"
- want:
- - text: id
- type: COLUMN
- definition: catalog1.public.users | INT
- comment: ""
- priority: 0
- - text: name
- type: COLUMN
- definition: catalog1.public.users | VARCHAR
- comment: ""
- priority: 0
- - text: email
- type: COLUMN
- definition: catalog1.public.users | VARCHAR
- comment: ""
- priority: 0
-
-- input: "SELECT * FROM users u WHERE u.|"
- want:
- - text: id
- type: COLUMN
- definition: catalog1.public.users | INT
- comment: ""
- priority: 0
- - text: name
- type: COLUMN
- definition: catalog1.public.users | VARCHAR
- comment: ""
- priority: 0
- - text: email
- type: COLUMN
- definition: catalog1.public.users | VARCHAR
- comment: ""
- priority: 0
-
-- input: "SELECT u.id, o.total FROM users u JOIN orders o ON u.id = o.|"
- want:
- - text: id
- type: COLUMN
- definition: catalog1.public.orders | INT
- comment: ""
- priority: 0
- - text: user_id
- type: COLUMN
- definition: catalog1.public.orders | INT
- comment: ""
- priority: 0
- - text: total
- type: COLUMN
- definition: catalog1.public.orders | DECIMAL
- comment: ""
- priority: 0
-
-- input: "WITH cte_test AS (SELECT id, name FROM users) SELECT | FROM cte_test"
- want:
- - text: id
- type: COLUMN
- definition: ""
- comment: ""
- priority: 0
- - text: name
- type: COLUMN
- definition: ""
- comment: ""
- priority: 0
-
-- input: "WITH cte_test AS (SELECT id, name FROM users) SELECT * FROM |"
- want:
- - text: cte_test
- type: TABLE
- definition: ""
- comment: ""
- priority: 0
- - text: orders
- type: TABLE
- definition: catalog1.public.orders
- comment: ""
- priority: 0
- - text: users
- type: TABLE
- definition: catalog1.public.users
- comment: ""
- priority: 0
- - text: active_users
- type: VIEW
- definition: catalog1.public.active_users
- comment: ""
- priority: 0
-
-- input: "SELECT * FROM (SELECT id, name FROM users) AS subq WHERE subq.|"
- want:
- - text: id
- type: COLUMN
- definition: ""
- comment: ""
- priority: 0
- - text: name
- type: COLUMN
- definition: ""
- comment: ""
- priority: 0
-
-- input: "SELECT id AS user_id, name FROM users ORDER BY |"
- want:
- - text: user_id
- type: COLUMN
- definition: ""
- comment: ""
- priority: 0
- - text: name
- type: COLUMN
- definition: ""
- comment: ""
- priority: 0
-
-- input: "SELECT metrics.| FROM catalog1.analytics.metrics"
- want:
- - text: timestamp
- type: COLUMN
- definition: catalog1.analytics.metrics | TIMESTAMP
- comment: ""
- priority: 0
- - text: user_id
- type: COLUMN
- definition: catalog1.analytics.metrics | INT
- comment: ""
- priority: 0
- - text: value
- type: COLUMN
- definition: catalog1.analytics.metrics | DOUBLE
- comment: ""
- priority: 0
-
-- input: "SELECT * FROM catalog2.|"
- want:
- - text: test
- type: SCHEMA
- definition: ""
- comment: ""
- priority: 0
-
-- input: "SELECT * FROM catalog2.test.external_data WHERE |"
- want:
- - text: id
- type: COLUMN
- definition: catalog2.test.external_data | VARCHAR
- comment: ""
- priority: 0
- - text: data
- type: COLUMN
- definition: catalog2.test.external_data | JSON
- comment: ""
- priority: 0
\ No newline at end of file
diff --git a/backend/plugin/parser/trino/test-data/completion/t_cc.yaml b/backend/plugin/parser/trino/test-data/completion/t_cc.yaml
deleted file mode 100644
index 8be91760b9..0000000000
--- a/backend/plugin/parser/trino/test-data/completion/t_cc.yaml
+++ /dev/null
@@ -1,33 +0,0 @@
-- description: Multiple statements
- 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
diff --git a/backend/plugin/parser/trino/test-data/completion/test_completion.yaml b/backend/plugin/parser/trino/test-data/completion/test_completion.yaml
index bbdbd5b23a..2d576cc969 100644
--- a/backend/plugin/parser/trino/test-data/completion/test_completion.yaml
+++ b/backend/plugin/parser/trino/test-data/completion/test_completion.yaml
@@ -1,267 +1,301 @@
-- input: |-
- select count(1) from users where id 'invalid'; SELECT * FROM |
- select * from orders where order_id like '%123';
+- 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: orders
- type: TABLE
- definition: catalog1.public.orders
+ - text: Company
+ type: DATABASE
+ definition: ""
comment: ""
priority: 0
- - text: users
- type: TABLE
- definition: catalog1.public.users
+ - text: School
+ type: DATABASE
+ definition: ""
comment: ""
priority: 0
- - text: active_users
- type: VIEW
- definition: catalog1.public.active_users
+ - text: MySchema
+ type: SCHEMA
+ definition: ""
comment: ""
priority: 0
-- input: SELECT * FROM |
- want:
- - text: orders
- type: TABLE
- definition: catalog1.public.orders
+ - text: dbo
+ type: SCHEMA
+ definition: ""
comment: ""
priority: 0
- - text: users
+ - text: Address
type: TABLE
- definition: catalog1.public.users
+ definition: ""
comment: ""
priority: 0
- - text: active_users
- type: VIEW
- definition: catalog1.public.active_users
+ - text: Employees
+ type: TABLE
+ definition: ""
comment: ""
priority: 0
-- input: SELECT | FROM users
+- description: Column alias can be used in order by clause
+ input: SELECT Id AS IdAlias, Name FROM Employees ORDER BY |;
want:
- - text: '"timestamp"'
+ - text: EmployeeId
type: COLUMN
- definition: catalog1.analytics.metrics | TIMESTAMP, NOT NULL
+ definition: Company.dbo.Address | int, NOT NULL
comment: ""
- priority: 0
- - text: '"value"'
+ priority: 1
+ - text: Id
type: COLUMN
- definition: catalog1.analytics.metrics | DOUBLE, NOT NULL
+ definition: Company.MySchema.SalaryLevel | int, NOT NULL
comment: ""
- priority: 0
- - text: email
+ priority: 1
+ - text: Id
type: COLUMN
- definition: catalog1.public.users | VARCHAR, NOT NULL
+ definition: Company.dbo.Employees | int, NOT NULL
comment: ""
priority: 0
- - text: id
+ - text: IdAlias
type: COLUMN
- definition: catalog1.public.orders | INT, NOT NULL
+ definition: ""
comment: ""
priority: 0
- - text: id
+ - text: Name
type: COLUMN
- definition: catalog1.public.users | INT, NOT NULL
+ definition: Company.dbo.Employees | varchar, NOT NULL
comment: ""
priority: 0
- - text: name
+ - text: SalaryUpBound
type: COLUMN
- definition: catalog1.public.users | VARCHAR, NOT NULL
+ definition: Company.MySchema.SalaryLevel | int, NOT NULL
comment: ""
- priority: 0
- - text: total
+ priority: 1
+ - text: Street
type: COLUMN
- definition: catalog1.public.orders | DECIMAL, NOT NULL
+ definition: Company.dbo.Address | varchar, NOT NULL
comment: ""
- priority: 0
- - text: user_id
- type: COLUMN
- definition: catalog1.analytics.metrics | INT, NOT NULL
+ priority: 1
+ - text: Company
+ type: DATABASE
+ definition: ""
comment: ""
priority: 0
- - text: user_id
- type: COLUMN
- definition: catalog1.public.orders | INT, NOT NULL
+ - text: School
+ type: DATABASE
+ definition: ""
comment: ""
priority: 0
-- input: SELECT * FROM catalog1.|
- want:
- - text: analytics
+ - text: MySchema
type: SCHEMA
definition: ""
comment: ""
- - text: public
+ priority: 0
+ - text: dbo
type: SCHEMA
definition: ""
comment: ""
-- input: SELECT * FROM catalog1.public.|
- want:
- - text: orders
- type: TABLE
- definition: catalog1.public.orders
- comment: ""
- - text: users
- type: TABLE
- definition: catalog1.public.users
- comment: ""
- - text: active_users
- type: VIEW
- definition: catalog1.public.active_users
- comment: ""
-- input: SELECT * FROM analytics.|
- want:
- - text: metrics
+ priority: 0
+ - text: Address
type: TABLE
- definition: catalog1.analytics.metrics
+ definition: ""
comment: ""
-- input: SELECT * FROM catalog2.test.|
- want:
- - text: external_data
+ priority: 0
+ - text: Employees
type: TABLE
- definition: catalog2.test.external_data
+ definition: ""
comment: ""
-- input: SELECT u.| FROM users u
+ priority: 0
+- description: Table alias referenced by select item
+ input: SELECT tableAlias.| FROM Employees AS tableAlias;
want:
- - text: id
- type: COLUMN
- definition: catalog1.public.users | INT, NOT NULL
- comment: ""
- - text: name
+ - text: Id
type: COLUMN
- definition: catalog1.public.users | VARCHAR, NOT NULL
+ definition: Company.dbo.Employees | int, NOT NULL
comment: ""
- - text: email
+ priority: 0
+ - text: Name
type: COLUMN
- definition: catalog1.public.users | VARCHAR, NOT NULL
+ definition: Company.dbo.Employees | varchar, NOT NULL
comment: ""
-- input: WITH x(x1, x2) AS (SELECT id, name FROM users) SELECT x.| FROM x;
+ priority: 0
+- description: Full column name
+ input: SELECT | FROM Employees;
want:
- - text: x1
+ - text: EmployeeId
type: COLUMN
- definition: ""
+ definition: Company.dbo.Address | int, NOT NULL
comment: ""
- - text: x2
+ priority: 1
+ - text: Id
type: COLUMN
- definition: ""
+ definition: Company.MySchema.SalaryLevel | int, NOT NULL
comment: ""
-- input: WITH x(x1, x2) AS (SELECT id, name FROM users) SELECT | FROM x;
- want:
- - text: '"timestamp"'
+ priority: 1
+ - text: Id
type: COLUMN
- definition: catalog1.analytics.metrics | TIMESTAMP, NOT NULL
+ definition: Company.dbo.Employees | int, NOT NULL
comment: ""
priority: 0
- - text: '"value"'
+ - text: Name
type: COLUMN
- definition: catalog1.analytics.metrics | DOUBLE, NOT NULL
+ definition: Company.dbo.Employees | varchar, NOT NULL
comment: ""
priority: 0
- - text: email
+ - text: SalaryUpBound
type: COLUMN
- definition: catalog1.public.users | VARCHAR, NOT NULL
+ definition: Company.MySchema.SalaryLevel | int, NOT NULL
comment: ""
- priority: 0
- - text: id
+ priority: 1
+ - text: Street
type: COLUMN
- definition: catalog1.public.orders | INT, NOT NULL
+ definition: Company.dbo.Address | varchar, NOT NULL
+ comment: ""
+ priority: 1
+ - text: Company
+ type: DATABASE
+ definition: ""
comment: ""
priority: 0
- - text: id
- type: COLUMN
- definition: catalog1.public.users | INT, NOT NULL
+ - text: School
+ type: DATABASE
+ definition: ""
comment: ""
priority: 0
- - text: name
- type: COLUMN
- definition: catalog1.public.users | VARCHAR, NOT NULL
+ - text: MySchema
+ type: SCHEMA
+ definition: ""
comment: ""
priority: 0
- - text: total
- type: COLUMN
- definition: catalog1.public.orders | DECIMAL, NOT NULL
+ - text: dbo
+ type: SCHEMA
+ definition: ""
comment: ""
priority: 0
- - text: user_id
- type: COLUMN
- definition: catalog1.analytics.metrics | INT, NOT NULL
+ - text: Address
+ type: TABLE
+ definition: ""
comment: ""
priority: 0
- - text: user_id
- type: COLUMN
- definition: catalog1.public.orders | INT, NOT NULL
+ - text: Employees
+ type: TABLE
+ definition: ""
comment: ""
priority: 0
-- input: SELECT catalog1.public.users.|
+- 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: id
+ - text: EmployeeId
type: COLUMN
- definition: catalog1.public.users | INT, NOT NULL
+ definition: Company.dbo.Address | int, NOT NULL
comment: ""
- - text: name
+ priority: 0
+ - text: Id
type: COLUMN
- definition: catalog1.public.users | VARCHAR, NOT NULL
+ definition: Company.MySchema.SalaryLevel | int, NOT NULL
comment: ""
- - text: email
+ priority: 1
+ - text: Id
type: COLUMN
- definition: catalog1.public.users | VARCHAR, NOT NULL
+ definition: Company.dbo.Employees | int, NOT NULL
comment: ""
-- input: SELECT id as user_id FROM users ORDER BY |
- want:
- - text: id
+ priority: 0
+ - text: Name
type: COLUMN
- definition: catalog1.public.users | INT, NOT NULL
+ definition: Company.dbo.Employees | varchar, NOT NULL
comment: ""
- - text: name
+ priority: 0
+ - text: SalaryUpBound
type: COLUMN
- definition: catalog1.public.users | VARCHAR, NOT NULL
+ definition: Company.MySchema.SalaryLevel | int, NOT NULL
comment: ""
- - text: email
+ priority: 1
+ - text: Street
type: COLUMN
- definition: catalog1.public.users | VARCHAR, NOT NULL
+ definition: Company.dbo.Address | varchar, NOT NULL
comment: ""
- - text: user_id
- type: COLUMN
+ 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: ""
-- input: SELECT * FROM users JOIN |
+ 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: orders
+ - text: SalaryLevel
type: TABLE
- definition: catalog1.public.orders
+ definition: ""
comment: ""
priority: 0
- - text: users
+- description: Completion table in specify schema
+ input: SELECT * FROM dbo.|
+ want:
+ - text: Address
type: TABLE
- definition: catalog1.public.users
+ definition: ""
comment: ""
priority: 0
- - text: active_users
- type: VIEW
- definition: catalog1.public.active_users
+ - text: Employees
+ type: TABLE
+ definition: ""
comment: ""
priority: 0
-- input: '|'
+- description: Completion table for simple select
+ input: SELECT * FROM |
want:
- - text: ALTER
- type: KEYWORD
+ - text: Company
+ type: DATABASE
definition: ""
comment: ""
priority: 0
- - text: CREATE
- type: KEYWORD
+ - text: School
+ type: DATABASE
definition: ""
comment: ""
priority: 0
- - text: DROP
- type: KEYWORD
+ - text: MySchema
+ type: SCHEMA
definition: ""
comment: ""
priority: 0
- - text: INSERT
- type: KEYWORD
+ - text: dbo
+ type: SCHEMA
definition: ""
comment: ""
priority: 0
-- input: SEL|
- want:
- - text: SELECT
- type: KEYWORD
+ - 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/tsql/test-data/test_completion.yaml b/backend/plugin/parser/tsql/test-data/test_completion.yaml
index 7546dd97a5..f0ae37ce63 100644
--- a/backend/plugin/parser/tsql/test-data/test_completion.yaml
+++ b/backend/plugin/parser/tsql/test-data/test_completion.yaml
@@ -349,4 +349,4 @@
type: TABLE
definition: ""
comment: ""
- priority: 0
+ priority: 0
\ No newline at end of file