Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/column-sort-editors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hyperdx/app': patch
---

When a removed column is one field in a multi-field sort, drop only that field
38 changes: 38 additions & 0 deletions packages/app/src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
parseTimestampToMs,
resolveConditionalColor,
sortingStateToOrderByString,
splitClauseList,
stripTrailingSlash,
useQueryHistory,
withMapKeyAlias,
Expand Down Expand Up @@ -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"`;

Expand Down Expand Up @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand All @@ -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);
Expand Down
159 changes: 159 additions & 0 deletions packages/app/src/components/SearchResultsToolbar/ClausePopover.tsx
Original file line number Diff line number Diff line change
@@ -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<ClauseMode>(initialMode);

const open = () => {
onOpen();
setMode(initialMode);
setOpened(true);
};

const apply = () => {
if (applyDisabled) return;
onApply();
setOpened(false);
};

return (
<Popover
opened={opened}
onChange={setOpened}
position="bottom-start"
width={440}
shadow="md"
withinPortal
>
<Popover.Target>
<SplitValueButton
name={name}
summary={summary}
count={count}
dimmed={dimmed}
title={title}
onClick={() => (opened ? setOpened(false) : open())}
data-testid={testId}
/>
</Popover.Target>
<Popover.Dropdown data-testid={`${testId}-popover`}>
<Stack gap="sm">
<SegmentedControl
size="xs"
fullWidth
value={mode}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 minor — ClausePopover is a second copy of the Group by popover shell

SearchGroupBySelect.tsx:109-189 already has the same shell: a split button target, a Fields/SQL SegmentedControl, the draft reset on open, and Reset to default / Apply buttons. Now that ClausePopover exists, the two will drift; they already differ in Cancel vs Clear and in the subtle vs link variant for Reset. Move SearchGroupBySelect onto ClausePopover so all three toolbar clauses share one shell.

Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond

onChange={value => setMode(value === 'sql' ? 'sql' : 'pick')}
data={MODE_OPTIONS}
data-testid={`${testId}-mode`}
/>
{mode === 'pick' ? renderPick(apply) : renderSql(apply)}
<Group justify="space-between" gap="xs" wrap="nowrap">
<Button
variant="link"
size="xs"
onClick={onReset}
disabled={resetDisabled}
data-testid={`${testId}-reset`}
>
Reset to default
</Button>
<Group gap="xs" wrap="nowrap">
<Button
variant="secondary"
size="xs"
onClick={() => setOpened(false)}
>
Cancel
</Button>
<Button
variant="primary"
size="xs"
onClick={apply}
disabled={applyDisabled}
data-testid={`${testId}-apply`}
>
Apply
</Button>
</Group>
</Group>
</Stack>
</Popover.Dropdown>
</Popover>
);
}
Loading
Loading