From 4c20cbaa85957cf500b1c7c583c690ab51a30f9d Mon Sep 17 00:00:00 2001 From: Elizabet Oliveira Date: Wed, 21 Oct 2026 16:42:39 +0100 Subject: [PATCH 1/4] feat(app): add the column and sort editors These editors are not on the search page yet. Removing a column now drops only that field from a multi-field sort, including names that contain a comma. Co-authored-by: Cursor --- .changeset/column-sort-editors.md | 5 + packages/app/src/__tests__/utils.test.ts | 38 +++ .../SearchResultsToolbar/ClausePopover.tsx | 131 ++++++++++ .../SearchResultsToolbar/ColumnsPopover.tsx | 226 +++++++++++++++++ .../SearchResultsToolbar/SortPopover.tsx | 223 +++++++++++++++++ .../SearchResultsToolbar/SortableColumn.tsx | 75 ++++++ .../__tests__/ColumnsPopover.test.tsx | 227 ++++++++++++++++++ .../__tests__/SortPopover.test.tsx | 207 ++++++++++++++++ .../__tests__/useClauseFieldOptions.test.tsx | 80 ++++++ .../SearchResultsToolbar/sortClause.ts | 112 +++++++++ .../useClauseFieldOptions.ts | 47 ++++ packages/app/src/utils.ts | 33 ++- 12 files changed, 1400 insertions(+), 4 deletions(-) create mode 100644 .changeset/column-sort-editors.md create mode 100644 packages/app/src/components/SearchResultsToolbar/ClausePopover.tsx create mode 100644 packages/app/src/components/SearchResultsToolbar/ColumnsPopover.tsx create mode 100644 packages/app/src/components/SearchResultsToolbar/SortPopover.tsx create mode 100644 packages/app/src/components/SearchResultsToolbar/SortableColumn.tsx create mode 100644 packages/app/src/components/SearchResultsToolbar/__tests__/ColumnsPopover.test.tsx create mode 100644 packages/app/src/components/SearchResultsToolbar/__tests__/SortPopover.test.tsx create mode 100644 packages/app/src/components/SearchResultsToolbar/__tests__/useClauseFieldOptions.test.tsx create mode 100644 packages/app/src/components/SearchResultsToolbar/sortClause.ts create mode 100644 packages/app/src/components/SearchResultsToolbar/useClauseFieldOptions.ts diff --git a/.changeset/column-sort-editors.md b/.changeset/column-sort-editors.md new file mode 100644 index 0000000000..3e9a1bcd1e --- /dev/null +++ b/.changeset/column-sort-editors.md @@ -0,0 +1,5 @@ +--- +'@hyperdx/app': patch +--- + +When a removed column is one field in a multi-field sort, drop only that field diff --git a/packages/app/src/__tests__/utils.test.ts b/packages/app/src/__tests__/utils.test.ts index 864d5ba1b2..92774a7183 100644 --- a/packages/app/src/__tests__/utils.test.ts +++ b/packages/app/src/__tests__/utils.test.ts @@ -30,6 +30,7 @@ import { parseTimestampToMs, resolveConditionalColor, sortingStateToOrderByString, + splitClauseList, stripTrailingSlash, useQueryHistory, withMapKeyAlias, @@ -1506,6 +1507,16 @@ describe('isColumnInSelect', () => { }); }); +describe('splitClauseList', () => { + it('keeps a comma inside a backtick-quoted name', () => { + expect(splitClauseList('Timestamp, `a,b`, Body')).toEqual([ + 'Timestamp', + '`a,b`', + 'Body', + ]); + }); +}); + describe('orderByAfterRemovingSelectItem', () => { const item = `ResourceAttributes['service.name'] AS "service.name"`; @@ -1534,6 +1545,33 @@ describe('orderByAfterRemovingSelectItem', () => { orderByAfterRemovingSelectItem('Body', 'Body DESC', 'Timestamp DESC'), ).toBe('Body DESC'); }); + + it('drops only the removed alias from a multi-field sort', () => { + expect( + orderByAfterRemovingSelectItem( + item, + '"service.name" ASC, Timestamp DESC', + 'Timestamp DESC', + ), + ).toBe('Timestamp DESC'); + expect( + orderByAfterRemovingSelectItem( + item, + '"http.method" DESC, "service.name" ASC', + 'Timestamp DESC', + ), + ).toBe('"http.method" DESC'); + }); + + it('keeps a comma inside a backtick-quoted sort name', () => { + expect( + orderByAfterRemovingSelectItem( + 'upper(Body) AS "message"', + '"message" ASC, `a,b` DESC', + 'Timestamp DESC', + ), + ).toBe('`a,b` DESC'); + }); }); describe('getLogSeverityLevel', () => { diff --git a/packages/app/src/components/SearchResultsToolbar/ClausePopover.tsx b/packages/app/src/components/SearchResultsToolbar/ClausePopover.tsx new file mode 100644 index 0000000000..1613d23dd0 --- /dev/null +++ b/packages/app/src/components/SearchResultsToolbar/ClausePopover.tsx @@ -0,0 +1,131 @@ +import { type ReactNode, useState } from 'react'; +import { Button, Group, Popover, SegmentedControl, Stack } from '@mantine/core'; + +import { SplitValueButton } from '@/components/SplitValueButton/SplitValueButton'; + +export type ClauseMode = 'pick' | 'sql'; + +const MODE_OPTIONS = [ + { label: 'Fields', value: 'pick' }, + { label: 'SQL', value: 'sql' }, +]; + +/** + * A toolbar button that edits one query clause in a popover. Edits stay in a + * draft owned by the caller until Apply, so closing the popover discards them. + */ +export function ClausePopover({ + name, + summary, + count, + dimmed, + title, + initialMode, + onOpen, + onApply, + onReset, + resetDisabled, + renderPick, + renderSql, + 'data-testid': testId, +}: { + /** The muted side of the split button, such as Columns or Sort. */ + name: string; + /** Desktop value: a field name, "3 fields", or "none". */ + summary: string; + /** How many fields the summary counts. */ + count: number; + /** The clause is still the source default, so the value is quiet. */ + dimmed?: boolean; + /** The full clause, shown on hover since the value only summarizes it. */ + title: string; + /** The tab to show each time the popover opens. */ + initialMode: ClauseMode; + /** Reset the caller's draft to the current value. */ + onOpen: () => void; + onApply: () => void; + /** Restore the source default in the draft. Apply still has to confirm it. */ + onReset: () => void; + resetDisabled?: boolean; + renderPick: (apply: () => void) => ReactNode; + renderSql: (apply: () => void) => ReactNode; + 'data-testid': string; +}) { + const [opened, setOpened] = useState(false); + const [mode, setMode] = useState(initialMode); + + const open = () => { + onOpen(); + setMode(initialMode); + setOpened(true); + }; + + const apply = () => { + onApply(); + setOpened(false); + }; + + return ( + + + (opened ? setOpened(false) : open())} + data-testid={testId} + /> + + + + setMode(value === 'sql' ? 'sql' : 'pick')} + data={MODE_OPTIONS} + data-testid={`${testId}-mode`} + /> + {mode === 'pick' ? renderPick(apply) : renderSql(apply)} + + + + + + + + + + + ); +} diff --git a/packages/app/src/components/SearchResultsToolbar/ColumnsPopover.tsx b/packages/app/src/components/SearchResultsToolbar/ColumnsPopover.tsx new file mode 100644 index 0000000000..375a0e5dcf --- /dev/null +++ b/packages/app/src/components/SearchResultsToolbar/ColumnsPopover.tsx @@ -0,0 +1,226 @@ +import { useMemo, useRef, useState } from 'react'; +import { + DndContext, + type DragEndEvent, + MouseSensor, + TouchSensor, + useSensor, + useSensors, +} from '@dnd-kit/core'; +import { + arrayMove, + SortableContext, + verticalListSortingStrategy, +} from '@dnd-kit/sortable'; +import { TableConnection } from '@hyperdx/common-utils/dist/core/metadata'; +import { + Autocomplete, + Box, + Button, + Code, + Group, + Stack, + Text, +} from '@mantine/core'; + +import { fieldListSummary } from '@/components/SplitValueButton/SplitValueButton'; +import SQLInlineEditor from '@/components/SQLEditor/SQLInlineEditor'; +import { splitClauseList, withMapKeyAlias } from '@/utils'; + +import { ClausePopover } from './ClausePopover'; +import { SortableColumn } from './SortableColumn'; + +const columnId = (item: string, index: number) => `${index}:${item}`; + +export function ColumnsPopover({ + value, + defaultValue, + fieldOptions, + knownColumns, + onApply, + tableConnection, + sourceId, + dateRange, +}: { + /** The SELECT expression in the search form. */ + value: string; + /** The source's SELECT, used while `value` is empty. */ + defaultValue: string; + fieldOptions: string[]; + knownColumns: Set; + /** Called with the new SELECT and the items it no longer contains. */ + onApply: (select: string, removedItems: string[]) => void; + tableConnection: TableConnection | undefined; + sourceId?: string; + dateRange?: [Date, Date]; +}) { + const [draft, setDraft] = useState(value); + const [search, setSearch] = useState(''); + // Autocomplete writes the chosen option back into the input after + // onOptionSubmit. Ignore that write so a picked map key keeps its alias. + const ignoreNextChangeRef = useRef(false); + + // An empty stored SELECT means "use the source default". An empty draft does + // not: the user may have removed every column before adding a replacement. + const storedColumns = (select: string) => + splitClauseList(select || defaultValue); + + const shown = storedColumns(value); + const items = splitClauseList(draft); + + const options = useMemo( + () => fieldOptions.filter(option => !items.includes(option)), + [fieldOptions, items], + ); + + const addColumn = (column: string) => { + const trimmed = column.trim(); + if (trimmed && !items.includes(trimmed)) { + setDraft( + [...items, withMapKeyAlias(trimmed, items, knownColumns)].join(', '), + ); + } + setSearch(''); + }; + + const removeColumn = (item: string) => { + setDraft(items.filter(i => i !== item).join(', ')); + }; + + const columnIds = items.map(columnId); + const sensors = useSensors( + useSensor(MouseSensor, { activationConstraint: { distance: 8 } }), + useSensor(TouchSensor, { + activationConstraint: { delay: 200, tolerance: 5 }, + }), + ); + + const reorderColumns = (event: DragEndEvent) => { + const { active, over } = event; + if (!over || active.id === over.id) return; + const from = columnIds.indexOf(String(active.id)); + const to = columnIds.indexOf(String(over.id)); + if (from === -1 || to === -1) return; + setDraft(arrayMove(items, from, to).join(', ')); + }; + + return ( + { + // The form stores "" while the source default is in use. Show that + // default as the editor value; Apply keeps "" unless it was edited. + setDraft(value || defaultValue); + setSearch(''); + }} + onReset={() => { + setDraft(defaultValue); + setSearch(''); + }} + resetDisabled={draft.trim() === defaultValue.trim()} + onApply={() => { + const baseline = value || defaultValue; + // An empty form value means "use the source default", so resetting + // stores "" instead of a copy of that default. + const submitted = + draft.trim() === defaultValue.trim() + ? '' + : draft === baseline + ? value + : draft; + const next = storedColumns(submitted); + onApply( + submitted, + storedColumns(value).filter(item => !next.includes(item)), + ); + }} + data-testid="search-columns-button" + renderPick={() => ( + + + {items.length === 0 ? ( + + No columns selected. + + ) : ( + + + + {items.map((item, index) => ( + removeColumn(item)} + /> + ))} + + + + )} + + + { + if (ignoreNextChangeRef.current) { + ignoreNextChangeRef.current = false; + return; + } + setSearch(next); + }} + onOptionSubmit={option => { + ignoreNextChangeRef.current = true; + setSearch(withMapKeyAlias(option, items, knownColumns)); + }} + data={options} + limit={50} + comboboxProps={{ withinPortal: false }} + style={{ flex: 1 }} + data-testid="search-columns-add-input" + /> + + + + Select a column, or type an expression such as{' '} + upper(Body) as message, then click Add. + + + )} + renderSql={apply => ( +
+ +
+ )} + /> + ); +} diff --git a/packages/app/src/components/SearchResultsToolbar/SortPopover.tsx b/packages/app/src/components/SearchResultsToolbar/SortPopover.tsx new file mode 100644 index 0000000000..0a58fa7acc --- /dev/null +++ b/packages/app/src/components/SearchResultsToolbar/SortPopover.tsx @@ -0,0 +1,223 @@ +import { useRef, useState } from 'react'; +import { TableConnection } from '@hyperdx/common-utils/dist/core/metadata'; +import { + ActionIcon, + Autocomplete, + Group, + ScrollArea, + SegmentedControl, + Stack, + Text, +} from '@mantine/core'; +import { IconX } from '@tabler/icons-react'; + +import SQLInlineEditor from '@/components/SQLEditor/SQLInlineEditor'; + +import { ClausePopover } from './ClausePopover'; +import { + newSortRow, + parseSortKeys, + sameKeys, + serializeSortKeys, + SortRow, + sortSummary, + toRows, +} from './sortClause'; + +const DIRECTION_OPTIONS = [ + { label: 'Asc', value: 'asc' }, + { label: 'Desc', value: 'desc' }, +]; + +export function SortPopover({ + value, + defaultValue, + fieldOptions, + onApply, + tableConnection, + sourceId, + dateRange, +}: { + /** The ORDER BY expression in the search form. */ + value: string; + /** The source's ORDER BY, used while `value` is empty. */ + defaultValue: string; + fieldOptions: string[]; + onApply: (orderBy: string) => void; + tableConnection: TableConnection | undefined; + sourceId?: string; + dateRange?: [Date, Date]; +}) { + const shown = value || defaultValue; + const [draft, setDraft] = useState(shown); + const [keys, setKeys] = useState(() => + toRows(parseSortKeys(shown)), + ); + const [search, setSearch] = useState(''); + // Autocomplete writes the chosen option back into the input after + // onOptionSubmit. Ignore that write so the field clears. + const ignoreNextChangeRef = useRef(false); + + const baseline = value || defaultValue; + const shownSort = sortSummary(baseline); + const used = new Set((keys ?? []).map(key => key.id)); + const addOptions = fieldOptions.filter(option => !used.has(option)); + + const reset = () => { + const next = value || defaultValue; + setDraft(next); + setKeys(toRows(parseSortKeys(next))); + setSearch(''); + }; + + const commitKeys = (next: SortRow[]) => { + setKeys(next); + const originalKeys = parseSortKeys(baseline); + // Keep `(a, b) DESC` until a field actually changes, so Apply doesn't + // rewrite the source default into separate keys. + setDraft( + originalKeys != null && sameKeys(next, originalKeys) + ? baseline + : serializeSortKeys(next), + ); + }; + + const addKey = (id: string) => { + const trimmed = id.trim(); + // A clause the field list cannot split, such as `Timestamp DESC NULLS LAST`, + // stays intact. Adding a field here would replace the whole clause. + if (keys == null || !trimmed || used.has(trimmed)) return; + commitKeys([...keys, newSortRow(trimmed, true)]); + setSearch(''); + }; + + return ( + { + setDraft(defaultValue); + setKeys(toRows(parseSortKeys(defaultValue))); + setSearch(''); + }} + resetDisabled={draft.trim() === defaultValue.trim()} + onApply={() => + onApply( + draft.trim() === defaultValue.trim() + ? '' + : draft === baseline + ? value + : draft, + ) + } + data-testid="search-sort-button" + renderPick={() => ( + + {keys == null && ( + + Custom sort. Edit it in SQL. + + )} + {keys != null && keys.length > 0 && ( + + + {keys.map((key, index) => ( + + { + const next = keys.slice(); + next[index] = { ...key, id }; + commitKeys(next); + }} + data={fieldOptions.filter( + option => option === key.id || !used.has(option), + )} + limit={50} + comboboxProps={{ withinPortal: false }} + style={{ flex: 1 }} + /> + { + const next = keys.slice(); + next[index] = { ...key, desc: direction === 'desc' }; + commitKeys(next); + }} + data={DIRECTION_OPTIONS} + data-testid="search-sort-direction" + /> + + commitKeys(keys.filter((_, i) => i !== index)) + } + aria-label={`Remove ${key.id}`} + > + + + + ))} + + + )} + {keys != null && ( + { + if (ignoreNextChangeRef.current) { + ignoreNextChangeRef.current = false; + return; + } + setSearch(next); + }} + data={addOptions} + limit={50} + onOptionSubmit={id => { + ignoreNextChangeRef.current = true; + addKey(id); + }} + comboboxProps={{ withinPortal: false }} + data-testid="search-sort-add-input" + /> + )} + + )} + renderSql={apply => ( + + { + setDraft(sql); + setKeys(toRows(parseSortKeys(sql))); + }} + placeholder="ORDER BY" + onSubmit={apply} + label="ORDER BY" + size="xs" + dateRange={dateRange} + sourceId={sourceId} + /> + + )} + /> + ); +} diff --git a/packages/app/src/components/SearchResultsToolbar/SortableColumn.tsx b/packages/app/src/components/SearchResultsToolbar/SortableColumn.tsx new file mode 100644 index 0000000000..6df50d85e5 --- /dev/null +++ b/packages/app/src/components/SearchResultsToolbar/SortableColumn.tsx @@ -0,0 +1,75 @@ +import { useSortable } from '@dnd-kit/sortable'; +import { CSS } from '@dnd-kit/utilities'; +import { ActionIcon, Box, Group, Text } from '@mantine/core'; +import { IconGripVertical, IconX } from '@tabler/icons-react'; + +export function SortableColumn({ + id, + label, + onRemove, +}: { + id: string; + label: string; + onRemove: () => void; +}) { + const { + attributes, + listeners, + setNodeRef, + transform, + transition, + isDragging, + } = useSortable({ id }); + + return ( + + + + + + + {label} + + + + + + + ); +} diff --git a/packages/app/src/components/SearchResultsToolbar/__tests__/ColumnsPopover.test.tsx b/packages/app/src/components/SearchResultsToolbar/__tests__/ColumnsPopover.test.tsx new file mode 100644 index 0000000000..1be855de79 --- /dev/null +++ b/packages/app/src/components/SearchResultsToolbar/__tests__/ColumnsPopover.test.tsx @@ -0,0 +1,227 @@ +import React from 'react'; +import { MantineProvider } from '@mantine/core'; +import { fireEvent, render, screen, waitFor } from '@testing-library/react'; + +import { ColumnsPopover } from '@/components/SearchResultsToolbar/ColumnsPopover'; + +jest.mock('@/components/SQLEditor/SQLInlineEditor', () => ({ + __esModule: true, + default: ({ + value, + onChange, + label, + }: { + value: string; + onChange: (value: string) => void; + label: string; + }) => ( + onChange(e.target.value)} + /> + ), +})); + +function renderPopover( + props: Partial> = {}, +) { + const onApply = jest.fn(); + render( + + + , + ); + return { onApply }; +} + +const openPopover = () => + fireEvent.click(screen.getByTestId('search-columns-button')); + +describe('ColumnsPopover', () => { + it('shows the column count and falls back to the default SELECT', () => { + renderPopover({ value: '' }); + expect(screen.getByTestId('search-columns-button')).toHaveAccessibleName( + 'Columns 3 fields', + ); + }); + + it('offers a drag handle for each column', async () => { + renderPopover(); + openPopover(); + expect( + await screen.findByLabelText('Reorder Timestamp'), + ).toBeInTheDocument(); + expect(screen.getByLabelText('Reorder Body')).toBeInTheDocument(); + }); + + it('stays empty after the last column is removed', async () => { + renderPopover({ + value: 'Timestamp', + defaultValue: 'Timestamp, ServiceName, Body', + }); + openPopover(); + fireEvent.click(await screen.findByLabelText('Remove Timestamp')); + expect(screen.getByText('No columns selected.')).toBeInTheDocument(); + expect( + screen.queryByLabelText('Remove ServiceName'), + ).not.toBeInTheDocument(); + + const input = screen.getByPlaceholderText('Search columns'); + fireEvent.change(input, { target: { value: 'Body' } }); + fireEvent.click(screen.getByTestId('search-columns-add-button')); + expect(screen.getByLabelText('Remove Body')).toBeInTheDocument(); + expect( + screen.queryByLabelText('Remove ServiceName'), + ).not.toBeInTheDocument(); + }); + + it('keeps a backtick-quoted name when another column is removed', async () => { + const { onApply } = renderPopover({ + value: 'Timestamp, `a,b`, Body', + defaultValue: 'Timestamp', + }); + openPopover(); + fireEvent.click(await screen.findByLabelText('Remove Body')); + expect(screen.getByLabelText('Remove `a,b`')).toBeInTheDocument(); + fireEvent.click(screen.getByTestId('search-columns-button-apply')); + expect(onApply).toHaveBeenCalledWith('Timestamp, `a,b`', ['Body']); + }); + + it('removes a column and reports it on apply', async () => { + const { onApply } = renderPopover(); + openPopover(); + fireEvent.click(await screen.findByLabelText('Remove Body')); + fireEvent.click(screen.getByTestId('search-columns-button-apply')); + expect(onApply).toHaveBeenCalledWith('Timestamp', ['Body']); + }); + + it('leaves a typed map subscript unchanged', async () => { + renderPopover({ + fieldOptions: ["LogAttributes['region']"], + }); + openPopover(); + const input = await screen.findByPlaceholderText('Search columns'); + fireEvent.change(input, { + target: { value: "LogAttributes['region']" }, + }); + expect(input).toHaveValue("LogAttributes['region']"); + }); + + it('shows a map key alias in the input before adding it', async () => { + renderPopover({ + fieldOptions: ["LogAttributes['ads.consent_given']"], + }); + openPopover(); + const input = await screen.findByPlaceholderText('Search columns'); + fireEvent.change(input, { + target: { value: "LogAttributes['ads" }, + }); + fireEvent.click( + await screen.findByText("LogAttributes['ads.consent_given']"), + ); + expect(input).toHaveValue( + `LogAttributes['ads.consent_given'] AS "ads.consent_given"`, + ); + fireEvent.click(screen.getByTestId('search-columns-add-button')); + expect(input).toHaveValue(''); + expect( + screen.getByText( + `LogAttributes['ads.consent_given'] AS "ads.consent_given"`, + ), + ).toBeInTheDocument(); + }); + + it('fills the input from a suggestion and adds only on Add', async () => { + const { onApply } = renderPopover(); + openPopover(); + const input = await screen.findByPlaceholderText('Search columns'); + fireEvent.change(input, { target: { value: 'Severity' } }); + fireEvent.click(await screen.findByText('SeverityText')); + expect(input).toHaveValue('SeverityText'); + expect( + screen.queryByLabelText('Remove SeverityText'), + ).not.toBeInTheDocument(); + fireEvent.click(screen.getByTestId('search-columns-add-button')); + expect(input).toHaveValue(''); + expect(screen.getByLabelText('Remove SeverityText')).toBeInTheDocument(); + fireEvent.click(screen.getByTestId('search-columns-button-apply')); + expect(onApply).toHaveBeenCalledWith('Timestamp, Body, SeverityText', []); + }); + + it('adds a typed expression such as an alias', async () => { + const { onApply } = renderPopover(); + openPopover(); + const input = await screen.findByPlaceholderText('Search columns'); + fireEvent.change(input, { + target: { value: 'upper(Body) as message' }, + }); + fireEvent.click(screen.getByTestId('search-columns-add-button')); + expect(input).toHaveValue(''); + expect( + screen.getByLabelText('Remove upper(Body) as message'), + ).toBeInTheDocument(); + fireEvent.click(screen.getByTestId('search-columns-button-apply')); + expect(onApply).toHaveBeenCalledWith( + 'Timestamp, Body, upper(Body) as message', + [], + ); + }); + + it('resets a changed column list to the source default', async () => { + const { onApply } = renderPopover(); + openPopover(); + fireEvent.click(await screen.findByLabelText('Remove Body')); + fireEvent.click(screen.getByTestId('search-columns-button-reset')); + expect(screen.getByLabelText('Remove ServiceName')).toBeInTheDocument(); + expect(screen.getByTestId('search-columns-button-reset')).toBeDisabled(); + fireEvent.click(screen.getByTestId('search-columns-button-apply')); + expect(onApply).toHaveBeenCalledWith('', []); + }); + + it('shows the default SELECT as the SQL value', async () => { + renderPopover({ value: '' }); + openPopover(); + expect( + await screen.findByTestId('search-columns-button-reset'), + ).toBeDisabled(); + fireEvent.click(await screen.findByText('SQL')); + expect(screen.getByLabelText('SELECT')).toHaveValue( + 'Timestamp, ServiceName, Body', + ); + }); + + it('edits the SELECT on the SQL tab', async () => { + const { onApply } = renderPopover(); + openPopover(); + fireEvent.click(await screen.findByText('SQL')); + fireEvent.change(screen.getByLabelText('SELECT'), { + target: { value: 'count()' }, + }); + fireEvent.click(screen.getByTestId('search-columns-button-apply')); + expect(onApply).toHaveBeenCalledWith('count()', ['Timestamp', 'Body']); + }); + + it('discards the draft on cancel', async () => { + const { onApply } = renderPopover(); + openPopover(); + fireEvent.click(await screen.findByLabelText('Remove Body')); + fireEvent.click(screen.getByText('Cancel')); + await waitFor(() => + expect( + screen.queryByTestId('search-columns-button-popover'), + ).not.toBeInTheDocument(), + ); + openPopover(); + expect(await screen.findByLabelText('Remove Body')).toBeInTheDocument(); + expect(onApply).not.toHaveBeenCalled(); + }); +}); diff --git a/packages/app/src/components/SearchResultsToolbar/__tests__/SortPopover.test.tsx b/packages/app/src/components/SearchResultsToolbar/__tests__/SortPopover.test.tsx new file mode 100644 index 0000000000..d1ec5d1409 --- /dev/null +++ b/packages/app/src/components/SearchResultsToolbar/__tests__/SortPopover.test.tsx @@ -0,0 +1,207 @@ +import React from 'react'; +import { MantineProvider } from '@mantine/core'; +import { fireEvent, render, screen } from '@testing-library/react'; + +import { SortPopover } from '@/components/SearchResultsToolbar/SortPopover'; + +jest.mock('@/components/SQLEditor/SQLInlineEditor', () => ({ + __esModule: true, + default: ({ + value, + onChange, + label, + }: { + value: string; + onChange: (value: string) => void; + label: string; + }) => ( + onChange(e.target.value)} + /> + ), +})); + +function renderPopover( + props: Partial> = {}, +) { + const onApply = jest.fn(); + render( + + + , + ); + return { onApply }; +} + +const openPopover = () => + fireEvent.click(screen.getByTestId('search-sort-button')); + +describe('SortPopover', () => { + it('labels the button with the parsed field and direction', () => { + renderPopover({ value: 'Body ASC' }); + expect(screen.getByTestId('search-sort-button')).toHaveAccessibleName( + 'Sort Body asc', + ); + }); + + it('flips the direction from the picker', async () => { + const { onApply } = renderPopover(); + openPopover(); + fireEvent.click(await screen.findByText('Asc')); + fireEvent.click(screen.getByTestId('search-sort-button-apply')); + expect(onApply).toHaveBeenCalledWith('Timestamp ASC'); + }); + + it('lists a tuple sort that spans more than one line', async () => { + const orderBy = '(toDateTime(Timestamp),\n Timestamp) DESC'; + renderPopover({ value: orderBy, defaultValue: orderBy }); + expect(screen.getByTestId('search-sort-button')).toHaveAccessibleName( + 'Sort 2 fields', + ); + openPopover(); + expect( + await screen.findByDisplayValue('toDateTime(Timestamp)'), + ).toBeInTheDocument(); + expect(screen.getByDisplayValue('Timestamp')).toBeInTheDocument(); + expect(screen.queryByTestId('search-sort-custom')).not.toBeInTheDocument(); + }); + + it('lists function calls inside a tuple sort', async () => { + const orderBy = '(toDateTime(Timestamp), Timestamp) DESC'; + renderPopover({ value: orderBy, defaultValue: orderBy }); + expect(screen.getByTestId('search-sort-button')).toHaveAccessibleName( + 'Sort 2 fields', + ); + openPopover(); + expect( + await screen.findByDisplayValue('toDateTime(Timestamp)'), + ).toBeInTheDocument(); + expect(screen.getByDisplayValue('Timestamp')).toBeInTheDocument(); + expect(screen.queryByTestId('search-sort-custom')).not.toBeInTheDocument(); + }); + + it('keeps a backtick-quoted field that contains a comma on SQL', async () => { + const orderBy = '(`a,b`, Timestamp) DESC'; + renderPopover({ value: orderBy, defaultValue: orderBy }); + openPopover(); + expect(await screen.findByLabelText('ORDER BY')).toHaveValue(orderBy); + expect(screen.queryByDisplayValue('Timestamp')).not.toBeInTheDocument(); + }); + + it('opens on the SQL tab for a custom ORDER BY', async () => { + const custom = 'Timestamp DESC NULLS LAST'; + const { onApply } = renderPopover({ value: custom, defaultValue: custom }); + expect(screen.getByTestId('search-sort-button')).toHaveAccessibleName( + `Sort ${custom}`, + ); + openPopover(); + const editor = await screen.findByLabelText('ORDER BY'); + expect(editor).toHaveValue(custom); + fireEvent.click(screen.getByText('Fields')); + expect(screen.getByTestId('search-sort-custom')).toBeInTheDocument(); + expect( + screen.queryByTestId('search-sort-add-input'), + ).not.toBeInTheDocument(); + fireEvent.click(screen.getByText('SQL')); + fireEvent.change(screen.getByLabelText('ORDER BY'), { + target: { value: 'Body ASC' }, + }); + fireEvent.click(screen.getByTestId('search-sort-button-apply')); + expect(onApply).toHaveBeenCalledWith('Body ASC'); + }); + + it('shows the source default as the SQL value, not a placeholder', async () => { + const orderBy = '(TimestampTime, Timestamp) DESC'; + const { onApply } = renderPopover({ value: '', defaultValue: orderBy }); + openPopover(); + fireEvent.click(await screen.findByText('SQL')); + expect(screen.getByLabelText('ORDER BY')).toHaveValue(orderBy); + fireEvent.click(screen.getByTestId('search-sort-button-apply')); + expect(onApply).toHaveBeenCalledWith(''); + }); + + it('lists every field of a tuple sort and can add another', async () => { + const { onApply } = renderPopover({ + value: '', + defaultValue: '(TimestampTime, Timestamp) DESC', + fieldOptions: ['Body', 'Timestamp', 'TimestampTime'], + }); + expect(screen.getByTestId('search-sort-button')).toHaveAccessibleName( + 'Sort 2 fields', + ); + openPopover(); + expect( + await screen.findByDisplayValue('TimestampTime'), + ).toBeInTheDocument(); + expect(screen.getByDisplayValue('Timestamp')).toBeInTheDocument(); + + const addSort = screen.getByPlaceholderText('Add sort'); + fireEvent.change(addSort, { + target: { value: 'Bo' }, + }); + fireEvent.click(await screen.findByText('Body')); + expect(addSort).toHaveValue(''); + fireEvent.click(screen.getByTestId('search-sort-button-apply')); + expect(onApply).toHaveBeenCalledWith( + 'TimestampTime DESC, Timestamp DESC, Body DESC', + ); + }); + + it('keeps separate tuple keys instead of merging them', async () => { + const orderBy = '(a, b) ASC, (c, d) DESC'; + const { onApply } = renderPopover({ + value: orderBy, + defaultValue: orderBy, + }); + expect(screen.getByTestId('search-sort-button')).toHaveAccessibleName( + 'Sort 2 fields', + ); + openPopover(); + expect(await screen.findByDisplayValue('(a, b)')).toBeInTheDocument(); + expect(screen.getByDisplayValue('(c, d)')).toBeInTheDocument(); + fireEvent.click(screen.getAllByText('Desc')[0]); + fireEvent.click(screen.getByTestId('search-sort-button-apply')); + expect(onApply).toHaveBeenCalledWith('(a, b) DESC, (c, d) DESC'); + }); + + it('lists every key of a multi-field sort', async () => { + renderPopover({ value: 'ServiceName ASC, Timestamp DESC' }); + expect(screen.getByTestId('search-sort-button')).toHaveAccessibleName( + 'Sort 2 fields', + ); + openPopover(); + expect(await screen.findByDisplayValue('ServiceName')).toBeInTheDocument(); + expect(screen.getByDisplayValue('Timestamp')).toBeInTheDocument(); + expect(screen.getByLabelText('Remove ServiceName')).toBeInTheDocument(); + }); + + it('resets a changed sort to the source default', async () => { + const { onApply } = renderPopover({ + value: 'Body ASC', + defaultValue: 'Timestamp DESC', + }); + openPopover(); + fireEvent.click(await screen.findByTestId('search-sort-button-reset')); + expect(await screen.findByDisplayValue('Timestamp')).toBeInTheDocument(); + expect(screen.getByTestId('search-sort-button-reset')).toBeDisabled(); + fireEvent.click(screen.getByTestId('search-sort-button-apply')); + expect(onApply).toHaveBeenCalledWith(''); + }); + + it('does not apply on cancel', async () => { + const { onApply } = renderPopover(); + openPopover(); + fireEvent.click(await screen.findByText('Asc')); + fireEvent.click(screen.getByText('Cancel')); + expect(onApply).not.toHaveBeenCalled(); + }); +}); diff --git a/packages/app/src/components/SearchResultsToolbar/__tests__/useClauseFieldOptions.test.tsx b/packages/app/src/components/SearchResultsToolbar/__tests__/useClauseFieldOptions.test.tsx new file mode 100644 index 0000000000..c044494281 --- /dev/null +++ b/packages/app/src/components/SearchResultsToolbar/__tests__/useClauseFieldOptions.test.tsx @@ -0,0 +1,80 @@ +import { ColumnMeta, JSDataType } from '@hyperdx/common-utils/dist/clickhouse'; +import { Field } from '@hyperdx/common-utils/dist/core/metadata'; +import { renderHook } from '@testing-library/react'; + +import { useClauseFieldOptions } from '@/components/SearchResultsToolbar/useClauseFieldOptions'; +import { useAllFields } from '@/hooks/useMetadata'; + +jest.mock('@/hooks/useMetadata', () => ({ + useAllFields: jest.fn(), +})); + +const column = (name: string, type: string): ColumnMeta => ({ + name, + type, + codec_expression: '', + comment: '', + default_expression: '', + default_type: '', + ttl_expression: '', +}); + +const columns = [ + column('ServiceName', 'LowCardinality(String)'), + column('service-name', 'String'), + column('my-map', 'Map(String, String)'), +]; + +function mockAllFields( + data: Field[] | undefined, + isFieldMetadataDisabled: boolean, +) { + const result: ReturnType = { + data, + error: null, + isError: false, + isPending: false, + isSuccess: true, + isLoading: false, + isFetching: false, + isFieldMetadataDisabled, + fieldsByTableConnection: new Map(), + columnsByTableConnection: new Map(), + errorsByTableConnection: new Map(), + refetch: () => Promise.reject(new Error('unused')), + }; + jest.mocked(useAllFields).mockReturnValue(result); +} + +describe('useClauseFieldOptions', () => { + it('quotes column names that are not bare identifiers', () => { + mockAllFields( + [ + { path: ['ServiceName'], type: 'String', jsType: JSDataType.String }, + { path: ['service-name'], type: 'String', jsType: JSDataType.String }, + { path: ['my-map', 'k'], type: 'String', jsType: null }, + ], + false, + ); + + const { result } = renderHook(() => + useClauseFieldOptions({ tableConnection: undefined, columns }), + ); + + expect(result.current).toEqual([ + 'ServiceName', + "`my-map`['k']", + '`service-name`', + ]); + }); + + it('offers scalar columns when field metadata is disabled', () => { + mockAllFields([], true); + + const { result } = renderHook(() => + useClauseFieldOptions({ tableConnection: undefined, columns }), + ); + + expect(result.current).toEqual(['ServiceName', '`service-name`']); + }); +}); diff --git a/packages/app/src/components/SearchResultsToolbar/sortClause.ts b/packages/app/src/components/SearchResultsToolbar/sortClause.ts new file mode 100644 index 0000000000..ba586497f0 --- /dev/null +++ b/packages/app/src/components/SearchResultsToolbar/sortClause.ts @@ -0,0 +1,112 @@ +import { splitAndTrimWithBracket } from '@hyperdx/common-utils/dist/core/utils'; +import { findBalancedParens } from '@hyperdx/common-utils/dist/variables'; + +import { parseAsSortingStateString } from '@/utils/queryParsers'; + +export type SortKey = { id: string; desc: boolean }; +export type SortRow = SortKey & { rowId: string }; + +let rowSeq = 0; + +export function newSortRow(id: string, desc: boolean): SortRow { + return { id, desc, rowId: `sort-${rowSeq++}` }; +} + +export const toRows = (keys: SortKey[] | null): SortRow[] | null => + keys?.map(key => newSortRow(key.id, key.desc)) ?? null; + +/** A comma inside backticks cannot be split without breaking the identifier. */ +function hasCommaInsideBackticks(input: string): boolean { + let quoted = false; + for (let i = 0; i < input.length; i++) { + if (input[i] !== '`') { + if (quoted && input[i] === ',') return true; + continue; + } + // ClickHouse escapes a backtick by doubling it. + if (quoted && input[i + 1] === '`') { + i += 1; + continue; + } + quoted = !quoted; + } + return false; +} + +/** + * Sort keys the picker can edit. A parenthesized list with one direction, + * `(toDateTime(Timestamp), Timestamp) DESC`, is several fields sharing that + * direction, including function calls. The parentheses have to wrap that whole + * expression: `(a, b) ASC, (c, d) DESC` is two keys, not one list. Anything + * without a plain ASC/DESC on each field stays in SQL. + */ +export function parseSortKeys(orderBy: string): SortKey[] | null { + const trimmed = orderBy.trim(); + if (!trimmed) return []; + // A comma inside a backtick-quoted name would be split into extra fields. + if (hasCommaInsideBackticks(trimmed)) return null; + + // [\s\S] so a newline inside `(toDateTime(Timestamp),\n Timestamp) DESC` + // still counts as one direction. `.` would miss it and hide the Fields controls. + const direction = trimmed.match(/^([\s\S]*)\s+(asc|desc)$/i); + const expr = direction?.[1].trim() ?? ''; + if ( + direction && + expr.startsWith('(') && + findBalancedParens(expr, 0) === expr.length - 1 + ) { + const ids = splitAndTrimWithBracket(expr.slice(1, -1)); + if (ids.length > 1 && ids.every(id => id.length > 0)) { + const desc = direction[2].toUpperCase() === 'DESC'; + return ids.map(id => ({ id, desc })); + } + } + + const keys: SortKey[] = []; + for (const part of splitAndTrimWithBracket(trimmed)) { + const parsed = parseAsSortingStateString.parse(part); + if (parsed == null || parsed.id.length === 0) return null; + keys.push({ id: parsed.id, desc: parsed.desc }); + } + return keys; +} + +export function serializeSortKeys(keys: SortKey[]) { + return keys + .filter(key => key.id.trim()) + .map(key => + parseAsSortingStateString.serialize({ + id: key.id.trim(), + desc: key.desc, + }), + ) + .join(', '); +} + +export function sameKeys(a: SortKey[], b: SortKey[]) { + return ( + a.length === b.length && + a.every((key, i) => key.id === b[i].id && key.desc === b[i].desc) + ); +} + +export function sortSummary(orderBy: string): { + summary: string; + count: number; +} { + const keys = parseSortKeys(orderBy); + if (keys == null) { + const summary = orderBy.trim() || 'none'; + // An expression the field list cannot split stays one value. A number + // would invent a field count the user cannot see in the list. + return { summary, count: summary === 'none' ? 0 : 1 }; + } + if (keys.length === 0) return { summary: 'none', count: 0 }; + if (keys.length === 1) { + return { + summary: `${keys[0].id} ${keys[0].desc ? 'desc' : 'asc'}`, + count: 1, + }; + } + return { summary: `${keys.length} fields`, count: keys.length }; +} diff --git a/packages/app/src/components/SearchResultsToolbar/useClauseFieldOptions.ts b/packages/app/src/components/SearchResultsToolbar/useClauseFieldOptions.ts new file mode 100644 index 0000000000..292844ba7f --- /dev/null +++ b/packages/app/src/components/SearchResultsToolbar/useClauseFieldOptions.ts @@ -0,0 +1,47 @@ +import { useMemo } from 'react'; +import { ColumnMeta } from '@hyperdx/common-utils/dist/clickhouse'; +import { TableConnection } from '@hyperdx/common-utils/dist/core/metadata'; + +import { + getSqlSourceColumnKeys, + getSqlSourceKeys, +} from '@/components/AdhocDashboardFilter/useAdhocFilterOptions'; +import { toQuotedClickHouseKeyExpression } from '@/components/DBSearchPageFilters/utils'; +import { useAllFields } from '@/hooks/useMetadata'; + +/** The source's fields as SQL expressions, for the Pick tabs. */ +export function useClauseFieldOptions({ + tableConnection, + columns, + dateRange, + timestampValueExpression, +}: { + tableConnection: TableConnection | undefined; + columns: ColumnMeta[] | undefined; + dateRange?: [Date, Date]; + timestampValueExpression?: string; +}): string[] { + const { data: fields, isFieldMetadataDisabled } = useAllFields( + tableConnection, + { + dateRange, + timestampValueExpression, + }, + ); + + return useMemo(() => { + if (columns == null) return []; + const knownColumns = new Set(columns.map(column => column.name)); + // Field discovery is off for some teams, and `useAllFields` then returns + // [] rather than "still loading". Scalar columns are still known. + const keys = + isFieldMetadataDisabled || fields == null + ? getSqlSourceColumnKeys(columns) + : getSqlSourceKeys(fields, columns); + return Array.from( + new Set( + keys.map(key => toQuotedClickHouseKeyExpression(key, knownColumns)), + ), + ).sort(); + }, [fields, columns, isFieldMetadataDisabled]); +} diff --git a/packages/app/src/utils.ts b/packages/app/src/utils.ts index b8e0e91ea3..1eb769dbf3 100644 --- a/packages/app/src/utils.ts +++ b/packages/app/src/utils.ts @@ -8,6 +8,11 @@ import { TableConnection, unquoteIdentifier, } from '@hyperdx/common-utils/dist/core/metadata'; +import { + replaceBacktickedIdentifiers, + restoreReplacements, + splitAndTrimWithBracket, +} from '@hyperdx/common-utils/dist/core/utils'; import { CATEGORICAL_PALETTE_TOKENS, ChartPaletteToken, @@ -1289,11 +1294,24 @@ export const withMapKeyAlias = ( return `${column} AS "${key}"`; }; +/** + * Split a SELECT or ORDER BY list. A comma inside a backtick-quoted name is + * not a separator. Those names are tokenized first so a later join cannot + * insert a space inside one. The shared splitter stays unaware of backticks. + */ +export function splitClauseList(input: string): string[] { + const { sqlWithReplacements, replacements } = + replaceBacktickedIdentifiers(input); + return splitAndTrimWithBracket(sqlWithReplacements).map(part => + restoreReplacements(part, replacements.quotedText), + ); +} + /** * The sort to keep once a SELECT item is removed. Sorting on an aliased column * from the table header orders by the alias itself (`"service.name" DESC`), - * which stops resolving once the item is gone, so that falls back to the - * default sort. + * which stops resolving once the item is gone. Each top-level key that sorts + * by that alias is dropped. The default sort is used only when none remain. */ export const orderByAfterRemovingSelectItem = ( item: string, @@ -1304,8 +1322,15 @@ export const orderByAfterRemovingSelectItem = ( if (alias == null) { return orderBy; } - const sortedBy = orderBy.trim().replace(/\s+(ASC|DESC)$/i, ''); - return sortedBy === `"${alias}"` ? defaultOrderBy : orderBy; + const quoted = `"${alias}"`; + const keys = splitClauseList(orderBy); + const kept = keys.filter( + key => key.replace(/\s+(ASC|DESC)$/i, '') !== quoted, + ); + if (kept.length === keys.length) { + return orderBy; + } + return kept.length === 0 ? defaultOrderBy : kept.join(', '); }; const _useTry = (fn: () => T): [null | Error | unknown, null | T] => { From 804ed04c53d95a1f188291dec4d099139aae52c0 Mon Sep 17 00:00:00 2001 From: Elizabet Oliveira Date: Wed, 21 Oct 2026 17:10:38 +0100 Subject: [PATCH 2/4] fix(app): keep an empty column or sort from restoring the default Apply stays off with nothing selected. A map key already in the list cannot be added again, removing a duplicated column drops one row, and a comma inside a backtick-quoted sort name stays one field. Co-authored-by: Cursor --- .../SearchResultsToolbar/ClausePopover.tsx | 5 +++ .../SearchResultsToolbar/ColumnsPopover.tsx | 15 ++++---- .../SearchResultsToolbar/SortPopover.tsx | 1 + .../__tests__/ColumnsPopover.test.tsx | 36 ++++++++++++++++++- .../__tests__/SortPopover.test.tsx | 19 ++++++++-- .../SearchResultsToolbar/sortClause.ts | 26 ++------------ 6 files changed, 68 insertions(+), 34 deletions(-) diff --git a/packages/app/src/components/SearchResultsToolbar/ClausePopover.tsx b/packages/app/src/components/SearchResultsToolbar/ClausePopover.tsx index 1613d23dd0..6d751096cc 100644 --- a/packages/app/src/components/SearchResultsToolbar/ClausePopover.tsx +++ b/packages/app/src/components/SearchResultsToolbar/ClausePopover.tsx @@ -25,6 +25,7 @@ export function ClausePopover({ onApply, onReset, resetDisabled, + applyDisabled, renderPick, renderSql, 'data-testid': testId, @@ -47,6 +48,8 @@ export function ClausePopover({ /** Restore the source default in the draft. Apply still has to confirm it. */ onReset: () => void; resetDisabled?: boolean; + /** An empty clause is stored as the source default, so Apply cannot submit one. */ + applyDisabled?: boolean; renderPick: (apply: () => void) => ReactNode; renderSql: (apply: () => void) => ReactNode; 'data-testid': string; @@ -61,6 +64,7 @@ export function ClausePopover({ }; const apply = () => { + if (applyDisabled) return; onApply(); setOpened(false); }; @@ -118,6 +122,7 @@ export function ClausePopover({ variant="primary" size="xs" onClick={apply} + disabled={applyDisabled} data-testid={`${testId}-apply`} > Apply diff --git a/packages/app/src/components/SearchResultsToolbar/ColumnsPopover.tsx b/packages/app/src/components/SearchResultsToolbar/ColumnsPopover.tsx index 375a0e5dcf..1877c7e42b 100644 --- a/packages/app/src/components/SearchResultsToolbar/ColumnsPopover.tsx +++ b/packages/app/src/components/SearchResultsToolbar/ColumnsPopover.tsx @@ -25,7 +25,7 @@ import { import { fieldListSummary } from '@/components/SplitValueButton/SplitValueButton'; import SQLInlineEditor from '@/components/SQLEditor/SQLInlineEditor'; -import { splitClauseList, withMapKeyAlias } from '@/utils'; +import { isColumnInSelect, splitClauseList, withMapKeyAlias } from '@/utils'; import { ClausePopover } from './ClausePopover'; import { SortableColumn } from './SortableColumn'; @@ -66,16 +66,16 @@ export function ColumnsPopover({ splitClauseList(select || defaultValue); const shown = storedColumns(value); - const items = splitClauseList(draft); + const items = useMemo(() => splitClauseList(draft), [draft]); const options = useMemo( - () => fieldOptions.filter(option => !items.includes(option)), + () => fieldOptions.filter(option => !isColumnInSelect(items, option)), [fieldOptions, items], ); const addColumn = (column: string) => { const trimmed = column.trim(); - if (trimmed && !items.includes(trimmed)) { + if (trimmed && !isColumnInSelect(items, trimmed)) { setDraft( [...items, withMapKeyAlias(trimmed, items, knownColumns)].join(', '), ); @@ -83,8 +83,8 @@ export function ColumnsPopover({ setSearch(''); }; - const removeColumn = (item: string) => { - setDraft(items.filter(i => i !== item).join(', ')); + const removeColumn = (index: number) => { + setDraft(items.filter((_, i) => i !== index).join(', ')); }; const columnIds = items.map(columnId); @@ -123,6 +123,7 @@ export function ColumnsPopover({ setSearch(''); }} resetDisabled={draft.trim() === defaultValue.trim()} + applyDisabled={draft.trim() === ''} onApply={() => { const baseline = value || defaultValue; // An empty form value means "use the source default", so resetting @@ -159,7 +160,7 @@ export function ColumnsPopover({ key={columnId(item, index)} id={columnId(item, index)} label={item} - onRemove={() => removeColumn(item)} + onRemove={() => removeColumn(index)} /> ))} diff --git a/packages/app/src/components/SearchResultsToolbar/SortPopover.tsx b/packages/app/src/components/SearchResultsToolbar/SortPopover.tsx index 0a58fa7acc..24fd08f99d 100644 --- a/packages/app/src/components/SearchResultsToolbar/SortPopover.tsx +++ b/packages/app/src/components/SearchResultsToolbar/SortPopover.tsx @@ -106,6 +106,7 @@ export function SortPopover({ setSearch(''); }} resetDisabled={draft.trim() === defaultValue.trim()} + applyDisabled={draft.trim() === ''} onApply={() => onApply( draft.trim() === defaultValue.trim() diff --git a/packages/app/src/components/SearchResultsToolbar/__tests__/ColumnsPopover.test.tsx b/packages/app/src/components/SearchResultsToolbar/__tests__/ColumnsPopover.test.tsx index 1be855de79..d0020458a1 100644 --- a/packages/app/src/components/SearchResultsToolbar/__tests__/ColumnsPopover.test.tsx +++ b/packages/app/src/components/SearchResultsToolbar/__tests__/ColumnsPopover.test.tsx @@ -64,7 +64,7 @@ describe('ColumnsPopover', () => { }); it('stays empty after the last column is removed', async () => { - renderPopover({ + const { onApply } = renderPopover({ value: 'Timestamp', defaultValue: 'Timestamp, ServiceName, Body', }); @@ -74,6 +74,9 @@ describe('ColumnsPopover', () => { expect( screen.queryByLabelText('Remove ServiceName'), ).not.toBeInTheDocument(); + expect(screen.getByTestId('search-columns-button-apply')).toBeDisabled(); + fireEvent.click(screen.getByTestId('search-columns-button-apply')); + expect(onApply).not.toHaveBeenCalled(); const input = screen.getByPlaceholderText('Search columns'); fireEvent.change(input, { target: { value: 'Body' } }); @@ -96,6 +99,37 @@ describe('ColumnsPopover', () => { expect(onApply).toHaveBeenCalledWith('Timestamp, `a,b`', ['Body']); }); + it('removes one copy when the same column is listed twice', async () => { + const { onApply } = renderPopover({ + value: 'Body, Body', + defaultValue: 'Timestamp', + }); + openPopover(); + const removes = await screen.findAllByLabelText('Remove Body'); + fireEvent.click(removes[0]); + expect(screen.getAllByLabelText('Remove Body')).toHaveLength(1); + fireEvent.click(screen.getByTestId('search-columns-button-apply')); + expect(onApply).toHaveBeenCalledWith('Body', []); + }); + + it('does not add a map key that is already selected under its alias', async () => { + const column = `LogAttributes['x'] AS "x"`; + renderPopover({ + value: column, + defaultValue: 'Timestamp', + fieldOptions: ["LogAttributes['x']", 'Body'], + knownColumns: new Set(['Body']), + }); + openPopover(); + const input = await screen.findByPlaceholderText('Search columns'); + fireEvent.change(input, { target: { value: "LogAttributes['x']" } }); + expect( + screen.queryByRole('option', { name: "LogAttributes['x']" }), + ).not.toBeInTheDocument(); + fireEvent.click(screen.getByTestId('search-columns-add-button')); + expect(screen.getAllByLabelText(`Remove ${column}`)).toHaveLength(1); + }); + it('removes a column and reports it on apply', async () => { const { onApply } = renderPopover(); openPopover(); diff --git a/packages/app/src/components/SearchResultsToolbar/__tests__/SortPopover.test.tsx b/packages/app/src/components/SearchResultsToolbar/__tests__/SortPopover.test.tsx index d1ec5d1409..dcf2cc898e 100644 --- a/packages/app/src/components/SearchResultsToolbar/__tests__/SortPopover.test.tsx +++ b/packages/app/src/components/SearchResultsToolbar/__tests__/SortPopover.test.tsx @@ -89,12 +89,25 @@ describe('SortPopover', () => { expect(screen.queryByTestId('search-sort-custom')).not.toBeInTheDocument(); }); - it('keeps a backtick-quoted field that contains a comma on SQL', async () => { + it('lists a backtick-quoted field that contains a comma', async () => { const orderBy = '(`a,b`, Timestamp) DESC'; renderPopover({ value: orderBy, defaultValue: orderBy }); openPopover(); - expect(await screen.findByLabelText('ORDER BY')).toHaveValue(orderBy); - expect(screen.queryByDisplayValue('Timestamp')).not.toBeInTheDocument(); + expect(await screen.findByDisplayValue('`a,b`')).toBeInTheDocument(); + expect(screen.getByDisplayValue('Timestamp')).toBeInTheDocument(); + expect(screen.queryByTestId('search-sort-custom')).not.toBeInTheDocument(); + }); + + it('does not apply an empty sort', async () => { + const { onApply } = renderPopover({ + value: 'Timestamp DESC', + defaultValue: 'Body DESC', + }); + openPopover(); + fireEvent.click(await screen.findByLabelText('Remove Timestamp')); + expect(screen.getByTestId('search-sort-button-apply')).toBeDisabled(); + fireEvent.click(screen.getByTestId('search-sort-button-apply')); + expect(onApply).not.toHaveBeenCalled(); }); it('opens on the SQL tab for a custom ORDER BY', async () => { diff --git a/packages/app/src/components/SearchResultsToolbar/sortClause.ts b/packages/app/src/components/SearchResultsToolbar/sortClause.ts index ba586497f0..bdf8bfeb17 100644 --- a/packages/app/src/components/SearchResultsToolbar/sortClause.ts +++ b/packages/app/src/components/SearchResultsToolbar/sortClause.ts @@ -1,6 +1,6 @@ -import { splitAndTrimWithBracket } from '@hyperdx/common-utils/dist/core/utils'; import { findBalancedParens } from '@hyperdx/common-utils/dist/variables'; +import { splitClauseList } from '@/utils'; import { parseAsSortingStateString } from '@/utils/queryParsers'; export type SortKey = { id: string; desc: boolean }; @@ -15,24 +15,6 @@ export function newSortRow(id: string, desc: boolean): SortRow { export const toRows = (keys: SortKey[] | null): SortRow[] | null => keys?.map(key => newSortRow(key.id, key.desc)) ?? null; -/** A comma inside backticks cannot be split without breaking the identifier. */ -function hasCommaInsideBackticks(input: string): boolean { - let quoted = false; - for (let i = 0; i < input.length; i++) { - if (input[i] !== '`') { - if (quoted && input[i] === ',') return true; - continue; - } - // ClickHouse escapes a backtick by doubling it. - if (quoted && input[i + 1] === '`') { - i += 1; - continue; - } - quoted = !quoted; - } - return false; -} - /** * Sort keys the picker can edit. A parenthesized list with one direction, * `(toDateTime(Timestamp), Timestamp) DESC`, is several fields sharing that @@ -43,8 +25,6 @@ function hasCommaInsideBackticks(input: string): boolean { export function parseSortKeys(orderBy: string): SortKey[] | null { const trimmed = orderBy.trim(); if (!trimmed) return []; - // A comma inside a backtick-quoted name would be split into extra fields. - if (hasCommaInsideBackticks(trimmed)) return null; // [\s\S] so a newline inside `(toDateTime(Timestamp),\n Timestamp) DESC` // still counts as one direction. `.` would miss it and hide the Fields controls. @@ -55,7 +35,7 @@ export function parseSortKeys(orderBy: string): SortKey[] | null { expr.startsWith('(') && findBalancedParens(expr, 0) === expr.length - 1 ) { - const ids = splitAndTrimWithBracket(expr.slice(1, -1)); + const ids = splitClauseList(expr.slice(1, -1)); if (ids.length > 1 && ids.every(id => id.length > 0)) { const desc = direction[2].toUpperCase() === 'DESC'; return ids.map(id => ({ id, desc })); @@ -63,7 +43,7 @@ export function parseSortKeys(orderBy: string): SortKey[] | null { } const keys: SortKey[] = []; - for (const part of splitAndTrimWithBracket(trimmed)) { + for (const part of splitClauseList(trimmed)) { const parsed = parseAsSortingStateString.parse(part); if (parsed == null || parsed.id.length === 0) return null; keys.push({ id: parsed.id, desc: parsed.desc }); From 27a1e6c2804eba59b2c4e904083ab9b1e87ff72a Mon Sep 17 00:00:00 2001 From: Elizabet Oliveira Date: Wed, 21 Oct 2026 17:27:17 +0100 Subject: [PATCH 3/4] fix(app): offer DateTime columns in the column and sort pickers The ad hoc filter helpers drop DateTime types. These pickers need Timestamp. Co-authored-by: Cursor --- .../__tests__/useClauseFieldOptions.test.tsx | 13 +++++- .../useClauseFieldOptions.ts | 46 +++++++++++++++---- 2 files changed, 50 insertions(+), 9 deletions(-) diff --git a/packages/app/src/components/SearchResultsToolbar/__tests__/useClauseFieldOptions.test.tsx b/packages/app/src/components/SearchResultsToolbar/__tests__/useClauseFieldOptions.test.tsx index c044494281..5dc5371d80 100644 --- a/packages/app/src/components/SearchResultsToolbar/__tests__/useClauseFieldOptions.test.tsx +++ b/packages/app/src/components/SearchResultsToolbar/__tests__/useClauseFieldOptions.test.tsx @@ -21,6 +21,7 @@ const column = (name: string, type: string): ColumnMeta => ({ const columns = [ column('ServiceName', 'LowCardinality(String)'), + column('Timestamp', 'DateTime64(9)'), column('service-name', 'String'), column('my-map', 'Map(String, String)'), ]; @@ -51,6 +52,11 @@ describe('useClauseFieldOptions', () => { mockAllFields( [ { path: ['ServiceName'], type: 'String', jsType: JSDataType.String }, + { + path: ['Timestamp'], + type: 'DateTime64(9)', + jsType: JSDataType.Date, + }, { path: ['service-name'], type: 'String', jsType: JSDataType.String }, { path: ['my-map', 'k'], type: 'String', jsType: null }, ], @@ -63,6 +69,7 @@ describe('useClauseFieldOptions', () => { expect(result.current).toEqual([ 'ServiceName', + 'Timestamp', "`my-map`['k']", '`service-name`', ]); @@ -75,6 +82,10 @@ describe('useClauseFieldOptions', () => { useClauseFieldOptions({ tableConnection: undefined, columns }), ); - expect(result.current).toEqual(['ServiceName', '`service-name`']); + expect(result.current).toEqual([ + 'ServiceName', + 'Timestamp', + '`service-name`', + ]); }); }); diff --git a/packages/app/src/components/SearchResultsToolbar/useClauseFieldOptions.ts b/packages/app/src/components/SearchResultsToolbar/useClauseFieldOptions.ts index 292844ba7f..26d6fd394a 100644 --- a/packages/app/src/components/SearchResultsToolbar/useClauseFieldOptions.ts +++ b/packages/app/src/components/SearchResultsToolbar/useClauseFieldOptions.ts @@ -1,13 +1,43 @@ import { useMemo } from 'react'; -import { ColumnMeta } from '@hyperdx/common-utils/dist/clickhouse'; -import { TableConnection } from '@hyperdx/common-utils/dist/core/metadata'; - import { - getSqlSourceColumnKeys, - getSqlSourceKeys, -} from '@/components/AdhocDashboardFilter/useAdhocFilterOptions'; + ColumnMeta, + convertCHDataTypeToJSType, + filterColumnMetaByType, + isJSDataTypeScalar, + JSDataType, +} from '@hyperdx/common-utils/dist/clickhouse'; +import { + Field, + TableConnection, +} from '@hyperdx/common-utils/dist/core/metadata'; + import { toQuotedClickHouseKeyExpression } from '@/components/DBSearchPageFilters/utils'; import { useAllFields } from '@/hooks/useMetadata'; +import { mergePath } from '@/utils'; + +function namesOfType(columns: ColumnMeta[], types: JSDataType[]) { + return ( + filterColumnMetaByType(columns, types)?.map(column => column.name) ?? [] + ); +} + +// Ad hoc filters drop DateTime because their comparisons are stringly. A +// column or sort field is a SQL expression, so Timestamp stays selectable. +function clauseFieldKeys(fields: Field[], columns: ColumnMeta[]) { + const jsonColumns = namesOfType(columns, [JSDataType.JSON]); + const mapColumns = namesOfType(columns, [JSDataType.Map]); + return fields + .filter(field => field.path.length > 1 || isJSDataTypeScalar(field.jsType)) + .map(field => mergePath(field.path, jsonColumns, mapColumns)); +} + +function clauseColumnKeys(columns: ColumnMeta[]) { + return columns + .filter(column => + isJSDataTypeScalar(convertCHDataTypeToJSType(column.type)), + ) + .map(column => column.name); +} /** The source's fields as SQL expressions, for the Pick tabs. */ export function useClauseFieldOptions({ @@ -36,8 +66,8 @@ export function useClauseFieldOptions({ // [] rather than "still loading". Scalar columns are still known. const keys = isFieldMetadataDisabled || fields == null - ? getSqlSourceColumnKeys(columns) - : getSqlSourceKeys(fields, columns); + ? clauseColumnKeys(columns) + : clauseFieldKeys(fields, columns); return Array.from( new Set( keys.map(key => toQuotedClickHouseKeyExpression(key, knownColumns)), From 4103e15aa58151f9f2a241daae29129b125608e5 Mon Sep 17 00:00:00 2001 From: Elizabet Oliveira Date: Fri, 23 Oct 2026 11:53:42 +0100 Subject: [PATCH 4/4] fix(app): share the clause apply rule and the field key list Columns and Sort store a default the same way. The pickers reuse the ad hoc key helpers and keep DateTime by passing their own type check. Co-authored-by: Cursor --- .../__tests__/useAdhocFilterOptions.test.tsx | 16 ++++++ .../useAdhocFilterOptions.ts | 17 ++++-- .../SearchResultsToolbar/ClausePopover.tsx | 23 ++++++++ .../SearchResultsToolbar/ColumnsPopover.tsx | 21 ++++---- .../SearchResultsToolbar/SortPopover.tsx | 21 ++++---- .../useClauseFieldOptions.ts | 54 ++++--------------- 6 files changed, 80 insertions(+), 72 deletions(-) diff --git a/packages/app/src/components/AdhocDashboardFilter/__tests__/useAdhocFilterOptions.test.tsx b/packages/app/src/components/AdhocDashboardFilter/__tests__/useAdhocFilterOptions.test.tsx index e652ed61b4..8f31610946 100644 --- a/packages/app/src/components/AdhocDashboardFilter/__tests__/useAdhocFilterOptions.test.tsx +++ b/packages/app/src/components/AdhocDashboardFilter/__tests__/useAdhocFilterOptions.test.tsx @@ -96,6 +96,22 @@ describe('getSqlSourceKeys', () => { ), ).toEqual(['Day']); }); + + it('keeps DateTime keys when the caller asks for every type', () => { + expect( + getSqlSourceKeys( + [ + { + path: ['Timestamp'], + type: 'DateTime64(9)', + jsType: JSDataType.Date, + }, + ], + [column('Timestamp', 'DateTime64(9)')], + () => true, + ), + ).toEqual(['Timestamp']); + }); }); describe('getSqlSourceColumnKeys', () => { diff --git a/packages/app/src/components/AdhocDashboardFilter/useAdhocFilterOptions.ts b/packages/app/src/components/AdhocDashboardFilter/useAdhocFilterOptions.ts index 06a73ada02..55c2af2db3 100644 --- a/packages/app/src/components/AdhocDashboardFilter/useAdhocFilterOptions.ts +++ b/packages/app/src/components/AdhocDashboardFilter/useAdhocFilterOptions.ts @@ -59,8 +59,14 @@ const toQuotedKeys = (keys: string[], columns: ColumnMeta[]) => { /** * A SQL source's filterable keys, as SQL expressions in the search sidebar's * key format: scalar columns, and the keys inside its map and JSON columns. + * `keepType` defaults to the ad hoc rule, which drops DateTime. Column and + * sort pickers pass a predicate that keeps every type. */ -export function getSqlSourceKeys(fields: Field[], columns: ColumnMeta[]) { +export function getSqlSourceKeys( + fields: Field[], + columns: ColumnMeta[], + keepType: (type: string) => boolean = isAdhocComparableDataType, +) { const columnNames = (types: JSDataType[]) => filterColumnMetaByType(columns, types)?.map(column => column.name) ?? []; const jsonColumns = columnNames([JSDataType.JSON]); @@ -69,19 +75,22 @@ export function getSqlSourceKeys(fields: Field[], columns: ColumnMeta[]) { .filter( field => (field.path.length > 1 || isJSDataTypeScalar(field.jsType)) && - isAdhocComparableDataType(field.type), + keepType(field.type), ) .map(field => mergePath(field.path, jsonColumns, mapColumns)); return toQuotedKeys(keys, columns); } /** A SQL source's scalar columns, the only keys known without field metadata. */ -export function getSqlSourceColumnKeys(columns: ColumnMeta[]) { +export function getSqlSourceColumnKeys( + columns: ColumnMeta[], + keepType: (type: string) => boolean = isAdhocComparableDataType, +) { const keys = columns .filter( column => isJSDataTypeScalar(convertCHDataTypeToJSType(column.type)) && - isAdhocComparableDataType(column.type), + keepType(column.type), ) .map(column => column.name); return toQuotedKeys(keys, columns); diff --git a/packages/app/src/components/SearchResultsToolbar/ClausePopover.tsx b/packages/app/src/components/SearchResultsToolbar/ClausePopover.tsx index 6d751096cc..0c7450e259 100644 --- a/packages/app/src/components/SearchResultsToolbar/ClausePopover.tsx +++ b/packages/app/src/components/SearchResultsToolbar/ClausePopover.tsx @@ -5,6 +5,29 @@ import { SplitValueButton } from '@/components/SplitValueButton/SplitValueButton export type ClauseMode = 'pick' | 'sql'; +const matchesDefault = (draft: string, defaultValue: string) => + draft.trim() === defaultValue.trim(); + +/** + * What Apply writes. An empty form value means "use the source default", so a + * draft that matches the default is stored as "" rather than a copy of it. + */ +export function storedClause( + draft: string, + value: string, + defaultValue: string, +) { + return matchesDefault(draft, defaultValue) + ? '' + : draft === (value || defaultValue) + ? value + : draft; +} + +export const clauseResetDisabled = matchesDefault; + +export const clauseApplyDisabled = (draft: string) => draft.trim() === ''; + const MODE_OPTIONS = [ { label: 'Fields', value: 'pick' }, { label: 'SQL', value: 'sql' }, diff --git a/packages/app/src/components/SearchResultsToolbar/ColumnsPopover.tsx b/packages/app/src/components/SearchResultsToolbar/ColumnsPopover.tsx index 1877c7e42b..0042e1aee3 100644 --- a/packages/app/src/components/SearchResultsToolbar/ColumnsPopover.tsx +++ b/packages/app/src/components/SearchResultsToolbar/ColumnsPopover.tsx @@ -27,7 +27,12 @@ import { fieldListSummary } from '@/components/SplitValueButton/SplitValueButton import SQLInlineEditor from '@/components/SQLEditor/SQLInlineEditor'; import { isColumnInSelect, splitClauseList, withMapKeyAlias } from '@/utils'; -import { ClausePopover } from './ClausePopover'; +import { + clauseApplyDisabled, + ClausePopover, + clauseResetDisabled, + storedClause, +} from './ClausePopover'; import { SortableColumn } from './SortableColumn'; const columnId = (item: string, index: number) => `${index}:${item}`; @@ -122,18 +127,10 @@ export function ColumnsPopover({ setDraft(defaultValue); setSearch(''); }} - resetDisabled={draft.trim() === defaultValue.trim()} - applyDisabled={draft.trim() === ''} + resetDisabled={clauseResetDisabled(draft, defaultValue)} + applyDisabled={clauseApplyDisabled(draft)} onApply={() => { - const baseline = value || defaultValue; - // An empty form value means "use the source default", so resetting - // stores "" instead of a copy of that default. - const submitted = - draft.trim() === defaultValue.trim() - ? '' - : draft === baseline - ? value - : draft; + const submitted = storedClause(draft, value, defaultValue); const next = storedColumns(submitted); onApply( submitted, diff --git a/packages/app/src/components/SearchResultsToolbar/SortPopover.tsx b/packages/app/src/components/SearchResultsToolbar/SortPopover.tsx index 24fd08f99d..6713e64be7 100644 --- a/packages/app/src/components/SearchResultsToolbar/SortPopover.tsx +++ b/packages/app/src/components/SearchResultsToolbar/SortPopover.tsx @@ -13,7 +13,12 @@ import { IconX } from '@tabler/icons-react'; import SQLInlineEditor from '@/components/SQLEditor/SQLInlineEditor'; -import { ClausePopover } from './ClausePopover'; +import { + clauseApplyDisabled, + ClausePopover, + clauseResetDisabled, + storedClause, +} from './ClausePopover'; import { newSortRow, parseSortKeys, @@ -105,17 +110,9 @@ export function SortPopover({ setKeys(toRows(parseSortKeys(defaultValue))); setSearch(''); }} - resetDisabled={draft.trim() === defaultValue.trim()} - applyDisabled={draft.trim() === ''} - onApply={() => - onApply( - draft.trim() === defaultValue.trim() - ? '' - : draft === baseline - ? value - : draft, - ) - } + resetDisabled={clauseResetDisabled(draft, defaultValue)} + applyDisabled={clauseApplyDisabled(draft)} + onApply={() => onApply(storedClause(draft, value, defaultValue))} data-testid="search-sort-button" renderPick={() => ( diff --git a/packages/app/src/components/SearchResultsToolbar/useClauseFieldOptions.ts b/packages/app/src/components/SearchResultsToolbar/useClauseFieldOptions.ts index 26d6fd394a..aff633466d 100644 --- a/packages/app/src/components/SearchResultsToolbar/useClauseFieldOptions.ts +++ b/packages/app/src/components/SearchResultsToolbar/useClauseFieldOptions.ts @@ -1,43 +1,14 @@ import { useMemo } from 'react'; -import { - ColumnMeta, - convertCHDataTypeToJSType, - filterColumnMetaByType, - isJSDataTypeScalar, - JSDataType, -} from '@hyperdx/common-utils/dist/clickhouse'; -import { - Field, - TableConnection, -} from '@hyperdx/common-utils/dist/core/metadata'; +import { ColumnMeta } from '@hyperdx/common-utils/dist/clickhouse'; +import { TableConnection } from '@hyperdx/common-utils/dist/core/metadata'; -import { toQuotedClickHouseKeyExpression } from '@/components/DBSearchPageFilters/utils'; +import { + getSqlSourceColumnKeys, + getSqlSourceKeys, +} from '@/components/AdhocDashboardFilter/useAdhocFilterOptions'; import { useAllFields } from '@/hooks/useMetadata'; -import { mergePath } from '@/utils'; -function namesOfType(columns: ColumnMeta[], types: JSDataType[]) { - return ( - filterColumnMetaByType(columns, types)?.map(column => column.name) ?? [] - ); -} - -// Ad hoc filters drop DateTime because their comparisons are stringly. A -// column or sort field is a SQL expression, so Timestamp stays selectable. -function clauseFieldKeys(fields: Field[], columns: ColumnMeta[]) { - const jsonColumns = namesOfType(columns, [JSDataType.JSON]); - const mapColumns = namesOfType(columns, [JSDataType.Map]); - return fields - .filter(field => field.path.length > 1 || isJSDataTypeScalar(field.jsType)) - .map(field => mergePath(field.path, jsonColumns, mapColumns)); -} - -function clauseColumnKeys(columns: ColumnMeta[]) { - return columns - .filter(column => - isJSDataTypeScalar(convertCHDataTypeToJSType(column.type)), - ) - .map(column => column.name); -} +const keepEveryType = () => true; /** The source's fields as SQL expressions, for the Pick tabs. */ export function useClauseFieldOptions({ @@ -61,17 +32,12 @@ export function useClauseFieldOptions({ return useMemo(() => { if (columns == null) return []; - const knownColumns = new Set(columns.map(column => column.name)); // Field discovery is off for some teams, and `useAllFields` then returns // [] rather than "still loading". Scalar columns are still known. const keys = isFieldMetadataDisabled || fields == null - ? clauseColumnKeys(columns) - : clauseFieldKeys(fields, columns); - return Array.from( - new Set( - keys.map(key => toQuotedClickHouseKeyExpression(key, knownColumns)), - ), - ).sort(); + ? getSqlSourceColumnKeys(columns, keepEveryType) + : getSqlSourceKeys(fields, columns, keepEveryType); + return Array.from(new Set(keys)).sort(); }, [fields, columns, isFieldMetadataDisabled]); }