Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

chore: remove dead traces CTE join from observations query (#12727)

Valery Meleshkin896896058169
diff --git a/packages/shared/src/eventsTable.ts b/packages/shared/src/eventsTable.ts
index 0f7ff71b6d..7b06e80d24 100644
--- a/packages/shared/src/eventsTable.ts
+++ b/packages/shared/src/eventsTable.ts
@@ -221,7 +221,7 @@ export const eventsTableCols: ColumnDefinition[] = [
name: "Trace Tags",
id: "traceTags",
type: "arrayOptions",
- internal: "t.tags",
+ internal: "e.tags",
options: [], // to be added at runtime
},
{
diff --git a/packages/shared/src/server/queries/clickhouse-sql/search.ts b/packages/shared/src/server/queries/clickhouse-sql/search.ts
index cd7d3c3e2d..c74da012f8 100644
--- a/packages/shared/src/server/queries/clickhouse-sql/search.ts
+++ b/packages/shared/src/server/queries/clickhouse-sql/search.ts
@@ -15,7 +15,7 @@ export const clickhouseSearchCondition = (
col.includes(".") ? col : `${prefix}${col}`,
);
- // We use a hard-coded prefix for user_id as it only occurs in the trace context.
+ // The default cols include t.user_id for callers querying via traces CTE (traces.ts, observations.ts).
const conditions = [
!searchType || searchType.includes("id")
? cols.map((col) => `${col} ILIKE {searchString: String}`).join(" OR ")
diff --git a/packages/shared/src/server/repositories/events.ts b/packages/shared/src/server/repositories/events.ts
index f2a3cfff2f..66cf92239f 100644
--- a/packages/shared/src/server/repositories/events.ts
+++ b/packages/shared/src/server/repositories/events.ts
@@ -449,11 +449,6 @@ async function getObservationsFromEventsTableInternal<T>(
["span_id", "name", "user_id", "session_id", "trace_id"],
);
- // Query optimization: joining traces onto observations is expensive.
- // Only join if search query requires it.
- // TODO further optimize by checking if specific trace fields are filtered on.
- const needsTraceJoin = search.query;
-
const orderByEntries = orderByToEntries(
[orderBy ?? null],
eventsTableUiColumnDefinitions,
@@ -535,18 +530,6 @@ async function getObservationsFromEventsTableInternal<T>(
}),
),
)
- .when(Boolean(needsTraceJoin), (b) =>
- b.withCTE(
- "traces",
- eventsTracesAggregation({ projectId, startTimeFrom }).buildWithParams(),
- ),
- )
- .when(Boolean(needsTraceJoin), (b) =>
- b.leftJoin(
- "traces t",
- "ON t.id = e.trace_id AND t.project_id = e.project_id",
- ),
- )
.when(hasObservationScoresFilter, (b) =>
b.leftJoin("scores_agg AS s", "ON s.observation_id = e.span_id"),
)
diff --git a/web/src/__tests__/server/repositories/event-repository.servertest.ts b/web/src/__tests__/server/repositories/event-repository.servertest.ts
index ce3ed49b24..7dd014494b 100644
--- a/web/src/__tests__/server/repositories/event-repository.servertest.ts
+++ b/web/src/__tests__/server/repositories/event-repository.servertest.ts
@@ -349,7 +349,7 @@ describe("Clickhouse Events Repository Test", () => {
});
maybe("Search Query Tests", () => {
- it("should not throw ambiguous column error when searchQuery is combined with score filter", async () => {
+ it("should not throw when searchQuery is combined with score filter", async () => {
const uniqueProjectId = randomUUID();
const traceId = randomUUID();
const spanId = randomUUID();
@@ -365,10 +365,8 @@ describe("Clickhouse Events Repository Test", () => {
await createEventsCh([event]);
- // searchQuery triggers a LEFT JOIN with traces CTE (which has a "name" column),
- // and the score filter triggers a LEFT JOIN with scores_agg (which also has "name").
- // With unqualified "name" in the search WHERE clause, ClickHouse raises
- // "ambiguous identifier 'name'" because 3 tables define it: e, t, and s.
+ // Verify that search columns qualified with e.* prefix do not conflict
+ // with the scores_agg LEFT JOIN when both are present.
const scoreFilter: FilterCondition = {
type: "numberObject" as const,
column: "scores_avg",
@@ -386,7 +384,7 @@ describe("Clickhouse Events Repository Test", () => {
expect(count).toBeGreaterThanOrEqual(0);
});
- it("should not throw ambiguous column error when searchQuery triggers traces JOIN", async () => {
+ it("should not throw when searchQuery is provided without filters", async () => {
const uniqueProjectId = randomUUID();
const traceId = randomUUID();
const spanId = randomUUID();
@@ -402,7 +400,7 @@ describe("Clickhouse Events Repository Test", () => {
await createEventsCh([event]);
- // searchQuery alone triggers a LEFT JOIN with traces CTE which also has a "name" column.
+ // Verify basic search with qualified e.* columns works without errors.
const count = await getObservationsCountFromEventsTable({
projectId: uniqueProjectId,
filter: [],