Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/db-statement-index-prefilter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hyperdx/app': patch
---

fix: Let the Services > Database query drawer use the span attribute skip index instead of scanning the whole trace window
140 changes: 140 additions & 0 deletions packages/app/src/__tests__/serviceDashboard.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { ColumnMeta } from '@hyperdx/common-utils/dist/clickhouse';
import { rewriteSqlFilterWithKvItems } from '@hyperdx/common-utils/dist/core/renderChartConfig';
import { parseQuery } from '@hyperdx/common-utils/dist/filters';
import type { TTraceSource } from '@hyperdx/common-utils/dist/types';
import { SourceKind } from '@hyperdx/common-utils/dist/types';
import { renderHook } from '@testing-library/react';
Expand All @@ -7,6 +9,7 @@ import * as metadataModule from '@/hooks/useMetadata';
import {
getExpressions,
makeCoalescedFieldsAccessQuery,
makeDbStatementCondition,
useServiceDashboardExpressions,
} from '@/serviceDashboard';

Expand Down Expand Up @@ -146,6 +149,143 @@ describe('Service Dashboard', () => {
});
});

describe('dbStatementPrefilterFields', () => {
it('should expose the map accessors the renderer can rewrite', () => {
expect(
getExpressions(mockSource, [], []).dbStatementPrefilterFields,
).toEqual([
"SpanAttributes['db.query.text']",
"SpanAttributes['db.statement']",
]);
});

it('should be empty for JSON attribute columns', () => {
expect(
getExpressions(mockSource, [], ['SpanAttributes'])
.dbStatementPrefilterFields,
).toEqual([]);
});
});

