feat(completion): optimize qualified SQL completion queries
This commit is contained in:
1 parent
fe269a28db
commit
3442acd52c
6 files changed
+393
-66
No files matched your search
@@ -582,6 +582,78 @@ describe("connectionStore completion assistant", () => {
|
||||
]);
|
||||
});
|
||||
|
||||
it("keeps PostgreSQL quoted column caches separate from unquoted identifiers", async () => {
|
||||
const completionAssistantSearch = vi.fn(async (request: { parent_name?: string | null; parent_schema?: string | null }) => ({
|
||||
candidates: [
|
||||
{
|
||||
name: request.parent_name === "Orders" ? "QuotedId" : "lower_id",
|
||||
kind: "column",
|
||||
parent_schema: request.parent_schema,
|
||||
parent_name: request.parent_name,
|
||||
data_type: "integer",
|
||||
},
|
||||
],
|
||||
incomplete: false,
|
||||
fallback_used: false,
|
||||
}));
|
||||
|
||||
vi.doMock("@/lib/backend/tauriRuntime", () => ({ isTauriRuntime: () => false }));
|
||||
vi.doMock("@/lib/backend/api", () => ({
|
||||
checkConnectionHealth: vi.fn().mockResolvedValue(undefined),
|
||||
completionAssistantSearch,
|
||||
getColumns: vi.fn(),
|
||||
}));
|
||||
|
||||
const { useConnectionStore } = await import("@/stores/connectionStore");
|
||||
const store = useConnectionStore();
|
||||
store.connections = [postgresConnection()];
|
||||
store.connectedIds.add("pg-1");
|
||||
|
||||
const unquoted = await store.listCompletionColumns("pg-1", "app", "orders", "public", { tableQuoted: false, schemaQuoted: false });
|
||||
const quoted = await store.listCompletionColumns("pg-1", "app", "Orders", "Public", { tableQuoted: true, schemaQuoted: true });
|
||||
|
||||
expect(completionAssistantSearch).toHaveBeenCalledTimes(2);
|
||||
expect(unquoted.map((column) => column.name)).toEqual(["lower_id"]);
|
||||
expect(quoted.map((column) => column.name)).toEqual(["QuotedId"]);
|
||||
expect(store.lookupLocalCompletionColumns("pg-1", "app", "orders", "public", undefined, { tableQuoted: false, schemaQuoted: false }).map((column) => column.name)).toEqual(["lower_id"]);
|
||||
expect(store.lookupLocalCompletionColumns("pg-1", "app", "Orders", "Public", undefined, { tableQuoted: true, schemaQuoted: true }).map((column) => column.name)).toEqual(["QuotedId"]);
|
||||
});
|
||||
|
||||
it("reads PostgreSQL prefix columns back with the same quoted context", async () => {
|
||||
const completionAssistantSearch = vi.fn(async (request: { parent_name?: string | null; parent_schema?: string | null }) => ({
|
||||
candidates: (request.parent_name === "Orders" ? ["QuotedName"] : ["name", "native"]).map((name) => ({
|
||||
name,
|
||||
kind: "column",
|
||||
parent_schema: request.parent_schema,
|
||||
parent_name: request.parent_name,
|
||||
data_type: "text",
|
||||
})),
|
||||
incomplete: false,
|
||||
fallback_used: false,
|
||||
}));
|
||||
|
||||
vi.doMock("@/lib/backend/tauriRuntime", () => ({ isTauriRuntime: () => false }));
|
||||
vi.doMock("@/lib/backend/api", () => ({
|
||||
checkConnectionHealth: vi.fn().mockResolvedValue(undefined),
|
||||
completionAssistantSearch,
|
||||
}));
|
||||
|
||||
const { useConnectionStore } = await import("@/stores/connectionStore");
|
||||
const store = useConnectionStore();
|
||||
store.connections = [postgresConnection()];
|
||||
store.connectedIds.add("pg-1");
|
||||
|
||||
await store.listCompletionColumnsByPrefix("pg-1", "app", "orders", "public", "na", undefined, { tableQuoted: false, schemaQuoted: false });
|
||||
const narrowed = await store.listCompletionColumnsByPrefix("pg-1", "app", "orders", "public", "nam", undefined, { tableQuoted: false, schemaQuoted: false });
|
||||
await store.listCompletionColumnsByPrefix("pg-1", "app", "Orders", "Public", "Na", undefined, { tableQuoted: true, schemaQuoted: true });
|
||||
|
||||
expect(completionAssistantSearch).toHaveBeenCalledTimes(2);
|
||||
expect(narrowed.map((column) => column.name)).toEqual(["name"]);
|
||||
expect(store.lookupLocalCompletionColumnsByPrefix("pg-1", "app", "orders", "public", "na", undefined, { tableQuoted: false, schemaQuoted: false }).map((column) => column.name)).toEqual(["name", "native"]);
|
||||
expect(store.lookupLocalCompletionColumnsByPrefix("pg-1", "app", "orders", "public", "nam", undefined, { tableQuoted: false, schemaQuoted: false }).map((column) => column.name)).toEqual(["name"]);
|
||||
expect(store.lookupLocalCompletionColumnsByPrefix("pg-1", "app", "Orders", "Public", "Na", undefined, { tableQuoted: true, schemaQuoted: true }).map((column) => column.name)).toEqual(["QuotedName"]);
|
||||
});
|
||||
|
||||
it("normalizes unquoted Oracle aliases while preserving quoted case before catalog lookup", async () => {
|
||||
const completionAssistantSearch = vi.fn();
|
||||
const getColumns = vi.fn().mockResolvedValue([]);
|
||||
|
||||
@@ -459,6 +459,7 @@ export const useConnectionStore = defineStore("connection", () => {
|
||||
const completionTableIndex = new Map<string, { touched: number; tables: SqlCompletionTable[] }>();
|
||||
const completionObjectIndex = new Map<string, { touched: number; objects: SqlCompletionObject[] }>();
|
||||
const completionColumnIndex = new Map<string, { touched: number; columns: SqlCompletionColumn[] }>();
|
||||
const completionColumnPrefixIndex = new Map<string, { touched: number; columns: SqlCompletionColumn[]; complete: boolean }>();
|
||||
const completionForeignKeyIndex = new Map<string, { touched: number; foreignKeys: SqlCompletionForeignKey[] }>();
|
||||
const completionInFlight = new Map<string, Promise<unknown>>();
|
||||
const completionCacheRevisions = ref<Record<string, number>>({});
|
||||
@@ -2903,6 +2904,9 @@ export const useConnectionStore = defineStore("connection", () => {
|
||||
for (const key of completionColumnIndex.keys()) {
|
||||
if (key.startsWith(cachePrefix)) completionColumnIndex.delete(key);
|
||||
}
|
||||
for (const key of completionColumnPrefixIndex.keys()) {
|
||||
if (key.startsWith(cachePrefix)) completionColumnPrefixIndex.delete(key);
|
||||
}
|
||||
for (const key of completionForeignKeyIndex.keys()) {
|
||||
if (key.startsWith(cachePrefix)) completionForeignKeyIndex.delete(key);
|
||||
}
|
||||
@@ -6360,12 +6364,21 @@ export const useConnectionStore = defineStore("connection", () => {
|
||||
}
|
||||
|
||||
function completionColumnsKey(connectionId: string, database: string, table: string, schema?: string, catalog?: string, context?: { tableQuoted?: boolean; schemaQuoted?: boolean }): string {
|
||||
if (getConfig(connectionId)?.db_type === "oracle") {
|
||||
const databaseType = getConfig(connectionId)?.db_type;
|
||||
if (databaseType === "oracle") {
|
||||
const normalizedTable = context?.tableQuoted === false ? table.toUpperCase() : table;
|
||||
const normalizedSchema = schema && context?.schemaQuoted === false ? schema.toUpperCase() : (schema ?? "");
|
||||
return `${connectionId}:${database}:${catalog ?? ""}:${normalizedSchema}:${normalizedTable}`;
|
||||
}
|
||||
return `${completionTableScopeKey(connectionId, database, schema, catalog)}:${table.toLowerCase()}`;
|
||||
const baseKey = `${completionTableScopeKey(connectionId, database, schema, catalog)}:${table.toLowerCase()}`;
|
||||
if (databaseType !== "postgres" || (!context?.tableQuoted && !context?.schemaQuoted)) return baseKey;
|
||||
const quotedSchema = context.schemaQuoted ? encodeURIComponent(schema ?? "") : "";
|
||||
const quotedTable = context.tableQuoted ? encodeURIComponent(table) : "";
|
||||
return `${baseKey}:quoted:s=${quotedSchema}:t=${quotedTable}`;
|
||||
}
|
||||
|
||||
function completionColumnPrefixKey(connectionId: string, database: string, table: string, schema: string | undefined, prefix: string, catalog?: string, context?: { tableQuoted?: boolean; schemaQuoted?: boolean }): string {
|
||||
return `${completionColumnsKey(connectionId, database, table, schema, catalog, context)}:prefix:${prefix.trim().toLowerCase()}`;
|
||||
}
|
||||
|
||||
function completionForeignKeysKey(connectionId: string, database: string, table: string, schema?: string): string {
|
||||
@@ -6410,6 +6423,13 @@ export const useConnectionStore = defineStore("connection", () => {
|
||||
removed++;
|
||||
}
|
||||
}
|
||||
for (const key of completionColumnPrefixIndex.keys()) {
|
||||
const prefixMarker = key.toLowerCase().lastIndexOf(":prefix:");
|
||||
const baseKey = prefixMarker >= 0 ? key.slice(0, prefixMarker) : key;
|
||||
if (!matches(baseKey)) continue;
|
||||
completionColumnPrefixIndex.delete(key);
|
||||
removed++;
|
||||
}
|
||||
return removed;
|
||||
}
|
||||
|
||||
@@ -6774,7 +6794,7 @@ export const useConnectionStore = defineStore("connection", () => {
|
||||
requestRevision,
|
||||
);
|
||||
const columns = completionAssistantColumns(response.candidates, table, schema, context);
|
||||
if (columns.length > 0 && requestRevision === completionCacheRevision(connectionId, database)) indexCompletionColumns(connectionId, database, table, schema, columns);
|
||||
if (columns.length > 0 && requestRevision === completionCacheRevision(connectionId, database)) indexCompletionColumns(connectionId, database, table, schema, columns, undefined, context);
|
||||
return columns;
|
||||
}
|
||||
|
||||
@@ -6872,12 +6892,16 @@ export const useConnectionStore = defineStore("connection", () => {
|
||||
}
|
||||
}
|
||||
|
||||
function indexCompletionColumns(connectionId: string, database: string, table: string, schema: string | undefined, columns: SqlCompletionColumn[], catalog?: string) {
|
||||
touchCompletionIndex(completionColumnIndex, completionColumnsKey(connectionId, database, table, schema, catalog), {
|
||||
function indexCompletionColumns(connectionId: string, database: string, table: string, schema: string | undefined, columns: SqlCompletionColumn[], catalog?: string, context?: { tableQuoted?: boolean; schemaQuoted?: boolean }) {
|
||||
touchCompletionIndex(completionColumnIndex, completionColumnsKey(connectionId, database, table, schema, catalog, context), {
|
||||
columns,
|
||||
});
|
||||
}
|
||||
|
||||
function indexCompletionColumnPrefix(connectionId: string, database: string, table: string, schema: string | undefined, prefix: string, columns: SqlCompletionColumn[], complete: boolean, catalog?: string, context?: { tableQuoted?: boolean; schemaQuoted?: boolean }) {
|
||||
touchCompletionIndex(completionColumnPrefixIndex, completionColumnPrefixKey(connectionId, database, table, schema, prefix, catalog, context), { columns, complete });
|
||||
}
|
||||
|
||||
function sqlCompletionForeignKeys(foreignKeys: ForeignKeyInfo[]): SqlCompletionForeignKey[] {
|
||||
return foreignKeys.map((foreignKey) => ({
|
||||
name: foreignKey.name,
|
||||
@@ -6957,10 +6981,75 @@ export const useConnectionStore = defineStore("connection", () => {
|
||||
return completionColumnIndex.get(completionColumnsKey(connectionId, database, table, schema, catalog, context))?.columns ?? [];
|
||||
}
|
||||
|
||||
function completionColumnPrefixEntry(connectionId: string, database: string, table: string, schema: string | undefined, prefix: string, catalog?: string, context?: { tableQuoted?: boolean; schemaQuoted?: boolean }): { columns: SqlCompletionColumn[]; complete: boolean } | undefined {
|
||||
const normalizedPrefix = prefix.trim().toLowerCase();
|
||||
const exact = completionColumnPrefixIndex.get(completionColumnPrefixKey(connectionId, database, table, schema, normalizedPrefix, catalog, context));
|
||||
if (exact) return exact;
|
||||
|
||||
const marker = `${completionColumnsKey(connectionId, database, table, schema, catalog, context)}:prefix:`;
|
||||
let reusable: { prefix: string; columns: SqlCompletionColumn[] } | undefined;
|
||||
for (const [key, entry] of completionColumnPrefixIndex) {
|
||||
if (!entry.complete || !key.startsWith(marker)) continue;
|
||||
const cachedPrefix = key.slice(marker.length);
|
||||
if (!normalizedPrefix.startsWith(cachedPrefix) || (reusable && cachedPrefix.length <= reusable.prefix.length)) continue;
|
||||
reusable = { prefix: cachedPrefix, columns: entry.columns };
|
||||
}
|
||||
if (!reusable) return undefined;
|
||||
return {
|
||||
columns: reusable.columns.filter((column) => column.name.toLowerCase().startsWith(normalizedPrefix)),
|
||||
complete: true,
|
||||
};
|
||||
}
|
||||
|
||||
function lookupLocalCompletionColumnsByPrefix(connectionId: string, database: string, table: string, schema: string | undefined, prefix: string, catalog?: string, context?: { tableQuoted?: boolean; schemaQuoted?: boolean }): SqlCompletionColumn[] {
|
||||
return completionColumnPrefixEntry(connectionId, database, table, schema, prefix, catalog, context)?.columns ?? [];
|
||||
}
|
||||
|
||||
function lookupLocalCompletionForeignKeys(connectionId: string, database: string, table: string, schema?: string): SqlCompletionForeignKey[] {
|
||||
return completionForeignKeyIndex.get(completionForeignKeysKey(connectionId, database, table, schema))?.foreignKeys ?? [];
|
||||
}
|
||||
|
||||
async function listCompletionColumnsByPrefix(connectionId: string, database: string, table: string, schema: string | undefined, prefix: string, catalog?: string, context?: { tableQuoted?: boolean; schemaQuoted?: boolean }): Promise<SqlCompletionColumn[]> {
|
||||
const normalizedPrefix = prefix.trim();
|
||||
if (normalizedPrefix.length < 2) return [];
|
||||
const databaseType = effectiveDatabaseTypeForConnection(getConfig(connectionId));
|
||||
if (databaseType !== "postgres" && databaseType !== "mysql") return [];
|
||||
const completionTable = table;
|
||||
const completionSchema = schema?.trim() || (databaseType === "mysql" ? database : undefined);
|
||||
const cacheKey = completionColumnPrefixKey(connectionId, database, completionTable, completionSchema, normalizedPrefix, catalog, context);
|
||||
const cached = completionColumnPrefixEntry(connectionId, database, completionTable, completionSchema, normalizedPrefix, catalog, context);
|
||||
if (cached) return cached.columns;
|
||||
const requestRevision = completionCacheRevision(connectionId, database);
|
||||
return withCompletionInFlight(
|
||||
`column-prefix:${requestRevision}:${cacheKey}`,
|
||||
async () => {
|
||||
const existing = completionColumnPrefixIndex.get(cacheKey);
|
||||
if (existing) return existing.columns;
|
||||
await ensureConnected(connectionId);
|
||||
const response = await completionAssistantSearch(
|
||||
{
|
||||
connection_id: connectionId,
|
||||
database,
|
||||
schema: completionSchema ?? null,
|
||||
object_kinds: ["column"],
|
||||
mask: normalizedPrefix,
|
||||
max_results: 128,
|
||||
parent_schema: completionSchema ?? null,
|
||||
parent_name: completionTable,
|
||||
match_mode: "prefix",
|
||||
},
|
||||
requestRevision,
|
||||
);
|
||||
const columns = completionAssistantColumns(response.candidates, completionTable, completionSchema, context);
|
||||
if (requestRevision === completionCacheRevision(connectionId, database)) {
|
||||
indexCompletionColumnPrefix(connectionId, database, completionTable, completionSchema, normalizedPrefix, columns, !response.incomplete, catalog, context);
|
||||
}
|
||||
return columns;
|
||||
},
|
||||
{ scope: completionLimiterScope(connectionId, database), kind: "column-prefix" },
|
||||
);
|
||||
}
|
||||
|
||||
function databaseNamesFromTree(connectionId: string): string[] {
|
||||
const node = findConnectionNode(connectionId);
|
||||
if (!node?.children) return [];
|
||||
@@ -7402,13 +7491,14 @@ export const useConnectionStore = defineStore("connection", () => {
|
||||
return [];
|
||||
}
|
||||
const sessionCacheScope = usesOracleCurrentSchema && context?.clientSessionId ? `:${context.clientSessionId}:${context.version ?? 0}` : "";
|
||||
const cacheKey = `${connectionId}:${database}:${catalog ?? ""}:${completionSchema || ""}:${completionTable}${sessionCacheScope}`;
|
||||
const cacheKey = `${completionColumnsKey(connectionId, database, completionTable, completionSchema, catalog, context)}${sessionCacheScope}`;
|
||||
if (!completionColumnsCache.value[cacheKey]) {
|
||||
const requestRevision = completionCacheRevision(connectionId, database);
|
||||
await withCompletionInFlight(
|
||||
`${cacheKey}:columns`,
|
||||
async () => {
|
||||
await ensureConnected(connectionId);
|
||||
// Use assistant metadata opportunistically, then fall back to canonical metadata.
|
||||
if (!usesOracleCurrentSchema && !catalog) {
|
||||
try {
|
||||
const assistantColumns = await listCompletionAssistantColumns(connectionId, database, completionTable, completionSchema, context, requestRevision);
|
||||
@@ -7456,7 +7546,7 @@ export const useConnectionStore = defineStore("connection", () => {
|
||||
isNullable: column.is_nullable,
|
||||
comment: column.comment,
|
||||
}));
|
||||
if (!usesOracleCurrentSchema) indexCompletionColumns(connectionId, database, completionTable, completionSchema, columns, catalog);
|
||||
if (!usesOracleCurrentSchema) indexCompletionColumns(connectionId, database, completionTable, completionSchema, columns, catalog, context);
|
||||
return columns;
|
||||
}
|
||||
|
||||
@@ -8396,6 +8486,7 @@ export const useConnectionStore = defineStore("connection", () => {
|
||||
listCompletionTables,
|
||||
listCompletionObjects,
|
||||
listCompletionColumns,
|
||||
listCompletionColumnsByPrefix,
|
||||
listCompletionForeignKeys,
|
||||
listCompletionSchemas,
|
||||
listCompletionDatabases,
|
||||
@@ -8403,6 +8494,7 @@ export const useConnectionStore = defineStore("connection", () => {
|
||||
lookupLocalCompletionTables,
|
||||
lookupLocalCompletionObjects,
|
||||
lookupLocalCompletionColumns,
|
||||
lookupLocalCompletionColumnsByPrefix,
|
||||
lookupLocalCompletionForeignKeys,
|
||||
lookupLocalCompletionSchemas,
|
||||
lookupLocalCompletionDatabases,
|
||||
|
||||
Reference in new issue
Block a user