Skip to content

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

feat: column order (#15904)

rebelice476ba1203e32
diff --git a/backend/api/lsp/completion.go b/backend/api/lsp/completion.go
index 8aadd86bee..7763e62b76 100644
--- a/backend/api/lsp/completion.go
+++ b/backend/api/lsp/completion.go
@@ -100,24 +100,24 @@ func (h *Handler) handleTextDocumentCompletion(ctx context.Context, _ *jsonrpc2.
func generateSortText(_ lsp.CompletionParams, _ storepb.Engine, candidate parserbase.Candidate) string {
switch candidate.Type {
case parserbase.CandidateTypeColumn:
- return "01" + candidate.Text
+ return "01" + candidate.TextWithPriority()
case parserbase.CandidateTypeSchema:
- return "02" + candidate.Text
+ return "02" + candidate.TextWithPriority()
case parserbase.CandidateTypeTable, parserbase.CandidateTypeForeignTable:
- return "03" + candidate.Text
+ return "03" + candidate.TextWithPriority()
case parserbase.CandidateTypeView, parserbase.CandidateTypeMaterializedView:
- return "04" + candidate.Text
+ return "04" + candidate.TextWithPriority()
case parserbase.CandidateTypeFunction:
- return "05" + candidate.Text
+ return "05" + candidate.TextWithPriority()
case parserbase.CandidateTypeKeyword:
switch candidate.Text {
case "SELECT", "SHOW", "SET", "FROM", "WHERE":
- return "09" + candidate.Text
+ return "09" + candidate.TextWithPriority()
default:
- return "10" + candidate.Text
+ return "10" + candidate.TextWithPriority()
}
default:
- return "10" + string(candidate.Type) + candidate.Text
+ return "10" + string(candidate.Type) + candidate.TextWithPriority()
}
}
diff --git a/backend/plugin/parser/base/complete.go b/backend/plugin/parser/base/complete.go
index abd08596a1..bdc36dd79c 100644
--- a/backend/plugin/parser/base/complete.go
+++ b/backend/plugin/parser/base/complete.go
@@ -37,6 +37,12 @@ type Candidate struct {
Type CandidateType
Definition string
Comment string
+ // The smaller the number, the higher the priority.
+ Priority int
+}
+
+func (c Candidate) TextWithPriority() string {
+ return fmt.Sprintf("%d.%s", c.Priority, c.Text)
}
func (c Candidate) String() string {
diff --git a/backend/plugin/parser/tsql/completion.go b/backend/plugin/parser/tsql/completion.go
index 0a83c69d23..4400472ad8 100644
--- a/backend/plugin/parser/tsql/completion.go
+++ b/backend/plugin/parser/tsql/completion.go
@@ -295,6 +295,7 @@ func (m CompletionMap) insertAllColumns(c *Completer) {
Text: c.quotedIdentifierIfNeeded(column.Name),
Definition: definition,
Comment: comment,
+ Priority: c.getPriority(c.defaultDatabase, schema, table),
}
}
}
@@ -302,6 +303,23 @@ func (m CompletionMap) insertAllColumns(c *Completer) {
}
}
+func (c *Completer) getPriority(database string, schema string, table string) int {
+ if database == "" {
+ database = c.defaultDatabase
+ }
+ if schema == "" {
+ schema = c.defaultSchema
+ }
+ if c.referenceMap == nil {
+ return 1
+ }
+ if c.referenceMap[fmt.Sprintf("%s.%s.%s", database, schema, table)] {
+ // The higher priority.
+ return 0
+ }
+ return 1
+}
+
func (m CompletionMap) insertMetadataColumns(c *Completer, linkedServer string, database string, schema string, table string) {
if linkedServer != "" {
return
@@ -370,6 +388,7 @@ func (m CompletionMap) insertMetadataColumns(c *Completer, linkedServer string,
Text: c.quotedIdentifierIfNeeded(column.Name),
Definition: definition,
Comment: comment,
+ Priority: c.getPriority(c.defaultDatabase, schema, table),
}
}
}
@@ -482,6 +501,7 @@ type Completer struct {
// 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
@@ -1260,8 +1280,25 @@ func notEmptySQLCount(list []base.SingleSQL) int {
}
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 {
+ database := r.Database
+ if database == "" {
+ database = c.defaultDatabase
+ }
+ schema := r.Schema
+ if schema == "" {
+ schema = c.defaultSchema
+ }
+ tableID := fmt.Sprintf("%s.%s.%s", database, schema, r.Table)
+ c.referenceMap[tableID] = true
+ }
+ }
}
}
diff --git a/backend/plugin/parser/tsql/test-data/test_completion.yaml b/backend/plugin/parser/tsql/test-data/test_completion.yaml
index dce6d39edb..7546dd97a5 100644
--- a/backend/plugin/parser/tsql/test-data/test_completion.yaml
+++ b/backend/plugin/parser/tsql/test-data/test_completion.yaml
@@ -7,26 +7,32 @@
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:
@@ -34,54 +40,67 @@
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:
@@ -89,10 +108,12 @@
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 considering CTE
input: WITH MyCTE_01 AS (SELECT * FROM dbo.Employees) SELECT MyCTE_01.| FROM MyCTE_01;
want:
@@ -100,10 +121,12 @@
type: COLUMN
definition: ""
comment: ""
+ priority: 0
- text: Name
type: COLUMN
definition: ""
comment: ""
+ priority: 0
- description: Full column name
input: SELECT | FROM Employees;
want:
@@ -111,50 +134,62 @@
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:
@@ -162,54 +197,67 @@
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:
@@ -217,6 +265,7 @@
type: TABLE
definition: ""
comment: ""
+ priority: 0
- description: Completion should consider CTE if no schema specified
input: WITH MyCTE_01 AS (SELECT * FROM dbo.Employees) SELECT * FROM |
want:
@@ -224,30 +273,37 @@
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 table in specify schema
input: SELECT * FROM dbo.|
want:
@@ -255,10 +311,12 @@
type: TABLE
definition: ""
comment: ""
+ priority: 0
- text: Employees
type: TABLE
definition: ""
comment: ""
+ priority: 0
- description: Completion table for simple select
input: SELECT * FROM |
want:
@@ -266,23 +324,29 @@
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