From 81850df0d11b7fc1bdaf0ff20ecd08a65e1e57e3 Mon Sep 17 00:00:00 2001 From: t8y2 Date: Sat, 3 Oct 2026 18:26:10 +0800 Subject: [PATCH] fix(query): bound editable metadata preflight across all databases Refs: #8757 --- .../queryStore.hiddenPrimaryKey.spec.ts | 16 +++++++++ apps/desktop/src/stores/queryStore.ts | 34 ++++++++++++++----- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/apps/desktop/src/stores/__tests__/queryStore.hiddenPrimaryKey.spec.ts b/apps/desktop/src/stores/__tests__/queryStore.hiddenPrimaryKey.spec.ts index 7925142ba7..00474b4a7d 100644 --- a/apps/desktop/src/stores/__tests__/queryStore.hiddenPrimaryKey.spec.ts +++ b/apps/desktop/src/stores/__tests__/queryStore.hiddenPrimaryKey.spec.ts @@ -159,6 +159,22 @@ describe("queryStore hidden primary key editing", () => { expect(tab.queryEditabilityReason).toBeUndefined(); }, 10_000); + it("falls back to the original SQL for non-Oracle databases when editability metadata exceeds the preflight budget", async () => { + // 慢元数据:列/索引加载永不在预算内返回,触发对所有数据库统一施加的 1s preflight 预算超时。 + // 泛化前仅 Oracle/Xugu 有此预算,MySQL 等会被慢元数据阻塞到结果显示。 + getColumns.mockReturnValue(deferred().promise); + listIndexes.mockReturnValue(deferred().promise); + + const { useQueryStore } = await import("@/stores/queryStore"); + const store = useQueryStore(); + const tabId = store.createTab("mysql-1", "app", "Query"); + + await store.executeTabSql(tabId, "SELECT name FROM users"); + + // 预算超时后先出结果:执行原始 SQL、未注入隐藏主键(可编辑能力随后台加载补齐)。 + expect(executeMulti).toHaveBeenCalledWith("mysql-1", "app", "SELECT name FROM users", undefined, expect.any(String), expect.objectContaining({ timeoutSecs: 30 })); + }, 10_000); + it("appends the hidden primary key for Dameng tables whose columns keep the created case", async () => { // #10233: Dameng keeps the case an unquoted identifier was created with, so the // server reports `name` while the Oracle-family dialect handling folds the SQL diff --git a/apps/desktop/src/stores/queryStore.ts b/apps/desktop/src/stores/queryStore.ts index 206150f531..a52ad3fd74 100644 --- a/apps/desktop/src/stores/queryStore.ts +++ b/apps/desktop/src/stores/queryStore.ts @@ -168,8 +168,8 @@ const QUERY_RESULT_EXPORT_UNSUPPORTED_ERROR = "Streaming export is unsupported f const BACKGROUND_CLIENT_SESSION_SUFFIXES = ["count", "explain", "export"] as const; const CANCEL_QUERY_TIMEOUT_MS = 10_000; const CANCEL_ACK_SETTLE_TIMEOUT_MS = 2_000; -const ORACLE_QUERY_METADATA_PREFLIGHT_BUDGET_MS = 1_000; -const ORACLE_QUERY_METADATA_PREFLIGHT_TIMEOUT = Symbol("oracle-query-metadata-preflight-timeout"); +const QUERY_METADATA_PREFLIGHT_BUDGET_MS = 1_000; +const QUERY_METADATA_PREFLIGHT_TIMEOUT = Symbol("query-metadata-preflight-timeout"); const SAVED_SQL_EDITOR_POSITION_PERSIST_DELAY_MS = 500; type CloseConfirmContext = "tab" | "batch" | "app"; @@ -780,13 +780,13 @@ async function withCancelQueryTimeout(promise: Promise): Promise { } } -async function waitForOracleQueryMetadataPreflight(promise: Promise): Promise { +async function waitForQueryMetadataPreflight(promise: Promise): Promise { let timer: ReturnType | undefined; try { return await Promise.race([ promise, - new Promise((resolve) => { - timer = setTimeout(() => resolve(ORACLE_QUERY_METADATA_PREFLIGHT_TIMEOUT), ORACLE_QUERY_METADATA_PREFLIGHT_BUDGET_MS); + new Promise((resolve) => { + timer = setTimeout(() => resolve(QUERY_METADATA_PREFLIGHT_TIMEOUT), QUERY_METADATA_PREFLIGHT_BUDGET_MS); }), ]); } finally { @@ -6276,12 +6276,12 @@ export const useQueryStore = defineStore("query", () => { void fullMetadataPromise.catch((error) => queryExecutionLog("warn", "metadata:table-prefetch:failed", { traceId, error, elapsed: elapsed() })); const wholeSourceAutoCommit = projectsAllColumnsForSource(target.analysis, target.source.key) && tab.autoCommit !== false; if (wholeSourceAutoCommit) { - const indexes = await waitForOracleQueryMetadataPreflight(loadTableIndexes(target.request)); - if (indexes === ORACLE_QUERY_METADATA_PREFLIGHT_TIMEOUT) { + const indexes = await waitForQueryMetadataPreflight(loadTableIndexes(target.request)); + if (indexes === QUERY_METADATA_PREFLIGHT_TIMEOUT) { queryExecutionLog("info", "metadata:preflight:timeout", { traceId, table: target.request.tableName, - budgetMs: ORACLE_QUERY_METADATA_PREFLIGHT_BUDGET_MS, + budgetMs: QUERY_METADATA_PREFLIGHT_BUDGET_MS, elapsed: elapsed(), }); return unchanged; @@ -6291,7 +6291,23 @@ export const useQueryStore = defineStore("query", () => { loaded = loadedEditableSourceFromMetadata(target, (await fullMetadataPromise).metadata); } - loaded ??= await loadEditableQuerySource(tab, analysis, source, conn, databaseType, executionDatabase, traceId, elapsed); + if (!loaded) { + // 可编辑性所需的源表元数据加载,对所有数据库统一施加预算上限:慢元数据 + // (如 SQLServer 只读账号、宽表、跨 schema 同义词)超时则先显示查询结果、 + // 放弃本次结果可编辑;loadEditableQuerySource 仍在后台完成加载并写入缓存, + // 下次打开同一查询即可编辑(原先仅 Oracle/Xugu 有预算,其它库会被慢元数据阻塞)。 + const loadedOrTimeout = await waitForQueryMetadataPreflight(loadEditableQuerySource(tab, analysis, source, conn, databaseType, executionDatabase, traceId, elapsed)); + if (loadedOrTimeout === QUERY_METADATA_PREFLIGHT_TIMEOUT) { + queryExecutionLog("info", "metadata:preflight:timeout", { + traceId, + table: target.request.tableName, + budgetMs: QUERY_METADATA_PREFLIGHT_BUDGET_MS, + elapsed: elapsed(), + }); + return unchanged; + } + loaded = loadedOrTimeout; + } if (loaded.tableMeta.columns.length === 0) return unchanged; if (loaded.tableMeta.tableType?.toUpperCase().includes("VIEW")) return unchanged; const columnPrimaryKeys = loaded.tableMeta.columns.filter((column) => column.is_primary_key).map((column) => column.name);