From 89ff875235d49f149ded47087f7c184de4b0bef6 Mon Sep 17 00:00:00 2001 From: Drew Davis Date: Fri, 20 Nov 2026 14:45:33 -0500 Subject: [PATCH] fix: Fix table sorting for formula, group by, and multi-series metric aliases --- .changeset/table-sort-aliased-columns.md | 6 + .../DBTable/__tests__/sorting.test.ts | 221 +++++++++++++++++- .../app/src/components/DBTable/sorting.ts | 52 +++++ packages/app/src/components/DBTableChart.tsx | 43 ++-- .../__tests__/DBTableChart.test.tsx | 63 +++++ .../useResultColumnFilterKeys.test.ts | 40 ++++ .../src/hooks/useResultColumnFilterKeys.ts | 76 +++--- packages/app/src/source.ts | 2 +- .../e2e/components/ChartEditorComponent.ts | 2 +- .../tests/e2e/features/chart-explorer.spec.ts | 48 +++- .../e2e/page-objects/ChartExplorerPage.ts | 16 ++ .../src/core/renderChartConfig.ts | 25 +- 12 files changed, 530 insertions(+), 64 deletions(-) create mode 100644 .changeset/table-sort-aliased-columns.md diff --git a/.changeset/table-sort-aliased-columns.md b/.changeset/table-sort-aliased-columns.md new file mode 100644 index 0000000000..a1fe86b935 --- /dev/null +++ b/.changeset/table-sort-aliased-columns.md @@ -0,0 +1,6 @@ +--- +'@hyperdx/app': patch +'@hyperdx/common-utils': patch +--- + +fix: Fix table sorting for formula, group by, and multi-series metric aliases diff --git a/packages/app/src/components/DBTable/__tests__/sorting.test.ts b/packages/app/src/components/DBTable/__tests__/sorting.test.ts index 1d2f963a8c..9e98943451 100644 --- a/packages/app/src/components/DBTable/__tests__/sorting.test.ts +++ b/packages/app/src/components/DBTable/__tests__/sorting.test.ts @@ -1,4 +1,13 @@ -import { numericRowSortingFn } from '@/components/DBTable/sorting'; +import { + BuilderChartConfig, + DisplayType, + MetricsDataType, +} from '@hyperdx/common-utils/dist/types'; + +import { + getAliasedResultColumns, + numericRowSortingFn, +} from '@/components/DBTable/sorting'; function makeRow(value: unknown) { return { getValue: (_key: string) => value } as any; @@ -60,3 +69,213 @@ describe('numericRowSortingFn', () => { ).toBeGreaterThan(0); }); }); + +describe('getAliasedResultColumns', () => { + const METRIC_TABLES = { + gauge: 'otel_metrics_gauge', + sum: 'otel_metrics_sum', + histogram: 'otel_metrics_histogram', + summary: 'otel_metrics_summary', + 'exponential histogram': 'otel_metrics_exponential_histogram', + }; + + const makeConfig = ( + overrides: Partial, + ): BuilderChartConfig => ({ + displayType: DisplayType.Table, + connection: 'conn', + from: { databaseName: 'default', tableName: 'otel_traces' }, + timestampValueExpression: 'Timestamp', + where: '', + select: [], + ...overrides, + }); + + const meta = (...names: string[]) => + names.map(name => ({ name, type: 'Float64' })); + + const gauge = (alias?: string) => ({ + aggFn: 'avg' as const, + valueExpression: 'Value', + metricName: 'cpu', + metricType: MetricsDataType.Gauge, + alias, + }); + + it('includes series aliases but not unaliased series', () => { + const config = makeConfig({ + select: [ + { aggFn: 'count', valueExpression: '' }, + { aggFn: 'avg', valueExpression: 'Duration', alias: 'duration min' }, + { aggFn: 'max', valueExpression: 'Duration', alias: ' ' }, + ], + }); + expect( + getAliasedResultColumns( + config, + meta('count()', 'duration min', 'max(Duration)'), + ), + ).toEqual(new Set(['duration min'])); + }); + + it('includes every formula column, and operands only when aliased', () => { + const config = makeConfig({ + select: [ + { aggFn: 'count', valueExpression: '' }, + { aggFn: 'avg', valueExpression: 'Duration', alias: 'avg dur' }, + ], + formulas: [ + { expression: 'B / A', alias: 'duration min' }, + { expression: 'A * 2' }, + ], + }); + expect( + getAliasedResultColumns( + config, + meta('count()', 'avg dur', 'duration min', 'A * 2'), + ), + ).toEqual(new Set(['avg dur', 'duration min', 'A * 2'])); + }); + + it('includes formula columns when the operands are hidden', () => { + const config = makeConfig({ + select: [ + { aggFn: 'count', valueExpression: '' }, + { aggFn: 'avg', valueExpression: 'Duration' }, + ], + formulas: [{ expression: 'B / A', alias: 'duration min' }], + showOperandSeries: false, + groupBy: 'ServiceName', + }); + expect( + getAliasedResultColumns(config, meta('duration min', 'ServiceName')), + ).toEqual(new Set(['duration min'])); + }); + + it('excludes the merged ratio column', () => { + const config = makeConfig({ + select: [ + { aggFn: 'count', valueExpression: '', alias: 'errors' }, + { aggFn: 'count', valueExpression: '', alias: 'total' }, + ], + seriesReturnType: 'ratio', + }); + expect( + getAliasedResultColumns(config, meta('divide(errors, total)')), + ).toEqual(new Set()); + }); + + it('includes aliased group-by entries from a group-by string', () => { + const config = makeConfig({ + select: [{ aggFn: 'count', valueExpression: '' }], + groupBy: `ServiceName, SpanAttributes['http.method'], ResourceAttributes['service.name'] AS "svc name", toString(Duration > 1000) as slow`, + }); + expect( + getAliasedResultColumns( + config, + meta( + 'count()', + 'ServiceName', + "arrayElement(SpanAttributes, 'http.method')", + 'svc name', + 'slow', + ), + ), + ).toEqual(new Set(['svc name', 'slow'])); + }); + + it('includes aliased entries from a structured group-by', () => { + const config = makeConfig({ + select: [{ aggFn: 'count', valueExpression: '' }], + groupBy: [ + { valueExpression: 'ServiceName' }, + { + valueExpression: "ResourceAttributes['service.name']", + alias: 'svc name', + }, + ], + }); + expect( + getAliasedResultColumns( + config, + meta('count()', 'ServiceName', 'svc name'), + ), + ).toEqual(new Set(['svc name'])); + }); + + it('treats a single-series metric like any other query', () => { + const config = makeConfig({ + metricTables: METRIC_TABLES, + select: [gauge()], + groupBy: `ServiceName, ResourceAttributes['service.name'] AS "svc name"`, + }); + expect( + getAliasedResultColumns( + config, + meta( + 'avg(toFloat64OrDefault(toString(LastValue)))', + 'ServiceName', + 'svc name', + ), + ), + ).toEqual(new Set(['svc name'])); + }); + + it.each([ + { + name: 'multiple series', + select: [gauge('cpu avg'), gauge()], + formulas: undefined, + }, + { + name: 'a single series with a formula', + select: [gauge()], + formulas: [{ expression: 'A * 100' }], + }, + ])( + 'includes every column of a metric query with $name', + ({ select, formulas }) => { + const config = makeConfig({ + metricTables: METRIC_TABLES, + select, + formulas, + groupBy: `ServiceName, Attributes['host']`, + }); + const names = [ + 'avg(cpu)', + 'ServiceName', + "arrayElement(Attributes, 'host')", + ]; + expect(getAliasedResultColumns(config, meta(...names))).toEqual( + new Set(names), + ); + }, + ); + + it('leaves the packed group column of a histogram bare', () => { + const config = makeConfig({ + metricTables: METRIC_TABLES, + select: [ + { + aggFn: 'quantile', + level: 0.95, + valueExpression: 'Value', + metricName: 'latency', + metricType: MetricsDataType.Histogram, + alias: 'p95 lat', + }, + ], + groupBy: `ServiceName, Attributes['host'] AS "the host"`, + }); + expect(getAliasedResultColumns(config, meta('group', 'p95 lat'))).toEqual( + new Set(['p95 lat']), + ); + }); + + it('returns nothing for a string select', () => { + const config = makeConfig({ select: 'count() AS "total count"' }); + expect(getAliasedResultColumns(config, meta('total count'))).toEqual( + new Set(), + ); + }); +}); diff --git a/packages/app/src/components/DBTable/sorting.ts b/packages/app/src/components/DBTable/sorting.ts index 3dc1583142..7efdedd098 100644 --- a/packages/app/src/components/DBTable/sorting.ts +++ b/packages/app/src/components/DBTable/sorting.ts @@ -2,8 +2,13 @@ import { ColumnMetaType, convertCHDataTypeToJSType, } from '@hyperdx/common-utils/dist/clickhouse'; +import { isComposedMetricChartConfig } from '@hyperdx/common-utils/dist/core/renderChartConfig'; +import { BuilderChartConfig } from '@hyperdx/common-utils/dist/types'; import { Row, SortingFnOption } from '@tanstack/react-table'; +import { getBuilderGroupByColumns } from '@/hooks/useResultColumnFilterKeys'; +import { getBuilderValueColumnCount, getFormulaConfig } from '@/source'; + export const numericRowSortingFn = (( a: Row, b: Row, @@ -38,3 +43,50 @@ export const getClientSideSortingFn = ( // metadata indicates that the column type is date (in which case the value will be a string type in JS) return 'alphanumeric'; }; + +const isNonEmptyAlias = (alias: string | undefined) => + alias != null && alias.trim() !== ''; + +/** + * The result columns of a builder table query that are explicit aliases in + * its final SELECT. ORDER BY must reference those by their quoted name, and + * every other column by its bare expression: ClickHouse re-evaluates a bare + * expression, but can't resolve a quoted auto-generated name like `"count()"`. + */ +export function getAliasedResultColumns( + config: BuilderChartConfig, + meta: ColumnMetaType[], +): Set { + if (!Array.isArray(config.select)) { + return new Set(); + } + if (isComposedMetricChartConfig(config)) { + return new Set(meta.map(column => column.name)); + } + + // Matched by name, not position: a histogram puts its packed group column + // ahead of the value column, and a ratio merges its series into one + // unaliased column. + const seriesAliases = new Set( + config.select.flatMap(s => (isNonEmptyAlias(s.alias) ? [s.alias] : [])), + ); + const aliased = new Set( + meta.map(column => column.name).filter(name => seriesAliases.has(name)), + ); + // Formula columns are always aliased, by their alias or expression, and + // follow the operand columns. They're matched by position because duplicate + // names get a suffix. + const formulaConfig = getFormulaConfig(config); + if (formulaConfig) { + const operandCount = formulaConfig.operandsHidden + ? 0 + : config.select.length; + meta + .slice(operandCount, getBuilderValueColumnCount(config)) + .forEach(column => aliased.add(column.name)); + } + for (const { column, alias } of getBuilderGroupByColumns(config, meta)) { + if (alias != null) aliased.add(column.name); + } + return aliased; +} diff --git a/packages/app/src/components/DBTableChart.tsx b/packages/app/src/components/DBTableChart.tsx index 028a6cae13..0f4c65d4f6 100644 --- a/packages/app/src/components/DBTableChart.tsx +++ b/packages/app/src/components/DBTableChart.tsx @@ -1,6 +1,9 @@ import { useCallback, useMemo, useState } from 'react'; import { inferNumericColumn } from '@hyperdx/common-utils/dist/clickhouse'; -import { isRatioChartConfig } from '@hyperdx/common-utils/dist/core/renderChartConfig'; +import { + isRatioChartConfig, + quotedColumnName, +} from '@hyperdx/common-utils/dist/core/renderChartConfig'; import { isBuilderChartConfig, isPromqlChartConfig, @@ -39,7 +42,10 @@ import ChartErrorState, { ChartErrorStateVariant, } from './charts/ChartErrorState'; import type { CellFilterHandler } from './DBTable/DBRowTableFieldWithPopover'; -import { getClientSideSortingFn } from './DBTable/sorting'; +import { + getAliasedResultColumns, + getClientSideSortingFn, +} from './DBTable/sorting'; import MVOptimizationIndicator from './MaterializedViews/MVOptimizationIndicator'; export default function DBTableChart({ @@ -139,25 +145,14 @@ export default function DBTableChart({ }); const { observerRef: fetchMoreRef } = useIntersectionObserver(fetchNextPage); - // Returns an array of aliases, so we can check if something is using an alias - const aliasMap = useMemo(() => { - if (isRawSqlChartConfig(config) || isPromqlChartConfig(config)) { - return []; - } - - // If the config.select is a string, we can't infer this. - // One day, we could potentially run this through chSqlToAliasMap but AST parsing - // doesn't work for most DBTableChart queries. - if (typeof config.select === 'string') { - return []; - } - return config.select.reduce((acc, select) => { - if (select.alias) { - acc.push(select.alias); - } - return acc; - }, [] as string[]); - }, [config]); + const resultMeta = data?.meta; + const aliasedColumns = useMemo( + () => + isBuilderChartConfig(queriedConfig) && resultMeta + ? getAliasedResultColumns(queriedConfig, resultMeta) + : new Set(), + [queriedConfig, resultMeta], + ); const { formatByColumn } = useChartNumberFormats(queriedConfig, data?.meta); @@ -257,8 +252,8 @@ export default function DBTableChart({ return orderedKeys .filter(key => !hiddenColumns?.includes(key)) .map(key => ({ - // If it's an alias, wrap in quotes to support a variety of formats (ex "Time (ms)", "Req/s", etc) - id: aliasMap.includes(key) ? `"${key}"` : key, + // The sort id becomes the ORDER BY expression (see queriedConfig). + id: aliasedColumns.has(key) ? quotedColumnName(key) : key, dataKey: key, displayName: key, numberFormat: groupByKeys.includes(key) @@ -276,7 +271,7 @@ export default function DBTableChart({ queriedConfig, hiddenColumns, filterKeyByColumn, - aliasMap, + aliasedColumns, formatByColumn, colorByColumn, rulesByColumn, diff --git a/packages/app/src/components/__tests__/DBTableChart.test.tsx b/packages/app/src/components/__tests__/DBTableChart.test.tsx index 06ebdba068..ab8691b9d7 100644 --- a/packages/app/src/components/__tests__/DBTableChart.test.tsx +++ b/packages/app/src/components/__tests__/DBTableChart.test.tsx @@ -43,6 +43,7 @@ jest.mock('@/source', () => ({ // group-by column inference under test matches production behavior. getBuilderValueColumnCount: jest.requireActual('@/source').getBuilderValueColumnCount, + getFormulaConfig: jest.requireActual('@/source').getFormulaConfig, })); jest.mock('@/HDXMultiSeriesTableChart', () => ({ @@ -56,6 +57,9 @@ jest.mock('@/hooks/useOnClickLinkBuilder', () => ({ jest.mock('@/hooks/useResultColumnFilterKeys', () => ({ useResultColumnFilterKeys: jest.fn(() => new Map()), + getBuilderGroupByColumns: jest.requireActual( + '@/hooks/useResultColumnFilterKeys', + ).getBuilderGroupByColumns, })); jest.mock('../MaterializedViews/MVOptimizationIndicator', () => @@ -439,6 +443,65 @@ describe('DBTableChart', () => { }); }); + describe('column sort ids', () => { + it('quotes aliased columns and leaves unaliased expressions bare', () => { + const row = { + 'count()': 10, + 'duration min': 42, + 'per call': 4.2, + ServiceName: 'web', + 'span name': 'GET /', + }; + jest.mocked(useOffsetPaginatedQuery).mockReturnValue({ + data: { + data: [row], + meta: Object.keys(row).map(name => ({ name, type: 'String' })), + chSql: { sql: '', params: {} }, + window: { + startTime: new Date(), + endTime: new Date(), + windowIndex: 0, + direction: 'DESC' as const, + }, + }, + fetchNextPage: jest.fn(), + hasNextPage: false, + isLoading: false, + isFetching: false, + isError: false, + error: null, + isPlaceholderData: false, + }); + + renderWithMantine( + , + ); + + const columns = jest.mocked(Table).mock.calls.at(-1)![0].columns; + expect(columns.map(c => c.id)).toEqual([ + 'count()', + '"duration min"', + '"per call"', + 'ServiceName', + '"span name"', + ]); + }); + }); + describe('alternateRowBackground', () => { const builderConfig = { ...baseTestConfig, diff --git a/packages/app/src/hooks/__tests__/useResultColumnFilterKeys.test.ts b/packages/app/src/hooks/__tests__/useResultColumnFilterKeys.test.ts index 51f9b01c67..22821aebc4 100644 --- a/packages/app/src/hooks/__tests__/useResultColumnFilterKeys.test.ts +++ b/packages/app/src/hooks/__tests__/useResultColumnFilterKeys.test.ts @@ -5,6 +5,7 @@ import { import { getBuilderFilterKeys, + getBuilderGroupByColumns, getPromqlFilterKeys, getRawSqlFilterKeys, } from '@/hooks/useResultColumnFilterKeys'; @@ -161,6 +162,45 @@ describe('getBuilderFilterKeys', () => { }); }); +describe('getBuilderGroupByColumns', () => { + it('pairs each group-by column with its expression and alias', () => { + const config: BuilderChartConfig = { + select: [{ aggFn: 'count', valueExpression: '' }], + from: { databaseName: 'default', tableName: 'logs' }, + where: '', + timestampValueExpression: 'Timestamp', + connection: 'local', + groupBy: `ServiceName, LogAttributes['k'] AS "the k", toStartOfDay(Timestamp) AS day`, + }; + expect( + getBuilderGroupByColumns( + config, + meta( + ['count()', 'UInt64'], + ['ServiceName', 'String'], + ['the k', 'String'], + ['day', 'DateTime'], + ), + ), + ).toEqual([ + { + column: { name: 'ServiceName', type: 'String' }, + expression: 'ServiceName', + }, + { + column: { name: 'the k', type: 'String' }, + expression: "LogAttributes['k']", + alias: 'the k', + }, + { + column: { name: 'day', type: 'DateTime' }, + expression: 'toStartOfDay(Timestamp)', + alias: 'day', + }, + ]); + }); +}); + describe('getRawSqlFilterKeys', () => { it('keys grouped columns by the expression behind them, leaving out aggregates', () => { const keys = getRawSqlFilterKeys( diff --git a/packages/app/src/hooks/useResultColumnFilterKeys.ts b/packages/app/src/hooks/useResultColumnFilterKeys.ts index f2b2ca89b2..553f74e72b 100644 --- a/packages/app/src/hooks/useResultColumnFilterKeys.ts +++ b/packages/app/src/hooks/useResultColumnFilterKeys.ts @@ -36,55 +36,75 @@ type FilterKeys = ReadonlyMap; const EMPTY_FILTER_KEYS: FilterKeys = new Map(); -/** The expression of a select item, without any `AS alias`. */ -const stripAlias = (item: string): string => - Object.values( +/** Splits a select item into its expression and its `AS alias`, if any. */ +const parseSelectItem = ( + item: string, +): { expression: string; alias?: string } => { + const [entry] = Object.entries( chSqlToAliasMap({ sql: `SELECT ${item} FROM t`, params: {} }), - )[0] ?? item; + ); + return entry + ? { expression: entry[1], alias: entry[0] } + : { expression: item }; +}; + +export type BuilderGroupByColumn = { + column: ColumnMetaType; + expression: string; + alias?: string; +}; /** - * Builder group-by columns follow the value columns (see `renderSelect`). They - * are matched by position because ClickHouse renames unaliased expressions. + * The result columns of a builder query that hold its group-by values, with + * the group-by entry behind each. They follow the value columns (see + * `renderSelect`) and are matched by position, because ClickHouse renames + * unaliased expressions. */ -export function getBuilderFilterKeys( +export function getBuilderGroupByColumns( config: BuilderChartConfig, meta: ColumnMetaType[], -): FilterKeys { +): BuilderGroupByColumn[] { if (!config.groupBy || config.selectGroupBy === false) { - return EMPTY_FILTER_KEYS; + return []; } - // Histograms pack their group-by values into one array column, which can't - // be filtered on. A mix with other metric types still has a column per - // group-by, for the other types' rows. + // Histograms pack their group-by values into one array column, so there + // are no individual group-by columns. A mix with other metric types still + // has a column per group-by, for the other types' rows. if ( Array.isArray(config.select) && config.select.every(isHistogramClassSelect) ) { - return EMPTY_FILTER_KEYS; + return []; } const expanded = tryExpandConfigVariables(config); const groupBy = expanded.groupBy ?? []; - const groupByExpressions = + const entries = typeof groupBy === 'string' - ? splitAndTrimWithBracket(groupBy).map(stripAlias) - : groupBy.map(entry => entry.valueExpression); - const selectExpressionCount = + ? splitAndTrimWithBracket(groupBy).map(parseSelectItem) + : groupBy.map(entry => ({ + expression: entry.valueExpression, + alias: entry.alias?.trim() ? entry.alias : undefined, + })); + const valueColumnCount = typeof expanded.select === 'string' ? splitAndTrimWithBracket(expanded.select).length : getBuilderValueColumnCount(expanded); + return meta + .slice(valueColumnCount, valueColumnCount + entries.length) + .map((column, index) => ({ column, ...entries[index] })); +} + +export function getBuilderFilterKeys( + config: BuilderChartConfig, + meta: ColumnMetaType[], +): FilterKeys { const keys = new Map(); - meta - .slice( - // Skip the expressions corresponding to the select columns - selectExpressionCount, - // Group by expressions come right after them. - selectExpressionCount + groupByExpressions.length, - ) - .forEach((column, index) => { - if (isAdhocComparableDataType(column.type)) - keys.set(column.name, groupByExpressions[index]); - }); + for (const { column, expression } of getBuilderGroupByColumns(config, meta)) { + if (isAdhocComparableDataType(column.type)) { + keys.set(column.name, expression); + } + } return keys; } diff --git a/packages/app/src/source.ts b/packages/app/src/source.ts index 2c1b781860..669149bbc6 100644 --- a/packages/app/src/source.ts +++ b/packages/app/src/source.ts @@ -696,7 +696,7 @@ interface ResolvedNumberFormats { * multi-series query and event (log/trace) formulas compile inline, but both * project the same column shape (operands unless hidden, then formulas). */ -function getFormulaConfig( +export function getFormulaConfig( config: ChartConfig | ChartConfigWithOptDateRange, ): { formulas: MetricFormula[]; operandsHidden: boolean } | undefined { if (!isBuilderChartConfig(config) || !config.formulas?.length) { diff --git a/packages/app/tests/e2e/components/ChartEditorComponent.ts b/packages/app/tests/e2e/components/ChartEditorComponent.ts index 25e68e9bd6..5c708a0945 100644 --- a/packages/app/tests/e2e/components/ChartEditorComponent.ts +++ b/packages/app/tests/e2e/components/ChartEditorComponent.ts @@ -1436,10 +1436,10 @@ export class ChartEditorComponent { const expressionInput = this.page .getByTestId('formula-expression-input') .last(); - await expressionInput.fill(expression); if (alias !== undefined) { await this.page.getByTestId('series-alias-input').last().fill(alias); } + await expressionInput.fill(expression); await expressionInput.blur(); } diff --git a/packages/app/tests/e2e/features/chart-explorer.spec.ts b/packages/app/tests/e2e/features/chart-explorer.spec.ts index acb05b582c..edbe516757 100644 --- a/packages/app/tests/e2e/features/chart-explorer.spec.ts +++ b/packages/app/tests/e2e/features/chart-explorer.spec.ts @@ -2,7 +2,11 @@ import { DisplayType } from '@hyperdx/common-utils/dist/types'; import { ChartExplorerPage } from '../page-objects/ChartExplorerPage'; import { expect, test } from '../utils/base-test'; -import { DEFAULT_LOGS_SOURCE_NAME, E2E_LOGS_TABLE } from '../utils/constants'; +import { + DEFAULT_LOGS_SOURCE_NAME, + DEFAULT_METRICS_SOURCE_NAME, + E2E_LOGS_TABLE, +} from '../utils/constants'; test.describe('Chart Explorer Functionality', { tag: ['@charts'] }, () => { let chartExplorerPage: ChartExplorerPage; @@ -159,6 +163,48 @@ test.describe('Chart Explorer Functionality', { tag: ['@charts'] }, () => { }); }); + test( + 'should sort a metric table by a formula column whose alias has a space', + { tag: ['@full-stack'] }, + async ({ page }) => { + const { chartEditor } = chartExplorerPage; + + await test.step('Query a metric table with a formula aliased "duration min"', async () => { + await expect(chartExplorerPage.form).toBeVisible(); + await chartEditor.setChartType(DisplayType.Table); + await chartEditor.selectSource(DEFAULT_METRICS_SOURCE_NAME); + await chartEditor.selectMetricForSeries( + 0, + 'container.cpu.utilization', + 'container.cpu.utilization:::::::gauge', + ); + await chartEditor.addFormula('A * 100', 'duration min'); + await chartEditor.runQuery(false); + await expect( + chartExplorerPage.getTableSortButton('duration min'), + ).toBeVisible({ timeout: 15000 }); + }); + + await test.step('Sort by the formula column and verify the query succeeds', async () => { + await chartExplorerPage.getTableSortButton('duration min').click(); + + await expect + .poll(() => chartExplorerPage.getOrderByText(), { timeout: 10000 }) + .toMatch(/^"duration min" (ASC|DESC)$/); + await expect( + chartExplorerPage + .getTableSortButton('duration min') + .getByTestId('raw-log-table-sort-indicator'), + ).toBeVisible({ timeout: 15000 }); + await page.waitForLoadState('networkidle'); + await expect(page.getByTestId('chart-error-state')).toHaveCount(0); + await expect( + page.locator('table tbody tr[data-index]').first(), + ).toBeVisible(); + }); + }, + ); + test('should apply a custom ORDER BY on a categorical pie chart', async () => { let ascendingLabels: string[] = []; let descendingLabels: string[] = []; diff --git a/packages/app/tests/e2e/page-objects/ChartExplorerPage.ts b/packages/app/tests/e2e/page-objects/ChartExplorerPage.ts index 2c7d4de75d..f28e2b694c 100644 --- a/packages/app/tests/e2e/page-objects/ChartExplorerPage.ts +++ b/packages/app/tests/e2e/page-objects/ChartExplorerPage.ts @@ -84,6 +84,22 @@ export class ChartExplorerPage { return titles.filter(t => t.length > 0); } + /** The sort button in the header of the results table column `name`. */ + getTableSortButton(name: string) { + return this.page + .getByTestId('raw-log-table-sort-button') + .filter({ hasText: name }); + } + + /** The text of the chart editor's ORDER BY input. */ + async getOrderByText(): Promise { + const text = await this.page + .getByTestId('order-by-input') + .locator('.cm-content') + .textContent(); + return (text ?? '').trim(); + } + // Getters for assertions get form() { diff --git a/packages/common-utils/src/core/renderChartConfig.ts b/packages/common-utils/src/core/renderChartConfig.ts index 95c696b4d9..20dc8affab 100644 --- a/packages/common-utils/src/core/renderChartConfig.ts +++ b/packages/common-utils/src/core/renderChartConfig.ts @@ -138,6 +138,20 @@ export const hasMetricFormulas = ( chartConfig: BuilderChartConfigWithOptDateRange, ): boolean => (chartConfig.formulas?.length ?? 0) > 0; +/** + * Whether a metric config renders as one composed query (see + * renderMultiSeriesMetricChartConfig): more than one series, or any formula. + * Every output column of a composed query is an explicit alias of the outer + * SELECT. + */ +export const isComposedMetricChartConfig = ( + chartConfig: BuilderChartConfigWithOptDateRange, +): boolean => + isMetricChartConfig(chartConfig) && + Array.isArray(chartConfig.select) && + (chartConfig.select.length > 1 || + (chartConfig.select.length === 1 && hasMetricFormulas(chartConfig))); + // TODO: apply this to all chart configs export const setChartSelectsAlias = ( config: BuilderChartConfigWithOptDateRange, @@ -186,7 +200,8 @@ const MULTI_SERIES_SORT_ALIAS_PREFIX = '__hdx_sort_'; * early. Escaping happens only at SQL-emission time — collision dedup and * the meta column names consumers see keep the raw name. */ -const quotedColumnName = (name: string) => `"${name.replace(/"/g, '""')}"`; +export const quotedColumnName = (name: string) => + `"${name.replace(/"/g, '""')}"`; // Histogram translations bake the group-by dimensions into a single Array // column named GROUP_ALIAS instead of projecting them as individual columns @@ -3484,13 +3499,7 @@ export async function renderChartConfig( // (and without formulas). A single-series chart with a formula (e.g. // `A * 100`) also takes the composed path — the formula projects over the // pivoted value columns, which only the composed shape produces. - if ( - isMetricChartConfig(substitutedChartConfig) && - Array.isArray(substitutedChartConfig.select) && - (substitutedChartConfig.select.length > 1 || - (substitutedChartConfig.select.length === 1 && - hasMetricFormulas(substitutedChartConfig))) - ) { + if (isComposedMetricChartConfig(substitutedChartConfig)) { return renderMultiSeriesMetricChartConfig( substitutedChartConfig, metadata,