|
| 1 | +import { clickhouseTest } from "@internal/testcontainers"; |
| 2 | +import { z } from "zod"; |
| 3 | +import { ClickHouse } from "./index.js"; |
| 4 | + |
| 5 | +function clickhouseDate(value: Date) { |
| 6 | + return value.toISOString().replace("T", " ").replace("Z", ""); |
| 7 | +} |
| 8 | + |
| 9 | +describe("task events v2", () => { |
| 10 | + clickhouseTest( |
| 11 | + "stores materialized attributes from the ephemeral JSON input", |
| 12 | + async ({ clickhouseContainer }) => { |
| 13 | + const ch = new ClickHouse({ url: clickhouseContainer.getConnectionUrl(), name: "test" }); |
| 14 | + const now = new Date("2026-09-01T10:00:00.000Z"); |
| 15 | + const spanId = "span_ephemeral_attributes"; |
| 16 | + |
| 17 | + const [insertError] = await ch.taskEventsV2.insert([ |
| 18 | + { |
| 19 | + environment_id: "env_ephemeral_attributes", |
| 20 | + organization_id: "org_ephemeral_attributes", |
| 21 | + project_id: "project_ephemeral_attributes", |
| 22 | + task_identifier: "ephemeral-attributes", |
| 23 | + run_id: "run_ephemeral_attributes", |
| 24 | + start_time: clickhouseDate(now), |
| 25 | + duration: "1000000", |
| 26 | + trace_id: "trace_ephemeral_attributes", |
| 27 | + span_id: spanId, |
| 28 | + parent_span_id: "", |
| 29 | + message: "Ephemeral attributes", |
| 30 | + kind: "SPAN", |
| 31 | + status: "OK", |
| 32 | + attributes: { |
| 33 | + z: 1, |
| 34 | + a: "hello", |
| 35 | + nested: { enabled: true }, |
| 36 | + }, |
| 37 | + metadata: "{}", |
| 38 | + expires_at: clickhouseDate(new Date(now.getTime() + 90 * 24 * 60 * 60 * 1000)), |
| 39 | + }, |
| 40 | + ]); |
| 41 | + expect(insertError).toBeNull(); |
| 42 | + |
| 43 | + const readAttributes = ch.reader.query({ |
| 44 | + name: "read-ephemeral-task-event-attributes", |
| 45 | + query: `SELECT attributes_text, |
| 46 | + toUInt8(inserted_at > toDateTime64('2020-01-01 00:00:00', 3)) AS has_inserted_at |
| 47 | + FROM trigger_dev.task_events_v2 |
| 48 | + WHERE environment_id = {environmentId: String} |
| 49 | + AND span_id = {spanId: String}`, |
| 50 | + params: z.object({ environmentId: z.string(), spanId: z.string() }), |
| 51 | + schema: z.object({ attributes_text: z.string(), has_inserted_at: z.number() }), |
| 52 | + }); |
| 53 | + const [readError, rows] = await readAttributes({ |
| 54 | + environmentId: "env_ephemeral_attributes", |
| 55 | + spanId, |
| 56 | + }); |
| 57 | + expect(readError).toBeNull(); |
| 58 | + expect(rows).toEqual([ |
| 59 | + { |
| 60 | + attributes_text: '{"a":"hello","nested":{"enabled":true},"z":1}', |
| 61 | + has_inserted_at: 1, |
| 62 | + }, |
| 63 | + ]); |
| 64 | + |
| 65 | + const readColumnKinds = ch.reader.query({ |
| 66 | + name: "read-task-event-attribute-column-kinds", |
| 67 | + query: `SELECT name, default_kind, default_expression |
| 68 | + FROM system.columns |
| 69 | + WHERE database = 'trigger_dev' |
| 70 | + AND table = 'task_events_v2' |
| 71 | + AND name IN ('attributes', 'attributes_text') |
| 72 | + ORDER BY name`, |
| 73 | + schema: z.object({ |
| 74 | + name: z.string(), |
| 75 | + default_kind: z.string(), |
| 76 | + default_expression: z.string(), |
| 77 | + }), |
| 78 | + }); |
| 79 | + const [columnError, columns] = await readColumnKinds({}); |
| 80 | + expect(columnError).toBeNull(); |
| 81 | + expect(columns).toEqual([ |
| 82 | + { |
| 83 | + name: "attributes", |
| 84 | + default_kind: "EPHEMERAL", |
| 85 | + default_expression: "defaultValueOfTypeName('JSON')", |
| 86 | + }, |
| 87 | + { |
| 88 | + name: "attributes_text", |
| 89 | + default_kind: "MATERIALIZED", |
| 90 | + default_expression: "toJSONString(attributes)", |
| 91 | + }, |
| 92 | + ]); |
| 93 | + |
| 94 | + const readRemovedIndexes = ch.reader.query({ |
| 95 | + name: "read-removed-task-event-text-indexes", |
| 96 | + query: `SELECT name |
| 97 | + FROM system.data_skipping_indices |
| 98 | + WHERE database = 'trigger_dev' |
| 99 | + AND table = 'task_events_v2' |
| 100 | + AND name IN ('idx_attributes_text_search', 'idx_message_text_search') |
| 101 | + ORDER BY name`, |
| 102 | + schema: z.object({ name: z.string() }), |
| 103 | + }); |
| 104 | + const [indexError, indexes] = await readRemovedIndexes({}); |
| 105 | + expect(indexError).toBeNull(); |
| 106 | + expect(indexes).toEqual([]); |
| 107 | + } |
| 108 | + ); |
| 109 | +}); |
0 commit comments