Skip to content

Commit 7d1b838

Browse files
committed
fix: keep JSON path segments with special characters quoted in row detail lookups
1 parent 8384fe6 commit 7d1b838

6 files changed

Lines changed: 212 additions & 6 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@hyperdx/common-utils': patch
3+
---
4+
5+
fix: keep JSON path segments with special characters quoted in row detail lookups

‎packages/app/tests/e2e/components/SidePanelComponent.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,27 @@ export class SidePanelComponent {
329329
.click({ timeout: this.defaultTimeout });
330330
}
331331

332+
/**
333+
* The "Column Values" (parsed) tab's content panel, which renders the full
334+
* fetched row via the JSON viewer.
335+
*/
336+
get columnValuesContent() {
337+
return this.panelContainer.getByTestId('side-panel-tab-parsed');
338+
}
339+
340+
/** Open the "Column Values" (parsed) tab. */
341+
async openColumnValues() {
342+
await this.clickTab('parsed');
343+
}
344+
345+
/**
346+
* A line in the "Column Values" tab matching `text` (substring). Used to
347+
* assert a fetched row's value is shown.
348+
*/
349+
columnValue(text: string) {
350+
return this.columnValuesContent.getByText(text, { exact: false });
351+
}
352+
332353
/**
333354
* Close the side panel (if it has a close button)
334355
*/
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* Selecting an at-prefixed JSON key breaks row details.
3+
*
4+
* When a displayed SELECT column is a JSON subcolumn whose key is not a bare
5+
* identifier (e.g. `ResourceAttributesJSON.`@request_id``), ClickHouse reports
6+
* the result column name without backticks (`ResourceAttributesJSON.@request_id`).
7+
* The row side panel rebuilds a WHERE clause from the displayed columns to fetch
8+
* the full row; before the fix it used that bare name verbatim, producing
9+
* invalid SQL that matched no rows, so the panel came up empty.
10+
*
11+
* This drives the real UI path against a JSON-column schema: select the
12+
* at-prefixed key, open the row, and confirm the detail panel is populated with
13+
* the selected event's values.
14+
*/
15+
import { SearchPage } from '../../page-objects/SearchPage';
16+
import {
17+
INTERESTING_FILTER_KEYS_JSON_AT_KEY,
18+
INTERESTING_FILTER_KEYS_ROWS,
19+
} from '../../seed-clickhouse';
20+
import { expect, test } from '../../utils/base-test';
21+
import { INTERESTING_FILTER_KEYS_SOURCE_NAME } from '../../utils/constants';
22+
23+
const QUERY_TIMEOUT = 20_000;
24+
25+
const [ROW1] = INTERESTING_FILTER_KEYS_ROWS;
26+
27+
test.describe(
28+
'Search: at-prefixed JSON key row details',
29+
{ tag: ['@search', '@full-stack'] },
30+
() => {
31+
test('opening a row with an at-prefixed JSON key in SELECT shows the event', async ({
32+
page,
33+
}) => {
34+
const searchPage = new SearchPage(page);
35+
await searchPage.goto();
36+
await searchPage.selectSource(INTERESTING_FILTER_KEYS_SOURCE_NAME);
37+
await searchPage.timePicker.selectRelativeTime('Last 1 hour');
38+
39+
await test.step('isolate a single row', async () => {
40+
await searchPage.performSearch(`ServiceName:"${ROW1.serviceName}"`);
41+
await expect(searchPage.getTableError()).toHaveCount(0);
42+
await expect(searchPage.table.getRows()).toHaveCount(1, {
43+
timeout: QUERY_TIMEOUT,
44+
});
45+
});
46+
47+
await test.step('select the at-prefixed JSON key as a column', async () => {
48+
await searchPage.setCustomSELECT(
49+
`Timestamp, ServiceName, ResourceAttributesJSON.\`${INTERESTING_FILTER_KEYS_JSON_AT_KEY}\``,
50+
);
51+
await searchPage.submitEmptySearch();
52+
await expect(searchPage.getTableError()).toHaveCount(0);
53+
await expect(searchPage.table.getRows()).toHaveCount(1, {
54+
timeout: QUERY_TIMEOUT,
55+
});
56+
});
57+
58+
await test.step('row details load the selected event', async () => {
59+
await searchPage.table.clickFirstRow();
60+
await expect(searchPage.sidePanel.container).toBeVisible();
61+
// Before the fix the detail WHERE dropped the key's backticks and
62+
// matched no rows, so the panel rendered empty with no error.
63+
await expect(searchPage.sidePanel.errorState).toHaveCount(0);
64+
65+
await searchPage.sidePanel.openColumnValues();
66+
await expect(
67+
searchPage.sidePanel.columnValue(ROW1.jsonAtKeyValue),
68+
).toBeVisible({ timeout: QUERY_TIMEOUT });
69+
await expect(searchPage.sidePanel.columnValue(ROW1.body)).toBeVisible();
70+
});
71+
});
72+
},
73+
);

