From 7f66a72b521da58a65a723da5fd3d15fcd59a024 Mon Sep 17 00:00:00 2001 From: Karl Power Date: Wed, 25 Nov 2026 11:38:38 +0100 Subject: [PATCH] fix(common-utils): apply Map KV items rewrite to SQL filters in $__filters Raw SQL tiles and SQL query dashboard filters expand $__filters through renderFiltersToSql, which never built the KV items lookup, so SQL filters on Map keys kept the Map subscript while Lucene filters got has(). --- .changeset/raw-sql-filters-kv-rewrite.md | 9 +++ .../src/__tests__/renderChartConfig.test.ts | 59 +++++++++++++++++++ .../src/core/renderChartConfig.ts | 22 ++++++- 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 .changeset/raw-sql-filters-kv-rewrite.md diff --git a/.changeset/raw-sql-filters-kv-rewrite.md b/.changeset/raw-sql-filters-kv-rewrite.md new file mode 100644 index 000000000..e61bd9d30 --- /dev/null +++ b/.changeset/raw-sql-filters-kv-rewrite.md @@ -0,0 +1,9 @@ +--- +'@hyperdx/common-utils': patch +'@hyperdx/app': patch +--- + +fix: use the Map KV items index for SQL filters expanded by `$__filters` in raw +SQL tiles and SQL query dashboard filters, as Lucene filters already do. As with +Lucene filters, `$__filters` must be placed where the source table's columns are +visible, since the rewrite references the KV items ALIAS column. diff --git a/packages/common-utils/src/__tests__/renderChartConfig.test.ts b/packages/common-utils/src/__tests__/renderChartConfig.test.ts index 7551176f4..3b772860e 100644 --- a/packages/common-utils/src/__tests__/renderChartConfig.test.ts +++ b/packages/common-utils/src/__tests__/renderChartConfig.test.ts @@ -2148,6 +2148,65 @@ describe('renderChartConfig', () => { expect(sql).not.toContain("toString(LogAttributes['env'])"); }); + const renderRawSqlFilters = async ( + filters: Filter[], + tableName = 'otel_logs', + ) => + ( + await renderChartConfig( + { + configType: 'sql', + sqlTemplate: 'SELECT count() FROM otel_logs WHERE $__filters', + connection: 'test-connection', + dateRange: [new Date('2025-01-01'), new Date('2025-01-02')], + source: 'source-1', + from: { databaseName: 'default', tableName }, + filters, + }, + mockMetadata, + querySettings, + ) + ).sql; + + it('rewrites ad hoc filter conditions in raw SQL $__filters', async () => { + stubKvItemsMetadata(); + expect( + await renderRawSqlFilters( + adhocConditionsToFilters([ + { key: "LogAttributes['country']", operator: '=', value: 'US' }, + { key: "LogAttributes['env']", operator: 'IN', value: 'prod' }, + { key: "LogAttributes['env']", operator: 'IN', value: 'dev' }, + { key: "LogAttributes['path']", operator: '!=', value: '/' }, + ]), + ), + ).toBe( + "SELECT count() FROM otel_logs WHERE ((has(`LogAttributeItems`, concat('country', '=', 'US'))) AND (hasAny(`LogAttributeItems`, array(concat('env', '=', 'prod'), concat('env', '=', 'dev')))) AND (toString(LogAttributes['path']) != '/'))", + ); + }); + + it('rewrites a SQL filter in raw SQL $__filters', async () => { + stubKvItemsMetadata(); + expect( + await renderRawSqlFilters([ + { type: 'sql', condition: "LogAttributes['k'] = 'v'" }, + ]), + ).toBe( + "SELECT count() FROM otel_logs WHERE ((has(`LogAttributeItems`, concat('k', '=', 'v'))))", + ); + }); + + it('leaves raw SQL $__filters unchanged for a source without a table', async () => { + stubKvItemsMetadata(); + expect( + await renderRawSqlFilters( + [{ type: 'sql', condition: "LogAttributes['k'] = 'v'" }], + '', + ), + ).toBe( + "SELECT count() FROM otel_logs WHERE ((LogAttributes['k'] = 'v'))", + ); + }); + const buildWhereConfig = (where: string): ChartConfigWithOptDateRange => ({ connection: 'test-connection', from: { databaseName: 'default', tableName: 'otel_logs' }, diff --git a/packages/common-utils/src/core/renderChartConfig.ts b/packages/common-utils/src/core/renderChartConfig.ts index b1b76ae3d..bcda0bbaa 100644 --- a/packages/common-utils/src/core/renderChartConfig.ts +++ b/packages/common-utils/src/core/renderChartConfig.ts @@ -2764,6 +2764,20 @@ async function renderFiltersToSql( return undefined; } + // The same Map-KV rewrite as builder tiles get in renderWhere. Like the + // Lucene serializer's rewrite, it needs the items ALIAS column to be visible + // where the SQL places $__filters. + const { databaseName, tableName } = chartConfig.from; + const textIndexInfoLookup: TextIndexInfoLookup = + databaseName && tableName && chartConfig.filters.some(f => f.type === 'sql') + ? await buildTextIndexInfoLookup({ + metadata, + databaseName, + tableName, + connectionId: chartConfig.connection, + }) + : new Map(); + const conditions = ( await Promise.all( chartConfig.filters.map(async filter => { @@ -2784,7 +2798,13 @@ async function renderFiltersToSql( hasSourceTable ) { const condition = await renderWhereExpressionStr({ - condition: filter.condition, + condition: + filter.type === 'sql' + ? rewriteSqlFilterWithKvItems( + filter.condition, + textIndexInfoLookup, + ) + : filter.condition, from: chartConfig.from!, language: filter.type, implicitColumnExpression: chartConfig.implicitColumnExpression,