From 4564541ef103aa7c20bb3ebc000b78215caece49 Mon Sep 17 00:00:00 2001 From: Achlesha Varshney <89573471+Achlesha@users.noreply.github.com> Date: Mon, 23 Nov 2026 16:10:00 +0530 Subject: [PATCH 1/3] fix(common-utils): cast Exemplars.TimeUnix before toUnixTimestamp64Milli Exemplars.TimeUnix is Array(DateTime) in the seed schema and the OTel ClickHouse exporter; toUnixTimestamp64Milli only accepts DateTime64. --- packages/common-utils/src/core/renderChartConfig.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/common-utils/src/core/renderChartConfig.ts b/packages/common-utils/src/core/renderChartConfig.ts index a343efb7d4..6f90a0eeed 100644 --- a/packages/common-utils/src/core/renderChartConfig.ts +++ b/packages/common-utils/src/core/renderChartConfig.ts @@ -3690,7 +3690,7 @@ export async function renderMetricExemplarsChartConfig( return concatChSql(' ', [ chSql`SELECT - toUnixTimestamp64Milli(ex_TimeUnix) AS timestamp, + toUnixTimestamp64Milli(toDateTime64(ex_TimeUnix, 9)) AS timestamp, ex_Value AS value, ex_TraceId AS traceId, ex_SpanId AS spanId, From 882cd3139d935a41152e857f5b727f7252833330 Mon Sep 17 00:00:00 2001 From: Achlesha Varshney <89573471+Achlesha@users.noreply.github.com> Date: Mon, 23 Nov 2026 16:12:54 +0530 Subject: [PATCH 2/3] test(common-utils): expect the DateTime64 cast in the exemplar scan test(common-utils): expect the DateTime64 cast in the exemplar scan --- packages/common-utils/src/__tests__/renderChartConfig.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/common-utils/src/__tests__/renderChartConfig.test.ts b/packages/common-utils/src/__tests__/renderChartConfig.test.ts index ee56defe74..5675ddd441 100644 --- a/packages/common-utils/src/__tests__/renderChartConfig.test.ts +++ b/packages/common-utils/src/__tests__/renderChartConfig.test.ts @@ -5971,7 +5971,7 @@ describe('renderMetricExemplarsChartConfig', () => { // Surfaces the exemplar columns expect(sql).toContain('ARRAY JOIN'); expect(sql).toContain('`Exemplars.TraceId` AS ex_TraceId'); - expect(sql).toContain('toUnixTimestamp64Milli(ex_TimeUnix)'); + expect(sql).toContain('toUnixTimestamp64Milli(toDateTime64(ex_TimeUnix, 9))'); // Points at the histogram table and filters by metric name + time expect(sql).toContain('otel_metrics_histogram'); expect(sql).toContain("MetricName = 'http.server.duration'"); From 4e114fcd0dbf224c18580b163943a670c82b09fa Mon Sep 17 00:00:00 2001 From: Achlesha Varshney <89573471+Achlesha@users.noreply.github.com> Date: Mon, 23 Nov 2026 16:55:56 +0530 Subject: [PATCH 3/3] test(common-utils): run the exemplar scan against Array(DateTime) Exemplars.TimeUnix --- .../__tests__/queryChartConfig.int.test.ts | 45 +++++++++++++++++++ .../src/core/renderChartConfig.ts | 3 ++ 2 files changed, 48 insertions(+) diff --git a/packages/common-utils/src/__tests__/queryChartConfig.int.test.ts b/packages/common-utils/src/__tests__/queryChartConfig.int.test.ts index 44cd80248b..f5d3d83c3b 100644 --- a/packages/common-utils/src/__tests__/queryChartConfig.int.test.ts +++ b/packages/common-utils/src/__tests__/queryChartConfig.int.test.ts @@ -4,6 +4,7 @@ import { ClickHouseClient } from '@clickhouse/client'; import { convertCHDataTypeToJSType, JSDataType } from '@/clickhouse'; import { ClickhouseClient as HdxClickhouseClient } from '@/clickhouse/node'; import { Metadata, MetadataCache } from '@/core/metadata'; +import { renderMetricExemplarsChartConfig } from '@/core/renderChartConfig'; import { convertToCategoricalChartConfig } from '@/core/utils'; import { ChartConfigWithOptDateRange, @@ -2786,6 +2787,50 @@ describe('queryChartConfig Integration Tests', () => { ).rejects.toThrow(); }); }); + // HIST_TABLE mirrors the seed schema, where Exemplars.TimeUnix is + // Array(DateTime). toUnixTimestamp64Milli only accepts DateTime64, so the + // scan must cast it; this runs the generated SQL against that column type. + it('reads exemplars from an Array(DateTime) Exemplars.TimeUnix column', async () => { + const traceId = '0af7651916cd43dd8448eb211c80319c'; + await client.insert({ + table: `${DATABASE}.${HIST_TABLE}`, + values: [ + { + ...histRow('exemplar.latency', insertTs(1), 'svc-a', [3, 1], [1]), + 'Exemplars.FilteredAttributes': [{}], + 'Exemplars.TimeUnix': [insertTs(1)], + 'Exemplars.Value': [2.5], + 'Exemplars.SpanId': ['b7ad6b7169203331'], + 'Exemplars.TraceId': [traceId], + }, + ], + format: 'JSONEachRow', + }); + + const exemplarSql = await renderMetricExemplarsChartConfig( + baseConfig({ select: [histQuantileSelect('exemplar.latency', 0.99)] }), + metadata, + ); + expect(exemplarSql).not.toBeNull(); + + const resp = await hdxClient.query({ + query: exemplarSql!.sql, + query_params: exemplarSql!.params, + format: 'JSON', + }); + const { data } = await resp.json<{ + timestamp: string; + value: number; + traceId: string; + }>(); + + expect(data).toHaveLength(1); + expect(Number(data[0].timestamp)).toBe( + Date.parse(`${insertTs(1).replace(' ', 'T')}Z`), + ); + expect(data[0].value).toBe(2.5); + expect(data[0].traceId).toBe(traceId); + }); }); // Formulas on event (log/trace) sources compile inline in the single-scan diff --git a/packages/common-utils/src/core/renderChartConfig.ts b/packages/common-utils/src/core/renderChartConfig.ts index 6f90a0eeed..681ce4176f 100644 --- a/packages/common-utils/src/core/renderChartConfig.ts +++ b/packages/common-utils/src/core/renderChartConfig.ts @@ -3688,6 +3688,9 @@ export async function renderMetricExemplarsChartConfig( alias: 'bucket', }); + // Exemplars.TimeUnix is Array(DateTime) in the seed schema and the OTel + // exporter's, Array(DateTime64(9)) elsewhere. toUnixTimestamp64Milli only + // accepts DateTime64, so the cast is required (and lossless for both). return concatChSql(' ', [ chSql`SELECT toUnixTimestamp64Milli(toDateTime64(ex_TimeUnix, 9)) AS timestamp,