describe('makeDbStatementCondition', () => {
const dbStatement =
"coalesce(nullif(SpanAttributes['db.query.text'], ''), nullif(SpanAttributes['db.statement'], ''))";
const prefilterFields = [
"SpanAttributes['db.query.text']",
"SpanAttributes['db.statement']",
];
const equality = `${dbStatement} IN ('SELECT 1')`;

it('should only compare the statement when there is nothing to prefilter on', () => {
expect(
makeDbStatementCondition({
expressions: { dbStatement, dbStatementPrefilterFields: [] },
statement: 'SELECT 1',
}),
).toBe(equality);
});

it('should prefilter on each attribute the statement can come from', () => {
expect(
makeDbStatementCondition({
expressions: {
dbStatement,
dbStatementPrefilterFields: prefilterFields,
},
statement: 'SELECT 1',
}),
).toBe(
"(SpanAttributes['db.query.text'] IN ('SELECT 1') OR " +
"SpanAttributes['db.statement'] IN ('SELECT 1')) " +
`AND ${equality}`,
);
});

it('should escape quotes everywhere the statement appears', () => {
const condition = makeDbStatementCondition({
expressions: {
dbStatement,
dbStatementPrefilterFields: prefilterFields,
},
statement: "SELECT 'a'",
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 minor — No test sends a statement with quotes or backslashes through the kv-items rewrite

The rewrite test only uses 'SELECT 1'. With a quote or backslash, the statement passes through escapeSqlString, then node-sql-parser, then SqlString.format re-escaping in generateHasSqlForKvItemsColumn (renderChartConfig.ts:383). Other code (renderChartConfig.ts:326) puts parsed single_quote_string values back between quotes without re-escaping them, which suggests the parser keeps the SQL escapes. If so, '' gets escaped a second time, has(items, …) never matches, and the drawer shows nothing. Run "SELECT 'a' WHERE x LIKE 'a\\_b'" through rewriteSqlFilterWithKvItems and check the decoded literal equals the original statement.

Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The behaviour holds; the gap is the test. From system.query_log, a statement full of quotes and braces going through the rewrite and running:

AND ((has(SpanAttributeItems, concat('db.query.text', '=', '{"update":"?","updates":[{"q":{"_id":"?"},"u":{"$set":{"expires":{}}}}],"ordered":"?"}')) OR has(...

I have the test written locally. This is the third round of findings, so I would rather a maintainer says whether to add it than keep pushing commits.


expect(condition.match(/SELECT ''a''/g)).toHaveLength(3);
expect(condition).not.toContain("IN ('SELECT 'a'')");
});

const kvItemsLookup = new Map([
[
'SpanAttributes',
{
kv: {
columnName: 'SpanAttributeItems',
indexName: 'idx_span_attr_items',
separator: ':',
useHasAny: false,
mapColumn: 'SpanAttributes',
},
},
],
]);

const rewrite = (statement: string) =>
rewriteSqlFilterWithKvItems(
makeDbStatementCondition({
expressions: {
dbStatement,
dbStatementPrefilterFields: prefilterFields,
},
statement,
}),
kvItemsLookup,
);

it('should be rewritten onto the items column by the query renderer', () => {
const rewritten = rewrite('SELECT 1');

expect(rewritten).toContain(
"has(`SpanAttributeItems`, concat('db.query.text', ':', 'SELECT 1'))",
);
expect(rewritten).toContain(
"has(`SpanAttributeItems`, concat('db.statement', ':', 'SELECT 1'))",
);
expect(rewritten).toContain(dbStatement);
});

it.each([["SELECT 'a' FROM t"], ["SELECT 'a\\b'"], ['{"q":{"_id":"?"}}']])(
'should survive the rewrite with its escaping intact (%j)',
statement => {
const rewritten = rewrite(statement);

const quoteCount = rewritten.split("'").length - 1;

expect(rewritten).toContain('has(`SpanAttributeItems`');
expect(quoteCount % 2).toBe(0);
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
},
);

it.each([[[]], [prefilterFields]])(
'should be read back by the search page as the same filter (%p)',
dbStatementPrefilterFields => {
const statement = "SELECT 'a' FROM t WHERE b IN (1) AND c = ?";

const { filters } = parseQuery([
{
type: 'sql',
condition: makeDbStatementCondition({
expressions: { dbStatement, dbStatementPrefilterFields },
statement,
}),
},
]);

expect(Object.keys(filters)).toEqual([dbStatement]);
expect([...filters[dbStatement].included]).toEqual([statement]);
},
);
});

describe('useServiceDashboardExpressions', () => {
const mockColumns: ColumnMeta[] = [
{ name: 'Duration', type: 'UInt64' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ import { ChartCard } from '@/components/charts/ChartCard';
import { DBTimeChart } from '@/components/DBTimeChart';
import { DrawerBody, DrawerHeader } from '@/components/DrawerUtils';
import SlowestEventsTile from '@/components/ServiceDashboardSlowestEventsTile';
import { useServiceDashboardExpressions } from '@/serviceDashboard';
import {
inList,
makeDbStatementCondition,
useServiceDashboardExpressions,
} from '@/serviceDashboard';
import { useSource } from '@/source';
import { useZIndex, ZIndexContext } from '@/zIndex';

Expand Down Expand Up @@ -46,16 +50,23 @@ export default function ServiceDashboardDbQuerySidePanel({
const drawerZIndex = contextZIndex + 10;

const dbQueryFilters = useMemo(() => {
if (!expressions || !dbQuery) {
return [];
}

const filters: Filter[] = [
{
type: 'sql',
condition: `${expressions?.dbStatement} IN ('${dbQuery}')`,
condition: makeDbStatementCondition({
expressions,
statement: dbQuery,
}),
},
];
if (service) {
filters.push({
type: 'sql',
condition: `${expressions?.service} IN ('${service}')`,
condition: inList(expressions.service, service),
});
}
return filters;
Expand Down
63 changes: 51 additions & 12 deletions packages/app/src/serviceDashboard.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { useMemo } from 'react';
import { ColumnMeta } from '@hyperdx/common-utils/dist/clickhouse';
import { tcFromSource } from '@hyperdx/common-utils/dist/core/metadata';
import { escapeSqlString } from '@hyperdx/common-utils/dist/core/utils';
import { SourceKind, TTraceSource } from '@hyperdx/common-utils/dist/types';

import { useColumns, useJsonColumns } from './hooks/useMetadata';

const COALESCE_FIELDS_LIMIT = 100;

// Ordered from highest to lowest precedence
const DB_STATEMENT_KEYS = ['db.query.text', 'db.statement'];

// Helper function to format field access based on column type
function formatFieldAccess(
field: string,
Expand Down Expand Up @@ -89,21 +93,21 @@ function getDefaults({
isAttributeFieldJSON?: boolean;
} = {}) {
const dbStatement = makeCoalescedFieldsAccessQuery(
[
formatFieldAccess(
spanAttributeField,
'db.query.text',
isAttributeFieldJSON,
),
formatFieldAccess(
spanAttributeField,
'db.statement',
isAttributeFieldJSON,
),
],
DB_STATEMENT_KEYS.map(key =>
formatFieldAccess(spanAttributeField, key, isAttributeFieldJSON),
),
isAttributeFieldJSON,
);

// Plain map accessors: the shape `rewriteSqlFilterWithKvItems` rewrites onto
// an indexed items column. JSON columns have no such column, and their
// accessors are not map subscripts, so they get no prefilter.
const dbStatementPrefilterFields = isAttributeFieldJSON
? []
: DB_STATEMENT_KEYS.map(key =>
formatFieldAccess(spanAttributeField, key, false),
);

return {
duration: 'Duration',
durationPrecision: 9,
Expand Down Expand Up @@ -133,11 +137,45 @@ function getDefaults({
isAttributeFieldJSON,
),
dbStatement,
dbStatementPrefilterFields,
};
}

const ENDPOINT_MATERIALIZED_COLUMN_NAME = 'endpoint';

export function inList(expression: string, value: string): string {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 minor — inList duplicates the escaped IN (...) builder already in filtersToQuery

filtersToQuery (packages/common-utils/src/filters.ts:75-88) already renders ${key} IN ('${escapeSqlString(v)}') for included values. Build the condition from that function, or move one shared single-value helper into common-utils/src/filters.ts that filtersToQuery also uses, so the escaping rules live in one place (the repository's REQUIRED DRY rule).

Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sharing it would mean moving the helper into common-utils/src/filters.ts so filtersToQuery uses it too, which is outside the files this PR touches. Flagging rather than widening the change here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 minor — The sibling Endpoint drawer still interpolates endpoint and service without escaping

inList is now exported as the shared helper, but packages/app/src/components/ServiceDashboardEndpointSidePanel.tsx:59 and :65 still build IN ('${endpoint}') and IN ('${service}') from raw values. A service or endpoint name containing ' breaks that query. Switch both to inList.

Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pre-existing, and in a file this PR does not touch, so I am leaving it. It is worth its own fix though: an endpoint or service name containing a quote breaks every query in that drawer.

return `${expression} IN ('${escapeSqlString(value)}')`;
}

/**
* Condition selecting the spans of a single database statement.
*
* No skip index covers the coalesced attribute lookup, so on its own it makes
* ClickHouse read every granule in the time range. The prefilter compares the
* same keys directly, which matches a superset of those spans while the
* coalesced comparison still decides; `rewriteSqlFilterWithKvItems` then
* rewrites each comparison onto the schema's indexed items column, and older
* schemas serve them from their `mapValues` index as written.
*/
export function makeDbStatementCondition({
expressions,
statement,
}: {
expressions: Pick<
ReturnType<typeof getExpressions>,
'dbStatement' | 'dbStatementPrefilterFields'
>;
statement: string;
}): string {
const equality = inList(expressions.dbStatement, statement);
const prefilter = expressions.dbStatementPrefilterFields.map(field =>
inList(field, statement),
);
return prefilter.length
? `(${prefilter.join(' OR ')}) AND ${equality}`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 major — The OR prefilter turns into an AND when you drill down to search, so spans disappear

parseQuery (packages/common-utils/src/filters.ts:532, extractInClauses) takes every IN clause in the condition, so a drawer chart's drill-down (ChartUtils.tsx:1387 serializes config.filters into the search URL) gives the sidebar three facets: SpanAttributes['db.query.text'], SpanAttributes['db.statement'] and the coalesce. The first time someone changes a filter, updateFilterQuery (searchFilters.tsx:137) rebuilds the query with filtersToQuery, which ANDs those facets together. Any span that has only one of the two attributes then gets dropped, and that is the usual case. Keep the prefilter out of the user-visible filter: add it at render time (e.g. as indexHint(...) or an internal condition the renderer adds), or make parseQuery ignore it. Also make the round-trip test assert Object.keys(filters) equals [dbStatement], not just that filters[dbStatement] is defined.

Nothing blocks merge automatically, but a maintainer will expect this fixed if it is a real defect in code this PR changes. If it is about surrounding code, reply and say so instead of patching. Do not widen the PR. How to respond

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reproduced this scenario in the running product and it does not happen.

The drill-down URL the drawer generated, filters param decoded:

[{"type":"sql","condition":"(SpanAttributes['db.query.text'] IN ('{\"create\":\"?\"}') OR SpanAttributes['db.statement'] IN ('{\"create\":\"?\"}')) AND coalesce(nullif(SpanAttributes['db.query.text'], ''), nullif(SpanAttributes['db.statement'], '')) IN ('{\"create\":\"?\"}')"}]

Opening it, the search sidebar renders one facet, coalesce(nullif(...)) — not three. The parenthesised OR never becomes one: extractInClauses skips it at filters.ts:431 (if (containsOperatorOutsideQuotes(part)) continue;).

system.query_log before touching any filter, 12 results:

AND ((has(SpanAttributeItems, concat('db.query.text', '=', '{"create":"?"}')) OR has(SpanAttributeItems, concat('db.statement', '=', '{"create":"?"}'))) AND (coalesce(...

system.query_log right after checking a ServiceName value in the sidebar — the updateFilterQuery → filtersToQuery path you describe — still 12 results:

AND ((coalesce(nullif(SpanAttributes['db.query.text'], ''), nullif(SpanAttributes['db.statement'], '')) IN ('{"create":"?"}')) AND (ServiceName IN ('hdx-oss-dev-api')))

The regenerated condition is the coalesce plus the facet the user added; the two attributes are never ANDed, so no span is dropped. What is lost is the prefilter, which is only an index hint — search falls back to the behaviour it has today. If I am misreading the scenario, point me at the step where a second facet appears.

: equality;
}

export function getExpressions(
source: TTraceSource,
columns: ColumnMeta[],
Expand Down Expand Up @@ -172,6 +210,7 @@ export function getExpressions(

// Database
dbStatement: defaults.dbStatement,
dbStatementPrefilterFields: defaults.dbStatementPrefilterFields,
};

const auxExpressions = {
Expand Down
Loading