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

@@ -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(),