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/polite-queens-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hyperdx/app": patch
---

fix(app): stop event deltas selection failing on Distributed tables
113 changes: 69 additions & 44 deletions packages/app/src/components/DBDeltaChart.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useCallback, useEffect, useMemo, useState } from 'react';
import { ClickHouseQueryError } from '@hyperdx/common-utils/dist/clickhouse';
import { tcFromChartConfig } from '@hyperdx/common-utils/dist/core/metadata';
import {
BuilderChartConfigWithDateRange,
Filter,
Expand All @@ -17,6 +18,7 @@ import { useElementSize } from '@mantine/hooks';

import { isAggregateFunction } from '@/ChartUtils';
import { useQueriedChartConfig } from '@/hooks/useChartConfig';
import { useTableMetadata } from '@/hooks/useMetadata';
import { getFirstTimestampValueExpression } from '@/source';
import { getChartColorError, getChartColorSuccess } from '@/utils';

Expand Down Expand Up @@ -102,6 +104,25 @@ export default function DBDeltaChart({
config.timestampValueExpression,
);

/** Distributed (and Merge) tables need different subquery handling:
* - An `IN <subquery>` over a Distributed table is rejected under the
* default `distributed_product_mode = 'deny'`, so subqueries whose values
* are global (AggregatedTimestamps) must use GLOBAL IN.
* - The PartIds prefetch hint is skipped entirely: `_part`/`_part_offset`
* only identify rows within one shard, so the hint is meaningless there.
*
* Only a table confirmed local gets plain IN and the hint. If the lookup
* fails, assume Distributed: GLOBAL IN also works on local tables, and
* dropping the hint doesn't change results.
*/
const { data: tableMetadata, isLoading: isTableMetadataLoading } =
useTableMetadata(tcFromChartConfig(config), { retry: false });
Comment thread
karl-power marked this conversation as resolved.
Resolved
const isLocalTable = tableMetadata != null && !tableMetadata.isPointerTable;
const canUsePartIdsHint = isLocalTable;
const inOperator = isLocalTable ? 'IN' : 'GLOBAL IN';
const notInOperator = isLocalTable ? 'NOT IN' : 'GLOBAL NOT IN';
const isSelectionQueryEnabled = hasSelection && !isTableMetadataLoading;

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 — A failing metadata lookup holds selection queries back for about 7s while react-query retries

useTableMetadata runs with the app's default QueryClient (packages/app/pages/_app.tsx:53), which has no retry override. React-query v5 therefore retries a failed lookup 3 times with backoff (1s, 2s, 4s). isLoading stays true through all of those retries, so the outlier and inlier queries, and the chart's loading state, wait roughly 7s before falling back. This is the case the PR calls a harmless fallback. Pass { retry: false } or retry: 1 to useTableMetadata(tcFromChartConfig(config), …), or gate on isFetched/isError rather than waiting out the retries.

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.

addressed in 8511657


// Helper to build the shared AggregatedTimestamps CTE (used by both outlier and inlier queries)
const buildAggregatedTimestampsCTE = () =>
isAggregate
Expand Down Expand Up @@ -136,10 +157,13 @@ export default function DBDeltaChart({
}
: null;

const buildAggregateTimestampCondition = (isOutlier: boolean) =>
`${timestampExpr} ${isOutlier ? inOperator : notInOperator} (SELECT ${timestampExpr} FROM AggregatedTimestamps)`;

// Helper to build WITH clauses for a query (outlier or inlier)
const buildWithClauses = (
isOutlier: boolean,
): NonNullable<BuilderChartConfigWithDateRange['with']> => {
): BuilderChartConfigWithDateRange['with'] => {
const aggregatedTimestampsCTE = buildAggregatedTimestampsCTE();

// Build the SQL condition for filtering
Expand All @@ -153,38 +177,40 @@ export default function DBDeltaChart({
return isOutlier ? query : `NOT (${query})`;
};

const sqlCondition = buildSqlCondition();
const aggregateTimestampCondition = isOutlier
? `${timestampExpr} IN (SELECT ${timestampExpr} FROM AggregatedTimestamps)`
: `${timestampExpr} NOT IN (SELECT ${timestampExpr} FROM AggregatedTimestamps)`;
const partIdsCTE = canUsePartIdsHint
? {
name: 'PartIds',
chartConfig: {
...config,
select: 'tuple(_part, _part_offset)',
filters: [
...(config.filters ?? []),
{
type: 'sql',
condition: buildSqlCondition(),
} satisfies Filter,
...(isAggregate
? [
{
type: 'sql',
condition: buildAggregateTimestampCondition(isOutlier),
} satisfies Filter,
]
: []),
],
orderBy: [
{ ordering: 'DESC' as const, valueExpression: stableSampleExpr },
],
limit: { limit: SAMPLE_SIZE },
},
}
: undefined;

return [
const withClauses = [
...(aggregatedTimestampsCTE ? [aggregatedTimestampsCTE] : []),
{
name: 'PartIds',
chartConfig: {
...config,
select: 'tuple(_part, _part_offset)',
filters: [
...(config.filters ?? []),
{
type: 'sql',
condition: sqlCondition,
} satisfies Filter,
...(isAggregate
? [
{
type: 'sql',
condition: aggregateTimestampCondition,
} satisfies Filter,
]
: []),
],
orderBy: [{ ordering: 'DESC', valueExpression: stableSampleExpr }],
limit: { limit: SAMPLE_SIZE },
},
},
...(partIdsCTE ? [partIdsCTE] : []),
];
return withClauses.length > 0 ? withClauses : undefined;
};

// Helper to build filters for the main query
Expand All @@ -204,29 +230,28 @@ export default function DBDeltaChart({
}
};

const sqlCondition = buildSqlCondition();
const aggregateTimestampCondition = isOutlier
? `${timestampExpr} IN (SELECT ${timestampExpr} FROM AggregatedTimestamps)`
: `${timestampExpr} NOT IN (SELECT ${timestampExpr} FROM AggregatedTimestamps)`;

return [
...(config.filters ?? []),
{
type: 'sql',
condition: sqlCondition,
condition: buildSqlCondition(),
} as { type: 'sql'; condition: string },
...(isAggregate
? [
{
type: 'sql',
condition: aggregateTimestampCondition,
condition: buildAggregateTimestampCondition(isOutlier),
} as { type: 'sql'; condition: string },
]
: []),
...(canUsePartIdsHint
? [
{
type: 'sql',
condition: `indexHint((_part, _part_offset) IN PartIds)`,
} as { type: 'sql'; condition: string },
]
: []),
{
type: 'sql',
condition: `indexHint((_part, _part_offset) IN PartIds)`,
} as { type: 'sql'; condition: string },
];
};

Expand All @@ -243,7 +268,7 @@ export default function DBDeltaChart({
orderBy: [{ ordering: 'DESC', valueExpression: stableSampleExpr }],
limit: { limit: SAMPLE_SIZE },
},
{ enabled: hasSelection },
{ enabled: isSelectionQueryEnabled },
);

const { data: inlierData, isLoading: isInlierLoading } =
Expand All @@ -256,7 +281,7 @@ export default function DBDeltaChart({
orderBy: [{ ordering: 'DESC', valueExpression: stableSampleExpr }],
limit: { limit: SAMPLE_SIZE },
},
{ enabled: hasSelection },
{ enabled: isSelectionQueryEnabled },
);

// When no selection exists, fetch all spans without any range filter
Expand All @@ -275,7 +300,7 @@ export default function DBDeltaChart({
);

const isLoading = hasSelection
? isOutlierLoading || isInlierLoading
? isTableMetadataLoading || isOutlierLoading || isInlierLoading
: isAllSpansLoading;

const error = outlierError ?? allSpansError;
Expand Down
130 changes: 130 additions & 0 deletions packages/app/src/components/__tests__/DBDeltaChart.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ import { screen } from '@testing-library/react';

import DBDeltaChart from '@/components/DBDeltaChart';
import { useQueriedChartConfig } from '@/hooks/useChartConfig';
import { useTableMetadata } from '@/hooks/useMetadata';

jest.mock('@/hooks/useChartConfig', () => ({
useQueriedChartConfig: jest.fn(),
}));

jest.mock('@/hooks/useMetadata', () => ({
useTableMetadata: jest.fn(),
}));

// PropertyComparisonChart is the per-attribute renderer. Stubbing it out keeps
// the legend assertions tight without dragging uPlot/element-size hooks into
// the test environment.
Expand All @@ -27,6 +32,7 @@ jest.mock('../PropertyComparisonChart', () => ({
}));

const mockUseQueriedChartConfig = useQueriedChartConfig as jest.Mock;
const mockUseTableMetadata = useTableMetadata as jest.Mock;

// Sentinel filter on baseConfig so the allSpans assertion can prove
// the branch passes user-supplied filters through unchanged. Without
Expand Down Expand Up @@ -69,6 +75,10 @@ describe('DBDeltaChart', () => {
error: undefined,
isLoading: false,
});
mockUseTableMetadata.mockReturnValue({
data: { engine: 'MergeTree', isPointerTable: false },
isLoading: false,
});
});

describe('distribution mode (no heatmap selection)', () => {
Expand Down Expand Up @@ -180,6 +190,126 @@ describe('DBDeltaChart', () => {
});
});

describe('selection queries by table engine', () => {
const selection = { xMin: 1, xMax: 2, yMin: 1, yMax: 2 };
const conditions = (config: BuilderChartConfigWithDateRange) =>
(config.filters ?? []).map(f => ('condition' in f ? f.condition : ''));

it('uses the PartIds prefetch hint on local MergeTree tables', () => {
renderChart(selection);

const [[outlierConfig], [inlierConfig]] =
mockUseQueriedChartConfig.mock.calls;
for (const config of [outlierConfig, inlierConfig]) {
expect(config.with?.map((cte: { name: string }) => cte.name)).toEqual([
'PartIds',
]);
expect(conditions(config)).toContain(
'indexHint((_part, _part_offset) IN PartIds)',
);
}
});

it('uses plain IN for the aggregated-timestamps subquery on local MergeTree tables', () => {
renderChart({ ...selection, valueExpr: 'count()' });

const [[outlierConfig], [inlierConfig]] =
mockUseQueriedChartConfig.mock.calls;
expect(conditions(outlierConfig)).toContain(
'Timestamp IN (SELECT Timestamp FROM AggregatedTimestamps)',
);
expect(conditions(inlierConfig)).toContain(
'Timestamp NOT IN (SELECT Timestamp FROM AggregatedTimestamps)',
);
});

it('skips the PartIds hint on Distributed tables', () => {
mockUseTableMetadata.mockReturnValue({
data: { engine: 'MergeTree', isPointerTable: true },
isLoading: false,
});
renderChart(selection);

const [[outlierConfig], [inlierConfig]] =
mockUseQueriedChartConfig.mock.calls;
for (const config of [outlierConfig, inlierConfig]) {
expect(config.with).toBeUndefined();
expect(conditions(config).join(' ')).not.toContain('_part');
}
});

it('uses GLOBAL IN for the aggregated-timestamps subquery on Distributed tables', () => {
Comment thread
karl-power marked this conversation as resolved.
Resolved
mockUseTableMetadata.mockReturnValue({
data: { engine: 'MergeTree', isPointerTable: true },
isLoading: false,
});
renderChart({ ...selection, valueExpr: 'count()' });

const [[outlierConfig], [inlierConfig]] =
mockUseQueriedChartConfig.mock.calls;
expect(
outlierConfig.with?.map((cte: { name: string }) => cte.name),
).toEqual(['AggregatedTimestamps']);
expect(conditions(outlierConfig)).toContain(
'Timestamp GLOBAL IN (SELECT Timestamp FROM AggregatedTimestamps)',
);
expect(conditions(inlierConfig)).toContain(
'Timestamp GLOBAL NOT IN (SELECT Timestamp FROM AggregatedTimestamps)',
);
});

it('waits for table metadata before running selection queries', () => {
mockUseTableMetadata.mockReturnValue({
data: undefined,
isLoading: true,
});
renderChart(selection);

const [outlierCall, inlierCall] = mockUseQueriedChartConfig.mock.calls;
expect(outlierCall[1]).toMatchObject({ enabled: false });
expect(inlierCall[1]).toMatchObject({ enabled: false });
});

it('falls back to the hint-free query when table metadata is unavailable', () => {
mockUseTableMetadata.mockReturnValue({
data: undefined,
isLoading: false,
});
renderChart(selection);

const [[outlierConfig, outlierOptions]] =
mockUseQueriedChartConfig.mock.calls;
expect(outlierOptions).toMatchObject({ enabled: true });
expect(outlierConfig.with).toBeUndefined();
});

it('uses GLOBAL IN for aggregate selections when table metadata is unavailable', () => {
mockUseTableMetadata.mockReturnValue({
data: undefined,
isLoading: false,
});
renderChart({ ...selection, valueExpr: 'count()' });

const [[outlierConfig], [inlierConfig]] =
mockUseQueriedChartConfig.mock.calls;
expect(conditions(outlierConfig)).toContain(
'Timestamp GLOBAL IN (SELECT Timestamp FROM AggregatedTimestamps)',
);
expect(conditions(inlierConfig)).toContain(
'Timestamp GLOBAL NOT IN (SELECT Timestamp FROM AggregatedTimestamps)',
);
});

it('does not retry a failed table metadata lookup', () => {
renderChart(selection);

expect(mockUseTableMetadata).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({ retry: false }),
);
});
});

describe('legendPrefix prop', () => {
// The divider is a vertical separator rendered immediately after the
// legendPrefix to visually divide it from the comparison legend. It's
Expand Down
Loading