From f882dba42e799e50a51674bf4f7d5be77f081ea1 Mon Sep 17 00:00:00 2001 From: Alex Fedotyev <61838744+alex-fedotyev@users.noreply.github.com> Date: Fri, 20 Nov 2026 06:02:41 +0000 Subject: [PATCH 1/2] fix(app): keep search counts exact when a materialized view serves the chart The search page asks for the exact selected range so the histogram and the results count match the results table (#2256). When a materialized view served those queries, the optimizer widened the range to whole view buckets, so counts included rows from outside the selection: about 1.7% more spans on a one-hour window on the demo data, and much more for bursty series. For requests with alignDateRangeToGranularity: false, the partial first and last chart buckets are now read from the source table with the exact bounds, and only the whole buckets in between from the view. Each bucket still comes from a single table, so counts, averages and percentiles all stay exact. Co-Authored-By: Claude Opus 5.5 --- .changeset/exact-search-counts-on-mvs.md | 5 + .../hooks/__tests__/useChartConfig.test.tsx | 256 ++++++++++++++++++ packages/app/src/hooks/useChartConfig.tsx | 153 +++++++++-- 3 files changed, 398 insertions(+), 16 deletions(-) create mode 100644 .changeset/exact-search-counts-on-mvs.md diff --git a/.changeset/exact-search-counts-on-mvs.md b/.changeset/exact-search-counts-on-mvs.md new file mode 100644 index 0000000000..a373fae127 --- /dev/null +++ b/.changeset/exact-search-counts-on-mvs.md @@ -0,0 +1,5 @@ +--- +'@hyperdx/app': patch +--- + +fix: keep search counts exact when a materialized view serves the histogram and trace RED charts. The view only holds whole buckets, so the partial first and last buckets of the selected range now come from the source table, and the results count again matches the rows in the results table. diff --git a/packages/app/src/hooks/__tests__/useChartConfig.test.tsx b/packages/app/src/hooks/__tests__/useChartConfig.test.tsx index a20788a176..15a1d1d909 100644 --- a/packages/app/src/hooks/__tests__/useChartConfig.test.tsx +++ b/packages/app/src/hooks/__tests__/useChartConfig.test.tsx @@ -21,6 +21,7 @@ import { appendChunk, getGranularityAlignedTimeWindows, getMinGranularitySeconds, + getQueryWindows, mergeQuerySettings, useQueriedChartConfig, } from '@/hooks/useChartConfig'; @@ -368,6 +369,180 @@ describe('useChartConfig', () => { }); }); + describe('getQueryWindows', () => { + // A search-style request: exact bounds that don't fall on a bucket + const exactConfig = createMockChartConfig({ + dateRange: [ + new Date('2025-10-01T10:00:20Z'), + new Date('2025-10-01T11:00:20Z'), + ], + dateRangeEndInclusive: true, + granularity: '1 minute', + alignDateRangeToGranularity: false, + }); + // The optimizer's rewrite, widened to whole view buckets + const mvConfig = createMockChartConfig({ + ...exactConfig, + from: { databaseName: 'default', tableName: 'otel_logs_1m' }, + select: [{ aggCondition: '', aggFn: 'sum', valueExpression: 'count' }], + dateRange: [ + new Date('2025-10-01T10:00:00Z'), + new Date('2025-10-01T11:01:00Z'), + ], + dateRangeEndInclusive: false, + }); + + it('reads everything from the source table without a view', () => { + expect(getQueryWindows({ config: exactConfig })).toEqual([ + { config: exactConfig }, + ]); + }); + + it('reads an aligned request from the view alone', () => { + const alignedConfig = createMockChartConfig({ + ...exactConfig, + alignDateRangeToGranularity: undefined, + }); + expect(getQueryWindows({ config: alignedConfig, mvConfig })).toEqual([ + { config: mvConfig }, + ]); + }); + + it('reads the partial edge buckets of an exact request from the source table', () => { + expect(getQueryWindows({ config: exactConfig, mvConfig })).toEqual([ + { + config: exactConfig, + window: { + dateRange: [ + new Date('2025-10-01T11:00:00Z'), + new Date('2025-10-01T11:00:20Z'), + ], + dateRangeStartInclusive: true, + dateRangeEndInclusive: true, + }, + }, + { + config: { + ...mvConfig, + dateRange: [ + new Date('2025-10-01T10:01:00Z'), + new Date('2025-10-01T11:00:00Z'), + ], + dateRangeStartInclusive: true, + dateRangeEndInclusive: false, + }, + }, + { + config: exactConfig, + window: { + dateRange: [ + new Date('2025-10-01T10:00:20Z'), + new Date('2025-10-01T10:01:00Z'), + ], + dateRangeEndInclusive: false, + }, + }, + ]); + }); + + it('reads an exact request shorter than a bucket from the source table', () => { + const shortConfig = createMockChartConfig({ + ...exactConfig, + dateRange: [ + new Date('2025-10-01T10:00:10Z'), + new Date('2025-10-01T10:00:50Z'), + ], + }); + expect(getQueryWindows({ config: shortConfig, mvConfig })).toEqual([ + { config: shortConfig }, + ]); + }); + + it('needs no edge queries when an exact request falls on bucket boundaries', () => { + const onBoundaries = createMockChartConfig({ + ...exactConfig, + dateRange: [ + new Date('2025-10-01T10:00:00Z'), + new Date('2025-10-01T11:00:00Z'), + ], + dateRangeEndInclusive: false, + }); + expect(getQueryWindows({ config: onBoundaries, mvConfig })).toEqual([ + { + config: { + ...mvConfig, + dateRange: [ + new Date('2025-10-01T10:00:00Z'), + new Date('2025-10-01T11:00:00Z'), + ], + dateRangeStartInclusive: true, + dateRangeEndInclusive: false, + }, + }, + ]); + }); + + it('still reads rows at an inclusive end on a bucket boundary', () => { + const inclusiveEnd = createMockChartConfig({ + ...exactConfig, + dateRange: [ + new Date('2025-10-01T10:00:00Z'), + new Date('2025-10-01T11:00:00Z'), + ], + dateRangeEndInclusive: true, + }); + expect(getQueryWindows({ config: inclusiveEnd, mvConfig })[0]).toEqual({ + config: inclusiveEnd, + window: { + dateRange: [ + new Date('2025-10-01T11:00:00Z'), + new Date('2025-10-01T11:00:00Z'), + ], + dateRangeStartInclusive: true, + dateRangeEndInclusive: true, + }, + }); + }); + + it('chunks the view interior and keeps the windows contiguous', () => { + const dayConfig = createMockChartConfig({ + ...exactConfig, + dateRange: [ + new Date('2025-10-01T00:10:00Z'), + new Date('2025-10-02T00:10:00Z'), + ], + granularity: '30 minute', + }); + const windows = getQueryWindows({ + config: dayConfig, + mvConfig, + enableQueryChunking: true, + }); + const rangeOf = ({ config, window }: (typeof windows)[number]) => + window?.dateRange ?? config.dateRange; + const tableOf = ({ config }: (typeof windows)[number]) => + isBuilderChartConfig(config) ? config.from.tableName : undefined; + + expect(windows.length).toBeGreaterThan(3); + expect(tableOf(windows[0])).toBe('otel_logs'); + expect(rangeOf(windows[0])).toEqual([ + new Date('2025-10-02T00:00:00Z'), + new Date('2025-10-02T00:10:00Z'), + ]); + expect(tableOf(windows[windows.length - 1])).toBe('otel_logs'); + expect(rangeOf(windows[windows.length - 1])).toEqual([ + new Date('2025-10-01T00:10:00Z'), + new Date('2025-10-01T00:30:00Z'), + ]); + for (const window of windows.slice(1, -1)) { + expect(tableOf(window)).toBe('otel_logs_1m'); + } + for (let i = 0; i < windows.length - 1; i++) { + expect(rangeOf(windows[i + 1])?.[1]).toEqual(rangeOf(windows[i])?.[0]); + } + }); + }); + describe('useQueriedChartConfig', () => { let queryClient: QueryClient; let wrapper: React.ComponentType<{ children: any }>; @@ -2394,6 +2569,87 @@ describe('useChartConfig', () => { expect(result2.current.data?.data).toBeDefined(); }); + + it('reads the partial edge buckets of an exact range from the source table when a view serves the rest', async () => { + const config = createMockChartConfig({ + dateRange: [ + new Date('2025-10-01T10:00:20Z'), + new Date('2025-10-01T11:00:20Z'), + ], + dateRangeEndInclusive: true, + granularity: '1 minute', + alignDateRangeToGranularity: false, + }); + const optimizedConfig = createMockChartConfig({ + ...config, + from: { databaseName: 'default', tableName: 'otel_logs_1m' }, + select: [{ aggCondition: '', aggFn: 'sum', valueExpression: 'count' }], + dateRange: [ + new Date('2025-10-01T10:00:00Z'), + new Date('2025-10-01T11:01:00Z'), + ], + dateRangeEndInclusive: false, + }); + // `any` (rather than an `as` cast) keeps the partial query result off the + // no-unsafe-type-assertion budget. + const explanationResult = ( + value: any, + ): ReturnType => value; + jest.mocked(useMVOptimizationExplanation).mockReturnValue( + explanationResult({ + data: { optimizedConfig, explanations: [] }, + isLoading: false, + }), + ); + + // One row per query, named after the table and window it came from + mockClickhouseClient.queryChartConfig.mockImplementation( + async ({ config: queried }) => { + const range = queried.dateRange; + if (!isBuilderChartConfig(queried) || !range) { + throw new Error('Expected a builder config with a date range'); + } + return createMockQueryResponse([ + { + 'count()': '1', + SeverityText: queried.from.tableName, + __hdx_time_bucket: range[0].toISOString(), + }, + ]); + }, + ); + + const { result } = renderHook(() => useQueriedChartConfig(config), { + wrapper, + }); + + await waitFor(() => expect(result.current.isSuccess).toBe(true)); + await waitFor(() => expect(result.current.isFetching).toBe(false)); + + const queried = mockClickhouseClient.queryChartConfig.mock.calls.map( + ([{ config: c }]) => { + const range = c.dateRange; + if (!isBuilderChartConfig(c) || !range) { + throw new Error('Expected a builder config with a date range'); + } + return [c.from.tableName, range.map(d => d.toISOString())]; + }, + ); + expect(queried).toEqual([ + ['otel_logs', ['2025-10-01T11:00:00.000Z', '2025-10-01T11:00:20.000Z']], + [ + 'otel_logs_1m', + ['2025-10-01T10:01:00.000Z', '2025-10-01T11:00:00.000Z'], + ], + ['otel_logs', ['2025-10-01T10:00:20.000Z', '2025-10-01T10:01:00.000Z']], + ]); + // Oldest first, like a single query's result + expect(result.current.data?.data.map(row => row.SeverityText)).toEqual([ + 'otel_logs', + 'otel_logs_1m', + 'otel_logs', + ]); + }); }); describe('getMinGranularitySeconds', () => { diff --git a/packages/app/src/hooks/useChartConfig.tsx b/packages/app/src/hooks/useChartConfig.tsx index ed367f0c27..cba1cf8a5b 100644 --- a/packages/app/src/hooks/useChartConfig.tsx +++ b/packages/app/src/hooks/useChartConfig.tsx @@ -34,6 +34,7 @@ import { isMetricSource, isPromqlSource, QuerySettings, + SQLInterval, TSource, } from '@hyperdx/common-utils/dist/types'; import { @@ -85,6 +86,13 @@ interface AdditionalUseQueriedChartConfigOptions { type TimeWindow = { dateRange: [Date, Date]; dateRangeEndInclusive?: boolean; + dateRangeStartInclusive?: boolean; +}; + +/** One query of a chart: the config to run and, when split, its time window. */ +type QueryWindow = { + config: ChartConfigWithOptDateRange; + window?: TimeWindow; }; type TQueryFnData = ChartQueryResult; @@ -180,8 +188,119 @@ export const getGranularityAlignedTimeWindows = ( return windows; }; +/** Rounds a date up to the next bucket boundary, or keeps it when on one. */ +const ceilToInterval = (date: Date, granularity: SQLInterval): Date => { + const floored = toStartOfInterval(date, granularity); + return floored.getTime() < date.getTime() + ? new Date( + floored.getTime() + convertGranularityToSeconds(granularity) * 1000, + ) + : floored; +}; + +const chunkedQueryWindows = ( + config: ChartConfigWithOptDateRange, + enableQueryChunking: boolean, +): QueryWindow[] => + enableQueryChunking && shouldUseChunking(config) + ? getGranularityAlignedTimeWindows(config).map(window => ({ + config, + window, + })) + : [{ config }]; + +/** + * The queries that fetch a chart, newest first. + * + * A materialized view only holds whole buckets of its granularity, and the + * optimizer widens the range to them. That suits aligned charts, but a caller + * that asked for the exact range (`alignDateRangeToGranularity: false`, e.g. + * search counts that must match the results table) would count rows from + * outside its selection. For those, the partial first and last chart buckets + * are read from the source table with the exact bounds, and only the whole + * buckets in between from the view. Each bucket comes from one table, so + * counts, averages and percentiles all stay exact. + * + * Exported for tests. + */ +export function getQueryWindows({ + config, + mvConfig, + enableQueryChunking = false, +}: { + config: ChartConfigWithOptDateRange; + /** The config rewritten to read from a materialized view, if one fits. */ + mvConfig?: ChartConfigWithOptDateRange; + enableQueryChunking?: boolean; +}): QueryWindow[] { + if (mvConfig == null) { + return chunkedQueryWindows(config, enableQueryChunking); + } + const { dateRange } = config; + if ( + config.alignDateRangeToGranularity !== false || + dateRange == null || + !isBuilderChartConfig(config) || + !isUsingGranularity(config) + ) { + return chunkedQueryWindows(mvConfig, enableQueryChunking); + } + + const [start, end] = dateRange; + const granularity = + config.granularity === 'auto' + ? convertDateRangeToGranularityString( + dateRange, + undefined, + config.minGranularitySeconds, + ) + : config.granularity; + const interiorStart = ceilToInterval(start, granularity); + const interiorEnd = toStartOfInterval(end, granularity); + // Less than one whole bucket: nothing for the view to serve. + if (interiorStart >= interiorEnd) { + return chunkedQueryWindows(config, enableQueryChunking); + } + + const windows: QueryWindow[] = []; + // The last bucket's start to the exact end. A range that ends on a bucket + // boundary still needs it when the end is inclusive. + if (interiorEnd < end || config.dateRangeEndInclusive) { + windows.push({ + config, + window: { + dateRange: [interiorEnd, end], + dateRangeStartInclusive: true, + dateRangeEndInclusive: config.dateRangeEndInclusive, + }, + }); + } + windows.push( + ...chunkedQueryWindows( + { + ...mvConfig, + dateRange: [interiorStart, interiorEnd], + dateRangeStartInclusive: true, + dateRangeEndInclusive: false, + }, + enableQueryChunking, + ), + ); + if (start < interiorStart) { + windows.push({ + config, + window: { + dateRange: [start, interiorStart], + dateRangeEndInclusive: false, + }, + }); + } + return windows; +} + async function* fetchDataInChunks({ config, + mvConfig, clickhouseClient, signal, enableQueryChunking = false, @@ -190,6 +309,7 @@ async function* fetchDataInChunks({ querySettings, }: { config: ChartConfigWithOptDateRange; + mvConfig?: ChartConfigWithOptDateRange; clickhouseClient: ClickhouseClient; signal: AbortSignal; enableQueryChunking?: boolean; @@ -197,10 +317,7 @@ async function* fetchDataInChunks({ metadata: Metadata; querySettings: QuerySettings | undefined; }) { - const windows = - enableQueryChunking && shouldUseChunking(config) - ? getGranularityAlignedTimeWindows(config) - : [undefined]; + const windows = getQueryWindows({ config, mvConfig, enableQueryChunking }); // Every chunk must rank the __hdx_series_limit CTE over the same fixed // range, or each window keeps its own top-N and the union across chunks @@ -208,24 +325,28 @@ async function* fetchDataInChunks({ // chart range) to bound the ranking scan; the trade-off is that series // are picked by recent activity, so groups with no events in the newest // window are dropped from the chart. - const rankingDateRange = windows[0]?.dateRange; + const rankingDateRange = windows[0]?.window?.dateRange; // Only a positive seriesLimit emits the __hdx_series_limit CTE (0 = unlimited, // null = default), so only then does the ranking need a pinned date range. const seriesLimit = isBuilderChartConfig(config) && hasPositiveSeriesLimit(config.seriesLimit) ? config.seriesLimit : undefined; - const windowedConfigFor = (w: (typeof windows)[number]) => ({ - ...config, - ...(w ?? {}), - ...(w != null && seriesLimit != null && rankingDateRange != null + const windowedConfigFor = ({ + config: windowConfig, + window, + }: QueryWindow) => ({ + ...windowConfig, + ...(window ?? {}), + ...(window != null && seriesLimit != null && rankingDateRange != null ? { seriesLimitDateRange: rankingDateRange } : {}), }); - if (IS_MTVIEWS_ENABLED && isBuilderChartConfig(config)) { + const queriedConfig = mvConfig ?? config; + if (IS_MTVIEWS_ENABLED && isBuilderChartConfig(queriedConfig)) { const { dataTableDDL, mtViewDDL, renderMTViewConfig } = - await buildMTViewSelectQuery(config, metadata, querySettings); + await buildMTViewSelectQuery(queriedConfig, metadata, querySettings); // TODO: show the DDLs in the UI so users can run commands manually // eslint-disable-next-line no-console console.log('dataTableDDL:', dataTableDDL); @@ -425,10 +546,9 @@ export function useQueriedChartConfig( ); } - const optimizedConfig = { - ...(mvOptimizationData?.optimizedConfig ?? config), - minGranularitySeconds, - }; + const mvConfig = mvOptimizationData?.optimizedConfig + ? { ...mvOptimizationData.optimizedConfig, minGranularitySeconds } + : undefined; const query = queryClient .getQueryCache() .find({ queryKey: context.queryKey, exact: true }); @@ -442,7 +562,8 @@ export function useQueriedChartConfig( }; const chunks = fetchDataInChunks({ - config: optimizedConfig, + config: { ...config, minGranularitySeconds }, + mvConfig, clickhouseClient, signal: context.signal, enableQueryChunking: options?.enableQueryChunking, From 70ea21e4bf1b1866b7b33a7f3c02c06f04b42813 Mon Sep 17 00:00:00 2001 From: Alex Fedotyev <61838744+alex-fedotyev@users.noreply.github.com> Date: Fri, 20 Nov 2026 06:26:41 +0000 Subject: [PATCH 2/2] fix(app): keep one column name per series when a chart reads a view and its table A materialized view names an unaliased aggregate after its merge expression (SUM(...) of the view's count column), while the source table returns count(). With the partial edge buckets read from the source table and the rest from the view, the same series came back under two names, so the search histogram drew the view's buckets as the wrong series and the results count missed them. Chunks now take the first chunk's column names, by position: both queries are built from the same select list. Co-Authored-By: Claude Opus 5.5 --- .../hooks/__tests__/useChartConfig.test.tsx | 79 ++++++++++++++++++- packages/app/src/hooks/useChartConfig.tsx | 64 ++++++++++++++- 2 files changed, 139 insertions(+), 4 deletions(-) diff --git a/packages/app/src/hooks/__tests__/useChartConfig.test.tsx b/packages/app/src/hooks/__tests__/useChartConfig.test.tsx index 15a1d1d909..92186f2dea 100644 --- a/packages/app/src/hooks/__tests__/useChartConfig.test.tsx +++ b/packages/app/src/hooks/__tests__/useChartConfig.test.tsx @@ -18,6 +18,7 @@ import { renderHook, waitFor } from '@testing-library/react'; import { prometheusApi } from '@/api'; import { useClickhouseClient } from '@/clickhouse'; import { + alignChunkColumns, appendChunk, getGranularityAlignedTimeWindows, getMinGranularitySeconds, @@ -2602,20 +2603,38 @@ describe('useChartConfig', () => { }), ); - // One row per query, named after the table and window it came from + // One row per query, named after the table and window it came from. The + // view names the unaliased count after its own merge expression, as + // ClickHouse does. + const viewCountColumn = 'SUM(toFloat64OrDefault(toString(count)))'; mockClickhouseClient.queryChartConfig.mockImplementation( async ({ config: queried }) => { const range = queried.dateRange; if (!isBuilderChartConfig(queried) || !range) { throw new Error('Expected a builder config with a date range'); } - return createMockQueryResponse([ + const response = createMockQueryResponse([ { 'count()': '1', SeverityText: queried.from.tableName, __hdx_time_bucket: range[0].toISOString(), }, ]); + if (queried.from.tableName !== 'otel_logs_1m') { + return response; + } + return { + ...response, + meta: response.meta?.map(column => + column.name === 'count()' + ? { ...column, name: viewCountColumn } + : column, + ), + data: response.data.map(({ 'count()': count, ...row }) => ({ + ...row, + [viewCountColumn]: count, + })), + }; }, ); @@ -2649,6 +2668,62 @@ describe('useChartConfig', () => { 'otel_logs_1m', 'otel_logs', ]); + // One name for the count across both tables + expect(result.current.data?.data.map(row => row['count()'])).toEqual([ + '1', + '1', + '1', + ]); + expect( + result.current.data?.data.some(row => viewCountColumn in row), + ).toBe(false); + expect(result.current.data?.meta?.map(column => column.name)).toEqual([ + 'count()', + 'SeverityText', + '__hdx_time_bucket', + ]); + }); + }); + + describe('alignChunkColumns', () => { + const reference = [ + { name: 'count()', type: 'UInt64' }, + { name: 'ServiceName', type: 'LowCardinality(String)' }, + ]; + + it('renames columns by position to the reference names', () => { + const chunk = { + meta: [ + { name: 'SUM(count)', type: 'Float64' }, + { name: 'ServiceName', type: 'LowCardinality(String)' }, + ], + data: [{ 'SUM(count)': 4, ServiceName: 'api' }], + rows: 1, + }; + expect(alignChunkColumns(chunk, reference)).toEqual({ + meta: [ + { name: 'count()', type: 'Float64' }, + { name: 'ServiceName', type: 'LowCardinality(String)' }, + ], + data: [{ 'count()': 4, ServiceName: 'api' }], + rows: 1, + }); + }); + + it('leaves a chunk alone when the names already match or the shapes differ', () => { + const matching = { + meta: reference, + data: [{ 'count()': '4', ServiceName: 'api' }], + rows: 1, + }; + expect(alignChunkColumns(matching, reference)).toBe(matching); + + const otherShape = { + meta: [{ name: 'SUM(count)', type: 'Float64' }], + data: [{ 'SUM(count)': 4 }], + rows: 1, + }; + expect(alignChunkColumns(otherShape, reference)).toBe(otherShape); }); }); diff --git a/packages/app/src/hooks/useChartConfig.tsx b/packages/app/src/hooks/useChartConfig.tsx index cba1cf8a5b..7fa7ecccfa 100644 --- a/packages/app/src/hooks/useChartConfig.tsx +++ b/packages/app/src/hooks/useChartConfig.tsx @@ -360,6 +360,22 @@ async function* fetchDataInChunks({ ? { readonly: '2' } : {}; + // When some windows read a materialized view and others the source table, + // give every chunk the first chunk's column names, so each series keeps one + // name across the whole range. + const readsSeveralTables = new Set(windows.map(w => w.config)).size > 1; + let referenceMeta: TChunk['chunk']['meta']; + const withAlignedColumns = (chunk: TChunk['chunk']): TChunk['chunk'] => { + if (!readsSeveralTables) { + return chunk; + } + if (referenceMeta == null) { + referenceMeta = chunk.meta; + return chunk; + } + return alignChunkColumns(chunk, referenceMeta); + }; + if (enableParallelQueries) { // fetch in parallel const promises = windows.map(async (w, index) => { @@ -393,7 +409,10 @@ async function* fetchDataInChunks({ while (bufferedChunks.length > 0 && bufferedChunks[0] !== undefined) { // remove data from front so that it always arrives in order const chunk = bufferedChunks.shift(); - yield { chunk, isComplete: bufferedChunks.length === 0 }; + yield { + chunk: withAlignedColumns(chunk), + isComplete: bufferedChunks.length === 0, + }; flushed += 1; } } @@ -413,8 +432,49 @@ async function* fetchDataInChunks({ querySettings, }); - yield { chunk: result, isComplete: i === windows.length - 1 }; + yield { + chunk: withAlignedColumns(result), + isComplete: i === windows.length - 1, + }; + } +} + +/** + * Renames a chunk's columns, by position, to the given reference columns. + * A materialized view names an unaliased aggregate differently from the source + * table (`SUM(...)` of the view's count column instead of `count()`), so chunks + * of one chart read from both would otherwise carry the same series under two + * names. The two queries share a select list, so position identifies a column. + * Exported for tests. + */ +export function alignChunkColumns( + chunk: TChunk['chunk'], + referenceMeta: TChunk['chunk']['meta'], +): TChunk['chunk'] { + const meta = chunk.meta ?? []; + if ( + referenceMeta == null || + referenceMeta.length !== meta.length || + meta.every((column, i) => column.name === referenceMeta[i].name) + ) { + return chunk; } + + const renamed = new Map( + meta.map((column, i) => [column.name, referenceMeta[i].name]), + ); + return { + ...chunk, + meta: meta.map((column, i) => ({ ...column, name: referenceMeta[i].name })), + data: chunk.data?.map(row => + Object.fromEntries( + Object.entries(row).map(([key, value]) => [ + renamed.get(key) ?? key, + value, + ]), + ), + ), + }; } /**