Skip to content

πŸͺ’ Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

perf(scores): dedup events rows path via GROUP BY + INNER JOIN (#17883)

Niklas Semmlere896d57bb528
diff --git a/packages/shared/src/server/repositories/__snapshots__/scores-selective-seek.golden.test.ts.snap b/packages/shared/src/server/repositories/__snapshots__/scores-selective-seek.golden.test.ts.snap
index 796677a4d3..4697e11a93 100644
--- a/packages/shared/src/server/repositories/__snapshots__/scores-selective-seek.golden.test.ts.snap
+++ b/packages/shared/src/server/repositories/__snapshots__/scores-selective-seek.golden.test.ts.snap
@@ -75,10 +75,10 @@ exports[`scores selective-seek: emitted SQL > full SQL text (one representative
},
"route": "unknown",
"sql": "SELECT
- s.id,
+ s.id AS id,
s.project_id,
s.environment,
- s.name,
+ s.name AS name,
s.value,
s.string_value,
s.timestamp,
@@ -104,25 +104,41 @@ exports[`scores selective-seek: emitted SQL > full SQL text (one representative
length(mapKeys(s.metadata)) > 0 AS has_metadata
FROM
(
- SELECT *
+ SELECT s.*
FROM scores AS s
- WHERE (s.project_id = {param1:String}) AND ((s.project_id, toDate(s.timestamp), s.name, s.id) IN (
- SELECT DISTINCT
+ INNER JOIN
+ (
+ SELECT
+ s.project_id AS latest_project_id,
+ toDate(s.timestamp) AS latest_date,
+ s.name AS latest_name,
+ s.id AS latest_id,
+ max(s.event_ts) AS latest_event_ts
+ FROM scores AS s
+ WHERE (s.project_id = {param1:String}) AND ((s.project_id, toDate(s.timestamp), s.name, s.id) IN (
+ SELECT DISTINCT
+ s.project_id,
+ toDate(s.timestamp),
+ s.name,
+ s.id
+ FROM scores AS s
+ WHERE (s.project_id = {param1:String}) AND (project_id = {param2:String}) AND (s.trace_id = {param3:String})
+ ))
+ GROUP BY
s.project_id,
toDate(s.timestamp),
s.name,
s.id
- FROM scores AS s
- WHERE (s.project_id = {param1:String}) AND (project_id = {param2:String}) AND (s.trace_id = {param3:String})
- ))
- ORDER BY s.event_ts DESC
+ ) AS latest ON (
+ s.project_id = latest.latest_project_id
+ ) AND (toDate(s.timestamp) = latest.latest_date) AND (s.name = latest.latest_name) AND (s.id = latest.latest_id) AND (s.event_ts = latest.latest_event_ts)
+ WHERE (s.data_type IN ({param4:Array(String)})) AND (project_id = {param2:String}) AND (s.trace_id = {param3:String})
LIMIT 1 BY
s.project_id,
toDate(s.timestamp),
s.name,
s.id
) AS s
-WHERE (s.data_type IN ({param4:Array(String)})) AND (project_id = {param2:String}) AND (s.trace_id = {param3:String})
ORDER BY s.timestamp DESC",
},
]
@@ -195,10 +211,10 @@ exports[`scores selective-seek: emitted SQL > full SQL text (one representative
},
"route": "unknown",
"sql": "SELECT
- s.id,
+ s.id AS id,
s.project_id,
s.environment,
- s.name,
+ s.name AS name,
s.value,
s.string_value,
s.timestamp,
@@ -224,17 +240,33 @@ exports[`scores selective-seek: emitted SQL > full SQL text (one representative
length(mapKeys(s.metadata)) > 0 AS has_metadata
FROM
(
- SELECT *
+ SELECT s.*
FROM scores AS s
- WHERE s.project_id = {param1:String}
- ORDER BY s.event_ts DESC
+ INNER JOIN
+ (
+ SELECT
+ s.project_id AS latest_project_id,
+ toDate(s.timestamp) AS latest_date,
+ s.name AS latest_name,
+ s.id AS latest_id,
+ max(s.event_ts) AS latest_event_ts
+ FROM scores AS s
+ WHERE s.project_id = {param1:String}
+ GROUP BY
+ s.project_id,
+ toDate(s.timestamp),
+ s.name,
+ s.id
+ ) AS latest ON (
+ s.project_id = latest.latest_project_id
+ ) AND (toDate(s.timestamp) = latest.latest_date) AND (s.name = latest.latest_name) AND (s.id = latest.latest_id) AND (s.event_ts = latest.latest_event_ts)
+ WHERE (s.data_type IN ({param2:Array(String)})) AND (project_id = {param3:String}) AND (s.value > {param4:Decimal64(12)})
LIMIT 1 BY
s.project_id,
toDate(s.timestamp),
s.name,
s.id
) AS s
-WHERE (s.data_type IN ({param2:Array(String)})) AND (project_id = {param3:String}) AND (s.value > {param4:Decimal64(12)})
ORDER BY s.timestamp DESC",
},
]
diff --git a/packages/shared/src/server/repositories/scores-selective-seek.golden.test.ts b/packages/shared/src/server/repositories/scores-selective-seek.golden.test.ts
index 3eecd8933b..210c4c7eeb 100644
--- a/packages/shared/src/server/repositories/scores-selective-seek.golden.test.ts
+++ b/packages/shared/src/server/repositories/scores-selective-seek.golden.test.ts
@@ -82,6 +82,10 @@ const environmentNoneOf = (values: string[]): FilterState => [
},
];
+const traceNameEq = (value: string): FilterState => [
+ { column: "traceName", type: "string", operator: "=", value },
+];
+
const resetCaptures = () => {
recorder.captured.length = 0;
};
@@ -197,6 +201,18 @@ describeWithFormat("scores selective-seek: emitted SQL", () => {
expect(normalizeCapturedQueries([q])).toMatchSnapshot();
});
});
+
+ // A trace filter joins the traces CTE (alias `e`), which also exposes id and
+ // name. The rows projection must alias s.id / s.name so ClickHouse does not
+ // qualify the output columns (s.id / s.name), which the row mapper cannot read.
+ describe("trace filter joins traces and aliases colliding columns", () => {
+ it("rows: traceName = ", async () => {
+ const q = await captureRowsSql(traceNameEq("root"));
+ expect(q.query).toContain("JOIN traces e");
+ expect(q.query).toContain("s.id AS id");
+ expect(q.query).toContain("s.name AS name");
+ });
+ });
});
// ── Correctness parity vs FINAL over a multi-version fixture ─────────────────
@@ -252,6 +268,10 @@ const SCORES_DDL = `
// c: two day-buckets (distinct dedup groups), trace_id T3
// d: has observation_id O1
// e: value mutates 0.9 -> 0.1 (latest) β€” exercises dedup-then-filter
+// f: two identical rows sharing the SAME max event_ts (a tie) β€” the equality
+// join emits both, so LIMIT 1 BY must collapse them to one row. Divergent
+// ties (tied versions differing in a filtered column) are FINAL-arbitrary by
+// design β€” FINAL's own pick flips with insert order β€” so they are not asserted
const ROWS: Array<
[string, string, string, string, string | null, number, number]
> = [
@@ -263,6 +283,8 @@ const ROWS: Array<
["d", "n3", "2026-01-10 07:00:00.000", "T4", "O1", 0.4, 1],
["e", "n4", "2026-01-10 06:00:00.000", "T5", null, 0.9, 1],
["e", "n4", "2026-01-10 06:05:00.000", "T5", null, 0.1, 2],
+ ["f", "n1", "2026-01-10 05:00:00.000", "T2", null, 0.9, 3],
+ ["f", "n1", "2026-01-10 05:00:00.000", "T2", null, 0.9, 3],
];
const insertRows = () => {
diff --git a/packages/shared/src/server/repositories/scores.ts b/packages/shared/src/server/repositories/scores.ts
index 5bcaaf0283..fe382f076b 100644
--- a/packages/shared/src/server/repositories/scores.ts
+++ b/packages/shared/src/server/repositories/scores.ts
@@ -1604,11 +1604,14 @@ const getScoresUiGenericFromEvents = async <T>(props: {
: `LEFT ANY JOIN traces e ON s.trace_id = e.id`
: "";
+ // id and name are aliased because the traces CTE (joined as `e`) also exposes
+ // id and name: without the alias ClickHouse qualifies the output columns as
+ // s.id / s.name to disambiguate, and the row mapper reads bare id / name.
const rowSelect = `
- s.id,
+ s.id AS id,
s.project_id,
s.environment,
- s.name,
+ s.name AS name,
s.value,
s.string_value,
s.timestamp,
@@ -1682,23 +1685,32 @@ const getScoresUiGenericFromEvents = async <T>(props: {
// ── Dedup without FINAL ───────────────────────────────────────────────────
// Reads dedup the ReplacingMergeTree by reconstructing each score's latest
- // version instead of FINAL: the count path via one argMax pass grouped on the
- // sorting key, the rows path via ORDER BY event_ts DESC + LIMIT 1 BY that same
- // key (keeps the whole latest row).
+ // version instead of FINAL. The count path collapses columns with one argMax
+ // GROUP BY pass. The rows path needs every (incl. wide Map/String) column, so
+ // it INNER JOINs scores back to a GROUP BY subquery holding each key's
+ // max(event_ts), matching on the full sorting key + that event_ts. Only the
+ // latest version has event_ts = max, so the join *is* the dedup: every joined
+ // row is a latest version.
//
// Rule: filter AFTER dedup, never before. value / comment / timestamp /
- // trace_id are all mutable across a score's versions, so filtering raw rows
- // can drop the true-latest version and surface a stale one β€” a result that
- // disagrees with FINAL. Example, filter `value > 0.5`:
- // v1 @10:02 value=0.9, v2 @10:07 value=0.1 (latest)
- // filter-then-dedup -> v1 kept (WRONG)
- // dedup-then-filter -> 0.1 excluded (matches FINAL)
+ // trace_id are mutable across versions, so filtering raw rows can surface a
+ // stale version (filter value>0.5 on v1=0.9β†’v2=0.1 keeps v1; FINAL keeps none).
+ // Filtering the join output honours this β€” filters only ever see latest
+ // versions. The GROUP BY that finds max(event_ts) carries only project scope +
+ // a coarse toDate prune (innerDatePruneQuery) + the seek (when eligible), so
+ // whole buckets are kept/dropped, the latest is never lost pre-dedup, and the
+ // seek scans once.
+ //
+ // Filters run INSIDE the dedup subquery (with the join) so ClickHouse prunes
+ // the wide scan by the real predicates before assembling the full join. The
+ // LIMIT 1 BY there collapses the only remaining duplicates: two raw rows
+ // sharing a key's max event_ts. That tie is two versions with equal version
+ // timestamps, where FINAL itself keeps an arbitrary row (its answer flips with
+ // insert order), so an arbitrary tie pick is FINAL-consistent.
//
- // So every filter and the trace join runs in the OUTER query. The inner scan
- // carries only a coarse toDate(timestamp) prune (see innerDatePruneQuery):
- // whole buckets are kept or dropped, so the latest is never lost pre-dedup.
- // Dedup granularity is the full sorting key, so different toDate(timestamp)
- // buckets of one id stay distinct β€” matching FINAL.
+ // The trace join, ORDER BY, and LIMIT/OFFSET run OUTSIDE the subquery, at one
+ // level, so pagination follows the requested sort (and a traces-column sort can
+ // reference the joined `e`).
const query =
props.select === "count"
? `
@@ -1724,14 +1736,28 @@ const getScoresUiGenericFromEvents = async <T>(props: {
SELECT
${rowSelect}
FROM (
- SELECT *
+ SELECT s.*
FROM scores s
- ${innerScanWhere}
- ORDER BY s.event_ts DESC
+ INNER JOIN (
+ SELECT
+ s.project_id AS latest_project_id,
+ toDate(s.timestamp) AS latest_date,
+ s.name AS latest_name,
+ s.id AS latest_id,
+ max(s.event_ts) AS latest_event_ts
+ FROM scores s
+ ${innerScanWhere}
+ GROUP BY s.project_id, toDate(s.timestamp), s.name, s.id
+ ) latest
+ ON s.project_id = latest.latest_project_id
+ AND toDate(s.timestamp) = latest.latest_date
+ AND s.name = latest.latest_name
+ AND s.id = latest.latest_id
+ AND s.event_ts = latest.latest_event_ts
+ ${outerWhereClause}
LIMIT 1 BY s.project_id, toDate(s.timestamp), s.name, s.id
) s
${eventsJoin}
- ${outerWhereClause}
${orderByToClickhouseSql(orderBy ?? null, scoresTableUiColumnDefinitionsFromEvents)}
${limit !== undefined && offset !== undefined ? `limit {limit: Int32} offset {offset: Int32}` : ""}
`;