🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
perf(events): fold io-lane start_time bounds into one cached scalar (#17546)
Niklas Semmlerea0373f104ff
diff --git a/packages/shared/src/server/queries/clickhouse-sql/event-query-builder.test.ts b/packages/shared/src/server/queries/clickhouse-sql/event-query-builder.test.tsindex cd83d07f84..f64a6dc46e 100644--- a/packages/shared/src/server/queries/clickhouse-sql/event-query-builder.test.ts+++ b/packages/shared/src/server/queries/clickhouse-sql/event-query-builder.test.ts@@ -207,14 +207,14 @@ describe("buildEventsFullTableSplitQuery", () => {// The bound is derived from base (no re-serialized params) so events_full// can prune partitions/primary key; the semi-join stays for join exactness.+ // Both bounds read one byte-identical (min, max) scalar subquery so+ // ClickHouse's scalar cache evaluates base's bounds pass once, not twice.+ expect(query).not.toContain("io_bounds");expect(query).toContain(- "SELECT min(start_time) AS io_min_start_time, max(start_time) AS io_max_start_time FROM base",+ "AND e.start_time >= (SELECT (min(start_time), max(start_time)) FROM base).1",);expect(query).toContain(- "AND e.start_time >= (SELECT io_min_start_time FROM io_bounds)",- );- expect(query).toContain(- "AND e.start_time <= (SELECT io_max_start_time FROM io_bounds)",+ "AND e.start_time <= (SELECT (min(start_time), max(start_time)) FROM base).2",);expect(query).toContain('AND (e.start_time, e.trace_id, e.span_id) IN (SELECT "start_time", "trace_id", id FROM base)',diff --git a/packages/shared/src/server/queries/clickhouse-sql/event-query-builder.ts b/packages/shared/src/server/queries/clickhouse-sql/event-query-builder.tsindex 35e2f51790..b62dbf5643 100644--- a/packages/shared/src/server/queries/clickhouse-sql/event-query-builder.ts+++ b/packages/shared/src/server/queries/clickhouse-sql/event-query-builder.ts@@ -2153,16 +2153,20 @@ export function buildEventsFullTableSplitQuery(opts: {// bound start_time to base's own matched range. Derived from base (not the// request filter) so it also tightens lookups that arrive without a time// filter, and the values are never re-serialized as params.+ //+ // Both bounds read one byte-identical (min, max) scalar subquery over base,+ // with .1/.2 applied outside. ClickHouse's scalar cache keys on subquery+ // text, so the identical text is evaluated once and base is scanned once for+ // both bounds.+ const ioBounds = "(SELECT (min(start_time), max(start_time)) FROM base)";const ioQuery = [`SELECT ${ioSelectParts.join(", ")}`,"FROM events_full e","WHERE e.project_id = {projectId: String}",- "AND e.start_time >= (SELECT io_min_start_time FROM io_bounds)",- "AND e.start_time <= (SELECT io_max_start_time FROM io_bounds)",+ `AND e.start_time >= ${ioBounds}.1`,+ `AND e.start_time <= ${ioBounds}.2`,'AND (e.start_time, e.trace_id, e.span_id) IN (SELECT "start_time", "trace_id", id FROM base)',].join("\n");- const ioBoundsQuery =- "SELECT min(start_time) AS io_min_start_time, max(start_time) AS io_max_start_time FROM base";// Compose final query using CTEQueryBuilderlet cteBuilder = new CTEQueryBuilder();@@ -2175,19 +2179,12 @@ export function buildEventsFullTableSplitQuery(opts: {});}- // Register base, io_bounds, and io CTEs, set up FROM and JOIN. io_bounds must- // follow base (it aggregates over it) and precede io (which reads from it).cteBuilder = cteBuilder.withCTE("base", {query: baseQuery,params: baseParams,schema: [] as string[],})- .withCTE("io_bounds", {- query: ioBoundsQuery,- params: {},- schema: [] as string[],- }).withCTE("io", {query: ioQuery,params: { projectId: opts.projectId },