fix(query): bound editable metadata preflight across all databases

Refs: #8757
This commit is contained in:
t8y2 committed 2026-10-03 18:26:10 +08:00
1 parent 037d5b1ab8
commit 81850df0d1
2 files changed
+41 -9

No files matched your search

@@ -159,6 +159,22 @@ describe("queryStore hidden primary key editing", () => {
expect(tab.queryEditabilityReason).toBeUndefined(); expect(tab.queryEditabilityReason).toBeUndefined();
}, 10_000); }, 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<never[]>().promise);
listIndexes.mockReturnValue(deferred<never[]>().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 () => { 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 // #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 // server reports `name` while the Oracle-family dialect handling folds the SQL
+25 -9
View File
@@ -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 BACKGROUND_CLIENT_SESSION_SUFFIXES = ["count", "explain", "export"] as const;
const CANCEL_QUERY_TIMEOUT_MS = 10_000; const CANCEL_QUERY_TIMEOUT_MS = 10_000;
const CANCEL_ACK_SETTLE_TIMEOUT_MS = 2_000; const CANCEL_ACK_SETTLE_TIMEOUT_MS = 2_000;
const ORACLE_QUERY_METADATA_PREFLIGHT_BUDGET_MS = 1_000; const QUERY_METADATA_PREFLIGHT_BUDGET_MS = 1_000;
const ORACLE_QUERY_METADATA_PREFLIGHT_TIMEOUT = Symbol("oracle-query-metadata-preflight-timeout"); const QUERY_METADATA_PREFLIGHT_TIMEOUT = Symbol("query-metadata-preflight-timeout");
const SAVED_SQL_EDITOR_POSITION_PERSIST_DELAY_MS = 500; const SAVED_SQL_EDITOR_POSITION_PERSIST_DELAY_MS = 500;
type CloseConfirmContext = "tab" | "batch" | "app"; type CloseConfirmContext = "tab" | "batch" | "app";
@@ -780,13 +780,13 @@ async function withCancelQueryTimeout<T>(promise: Promise<T>): Promise<T> {
} }
} }
async function waitForOracleQueryMetadataPreflight<T>(promise: Promise<T>): Promise<T | typeof ORACLE_QUERY_METADATA_PREFLIGHT_TIMEOUT> { async function waitForQueryMetadataPreflight<T>(promise: Promise<T>): Promise<T | typeof QUERY_METADATA_PREFLIGHT_TIMEOUT> {
let timer: ReturnType<typeof setTimeout> | undefined; let timer: ReturnType<typeof setTimeout> | undefined;
try { try {
return await Promise.race([ return await Promise.race([
promise, promise,
new Promise<typeof ORACLE_QUERY_METADATA_PREFLIGHT_TIMEOUT>((resolve) => { new Promise<typeof QUERY_METADATA_PREFLIGHT_TIMEOUT>((resolve) => {
timer = setTimeout(() => resolve(ORACLE_QUERY_METADATA_PREFLIGHT_TIMEOUT), ORACLE_QUERY_METADATA_PREFLIGHT_BUDGET_MS); timer = setTimeout(() => resolve(QUERY_METADATA_PREFLIGHT_TIMEOUT), QUERY_METADATA_PREFLIGHT_BUDGET_MS);
}), }),
]); ]);
} finally { } finally {
@@ -6276,12 +6276,12 @@ export const useQueryStore = defineStore("query", () => {
void fullMetadataPromise.catch((error) => queryExecutionLog("warn", "metadata:table-prefetch:failed", { traceId, error, elapsed: elapsed() })); 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; const wholeSourceAutoCommit = projectsAllColumnsForSource(target.analysis, target.source.key) && tab.autoCommit !== false;
if (wholeSourceAutoCommit) { if (wholeSourceAutoCommit) {
const indexes = await waitForOracleQueryMetadataPreflight(loadTableIndexes(target.request)); const indexes = await waitForQueryMetadataPreflight(loadTableIndexes(target.request));
if (indexes === ORACLE_QUERY_METADATA_PREFLIGHT_TIMEOUT) { if (indexes === QUERY_METADATA_PREFLIGHT_TIMEOUT) {
queryExecutionLog("info", "metadata:preflight:timeout", { queryExecutionLog("info", "metadata:preflight:timeout", {
traceId, traceId,
table: target.request.tableName, table: target.request.tableName,
budgetMs: ORACLE_QUERY_METADATA_PREFLIGHT_BUDGET_MS, budgetMs: QUERY_METADATA_PREFLIGHT_BUDGET_MS,
elapsed: elapsed(), elapsed: elapsed(),
}); });
return unchanged; return unchanged;
@@ -6291,7 +6291,23 @@ export const useQueryStore = defineStore("query", () => {
loaded = loadedEditableSourceFromMetadata(target, (await fullMetadataPromise).metadata); 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.columns.length === 0) return unchanged;
if (loaded.tableMeta.tableType?.toUpperCase().includes("VIEW")) 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); const columnPrimaryKeys = loaded.tableMeta.columns.filter((column) => column.is_primary_key).map((column) => column.name);