Skip to content

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

fix(api): fix single SELECT optimization on count measures (#11236)

Valery Meleshkin5fd4138707ed
diff --git a/web/src/__tests__/queryBuilder.servertest.ts b/web/src/__tests__/queryBuilder.servertest.ts
index 827875e164..cae9715181 100644
--- a/web/src/__tests__/queryBuilder.servertest.ts
+++ b/web/src/__tests__/queryBuilder.servertest.ts
@@ -3667,5 +3667,63 @@ describe("queryBuilder", () => {
expect(resultWithOpt).toHaveLength(1);
expect(resultWithOpt[0].startTimeMonth).toBe("2024-03");
});
+
+ it("should produce correct results with sum aggregation on count measure", async () => {
+ const projectId = randomUUID();
+
+ // Create traces
+ const traces = Array.from({ length: 2 }, (_, i) =>
+ createTrace({
+ project_id: projectId,
+ name: `trace-${i}`,
+ timestamp: new Date().getTime(),
+ }),
+ );
+ await createTracesCh(traces);
+
+ // Create 5 observations: 3 for trace-0, 2 for trace-1
+ const observationsPerTrace = [3, 2];
+ const observations = traces.flatMap((trace, traceIdx) =>
+ Array.from({ length: observationsPerTrace[traceIdx] }, (_, obsIdx) =>
+ createObservation({
+ project_id: projectId,
+ trace_id: trace.id,
+ name: `obs-${traceIdx}-${obsIdx}`,
+ start_time: new Date().getTime(),
+ }),
+ ),
+ );
+ await createObservationsCh(observations);
+
+ // Query with sum on count measure (this was producing incorrect results)
+ const query: QueryType = {
+ view: "observations",
+ dimensions: [],
+ metrics: [{ measure: "count", aggregation: "sum" }],
+ filters: [],
+ timeDimension: null,
+ fromTimestamp: new Date(Date.now() - 86400000).toISOString(),
+ toTimestamp: new Date(Date.now() + 86400000).toISOString(),
+ orderBy: null,
+ };
+
+ // Execute without optimization (two-level query)
+ const resultWithoutOpt = await executeQuery(
+ projectId,
+ query,
+ "v1",
+ false,
+ );
+
+ // Execute with optimization (single-level query)
+ const resultWithOpt = await executeQuery(projectId, query, "v1", true);
+
+ // Both should return sum of counts = 5 (total observations)
+ expect(resultWithoutOpt).toHaveLength(1);
+ expect(resultWithOpt).toHaveLength(1);
+ expect(resultWithoutOpt[0].sum_count).toBe(5);
+ expect(resultWithOpt[0].sum_count).toBe(5);
+ expect(resultWithOpt).toEqual(resultWithoutOpt);
+ });
});
});
diff --git a/web/src/features/query/dataModel.ts b/web/src/features/query/dataModel.ts
index 4b4e3a8519..ad060fc829 100644
--- a/web/src/features/query/dataModel.ts
+++ b/web/src/features/query/dataModel.ts
@@ -432,7 +432,7 @@ export const observationsView: ViewDeclarationType = {
},
measures: {
count: {
- sql: "@@AGG@@(id)",
+ sql: "@@AGG@@(1)",
aggs: { agg: "count" },
alias: "count",
type: "integer",
@@ -1059,7 +1059,7 @@ export const eventsObservationsView: ViewDeclarationType = {
},
measures: {
count: {
- sql: "@@AGG@@(events_observations.span_id)",
+ sql: "@@AGG@@(1)",
aggs: { agg: "count" },
alias: "count",
type: "integer",