From cd2255826fac467db0b4f4e40d290eee124a76a8 Mon Sep 17 00:00:00 2001 From: Aaron Knudtson <87577305+knudtty@users.noreply.github.com> Date: Fri, 20 Nov 2026 16:09:35 -0500 Subject: [PATCH 1/7] fix: prevent unnecessary histogram rerender --- .changeset/histogram-skip-column-refetch.md | 5 +++ packages/app/src/ChartUtils.tsx | 3 ++ packages/app/src/DBSearchPage.tsx | 45 ++++++++++++------- packages/app/src/__tests__/ChartUtils.test.ts | 20 +++++++++ .../Search/__tests__/histogramWith.test.ts | 39 ++++++++++++++++ .../src/components/Search/histogramWith.ts | 33 ++++++++++++++ 6 files changed, 129 insertions(+), 16 deletions(-) create mode 100644 .changeset/histogram-skip-column-refetch.md create mode 100644 packages/app/src/components/Search/__tests__/histogramWith.test.ts create mode 100644 packages/app/src/components/Search/histogramWith.ts diff --git a/.changeset/histogram-skip-column-refetch.md b/.changeset/histogram-skip-column-refetch.md new file mode 100644 index 0000000000..54b1ea46e3 --- /dev/null +++ b/.changeset/histogram-skip-column-refetch.md @@ -0,0 +1,5 @@ +--- +'@hyperdx/app': patch +--- + +fix: Don't refetch the search histogram when a table column is added or removed diff --git a/packages/app/src/ChartUtils.tsx b/packages/app/src/ChartUtils.tsx index 98689c44be..5eb4663e36 100644 --- a/packages/app/src/ChartUtils.tsx +++ b/packages/app/src/ChartUtils.tsx @@ -214,6 +214,9 @@ export function convertToTimeChartConfig( // Overwrite (not conditionally spread) so a cleared `null` from the // source config is normalized to undefined rather than carried over. seriesLimit, + // Only feeds "View events" links (read from the unconverted config); + // leaving it in would refetch the chart whenever a table column changes. + eventTableSelect: undefined, } : { ...config, diff --git a/packages/app/src/DBSearchPage.tsx b/packages/app/src/DBSearchPage.tsx index e9b8678858..e5024fcef8 100644 --- a/packages/app/src/DBSearchPage.tsx +++ b/packages/app/src/DBSearchPage.tsx @@ -166,6 +166,7 @@ import DBSqlRowTableWithSideBar from './components/DBSqlRowTableWithSidebar'; import PatternTable from './components/PatternTable'; import { DBSearchHeatmapChart } from './components/Search/DBSearchHeatmapChart'; import DirectTraceSidePanel from './components/Search/DirectTraceSidePanel'; +import { getReferencedWithClauses } from './components/Search/histogramWith'; import { TRACE_CHART_MODES, TraceRedMetricsChart, @@ -1983,19 +1984,24 @@ function DBSearchPageContent() { ...displayedColumns, withMapKeyAlias(column, displayedColumns, knownColumns), ]; - setValue('select', newSelectArray.join(', ')); - if (selected) { - const orderBy = getValues('orderBy') ?? ''; - const nextOrderBy = orderByAfterRemovingSelectItem( - selected, - orderBy, - defaultSearchConfig.orderBy ?? '', - ); - if (nextOrderBy !== orderBy) { - setValue('orderBy', nextOrderBy); - } + const select = newSelectArray.join(', '); + setValue('select', select); + const orderBy = getValues('orderBy') ?? ''; + const nextOrderBy = selected + ? orderByAfterRemovingSelectItem( + selected, + orderBy, + defaultSearchConfig.orderBy ?? '', + ) + : orderBy; + // Apply only the column change: a full submit re-resolves a relative + // time range, which refetches the histogram for nothing. + if (nextOrderBy !== orderBy) { + setValue('orderBy', nextOrderBy); + setSearchedConfig({ select, orderBy: nextOrderBy }); + } else { + setSearchedConfig({ select }); } - onSubmit(); }, [ displayedColumns, @@ -2003,7 +2009,7 @@ function DBSearchPageContent() { setValue, getValues, defaultSearchConfig.orderBy, - onSubmit, + setSearchedConfig, ], ); @@ -2141,6 +2147,7 @@ function DBSearchPageContent() { return undefined; } + const groupBy = resolveSearchGroupBy(groupByParam, searchedSource); return { ...chartConfig, select: [ @@ -2154,8 +2161,14 @@ function DBSearchPageContent() { granularity: 'auto', dateRange: searchedTimeRange, displayType: DisplayType.StackedBar, - with: aliasWith, - // Preserve the original table select string for "View Events" links + with: getReferencedWithClauses(aliasWith, [ + chartConfig.where, + JSON.stringify(chartConfig.filters ?? []), + groupBy ?? '', + chartConfig.implicitColumnExpression ?? '', + ]), + // Preserve the original table select string for "View Events" links. + // convertToTimeChartConfig drops it from the query key. eventTableSelect: searchedConfig.select ?? undefined, // Never align to granularity boundaries on the search page: the histogram // and total count must reflect the user's exact selected range so they @@ -2166,7 +2179,7 @@ function DBSearchPageContent() { alignDateRangeToGranularity: false, // Make sure the end date is inclusive so that the histogram and table counts match dateRangeEndInclusive: true, - groupBy: resolveSearchGroupBy(groupByParam, searchedSource), + groupBy, } satisfies BuilderChartConfigWithDateRange; }, [ chartConfig, diff --git a/packages/app/src/__tests__/ChartUtils.test.ts b/packages/app/src/__tests__/ChartUtils.test.ts index 612f026a43..e570ae3b43 100644 --- a/packages/app/src/__tests__/ChartUtils.test.ts +++ b/packages/app/src/__tests__/ChartUtils.test.ts @@ -1382,6 +1382,26 @@ describe('ChartUtils', () => { expect(granularityFromFunction).toBe('5 minute'); }); + it('drops eventTableSelect so table column changes do not change the query key', () => { + const config: BuilderChartConfigWithDateRange = { + select: 'count()', + from: { databaseName: 'test', tableName: 'logs' }, + where: '', + timestampValueExpression: 'timestamp', + connection: 'test-connection', + granularity: '5 minute', + dateRange: [ + new Date('2025-11-26T00:00:00Z'), + new Date('2025-11-27T00:00:00Z'), + ], + eventTableSelect: 'Timestamp, Body', + }; + + expect(convertToTimeChartConfig(config)).toMatchObject({ + eventTableSelect: undefined, + }); + }); + // seriesLimit lives on the builder member of the ChartConfigWithDateRange // union, so narrow the result before reading it. The per-tile value is // read from the config itself (no team override anymore). diff --git a/packages/app/src/components/Search/__tests__/histogramWith.test.ts b/packages/app/src/components/Search/__tests__/histogramWith.test.ts new file mode 100644 index 0000000000..4b2d118e3e --- /dev/null +++ b/packages/app/src/components/Search/__tests__/histogramWith.test.ts @@ -0,0 +1,39 @@ +import { getReferencedWithClauses } from '@/components/Search/histogramWith'; + +const clause = (name: string, sql: string) => ({ + name, + sql: { sql, params: {} }, + isSubquery: false, +}); + +describe('getReferencedWithClauses', () => { + const withClauses = [ + clause('service', 'ServiceName'), + clause('body', 'toString(Body)'), + clause('shout', 'upper(body)'), + ]; + + it('returns undefined when nothing is referenced', () => { + expect( + getReferencedWithClauses(withClauses, ['level:error', '[]']), + ).toBeUndefined(); + }); + + it('keeps clauses referenced by the where, filters or group by', () => { + expect( + getReferencedWithClauses(withClauses, ["service = 'api'", '[]', ''])?.map( + c => c.name, + ), + ).toEqual(['service']); + }); + + it('keeps aliases that a referenced alias depends on', () => { + expect( + getReferencedWithClauses(withClauses, ['shout:ERROR'])?.map(c => c.name), + ).toEqual(['body', 'shout']); + }); + + it('handles missing clauses', () => { + expect(getReferencedWithClauses(undefined, ['service'])).toBeUndefined(); + }); +}); diff --git a/packages/app/src/components/Search/histogramWith.ts b/packages/app/src/components/Search/histogramWith.ts new file mode 100644 index 0000000000..7aec8e455c --- /dev/null +++ b/packages/app/src/components/Search/histogramWith.ts @@ -0,0 +1,33 @@ +import { BuilderChartConfig } from '@hyperdx/common-utils/dist/types'; + +/** + * Keeps only the alias WITH clauses that `texts` (the where, filters and + * group by) reference, directly or through another kept alias. The histogram + * doesn't select the table's columns, so the rest only change its query key + * and refetch it whenever a column is added or removed. + */ +export function getReferencedWithClauses( + withClauses: BuilderChartConfig['with'], + texts: string[], +): BuilderChartConfig['with'] { + if (!withClauses) return undefined; + + const kept = new Set(); + let haystack = texts.join('\n'); + let added = true; + while (added) { + added = false; + for (const clause of withClauses) { + // Substring match over-includes (e.g. `svc` in `svc_name`), which only + // costs a refetch; under-including would break the query. + if (!kept.has(clause.name) && haystack.includes(clause.name)) { + kept.add(clause.name); + haystack += `\n${clause.sql?.sql ?? ''}`; + added = true; + } + } + } + + const referenced = withClauses.filter(clause => kept.has(clause.name)); + return referenced.length > 0 ? referenced : undefined; +} From 7398144c107e4698ec8c66ef8a868c30bd8931d8 Mon Sep 17 00:00:00 2001 From: Aaron Knudtson <87577305+knudtty@users.noreply.github.com> Date: Fri, 20 Nov 2026 16:26:42 -0500 Subject: [PATCH 2/7] pr suggestions --- packages/app/src/DBSearchPage.tsx | 42 ++++++++++++++++--------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/packages/app/src/DBSearchPage.tsx b/packages/app/src/DBSearchPage.tsx index e5024fcef8..d9b7e9f134 100644 --- a/packages/app/src/DBSearchPage.tsx +++ b/packages/app/src/DBSearchPage.tsx @@ -1421,8 +1421,13 @@ function DBSearchPageContent() { // completes "Explore your data". An explicit arg, not a shared ref, so // concurrent submits can't consume each other's suppression. const onSubmit = useCallback( - ({ recordExploration = true }: { recordExploration?: boolean } = {}) => { - onSearch(displayedTimeInputValue); + ({ + recordExploration = true, + resolveTimeRange = true, + }: { recordExploration?: boolean; resolveTimeRange?: boolean } = {}) => { + if (resolveTimeRange) { + onSearch(displayedTimeInputValue); + } handleSubmit( ({ select, where, whereLanguage, source, filters, orderBy }) => { setSearchedConfig({ @@ -1984,24 +1989,21 @@ function DBSearchPageContent() { ...displayedColumns, withMapKeyAlias(column, displayedColumns, knownColumns), ]; - const select = newSelectArray.join(', '); - setValue('select', select); - const orderBy = getValues('orderBy') ?? ''; - const nextOrderBy = selected - ? orderByAfterRemovingSelectItem( - selected, - orderBy, - defaultSearchConfig.orderBy ?? '', - ) - : orderBy; - // Apply only the column change: a full submit re-resolves a relative - // time range, which refetches the histogram for nothing. - if (nextOrderBy !== orderBy) { - setValue('orderBy', nextOrderBy); - setSearchedConfig({ select, orderBy: nextOrderBy }); - } else { - setSearchedConfig({ select }); + setValue('select', newSelectArray.join(', ')); + if (selected) { + const orderBy = getValues('orderBy') ?? ''; + const nextOrderBy = orderByAfterRemovingSelectItem( + selected, + orderBy, + defaultSearchConfig.orderBy ?? '', + ); + if (nextOrderBy !== orderBy) { + setValue('orderBy', nextOrderBy); + } } + // Keep the current time range: re-resolving a relative range would + // refetch the histogram for a column change. + onSubmit({ resolveTimeRange: false }); }, [ displayedColumns, @@ -2009,7 +2011,7 @@ function DBSearchPageContent() { setValue, getValues, defaultSearchConfig.orderBy, - setSearchedConfig, + onSubmit, ], ); From b5312ca30854a0f558fd029ce6117ee6a9f7c251 Mon Sep 17 00:00:00 2001 From: Aaron Knudtson <87577305+knudtty@users.noreply.github.com> Date: Fri, 20 Nov 2026 16:48:47 -0500 Subject: [PATCH 3/7] fix: apply a typed time range when toggling a column --- packages/app/src/DBSearchPage.tsx | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/packages/app/src/DBSearchPage.tsx b/packages/app/src/DBSearchPage.tsx index d9b7e9f134..47d0913f2e 100644 --- a/packages/app/src/DBSearchPage.tsx +++ b/packages/app/src/DBSearchPage.tsx @@ -1315,6 +1315,21 @@ function DBSearchPageContent() { updateInput: !isLive, }); + // The time input as of the last applied range, so a submit can tell whether + // the user has typed a new range since. + const searchedRangeKey = `${searchedTimeRange[0].getTime()}-${searchedTimeRange[1].getTime()}`; + const [appliedTime, setAppliedTime] = useState({ + rangeKey: searchedRangeKey, + input: displayedTimeInputValue, + }); + if (appliedTime.rangeKey !== searchedRangeKey) { + setAppliedTime({ + rangeKey: searchedRangeKey, + input: displayedTimeInputValue, + }); + } + const isTimeInputEdited = displayedTimeInputValue !== appliedTime.input; + // Sync url state back with form state // (ex. for history navigation) // TODO: Check if there are any bad edge cases here @@ -2001,9 +2016,10 @@ function DBSearchPageContent() { setValue('orderBy', nextOrderBy); } } - // Keep the current time range: re-resolving a relative range would - // refetch the histogram for a column change. - onSubmit({ resolveTimeRange: false }); + // Keep the current time range unless the user typed a new one: + // re-resolving a relative range would refetch the histogram for a + // column change. + onSubmit({ resolveTimeRange: isTimeInputEdited }); }, [ displayedColumns, @@ -2012,6 +2028,7 @@ function DBSearchPageContent() { getValues, defaultSearchConfig.orderBy, onSubmit, + isTimeInputEdited, ], ); From 61f58e2f3b25d977e828a8a4f9dcd9d48ea02c70 Mon Sep 17 00:00:00 2001 From: Aaron Knudtson <87577305+knudtty@users.noreply.github.com> Date: Fri, 20 Nov 2026 17:02:37 -0500 Subject: [PATCH 4/7] fix: re-resolve the time range on column toggle in live tail --- packages/app/src/DBSearchPage.tsx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/app/src/DBSearchPage.tsx b/packages/app/src/DBSearchPage.tsx index 47d0913f2e..dd29babe20 100644 --- a/packages/app/src/DBSearchPage.tsx +++ b/packages/app/src/DBSearchPage.tsx @@ -2018,8 +2018,9 @@ function DBSearchPageContent() { } // Keep the current time range unless the user typed a new one: // re-resolving a relative range would refetch the histogram for a - // column change. - onSubmit({ resolveTimeRange: isTimeInputEdited }); + // column change. Live tail refetches on every tick anyway, and its ticks + // would mark a typed range as applied, so it always re-resolves. + onSubmit({ resolveTimeRange: !!isLive || isTimeInputEdited }); }, [ displayedColumns, @@ -2028,6 +2029,7 @@ function DBSearchPageContent() { getValues, defaultSearchConfig.orderBy, onSubmit, + isLive, isTimeInputEdited, ], ); From 341c7d087d87b98c0fcb7e21e66141ae60e4ade5 Mon Sep 17 00:00:00 2001 From: Aaron Knudtson <87577305+knudtty@users.noreply.github.com> Date: Fri, 20 Nov 2026 17:06:56 -0500 Subject: [PATCH 5/7] fix: track the applied time input where the time query writes it --- packages/app/src/DBSearchPage.tsx | 45 +++++++++++++++---------------- 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/packages/app/src/DBSearchPage.tsx b/packages/app/src/DBSearchPage.tsx index dd29babe20..c3eb0734d3 100644 --- a/packages/app/src/DBSearchPage.tsx +++ b/packages/app/src/DBSearchPage.tsx @@ -1305,31 +1305,25 @@ function DBSearchPageContent() { const [displayedTimeInputValue, setDisplayedTimeInputValue] = useState('Live Tail'); + // The last time input the time query wrote, i.e. the applied range. User + // typing goes through setDisplayedTimeInputValue, so it differs once edited. + const [appliedTimeInputValue, setAppliedTimeInputValue] = + useState('Live Tail'); + const setAppliedTimeInput = useCallback((value: string) => { + setDisplayedTimeInputValue(value); + setAppliedTimeInputValue(value); + }, []); + const isTimeInputEdited = displayedTimeInputValue !== appliedTimeInputValue; const { isReady, searchedTimeRange, onSearch, onTimeRangeSelect } = useNewTimeQuery({ initialDisplayValue: 'Live Tail', initialTimeRange: defaultTimeRange, showRelativeInterval: isLive ?? true, - setDisplayedTimeInputValue, + setDisplayedTimeInputValue: setAppliedTimeInput, updateInput: !isLive, }); - // The time input as of the last applied range, so a submit can tell whether - // the user has typed a new range since. - const searchedRangeKey = `${searchedTimeRange[0].getTime()}-${searchedTimeRange[1].getTime()}`; - const [appliedTime, setAppliedTime] = useState({ - rangeKey: searchedRangeKey, - input: displayedTimeInputValue, - }); - if (appliedTime.rangeKey !== searchedRangeKey) { - setAppliedTime({ - rangeKey: searchedRangeKey, - input: displayedTimeInputValue, - }); - } - const isTimeInputEdited = displayedTimeInputValue !== appliedTime.input; - // Sync url state back with form state // (ex. for history navigation) // TODO: Check if there are any bad edge cases here @@ -1879,12 +1873,15 @@ function DBSearchPageContent() { parseAsInteger.withDefault(DEFAULT_REFRESH_FREQUENCY), ); - const updateRelativeTimeInputValue = useCallback((interval: number) => { - const label = getRelativeTimeOptionLabel(interval); - if (label) { - setDisplayedTimeInputValue(label); - } - }, []); + const updateRelativeTimeInputValue = useCallback( + (interval: number) => { + const label = getRelativeTimeOptionLabel(interval); + if (label) { + setAppliedTimeInput(label); + } + }, + [setAppliedTimeInput], + ); useEffect(() => { if (isReady && isLive) { @@ -2018,8 +2015,8 @@ function DBSearchPageContent() { } // Keep the current time range unless the user typed a new one: // re-resolving a relative range would refetch the histogram for a - // column change. Live tail refetches on every tick anyway, and its ticks - // would mark a typed range as applied, so it always re-resolves. + // column change. Live tail refetches on every tick anyway, so it always + // re-resolves. onSubmit({ resolveTimeRange: !!isLive || isTimeInputEdited }); }, [ From 39998be18a94dd120d1d7cad645f6f339836d0ab Mon Sep 17 00:00:00 2001 From: Aaron Knudtson <87577305+knudtty@users.noreply.github.com> Date: Fri, 20 Nov 2026 17:15:38 -0500 Subject: [PATCH 6/7] fix: keep aliases used anywhere outside the table select --- packages/app/src/DBSearchPage.tsx | 6 +++--- packages/app/src/components/Search/histogramWith.ts | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/app/src/DBSearchPage.tsx b/packages/app/src/DBSearchPage.tsx index c3eb0734d3..e75e0bde89 100644 --- a/packages/app/src/DBSearchPage.tsx +++ b/packages/app/src/DBSearchPage.tsx @@ -2179,11 +2179,11 @@ function DBSearchPageContent() { granularity: 'auto', dateRange: searchedTimeRange, displayType: DisplayType.StackedBar, + // Everything but the table's select and sort can reference an alias, + // including the body, timestamp and sample-weight expressions. with: getReferencedWithClauses(aliasWith, [ - chartConfig.where, - JSON.stringify(chartConfig.filters ?? []), + JSON.stringify({ ...chartConfig, select: '', orderBy: '' }), groupBy ?? '', - chartConfig.implicitColumnExpression ?? '', ]), // Preserve the original table select string for "View Events" links. // convertToTimeChartConfig drops it from the query key. diff --git a/packages/app/src/components/Search/histogramWith.ts b/packages/app/src/components/Search/histogramWith.ts index 7aec8e455c..1fb301b559 100644 --- a/packages/app/src/components/Search/histogramWith.ts +++ b/packages/app/src/components/Search/histogramWith.ts @@ -1,8 +1,8 @@ import { BuilderChartConfig } from '@hyperdx/common-utils/dist/types'; /** - * Keeps only the alias WITH clauses that `texts` (the where, filters and - * group by) reference, directly or through another kept alias. The histogram + * Keeps only the alias WITH clauses that `texts` (the query outside the + * table's select) reference, directly or through another kept alias. The histogram * doesn't select the table's columns, so the rest only change its query key * and refetch it whenever a column is added or removed. */ From 69f52c025eb361b4821d3ee6265c0ef1d2a2c013 Mon Sep 17 00:00:00 2001 From: Aaron Knudtson <87577305+knudtty@users.noreply.github.com> Date: Mon, 23 Nov 2026 10:08:18 -0500 Subject: [PATCH 7/7] refactor: move getReferencedWithClauses next to aliasMapToWithClauses --- .changeset/histogram-skip-column-refetch.md | 1 + packages/app/src/DBSearchPage.tsx | 2 +- .../Search/__tests__/histogramWith.test.ts | 39 ------------------ .../src/components/Search/histogramWith.ts | 33 --------------- .../common-utils/src/__tests__/utils.test.ts | 40 +++++++++++++++++++ packages/common-utils/src/core/utils.ts | 32 +++++++++++++++ 6 files changed, 74 insertions(+), 73 deletions(-) delete mode 100644 packages/app/src/components/Search/__tests__/histogramWith.test.ts delete mode 100644 packages/app/src/components/Search/histogramWith.ts diff --git a/.changeset/histogram-skip-column-refetch.md b/.changeset/histogram-skip-column-refetch.md index 54b1ea46e3..351c1b66d7 100644 --- a/.changeset/histogram-skip-column-refetch.md +++ b/.changeset/histogram-skip-column-refetch.md @@ -1,5 +1,6 @@ --- '@hyperdx/app': patch +'@hyperdx/common-utils': patch --- fix: Don't refetch the search histogram when a table column is added or removed diff --git a/packages/app/src/DBSearchPage.tsx b/packages/app/src/DBSearchPage.tsx index e75e0bde89..edfa5f351d 100644 --- a/packages/app/src/DBSearchPage.tsx +++ b/packages/app/src/DBSearchPage.tsx @@ -36,6 +36,7 @@ import { tcFromSource } from '@hyperdx/common-utils/dist/core/metadata'; import { buildSearchChartConfig } from '@hyperdx/common-utils/dist/core/searchChartConfig'; import { aliasMapToWithClauses, + getReferencedWithClauses, isBrowser, splitAndTrimWithBracket, } from '@hyperdx/common-utils/dist/core/utils'; @@ -166,7 +167,6 @@ import DBSqlRowTableWithSideBar from './components/DBSqlRowTableWithSidebar'; import PatternTable from './components/PatternTable'; import { DBSearchHeatmapChart } from './components/Search/DBSearchHeatmapChart'; import DirectTraceSidePanel from './components/Search/DirectTraceSidePanel'; -import { getReferencedWithClauses } from './components/Search/histogramWith'; import { TRACE_CHART_MODES, TraceRedMetricsChart, diff --git a/packages/app/src/components/Search/__tests__/histogramWith.test.ts b/packages/app/src/components/Search/__tests__/histogramWith.test.ts deleted file mode 100644 index 4b2d118e3e..0000000000 --- a/packages/app/src/components/Search/__tests__/histogramWith.test.ts +++ /dev/null @@ -1,39 +0,0 @@ -import { getReferencedWithClauses } from '@/components/Search/histogramWith'; - -const clause = (name: string, sql: string) => ({ - name, - sql: { sql, params: {} }, - isSubquery: false, -}); - -describe('getReferencedWithClauses', () => { - const withClauses = [ - clause('service', 'ServiceName'), - clause('body', 'toString(Body)'), - clause('shout', 'upper(body)'), - ]; - - it('returns undefined when nothing is referenced', () => { - expect( - getReferencedWithClauses(withClauses, ['level:error', '[]']), - ).toBeUndefined(); - }); - - it('keeps clauses referenced by the where, filters or group by', () => { - expect( - getReferencedWithClauses(withClauses, ["service = 'api'", '[]', ''])?.map( - c => c.name, - ), - ).toEqual(['service']); - }); - - it('keeps aliases that a referenced alias depends on', () => { - expect( - getReferencedWithClauses(withClauses, ['shout:ERROR'])?.map(c => c.name), - ).toEqual(['body', 'shout']); - }); - - it('handles missing clauses', () => { - expect(getReferencedWithClauses(undefined, ['service'])).toBeUndefined(); - }); -}); diff --git a/packages/app/src/components/Search/histogramWith.ts b/packages/app/src/components/Search/histogramWith.ts deleted file mode 100644 index 1fb301b559..0000000000 --- a/packages/app/src/components/Search/histogramWith.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { BuilderChartConfig } from '@hyperdx/common-utils/dist/types'; - -/** - * Keeps only the alias WITH clauses that `texts` (the query outside the - * table's select) reference, directly or through another kept alias. The histogram - * doesn't select the table's columns, so the rest only change its query key - * and refetch it whenever a column is added or removed. - */ -export function getReferencedWithClauses( - withClauses: BuilderChartConfig['with'], - texts: string[], -): BuilderChartConfig['with'] { - if (!withClauses) return undefined; - - const kept = new Set(); - let haystack = texts.join('\n'); - let added = true; - while (added) { - added = false; - for (const clause of withClauses) { - // Substring match over-includes (e.g. `svc` in `svc_name`), which only - // costs a refetch; under-including would break the query. - if (!kept.has(clause.name) && haystack.includes(clause.name)) { - kept.add(clause.name); - haystack += `\n${clause.sql?.sql ?? ''}`; - added = true; - } - } - } - - const referenced = withClauses.filter(clause => kept.has(clause.name)); - return referenced.length > 0 ? referenced : undefined; -} diff --git a/packages/common-utils/src/__tests__/utils.test.ts b/packages/common-utils/src/__tests__/utils.test.ts index 477a342d6d..463cf5ef78 100644 --- a/packages/common-utils/src/__tests__/utils.test.ts +++ b/packages/common-utils/src/__tests__/utils.test.ts @@ -15,6 +15,7 @@ import { getDistributedTableArgs, getFirstOrderingItem, getPercentileAggFnLevel, + getReferencedWithClauses, getTextIndexDictionaryBlockSize, hasNonEmptyOrderBy, hasPositiveSeriesLimit, @@ -3385,6 +3386,45 @@ describe('utils', () => { }); }); + describe('getReferencedWithClauses', () => { + const clause = (name: string, sql: string) => ({ + name, + sql: { sql, params: {} }, + isSubquery: false, + }); + const withClauses = [ + clause('service', 'ServiceName'), + clause('body', 'toString(Body)'), + clause('shout', 'upper(body)'), + ]; + + it('returns undefined when nothing is referenced', () => { + expect( + getReferencedWithClauses(withClauses, ['level:error', '[]']), + ).toBeUndefined(); + }); + + it('keeps clauses the texts reference', () => { + expect( + getReferencedWithClauses(withClauses, ["service = 'api'", '[]'])?.map( + c => c.name, + ), + ).toEqual(['service']); + }); + + it('keeps clauses that a referenced clause depends on', () => { + expect( + getReferencedWithClauses(withClauses, ['shout:ERROR'])?.map( + c => c.name, + ), + ).toEqual(['body', 'shout']); + }); + + it('handles missing clauses', () => { + expect(getReferencedWithClauses(undefined, ['service'])).toBeUndefined(); + }); + }); + describe('getLocalTableFromDistributedTable', () => { const makeMetadata = (engineFull: string) => ({ engine_full: engineFull }) as any; diff --git a/packages/common-utils/src/core/utils.ts b/packages/common-utils/src/core/utils.ts index a582af9d01..0308eee243 100644 --- a/packages/common-utils/src/core/utils.ts +++ b/packages/common-utils/src/core/utils.ts @@ -1696,6 +1696,38 @@ export function aliasMapToWithClauses( return withClauses.length > 0 ? withClauses : undefined; } +/** + * Keeps only the WITH clauses that `texts` reference, directly or through + * another kept clause. Lets a query that doesn't select a config's columns + * (e.g. a count histogram) skip the aliases it never uses, so adding or + * removing a column doesn't change its query key. + */ +export function getReferencedWithClauses( + withClauses: BuilderChartConfig['with'], + texts: string[], +): BuilderChartConfig['with'] { + if (!withClauses) return undefined; + + const kept = new Set(); + let haystack = texts.join('\n'); + let added = true; + while (added) { + added = false; + for (const clause of withClauses) { + // Substring match over-includes (e.g. `svc` in `svc_name`), which only + // costs a refetch; under-including would break the query. + if (!kept.has(clause.name) && haystack.includes(clause.name)) { + kept.add(clause.name); + haystack += `\n${clause.sql?.sql ?? ''}`; + added = true; + } + } + } + + const referenced = withClauses.filter(clause => kept.has(clause.name)); + return referenced.length > 0 ? referenced : undefined; +} + const stripQuotes = (s: string) => s.replace(/^["'`]|["'`]$/g, ''); /** Parses and returns the cluster, database, and table from the given distributed table metadata */