Skip to content

Commit 2829187

Browse files
authored
fix(common-utils): cast Exemplars.TimeUnix before toUnixTimestamp64Milli (#3769)
1 parent 7cc91dd commit 2829187

3 files changed

Lines changed: 50 additions & 2 deletions

File tree

‎packages/common-utils/src/__tests__/queryChartConfig.int.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ClickHouseClient } from '@clickhouse/client';
44
import { convertCHDataTypeToJSType, JSDataType } from '@/clickhouse';
55
import { ClickhouseClient as HdxClickhouseClient } from '@/clickhouse/node';
66
import { Metadata, MetadataCache } from '@/core/metadata';
7+
import { renderMetricExemplarsChartConfig } from '@/core/renderChartConfig';
78
import { convertToCategoricalChartConfig } from '@/core/utils';
89
import {
910
ChartConfigWithOptDateRange,
@@ -2786,6 +2787,50 @@ describe('queryChartConfig Integration Tests', () => {
27862787
).rejects.toThrow();
27872788
});
27882789
});
2790+
// HIST_TABLE mirrors the seed schema, where Exemplars.TimeUnix is
2791+
// Array(DateTime). toUnixTimestamp64Milli only accepts DateTime64, so the
2792+
// scan must cast it; this runs the generated SQL against that column type.
2793+
it('reads exemplars from an Array(DateTime) Exemplars.TimeUnix column', async () => {
2794+
const traceId = '0af7651916cd43dd8448eb211c80319c';
2795+
await client.insert({
2796+
table: `${DATABASE}.${HIST_TABLE}`,
2797+
values: [
2798+
{
2799+
...histRow('exemplar.latency', insertTs(1), 'svc-a', [3, 1], [1]),
2800+
'Exemplars.FilteredAttributes': [{}],
2801+
'Exemplars.TimeUnix': [insertTs(1)],
2802+
'Exemplars.Value': [2.5],
2803+
'Exemplars.SpanId': ['b7ad6b7169203331'],
2804+
'Exemplars.TraceId': [traceId],
2805+
},
2806+
],
2807+
format: 'JSONEachRow',
2808+
});
2809+
2810+
const exemplarSql = await renderMetricExemplarsChartConfig(
2811+
baseConfig({ select: [histQuantileSelect('exemplar.latency', 0.99)] }),
2812+
metadata,
2813+
);
2814+
expect(exemplarSql).not.toBeNull();
2815+
2816+
const resp = await hdxClient.query({
2817+
query: exemplarSql!.sql,
2818+
query_params: exemplarSql!.params,
2819+
format: 'JSON',
2820+
});
2821+
const { data } = await resp.json<{
2822+
timestamp: string;
2823+
value: number;
2824+
traceId: string;
2825+
}>();
2826+
2827+
expect(data).toHaveLength(1);
2828+
expect(Number(data[0].timestamp)).toBe(
2829+
Date.parse(`${insertTs(1).replace(' ', 'T')}Z`),
2830+
);
2831+
expect(data[0].value).toBe(2.5);
2832+
expect(data[0].traceId).toBe(traceId);
2833+
});
27892834
});
27902835

27912836
// Formulas on event (log/trace) sources compile inline in the single-scan

‎packages/common-utils/src/__tests__/renderChartConfig.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5971,7 +5971,7 @@ describe('renderMetricExemplarsChartConfig', () => {
59715971
// Surfaces the exemplar columns
59725972
expect(sql).toContain('ARRAY JOIN');
59735973
expect(sql).toContain('`Exemplars.TraceId` AS ex_TraceId');
5974-
expect(sql).toContain('toUnixTimestamp64Milli(ex_TimeUnix)');
5974+
expect(sql).toContain('toUnixTimestamp64Milli(toDateTime64(ex_TimeUnix, 9))');
59755975
// Points at the histogram table and filters by metric name + time
59765976
expect(sql).toContain('otel_metrics_histogram');
59775977
expect(sql).toContain("MetricName = 'http.server.duration'");

‎packages/common-utils/src/core/renderChartConfig.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3688,9 +3688,12 @@ export async function renderMetricExemplarsChartConfig(
36883688
alias: 'bucket',
36893689
});
36903690

3691+
// Exemplars.TimeUnix is Array(DateTime) in the seed schema and the OTel
3692+
// exporter's, Array(DateTime64(9)) elsewhere. toUnixTimestamp64Milli only
3693+
// accepts DateTime64, so the cast is required (and lossless for both).
36913694
return concatChSql(' ', [
36923695
chSql`SELECT
3693-
toUnixTimestamp64Milli(ex_TimeUnix) AS timestamp,
3696+
toUnixTimestamp64Milli(toDateTime64(ex_TimeUnix, 9)) AS timestamp,
36943697
ex_Value AS value,
36953698
ex_TraceId AS traceId,
36963699
ex_SpanId AS spanId,

0 commit comments

Comments
 (0)