Skip to content

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

feat(mssql-lsp): return all columns for no table (#14065)

rebelice88ed22b48ddc
diff --git a/backend/api/lsp/completion.go b/backend/api/lsp/completion.go
index a80026aead..dada18bcd5 100644
--- a/backend/api/lsp/completion.go
+++ b/backend/api/lsp/completion.go
@@ -97,52 +97,27 @@ func (h *Handler) handleTextDocumentCompletion(ctx context.Context, _ *jsonrpc2.
}, nil
}
-func generateSortText(_ lsp.CompletionParams, engine storepb.Engine, candidate base.Candidate) string {
- switch engine {
- case storepb.Engine_MSSQL:
- switch candidate.Type {
- case base.CandidateTypeSchema:
- return "01" + candidate.Text
- case base.CandidateTypeTable, base.CandidateTypeForeignTable:
- return "02" + candidate.Text
- case base.CandidateTypeView, base.CandidateTypeMaterializedView:
- return "03" + candidate.Text
- case base.CandidateTypeColumn:
- return "04" + candidate.Text
- case base.CandidateTypeFunction:
- return "05" + candidate.Text
- case base.CandidateTypeKeyword:
- switch candidate.Text {
- case "SELECT", "SHOW", "SET", "FROM", "WHERE":
- return "09" + candidate.Text
- default:
- return "10" + candidate.Text
- }
+func generateSortText(_ lsp.CompletionParams, _ storepb.Engine, candidate base.Candidate) string {
+ switch candidate.Type {
+ case base.CandidateTypeColumn:
+ return "01" + candidate.Text
+ case base.CandidateTypeSchema:
+ return "02" + candidate.Text
+ case base.CandidateTypeTable, base.CandidateTypeForeignTable:
+ return "03" + candidate.Text
+ case base.CandidateTypeView, base.CandidateTypeMaterializedView:
+ return "04" + candidate.Text
+ case base.CandidateTypeFunction:
+ return "05" + candidate.Text
+ case base.CandidateTypeKeyword:
+ switch candidate.Text {
+ case "SELECT", "SHOW", "SET", "FROM", "WHERE":
+ return "09" + candidate.Text
default:
- return "10" + string(candidate.Type) + candidate.Text
+ return "10" + candidate.Text
}
default:
- switch candidate.Type {
- case base.CandidateTypeColumn:
- return "01" + candidate.Text
- case base.CandidateTypeSchema:
- return "02" + candidate.Text
- case base.CandidateTypeTable, base.CandidateTypeForeignTable:
- return "03" + candidate.Text
- case base.CandidateTypeView, base.CandidateTypeMaterializedView:
- return "04" + candidate.Text
- case base.CandidateTypeFunction:
- return "05" + candidate.Text
- case base.CandidateTypeKeyword:
- switch candidate.Text {
- case "SELECT", "SHOW", "SET", "FROM", "WHERE":
- return "09" + candidate.Text
- default:
- return "10" + candidate.Text
- }
- default:
- return "10" + string(candidate.Type) + candidate.Text
- }
+ return "10" + string(candidate.Type) + candidate.Text
}
}
diff --git a/backend/plugin/parser/tsql/completion.go b/backend/plugin/parser/tsql/completion.go
index 619235b57e..1d5a9660f9 100644
--- a/backend/plugin/parser/tsql/completion.go
+++ b/backend/plugin/parser/tsql/completion.go
@@ -261,6 +261,41 @@ func (m CompletionMap) insertMetadataTables(c *Completer, linkedServer string, d
}
}
+func (m CompletionMap) insertAllColumns(c *Completer) {
+ _, databaseMeta, err := c.metadataGetter(c.ctx, c.instanceID, c.defaultDatabase)
+ if err != nil {
+ return
+ }
+ for _, schema := range databaseMeta.ListSchemaNames() {
+ schemaMeta := databaseMeta.GetSchema(schema)
+ if schemaMeta == nil {
+ continue
+ }
+ for _, table := range schemaMeta.ListTableNames() {
+ tableMeta := schemaMeta.GetTable(table)
+ if tableMeta == nil {
+ continue
+ }
+ for _, column := range tableMeta.GetColumns() {
+ columnID := getColumnID(c.defaultDatabase, schema, table, column.Name)
+ if _, ok := m[columnID]; !ok {
+ definition := fmt.Sprintf("%s.%s.%s | %s", c.defaultDatabase, schema, table, column.Type)
+ if !column.Nullable {
+ definition += ", NOT NULL"
+ }
+ comment := column.UserComment
+ m[columnID] = base.Candidate{
+ Type: base.CandidateTypeColumn,
+ Text: c.quotedIdentifierIfNeeded(column.Name),
+ Definition: definition,
+ Comment: comment,
+ }
+ }
+ }
+ }
+ }
+}
+
func (m CompletionMap) insertMetadataColumns(c *Completer, linkedServer string, database string, schema string, table string) {
if linkedServer != "" {
return
@@ -317,13 +352,14 @@ func (m CompletionMap) insertMetadataColumns(c *Completer, linkedServer string,
for _, table := range tableNames {
tableMetadata := schemaMetadata.GetTable(table)
for _, column := range tableMetadata.GetColumns() {
- if _, ok := m[column.Name]; !ok {
- definition := fmt.Sprintf("%s | %s", table, column.Type)
+ columnID := getColumnID(databaseName, schemaName, table, column.Name)
+ if _, ok := m[columnID]; !ok {
+ definition := fmt.Sprintf("%s.%s.%s | %s", databaseName, schemaName, table, column.Type)
if !column.Nullable {
definition += ", NOT NULL"
}
comment := column.UserComment
- m[column.Name] = base.Candidate{
+ m[columnID] = base.Candidate{
Type: base.CandidateTypeColumn,
Text: c.quotedIdentifierIfNeeded(column.Name),
Definition: definition,
@@ -334,6 +370,10 @@ func (m CompletionMap) insertMetadataColumns(c *Completer, linkedServer string,
}
}
+func getColumnID(databaseName, schemaName, tableName, columnName string) string {
+ return fmt.Sprintf("%s.%s.%s.%s", databaseName, schemaName, tableName, columnName)
+}
+
func (m CompletionMap) insertCTEs(c *Completer) {
for _, cte := range c.cteTables {
if _, ok := m[cte.Table]; !ok {
@@ -798,6 +838,9 @@ func (c *Completer) convertCandidates(candidates *base.CandidatesCollection) ([]
}
}
}
+ if context.empty() {
+ columnEntries.insertAllColumns(c)
+ }
}
}
}
@@ -866,6 +909,10 @@ type objectRefContext struct {
flags objectFlag
}
+func (o *objectRefContext) empty() bool {
+ return o.linkedServer == "" && o.database == "" && o.schema == "" && o.object == "" && o.column == ""
+}
+
func (o *objectRefContext) clone() *objectRefContext {
return &objectRefContext{
linkedServer: o.linkedServer,
diff --git a/backend/plugin/parser/tsql/test-data/test_completion.yaml b/backend/plugin/parser/tsql/test-data/test_completion.yaml
index 44447fd12f..8dd05ed8f2 100644
--- a/backend/plugin/parser/tsql/test-data/test_completion.yaml
+++ b/backend/plugin/parser/tsql/test-data/test_completion.yaml
@@ -33,19 +33,27 @@
comment: ""
- text: EmployeeId
type: COLUMN
- definition: Address | int, NOT NULL
+ definition: Company.dbo.Address | int, NOT NULL
comment: ""
- text: Id
type: COLUMN
- definition: Employees | int, NOT NULL
+ definition: Company.dbo.Employees | int, NOT NULL
+ comment: ""
+ - text: Id
+ type: COLUMN
+ definition: Company.MySchema.SalaryLevel | int, NOT NULL
comment: ""
- text: Name
type: COLUMN
- definition: Employees | varchar, NOT NULL
+ definition: Company.dbo.Employees | varchar, NOT NULL
+ comment: ""
+ - text: SalaryUpBound
+ type: COLUMN
+ definition: Company.MySchema.SalaryLevel | int, NOT NULL
comment: ""
- text: Street
type: COLUMN
- definition: Address | varchar, NOT NULL
+ definition: Company.dbo.Address | varchar, NOT NULL
comment: ""
- description: Column alias can be used in order by clause
input: SELECT Id AS IdAlias, Name FROM Employees ORDER BY |;
@@ -76,11 +84,15 @@
comment: ""
- text: EmployeeId
type: COLUMN
- definition: Address | int, NOT NULL
+ definition: Company.dbo.Address | int, NOT NULL
+ comment: ""
+ - text: Id
+ type: COLUMN
+ definition: Company.dbo.Employees | int, NOT NULL
comment: ""
- text: Id
type: COLUMN
- definition: Employees | int, NOT NULL
+ definition: Company.MySchema.SalaryLevel | int, NOT NULL
comment: ""
- text: IdAlias
type: COLUMN
@@ -88,22 +100,26 @@
comment: ""
- text: Name
type: COLUMN
- definition: Employees | varchar, NOT NULL
+ definition: Company.dbo.Employees | varchar, NOT NULL
+ comment: ""
+ - text: SalaryUpBound
+ type: COLUMN
+ definition: Company.MySchema.SalaryLevel | int, NOT NULL
comment: ""
- text: Street
type: COLUMN
- definition: Address | varchar, NOT NULL
+ definition: Company.dbo.Address | varchar, NOT NULL
comment: ""
- description: Table alias referenced by select item
input: SELECT tableAlias.| FROM Employees AS tableAlias;
want:
- text: Id
type: COLUMN
- definition: Employees | int, NOT NULL
+ definition: Company.dbo.Employees | int, NOT NULL
comment: ""
- text: Name
type: COLUMN
- definition: Employees | varchar, NOT NULL
+ definition: Company.dbo.Employees | varchar, NOT NULL
comment: ""
- description: Full column name considering CTE
input: WITH MyCTE_01 AS (SELECT * FROM dbo.Employees) SELECT MyCTE_01.| FROM MyCTE_01;
@@ -145,19 +161,27 @@
comment: ""
- text: EmployeeId
type: COLUMN
- definition: Address | int, NOT NULL
+ definition: Company.dbo.Address | int, NOT NULL
comment: ""
- text: Id
type: COLUMN
- definition: Employees | int, NOT NULL
+ definition: Company.dbo.Employees | int, NOT NULL
+ comment: ""
+ - text: Id
+ type: COLUMN
+ definition: Company.MySchema.SalaryLevel | int, NOT NULL
comment: ""
- text: Name
type: COLUMN
- definition: Employees | varchar, NOT NULL
+ definition: Company.dbo.Employees | varchar, NOT NULL
+ comment: ""
+ - text: SalaryUpBound
+ type: COLUMN
+ definition: Company.MySchema.SalaryLevel | int, NOT NULL
comment: ""
- text: Street
type: COLUMN
- definition: Address | varchar, NOT NULL
+ definition: Company.dbo.Address | varchar, NOT NULL
comment: ""
- 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
@@ -192,19 +216,27 @@
comment: ""
- text: EmployeeId
type: COLUMN
- definition: Address | int, NOT NULL
+ definition: Company.dbo.Address | int, NOT NULL
comment: ""
- text: Id
type: COLUMN
- definition: Employees | int, NOT NULL
+ definition: Company.dbo.Employees | int, NOT NULL
+ comment: ""
+ - text: Id
+ type: COLUMN
+ definition: Company.MySchema.SalaryLevel | int, NOT NULL
comment: ""
- text: Name
type: COLUMN
- definition: Employees | varchar, NOT NULL
+ definition: Company.dbo.Employees | varchar, NOT NULL
+ comment: ""
+ - text: SalaryUpBound
+ type: COLUMN
+ definition: Company.MySchema.SalaryLevel | int, NOT NULL
comment: ""
- text: Street
type: COLUMN
- definition: Address | varchar, NOT NULL
+ definition: Company.dbo.Address | varchar, NOT NULL
comment: ""
- description: Completion should not consider CTE if no schema specified
input: WITH MyCTE_01 AS (SELECT * FROM dbo.Employees) SELECT * FROM MySchema.|
@@ -215,11 +247,11 @@
comment: ""
- text: Id
type: COLUMN
- definition: SalaryLevel | int, NOT NULL
+ definition: Company.MySchema.SalaryLevel | int, NOT NULL
comment: ""
- text: SalaryUpBound
type: COLUMN
- definition: SalaryLevel | int, NOT NULL
+ definition: Company.MySchema.SalaryLevel | int, NOT NULL
comment: ""
- description: Completion should consider CTE if no schema specified
input: WITH MyCTE_01 AS (SELECT * FROM dbo.Employees) SELECT * FROM |
@@ -254,19 +286,27 @@
comment: ""
- text: EmployeeId
type: COLUMN
- definition: Address | int, NOT NULL
+ definition: Company.dbo.Address | int, NOT NULL
+ comment: ""
+ - text: Id
+ type: COLUMN
+ definition: Company.dbo.Employees | int, NOT NULL
comment: ""
- text: Id
type: COLUMN
- definition: Employees | int, NOT NULL
+ definition: Company.MySchema.SalaryLevel | int, NOT NULL
comment: ""
- text: Name
type: COLUMN
- definition: Employees | varchar, NOT NULL
+ definition: Company.dbo.Employees | varchar, NOT NULL
+ comment: ""
+ - text: SalaryUpBound
+ type: COLUMN
+ definition: Company.MySchema.SalaryLevel | int, NOT NULL
comment: ""
- text: Street
type: COLUMN
- definition: Address | varchar, NOT NULL
+ definition: Company.dbo.Address | varchar, NOT NULL
comment: ""
- description: Completion table in specify schema
input: SELECT * FROM dbo.|
@@ -281,19 +321,19 @@
comment: ""
- text: EmployeeId
type: COLUMN
- definition: Address | int, NOT NULL
+ definition: Company.dbo.Address | int, NOT NULL
comment: ""
- text: Id
type: COLUMN
- definition: Employees | int, NOT NULL
+ definition: Company.dbo.Employees | int, NOT NULL
comment: ""
- text: Name
type: COLUMN
- definition: Employees | varchar, NOT NULL
+ definition: Company.dbo.Employees | varchar, NOT NULL
comment: ""
- text: Street
type: COLUMN
- definition: Address | varchar, NOT NULL
+ definition: Company.dbo.Address | varchar, NOT NULL
comment: ""
- description: Completion table for simple select
input: SELECT * FROM |
@@ -324,17 +364,25 @@
comment: ""
- text: EmployeeId
type: COLUMN
- definition: Address | int, NOT NULL
+ definition: Company.dbo.Address | int, NOT NULL
comment: ""
- text: Id
type: COLUMN
- definition: Employees | int, NOT NULL
+ definition: Company.dbo.Employees | int, NOT NULL
+ comment: ""
+ - text: Id
+ type: COLUMN
+ definition: Company.MySchema.SalaryLevel | int, NOT NULL
comment: ""
- text: Name
type: COLUMN
- definition: Employees | varchar, NOT NULL
+ definition: Company.dbo.Employees | varchar, NOT NULL
+ comment: ""
+ - text: SalaryUpBound
+ type: COLUMN
+ definition: Company.MySchema.SalaryLevel | int, NOT NULL
comment: ""
- text: Street
type: COLUMN
- definition: Address | varchar, NOT NULL
+ definition: Company.dbo.Address | varchar, NOT NULL
comment: ""