Skip to content
Draft
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
9 changes: 9 additions & 0 deletions .changeset/null-literal-select-alias.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@hyperdx/common-utils': patch
---

Fix `NULL AS "alias"` projections being dropped from the SELECT alias map. A
column projected as a NULL literal was omitted, so anything resolving a value
back to its source expression (for example building a WHERE clause that
identifies a specific row) treated the alias as a real table column and
produced SQL referencing a column that does not exist.
16 changes: 16 additions & 0 deletions packages/common-utils/src/__tests__/clickhouse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,22 @@ describe('chSqlToAliasMap - alias unit test', () => {
expect(res).toEqual(aliasMap);
});

it('NULL literal alias (multi-source padding column)', () => {
const chSqlInput: ChSql = {
sql: 'SELECT Timestamp as "__hdx_timestamp", NULL as "__hdx_duration_ms" FROM {HYPERDX_PARAM_1544803905:Identifier}.{HYPERDX_PARAM_129845054:Identifier} ORDER BY Timestamp DESC LIMIT {HYPERDX_PARAM_49586:Int32}',
params: {
HYPERDX_PARAM_1544803905: 'default',
HYPERDX_PARAM_129845054: 'otel_logs',
HYPERDX_PARAM_49586: 200,
},
};
const res = chSqlToAliasMap(chSqlInput);
expect(res).toEqual({
__hdx_timestamp: 'Timestamp',
__hdx_duration_ms: 'NULL',
});
});

it('Normal alias, with brackets', () => {
const chSqlInput: ChSql = {
sql: "SELECT Timestamp as ts,ResourceAttributes['service.name'] as serviceTest,Body,TimestampTime,ServiceName,TimestampTime FROM {HYPERDX_PARAM_1544803905:Identifier}.{HYPERDX_PARAM_129845054:Identifier} WHERE (TimestampTime >= fromUnixTimestamp64Milli({HYPERDX_PARAM_1456399765:Int64}) AND TimestampTime <= fromUnixTimestamp64Milli({HYPERDX_PARAM_1719057412:Int64})) ORDER BY TimestampTime DESC LIMIT {HYPERDX_PARAM_49586:Int32} OFFSET {HYPERDX_PARAM_48:Int32}",
Expand Down
4 changes: 4 additions & 0 deletions packages/common-utils/src/clickhouse/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,10 @@ function selectColumnsToAliasMap(
`${column.expr.column.expr.value}['${column.expr.array_index[0].index.value}']`
: // normal alias
column.expr.column.expr.value;
} else if (column.expr.type === 'null') {
// NULL literal projection (multi-source search pads columns a source
// lacks with `NULL AS "alias"`); the parser emits it without a loc.
aliasMap[column.as] = 'NULL';
} else if (column.expr.loc != null) {
aliasMap[column.as] = parsedSql.slice(
column.expr.loc.start.offset,
Expand Down
Loading