Skip to content

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

chore: complete schema for non-schema (#18014)

rebeliceea5bb4b3c67e
diff --git a/backend/plugin/parser/pg/completion.go b/backend/plugin/parser/pg/completion.go
index c25c0b09c3..0cac7a16af 100644
--- a/backend/plugin/parser/pg/completion.go
+++ b/backend/plugin/parser/pg/completion.go
@@ -153,6 +153,7 @@ type Completer struct {
instanceID string
defaultDatabase string
defaultSchema string
+ schemaNotSelected bool // true if user didn't explicitly select a schema
getMetadata base.GetDatabaseMetadataFunc
listDatabaseNames base.ListDatabaseNamesFunc
metadataCache map[string]*model.DatabaseMetadata
@@ -184,6 +185,7 @@ func NewTrickyCompleter(ctx context.Context, cCtx base.CompletionContext, statem
pg.PostgreSQLParserRULE_with_clause,
)
defaultSchema := cCtx.DefaultSchema
+ schemaNotSelected := defaultSchema == ""
if defaultSchema == "" {
defaultSchema = "public"
}
@@ -197,6 +199,7 @@ func NewTrickyCompleter(ctx context.Context, cCtx base.CompletionContext, statem
instanceID: cCtx.InstanceID,
defaultDatabase: cCtx.DefaultDatabase,
defaultSchema: defaultSchema,
+ schemaNotSelected: schemaNotSelected,
getMetadata: cCtx.Metadata,
metadataCache: make(map[string]*model.DatabaseMetadata),
noSeparatorRequired: newNoSeparatorRequired(),
@@ -220,6 +223,7 @@ func NewStandardCompleter(ctx context.Context, cCtx base.CompletionContext, stat
pg.PostgreSQLParserRULE_with_clause,
)
defaultSchema := cCtx.DefaultSchema
+ schemaNotSelected := defaultSchema == ""
if defaultSchema == "" {
defaultSchema = "public"
}
@@ -233,6 +237,7 @@ func NewStandardCompleter(ctx context.Context, cCtx base.CompletionContext, stat
instanceID: cCtx.InstanceID,
defaultDatabase: cCtx.DefaultDatabase,
defaultSchema: defaultSchema,
+ schemaNotSelected: schemaNotSelected,
getMetadata: cCtx.Metadata,
metadataCache: make(map[string]*model.DatabaseMetadata),
noSeparatorRequired: newNoSeparatorRequired(),
@@ -304,7 +309,7 @@ func (m CompletionMap) insertSchemas(c *Completer) {
}
}
-func (m CompletionMap) insertTables(c *Completer, schemas map[string]bool) {
+func (m CompletionMap) insertTablesWithPrefix(c *Completer, schemas map[string]bool, includeSchemaPrefix bool) {
for schema := range schemas {
if len(schema) == 0 {
// User didn't specify the schema, we need to append cte tables.
@@ -317,32 +322,55 @@ func (m CompletionMap) insertTables(c *Completer, schemas map[string]bool) {
continue
}
for _, table := range c.listTables(schema) {
+ text := c.quotedIdentifierIfNeeded(table)
+ // Only add schema prefix if user didn't select a schema and the table is not in the default schema
+ if includeSchemaPrefix && schema != c.defaultSchema {
+ text = c.quotedIdentifierIfNeeded(schema) + "." + text
+ }
m.Insert(base.Candidate{
Type: base.CandidateTypeTable,
- Text: c.quotedIdentifierIfNeeded(table),
+ Text: text,
})
}
for _, fTable := range c.listForeignTables(schema) {
+ text := c.quotedIdentifierIfNeeded(fTable)
+ // Only add schema prefix if user didn't select a schema and the table is not in the default schema
+ if includeSchemaPrefix && schema != c.defaultSchema {
+ text = c.quotedIdentifierIfNeeded(schema) + "." + text
+ }
m.Insert(base.Candidate{
Type: base.CandidateTypeForeignTable,
- Text: c.quotedIdentifierIfNeeded(fTable),
+ Text: text,
})
}
}
}
-func (m CompletionMap) insertViews(c *Completer, schemas map[string]bool) {
+func (m CompletionMap) insertViewsWithPrefix(c *Completer, schemas map[string]bool, includeSchemaPrefix bool) {
for schema := range schemas {
+ if len(schema) == 0 {
+ continue
+ }
for _, view := range c.listViews(schema) {
+ text := c.quotedIdentifierIfNeeded(view)
+ // Only add schema prefix if user didn't select a schema and the view is not in the default schema
+ if includeSchemaPrefix && schema != c.defaultSchema {
+ text = c.quotedIdentifierIfNeeded(schema) + "." + text
+ }
m.Insert(base.Candidate{
Type: base.CandidateTypeView,
- Text: c.quotedIdentifierIfNeeded(view),
+ Text: text,
})
}
for _, matView := range c.listMaterializedViews(schema) {
+ text := c.quotedIdentifierIfNeeded(matView)
+ // Only add schema prefix if user didn't select a schema and the view is not in the default schema
+ if includeSchemaPrefix && schema != c.defaultSchema {
+ text = c.quotedIdentifierIfNeeded(schema) + "." + text
+ }
m.Insert(base.Candidate{
Type: base.CandidateTypeMaterializedView,
- Text: c.quotedIdentifierIfNeeded(matView),
+ Text: text,
})
}
}
@@ -528,15 +556,25 @@ func (c *Completer) convertCandidates(candidates *base.CandidatesCollection) ([]
if flags&ObjectFlagsShowSecond != 0 {
schemas := make(map[string]bool)
if len(qualifier) == 0 {
- schemas[c.defaultSchema] = true
- // User didn't specify the schema, we need to append cte tables.
+ // User didn't specify the schema
+ // If no default schema is selected by user, search all schemas
+ if c.schemaNotSelected {
+ for _, schema := range c.listAllSchemas() {
+ schemas[schema] = true
+ }
+ } else {
+ schemas[c.defaultSchema] = true
+ }
+ // Also include CTE tables
schemas[""] = true
} else {
schemas[qualifier] = true
}
- tableEntries.insertTables(c, schemas)
- viewEntries.insertViews(c, schemas)
+ // Pass true for includeSchemaPrefix when no schema is selected by user
+ includeSchemaPrefix := len(qualifier) == 0 && c.schemaNotSelected
+ tableEntries.insertTablesWithPrefix(c, schemas, includeSchemaPrefix)
+ viewEntries.insertViewsWithPrefix(c, schemas, includeSchemaPrefix)
}
case pg.PostgreSQLParserRULE_columnref:
schema, table, flags := c.determineColumnRef()
@@ -558,14 +596,23 @@ func (c *Completer) convertCandidates(candidates *base.CandidatesCollection) ([]
}
if len(schema) == 0 {
- schemas[c.defaultSchema] = true
+ // If no default schema is selected by user, search all schemas
+ if c.schemaNotSelected && len(c.references) == 0 {
+ for _, s := range c.listAllSchemas() {
+ schemas[s] = true
+ }
+ } else {
+ schemas[c.defaultSchema] = true
+ }
// User didn't specify the schema, we need to append cte tables.
schemas[""] = true
}
if flags&ObjectFlagsShowTables != 0 {
- tableEntries.insertTables(c, schemas)
- viewEntries.insertViews(c, schemas)
+ // Pass true for includeSchemaPrefix when no schema is selected by user and no references
+ includeSchemaPrefix := len(schema) == 0 && c.schemaNotSelected && len(c.references) == 0
+ tableEntries.insertTablesWithPrefix(c, schemas, includeSchemaPrefix)
+ viewEntries.insertViewsWithPrefix(c, schemas, includeSchemaPrefix)
for _, reference := range c.references {
switch reference := reference.(type) {
diff --git a/backend/plugin/parser/pg/completion_test.go b/backend/plugin/parser/pg/completion_test.go
index 5ebb0154dd..db80ac9a21 100644
--- a/backend/plugin/parser/pg/completion_test.go
+++ b/backend/plugin/parser/pg/completion_test.go
@@ -141,6 +141,37 @@ func getMetadataForTest(_ context.Context, _, databaseName string) (string, *mod
},
},
},
+ {
+ Name: "test",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "auto",
+ Columns: []*storepb.ColumnMetadata{
+ {
+ Name: "id",
+ Type: "int",
+ },
+ {
+ Name: "name",
+ Type: "varchar",
+ },
+ },
+ },
+ {
+ Name: "users",
+ Columns: []*storepb.ColumnMetadata{
+ {
+ Name: "user_id",
+ Type: "int",
+ },
+ {
+ Name: "username",
+ Type: "varchar",
+ },
+ },
+ },
+ },
+ },
},
}, true /* isObjectCaseSensitive */, true /* isDetailCaseSensitive */), nil
}
diff --git a/backend/plugin/parser/pg/test-data/test_completion.yaml b/backend/plugin/parser/pg/test-data/test_completion.yaml
index b1f01dd538..62b46352b4 100644
--- a/backend/plugin/parser/pg/test-data/test_completion.yaml
+++ b/backend/plugin/parser/pg/test-data/test_completion.yaml
@@ -6,6 +6,10 @@
type: SCHEMA
definition: ""
comment: ""
+ - text: test
+ type: SCHEMA
+ definition: ""
+ comment: ""
- text: t1
type: TABLE
definition: ""
@@ -14,6 +18,14 @@
type: TABLE
definition: ""
comment: ""
+ - text: test.auto
+ type: TABLE
+ definition: ""
+ comment: ""
+ - text: test.users
+ type: TABLE
+ definition: ""
+ comment: ""
- text: v1
type: VIEW
definition: ""
@@ -62,6 +74,10 @@
type: SCHEMA
definition: ""
comment: ""
+ - text: test
+ type: SCHEMA
+ definition: ""
+ comment: ""
- text: t1
type: TABLE
definition: ""
@@ -100,6 +116,10 @@
type: SCHEMA
definition: ""
comment: ""
+ - text: test
+ type: SCHEMA
+ definition: ""
+ comment: ""
- text: t1
type: TABLE
definition: ""
@@ -126,6 +146,10 @@
type: SCHEMA
definition: ""
comment: ""
+ - text: test
+ type: SCHEMA
+ definition: ""
+ comment: ""
- text: t1
type: TABLE
definition: ""
@@ -148,6 +172,10 @@
type: SCHEMA
definition: ""
comment: ""
+ - text: test
+ type: SCHEMA
+ definition: ""
+ comment: ""
- text: t1
type: TABLE
definition: ""
@@ -170,6 +198,10 @@
type: SCHEMA
definition: ""
comment: ""
+ - text: test
+ type: SCHEMA
+ definition: ""
+ comment: ""
- text: cc1
type: TABLE
definition: ""
@@ -234,6 +266,10 @@
type: SCHEMA
definition: ""
comment: ""
+ - text: test
+ type: SCHEMA
+ definition: ""
+ comment: ""
- text: t1
type: TABLE
definition: ""
@@ -242,6 +278,14 @@
type: TABLE
definition: ""
comment: ""
+ - text: test.auto
+ type: TABLE
+ definition: ""
+ comment: ""
+ - text: test.users
+ type: TABLE
+ definition: ""
+ comment: ""
- text: v1
type: VIEW
definition: ""
@@ -256,6 +300,10 @@
type: SCHEMA
definition: ""
comment: ""
+ - text: test
+ type: SCHEMA
+ definition: ""
+ comment: ""
- text: t1
type: TABLE
definition: ""
@@ -282,6 +330,10 @@
type: SCHEMA
definition: ""
comment: ""
+ - text: test
+ type: SCHEMA
+ definition: ""
+ comment: ""
- text: t1
type: TABLE
definition: ""
@@ -294,3 +346,81 @@
type: VIEW
definition: ""
comment: ""
+- input: SELECT * FROM |
+ want:
+ - text: public
+ type: SCHEMA
+ definition: ""
+ comment: ""
+ - text: test
+ type: SCHEMA
+ definition: ""
+ comment: ""
+ - text: t1
+ type: TABLE
+ definition: ""
+ comment: ""
+ - text: t2
+ type: TABLE
+ definition: ""
+ comment: ""
+ - text: test.auto
+ type: TABLE
+ definition: ""
+ comment: ""
+ - text: test.users
+ type: TABLE
+ definition: ""
+ comment: ""
+ - text: v1
+ type: VIEW
+ definition: ""
+ comment: ""
+- input: SELECT * FROM test.|
+ want:
+ - text: auto
+ type: TABLE
+ definition: ""
+ comment: ""
+ - text: users
+ type: TABLE
+ definition: ""
+ comment: ""
+- input: SELECT * FROM test.auto WHERE |
+ want:
+ - text: id
+ type: COLUMN
+ definition: test.auto | int, NOT NULL
+ comment: ""
+ - text: name
+ type: COLUMN
+ definition: test.auto | varchar, NOT NULL
+ comment: ""
+ - text: public
+ type: SCHEMA
+ definition: ""
+ comment: ""
+ - text: test
+ type: SCHEMA
+ definition: ""
+ comment: ""
+ - text: auto
+ type: TABLE
+ definition: ""
+ comment: ""
+ - text: t1
+ type: TABLE
+ definition: ""
+ comment: ""
+ - text: t2
+ type: TABLE
+ definition: ""
+ comment: ""
+ - text: users
+ type: TABLE
+ definition: ""
+ comment: ""
+ - text: v1
+ type: VIEW
+ definition: ""
+ comment: ""