diff --git a/.changeset/backtick-column-split.md b/.changeset/backtick-column-split.md new file mode 100644 index 0000000000..f25d12e532 --- /dev/null +++ b/.changeset/backtick-column-split.md @@ -0,0 +1,6 @@ +--- +'@hyperdx/common-utils': patch +'@hyperdx/app': patch +--- + +fix: keep backtick-quoted column names that contain a comma intact when toggling search columns or editing group by diff --git a/packages/app/src/__tests__/utils.test.ts b/packages/app/src/__tests__/utils.test.ts index 323a17fbd7..bdfdfc67b1 100644 --- a/packages/app/src/__tests__/utils.test.ts +++ b/packages/app/src/__tests__/utils.test.ts @@ -30,7 +30,6 @@ import { parseTimestampToMs, resolveConditionalColor, sortingStateToOrderByString, - splitClauseList, stripTrailingSlash, useQueryHistory, withMapKeyAlias, @@ -1537,16 +1536,6 @@ 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"`; diff --git a/packages/app/src/components/SearchResultsToolbar/ColumnsPopover.tsx b/packages/app/src/components/SearchResultsToolbar/ColumnsPopover.tsx index 0042e1aee3..2e69e5afb9 100644 --- a/packages/app/src/components/SearchResultsToolbar/ColumnsPopover.tsx +++ b/packages/app/src/components/SearchResultsToolbar/ColumnsPopover.tsx @@ -13,6 +13,7 @@ import { verticalListSortingStrategy, } from '@dnd-kit/sortable'; import { TableConnection } from '@hyperdx/common-utils/dist/core/metadata'; +import { splitAndTrimWithBracket } from '@hyperdx/common-utils/dist/core/utils'; import { Autocomplete, Box, @@ -25,7 +26,7 @@ import { import { fieldListSummary } from '@/components/SplitValueButton/SplitValueButton'; import SQLInlineEditor from '@/components/SQLEditor/SQLInlineEditor'; -import { isColumnInSelect, splitClauseList, withMapKeyAlias } from '@/utils'; +import { isColumnInSelect, withMapKeyAlias } from '@/utils'; import { clauseApplyDisabled, @@ -68,10 +69,10 @@ export function ColumnsPopover({ // 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); + splitAndTrimWithBracket(select || defaultValue); const shown = storedColumns(value); - const items = useMemo(() => splitClauseList(draft), [draft]); + const items = useMemo(() => splitAndTrimWithBracket(draft), [draft]); const options = useMemo( () => fieldOptions.filter(option => !isColumnInSelect(items, option)), diff --git a/packages/app/src/components/SearchResultsToolbar/sortClause.ts b/packages/app/src/components/SearchResultsToolbar/sortClause.ts index bdf8bfeb17..859a93f22d 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 }; @@ -35,7 +35,7 @@ export function parseSortKeys(orderBy: string): SortKey[] | null { expr.startsWith('(') && findBalancedParens(expr, 0) === expr.length - 1 ) { - const ids = splitClauseList(expr.slice(1, -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 })); @@ -43,7 +43,7 @@ export function parseSortKeys(orderBy: string): SortKey[] | null { } const keys: SortKey[] = []; - for (const part of splitClauseList(trimmed)) { + 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 }); diff --git a/packages/app/src/utils.ts b/packages/app/src/utils.ts index 9bb41cfba5..d438f4fb1c 100644 --- a/packages/app/src/utils.ts +++ b/packages/app/src/utils.ts @@ -8,11 +8,7 @@ import { TableConnection, unquoteIdentifier, } from '@hyperdx/common-utils/dist/core/metadata'; -import { - replaceBacktickedIdentifiers, - restoreReplacements, - splitAndTrimWithBracket, -} from '@hyperdx/common-utils/dist/core/utils'; +import { splitAndTrimWithBracket } from '@hyperdx/common-utils/dist/core/utils'; import { CATEGORICAL_PALETTE_TOKENS, ChartPaletteToken, @@ -1326,19 +1322,6 @@ 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`), @@ -1355,7 +1338,7 @@ export const orderByAfterRemovingSelectItem = ( return orderBy; } const quoted = `"${alias}"`; - const keys = splitClauseList(orderBy); + const keys = splitAndTrimWithBracket(orderBy); const kept = keys.filter( key => key.replace(/\s+(ASC|DESC)$/i, '') !== quoted, ); diff --git a/packages/common-utils/src/__tests__/utils.test.ts b/packages/common-utils/src/__tests__/utils.test.ts index ee0ae6eae0..327585152b 100644 --- a/packages/common-utils/src/__tests__/utils.test.ts +++ b/packages/common-utils/src/__tests__/utils.test.ts @@ -292,6 +292,27 @@ describe('utils', () => { expect(splitAndTrimWithBracket(input)).toEqual(expected); }); + it('should keep commas and brackets inside backtick-quoted identifiers', () => { + const input = "Timestamp, `a,b`, `x(y`, `it's` AS label, Body"; + const expected = [ + 'Timestamp', + '`a,b`', + '`x(y`', + "`it's` AS label", + 'Body', + ]; + expect(splitAndTrimWithBracket(input)).toEqual(expected); + }); + + it('should keep the text after an unclosed quote', () => { + expect(splitAndTrimWithBracket('Timestamp, `a')).toEqual([ + 'Timestamp', + '`a', + ]); + expect(splitAndTrimWithBracket('a`b, c')).toEqual(['a`b, c']); + expect(splitAndTrimWithBracket("x, 'y, z")).toEqual(['x', "'y, z"]); + }); + it('should handle mixed quotes with commas', () => { const input = `col1, "double, quoted", col2, 'single, quoted', col3`; const expected = [ diff --git a/packages/common-utils/src/core/utils.ts b/packages/common-utils/src/core/utils.ts index 0042b9da81..df92f006a1 100644 --- a/packages/common-utils/src/core/utils.ts +++ b/packages/common-utils/src/core/utils.ts @@ -134,12 +134,29 @@ export function splitAndTrimWithBracket(input: string): string[] { let squareCount: number = 0; let inSingleQuote: boolean = false; let inDoubleQuote: boolean = false; + let inBacktick: boolean = false; const res: string[] = []; let cur: string = ''; for (let i = 0; i <= input.length; i++) { const c = i === input.length ? ',' : input[i]; + if ( + c === '`' && + !inSingleQuote && + !inDoubleQuote && + !isQuoteEscapedByBackslash(input, i) + ) { + inBacktick = !inBacktick; + cur += c; + continue; + } + + if (inBacktick) { + cur += c; + continue; + } + if (c === '"' && !inSingleQuote && !isQuoteEscapedByBackslash(input, i)) { inDoubleQuote = !inDoubleQuote; cur += c; @@ -178,6 +195,12 @@ export function splitAndTrimWithBracket(input: string): string[] { cur += c; } } + // An unclosed quote swallows the closing ','; keep its text so a half-typed + // name survives. Unclosed brackets still drop it, as parseToStartOfFunction expects. + if (inBacktick || inSingleQuote || inDoubleQuote) { + const rest = cur.slice(0, -1).trim(); + if (rest) res.push(rest); + } return res; }