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 f52c61f7c0..323a17fbd7 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, @@ -1536,6 +1537,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"`; @@ -1564,6 +1575,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/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 new file mode 100644 index 0000000000..0c7450e259 --- /dev/null +++ b/packages/app/src/components/SearchResultsToolbar/ClausePopover.tsx @@ -0,0 +1,159 @@ +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 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' }, +]; + +/** + * 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, + applyDisabled, + 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; + /** 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; +}) { + const [opened, setOpened] = useState(false); + const [mode, setMode] = useState(initialMode); + + const open = () => { + onOpen(); + setMode(initialMode); + setOpened(true); + }; + + const apply = () => { + if (applyDisabled) return; + 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..0042e1aee3 --- /dev/null +++ b/packages/app/src/components/SearchResultsToolbar/ColumnsPopover.tsx @@ -0,0 +1,224 @@ +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 { isColumnInSelect, splitClauseList, withMapKeyAlias } from '@/utils'; + +import { + clauseApplyDisabled, + ClausePopover, + clauseResetDisabled, + storedClause, +} 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 = useMemo(() => splitClauseList(draft), [draft]); + + const options = useMemo( + () => fieldOptions.filter(option => !isColumnInSelect(items, option)), + [fieldOptions, items], + ); + + const addColumn = (column: string) => { + const trimmed = column.trim(); + if (trimmed && !isColumnInSelect(items, trimmed)) { + setDraft( + [...items, withMapKeyAlias(trimmed, items, knownColumns)].join(', '), + ); + } + setSearch(''); + }; + + const removeColumn = (index: number) => { + setDraft(items.filter((_, i) => i !== index).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={clauseResetDisabled(draft, defaultValue)} + applyDisabled={clauseApplyDisabled(draft)} + onApply={() => { + const submitted = storedClause(draft, value, defaultValue); + 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(index)} + /> + ))} + + + + )} + + + { + 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..6713e64be7 --- /dev/null +++ b/packages/app/src/components/SearchResultsToolbar/SortPopover.tsx @@ -0,0 +1,221 @@ +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 { + clauseApplyDisabled, + ClausePopover, + clauseResetDisabled, + storedClause, +} 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={clauseResetDisabled(draft, defaultValue)} + applyDisabled={clauseApplyDisabled(draft)} + onApply={() => onApply(storedClause(draft, value, defaultValue))} + 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..d0020458a1 --- /dev/null +++ b/packages/app/src/components/SearchResultsToolbar/__tests__/ColumnsPopover.test.tsx @@ -0,0 +1,261 @@ +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 () => { + const { onApply } = 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(); + 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' } }); + 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 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(); + 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..dcf2cc898e --- /dev/null +++ b/packages/app/src/components/SearchResultsToolbar/__tests__/SortPopover.test.tsx @@ -0,0 +1,220 @@ +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('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.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 () => { + 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..5dc5371d80 --- /dev/null +++ b/packages/app/src/components/SearchResultsToolbar/__tests__/useClauseFieldOptions.test.tsx @@ -0,0 +1,91 @@ +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('Timestamp', 'DateTime64(9)'), + 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: ['Timestamp'], + type: 'DateTime64(9)', + jsType: JSDataType.Date, + }, + { 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', + 'Timestamp', + "`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', + 'Timestamp', + '`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..bdf8bfeb17 --- /dev/null +++ b/packages/app/src/components/SearchResultsToolbar/sortClause.ts @@ -0,0 +1,92 @@ +import { findBalancedParens } from '@hyperdx/common-utils/dist/variables'; + +import { splitClauseList } from '@/utils'; +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; + +/** + * 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 []; + + // [\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 = 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 })); + } + } + + const keys: SortKey[] = []; + 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 }); + } + 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..aff633466d --- /dev/null +++ b/packages/app/src/components/SearchResultsToolbar/useClauseFieldOptions.ts @@ -0,0 +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'; +import { useAllFields } from '@/hooks/useMetadata'; + +const keepEveryType = () => true; + +/** 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 []; + // 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, keepEveryType) + : getSqlSourceKeys(fields, columns, keepEveryType); + return Array.from(new Set(keys)).sort(); + }, [fields, columns, isFieldMetadataDisabled]); +} diff --git a/packages/app/src/utils.ts b/packages/app/src/utils.ts index a80e9495c3..9bb41cfba5 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, @@ -1321,11 +1326,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, @@ -1336,8 +1354,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] => {