:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(dashboards): size virtualized table columns to their content (#123270)
Marius Andrace5c85b34084
diff --git a/frontend/snapshots.yml b/frontend/snapshots.ymlindex 20c7fee66ae7..125266b69037 100644--- a/frontend/snapshots.yml+++ b/frontend/snapshots.yml@@ -10645,17 +10645,17 @@ snapshots:scenes-app-data-warehouse-sql-editor--bi-saved-insight--light:hash: v1.k794b7964.2c5f15b06d4347f767fc34bf934ea1bd53e988c443d528edb457795e778cc556.FcMHWv530RuOEcqg3G_bnos4zzZ4mku3Q9pdsK-VCXUscenes-app-data-warehouse-sql-editor--bi-source-preview-large-sample--dark:- hash: v1.k794b7964.5be150e822bb692b47b8f7d04846613a6a5d43759c289c35076fb5773481cac6.wsA7jkLolGQWEQjQP8D3xOjPCCpFbu0sdJvUwEfIrOw+ hash: v1.k794b7964.0ed0cb3bd9b26b4a1f40f768c9e98293837944d66c6478f2a7fecce9809f7fbc.uqTpRZhKp00TK821I0X9H9VVi3feBkIYSQPw-rOXd7Escenes-app-data-warehouse-sql-editor--bi-source-preview-large-sample--light:- hash: v1.k794b7964.f9bdafe72c33b8454fe4d48e70433457801d2d198e398a25d58ff3926f4f29ab.VcdI3e-Cydfw21sTHkEAiZdXPeRVvBT6TBGUZbP5lTc+ hash: v1.k794b7964.ddb42f5556e7e09b1219e54d68f83213e8737ebbde55266a4dd1427d9cab5326.S77Umy6YW9HB314IPWIm5yyYg_G0KpQKpBoG99A-iBEscenes-app-data-warehouse-sql-editor--bi-table-analysis--dark:hash: v1.k794b7964.13e469538d6b0f6d5fb6f5a1d3b0f7c93a08282d843f28e1b54670cc234f1ef3.0LLI_p2z10NDsCrEq3dnR0bM-utFqudI3A5jwdkcARIscenes-app-data-warehouse-sql-editor--bi-table-analysis--light:hash: v1.k794b7964.44d69a1bbaccaf0b36c54de60fe8da960486b037d9c463ebd792babe5c06e5e9.fxjYqqNq6B7MDjD9WUXXn2D6PCVSTqWdPMbdlAo44NAscenes-app-data-warehouse-sql-editor--bi-wide-transposed-results--dark:- hash: v1.k794b7964.149351e0c2ce9de8ced8adf4a1c2a990069579b37d826dd4a745d630ef2395e5.wA9l58Kdj1OH6f_5SRT-_guSbpryqUVzCZWDhaFNulw+ hash: v1.k794b7964.6a420f7474558367a8c3e4ed5d3fa96f7d51edb3b4f7a6cac4e9af1b5cc0e531.Fcz42DazeV4XO1xHNi5h6puOzCUrftQY5jVIeXZut8kscenes-app-data-warehouse-sql-editor--bi-wide-transposed-results--light:- hash: v1.k794b7964.ac191f6f035db50e404e0a276d92a055e9a17990f20899ffd6558d2aa1b2c664.0ZOBH6x3bciLoDdJLpvR-Bm07GxBSjP9Mc7uZlBEpYc+ hash: v1.k794b7964.ca232e5bde8705f79816e729e77216e37fec62eb32164484e2364620b0d86dde.8AHKAl3ynJlU82UXlaJAAgcZsHhGw5-O1ZkAC5XVugEscenes-app-data-warehouse-sql-editor--customer-images--dark:hash: v1.k794b7964.10b9dd73c78556d1b3990ff4ba8d0872e17f375ed655c487b3ca9ca8af21c665.K_W25GnvkQUo791SarbtGIBFaySZ0v_Ebd4cv6sJhsQscenes-app-data-warehouse-sql-editor--customer-images--light:diff --git a/frontend/src/lib/lemon-ui/LemonTable/LemonTable.tsx b/frontend/src/lib/lemon-ui/LemonTable/LemonTable.tsxindex 3ea01a43332b..847e90e22717 100644--- a/frontend/src/lib/lemon-ui/LemonTable/LemonTable.tsx+++ b/frontend/src/lib/lemon-ui/LemonTable/LemonTable.tsx@@ -132,7 +132,8 @@ export interface LemonTableProps<T extends Record<string, any>, K extends BulkSeallowContentScroll?: boolean/** Requires a bounded scroll container. Expandable rows use the normal rendering path. */virtualized?: boolean- /** Fixed column width in pixels for horizontal virtualization of flat, non-spanning tables.+ /** Default column width in pixels for horizontal virtualization of flat, non-spanning tables.+ * Columns with a numeric `width` keep that width instead.* Grouped, expandable, pinned, selectable, and resizable tables use the normal rendering path. */virtualizedColumnWidth?: number/**@@ -445,27 +446,41 @@ export function LemonTable<T extends Record<string, any>, K extends BulkSelectio!bulkSelection &&!rowActions &&!columns.some((column) => column.resizable)+ const getVirtualizedColumnWidth = (column: LemonTableColumn<T, keyof T | undefined> | undefined): number =>+ typeof column?.width === 'number' ? column.width : (virtualizedColumnWidth ?? 192)const columnVirtualizer = useVirtualizer<HTMLDivElement, HTMLTableCellElement>({count: visibleColumns.length,getScrollElement: () => scrollRef.current,- estimateSize: () => virtualizedColumnWidth ?? 192,+ estimateSize: (index) => getVirtualizedColumnWidth(visibleColumns[index]),horizontal: true,overscan: 3,enabled: virtualizeColumns,})+ // The virtualizer caches estimated sizes, so measure again when the column widths change+ const virtualizedColumnWidthsKey = virtualizeColumns+ ? visibleColumns.map((column) => getVirtualizedColumnWidth(column)).join(',')+ : ''+ useEffect(() => {+ if (virtualizedColumnWidthsKey) {+ columnVirtualizer.measure()+ }+ }, [virtualizedColumnWidthsKey, columnVirtualizer])const virtualColumns = columnVirtualizer.getVirtualItems()const firstColumnIndex = virtualColumns[0]?.index ?? 0const lastColumnIndex = virtualColumns.at(-1)?.index ?? -1+ const columnsStart = virtualColumns[0]?.start ?? 0+ const columnsEnd = virtualColumns.at(-1)?.end ?? 0+ const columnsTotalSize = columnVirtualizer.getTotalSize()const columnWindow = useMemo(() =>virtualizeColumns? {startIndex: firstColumnIndex,- paddingLeft: firstColumnIndex * virtualizedColumnWidth!,- paddingRight: (visibleColumns.length - lastColumnIndex - 1) * virtualizedColumnWidth!,+ paddingLeft: columnsStart,+ paddingRight: Math.max(0, columnsTotalSize - columnsEnd),}: undefined,- [virtualizeColumns, firstColumnIndex, lastColumnIndex, virtualizedColumnWidth, visibleColumns.length]+ [virtualizeColumns, firstColumnIndex, columnsStart, columnsEnd, columnsTotalSize])const renderedColumnGroups = useMemo(() =>@@ -580,8 +595,8 @@ export function LemonTable<T extends Record<string, any>, K extends BulkSelectiovirtualizeColumns? {...tableStyle,- width: columnVirtualizer.getTotalSize(),- minWidth: columnVirtualizer.getTotalSize(),+ width: columnsTotalSize,+ minWidth: columnsTotalSize,}: tableStyle}@@ -603,7 +618,7 @@ export function LemonTable<T extends Record<string, any>, K extends BulkSelectio// width:1% is a standard workaround to shrink a column to its content.style={{width: virtualizeColumns- ? virtualizedColumnWidth+ ? getVirtualizedColumnWidth(column): column.width === 0? '1%': column.width,diff --git a/frontend/src/queries/nodes/DataVisualization/Components/Table.test.ts b/frontend/src/queries/nodes/DataVisualization/Components/Table.test.tsindex 5e4d3233ebc8..5a99ead8995c 100644--- a/frontend/src/queries/nodes/DataVisualization/Components/Table.test.ts+++ b/frontend/src/queries/nodes/DataVisualization/Components/Table.test.ts@@ -1,6 +1,6 @@import { TableDataCell } from '../dataVisualizationLogic'import { ColumnScalar } from '../types'-import { compareTableCells } from './Table'+import { MAX_COLUMN_WIDTH, MIN_COLUMN_WIDTH, compareTableCells, estimateColumnWidths } from './Table'function cell(value: string | number | boolean | Date | null, type: ColumnScalar = 'STRING'): TableDataCell<any> {return { value, formattedValue: value === null ? null : String(value), type }@@ -29,3 +29,66 @@ describe('compareTableCells', () => {).toBe(expected)})})++describe('estimateColumnWidths', () => {+ const widthsFor = (titles: string[], rows: (string | number | null)[][]): number[] =>+ estimateColumnWidths(titles, rows.length, (rowIndex, columnIndex) => cell(rows[rowIndex][columnIndex]))++ it('keeps short columns narrow and widens columns with longer values', () => {+ const [period, mrr, label] = widthsFor(+ ['period', 'mrr', 'label'],+ [+ ['2026-11', '$8,565,043', 'A much longer description for this row'],+ ['2026-10', '$7,973,096', 'short'],+ ]+ )++ expect(period).toBeLessThan(150)+ expect(mrr).toBeLessThan(150)+ expect(label).toBeGreaterThan(mrr)+ expect(label).toBeLessThanOrEqual(MAX_COLUMN_WIDTH)+ })++ it('stays inside the width bounds for empty and very long content', () => {+ const [empty, long] = widthsFor(['a', 'b'], [[null, 'x'.repeat(1000)]])++ expect(empty).toBeGreaterThanOrEqual(MIN_COLUMN_WIDTH)+ expect(empty).toBeLessThan(100)+ expect(long).toBe(MAX_COLUMN_WIDTH)+ })++ it('fits the longest header segment, because headers wrap at underscores', () => {+ const [underscored, unbroken] = widthsFor(+ ['a_b_c_d_e_f_g_h_i_j_k_l_m_n', 'abcdefghijklmnopqrstuvwxyz'],+ [[1, 1]]+ )++ expect(underscored).toBeLessThan(unbroken)+ })++ it('includes values found late in the result', () => {+ const rows = Array.from({ length: 1000 }, (_, index) => [index % 100 === 99 ? 'x'.repeat(30) : 'x'])+ const getCell = jest.fn((rowIndex: number, columnIndex: number) => cell(rows[rowIndex][columnIndex]))++ const [width] = estimateColumnWidths(['value'], rows.length, getCell)++ expect(width).toBeGreaterThan(200)+ expect(getCell.mock.calls.length).toBeLessThanOrEqual(1000)+ })++ it('keeps the sampled cells within the budget for very wide tables', () => {+ const columnCount = 10_001+ const titles = Array.from({ length: columnCount }, (_, index) => `row_${index}`)+ const getCell = jest.fn((_rowIndex: number, columnIndex: number) =>+ cell(columnIndex === 0 ? 'field_name' : 'x'.repeat(20))+ )++ const widths = estimateColumnWidths(titles, 10, getCell)++ expect(getCell.mock.calls.length).toBeLessThanOrEqual(5000)+ expect(widths).toHaveLength(columnCount)+ // Unsampled columns reuse the typical sampled width+ expect(widths[1]).toBe(widths[columnCount - 2])+ expect(widths[1]).toBeGreaterThan(150)+ })+})diff --git a/frontend/src/queries/nodes/DataVisualization/Components/Table.tsx b/frontend/src/queries/nodes/DataVisualization/Components/Table.tsxindex 3b9e2171d714..894972305d69 100644--- a/frontend/src/queries/nodes/DataVisualization/Components/Table.tsx+++ b/frontend/src/queries/nodes/DataVisualization/Components/Table.tsx@@ -2,7 +2,7 @@ import '../../DataTable/DataTable.scss'import { useActions, useValues } from 'kea'import posthog from 'posthog-js'-import React, { useCallback, useLayoutEffect, useRef, useState } from 'react'+import React, { useCallback, useLayoutEffect, useMemo, useRef, useState } from 'react'import { IconPin, IconPinFilled } from '@posthog/icons'import { LemonBanner, LemonButton, LemonTable, LemonTableColumn, Tooltip } from '@posthog/lemon-ui'@@ -97,6 +97,78 @@ export function compareTableCells(a: TableDataCell<any> | undefined, b: TableDat// Plain-text representation of a cell, used as the hover title so clipped content is// still visible on hover. The full value stays in the DOM (CSS ellipsis only), so// selecting and copying a cell copies the whole value, not just the clipped portion.+// Fixed-layout tables can't size columns from their content, so estimate the widths from character counts.+// The estimates use the table font sizes and avoid DOM measurement, which would read every offscreen cell.+const CELL_CHARACTER_WIDTH = 7.5+const HEADER_CHARACTER_WIDTH = 8.5+const HEADER_ICONS_WIDTH = 44+const HEADER_LINE_MAX_WIDTH = 160+const CELL_HORIZONTAL_PADDING = 16+const EDGE_CELL_EXTRA_PADDING = 16+export const MIN_COLUMN_WIDTH = 64+export const MAX_COLUMN_WIDTH = 320+const COLUMN_WIDTH_SAMPLE_CELLS = 5000+const MIN_COLUMN_WIDTH_SAMPLE_ROWS = 10++function getCellTextLength(cell: TableDataCell<any>): number {+ if (cell.isTransposedHeader && cell.sourceColumnName) {+ return cell.sourceColumnName.length+ }+ if (cell.formattedValue !== null && typeof cell.formattedValue === 'object') {+ return Infinity+ }+ return String(cell.formattedValue ?? cell.value ?? '').length+}++/** Estimates pixel widths that fit each column's header and a sample of its cells. */+export function estimateColumnWidths(+ columnTitles: string[],+ rowCount: number,+ getCell: (sampleRowIndex: number, columnIndex: number) => TableDataCell<any>+): number[] {+ const columnCount = columnTitles.length+ // Keep the total number of formatted cells within the budget. Very wide tables (e.g. a transposed+ // result) sample a spread of columns, and the other columns reuse the typical sampled width.+ const minSampleRows = Math.max(1, Math.min(MIN_COLUMN_WIDTH_SAMPLE_ROWS, rowCount))+ const sampledColumnCount = Math.min(columnCount, Math.floor(COLUMN_WIDTH_SAMPLE_CELLS / minSampleRows))+ const sampleRowCount = Math.min(rowCount, Math.floor(COLUMN_WIDTH_SAMPLE_CELLS / Math.max(1, sampledColumnCount)))+ // Spread the sample over the whole result, so a long tail of wider values still counts+ const sampleRowIndexes = Array.from({ length: sampleRowCount }, (_, index) =>+ Math.floor((index * rowCount) / sampleRowCount)+ )++ const longestCellLengths = new Map<number, number>()+ for (let index = 0; index < sampledColumnCount; index++) {+ const columnIndex = Math.floor((index * columnCount) / sampledColumnCount)+ let longestCellLength = 0+ for (const rowIndex of sampleRowIndexes) {+ longestCellLength = Math.max(longestCellLength, getCellTextLength(getCell(rowIndex, columnIndex)))+ if (longestCellLength * CELL_CHARACTER_WIDTH >= MAX_COLUMN_WIDTH) {+ break+ }+ }+ longestCellLengths.set(columnIndex, longestCellLength)+ }+ const sampledLengths = [...longestCellLengths.values()].sort((a, b) => a - b)+ const typicalCellLength = sampledLengths[Math.floor(sampledLengths.length / 2)] ?? 0++ return columnTitles.map((title, columnIndex) => {+ // Headers wrap at spaces, dashes, and underscores, so only the longest segment must fit on one line+ const longestTitleSegment = Math.max(0, ...title.split(/[\s_-]+/).map((segment) => segment.length + 1))+ const headerWidth =+ Math.max(+ longestTitleSegment * HEADER_CHARACTER_WIDTH,+ Math.min(title.length * HEADER_CHARACTER_WIDTH, HEADER_LINE_MAX_WIDTH)+ ) + HEADER_ICONS_WIDTH++ const longestCellLength = longestCellLengths.get(columnIndex) ?? typicalCellLength+ const isEdgeColumn = columnIndex === 0 || columnIndex === columnCount - 1+ const padding = CELL_HORIZONTAL_PADDING + (isEdgeColumn ? EDGE_CELL_EXTRA_PADDING : 0)+ const contentWidth = Math.max(headerWidth, longestCellLength * CELL_CHARACTER_WIDTH)+ return Math.round(Math.min(MAX_COLUMN_WIDTH, Math.max(MIN_COLUMN_WIDTH, contentWidth + padding)))+ })+}+function getCellTitle(cell: TableDataCell<any>): string | undefined {if (typeof cell.formattedValue === 'string') {return cell.formattedValue@@ -177,8 +249,22 @@ export const Table = (props: TableProps): JSX.Element => {!!props.embedded &&!!props.context?.dataVisualizationTableVirtualized &&tableRows.length * tabularColumns.length > virtualizationCellThreshold+ const virtualizeColumns = (virtualized || isTransposed) && tabularColumns.length > 50const { toggleColumnPin, setTableSorted } = useActions(dataVisualizationLogic)+ // Virtualized tables use a fixed layout, which can't fit columns to cells that aren't rendered+ const columnWidths = useMemo(+ () =>+ virtualized || virtualizeColumns+ ? estimateColumnWidths(+ tabularColumns.map(({ column, settings }) => settings?.display?.label || column.name),+ tableRows.length,+ (sampleRowIndex, columnIndex) => getTableCell(tableRows[sampleRowIndex].rowIndex, columnIndex)+ )+ : undefined,+ [virtualized, virtualizeColumns, tabularColumns, tableRows, getTableCell]+ )+const sourceTabularColumnsByName = new Map(sourceTabularColumns.map((column) => [column.column.name, column]))const tableColumns = tabularColumns.map<LemonTableColumn<TableRow, any>>(({ column, settings }, index) => {@@ -260,6 +346,7 @@ export const Table = (props: TableProps): JSX.Element => {return {...columnMeta,+ width: columnWidths?.[index] ?? columnMeta.width,key: column.name,// Sorting reorders rows, which doesn't make sense once the table is transposed// (rows become the original columns), so only offer it in the normal orientation.@@ -434,9 +521,11 @@ export const Table = (props: TableProps): JSX.Element => {embedded={props.embedded}allowContentScroll={!!props.embedded}virtualized={virtualized}- virtualizedColumnWidth={(virtualized || isTransposed) && tabularColumns.length > 50 ? 192 : undefined}+ virtualizedColumnWidth={virtualizeColumns ? 192 : undefined}tableLayout={virtualized ? 'fixed' : 'auto'}- tableStyle={virtualized ? { minWidth: `${tabularColumns.length * 12}rem` } : undefined}+ tableStyle={+ columnWidths ? { minWidth: columnWidths.reduce((total, width) => total + width, 0) } : undefined+ }/></>)diff --git a/products/dashboards/PERFORMANCE.md b/products/dashboards/PERFORMANCE.mdindex 4319abc0191a..a63e288f0110 100644--- a/products/dashboards/PERFORMANCE.md+++ b/products/dashboards/PERFORMANCE.md@@ -155,7 +155,8 @@ A fast network response can still cause a slow interface if parsing, state propaA slow request does not explain a delayed local drag unless the interaction actually waits for it.SQL result tables in dashboard cards use row virtualization inside the tile's scroll area.-Tables with more than 50 columns also use column virtualization with fixed column widths, unless pinned columns require the full column layout.+Virtualized tables use a fixed layout. Each column width comes from its header and a sample of its cells, between 4rem and 20rem.+Tables with more than 50 columns also use column virtualization with those widths, unless pinned columns require the full column layout.Image exports keep the full table layout.Table renderers prepare cells by row and column index, including transposed cells, so offscreen cells do not require formatting before display.Sorting prepares the selected column once per column configuration; comparisons reuse those cells.