Skip to content

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

feat(completion): plsql completion supports column alias (#12884)

rebelice65d29243a9ed
diff --git a/backend/plugin/parser/plsql/completion.go b/backend/plugin/parser/plsql/completion.go
index ebfcfaed53..42468b6e77 100644
--- a/backend/plugin/parser/plsql/completion.go
+++ b/backend/plugin/parser/plsql/completion.go
@@ -190,9 +190,9 @@ func NewTrickyCompleter(ctx context.Context, cCtx base.CompletionContext, statem
newIgnoredTokens(),
newPreferredRules(),
&globalFollowSetsByState,
- 0, // todo
- 0, // todo
- 0, // todo
+ plsql.PlSqlParserRULE_query_block,
+ plsql.PlSqlParserRULE_select_statement,
+ plsql.PlSqlParserRULE_column_alias,
0, // todo
)
return &Completer{
@@ -219,9 +219,9 @@ func NewStandardCompleter(ctx context.Context, cCtx base.CompletionContext, stat
newIgnoredTokens(),
newPreferredRules(),
&globalFollowSetsByState,
- 0, // todo
- 0, // todo
- 0, // todo
+ plsql.PlSqlParserRULE_query_block,
+ plsql.PlSqlParserRULE_select_statement,
+ plsql.PlSqlParserRULE_column_alias,
0, // todo
)
return &Completer{
@@ -619,11 +619,67 @@ func (c *Completer) convertCandidates(candidates *base.CandidatesCollection) ([]
return result, nil
}
-func (*Completer) fetchSelectItemAliases(_ []*base.RuleContext) []string {
- // todo
+func (c *Completer) fetchSelectItemAliases(ruleStack []*base.RuleContext) []string {
+ canUseAliases := false
+ for i := len(ruleStack) - 1; i >= 0; i-- {
+ switch ruleStack[i].ID {
+ case plsql.PlSqlParserRULE_group_by_clause, plsql.PlSqlParserRULE_order_by_clause:
+ canUseAliases = true
+ case plsql.PlSqlParserRULE_query_block, plsql.PlSqlParserRULE_select_statement:
+ 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)
+ }
+ sort.Slice(result, func(i, j int) bool {
+ return result[i] < result[j]
+ })
+ return result
+ }
+ }
return nil
}
+func (c *Completer) extractAliasText(pos int) string {
+ followingText := c.scanner.GetFollowingTextAfter(pos)
+ if len(followingText) == 0 {
+ return ""
+ }
+
+ input := antlr.NewInputStream(followingText)
+ lexer := plsql.NewPlSqlLexer(input)
+ tokens := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
+ parser := plsql.NewPlSqlParser(tokens)
+
+ parser.BuildParseTrees = true
+ parser.RemoveErrorListeners()
+ lexer.RemoveErrorListeners()
+ tree := parser.Column_alias()
+
+ listener := &AliasListener{}
+ antlr.ParseTreeWalkerDefault.Walk(listener, tree)
+ return listener.result
+}
+
+type AliasListener struct {
+ *plsql.BasePlSqlParserListener
+
+ result string
+}
+
+func (l *AliasListener) EnterColumn_alias(ctx *plsql.Column_aliasContext) {
+ l.result = normalizeColumnAlias(ctx)
+}
+
type ObjectFlags int
const (
diff --git a/backend/plugin/parser/plsql/test-data/test_completion.yaml b/backend/plugin/parser/plsql/test-data/test_completion.yaml
index 428625a8a9..8729159544 100644
--- a/backend/plugin/parser/plsql/test-data/test_completion.yaml
+++ b/backend/plugin/parser/plsql/test-data/test_completion.yaml
@@ -1,3 +1,45 @@
+- input: SELECT c1 as eid, c2 as xid FROM t2 GROUP BY |
+ want:
+ - text: SCHEMA1
+ type: SCHEMA
+ definition: ""
+ comment: ""
+ - text: SCHEMA2
+ type: SCHEMA
+ definition: ""
+ comment: ""
+ - text: SCHEMA3
+ type: SCHEMA
+ definition: ""
+ comment: ""
+ - text: T1
+ type: TABLE
+ definition: ""
+ comment: ""
+ - text: T2
+ type: TABLE
+ definition: ""
+ comment: ""
+ - text: C1
+ type: COLUMN
+ definition: T2 | int, NOT NULL
+ comment: ""
+ - text: C2
+ type: COLUMN
+ definition: T2 | int, NOT NULL
+ comment: ""
+ - text: EID
+ type: COLUMN
+ definition: ""
+ comment: ""
+ - text: XID
+ type: COLUMN
+ definition: ""
+ comment: ""
+ - text: V1
+ type: VIEW
+ definition: ""
+ comment: ""
- input: SELECT c1 as eid, c2 as xid FROM t2 ORDER BY |
want:
- text: SCHEMA1
@@ -28,6 +70,14 @@
type: COLUMN
definition: T2 | int, NOT NULL
comment: ""
+ - text: EID
+ type: COLUMN
+ definition: ""
+ comment: ""
+ - text: XID
+ type: COLUMN
+ definition: ""
+ comment: ""
- text: V1
type: VIEW
definition: ""
@@ -58,6 +108,10 @@
type: COLUMN
definition: T1 | int, NOT NULL
comment: ""
+ - text: EID
+ type: COLUMN
+ definition: ""
+ comment: ""
- text: V1
type: VIEW
definition: ""