Skip to content

Commit 96090b4

Browse files
carderneTrigger.dev RepoOps
authored andcommitted
perf(clickhouse): stage ephemeral event attribute input
Adds a separate ephemeral input for task event attributes and temporarily writes both attribute inputs. Existing materialization behavior remains unchanged while the new input is deployed everywhere. Mono-RevId: af3261bb83d40c1a6333e214f8263f096f7bad13
1 parent e3db7a8 commit 96090b4

3 files changed

Lines changed: 67 additions & 3 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- +goose Up
2+
3+
ALTER TABLE trigger_dev.task_events_v2
4+
ADD COLUMN IF NOT EXISTS attributes_input JSON
5+
EPHEMERAL defaultValueOfTypeName('JSON')
6+
CODEC(ZSTD(1))
7+
AFTER attributes;
8+
9+
-- +goose Down
10+
11+
ALTER TABLE trigger_dev.task_events_v2
12+
DROP COLUMN IF EXISTS attributes_input;

‎internal-packages/clickhouse/src/taskEvents.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,29 @@ describe("task events v2", () => {
112112
expect(indexes).toEqual([]);
113113
}
114114
);
115+
116+
clickhouseTest("adds an ephemeral attributes input", async ({ clickhouseContainer }) => {
117+
const ch = new ClickHouse({ url: clickhouseContainer.getConnectionUrl(), name: "test" });
118+
const readInputColumn = ch.reader.query({
119+
name: "read-task-event-attributes-input-kind",
120+
query: `SELECT default_kind, default_expression
121+
FROM system.columns
122+
WHERE database = 'trigger_dev'
123+
AND table = 'task_events_v2'
124+
AND name = 'attributes_input'`,
125+
schema: z.object({
126+
default_kind: z.string(),
127+
default_expression: z.string(),
128+
}),
129+
});
130+
131+
const [columnError, columns] = await readInputColumn({});
132+
expect(columnError).toBeNull();
133+
expect(columns).toEqual([
134+
{
135+
default_kind: "EPHEMERAL",
136+
default_expression: "defaultValueOfTypeName('JSON')",
137+
},
138+
]);
139+
});
115140
});

‎internal-packages/clickhouse/src/taskEvents.ts‎

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import type { ClickHouseSettings } from "@clickhouse/client";
22
import { z } from "zod";
3-
import type { ClickhouseReader, ClickhouseWriter } from "./client/types.js";
3+
import type {
4+
ClickhouseInsertFunction,
5+
ClickhouseReader,
6+
ClickhouseWriter,
7+
} from "./client/types.js";
48

59
export const TaskEventV1Input = z.object({
610
environment_id: z.string(),
@@ -191,6 +195,7 @@ const TASK_EVENT_V2_INSERT_COLUMNS = [
191195
"kind",
192196
"status",
193197
"attributes",
198+
"attributes_input",
194199
"metadata",
195200
"expires_at",
196201
"machine_id",
@@ -221,8 +226,22 @@ export const TaskEventV2Input = z.object({
221226

222227
export type TaskEventV2Input = z.input<typeof TaskEventV2Input>;
223228

224-
export function insertTaskEventsV2(ch: ClickhouseWriter, settings?: ClickHouseSettings) {
225-
return ch.insertUnsafe<TaskEventV2Input>({
229+
type TaskEventV2DualAttributesInput = TaskEventV2Input & {
230+
attributes_input: unknown;
231+
};
232+
233+
function withAttributesInput(event: TaskEventV2Input): TaskEventV2DualAttributesInput {
234+
return {
235+
...event,
236+
attributes_input: event.attributes,
237+
};
238+
}
239+
240+
export function insertTaskEventsV2(
241+
ch: ClickhouseWriter,
242+
settings?: ClickHouseSettings
243+
): ClickhouseInsertFunction<TaskEventV2Input> {
244+
const insert = ch.insertUnsafe<TaskEventV2DualAttributesInput>({
226245
name: "insertTaskEventsV2",
227246
table: "trigger_dev.task_events_v2",
228247
columns: TASK_EVENT_V2_INSERT_COLUMNS,
@@ -235,6 +254,14 @@ export function insertTaskEventsV2(ch: ClickhouseWriter, settings?: ClickHouseSe
235254
...settings,
236255
},
237256
});
257+
258+
return (events, options) => {
259+
const values = Array.isArray(events)
260+
? events.map(withAttributesInput)
261+
: withAttributesInput(events);
262+
263+
return insert(values, options);
264+
};
238265
}
239266

240267
export function getTraceSummaryQueryBuilderV2(ch: ClickhouseReader, settings?: ClickHouseSettings) {

0 commit comments

Comments
 (0)