Skip to content
Open
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
7 changes: 7 additions & 0 deletions .changeset/timeseries-selector-26-11.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@hyperdx/api': patch
'@hyperdx/common-utils': patch
---

fix: PromQL label lookups with `match[]` work on ClickHouse 26.11, which
requires the `return_columns` argument of `timeSeriesSelector()`
19 changes: 19 additions & 0 deletions packages/api/src/controllers/__tests__/timeseriesEngine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,28 @@ import { fromAny } from '@total-typescript/shoehorn';
import {
formatMatrixResponse,
formatVectorResponse,
getSeriesSelectorCondition,
isNoTimeRangesTableError,
} from '@/controllers/timeseriesEngine';

describe('getSeriesSelectorCondition', () => {
const selectorSql = (takesReturnColumns: boolean) =>
getSeriesSelectorCondition({
databaseName: 'db',
tableName: 'ts',
match: ['up'],
takesReturnColumns,
}).sql;

it("passes return_columns 'samples' from ClickHouse 26.11", () => {
expect(selectorSql(true)).toContain("), 'samples'\n");
});

it('keeps the 5-argument call before 26.11', () => {
expect(selectorSql(false)).not.toContain('samples');
});
});

describe('isNoTimeRangesTableError', () => {
// The shape ClickhouseClient throws: the server's error wrapped as `cause`.
const queryError = (type: string) =>
Expand Down
13 changes: 11 additions & 2 deletions packages/api/src/controllers/timeseriesEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
import {
supportsPrometheusHttpApi,
supportsPrometheusLabelsApi,
timeSeriesSelectorTakesReturnColumns,
} from '@hyperdx/common-utils/dist/core/clickhouseVersion';
import {
Metadata,
Expand Down Expand Up @@ -107,21 +108,27 @@ const TIMESERIES_MAX_TIME_MS = 10413791999999; // 2299-12-31T23:59:59.999Z
* Returns a predicate keeping only the series matched by at least one of
* the given `match` selectors.
*/
function getSeriesSelectorCondition({
export function getSeriesSelectorCondition({
databaseName,
tableName,
match,
startMs,
endMs,
takesReturnColumns,
}: {
databaseName: string;
tableName: string;
match: string[];
startMs?: number;
endMs?: number;
/** ClickHouse 26.11+ requires the `return_columns` argument. */
takesReturnColumns: boolean;
}): ChSql {
const minTime = { Int64: startMs ?? TIMESERIES_MIN_TIME_MS };
const maxTime = { Int64: endMs ?? TIMESERIES_MAX_TIME_MS };
// Only `id` is read. 'samples' matches the bucketed layout that 26.11 tables
// use; 'timestamp_and_value' would unfold every bucket with arrayJoin.
const returnColumns = takesReturnColumns ? chSql`, 'samples'` : chSql``;
const idsPerSelector = match.map(
selector => chSql`
SELECT id
Expand All @@ -130,7 +137,7 @@ function getSeriesSelectorCondition({
${{ String: tableName }},
${{ String: selector }},
fromUnixTimestamp64Milli(${minTime}),
fromUnixTimestamp64Milli(${maxTime})
fromUnixTimestamp64Milli(${maxTime})${returnColumns}
)`,
);

Expand Down Expand Up @@ -185,13 +192,15 @@ async function getSeriesFilterConditions({
'match[] requires a TimeSeries table that stores time ranges (store_time_ranges = 1, or store_min_time_and_max_time = 1 before version 8), or the prometheus_api_v1 HTTP handler on ClickHouse 26.9+',
);
}
const version = await metadata.getServerVersion({ connectionId });

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.

🟡 P2 — SELECT version() now runs on every match[] label lookup, after the time-bounds probes have finished

metadata is built on a throwaway new MetadataCache() (line 176), so getServerVersion sends a fresh SELECT version() on every call. It waits until timeSeriesTimeBounds has finished its one or two probe queries, which adds a third sequential round trip to every dashboard-variable lookup that uses match[]. When selectors != null, start metadata.getServerVersion({ connectionId }) alongside timeSeriesTimeBounds with Promise.all (it does not depend on the bounds), or pass in a version the caller already holds.

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.

Leaving this as is: it only runs when match[] is given, and SELECT version() is a sub-millisecond query next to the tags scan that follows. Running it in parallel is a fine follow-up if lookups ever show it.

conditions.push(
getSeriesSelectorCondition({
databaseName,
tableName,
match: selectors,
startMs,
endMs,
takesReturnColumns: timeSeriesSelectorTakesReturnColumns(version),
}),
);
}
Expand Down
23 changes: 23 additions & 0 deletions packages/common-utils/src/__tests__/clickhouseVersion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
supportsMergeTreeIndex,
supportsPrometheusHttpApi,
supportsPrometheusLabelsApi,
timeSeriesSelectorTakesReturnColumns,
} from '@/core/clickhouseVersion';

type ClickHouseVersionTuple = readonly [number, number, number, number];
Expand Down Expand Up @@ -324,3 +325,25 @@ describe('supportsPrometheusLabelsApi', () => {
expect(supportsPrometheusLabelsApi(undefined)).toBe(false);
});
});

describe('timeSeriesSelectorTakesReturnColumns', () => {
it.each<readonly [ClickHouseVersionTuple]>([
[[26, 11, 0, 0]],
[[26, 11, 1, 2942]],
[[27, 0, 0, 0]],
])('accepts %j', version => {
expect(timeSeriesSelectorTakesReturnColumns(version)).toBe(true);
});

it.each<readonly [ClickHouseVersionTuple]>([
[[26, 10, 3, 7]],
[[26, 9, 0, 0]],
[[25, 12, 0, 0]],
])('rejects %j', version => {
expect(timeSeriesSelectorTakesReturnColumns(version)).toBe(false);
});

it('returns false when the version is unknown', () => {
expect(timeSeriesSelectorTakesReturnColumns(undefined)).toBe(false);
});
});
18 changes: 18 additions & 0 deletions packages/common-utils/src/core/clickhouseVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,21 @@ export function supportsPrometheusLabelsApi(
): boolean {
return isClickHouseVersionAtLeast(version, PROMETHEUS_LABELS_API_MIN);
}

/**
* First release whose `timeSeriesSelector()` takes a `return_columns`
* argument. From 26.11 it is required, and the 5-argument
* `('db', 'table', selector, min, max)` call reads 'db' as the table.
*/
const TIME_SERIES_SELECTOR_RETURN_COLUMNS_MIN: ClickHouseVersion = [
26, 11, 0, 0,
];

export function timeSeriesSelectorTakesReturnColumns(
version: ClickHouseVersion | undefined,
): boolean {
return isClickHouseVersionAtLeast(
version,
TIME_SERIES_SELECTOR_RETURN_COLUMNS_MIN,
);
}
Loading