‎packages/app/tests/e2e/seed-clickhouse.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ export const INTERESTING_FILTER_KEYS_ROWS = [
9898
serviceName: 'service1',
9999
resourceAttrValue: 'value1',
100100
jsonValue: 'value1',
101+
// Top-level JSON key that is NOT a bare identifier (`@` prefix). Selecting
102+
// it (`ResourceAttributesJSON.`@request_id``) and opening the row must keep
103+
// the key backtick-quoted in the detail WHERE clause (HDX-5867).
104+
jsonAtKeyValue: 'request-A',
101105
clusterName: 'cluster1',
102106
serviceNameHyphen: 'svc-one',
103107
mapHyphenValue: 'mapval1',
@@ -108,6 +112,7 @@ export const INTERESTING_FILTER_KEYS_ROWS = [
108112
serviceName: 'service2',
109113
resourceAttrValue: 'value2',
110114
jsonValue: 'value2',
115+
jsonAtKeyValue: 'request-B',
111116
clusterName: 'cluster2',
112117
serviceNameHyphen: 'svc-two',
113118
mapHyphenValue: 'mapval2',
@@ -118,6 +123,7 @@ export const INTERESTING_FILTER_KEYS_ROWS = [
118123
serviceName: 'service3',
119124
resourceAttrValue: 'value3',
120125
jsonValue: 'value3',
126+
jsonAtKeyValue: 'request-C',
121127
clusterName: 'cluster3',
122128
serviceNameHyphen: 'svc-three',
123129
mapHyphenValue: 'mapval3',
@@ -126,6 +132,10 @@ export const INTERESTING_FILTER_KEYS_ROWS = [
126132
},
127133
] as const;
128134

135+
// The at-prefixed JSON key seeded into `ResourceAttributesJSON`. Exported so the
136+
// row-details spec selects and asserts against the same key/values.
137+
export const INTERESTING_FILTER_KEYS_JSON_AT_KEY = '@request_id';
138+
129139
// A single log whose Body is a JSON string with a flat, dotted key. It lives in
130140
// the default E2E Logs table (so it shares the E2E Logs source); the unique
131141
// ServiceName isolates it from other default-logs tests, which all scope by
@@ -793,7 +803,7 @@ function generateK8sEventLogs(
793803
function generateInterestingFilterKeyLogData(seedRef: number): string {
794804
return INTERESTING_FILTER_KEYS_ROWS.map((row, i) => {
795805
const timestampNs = (seedRef - (i + 1) * 1000) * 1000000;
796-
const json = `{"key": {"subKey": {"subSubKey": "${row.jsonValue}"}}}`;
806+
const json = `{"key": {"subKey": {"subSubKey": "${row.jsonValue}"}}, "${INTERESTING_FILTER_KEYS_JSON_AT_KEY}": "${row.jsonAtKeyValue}"}`;
797807
// JSON column with a hyphenated name AND hyphenated nested keys.
798808
const jsonHyphen = `{"key-1": {"key-2": "${row.jsonHyphenValue}"}}`;
799809
return (

‎packages/common-utils/src/__tests__/metadata.test.ts‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4072,6 +4072,66 @@ describe('quoteResultColumnNameIfNeeded', () => {
40724072
'`x-host-header`',
40734073
);
40744074
});
4075+
4076+
it('quotes non-identifier segments of a dotted JSON path', () => {
4077+
expect(quoteResultColumnNameIfNeeded('event.@request_id')).toBe(
4078+
'event.`@request_id`',
4079+
);
4080+
expect(quoteResultColumnNameIfNeeded('event.my key')).toBe(
4081+
'event.`my key`',
4082+
);
4083+
expect(quoteResultColumnNameIfNeeded('a.b.@c')).toBe('a.b.`@c`');
4084+
});
4085+
4086+
it('quotes the path but preserves a JSON type cast', () => {
4087+
// ClickHouse reports `event.foo.:String` as event.foo.:`String`.
4088+
expect(quoteResultColumnNameIfNeeded('event.foo.:`String`')).toBe(
4089+
'event.foo.:`String`',
4090+
);
4091+
expect(quoteResultColumnNameIfNeeded('event.@request_id.:`String`')).toBe(
4092+
'event.`@request_id`.:`String`',
4093+
);
4094+
expect(quoteResultColumnNameIfNeeded('event.@id.:`Decimal(10, 2)`')).toBe(
4095+
'event.`@id`.:`Decimal(10, 2)`',
4096+
);
4097+
expect(quoteResultColumnNameIfNeeded('event.@id.:`Int64`.null')).toBe(
4098+
'event.`@id`.:`Int64`.null',
4099+
);
4100+
});
4101+
4102+
it('quotes a key that starts with `:`', () => {
4103+
expect(quoteResultColumnNameIfNeeded('event.:k')).toBe('event.`:k`');
4104+
});
4105+
4106+
it('quotes a dotted-path segment that itself contains a colon', () => {
4107+
expect(quoteResultColumnNameIfNeeded('event.k8s:pod')).toBe(
4108+
'event.`k8s:pod`',
4109+
);
4110+
});
4111+
4112+
it('quotes an all-digit JSON key (unquoted it errors in ClickHouse)', () => {
4113+
// `event.200` unquoted is parsed as tuple-element access and rejected for a
4114+
// JSON column; backticks make it a key reference. Tuple positional access
4115+
// is reported by ClickHouse as `tupleElement(...)`, never a bare `t.1`.
4116+
expect(quoteResultColumnNameIfNeeded('event.200')).toBe('event.`200`');
4117+
expect(quoteResultColumnNameIfNeeded('tup.1')).toBe('tup.`1`');
4118+
});
4119+
4120+
it('preserves a sub-object accessor', () => {
4121+
// ClickHouse reports `event.^sub` as event.^`sub`.
4122+
expect(quoteResultColumnNameIfNeeded('event.^`sub`')).toBe('event.^`sub`');
4123+
expect(quoteResultColumnNameIfNeeded('event.@a.^`b.c`')).toBe(
4124+
'event.`@a`.^`b.c`',
4125+
);
4126+
});
4127+
4128+
it('quotes a key that starts with `^`', () => {
4129+
expect(quoteResultColumnNameIfNeeded('event.^k')).toBe('event.`^k`');
4130+
});
4131+
4132+
it('leaves a malformed path with an empty segment untouched', () => {
4133+
expect(quoteResultColumnNameIfNeeded('a..b')).toBe('a..b');
4134+
});
40754135
});
40764136

40774137
describe('parametric aggregate arguments are inlined as literals', () => {

‎packages/common-utils/src/core/metadata.ts‎

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,19 +190,56 @@ export const quoteIdentifierIfNeeded = (identifier: string): string => {
190190
: quoteJsonPathSegment(unquoted);
191191
};
192192

193+
// A single column or JSON path segment. No quoting/grouping characters, no `.` separator.
194+
const PLAIN_PATH_SEGMENT = /^[^`'"()[\],.]+/;
195+
196+
// A JSON sub-object accessor or type cast. ClickHouse reports the operand
197+
// backticked (j.^`a`, j.a.:`Int64`).
198+
const OPERATOR_PATH_SEGMENT = /^[\^:]`(?:[^`]|``)*`/;
199+
200+
// Split a name into `.`-separated path segments, or undefined when it isn't a
201+
// plain JSON path (function calls, literals, empty segments).
202+
const splitResultColumnPath = (name: string): string[] | undefined => {
203+
const segments: string[] = [];
204+
let rest = name;
205+
while (true) {
206+
const segment =
207+
rest.match(OPERATOR_PATH_SEGMENT)?.[0] ??
208+
rest.match(PLAIN_PATH_SEGMENT)?.[0];
209+
if (segment == null) return undefined;
210+
segments.push(segment);
211+
rest = rest.slice(segment.length);
212+
if (rest === '') return segments;
213+
if (!rest.startsWith('.')) return undefined;
214+
rest = rest.slice(1);
215+
}
216+
};
217+
218+
const quotePathSegment = (segment: string): string =>
219+
OPERATOR_PATH_SEGMENT.test(segment)
220+
? segment
221+
: quoteIdentifierIfNeeded(segment);
222+
193223
/**
194224
* Quote a query result column name so it can be referenced in raw SQL.
195225
*
196226
* Most result column names do not need quoting:
197227
* - Bare identifiers (alphanumeric and underscore, starting with a letter or underscore)
198228
* - Numeric literals (e.g., `1`, `0x10`)
199-
* - Dotted column names (e.g., `my.col`)
200229
* - Function calls (e.g., `plus(a, b)`, `arrayElement(m, 'key')`)
230+
*
231+
* Dotted names are JSON/nested subcolumn paths (e.g. `event.@request_id`,
232+
* which ClickHouse reports for the select `` event.`@request_id` ``). Each
233+
* segment is quoted on its own. JSON type casts (eg. `` .:`String` ``) and
234+
* `` ^`sub` `` sub-object accessors are preserved.
201235
*/
202-
export const quoteResultColumnNameIfNeeded = (name: string): string =>
203-
/^[^`'"()[\],.]+$/.test(name) && !Number.isFinite(Number(name))
204-
? quoteIdentifierIfNeeded(name)
205-
: name;
236+
export const quoteResultColumnNameIfNeeded = (name: string): string => {
237+
if (Number.isFinite(Number(name))) {
238+
return name;
239+
}
240+
const segments = splitResultColumnPath(name);
241+
return segments ? segments.map(quotePathSegment).join('.') : name;
242+
};
206243

207244
const columnAppearsInMvSelect = (sql: string, columnName: string): boolean => {
208245
const escaped = columnName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');

0 commit comments

Comments
 (0)