fix(grid): budget large-value previews by segment and show executed SQL

This commit is contained in:
zipg authored and GitHub committed 2026-09-30 00:59:18 +08:00
1 parent de70545c70
commit 237064f9e3
12 files changed
+194 -10

No files matched your search

+19 -3
View File
@@ -217,6 +217,7 @@ import {
canGoNextDataGridPage,
dataGridLoadAllSegment,
dataGridTotalRowCountLabelKey,
dataGridUserFacingPage,
dataGridTruncationHintKey,
ELASTICSEARCH_PAGE_JUMP_WARNING_REQUESTS,
elasticsearchCursorPageJumpRequestCount,
@@ -501,6 +502,13 @@ interface DataGridProps {
autoShowTableInfo?: boolean;
pageOffset?: number;
pageLimit?: number;
/**
* Pagination of the segment that produced the currently displayed rows. It
* differs from `pageOffset`/`pageLimit` once a result was extended by
* "load all" or infinite scroll, because those stay on the logical first page.
*/
executedPageOffset?: number;
executedPageLimit?: number;
countSql?: string;
totalRowCount?: number;
totalRowCountIsExact?: boolean;
@@ -7858,6 +7866,14 @@ async function syncUserFacingSql() {
try {
const config = props.connectionId ? connectionStore.getConfig(props.connectionId) : undefined;
const footerPage = dataGridUserFacingPage({
executedPageLimit: props.executedPageLimit,
executedPageOffset: props.executedPageOffset,
pageLimit: props.pageLimit,
pageOffset: props.pageOffset,
fallbackLimit: pageSize.value,
fallbackOffset: Math.max(0, currentPage.value - 1) * pageSize.value,
});
const sql = await buildTableSelectSql({
databaseType: resolvedDatabaseType.value,
driverProfile: config?.driver_profile,
@@ -7872,8 +7888,8 @@ async function syncUserFacingSql() {
injectDefaultTimeSeriesWhere: true,
whereInput: currentWhereInput(),
orderBy: currentOrderBy(),
limit: props.pageLimit ?? pageSize.value,
offset: props.pageOffset ?? Math.max(0, currentPage.value - 1) * pageSize.value,
limit: footerPage.limit,
offset: footerPage.offset,
});
if (generation === userFacingSqlGeneration) userFacingSql.value = sqlWithDisplayDatabaseName(sql);
} catch {
@@ -7882,7 +7898,7 @@ async function syncUserFacingSql() {
}
watch(
() => [props.sql, props.context, props.tableMeta, props.pageLimit, props.pageOffset, currentWhereInput(), currentOrderBy(), settingsStore.editorSettings.generateSqlIncludeDatabaseName],
() => [props.sql, props.context, props.tableMeta, props.pageLimit, props.pageOffset, props.executedPageLimit, props.executedPageOffset, currentWhereInput(), currentOrderBy(), settingsStore.editorSettings.generateSqlIncludeDatabaseName],
() => void syncUserFacingSql(),
{ immediate: true },
);
@@ -2352,6 +2352,8 @@ defineExpose({
:table-info-tab="activeTab.tableInfoTab"
:page-offset="activeTab.resultPageOffset"
:page-limit="activeTab.resultPageLimit"
:executed-page-offset="activeTab.resultExecutedPageOffset"
:executed-page-limit="activeTab.resultExecutedPageLimit"
:count-sql="activeTab.resultCountSql"
:count-total-rows="activeTab.resultCountSql ? () => queryStore.countTabResultRows(activeTab.id) : undefined"
:total-row-count="activeTab.resultTotalRowCount"
@@ -2802,6 +2804,8 @@ defineExpose({
:auto-show-table-info="settingsStore.editorSettings.tableInfoDrawerPinned"
:page-offset="activeTab.resultPageOffset"
:page-limit="activeTab.resultPageLimit"
:executed-page-offset="activeTab.resultExecutedPageOffset"
:executed-page-limit="activeTab.resultExecutedPageLimit"
:total-row-count="activeTab.resultTotalRowCount"
:total-row-count-is-exact="activeTab.resultTotalRowCount !== undefined || activeTab.result.total_is_exact !== false"
:total-row-count-loading="activeTab.resultTotalRowCountLoading"
@@ -589,6 +589,35 @@ describe("useDataGridActions", () => {
expect(mocks.executeTabSql).toHaveBeenCalledWith("tab-1", "SELECT * FROM public.users LIMIT 100 OFFSET 100", expect.objectContaining({ appendResult: { maxRows: 10_000 } }));
});
it("budgets a load-all large-value preview against the rows the table still has", async () => {
const columns = [
{ name: "id", data_type: "bigint", is_nullable: false, column_default: null, is_primary_key: true, extra: null },
...Array.from({ length: 10 }, (_, index) => ({ name: `payload_${index}`, data_type: "varchar(64)", is_nullable: true, column_default: null, is_primary_key: false, extra: null })),
];
const tab = tableDataTab({
resultTotalRowCount: 506,
resultPageLimit: 100,
resultPageOffset: 0,
result: {
columns: ["id", "payload_0"],
rows: Array.from({ length: 100 }, (_, index) => [index + 1, "19999386"]),
affected_rows: 0,
execution_time_ms: 1,
},
tableMeta: { schema: "ads", tableName: "ads_platform_order_receiver", tableType: "TABLE", columns, primaryKeys: ["id"] },
});
mocks.buildTableSelectSql.mockResolvedValueOnce('SELECT * FROM "ads"."ads_platform_order_receiver" LIMIT 99900 OFFSET 100;');
mocks.tabs.push(tab);
const actions = useDataGridActions(computed(() => tab));
// "Load all" asks for the whole remaining table; the preview budget has to be
// spread over the 406 rows that can still arrive, not over the 99_900 asked for.
await actions.onPaginate(tab.id, 100, 99_900, "", undefined, true);
expect(mocks.buildTableSelectSql).toHaveBeenCalledWith(expect.objectContaining({ limit: 99_900, offset: 100, largeValuePreviewSize: 1033 }));
expect(mocks.executeTabSql).toHaveBeenCalledWith("tab-1", expect.any(String), expect.objectContaining({ pagination: { offset: 100, limit: 99_900, sessionId: undefined, clientSessionId: undefined } }));
});
it("continues the matching Cassandra table cursor and client session", async () => {
mocks.infiniteScroll = false;
mocks.getConfig.mockReturnValue({ id: "cassandra-1", db_type: "cassandra" });
@@ -6,7 +6,7 @@ import { useQueryStore } from "@/stores/queryStore";
import { useSettingsStore } from "@/stores/settingsStore";
import { buildTableSelectSql, quoteTableDataIdentifier } from "@/lib/table/tableSelectSql";
import { tableOpenPageLimit } from "@/lib/table/tableOpenPageLimit";
import { tableDataLargeValuePreviewOptions } from "@/lib/dataGrid/dataGridLargeValues";
import { tableDataLargeValuePreviewOptions, tableDataPreviewRowBudget } from "@/lib/dataGrid/dataGridLargeValues";
import { elasticsearchCursorPageJumpRequestCount } from "@/lib/dataGrid/dataGridPagination";
import { editablePrimaryKeys, shouldIncludeSyntheticRowId } from "@/lib/table/tableEditing";
import { tableMetaForDataTab } from "@/lib/table/tableDataTabMeta";
@@ -108,7 +108,8 @@ export function useDataGridActions(activeTab: ComputedRef<QueryTab | undefined>)
return typeof limit === "number" && limit > 0 ? { limit, offset: 0 } : undefined;
}
function buildTableSql(tab: QueryTab, options: { orderBy?: string; limit?: number; offset?: number; whereInput?: string } = {}): Promise<string> {
function buildTableSql(tab: QueryTab, options: { orderBy?: string; limit?: number; offset?: number; whereInput?: string; previewPageSize?: number } = {}): Promise<string> {
const { previewPageSize, ...sqlOptions } = options;
const config = connectionStore.getConfig(tab.connectionId);
const effectiveDbType = effectiveDatabaseTypeForConnection(config);
const tableMeta = tableMetaForDataTab(tab);
@@ -118,7 +119,9 @@ export function useDataGridActions(activeTab: ComputedRef<QueryTab | undefined>)
// 结果(可能是失败结果的 ["Error"]),进入 SQL 会生成非法投影;
// 真实列缺失时省略 columns 让 builder 生成 SELECT *
const realColumns = tab.tableMeta?.columns.length ? tab.tableMeta.columns : undefined;
const limit = options.limit ?? tableDataPageLimit(tab);
const limit = sqlOptions.limit ?? tableDataPageLimit(tab);
// The preview budget is a per-fetch byte budget, so it has to be spread over
// the rows this fetch returns rather than over the rows it asks for.
return buildTableSelectSql({
databaseType: effectiveDbType,
driverProfile: config?.driver_profile,
@@ -131,12 +134,12 @@ export function useDataGridActions(activeTab: ComputedRef<QueryTab | undefined>)
catalog: tableMeta?.catalog,
columns: realColumns?.map((column) => column.name),
primaryKeys,
...tableDataLargeValuePreviewOptions(effectiveDbType, realColumns ?? [], primaryKeys, limit),
...tableDataLargeValuePreviewOptions(effectiveDbType, realColumns ?? [], primaryKeys, previewPageSize ?? limit),
includeDatabaseName: settingsStore.editorSettings.generateSqlIncludeDatabaseName,
includeRowId: useRowId,
limit,
injectDefaultTimeSeriesWhere: true,
...options,
...sqlOptions,
});
}
@@ -599,7 +602,18 @@ export function useDataGridActions(activeTab: ComputedRef<QueryTab | undefined>)
if (!tableMetaForDataTab(tab)) return;
tab.whereInput = whereInput ?? "";
const sql = await buildTableSql(tab, { limit, offset, whereInput, orderBy: orderBy ?? tab.orderByInput });
const sql = await buildTableSql(tab, {
limit,
offset,
whereInput,
orderBy: orderBy ?? tab.orderByInput,
previewPageSize: tableDataPreviewRowBudget({
limit,
offset,
expectedTotalRows: tab.resultTotalRowCount,
loadedRowCount: tab.result?.rows.length ?? 0,
}),
});
queryStore.updateSql(tab.id, sql);
const expectedNextOffset = appendResult ? tab.result?.rows.length : (tab.resultPageOffset ?? 0) + (tab.resultPageLimit ?? limit);
const continuesResultSession = offset === expectedNextOffset && limit === tab.resultPageLimit;
@@ -10,6 +10,7 @@ import {
TABLE_DATA_CELL_PREVIEW_SIZE,
TABLE_DATA_PREVIEW_CONTENT_MAX_BYTES,
tableDataLargeValuePreviewOptions,
tableDataPreviewRowBudget,
tableDataVisiblePreviewContentBytes,
tableDataVisiblePreviewRowRange,
} from "@/lib/dataGrid/dataGridLargeValues";
@@ -79,6 +80,35 @@ describe("data grid large-value metadata", () => {
}
});
it("spreads the load-all byte budget over the rows the segment will really return", () => {
// Issue #10661: a 506-row table asked for a 99_900-row "load all" segment.
// Budgeting over the request instead of the remaining rows shrank every cell
// to four characters ("1999..."), so off-screen rows arrived already garbled.
const rowBudget = tableDataPreviewRowBudget({ limit: 99_900, offset: 100, expectedTotalRows: 506, loadedRowCount: 100 });
expect(rowBudget).toBe(406);
const columns = [column("id", "bigint", true), ...Array.from({ length: 10 }, (_, index) => column(`payload_${index}`, "varchar(64)"))];
const loadAll = tableDataLargeValuePreviewOptions("postgres", columns, ["id"], rowBudget);
const requested = tableDataLargeValuePreviewOptions("postgres", columns, ["id"], 99_900);
expect(loadAll).toMatchObject({ largeValuePreviewSize: 1033 });
expect(requested).toMatchObject({ largeValuePreviewSize: 4 });
});
it("keeps the requested limit when the real row count is unknown or unusable", () => {
// A normal page never shrinks the budget.
expect(tableDataPreviewRowBudget({ limit: 100, offset: 0, expectedTotalRows: 506, loadedRowCount: 100 })).toBe(100);
expect(tableDataPreviewRowBudget({ limit: 100, offset: 400, expectedTotalRows: 506, loadedRowCount: 400 })).toBe(100);
// Last short page may use the rows that can still arrive.
expect(tableDataPreviewRowBudget({ limit: 100, offset: 450, expectedTotalRows: 506, loadedRowCount: 450 })).toBe(56);
// No known total: keep the previous behaviour.
expect(tableDataPreviewRowBudget({ limit: 99_900, offset: 100, expectedTotalRows: undefined, loadedRowCount: 100 })).toBe(99_900);
// A total below the rows already on screen (e.g. an affected_rows hint of 1)
// must not balloon the preview budget.
expect(tableDataPreviewRowBudget({ limit: 99_900, offset: 100, expectedTotalRows: 1, loadedRowCount: 100 })).toBe(99_900);
expect(tableDataPreviewRowBudget({ limit: 99_900, offset: 100, expectedTotalRows: Number.NaN, loadedRowCount: 100 })).toBe(99_900);
expect(tableDataPreviewRowBudget({ limit: 99_900, offset: 506, expectedTotalRows: 506, loadedRowCount: 506 })).toBe(99_900);
});
it("selects the visible rows plus a bounded viewport buffer", () => {
expect(tableDataVisiblePreviewRowRange(26 * 50, 26 * 20, 26, 10_000)).toEqual({ start: 30, end: 90 });
expect(tableDataVisiblePreviewRowRange(0, 26 * 20, 26, 10_000)).toEqual({ start: 0, end: 60 });
@@ -178,6 +178,23 @@ export function canUseTableDataLargeValuePreview(databaseType: DatabaseType | un
return (databaseType === "mysql" || databaseType === "postgres") && columns.length > 0 && primaryKeys.length > 0 && !columns.some((column) => column.name.toLocaleUpperCase().startsWith(TABLE_DATA_LARGE_VALUE_MARKER_PREFIX));
}
/**
* Rows one fetch is expected to return, used to spread the large-value byte
* budget over the rows that will actually arrive. "Load all" and infinite scroll
* ask for the whole remaining table in one segment, so dividing the budget by
* that request shrinks every preview to a couple of characters even when the
* table only holds a few hundred rows.
*/
export function tableDataPreviewRowBudget(options: { limit: number; offset: number; expectedTotalRows: number | undefined; loadedRowCount: number }): number {
const { limit, offset, expectedTotalRows, loadedRowCount } = options;
// A total below the rows already on screen is not a usable row estimate (e.g.
// an `affected_rows` hint of 1), so keep the requested limit.
if (typeof expectedTotalRows !== "number" || !Number.isFinite(expectedTotalRows) || expectedTotalRows < loadedRowCount) return limit;
const remainingRows = expectedTotalRows - offset;
if (remainingRows <= 0 || remainingRows >= limit) return limit;
return remainingRows;
}
export function tableDataLargeValuePreviewOptions(databaseType: DatabaseType | undefined, columns: readonly ColumnInfo[], primaryKeys: readonly string[], pageSize?: number): { columnTypes: string[]; largeValuePreviewSize: number } | Record<string, never> {
if (!canUseTableDataLargeValuePreview(databaseType, columns, primaryKeys)) return {};
const keyColumns = new Set(primaryKeys.map((column) => column.toLocaleLowerCase()));
@@ -115,6 +115,19 @@ export function canFetchNextDataGridSegment(options: CanFetchNextDataGridSegment
return options.loadedRowCount >= Math.max(1, options.pageSize);
}
/**
* Page that the SQL shown under the grid describes. `executedPage*` is the
* segment that actually ran — it can be the whole remaining table after
* "load all" — while `page*` intentionally stays on the logical first page of
* the displayed result, so it cannot describe an extended result on its own.
*/
export function dataGridUserFacingPage(options: { executedPageLimit?: number; executedPageOffset?: number; pageLimit?: number; pageOffset?: number; fallbackLimit: number; fallbackOffset: number }): { limit: number; offset: number } {
return {
limit: options.executedPageLimit ?? options.pageLimit ?? options.fallbackLimit,
offset: options.executedPageOffset ?? options.pageOffset ?? options.fallbackOffset,
};
}
export function dataGridLoadAllSegment(loadedRowCount: number, maxRows: number, canFetchMore: boolean): DataGridLoadAllSegment | null {
const offset = Number.isFinite(loadedRowCount) ? Math.max(0, Math.trunc(loadedRowCount)) : 0;
const boundedMaxRows = Number.isFinite(maxRows) ? Math.max(0, Math.trunc(maxRows)) : 0;
@@ -49,6 +49,8 @@ export interface TabResultSnapshot {
resultPageSql?: string;
resultPageLimit?: number;
resultPageOffset?: number;
resultExecutedPageLimit?: number;
resultExecutedPageOffset?: number;
resultCountSql?: string;
resultTotalRowCount?: number;
cachedAt: number;
@@ -811,6 +813,8 @@ export function buildTabResultSnapshot(tab: QueryTab): TabResultSnapshot | undef
resultPageSql: tab.resultPageSql,
resultPageLimit: tab.resultPageLimit,
resultPageOffset: tab.resultPageOffset,
resultExecutedPageLimit: tab.resultExecutedPageLimit,
resultExecutedPageOffset: tab.resultExecutedPageOffset,
resultCountSql: tab.resultCountSql,
resultTotalRowCount: tab.resultTotalRowCount,
cachedAt: Date.now(),
+22
View File
@@ -1775,6 +1775,8 @@ export const useQueryStore = defineStore("query", () => {
tab.resultPageSql = undefined;
tab.resultPageLimit = undefined;
tab.resultPageOffset = undefined;
tab.resultExecutedPageLimit = undefined;
tab.resultExecutedPageOffset = undefined;
tab.resultCountSql = undefined;
tab.resultTotalRowCount = undefined;
tab.resultTotalRowCountLoading = false;
@@ -1856,6 +1858,8 @@ export const useQueryStore = defineStore("query", () => {
tab.resultPageSql = run.resultPageSql;
tab.resultPageLimit = run.resultPageLimit;
tab.resultPageOffset = run.resultPageOffset;
tab.resultExecutedPageLimit = run.resultExecutedPageLimit;
tab.resultExecutedPageOffset = run.resultExecutedPageOffset;
tab.resultCountSql = run.resultCountSql;
tab.resultTotalRowCount = run.resultTotalRowCount;
tab.resultTotalRowCountLoading = run.resultTotalRowCountLoading;
@@ -2131,6 +2135,8 @@ export const useQueryStore = defineStore("query", () => {
resultPageSql: run.resultPageSql,
resultPageLimit: run.resultPageLimit,
resultPageOffset: run.resultPageOffset,
resultExecutedPageLimit: run.resultExecutedPageLimit,
resultExecutedPageOffset: run.resultExecutedPageOffset,
resultCountSql: run.resultCountSql,
resultTotalRowCount: run.resultTotalRowCount,
cachedAt: Date.now(),
@@ -2199,6 +2205,8 @@ export const useQueryStore = defineStore("query", () => {
resultPageSql: tab.resultPageSql,
resultPageLimit: tab.resultPageLimit,
resultPageOffset: tab.resultPageOffset,
resultExecutedPageLimit: tab.resultExecutedPageLimit,
resultExecutedPageOffset: tab.resultExecutedPageOffset,
resultCountSql: tab.resultCountSql,
resultTotalRowCount: tab.resultTotalRowCount,
resultTotalRowCountLoading: tab.resultTotalRowCountLoading,
@@ -2306,6 +2314,8 @@ export const useQueryStore = defineStore("query", () => {
resultPageSql: tab.resultPageSql,
resultPageLimit: tab.resultPageLimit,
resultPageOffset: tab.resultPageOffset,
resultExecutedPageLimit: tab.resultExecutedPageLimit,
resultExecutedPageOffset: tab.resultExecutedPageOffset,
resultCountSql: tab.resultCountSql,
resultTotalRowCount: tab.resultTotalRowCount,
resultTotalRowCountLoading: tab.resultTotalRowCountLoading,
@@ -4647,6 +4657,8 @@ export const useQueryStore = defineStore("query", () => {
resultPageSql: undefined,
resultPageLimit: undefined,
resultPageOffset: undefined,
resultExecutedPageLimit: undefined,
resultExecutedPageOffset: undefined,
resultCountSql: undefined,
resultTotalRowCount: undefined,
resultTotalRowCountLoading: undefined,
@@ -7513,6 +7525,8 @@ export const useQueryStore = defineStore("query", () => {
current.resultPageSql = undefined;
current.resultPageLimit = mongoFindPageState?.pageLimit;
current.resultPageOffset = shouldAppendResult ? (current.resultPageOffset ?? 0) : mongoFindPageState?.pageOffset;
current.resultExecutedPageLimit = mongoFindPageState?.pageLimit;
current.resultExecutedPageOffset = mongoFindPageState?.pageOffset;
current.resultCountSql = undefined;
current.resultSessionId = undefined;
current.resultClientSessionId = undefined;
@@ -8029,6 +8043,10 @@ export const useQueryStore = defineStore("query", () => {
const displayPageLimit = typeof pageLimit === "number" ? (requestedPageLimit ?? pageLimit) : undefined;
current.resultPageLimit = shouldAppendResult ? (current.resultPageLimit ?? displayPageLimit) : displayPageLimit;
current.resultPageOffset = shouldAppendResult ? (current.resultPageOffset ?? 0) : pageOffset;
// An appended segment keeps the base page above, but the SQL shown for the
// grid has to describe the segment that actually ran.
current.resultExecutedPageLimit = pageLimit;
current.resultExecutedPageOffset = pageOffset;
current.resultCountSql = countSql;
current.resultSessionId = current.result?.session_id ?? undefined;
current.resultClientSessionId = current.resultSessionId ? executionClientSessionId : undefined;
@@ -8205,6 +8223,8 @@ export const useQueryStore = defineStore("query", () => {
current.resultPageSql = pageSql;
current.resultPageLimit = typeof pageLimit === "number" ? (requestedPageLimit ?? pageLimit) : undefined;
current.resultPageOffset = pageOffset;
current.resultExecutedPageLimit = pageLimit;
current.resultExecutedPageOffset = pageOffset;
current.resultCountSql = countSql;
current.resultSessionId = undefined;
current.resultClientSessionId = undefined;
@@ -8881,6 +8901,8 @@ export const useQueryStore = defineStore("query", () => {
// Restoring this state must not change the global page-size defaults.
tab.resultPageLimit = snapshot.resultPageLimit;
tab.resultPageOffset = snapshot.resultPageOffset;
tab.resultExecutedPageLimit = snapshot.resultExecutedPageLimit;
tab.resultExecutedPageOffset = snapshot.resultExecutedPageOffset;
tab.resultCountSql = snapshot.resultCountSql;
tab.resultTotalRowCount = snapshot.resultTotalRowCount;
tab.resultTotalRowCountLoading = false;
+11
View File
@@ -1440,6 +1440,8 @@ export interface QueryResultRun {
resultPageSql?: string;
resultPageLimit?: number;
resultPageOffset?: number;
resultExecutedPageLimit?: number;
resultExecutedPageOffset?: number;
resultCountSql?: string;
resultTotalRowCount?: number;
resultTotalRowCountLoading?: boolean;
@@ -1913,6 +1915,15 @@ export interface QueryTab {
resultPageSql?: string;
resultPageLimit?: number;
resultPageOffset?: number;
/**
* Pagination of the execution that actually produced (or extended) the
* displayed result. `resultPageLimit`/`resultPageOffset` deliberately stay on
* the logical first page so a later refresh never re-runs only the appended
* tail segment; this pair mirrors the segment that ran instead, which the grid
* needs to show the SQL behind the current rows after "load all".
*/
resultExecutedPageLimit?: number;
resultExecutedPageOffset?: number;
resultCountSql?: string;
resultTotalRowCount?: number;
resultTotalRowCountLoading?: boolean;