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/raw-sql-filters-kv-rewrite.md
Original file line number Diff line number Diff line change
@@ -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.
59 changes: 59 additions & 0 deletions packages/common-utils/src/__tests__/renderChartConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand Down
22 changes: 21 additions & 1 deletion packages/common-utils/src/core/renderChartConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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,
Expand Down
Loading