Skip to content

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

perf(query): switch to INNER JOIN and add useFinal flag to tableRelations (#12340)

Steffen Schmitzaa2f7568a44a
diff --git a/web/src/__tests__/server/queryBuilder.servertest.ts b/web/src/__tests__/server/queryBuilder.servertest.ts
index 2fa7697056..3059b8b447 100644
--- a/web/src/__tests__/server/queryBuilder.servertest.ts
+++ b/web/src/__tests__/server/queryBuilder.servertest.ts
@@ -2123,8 +2123,8 @@ describe("queryBuilder", () => {
);
// Verify joins included
- expect(compiledQuery).toContain("LEFT JOIN traces");
- expect(compiledQuery).toContain("LEFT JOIN observations");
+ expect(compiledQuery).toContain("INNER JOIN traces");
+ expect(compiledQuery).toContain("INNER JOIN observations");
// Execute query
const result: { data: Array<any> } = { data: [] };
@@ -3435,7 +3435,7 @@ describe("queryBuilder", () => {
expect(compiledQuery.match(/GROUP BY/g)?.length).toBe(2); // Two GROUP BY clauses
// Should have the JOIN
- expect(compiledQuery).toContain("LEFT JOIN scores");
+ expect(compiledQuery).toContain("INNER JOIN scores");
});
it("should handle complex multi-aggregation measure (tokensPerSecond)", async () => {
@@ -4650,4 +4650,83 @@ describe("query builder measure-aggregation validation", () => {
},
);
});
+
+ describe("useFinal flag on events_core joins", () => {
+ it("should omit FINAL for events_core joins in v2 scores-numeric view", async () => {
+ const projectId = randomUUID();
+ const builder = new QueryBuilder(undefined, "v2");
+ const { query: compiledQuery } = await builder.build(
+ {
+ view: "scores-numeric",
+ dimensions: [{ field: "traceName" }],
+ metrics: [{ measure: "count", aggregation: "count" }],
+ filters: [],
+ timeDimension: null,
+ fromTimestamp: new Date(
+ Date.now() - 7 * 24 * 60 * 60 * 1000,
+ ).toISOString(),
+ toTimestamp: new Date(Date.now()).toISOString(),
+ orderBy: null,
+ },
+ projectId,
+ );
+
+ // events_core should be joined without FINAL (useFinal: false)
+ expect(compiledQuery).toContain("JOIN events_core");
+ expect(compiledQuery).not.toContain(
+ "JOIN events_core AS events_traces FINAL",
+ );
+ // scores base CTE should still use FINAL
+ expect(compiledQuery).toContain("scores scores_numeric FINAL");
+ });
+
+ it("should omit FINAL for events_observations join in v2 scores-categorical view", async () => {
+ const projectId = randomUUID();
+ const builder = new QueryBuilder(undefined, "v2");
+ const { query: compiledQuery } = await builder.build(
+ {
+ view: "scores-categorical",
+ dimensions: [{ field: "observationName" }],
+ metrics: [{ measure: "count", aggregation: "count" }],
+ filters: [],
+ timeDimension: null,
+ fromTimestamp: new Date(
+ Date.now() - 7 * 24 * 60 * 60 * 1000,
+ ).toISOString(),
+ toTimestamp: new Date(Date.now()).toISOString(),
+ orderBy: null,
+ },
+ projectId,
+ );
+
+ // events_core should be joined without FINAL (useFinal: false)
+ expect(compiledQuery).toContain("JOIN events_core");
+ expect(compiledQuery).not.toContain(
+ "JOIN events_core AS events_observations FINAL",
+ );
+ });
+
+ it("should keep FINAL for non-events_core joins in v1 scores view", async () => {
+ const projectId = randomUUID();
+ const builder = new QueryBuilder(undefined, "v1");
+ const { query: compiledQuery } = await builder.build(
+ {
+ view: "scores-numeric",
+ dimensions: [{ field: "traceName" }],
+ metrics: [{ measure: "count", aggregation: "count" }],
+ filters: [],
+ timeDimension: null,
+ fromTimestamp: new Date(
+ Date.now() - 7 * 24 * 60 * 60 * 1000,
+ ).toISOString(),
+ toTimestamp: new Date(Date.now()).toISOString(),
+ orderBy: null,
+ },
+ projectId,
+ );
+
+ // v1 traces join should still use FINAL (useFinal defaults to true)
+ expect(compiledQuery).toContain("JOIN traces FINAL");
+ });
+ });
});
diff --git a/web/src/features/query/dataModel.ts b/web/src/features/query/dataModel.ts
index fafac7401f..0f30b1cfff 100644
--- a/web/src/features/query/dataModel.ts
+++ b/web/src/features/query/dataModel.ts
@@ -834,7 +834,12 @@ const createScoreTableRelations = (
version: "v1" | "v2",
): Record<
string,
- { name: string; joinConditionSql: string; timeDimension: string }
+ {
+ name: string;
+ joinConditionSql: string;
+ timeDimension: string;
+ useFinal?: boolean;
+ }
> => {
if (version === "v1") {
return {
@@ -858,12 +863,14 @@ const createScoreTableRelations = (
joinConditionSql:
"ON scores.trace_id = events_traces.trace_id AND scores.project_id = events_traces.project_id AND events_traces.parent_span_id = ''",
timeDimension: "start_time",
+ useFinal: false,
},
events_observations: {
name: "events_core",
joinConditionSql:
"ON scores.project_id = events_observations.project_id AND scores.trace_id = events_observations.trace_id AND scores.observation_id = events_observations.span_id",
timeDimension: "start_time",
+ useFinal: false,
},
};
}
diff --git a/web/src/features/query/server/queryBuilder.ts b/web/src/features/query/server/queryBuilder.ts
index d175bc1613..734a9413a5 100644
--- a/web/src/features/query/server/queryBuilder.ts
+++ b/web/src/features/query/server/queryBuilder.ts
@@ -579,13 +579,13 @@ export class QueryBuilder {
}
const relation = view.tableRelations[relationTableName];
- // Conditionally add FINAL - skip for observations if flag is set
- const shouldUseFinal = !(
- relation.name === "observations" && skipObservationsFinal
- );
+ // Conditionally add FINAL - skip for observations if flag is set, and respect per-relation useFinal
+ const shouldUseFinal =
+ (relation.useFinal ?? true) &&
+ !(relation.name === "observations" && skipObservationsFinal);
const alias =
relation.name !== relationTableName ? ` AS ${relationTableName}` : "";
- let joinStatement = `LEFT JOIN ${relation.name}${alias}${shouldUseFinal ? " FINAL" : ""} ${relation.joinConditionSql}`;
+ let joinStatement = `INNER JOIN ${relation.name}${alias}${shouldUseFinal ? " FINAL" : ""} ${relation.joinConditionSql}`;
// Create time dimension mapping for the relation table
const relationTimeDimensionMapping = {
diff --git a/web/src/features/query/types.ts b/web/src/features/query/types.ts
index bb72e6e75f..0c22cac901 100644
--- a/web/src/features/query/types.ts
+++ b/web/src/features/query/types.ts
@@ -61,6 +61,7 @@ export const viewDeclaration = z.object({
name: z.string(),
joinConditionSql: z.string(),
timeDimension: z.string(),
+ useFinal: z.boolean().optional(),
}),
),
// Segments are used to apply "constant" filters to the query. For example, if we only want one type of observations.