fix(doris): complete external catalog suggestions

This commit is contained in:
t8y2 committed 2026-07-24 14:11:18 +08:00
1 parent 959943315f
commit 6b98c7ebf1
11 files changed
+254 -81

No files matched your search

@@ -49,6 +49,18 @@ function sqlServerConnection(): ConnectionConfig {
} as ConnectionConfig;
}
function dorisConnection(): ConnectionConfig {
return {
...postgresConnection(),
id: "doris-1",
name: "Doris",
db_type: "doris",
port: 9030,
username: "root",
database: "sales",
} as ConnectionConfig;
}
function deferred<T>() {
let resolve!: (value: T) => void;
const promise = new Promise<T>((res) => {
@@ -468,6 +480,72 @@ describe("connectionStore completion assistant", () => {
expect(maxActiveColumns).toBe(2);
});
it("keeps Doris table completion caches isolated by catalog", async () => {
const listTables = vi.fn(async (...args: unknown[]) => {
const catalog = args[7];
return [{ name: catalog ? "external_orders" : "internal_orders", table_type: "BASE TABLE", comment: null }];
});
vi.doMock("@/lib/backend/tauriRuntime", () => ({ isTauriRuntime: () => false }));
vi.doMock("@/lib/backend/api", () => ({
checkConnectionHealth: vi.fn().mockResolvedValue(undefined),
listTables,
}));
const { useConnectionStore } = await import("@/stores/connectionStore");
const store = useConnectionStore();
store.connections = [dorisConnection()];
store.connectedIds.add("doris-1");
const internal = await store.listCompletionTables("doris-1", "sales", "orders", 20);
const external = await store.listCompletionTables("doris-1", "sales", "orders", 20, undefined, false, undefined, "hive_catalog");
expect(listTables).toHaveBeenNthCalledWith(1, "doris-1", "sales", "sales", "orders", 20);
expect(listTables).toHaveBeenNthCalledWith(2, "doris-1", "sales", "", "orders", 20, undefined, undefined, "hive_catalog");
expect(internal).toEqual([{ name: "internal_orders", catalog: undefined, type: "table" }]);
expect(external).toEqual([{ name: "external_orders", catalog: "hive_catalog", type: "table" }]);
expect(store.lookupLocalCompletionTables("doris-1", "sales", "", 20)).toEqual(internal);
expect(store.lookupLocalCompletionTables("doris-1", "sales", "", 20, undefined, "hive_catalog")).toEqual(external);
});
it("passes Doris catalog to column metadata and isolates same-name table caches", async () => {
const completionAssistantSearch = vi.fn().mockResolvedValue({ candidates: [], incomplete: false, fallback_used: false });
const getColumns = vi.fn(async (_connectionId: string, _database: string, _schema: string, _table: string, catalog?: string) => [
{
name: catalog ? "external_id" : "internal_id",
data_type: "BIGINT",
is_nullable: false,
column_default: null,
is_primary_key: true,
extra: null,
comment: null,
},
]);
vi.doMock("@/lib/backend/tauriRuntime", () => ({ isTauriRuntime: () => false }));
vi.doMock("@/lib/backend/api", () => ({
checkConnectionHealth: vi.fn().mockResolvedValue(undefined),
completionAssistantSearch,
getColumns,
}));
const { useConnectionStore } = await import("@/stores/connectionStore");
const store = useConnectionStore();
store.connections = [dorisConnection()];
store.connectedIds.add("doris-1");
const internal = await store.listCompletionColumns("doris-1", "sales", "orders");
const external = await store.listCompletionColumns("doris-1", "sales", "orders", undefined, undefined, "hive_catalog");
expect(completionAssistantSearch).toHaveBeenCalledTimes(1);
expect(getColumns).toHaveBeenNthCalledWith(1, "doris-1", "sales", "sales", "orders", undefined, undefined);
expect(getColumns).toHaveBeenNthCalledWith(2, "doris-1", "sales", "sales", "orders", "hive_catalog", undefined);
expect(internal.map((column) => column.name)).toEqual(["internal_id"]);
expect(external.map((column) => column.name)).toEqual(["external_id"]);
expect(store.lookupLocalCompletionColumns("doris-1", "sales", "orders").map((column) => column.name)).toEqual(["internal_id"]);
expect(store.lookupLocalCompletionColumns("doris-1", "sales", "orders", undefined, "hive_catalog").map((column) => column.name)).toEqual(["external_id"]);
});
it("evicts old completion database entries", async () => {
const listDatabases = vi.fn(async (connectionId: string) => [{ name: `db_${connectionId}` }]);
+53 -37
View File
@@ -3263,7 +3263,7 @@ export const useConnectionStore = defineStore("connection", () => {
const tables = await withMetadataLoadTimeout(connectionId, api.listTables(connectionId, database, "", searchFilter, fetchLimit, fetchOffset, objectTypesForScope, catalog), "tables");
const hasMore = searchFilter ? false : tables.length > pageSize;
const pageTables = hasMore ? tables.slice(0, pageSize) : tables;
indexCompletionTables(connectionId, database, undefined, tableInfosToCompletionTables(pageTables, undefined));
indexCompletionTables(connectionId, database, undefined, tableInfosToCompletionTables(pageTables, undefined), catalog);
let children = buildTableTreeNodes({
nodeId: node.id,
connectionId,
@@ -4199,8 +4199,12 @@ export const useConnectionStore = defineStore("connection", () => {
return `${connectionId}:${database}:${schema?.toLowerCase() ?? ""}`;
}
function completionColumnsKey(connectionId: string, database: string, table: string, schema?: string): string {
return `${completionScopeKey(connectionId, database, schema)}:${table.toLowerCase()}`;
function completionTableScopeKey(connectionId: string, database: string, schema?: string, catalog?: string): string {
return `${connectionId}:${database}:${catalog?.toLowerCase() ?? ""}:${schema?.toLowerCase() ?? ""}`;
}
function completionColumnsKey(connectionId: string, database: string, table: string, schema?: string, catalog?: string): string {
return `${completionTableScopeKey(connectionId, database, schema, catalog)}:${table.toLowerCase()}`;
}
function completionForeignKeysKey(connectionId: string, database: string, table: string, schema?: string): string {
@@ -4453,13 +4457,14 @@ export const useConnectionStore = defineStore("connection", () => {
return tableMatchScore(tableLike, filter, preferredSchema);
}
function indexCompletionTables(connectionId: string, database: string, schema: string | undefined, tables: SqlCompletionTable[]) {
function indexCompletionTables(connectionId: string, database: string, schema: string | undefined, tables: SqlCompletionTable[], catalog?: string) {
const groups = new Map<string, SqlCompletionTable[]>();
for (const table of tables) {
const tableSchema = table.schema ?? schema;
const key = completionScopeKey(connectionId, database, tableSchema);
const tableCatalog = table.catalog ?? catalog;
const key = completionTableScopeKey(connectionId, database, tableSchema, tableCatalog);
const list = groups.get(key) ?? [];
list.push({ ...table, schema: tableSchema });
list.push({ ...table, catalog: tableCatalog, schema: tableSchema });
groups.set(key, list);
}
for (const [key, group] of groups) {
@@ -4487,8 +4492,8 @@ export const useConnectionStore = defineStore("connection", () => {
}
}
function indexCompletionColumns(connectionId: string, database: string, table: string, schema: string | undefined, columns: SqlCompletionColumn[]) {
touchCompletionIndex(completionColumnIndex, completionColumnsKey(connectionId, database, table, schema), {
function indexCompletionColumns(connectionId: string, database: string, table: string, schema: string | undefined, columns: SqlCompletionColumn[], catalog?: string) {
touchCompletionIndex(completionColumnIndex, completionColumnsKey(connectionId, database, table, schema, catalog), {
columns,
});
}
@@ -4509,11 +4514,12 @@ export const useConnectionStore = defineStore("connection", () => {
});
}
function lookupLocalCompletionTables(connectionId: string, database: string, filter = "", limit?: number, schema?: string): SqlCompletionTable[] {
const allScopes = [...completionTableIndex.entries()].filter(([key]) => key.startsWith(`${connectionId}:${database}:`)).map(([, entry]) => entry);
const preferred = schema ? completionTableIndex.get(completionScopeKey(connectionId, database, schema)) : undefined;
function lookupLocalCompletionTables(connectionId: string, database: string, filter = "", limit?: number, schema?: string, catalog?: string): SqlCompletionTable[] {
const scopePrefix = `${connectionId}:${database}:${catalog?.toLowerCase() ?? ""}:`;
const allScopes = [...completionTableIndex.entries()].filter(([key]) => key.startsWith(scopePrefix)).map(([, entry]) => entry);
const preferred = schema ? completionTableIndex.get(completionTableScopeKey(connectionId, database, schema, catalog)) : undefined;
const scopes = schema ? (preferred ? [preferred] : []) : allScopes;
const treeTables = completionTablesFromTree(treeNodes.value, connectionId, database, schema);
const treeTables = completionTablesFromTree(treeNodes.value, connectionId, database, schema, catalog);
const ranked = scopes
.flatMap((entry) => entry?.tables ?? [])
.concat(treeTables)
@@ -4567,8 +4573,8 @@ export const useConnectionStore = defineStore("connection", () => {
return result;
}
function lookupLocalCompletionColumns(connectionId: string, database: string, table: string, schema?: string): SqlCompletionColumn[] {
return completionColumnIndex.get(completionColumnsKey(connectionId, database, table, schema))?.columns ?? [];
function lookupLocalCompletionColumns(connectionId: string, database: string, table: string, schema?: string, catalog?: string): SqlCompletionColumn[] {
return completionColumnIndex.get(completionColumnsKey(connectionId, database, table, schema, catalog))?.columns ?? [];
}
function lookupLocalCompletionForeignKeys(connectionId: string, database: string, table: string, schema?: string): SqlCompletionForeignKey[] {
@@ -4686,7 +4692,12 @@ export const useConnectionStore = defineStore("connection", () => {
});
}
async function listCompletionTables(connectionId: string, database: string, filter = "", limit?: number, schema?: string, globalSearch = false, currentSchema?: string): Promise<SqlCompletionTable[]> {
function listCompletionTableMetadata(connectionId: string, database: string, schema: string, filter?: string, limit?: number, catalog?: string): Promise<TableInfo[]> {
if (catalog) return api.listTables(connectionId, database, schema, filter, limit, undefined, undefined, catalog);
return api.listTables(connectionId, database, schema, filter, limit);
}
async function listCompletionTables(connectionId: string, database: string, filter = "", limit?: number, schema?: string, globalSearch = false, currentSchema?: string, catalog?: string): Promise<SqlCompletionTable[]> {
const trimmedFilter = filter.trim();
const normalizedFilter = trimmedFilter.toLowerCase();
// Remote queries (Dameng/Oracle) are case-sensitive, so the cache key must
@@ -4694,7 +4705,7 @@ export const useConnectionStore = defineStore("connection", () => {
// second lookup returns the first's stale results. Local lookups below stay
// case-insensitive because tableMatchScore normalizes internally.
const relaxedFilter = relaxedCompletionTableFilter(trimmedFilter);
const cacheKey = `${connectionId}:${database}:${trimmedFilter}:${limit ?? ""}:${schema ?? ""}:${globalSearch ? "global" : "scoped"}:${currentSchema ?? ""}`;
const cacheKey = `${connectionId}:${database}:${catalog ?? ""}:${trimmedFilter}:${limit ?? ""}:${schema ?? ""}:${globalSearch ? "global" : "scoped"}:${currentSchema ?? ""}`;
if (completionTablesCache.value[cacheKey]) {
return completionTablesCache.value[cacheKey];
}
@@ -4711,14 +4722,15 @@ export const useConnectionStore = defineStore("connection", () => {
results = await listCompletionAssistantTables(connectionId, database, trimmedFilter, limit, schema, globalSearch, currentSchema);
} catch {
if (schema) {
const tables = await api.listTables(connectionId, database, schema, trimmedFilter, limit);
const tables = await listCompletionTableMetadata(connectionId, database, schema, trimmedFilter, limit, catalog);
results = tables.map((table) => ({
name: table.name,
catalog,
schema,
type: sqlObjectNavigationTypeFromTableType(table.table_type),
}));
} else {
results = lookupLocalCompletionTables(connectionId, database, normalizedFilter, limit);
results = lookupLocalCompletionTables(connectionId, database, normalizedFilter, limit, undefined, catalog);
}
}
if (results.length === 0 && relaxedFilter) {
@@ -4730,9 +4742,10 @@ export const useConnectionStore = defineStore("connection", () => {
}
} else if (schema) {
try {
const tables = await api.listTables(connectionId, database, schema, relaxedFilter, expandedCompletionLimit(limit));
const tables = await listCompletionTableMetadata(connectionId, database, schema, relaxedFilter, expandedCompletionLimit(limit), catalog);
results = tables.map((table) => ({
name: table.name,
catalog,
schema,
type: sqlObjectNavigationTypeFromTableType(table.table_type),
}));
@@ -4740,41 +4753,44 @@ export const useConnectionStore = defineStore("connection", () => {
results = [];
}
} else {
results = lookupLocalCompletionTables(connectionId, database, relaxedFilter, expandedCompletionLimit(limit));
results = lookupLocalCompletionTables(connectionId, database, relaxedFilter, expandedCompletionLimit(limit), undefined, catalog);
}
}
const limitedTables = limit ? dedupeCompletionTables(results).slice(0, limit) : results;
completionTablesCache.value[cacheKey] = limitedTables;
indexCompletionTables(connectionId, database, undefined, limitedTables);
indexCompletionTables(connectionId, database, undefined, limitedTables, catalog);
evictOldestCacheEntries(completionTablesCache.value, COMPLETION_CACHE_MAX);
return completionTablesCache.value[cacheKey];
}
if (schema) {
const tables = await api.listTables(connectionId, database, schema);
const tables = await listCompletionTableMetadata(connectionId, database, schema, undefined, undefined, catalog);
completionTablesCache.value[cacheKey] = tables.map((table) => ({
name: table.name,
catalog,
schema,
type: sqlObjectNavigationTypeFromTableType(table.table_type),
}));
} else {
completionTablesCache.value[cacheKey] = lookupLocalCompletionTables(connectionId, database, normalizedFilter, limit);
completionTablesCache.value[cacheKey] = lookupLocalCompletionTables(connectionId, database, normalizedFilter, limit, undefined, catalog);
}
indexCompletionTables(connectionId, database, undefined, completionTablesCache.value[cacheKey]);
indexCompletionTables(connectionId, database, undefined, completionTablesCache.value[cacheKey], catalog);
evictOldestCacheEntries(completionTablesCache.value, COMPLETION_CACHE_MAX);
return completionTablesCache.value[cacheKey];
}
let tables = await api.listTables(connectionId, database, database, trimmedFilter, limit);
const querySchema = catalog ? "" : database;
let tables = await listCompletionTableMetadata(connectionId, database, querySchema, trimmedFilter, limit, catalog);
if (tables.length === 0 && relaxedFilter) {
tables = await api.listTables(connectionId, database, database, relaxedFilter, expandedCompletionLimit(limit));
tables = await listCompletionTableMetadata(connectionId, database, querySchema, relaxedFilter, expandedCompletionLimit(limit), catalog);
}
completionTablesCache.value[cacheKey] = tables.map((table) => ({
name: table.name,
catalog,
type: sqlObjectNavigationTypeFromTableType(table.table_type),
}));
completionTablesCache.value[cacheKey] = limit ? completionTablesCache.value[cacheKey].slice(0, limit) : completionTablesCache.value[cacheKey];
indexCompletionTables(connectionId, database, schema, completionTablesCache.value[cacheKey]);
indexCompletionTables(connectionId, database, schema, completionTablesCache.value[cacheKey], catalog);
evictOldestCacheEntries(completionTablesCache.value, COMPLETION_CACHE_MAX);
return completionTablesCache.value[cacheKey];
},
@@ -4796,7 +4812,7 @@ export const useConnectionStore = defineStore("connection", () => {
const indexByKey = new Map<string, number>();
const deduped: SqlCompletionTable[] = [];
for (const table of tables) {
const key = `${table.schema ?? ""}.${table.name}`.toLowerCase();
const key = `${table.catalog ?? ""}.${table.schema ?? ""}.${table.name}`.toLowerCase();
const existingIndex = indexByKey.get(key);
if (existingIndex != null) {
const existing = deduped[existingIndex];
@@ -4907,7 +4923,7 @@ export const useConnectionStore = defineStore("connection", () => {
return deduped;
}
async function listCompletionColumns(connectionId: string, database: string, table: string, schema?: string, context?: { clientSessionId?: string; version?: number }): Promise<SqlCompletionColumn[]> {
async function listCompletionColumns(connectionId: string, database: string, table: string, schema?: string, context?: { clientSessionId?: string; version?: number }, catalog?: string): Promise<SqlCompletionColumn[]> {
const config = getConfig(connectionId);
const completionSchema = schema?.trim() || undefined;
const usesOracleCurrentSchema = config?.db_type === "oracle" && !completionSchema;
@@ -4915,13 +4931,13 @@ export const useConnectionStore = defineStore("connection", () => {
return [];
}
const sessionCacheScope = usesOracleCurrentSchema && context?.clientSessionId ? `:${context.clientSessionId}:${context.version ?? 0}` : "";
const cacheKey = `${connectionId}:${database}:${schema || ""}:${table}${sessionCacheScope}`;
const cacheKey = `${connectionId}:${database}:${catalog ?? ""}:${schema || ""}:${table}${sessionCacheScope}`;
if (!completionColumnsCache.value[cacheKey]) {
await withCompletionInFlight(
`${cacheKey}:columns`,
async () => {
await ensureConnected(connectionId);
if (!usesOracleCurrentSchema) {
if (!usesOracleCurrentSchema && !catalog) {
try {
const assistantColumns = await listCompletionAssistantColumns(connectionId, database, table, completionSchema);
if (assistantColumns.length > 0) {
@@ -4945,7 +4961,7 @@ export const useConnectionStore = defineStore("connection", () => {
}
}
const querySchema = usesOracleCurrentSchema ? "" : metadataQuerySchema(connectionId, database, completionSchema);
completionColumnsCache.value[cacheKey] = await api.getColumns(connectionId, database, querySchema, table, undefined, usesOracleCurrentSchema ? context?.clientSessionId : undefined);
completionColumnsCache.value[cacheKey] = await api.getColumns(connectionId, database, querySchema, table, catalog, usesOracleCurrentSchema ? context?.clientSessionId : undefined);
evictOldestCacheEntries(completionColumnsCache.value, COMPLETION_CACHE_MAX);
},
{ scope: completionLimiterScope(connectionId, database), kind: "columns" },
@@ -4960,7 +4976,7 @@ export const useConnectionStore = defineStore("connection", () => {
isNullable: column.is_nullable,
comment: column.comment,
}));
if (!usesOracleCurrentSchema) indexCompletionColumns(connectionId, database, table, schema, columns);
if (!usesOracleCurrentSchema) indexCompletionColumns(connectionId, database, table, schema, columns, catalog);
return columns;
}
@@ -4990,8 +5006,8 @@ export const useConnectionStore = defineStore("connection", () => {
return foreignKeys;
}
function refreshCompletionTables(connectionId: string, database: string, filter = "", limit?: number, schema?: string, globalSearch = false, currentSchema?: string): Promise<SqlCompletionTable[]> {
return listCompletionTables(connectionId, database, filter, limit, schema, globalSearch, currentSchema);
function refreshCompletionTables(connectionId: string, database: string, filter = "", limit?: number, schema?: string, globalSearch = false, currentSchema?: string, catalog?: string): Promise<SqlCompletionTable[]> {
return listCompletionTables(connectionId, database, filter, limit, schema, globalSearch, currentSchema, catalog);
}
function refreshCompletionObjects(connectionId: string, database: string, filter = "", limit?: number, schema?: string, parentName?: string, globalSearch = false, currentSchema?: string): Promise<SqlCompletionObject[]> {
@@ -5006,8 +5022,8 @@ export const useConnectionStore = defineStore("connection", () => {
return listCompletionDatabases(connectionId);
}
function refreshCompletionColumns(connectionId: string, database: string, table: string, schema?: string, context?: { clientSessionId?: string; version?: number }): Promise<SqlCompletionColumn[]> {
return listCompletionColumns(connectionId, database, table, schema, context);
function refreshCompletionColumns(connectionId: string, database: string, table: string, schema?: string, context?: { clientSessionId?: string; version?: number }, catalog?: string): Promise<SqlCompletionColumn[]> {
return listCompletionColumns(connectionId, database, table, schema, context, catalog);
}
function refreshCompletionForeignKeys(connectionId: string, database: string, table: string, schema?: string): Promise<SqlCompletionForeignKey[]> {