fix(completion): scoped table lookup, preserve casing, lower Oracle table-function boost
* fix: improve schema-qualified SQL completion * fix(completion): preserve casing in completion table cache key and relaxed filter --------- Co-authored-by: zipg <4047349+zipg@users.noreply.github.com> Co-authored-by: t8y2 <1156263951@qq.com>
This commit is contained in:
5 files changed
+51
-9
No files matched your search
@@ -2024,8 +2024,8 @@ async function performAsyncCompletionWithResult(epoch: number, completionContext
|
|||||||
}
|
}
|
||||||
const qualifierDatabase = completionContext.qualifier ? findExactName(databaseNames, completionContext.qualifier) : undefined;
|
const qualifierDatabase = completionContext.qualifier ? findExactName(databaseNames, completionContext.qualifier) : undefined;
|
||||||
const tableLookupDatabase = qualifierDatabase ?? props.database!;
|
const tableLookupDatabase = qualifierDatabase ?? props.database!;
|
||||||
const tableLookupSchema = qualifierDatabase ? undefined : props.schema;
|
const tableLookupSchema = qualifierDatabase ? undefined : completionContext.qualifier && completionContext.suggestTables ? completionContext.qualifier : props.schema;
|
||||||
const tableLookupFilter = completionContext.qualifier && !qualifierDatabase ? completionContext.qualifier : completionContext.prefix;
|
const tableLookupFilter = completionContext.qualifier && completionContext.suggestTables ? completionContext.prefix : completionContext.qualifier && !qualifierDatabase ? completionContext.qualifier : completionContext.prefix;
|
||||||
let tables = shouldLoadTables
|
let tables = shouldLoadTables
|
||||||
? localOnlyMetadata
|
? localOnlyMetadata
|
||||||
? connectionStore.lookupLocalCompletionTables(props.connectionId!, tableLookupDatabase, tableLookupFilter, MAX_COMPLETION_TABLES, tableLookupSchema)
|
? connectionStore.lookupLocalCompletionTables(props.connectionId!, tableLookupDatabase, tableLookupFilter, MAX_COMPLETION_TABLES, tableLookupSchema)
|
||||||
|
|||||||
@@ -271,4 +271,15 @@ describe("sqlCompletion scoped metadata ranking", () => {
|
|||||||
expect(items.length).toBeLessThanOrEqual(200);
|
expect(items.length).toBeLessThanOrEqual(200);
|
||||||
expect(items[0]?.label).toBe("TempTable_000");
|
expect(items[0]?.label).toBe("TempTable_000");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("ranks real Oracle tables before built-in table functions in FROM contexts", () => {
|
||||||
|
const sql = "SELECT * FROM ";
|
||||||
|
const items = buildSqlCompletionItems(sql, sql.length, {
|
||||||
|
databaseType: "oracle",
|
||||||
|
tables: [{ name: "ORDERS_10K", schema: "DBX_TEST", type: "table" }],
|
||||||
|
columnsByTable: new Map(),
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(items.findIndex((item) => item.label === "ORDERS_10K")).toBeLessThan(items.findIndex((item) => item.label === "TABLE"));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
@@ -2729,7 +2729,7 @@ function buildOracleTableFunctionItems(prefix: string): SqlCompletionItem[] {
|
|||||||
.map((item) => ({
|
.map((item) => ({
|
||||||
...item,
|
...item,
|
||||||
type: "function" as const,
|
type: "function" as const,
|
||||||
boost: computeBoost(item.label, prefix) + 2200,
|
boost: computeBoost(item.label, prefix) + 600,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -121,6 +121,32 @@ describe("connectionStore completion assistant", () => {
|
|||||||
expect(dwsTables).toEqual([]);
|
expect(dwsTables).toEqual([]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("preserves table filter casing for assistant searches", async () => {
|
||||||
|
const completionAssistantSearch = vi.fn().mockResolvedValue({
|
||||||
|
candidates: [{ name: "TEST_USERS", kind: "table", schema: "SYSDBA" }],
|
||||||
|
incomplete: false,
|
||||||
|
fallback_used: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
vi.doMock("@/lib/backend/tauriRuntime", () => ({ isTauriRuntime: () => false }));
|
||||||
|
vi.doMock("@/lib/backend/api", () => ({
|
||||||
|
checkConnectionHealth: vi.fn().mockResolvedValue(undefined),
|
||||||
|
completionAssistantSearch,
|
||||||
|
listSchemas: vi.fn().mockResolvedValue(["SYSDBA"]),
|
||||||
|
listTables: vi.fn().mockResolvedValue([]),
|
||||||
|
}));
|
||||||
|
|
||||||
|
const { useConnectionStore } = await import("@/stores/connectionStore");
|
||||||
|
const store = useConnectionStore();
|
||||||
|
store.connections = [postgresConnection()];
|
||||||
|
store.connectedIds.add("pg-1");
|
||||||
|
|
||||||
|
const tables = await store.listCompletionTables("pg-1", "app", "TEST_", 20, "SYSDBA");
|
||||||
|
|
||||||
|
expect(completionAssistantSearch).toHaveBeenCalledWith(expect.objectContaining({ mask: "TEST_", schema: "SYSDBA", parent_schema: "SYSDBA" }));
|
||||||
|
expect(tables).toEqual([{ name: "TEST_USERS", schema: "SYSDBA", type: "table" }]);
|
||||||
|
});
|
||||||
|
|
||||||
it("limits concurrent completion column metadata requests per connection database", async () => {
|
it("limits concurrent completion column metadata requests per connection database", async () => {
|
||||||
const gates = [deferred<any[]>(), deferred<any[]>(), deferred<any[]>(), deferred<any[]>()];
|
const gates = [deferred<any[]>(), deferred<any[]>(), deferred<any[]>(), deferred<any[]>()];
|
||||||
let activeColumns = 0;
|
let activeColumns = 0;
|
||||||
|
|||||||
@@ -3916,9 +3916,14 @@ export const useConnectionStore = defineStore("connection", () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function listCompletionTables(connectionId: string, database: string, filter = "", limit?: number, schema?: string): Promise<SqlCompletionTable[]> {
|
async function listCompletionTables(connectionId: string, database: string, filter = "", limit?: number, schema?: string): Promise<SqlCompletionTable[]> {
|
||||||
const normalizedFilter = filter.trim().toLowerCase();
|
const trimmedFilter = filter.trim();
|
||||||
const relaxedFilter = relaxedCompletionTableFilter(normalizedFilter);
|
const normalizedFilter = trimmedFilter.toLowerCase();
|
||||||
const cacheKey = `${connectionId}:${database}:${normalizedFilter}:${limit ?? ""}:${schema ?? ""}`;
|
// Remote queries (Dameng/Oracle) are case-sensitive, so the cache key must
|
||||||
|
// preserve original casing — otherwise "TEST" and "test" collide and the
|
||||||
|
// 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 ?? ""}`;
|
||||||
if (completionTablesCache.value[cacheKey]) {
|
if (completionTablesCache.value[cacheKey]) {
|
||||||
return completionTablesCache.value[cacheKey];
|
return completionTablesCache.value[cacheKey];
|
||||||
}
|
}
|
||||||
@@ -3932,10 +3937,10 @@ export const useConnectionStore = defineStore("connection", () => {
|
|||||||
if (normalizedFilter || limit) {
|
if (normalizedFilter || limit) {
|
||||||
let results: SqlCompletionTable[] = [];
|
let results: SqlCompletionTable[] = [];
|
||||||
try {
|
try {
|
||||||
results = await listCompletionAssistantTables(connectionId, database, normalizedFilter, limit, schema);
|
results = await listCompletionAssistantTables(connectionId, database, trimmedFilter, limit, schema);
|
||||||
} catch {
|
} catch {
|
||||||
if (schema) {
|
if (schema) {
|
||||||
const tables = await api.listTables(connectionId, database, schema, normalizedFilter, limit);
|
const tables = await api.listTables(connectionId, database, schema, trimmedFilter, limit);
|
||||||
results = tables.map((table) => ({
|
results = tables.map((table) => ({
|
||||||
name: table.name,
|
name: table.name,
|
||||||
schema,
|
schema,
|
||||||
@@ -3983,7 +3988,7 @@ export const useConnectionStore = defineStore("connection", () => {
|
|||||||
return completionTablesCache.value[cacheKey];
|
return completionTablesCache.value[cacheKey];
|
||||||
}
|
}
|
||||||
|
|
||||||
let tables = await api.listTables(connectionId, database, database, normalizedFilter, limit);
|
let tables = await api.listTables(connectionId, database, database, trimmedFilter, limit);
|
||||||
if (tables.length === 0 && relaxedFilter) {
|
if (tables.length === 0 && relaxedFilter) {
|
||||||
tables = await api.listTables(connectionId, database, database, relaxedFilter, expandedCompletionLimit(limit));
|
tables = await api.listTables(connectionId, database, database, relaxedFilter, expandedCompletionLimit(limit));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user