fix(db2): defer LOB loading in query results

Refs: #10723
This commit is contained in:
t8y2 committed 2026-10-03 23:55:40 +08:00
1 parent aa2fe831b6
commit da1094df93
11 files changed
+505 -35

No files matched your search

@@ -1356,6 +1356,71 @@ describe("queryStore hidden primary key editing", () => {
expect(executeMulti).toHaveBeenCalledWith("oracle-1", "ORCL", 'SELECT PAYLOAD, "ID" AS "__DBX_PK_0" FROM APP.DOCUMENTS', undefined, expect.any(String), expect.objectContaining({ tableDataPreview: true, timeoutSecs: 30 }));
});
it("defers DB2 BLOB materialization for a stable-key star query", async () => {
getConnectionConfig.mockReturnValue({ id: "db2-1", name: "DB2", db_type: "db2", database: "MAXIMO", query_timeout_secs: 60 });
getColumns.mockResolvedValue([
{ name: "MAFAPPDATAID", data_type: "BIGINT", is_nullable: false, column_default: null, is_primary_key: true, extra: null },
{ name: "APP", data_type: "BLOB", is_nullable: true, column_default: null, is_primary_key: false, extra: null },
]);
listIndexes.mockResolvedValue([{ name: "MAFAPPDATA_PK", columns: ["MAFAPPDATAID"], is_unique: true, is_primary: true }]);
analyzeEditableQueryEditability.mockResolvedValue({
editable: true,
analysis: {
schema: undefined,
tableName: "MAFAPPDATA",
tableAlias: "mf",
selectStar: true,
columns: [],
},
});
executeMulti.mockResolvedValue([
{
columns: ["MAFAPPDATAID", "APP"],
rows: [[1, "<BLOB>"]],
affected_rows: 0,
execution_time_ms: 57,
large_value_cells: [{ row_index: 0, column_index: 1, original_bytes: 64 * 1024 * 1024 }],
},
]);
const { useQueryStore } = await import("@/stores/queryStore");
const store = useQueryStore();
const tabId = store.createTab("db2-1", "MAXIMO", "Query", "query", "MAXIMO");
store.setAutoCommit(tabId, true);
await store.executeTabSql(tabId, "select * from MAFAPPDATA mf");
expect(executeMulti).toHaveBeenCalledWith("db2-1", "MAXIMO", "select * from MAFAPPDATA mf", "MAXIMO", expect.any(String), expect.objectContaining({ tableDataPreview: true, timeoutSecs: 60 }));
expect(store.tabs.find((tab) => tab.id === tabId)?.result?.large_value_cells).toEqual([{ row_index: 0, column_index: 1, original_bytes: 64 * 1024 * 1024 }]);
});
it("keeps deferred DB2 LOB handling disabled without a stable key", async () => {
getConnectionConfig.mockReturnValue({ id: "db2-1", name: "DB2", db_type: "db2", database: "MAXIMO", query_timeout_secs: 60 });
getColumns.mockResolvedValue([{ name: "APP", data_type: "BLOB", is_nullable: true, column_default: null, is_primary_key: false, extra: null }]);
listIndexes.mockResolvedValue([]);
analyzeEditableQueryEditability.mockResolvedValue({
editable: true,
analysis: {
schema: undefined,
tableName: "MAFAPPDATA",
tableAlias: "mf",
selectStar: true,
columns: [],
},
});
executeMulti.mockResolvedValue([{ columns: ["APP"], rows: [["0x01"]], affected_rows: 0, execution_time_ms: 1 }]);
const { useQueryStore } = await import("@/stores/queryStore");
const store = useQueryStore();
const tabId = store.createTab("db2-1", "MAXIMO", "Query", "query", "MAXIMO");
store.setAutoCommit(tabId, true);
await store.executeTabSql(tabId, "select * from MAFAPPDATA mf");
expect(executeMulti).toHaveBeenCalledOnce();
expect(executeMulti.mock.calls[0]?.[5]).not.toHaveProperty("tableDataPreview");
});
it("keeps deferred Oracle LOBs disabled for views", async () => {
getConnectionConfig.mockReturnValue({ id: "oracle-1", name: "Oracle", db_type: "oracle", database: "ORCL", query_timeout_secs: 30 });
getColumns.mockResolvedValue([
@@ -714,6 +714,59 @@ describe("queryStore table data refresh", () => {
);
});
it("enables bounded DB2 BLOB previews for table refreshes", async () => {
mocks.getConnectionConfig.mockReturnValue({
id: "db2-1",
name: "DB2",
db_type: "db2",
database: "MAXIMO",
query_timeout_secs: 60,
});
mocks.buildTableSelectSql.mockResolvedValue('SELECT "MAFAPPDATAID", SUBSTR("APP", 1, 8193) AS "APP" FROM "MAXIMO"."MAFAPPDATA";');
const { useQueryStore } = await import("@/stores/queryStore");
const store = useQueryStore();
const tabId = store.createTab("db2-1", "MAXIMO", "MAFAPPDATA", "data", "MAXIMO");
store.setTableMeta(tabId, {
database: "MAXIMO",
schema: "MAXIMO",
tableName: "MAFAPPDATA",
tableType: "TABLE",
columns: [
{ name: "MAFAPPDATAID", data_type: "BIGINT", is_nullable: false, column_default: null, is_primary_key: true, extra: null },
{ name: "APP", data_type: "BLOB", is_nullable: true, column_default: null, is_primary_key: false, extra: null },
],
primaryKeys: ["MAFAPPDATAID"],
});
const tab = store.tabs.find((candidate) => candidate.id === tabId)!;
tab.resultPageLimit = 100;
tab.resultPageOffset = 0;
await expect(store.refreshDataTab(tabId)).resolves.toBe(true);
expect(mocks.buildTableSelectSql).toHaveBeenCalledWith(
expect.objectContaining({
databaseType: "db2",
columnTypes: ["BIGINT", "BLOB"],
largeValuePreviewSize: 8 * 1024,
}),
);
expect(mocks.executeMulti).toHaveBeenCalledWith(
"db2-1",
"MAXIMO",
expect.any(String),
undefined,
expect.any(String),
expect.objectContaining({
maxRows: 100,
fetchSize: 100,
maxResultBytes: 32 * 1024 * 1024,
resultKeyColumns: ["MAFAPPDATAID"],
tableDataPreview: true,
timeoutSecs: 60,
}),
);
});
it("keeps a MySQL table refresh unqualified in the selected database context", async () => {
mocks.getConnectionConfig.mockReturnValue({
id: "mysql-1",
+31 -20
View File
@@ -95,7 +95,7 @@ import { queryResultNameFromPreamble, queryResultSourceLabel, queryResultSourceN
import { sqlServerCountUsesLocalTempTable } from "@/lib/query/queryResultCountSession";
import { stripPaginationRowNumber } from "@/lib/query/queryPaginationResult";
import { beginDataGridNativeSelectionBlock, finishDataGridNativeSelectionBlock } from "@/lib/dataGrid/dataGridNativeSelection";
import { appendLargeValueCells, canUseTableDataLargeValuePreview, remapLargeValueCells, tableDataLargeValuePreviewOptions, TABLE_DATA_RESULT_MAX_BYTES } from "@/lib/dataGrid/dataGridLargeValues";
import { appendLargeValueCells, canUseTableDataLargeValuePreview, remapLargeValueCells, supportsTableDataLargeValuePreview, tableDataLargeValuePreviewOptions, TABLE_DATA_RESULT_MAX_BYTES } from "@/lib/dataGrid/dataGridLargeValues";
import { simpleDataGridOrderByReferencesMissingColumn, sortDataGridRowIndexes, type DataGridSortDirection } from "@/lib/dataGrid/dataGridSort";
import { normalizeResultPageSize } from "@/lib/dataGrid/paginationPageSize";
import { agentProtocolQueryResultMaxRows, capQueryResultTotal, effectiveQueryResultMaxRows, limitQueryPagination, queryResultLimitReached } from "@/lib/dataGrid/queryResultRowLimit";
@@ -148,6 +148,7 @@ const QUERY_SURFACE_ACTIVATION_EVENT = "dbx:activate-query-surface";
const ORACLE_LIKE_METADATA_TYPES = new Set<string>(["oracle", "dameng", "oceanbase-oracle"]);
const ORACLE_DEFERRED_LOB_TYPES = new Set<string>(["CLOB", "NCLOB", "BLOB", "BFILE", "XMLTYPE", "SYS.XMLTYPE"]);
const DB2_DEFERRED_LOB_TYPES = new Set<string>(["CLOB", "DBCLOB", "BLOB"]);
// Bounded concurrency for grouped-query display column loads, scoped per
// connection so different connections never block each other. Matches the
@@ -163,7 +164,7 @@ const UPPERCASE_FOLDED_METADATA_TYPES = new Set<string>([...ORACLE_LIKE_METADATA
// OceanBase Oracle mode ride the Oracle adapter and HighGo rides the PostgreSQL one
// (issue #10233: without this, a query that omits the primary key stays read-only
// even though the table has one).
const HIDDEN_QUERY_KEY_DATABASE_TYPES = new Set<DatabaseType>(["mysql", "postgres", "sqlserver", "oracle", "xugu", "dameng", "highgo", "oceanbase-oracle"]);
const HIDDEN_QUERY_KEY_DATABASE_TYPES = new Set<DatabaseType>(["mysql", "postgres", "sqlserver", "oracle", "xugu", "dameng", "highgo", "oceanbase-oracle", "db2"]);
const QUERY_RESULT_EXPORT_UNSUPPORTED_ERROR = "Streaming export is unsupported for this query. Simplify it or use a supported driver.";
const BACKGROUND_CLIENT_SESSION_SUFFIXES = ["count", "explain", "export"] as const;
const CANCEL_QUERY_TIMEOUT_MS = 10_000;
@@ -838,14 +839,23 @@ function projectsAllColumnsForSource(analysis: EditableQueryInfo, sourceKey: str
return analysis.selectStar || analysis.columns.some((column) => column.star && (!column.sourceKey || column.sourceKey === sourceKey));
}
function oracleQueryProjectsDeferredLob(analysis: EditableQueryInfo, sourceKey: string, columns: readonly { name: string; data_type: string }[]): boolean {
const deferredColumns = new Set(columns.filter((column) => ORACLE_DEFERRED_LOB_TYPES.has(column.data_type.trim().toUpperCase())).map((column) => column.name.toLowerCase()));
function queryProjectsDeferredLob(databaseType: DatabaseType, analysis: EditableQueryInfo, sourceKey: string, columns: readonly { name: string; data_type: string }[]): boolean {
const deferredTypes = databaseType === "oracle" ? ORACLE_DEFERRED_LOB_TYPES : databaseType === "db2" ? DB2_DEFERRED_LOB_TYPES : undefined;
if (!deferredTypes) return false;
const deferredColumns = new Set(
columns
.filter((column) => {
const normalized = column.data_type.trim().toUpperCase();
return deferredTypes.has(normalized) || deferredTypes.has(normalized.split("(", 1)[0]!.trim());
})
.map((column) => column.name.toLowerCase()),
);
if (deferredColumns.size === 0) return false;
if (projectsAllColumnsForSource(analysis, sourceKey)) return true;
return analysis.columns.some((column) => column.sourceName && column.sourceKey === sourceKey && deferredColumns.has(column.sourceName.toLowerCase()));
}
function oracleColumnsAllowDeferredLobMarkers(columns: readonly { name: string }[]): boolean {
function columnsAllowDeferredLobMarkers(columns: readonly { name: string }[]): boolean {
return !columns.some((column) => column.name.toUpperCase().startsWith("__DBX_LARGE_VALUE_BYTES_"));
}
@@ -5961,7 +5971,7 @@ export const useQueryStore = defineStore("query", () => {
sql: string;
metadataSql: string;
hiddenPrimaryKeys: HiddenPrimaryKeyProjection[];
oracleLobPreview: boolean;
largeValuePreview: boolean;
}
function oracleCompletionTableType(tab: QueryTab, metadataDbType: string, database: string, schema: string, tableName: string, catalog?: string): string | undefined {
@@ -6215,8 +6225,9 @@ export const useQueryStore = defineStore("query", () => {
function buildHiddenPrimaryKeyPreparation(tab: QueryTab, sql: string, databaseType: DatabaseType, loaded: LoadedEditableSource, primaryKeys: string[], declaredPrimaryKeys: string[], traceId: string, elapsed: () => string): EditableQueryExecutionPreparation {
const metadataAnalysis = expandStarProjectionColumnsForSource(bindColumnsForSource(databaseType, loaded.analysis, loaded.source, loaded.tableMeta.columns), loaded.source, loaded.tableMeta.columns);
const oracleLobPreview = databaseType === "oracle" && primaryKeys.length > 0 && oracleRowIdIsSafeForQuery(tab, loaded) && oracleColumnsAllowDeferredLobMarkers(loaded.tableMeta.columns) && oracleQueryProjectsDeferredLob(metadataAnalysis, loaded.source.key, loaded.tableMeta.columns);
const unchanged = { sql, metadataSql: sql, hiddenPrimaryKeys: [], oracleLobPreview };
const stableLobSource = databaseType === "oracle" ? oracleRowIdIsSafeForQuery(tab, loaded) : databaseType === "db2";
const largeValuePreview = (databaseType === "oracle" || databaseType === "db2") && primaryKeys.length > 0 && stableLobSource && columnsAllowDeferredLobMarkers(loaded.tableMeta.columns) && queryProjectsDeferredLob(databaseType, metadataAnalysis, loaded.source.key, loaded.tableMeta.columns);
const unchanged = { sql, metadataSql: sql, hiddenPrimaryKeys: [], largeValuePreview };
const missingPrimaryKeys =
declaredPrimaryKeys.length === 0
? primaryKeys.filter((primaryKey) => !(databaseType === "oracle" && primaryKey === DBX_ROWID_COLUMN && metadataAnalysis.columns.some((column) => column.sourceKey === loaded.source.key && !column.sourceNameQuoted && column.sourceName?.toUpperCase() === "ROWID")))
@@ -6240,11 +6251,11 @@ export const useQueryStore = defineStore("query", () => {
keyCount: rewritten.projections.length,
elapsed: elapsed(),
});
return { sql: rewritten.sql, metadataSql: rewritten.sql, hiddenPrimaryKeys: rewritten.projections, oracleLobPreview };
return { sql: rewritten.sql, metadataSql: rewritten.sql, hiddenPrimaryKeys: rewritten.projections, largeValuePreview };
}
async function prepareEditableQueryExecution(tab: QueryTab, sql: string, conn: ConnectionConfig | undefined, databaseType: DatabaseType | undefined, executionDatabase: string, traceId: string, elapsed: () => string): Promise<EditableQueryExecutionPreparation> {
const unchanged = { sql, metadataSql: sql, hiddenPrimaryKeys: [], oracleLobPreview: false };
const unchanged = { sql, metadataSql: sql, hiddenPrimaryKeys: [], largeValuePreview: false };
if (!databaseType || !HIDDEN_QUERY_KEY_DATABASE_TYPES.has(databaseType) || !tab.connectionId) return unchanged;
try {
@@ -6257,10 +6268,10 @@ export const useQueryStore = defineStore("query", () => {
const wholeSourceProjected = projectsAllColumnsForSource(analysis, source.key);
const hasDirectSourceProjection = analysis.columns.some((column) => Boolean(column.sourceName) && (!column.sourceKey || column.sourceKey === source.key));
if (!wholeSourceProjected && !hasDirectSourceProjection) return unchanged;
// Whole-source projections already include declared primary keys. Only
// Oracle and Xugu need preflight metadata here to add their synthetic
// row key for a keyless base table.
if (databaseType !== "oracle" && databaseType !== "xugu" && wholeSourceProjected) return unchanged;
// Whole-source projections already include declared primary keys. Oracle
// and Xugu may need a synthetic key, while DB2 needs column metadata to
// decide whether LOB materialization can be deferred safely.
if (databaseType !== "oracle" && databaseType !== "xugu" && databaseType !== "db2" && wholeSourceProjected) return unchanged;
const target = resolveEditableSourceMetadataTarget(tab, analysis, source, conn, databaseType, executionDatabase);
const cached = getCachedTableMetadata(target.request);
@@ -6951,7 +6962,7 @@ export const useQueryStore = defineStore("query", () => {
let resultSortedSql = options?.resultSortedSql;
let queryMetadataSql = queryBaseSql;
let hiddenPrimaryKeys: HiddenPrimaryKeyProjection[] = [];
let useOracleLobPreview = false;
let useLargeValuePreview = false;
let pageSql: string | undefined;
let requestedPageLimit: number | undefined;
let pageLimit: number | undefined;
@@ -7703,7 +7714,7 @@ export const useQueryStore = defineStore("query", () => {
// does not turn an otherwise editable result into a complex read-only one.
queryMetadataSql = options?.resultSortedSql && !options?.querySort ? queryBaseSql : prepared.metadataSql;
hiddenPrimaryKeys = prepared.hiddenPrimaryKeys;
useOracleLobPreview = prepared.oracleLobPreview;
useLargeValuePreview = prepared.largeValuePreview;
if (options?.querySort) {
const sorted = await api.buildSortedQuerySql({
originalSql: sqlToExecute,
@@ -7816,14 +7827,14 @@ export const useQueryStore = defineStore("query", () => {
: { maxRows: agentProtocolQueryResultMaxRows(queryResultMaxRows) }),
...(useJdbcDriverRowOffset && typeof pageOffset === "number" && pageOffset > 0 ? { rowOffset: pageOffset } : {}),
...(executionClientSessionId ? { clientSessionId: executionClientSessionId } : {}),
...(tab.mode === "data" && (effectiveDbType === "mysql" || effectiveDbType === "postgres")
...(tab.mode === "data" && supportsTableDataLargeValuePreview(effectiveDbType)
? {
maxResultBytes: TABLE_DATA_RESULT_MAX_BYTES,
resultKeyColumns: dataTabMeta?.primaryKeys ?? [],
tableDataPreview: useTableDataPreview,
}
: {}),
...(useOracleLobPreview ? { tableDataPreview: true } : {}),
...(useLargeValuePreview ? { tableDataPreview: true } : {}),
timeoutSecs: queryTimeoutSecs,
catalog: executionCatalog,
continueOnError: continueOnBatchError,
@@ -7953,8 +7964,8 @@ export const useQueryStore = defineStore("query", () => {
// strand it after returning the first page.
const executeInTransaction = (sessionId: string) =>
useAgentResultSession && !isOffsetJumpPage
? api.executeInManualTransaction(sessionId, sqlToExecute, executionDatabase, executionSchema, agentProtocolQueryResultMaxRows(queryResultMaxRows), useOracleLobPreview, pageLimit, options?.pagination?.sessionId, classificationSql)
: api.executeInManualTransaction(sessionId, sqlToExecute, executionDatabase, executionSchema, pageLimit ?? agentProtocolQueryResultMaxRows(queryResultMaxRows), useOracleLobPreview, undefined, undefined, classificationSql);
? api.executeInManualTransaction(sessionId, sqlToExecute, executionDatabase, executionSchema, agentProtocolQueryResultMaxRows(queryResultMaxRows), useLargeValuePreview, pageLimit, options?.pagination?.sessionId, classificationSql)
: api.executeInManualTransaction(sessionId, sqlToExecute, executionDatabase, executionSchema, pageLimit ?? agentProtocolQueryResultMaxRows(queryResultMaxRows), useLargeValuePreview, undefined, undefined, classificationSql);
try {
return await executeInTransaction(txnSessionId);
} catch (error) {