fix(cassandra): use cursor pagination for table data and exports

Closes #7465
This commit is contained in:
t8y2 committed 2026-09-28 05:59:12 +00:00
1 parent d452346082
commit 4190bab5ad
17 files changed
+1099 -88

No files matched your search

@@ -2290,16 +2290,31 @@ function tableDdlObjectType(type: ObjectBrowserRow["type"]): ObjectSourceKind |
}
async function exportDataLegacy(row: ObjectBrowserRow, format: "json") {
const { connection, database, catalog } = props;
const { id: connectionId, db_type: databaseType, query_timeout_secs: timeoutSecs } = connection;
const exportDatabaseType = effectiveDatabaseType.value;
try {
const schema = row.schema || selectedSchema.value;
const queryColumns = props.connection.db_type === "neo4j" ? (await api.getColumns(props.connection.id, props.database, schema || props.database, row.name, props.catalog)).map((column) => column.name) : undefined;
const queryColumns = databaseType === "neo4j" ? (await api.getColumns(connectionId, database, schema || database, row.name, catalog)).map((column) => column.name) : undefined;
const useAgentCursor = databaseType === "cassandra";
const clientSessionId = useAgentCursor ? `table-export:${generateDatabaseExportId()}` : undefined;
const result = await fetchTableDataForExport({
databaseType: effectiveDatabaseType.value,
identifierQuote: connectionStore.connectionIdentifierQuote?.(props.connection.id),
databaseType: exportDatabaseType,
identifierQuote: connectionStore.connectionIdentifierQuote?.(connectionId),
schema,
tableName: row.name,
columns: queryColumns,
executePage: (sql) => api.executeQuery(props.connection.id, props.database, sql),
useAgentCursor,
executePage: (sql, cursorOptions) => (cursorOptions ? api.executeQuery(connectionId, database, sql, undefined, undefined, { ...cursorOptions, clientSessionId, catalog, timeoutSecs }) : api.executeQuery(connectionId, database, sql)),
closeCursor: useAgentCursor
? async (sessionId) => {
try {
if (sessionId) await api.closeQuerySession(connectionId, database, sessionId, clientSessionId, catalog);
} finally {
await api.closeClientConnectionSession(connectionId, database, clientSessionId!, catalog);
}
}
: undefined,
});
if (format === "json") {
@@ -589,6 +589,70 @@ describe("useDataGridActions", () => {
expect(mocks.executeTabSql).toHaveBeenCalledWith("tab-1", "SELECT * FROM public.users LIMIT 100 OFFSET 100", expect.objectContaining({ appendResult: { maxRows: 10_000 } }));
});
it("continues the matching Cassandra table cursor and client session", async () => {
mocks.infiniteScroll = false;
mocks.getConfig.mockReturnValue({ id: "cassandra-1", db_type: "cassandra" });
mocks.buildTableSelectSql.mockResolvedValueOnce('SELECT * FROM "paged_rows";');
const active = tableDataTab({ id: "tab-a" });
const target = tableDataTab({
id: "tab-b",
connectionId: "cassandra-1",
resultPageLimit: 100,
resultPageOffset: 0,
resultClientSessionId: "tab-b",
result: {
columns: ["id"],
rows: Array.from({ length: 100 }, (_, index) => [index]),
affected_rows: 0,
execution_time_ms: 1,
session_id: "cassandra-page-1",
has_more: true,
},
});
mocks.tabs.push(active, target);
const actions = useDataGridActions(computed(() => active));
await actions.onPaginate(target.id, 100, 100);
expect(mocks.executeTabSql).toHaveBeenCalledWith(
"tab-b",
'SELECT * FROM "paged_rows";',
expect.objectContaining({
pagination: { offset: 100, limit: 100, sessionId: "cassandra-page-1", clientSessionId: "tab-b" },
}),
);
});
it("restarts Cassandra table cursors for jumps and page-size changes", async () => {
mocks.infiniteScroll = false;
mocks.getConfig.mockReturnValue({ id: "cassandra-1", db_type: "cassandra" });
mocks.buildTableSelectSql.mockResolvedValue('SELECT * FROM "paged_rows";');
const tab = tableDataTab({
connectionId: "cassandra-1",
resultPageLimit: 100,
resultPageOffset: 0,
resultClientSessionId: "tab-1",
result: {
columns: ["id"],
rows: Array.from({ length: 100 }, (_, index) => [index]),
affected_rows: 0,
execution_time_ms: 1,
session_id: "cassandra-page-1",
has_more: true,
},
});
mocks.tabs.push(tab);
const actions = useDataGridActions(computed(() => tab));
await actions.onPaginate(tab.id, 300, 100);
await actions.onPaginate(tab.id, 100, 50);
expect(mocks.executeTabSql.mock.calls.map((call) => call[2].pagination)).toEqual([
{ offset: 300, limit: 100, sessionId: undefined, clientSessionId: undefined },
{ offset: 100, limit: 50, sessionId: undefined, clientSessionId: undefined },
]);
});
it("ignores a stale structured order when its column was renamed", async () => {
const tab = tableDataTab({
resultSortColumn: "old_name",
@@ -8,6 +8,8 @@ const addExportTaskMock = vi.hoisted(() => vi.fn());
const updateTableExportTaskMock = vi.hoisted(() => vi.fn());
const apiMock = vi.hoisted(() => ({
buildTableSelectSql: vi.fn(async () => 'SELECT * FROM "main"."users" LIMIT 10000'),
closeClientConnectionSession: vi.fn(),
closeQuerySession: vi.fn(),
executeQuery: vi.fn(),
exportQueryResultCsv: vi.fn(),
exportQueryResultJson: vi.fn(),
@@ -242,6 +244,70 @@ describe("useSidebarTreeExportRuntime", () => {
expect(toastMock).toHaveBeenCalledWith("导出失败:上一个 DuckDB 查询仍在停止中,请稍后重试。", 5000);
});
it("isolates and advances Cassandra JSON export cursors", async () => {
apiMock.buildTableSelectSql.mockResolvedValue('SELECT * FROM "events";');
apiMock.executeQuery.mockResolvedValueOnce({ columns: ["id"], rows: [[1]], affected_rows: 0, execution_time_ms: 1, session_id: "cursor-1", has_more: true }).mockResolvedValueOnce({ columns: ["id"], rows: [[2]], affected_rows: 0, execution_time_ms: 1, has_more: false });
const activeNode = shallowRef({ id: "table-1", type: "table", label: "events", connectionId: "cassandra-1", database: "app", children: [] } as TreeNode);
const connectionStore = {
ensureConnected: vi.fn(),
getConfig: vi.fn(() => ({ db_type: "cassandra", query_timeout_secs: 30 })),
connectionIdentifierQuote: vi.fn(() => '"'),
treeNodes: [],
selectedTreeNodeIds: [],
};
const runtime = useSidebarTreeExportRuntime({
activeNode,
connectionStore: connectionStore as never,
settingsStore: exportSettings() as never,
acceptedSelectionIds: () => null,
});
await runtime.exportData("json");
const firstOptions = apiMock.executeQuery.mock.calls[0]?.[5];
const secondOptions = apiMock.executeQuery.mock.calls[1]?.[5];
expect(firstOptions).toMatchObject({ maxRows: 2_147_483_647, fetchSize: 10_000, pageSize: 10_000, resultSessionId: undefined, timeoutSecs: 30 });
expect(secondOptions).toMatchObject({ maxRows: 2_147_483_647, fetchSize: 10_000, pageSize: 10_000, resultSessionId: "cursor-1", clientSessionId: firstOptions.clientSessionId, timeoutSecs: 30 });
expect(firstOptions.clientSessionId).toMatch(/^table-export:/);
expect(apiMock.exportQueryResultJson).toHaveBeenCalledWith("events.json", ["id"], [[1], [2]]);
expect(apiMock.closeClientConnectionSession).toHaveBeenCalledWith("cassandra-1", "app", firstOptions.clientSessionId, undefined);
});
it("closes a failed Cassandra cursor before disposing its client session", async () => {
apiMock.buildTableSelectSql.mockResolvedValue('SELECT * FROM "events";');
apiMock.executeQuery.mockResolvedValueOnce({ columns: ["id"], rows: [[1]], affected_rows: 0, execution_time_ms: 1, session_id: "cursor-1", has_more: true }).mockRejectedValueOnce(new Error("cursor lost"));
const closing = deferred<void>();
apiMock.closeQuerySession.mockReturnValueOnce(closing.promise);
const runtime = useSidebarTreeExportRuntime({
activeNode: shallowRef({ id: "table-1", type: "table", label: "events", connectionId: "cassandra-1", database: "app", children: [] } as TreeNode),
connectionStore: { ensureConnected: vi.fn(), getConfig: () => ({ db_type: "cassandra" }), connectionIdentifierQuote: () => '"', treeNodes: [], selectedTreeNodeIds: [] } as never,
settingsStore: exportSettings() as never,
acceptedSelectionIds: () => null,
});
const exported = runtime.exportData("json");
await vi.waitFor(() => expect(apiMock.closeQuerySession).toHaveBeenCalled());
expect(apiMock.closeClientConnectionSession).not.toHaveBeenCalled();
closing.reject(new Error("close failed"));
await exported;
expect(apiMock.closeClientConnectionSession).toHaveBeenCalledTimes(1);
expect(apiMock.exportQueryResultJson).not.toHaveBeenCalled();
expect(toastMock).toHaveBeenCalledWith("导出失败:cursor lost", 5000);
});
it("preserves SQL Server legacy JSON export without enabling a new cursor path", async () => {
apiMock.executeQuery.mockResolvedValueOnce({ columns: ["id"], rows: [[1]], affected_rows: 0, execution_time_ms: 1 });
const runtime = useSidebarTreeExportRuntime({
activeNode: shallowRef({ id: "table-1", type: "table", label: "events", connectionId: "sqlserver-1", database: "app", children: [] } as TreeNode),
connectionStore: { ensureConnected: vi.fn(), getConfig: () => ({ db_type: "sqlserver", driver_profile: "sqlserver-legacy" }), connectionIdentifierQuote: () => '"', treeNodes: [], selectedTreeNodeIds: [] } as never,
settingsStore: exportSettings() as never,
acceptedSelectionIds: () => null,
});
await runtime.exportData("json");
expect(apiMock.executeQuery.mock.calls[0]).toHaveLength(3);
expect(apiMock.closeClientConnectionSession).not.toHaveBeenCalled();
expect(apiMock.exportQueryResultJson).toHaveBeenCalledWith("events.json", ["id"], [[1]]);
});
it("exports a mongo collection through the save-file path without a setup dialog", async () => {
apiMock.exportMongodbQuery.mockImplementation(async (_request, onProgress) => {
onProgress({ exportId: "export-1", status: "done", documentsRead: 3, bytesWritten: 12, elapsedMs: 4 });
@@ -23,6 +23,7 @@ import type { DataGridReloadIntent } from "@/lib/dataGrid/dataGridToolbar";
import { continuousQueryResultMaxRows } from "@/lib/dataGrid/queryResultRowLimit";
import { queryResultBaseSql, queryResultExecutionSql } from "@/lib/tabs/tabPresentation";
import { sqlExecutionTargetCapabilities } from "@/lib/database/sqlExecutionTargetCapabilities";
import { usesAgentCursorForTableData } from "@/lib/database/databaseDriverManifest";
const DATA_TAB_METADATA_TTL_MS = TABLE_METADATA_CACHE_TTL_MS;
@@ -602,8 +603,7 @@ export function useDataGridActions(activeTab: ComputedRef<QueryTab | undefined>)
const expectedNextOffset = appendResult ? tab.result?.rows.length : (tab.resultPageOffset ?? 0) + (tab.resultPageLimit ?? limit);
const continuesResultSession = offset === expectedNextOffset && limit === tab.resultPageLimit;
const connection = useConnectionStore().getConfig(tab.connectionId);
const isSqlServerLegacy = connection?.db_type === "sqlserver" && connection.driver_profile?.trim().toLowerCase() === "sqlserver-legacy";
const sessionId = isSqlServerLegacy && tab.result?.has_more && tab.result.session_id && continuesResultSession ? tab.result.session_id : undefined;
const sessionId = usesAgentCursorForTableData(connection?.db_type, connection?.driver_profile) && tab.result?.has_more && tab.result.session_id && continuesResultSession ? tab.result.session_id : undefined;
await queryStore.executeTabSql(tab.id, sql, {
pagination: { offset, limit, sessionId, clientSessionId: sessionId ? tab.resultClientSessionId : undefined },
...appendOptions,
@@ -33,6 +33,7 @@ import {
} from "@/components/sidebar/sidebarTreeDialogState";
import type { CsvQuoteMode } from "@/lib/export/csvQuoteMode";
import type { SqlInsertDialect } from "@/lib/export/sqlInsertMode";
import { uuid } from "@/lib/common/utils";
type StructureCopyFormat = "tsv" | "markdown";
@@ -355,6 +356,8 @@ export function useSidebarTreeExportRuntime(options: SidebarTreeExportRuntimeOpt
try {
await connectionStore.ensureConnected(connectionId);
const queryColumns = config.db_type === "neo4j" ? (await api.getColumns(connectionId, database, target.metadataSchema, target.tableName, target.catalog)).map((column) => column.name) : undefined;
const useAgentCursor = config.db_type === "cassandra";
const clientSessionId = useAgentCursor ? `table-export:${uuid()}` : undefined;
const result = await fetchTableDataForExport({
databaseType: target.databaseType,
identifierQuote: target.identifierQuote,
@@ -362,7 +365,17 @@ export function useSidebarTreeExportRuntime(options: SidebarTreeExportRuntimeOpt
tableName: target.tableName,
tableType: target.tableType,
columns: queryColumns,
executePage: (sql) => api.executeQuery(connectionId, database, sql),
useAgentCursor,
executePage: (sql, cursorOptions) => (cursorOptions ? api.executeQuery(connectionId, database, sql, undefined, undefined, { ...cursorOptions, clientSessionId, catalog: target.catalog, timeoutSecs: config.query_timeout_secs }) : api.executeQuery(connectionId, database, sql)),
closeCursor: useAgentCursor
? async (sessionId) => {
try {
if (sessionId) await api.closeQuerySession(connectionId, database, sessionId, clientSessionId, target.catalog);
} finally {
await api.closeClientConnectionSession(connectionId, database, clientSessionId!, target.catalog);
}
}
: undefined,
});
const outputPath = await resolveTableExportOutputPath(target, "json", outputDirectory);
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { databaseConnectionFormKind, databaseDefaultPort, databaseManifestEntry, databaseRuntimeMode, usesAgentCursorForQuery } from "@/lib/database/databaseDriverManifest";
import { databaseConnectionFormKind, databaseDefaultPort, databaseManifestEntry, databaseRuntimeMode, usesAgentCursorForQuery, usesAgentCursorForTableData } from "@/lib/database/databaseDriverManifest";
describe("databaseDriverManifest", () => {
it("uses agent cursor only for agent or external runtimes", () => {
@@ -10,6 +10,7 @@ describe("databaseDriverManifest", () => {
expect(usesAgentCursorForQuery("meilisearch")).toBe(false);
expect(usesAgentCursorForQuery("dynamodb")).toBe(false);
expect(usesAgentCursorForQuery("mysql")).toBe(false);
expect(usesAgentCursorForQuery("cassandra")).toBe(true);
expect(usesAgentCursorForQuery("jdbc")).toBe(true);
expect(usesAgentCursorForQuery("prestosql")).toBe(true);
expect(usesAgentCursorForQuery("sqlserver")).toBe(false);
@@ -17,6 +18,14 @@ describe("databaseDriverManifest", () => {
expect(usesAgentCursorForQuery("sqlserver", " SQLSERVER-LEGACY ")).toBe(true);
});
it("limits table-data agent cursors to Cassandra and SQL Server legacy", () => {
expect(usesAgentCursorForTableData("cassandra")).toBe(true);
expect(usesAgentCursorForTableData("sqlserver", "sqlserver-legacy")).toBe(true);
expect(usesAgentCursorForTableData("sqlserver")).toBe(false);
expect(usesAgentCursorForTableData("jdbc")).toBe(false);
expect(usesAgentCursorForTableData("prestosql")).toBe(false);
});
it("exposes connection defaults from the shared manifest", () => {
expect(databaseDefaultPort("mysql")).toBe(3306);
expect(databaseManifestEntry("h2")).toMatchObject({
@@ -159,6 +159,10 @@ export function usesAgentCursorForQuery(dbType?: DatabaseType, driverProfile?: s
return runtimeMode === "agent" || runtimeMode === "external";
}
export function usesAgentCursorForTableData(dbType?: DatabaseType, driverProfile?: string): boolean {
return dbType === "cassandra" || (dbType === "sqlserver" && driverProfile?.trim().toLowerCase() === "sqlserver-legacy");
}
function productCapabilities(overrides: Partial<DatabaseProductCapabilities>): DatabaseProductCapabilities {
const capabilities = Object.fromEntries(DATABASE_PRODUCT_CAPABILITY_KEYS.map((key) => [key, false])) as DatabaseProductCapabilities;
return { ...capabilities, ...overrides };
+63 -28
View File
@@ -2,6 +2,14 @@ import type { DatabaseType, QueryResult } from "@/types/database.ts";
import { buildTableSelectSql } from "@/lib/table/tableSelectSql.ts";
export const TABLE_DATA_EXPORT_PAGE_SIZE = 10_000;
export const TABLE_DATA_EXPORT_MAX_ROWS = 2_147_483_647;
export interface TableDataExportCursorPageOptions {
maxRows: number;
fetchSize: number;
pageSize: number;
resultSessionId?: string;
}
export interface FetchTableDataForExportOptions {
databaseType?: DatabaseType;
@@ -11,8 +19,10 @@ export interface FetchTableDataForExportOptions {
tableType?: string;
columns?: string[];
pageSize?: number;
useAgentCursor?: boolean;
buildPageSql?: (options: { databaseType?: DatabaseType; schema?: string; tableName: string; tableType?: string; columns?: string[]; limit: number; offset: number }) => Promise<string> | string;
executePage: (sql: string) => Promise<QueryResult>;
executePage: (sql: string, cursorOptions?: TableDataExportCursorPageOptions) => Promise<QueryResult>;
closeCursor?: (sessionId?: string) => Promise<void>;
}
export async function fetchTableDataForExport(options: FetchTableDataForExportOptions): Promise<QueryResult> {
@@ -33,35 +43,60 @@ export async function fetchTableDataForExport(options: FetchTableDataForExportOp
const rows: QueryResult["rows"] = [];
let columns: string[] = [];
let executionTimeMs = 0;
let sessionId: string | undefined;
while (true) {
const sql = await (options.buildPageSql ?? buildTableSelectSql)({
databaseType: options.databaseType,
identifierQuote: options.identifierQuote,
schema: options.schema,
tableName: options.tableName,
tableType: options.tableType,
columns: options.columns,
limit: pageSize,
offset,
});
const result = await options.executePage(sql);
if (columns.length === 0) columns = result.columns;
rows.push(...result.rows);
executionTimeMs += result.execution_time_ms ?? 0;
try {
while (true) {
const sql = await (options.buildPageSql ?? buildTableSelectSql)({
databaseType: options.databaseType,
identifierQuote: options.identifierQuote,
schema: options.schema,
tableName: options.tableName,
tableType: options.tableType,
columns: options.columns,
limit: pageSize,
offset,
});
const result = await options.executePage(
sql,
options.useAgentCursor
? {
maxRows: TABLE_DATA_EXPORT_MAX_ROWS,
fetchSize: pageSize,
pageSize,
resultSessionId: sessionId,
}
: undefined,
);
const nextSessionId = result.session_id?.trim() || undefined;
if (options.useAgentCursor) {
sessionId = nextSessionId ?? sessionId;
if (result.truncated || typeof result.has_more !== "boolean") throw new Error("Incomplete cursor result during table export");
if (result.has_more && !nextSessionId) throw new Error("Result session ended before table export completed");
}
if (columns.length === 0) columns = result.columns;
rows.push(...result.rows);
executionTimeMs += result.execution_time_ms ?? 0;
sessionId = nextSessionId;
if (result.rows.length < pageSize) {
return {
columns,
rows,
affected_rows: 0,
execution_time_ms: executionTimeMs,
truncated: false,
session_id: undefined,
has_more: false,
};
if (options.useAgentCursor ? result.has_more !== true : result.rows.length < pageSize) {
return {
columns,
rows,
affected_rows: 0,
execution_time_ms: executionTimeMs,
truncated: false,
session_id: undefined,
has_more: false,
};
}
offset += result.rows.length;
}
} finally {
if (options.useAgentCursor && options.closeCursor) {
try {
await options.closeCursor(sessionId);
} catch {}
}
offset += result.rows.length;
}
}
@@ -248,6 +248,227 @@ describe("queryStore table data refresh", () => {
expect(mocks.executeMulti).toHaveBeenCalledWith("yasdb-1", "app", "SELECT * FROM app.users", undefined, expect.any(String), expect.not.objectContaining({ rowOffset: expect.anything() }));
});
it("continues and restarts Cassandra table cursors without duplicating page one", async () => {
mocks.getConnectionConfig.mockReturnValue({
id: "cassandra-1",
name: "Cassandra",
db_type: "cassandra",
database: "app",
query_timeout_secs: 30,
});
mocks.executeMulti
.mockResolvedValueOnce([{ columns: ["id"], rows: [[1], [2]], affected_rows: 0, execution_time_ms: 1, session_id: "cursor-1", has_more: true }])
.mockResolvedValueOnce([{ columns: ["id"], rows: [[3], [4]], affected_rows: 0, execution_time_ms: 1, session_id: "cursor-1", has_more: true }])
.mockResolvedValueOnce([{ columns: ["id"], rows: [[1], [2]], affected_rows: 0, execution_time_ms: 1, session_id: "cursor-2", has_more: true }])
.mockResolvedValueOnce([{ columns: ["id"], rows: [[3], [4]], affected_rows: 0, execution_time_ms: 1, session_id: "cursor-2", has_more: true }]);
const { useQueryStore } = await import("@/stores/queryStore");
const store = useQueryStore();
const tabId = store.createTab("cassandra-1", "app", "paged_rows", "data");
store.setTableMeta(tabId, {
tableName: "paged_rows",
tableType: "TABLE",
columns: [{ name: "id", data_type: "int", is_nullable: false, column_default: null, is_primary_key: true, extra: null }],
primaryKeys: ["id"],
});
const sql = 'SELECT * FROM "paged_rows";';
await store.executeTabSql(tabId, sql, { pagination: { limit: 2, offset: 0 } });
let tab = store.tabs.find((candidate) => candidate.id === tabId)!;
expect(tab.result?.rows).toEqual([[1], [2]]);
expect(tab.resultSessionId).toBe("cursor-1");
expect(tab.resultClientSessionId).toBe(tabId);
await store.executeTabSql(tabId, sql, {
pagination: { limit: 2, offset: 2, sessionId: "cursor-1", clientSessionId: tab.resultClientSessionId },
});
tab = store.tabs.find((candidate) => candidate.id === tabId)!;
expect(tab.result?.rows).toEqual([[3], [4]]);
expect(mocks.closeQuerySession).not.toHaveBeenCalled();
await store.executeTabSql(tabId, sql, { pagination: { limit: 2, offset: 0 } });
tab = store.tabs.find((candidate) => candidate.id === tabId)!;
expect(tab.result?.rows).toEqual([[1], [2]]);
expect(mocks.closeQuerySession).toHaveBeenCalledWith("cassandra-1", "app", "cursor-1", tabId);
await store.executeTabSql(tabId, sql, {
pagination: { limit: 2, offset: 2, sessionId: "cursor-2", clientSessionId: tab.resultClientSessionId },
});
expect(store.tabs.find((candidate) => candidate.id === tabId)?.result?.rows).toEqual([[3], [4]]);
expect(mocks.executeMulti.mock.calls.map((call) => call[5])).toEqual([
expect.objectContaining({ maxRows: 2_147_483_647, fetchSize: 2, pageSize: 2, resultSessionId: undefined, clientSessionId: tabId }),
expect.objectContaining({ maxRows: 2_147_483_647, fetchSize: 2, pageSize: 2, resultSessionId: "cursor-1", clientSessionId: tabId }),
expect.objectContaining({ maxRows: 2_147_483_647, fetchSize: 2, pageSize: 2, resultSessionId: undefined, clientSessionId: tabId }),
expect.objectContaining({ maxRows: 2_147_483_647, fetchSize: 2, pageSize: 2, resultSessionId: "cursor-2", clientSessionId: tabId }),
]);
});
it("replays Cassandra table cursors to direct, last, and exact-boundary pages", async () => {
mocks.getConnectionConfig.mockReturnValue({ id: "cassandra-1", name: "Cassandra", db_type: "cassandra", database: "app" });
const page = (rows: number[], sessionId?: string) => ({
columns: ["id"],
rows: rows.map((value) => [value]),
affected_rows: 0,
execution_time_ms: rows.length,
session_id: sessionId,
has_more: !!sessionId,
query_timings_ms: { agent_total: rows.length },
});
mocks.executeMulti
.mockResolvedValueOnce([page([1, 2], "cursor-last")])
.mockResolvedValueOnce([page([3, 4], "cursor-last")])
.mockResolvedValueOnce([page([5])]);
const { useQueryStore } = await import("@/stores/queryStore");
const store = useQueryStore();
const tabId = store.createTab("cassandra-1", "app", "paged_rows", "data");
store.setTableMeta(tabId, {
tableName: "paged_rows",
tableType: "TABLE",
columns: [{ name: "id", data_type: "int", is_nullable: false, column_default: null, is_primary_key: true, extra: null }],
primaryKeys: ["id"],
});
const sql = 'SELECT * FROM "paged_rows";';
await store.executeTabSql(tabId, sql, { pagination: { limit: 2, offset: 4 } });
let tab = store.tabs.find((candidate) => candidate.id === tabId)!;
expect(tab.result?.rows).toEqual([[5]]);
expect(tab.result?.execution_time_ms).toBe(5);
expect(tab.result?.timing_page_count).toBe(3);
expect(tab.resultPageOffset).toBe(4);
expect(tab.resultTotalRowCount).toBe(5);
expect(mocks.executeMulti.mock.calls.map((call) => call[5]?.resultSessionId)).toEqual([undefined, "cursor-last", "cursor-last"]);
mocks.executeMulti.mockReset();
mocks.executeMulti.mockResolvedValueOnce([page([1, 2], "cursor-boundary")]).mockResolvedValueOnce([page([3, 4])]);
await store.executeTabSql(tabId, sql, { pagination: { limit: 2, offset: 4 } });
tab = store.tabs.find((candidate) => candidate.id === tabId)!;
expect(tab.result?.rows).toEqual([]);
expect(tab.result?.has_more).toBe(false);
expect(tab.resultPageOffset).toBe(4);
expect(tab.resultTotalRowCount).toBeUndefined();
expect(mocks.executeMulti).toHaveBeenCalledTimes(2);
});
it("closes a Cassandra replay cursor when a later page fails", async () => {
mocks.getConnectionConfig.mockReturnValue({ id: "cassandra-1", name: "Cassandra", db_type: "cassandra", database: "app" });
mocks.executeMulti.mockResolvedValueOnce([{ columns: ["id"], rows: [[1], [2]], affected_rows: 0, execution_time_ms: 1, session_id: "cursor-lost", has_more: true }]).mockRejectedValueOnce(new Error("query session not found: cursor-lost"));
const { useQueryStore } = await import("@/stores/queryStore");
const store = useQueryStore();
const tabId = store.createTab("cassandra-1", "app", "paged_rows", "data");
store.setTableMeta(tabId, {
tableName: "paged_rows",
tableType: "TABLE",
columns: [{ name: "id", data_type: "int", is_nullable: false, column_default: null, is_primary_key: true, extra: null }],
primaryKeys: ["id"],
});
await store.executeTabSql(tabId, 'SELECT * FROM "paged_rows";', { pagination: { limit: 2, offset: 2 } });
const tab = store.tabs.find((candidate) => candidate.id === tabId)!;
expect(tab.result?.execution_error).toBe(true);
expect(tab.resultSessionId).toBeUndefined();
expect(mocks.closeQuerySession).toHaveBeenCalledWith("cassandra-1", "app", "cursor-lost", tabId, undefined);
});
it.each(["cancellation", "supersession"] as const)("does not dispatch another Cassandra replay page after %s", async (interruption) => {
mocks.getConnectionConfig.mockReturnValue({ id: "cassandra-1", name: "Cassandra", db_type: "cassandra", database: "app" });
const { useQueryStore } = await import("@/stores/queryStore");
const store = useQueryStore();
const tabId = store.createTab("cassandra-1", "app", "paged_rows", "data");
store.setTableMeta(tabId, {
tableName: "paged_rows",
tableType: "TABLE",
columns: [{ name: "id", data_type: "int", is_nullable: false, column_default: null, is_primary_key: true, extra: null }],
primaryKeys: ["id"],
});
mocks.executeMulti.mockImplementationOnce(async () => {
const tab = store.tabs.find((candidate) => candidate.id === tabId)!;
if (interruption === "cancellation") {
tab.isCancelling = true;
tab.cancelRequestCount = (tab.cancelRequestCount ?? 0) + 1;
} else {
tab.executionId = "replacement-execution";
}
return [{ columns: ["id"], rows: [[1], [2]], affected_rows: 0, execution_time_ms: 1, session_id: "cursor-cancel", has_more: true }];
});
await store.executeTabSql(tabId, 'SELECT * FROM "paged_rows";', { pagination: { limit: 2, offset: 4 } });
expect(mocks.executeMulti).toHaveBeenCalledTimes(1);
expect(mocks.closeQuerySession).toHaveBeenCalledWith("cassandra-1", "app", "cursor-cancel", tabId, undefined);
});
it("exports Cassandra table data by advancing one Agent cursor", async () => {
mocks.getConnectionConfig.mockReturnValue({ id: "cassandra-1", name: "Cassandra", db_type: "cassandra", database: "app" });
mocks.buildTableSelectSql.mockResolvedValue('SELECT * FROM "paged_rows";');
mocks.executeMulti.mockResolvedValueOnce([{ columns: ["id"], rows: [[1], [2]], affected_rows: 0, execution_time_ms: 2, session_id: "export-cursor", has_more: true }]).mockResolvedValueOnce([{ columns: ["id"], rows: [[3]], affected_rows: 0, execution_time_ms: 1, has_more: false }]);
const { useQueryStore } = await import("@/stores/queryStore");
const store = useQueryStore();
const tabId = store.createTab("cassandra-1", "app", "paged_rows", "data");
store.setTableMeta(tabId, {
tableName: "paged_rows",
tableType: "TABLE",
columns: [{ name: "id", data_type: "int", is_nullable: false, column_default: null, is_primary_key: true, extra: null }],
primaryKeys: ["id"],
});
const tab = store.tabs.find((candidate) => candidate.id === tabId)!;
tab.result = { columns: ["id"], rows: [[1], [2]], affected_rows: 0, execution_time_ms: 2 };
const exported = await store.fetchTabResultForExport(tabId);
expect(exported?.rows).toEqual([[1], [2], [3]]);
expect(exported?.execution_time_ms).toBe(3);
expect(mocks.buildTableSelectSql).toHaveBeenCalledTimes(2);
expect(mocks.executeMulti.mock.calls.map((call) => call[5])).toEqual([
expect.objectContaining({ maxRows: 2_147_483_647, fetchSize: 10_000, pageSize: 10_000, resultSessionId: undefined, clientSessionId: `${tabId}:export` }),
expect.objectContaining({ maxRows: 2_147_483_647, fetchSize: 10_000, pageSize: 10_000, resultSessionId: "export-cursor", clientSessionId: `${tabId}:export` }),
]);
expect(mocks.closeClientConnectionSession).toHaveBeenCalledWith("cassandra-1", "app", `${tabId}:export`);
});
it("stops Cassandra table export when continuation has no session", async () => {
mocks.getConnectionConfig.mockReturnValue({ id: "cassandra-1", name: "Cassandra", db_type: "cassandra", database: "app" });
mocks.buildTableSelectSql.mockResolvedValue('SELECT * FROM "paged_rows";');
mocks.executeMulti.mockResolvedValueOnce([{ columns: ["id"], rows: [[1], [2]], affected_rows: 0, execution_time_ms: 1, has_more: true }]);
const { useQueryStore } = await import("@/stores/queryStore");
const store = useQueryStore();
const tabId = store.createTab("cassandra-1", "app", "paged_rows", "data");
store.setTableMeta(tabId, {
tableName: "paged_rows",
tableType: "TABLE",
columns: [{ name: "id", data_type: "int", is_nullable: false, column_default: null, is_primary_key: true, extra: null }],
primaryKeys: ["id"],
});
const tab = store.tabs.find((candidate) => candidate.id === tabId)!;
tab.result = { columns: ["id"], rows: [[1], [2]], affected_rows: 0, execution_time_ms: 1 };
await expect(store.fetchTabResultForExport(tabId)).rejects.toThrow("Result session ended before table export completed");
expect(mocks.executeMulti).toHaveBeenCalledTimes(1);
expect(mocks.closeClientConnectionSession).toHaveBeenCalledWith("cassandra-1", "app", `${tabId}:export`);
});
it.each([{ has_more: undefined }, { has_more: false, truncated: true }, { has_more: true, session_id: " " }])("rejects incomplete Cassandra export continuation %j and cleans its active cursor", async (malformed) => {
mocks.getConnectionConfig.mockReturnValue({ id: "cassandra-1", name: "Cassandra", db_type: "cassandra", database: "app" });
mocks.buildTableSelectSql.mockResolvedValue('SELECT * FROM "paged_rows";');
mocks.executeMulti.mockResolvedValueOnce([{ columns: ["id"], rows: [[1]], affected_rows: 0, execution_time_ms: 1, session_id: "active-cursor", has_more: true }]).mockResolvedValueOnce([{ columns: ["id"], rows: [[2]], affected_rows: 0, execution_time_ms: 1, ...malformed }]);
const { useQueryStore } = await import("@/stores/queryStore");
const store = useQueryStore();
const tabId = store.createTab("cassandra-1", "app", "paged_rows", "data");
store.setTableMeta(tabId, {
tableName: "paged_rows",
tableType: "TABLE",
columns: [{ name: "id", data_type: "int", is_nullable: false, column_default: null, is_primary_key: true, extra: null }],
primaryKeys: ["id"],
});
const tab = store.tabs.find((candidate) => candidate.id === tabId)!;
tab.result = { columns: ["id"], rows: [[1]], affected_rows: 0, execution_time_ms: 1 };
await expect(store.fetchTabResultForExport(tabId)).rejects.toThrow(/export/);
expect(mocks.executeMulti).toHaveBeenCalledTimes(2);
expect(mocks.closeQuerySession).toHaveBeenCalledWith("cassandra-1", "app", "active-cursor", `${tabId}:export`, undefined);
expect(mocks.closeClientConnectionSession).toHaveBeenCalledWith("cassandra-1", "app", `${tabId}:export`);
});
it("executes Doris external catalog data tabs against the catalog database, not the connection default database", async () => {
mocks.getConnectionConfig.mockReturnValue({
id: "doris-1",
+65 -35
View File
@@ -62,7 +62,7 @@ import { redisCommandResultToQueryResult } from "@/lib/redis/redisQueryResult";
import { nextRedisCommandDb } from "@/lib/redis/redisCommandSession";
import { isRedisMutatingCommand } from "@/lib/redis/redisCommandTable";
import { formatRedisConsoleValue } from "@/lib/redis/redisValuePresentation";
import { usesAgentCursorForQuery } from "@/lib/database/databaseDriverManifest";
import { usesAgentCursorForQuery, usesAgentCursorForTableData } from "@/lib/database/databaseDriverManifest";
import { connectionIsDorisFamilyCatalogCapable, defaultAutoCommitForDbType, supportsClearableQuerySchema, supportsTransaction, usesOracleStickyTransactionState, usesProvenReadOnlyStickyTransactionState } from "@/lib/database/databaseFeatureSupport";
import { canInsertTableRows, canUseKeylessRowPredicate, DBX_ROWID_COLUMN, editablePrimaryKeys, shouldIncludeSyntheticRowId, usesSyntheticRowIdKey } from "@/lib/table/tableEditing";
import { TABLE_DATA_EXPORT_PAGE_SIZE } from "@/lib/table/tableDataExport";
@@ -6732,6 +6732,7 @@ export const useQueryStore = defineStore("query", () => {
const traceId = executionId.slice(0, 8);
const startedAt = performance.now();
const executionTargetEpoch = manualTransactionTargetEpoch(tab);
const cancelRequestCountAtStart = tab.cancelRequestCount ?? 0;
const elapsed = () => `${Math.round(performance.now() - startedAt)}ms`;
const batchResume = options?.batchResume;
const continueOnBatchError = batchResume?.continueOnError ?? settingsStore.editorSettings.continueOnErrorOnBatch;
@@ -7602,7 +7603,7 @@ export const useQueryStore = defineStore("query", () => {
const pagination = limitQueryPagination(requestedPagination, queryResultMaxRows);
pageLimit = pagination.limit;
pageOffset = pagination.offset;
useAgentResultSession = conn?.db_type === "sqlserver" && conn?.driver_profile?.trim().toLowerCase() === "sqlserver-legacy";
useAgentResultSession = usesAgentCursorForTableData(conn?.db_type, conn?.driver_profile);
}
errorLocateContext = {
@@ -7670,7 +7671,7 @@ export const useQueryStore = defineStore("query", () => {
});
executionDispatched = true;
if (tab.mode === "query") clientRequestStartedAt = performance.now();
if (useAgentResultSession && tab.mode === "query" && typeof pageOffset === "number" && pageOffset > 0 && !options?.pagination?.sessionId && !(tab.batchSqlExecution && tab.batchSqlExecution.total > 1)) {
if (useAgentResultSession && (tab.mode === "query" || tab.mode === "data") && typeof pageOffset === "number" && pageOffset > 0 && !options?.pagination?.sessionId && !(tab.batchSqlExecution && tab.batchSqlExecution.total > 1)) {
return (async () => {
let sessionId: string | undefined;
let skipped = 0;
@@ -7678,35 +7679,50 @@ export const useQueryStore = defineStore("query", () => {
let executionMs = 0;
let completeTimings = true;
const timings: Record<string, number> = {};
while (true) {
const pageResults = await api.executeMulti(executionConnectionId, executionDatabase, sqlToExecute, executionSchema, executionId, {
...executionOptions,
resultSessionId: sessionId,
});
const page = pageResults[0];
if (!page) return pageResults;
pageCount += 1;
executionMs += page.execution_time_ms;
if (!page.query_timings_ms) completeTimings = false;
else for (const [key, value] of Object.entries(page.query_timings_ms)) timings[key] = (timings[key] ?? 0) + value;
// Offset jumps consume several cursor pages before publication.
// Keep their timings as well, without retaining skipped rows.
const timingSummary = { query_timings_ms: completeTimings ? { ...timings } : undefined, execution_time_ms: executionMs, timing_page_count: pageCount };
if (skipped + page.rows.length > pageOffset) {
const start = pageOffset - skipped;
const limit = typeof pageLimit === "number" ? pageLimit : page.rows.length - start;
return [{ ...page, ...timingSummary, rows: page.rows.slice(start, start + limit) }];
}
skipped += page.rows.length;
if (!page.has_more || !page.session_id) {
if (page.has_more) {
// The cursor session ended before the requested offset;
// returning the short page would show the wrong rows.
throw new Error("Result session ended before the requested page offset");
try {
while (true) {
const pageResults = await api.executeMulti(executionConnectionId, executionDatabase, sqlToExecute, executionSchema, executionId, {
...executionOptions,
resultSessionId: sessionId,
});
const page = pageResults[0];
if (!page) return pageResults;
pageCount += 1;
executionMs += page.execution_time_ms;
if (!page.query_timings_ms) completeTimings = false;
else for (const [key, value] of Object.entries(page.query_timings_ms)) timings[key] = (timings[key] ?? 0) + value;
// Offset jumps consume several cursor pages before publication.
// Keep their timings as well, without retaining skipped rows.
const timingSummary = { query_timings_ms: completeTimings ? { ...timings } : undefined, execution_time_ms: executionMs, timing_page_count: pageCount };
if (skipped + page.rows.length > pageOffset) {
const start = pageOffset - skipped;
const limit = typeof pageLimit === "number" ? pageLimit : page.rows.length - start;
return [{ ...page, ...timingSummary, rows: page.rows.slice(start, start + limit) }];
}
skipped += page.rows.length;
if (!page.has_more || !page.session_id) {
if (page.has_more) {
// The cursor session ended before the requested offset;
// returning the short page would show the wrong rows.
throw new Error("Result session ended before the requested page offset");
}
return [{ ...page, ...timingSummary, rows: [] }];
}
sessionId = page.session_id;
const replayTab = findExecutionTab(id);
if (replayTab !== tab || replayTab.executionId !== executionId || replayTab.isCancelling || (replayTab.cancelRequestCount ?? 0) !== cancelRequestCountAtStart || manualTransactionTargetEpoch(replayTab) !== executionTargetEpoch) {
throw new Error("Query canceled");
}
return [{ ...page, ...timingSummary, rows: [] }];
}
sessionId = page.session_id;
} catch (error) {
if (sessionId) {
try {
await api.closeQuerySession(executionConnectionId, executionDatabase, sessionId, executionClientSessionId, executionCatalog);
} catch (closeError) {
queryExecutionLog("warn", "offset-replay-session-close:error", { traceId, sessionId, error: closeError });
}
}
throw error;
}
})();
}
@@ -8937,7 +8953,7 @@ export const useQueryStore = defineStore("query", () => {
let offset = 0;
const clientSessionId = tabClientSessionId(tab, "export");
const exportExecutionId = uuid();
const useAgentCursor = conn?.db_type === "sqlserver" && conn?.driver_profile?.trim().toLowerCase() === "sqlserver-legacy";
const useAgentCursor = usesAgentCursorForTableData(conn?.db_type, conn?.driver_profile);
let sessionId: string | undefined;
try {
@@ -8983,19 +8999,33 @@ export const useQueryStore = defineStore("query", () => {
},
);
const result = results[0];
if (!result) break;
if (!result) {
if (useAgentCursor) throw new Error("Missing cursor result during table export");
break;
}
const nextSessionId = result.session_id?.trim() || undefined;
if (useAgentCursor) {
sessionId = nextSessionId ?? sessionId;
if (conn?.db_type === "cassandra" && (result.truncated || typeof result.has_more !== "boolean")) throw new Error("Incomplete cursor result during table export");
if (result.has_more === true && !nextSessionId) throw new Error("Result session ended before table export completed");
}
if (columns.length === 0) columns = result.columns;
rows.push(...result.rows);
executionTimeMs += result.execution_time_ms ?? 0;
onProgress?.({ rowsExported: rows.length, totalRows });
sessionId = result.session_id ?? undefined;
sessionId = nextSessionId;
const shouldFetchNextPage = useAgentCursor ? result.has_more === true : result.rows.length >= pageLimit;
if (!shouldFetchNextPage) break;
offset += result.rows.length;
}
} finally {
if (sessionId) void api.closeQuerySession(tab.connectionId, executionDatabase, sessionId, clientSessionId, tableMeta.catalog);
void closeClientSessionId(tab.connectionId, executionDatabase, clientSessionId, tableMeta.catalog, { tabId: tab.id });
try {
if (sessionId) await api.closeQuerySession(tab.connectionId, executionDatabase, sessionId, clientSessionId, tableMeta.catalog);
} catch (error) {
queryExecutionLog("warn", "table-export-session-close:error", { sessionId, error });
} finally {
await closeClientSessionId(tab.connectionId, executionDatabase, clientSessionId, tableMeta.catalog, { tabId: tab.id });
}
}
return {