Skip to content

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

fix(events): qualify search columns to avoid ambiguous identifier in ClickHouse (#12685)

Valery Meleshkin214b72e8e87b
diff --git a/packages/shared/src/server/queries/clickhouse-sql/search.ts b/packages/shared/src/server/queries/clickhouse-sql/search.ts
index 9fa630c07d..cd7d3c3e2d 100644
--- a/packages/shared/src/server/queries/clickhouse-sql/search.ts
+++ b/packages/shared/src/server/queries/clickhouse-sql/search.ts
@@ -10,9 +10,10 @@ export const clickhouseSearchCondition = (
) => {
const prefix = tablePrefix ? `${tablePrefix}.` : "";
- const cols = searchColumns
- ? searchColumns
- : [`${prefix}id`, `t.user_id`, `${prefix}name`];
+ const defaultCols = [`${prefix}id`, `t.user_id`, `${prefix}name`];
+ const cols = (searchColumns ?? defaultCols).map((col) =>
+ col.includes(".") ? col : `${prefix}${col}`,
+ );
// We use a hard-coded prefix for user_id as it only occurs in the trace context.
const conditions = [
diff --git a/web/src/__tests__/server/clickhouseSearchCondition.servertest.ts b/web/src/__tests__/server/clickhouseSearchCondition.servertest.ts
new file mode 100644
index 0000000000..351fd1cd6c
--- /dev/null
+++ b/web/src/__tests__/server/clickhouseSearchCondition.servertest.ts
@@ -0,0 +1,57 @@
+import { clickhouseSearchCondition } from "@langfuse/shared/src/server";
+
+describe("clickhouseSearchCondition", () => {
+ it("should auto-prefix custom columns with tablePrefix", () => {
+ const result = clickhouseSearchCondition("test", undefined, "e", [
+ "span_id",
+ "name",
+ "user_id",
+ "session_id",
+ "trace_id",
+ ]);
+
+ expect(result.query).toContain("e.span_id ILIKE");
+ expect(result.query).toContain("e.name ILIKE");
+ expect(result.query).toContain("e.user_id ILIKE");
+ expect(result.query).toContain("e.session_id ILIKE");
+ expect(result.query).toContain("e.trace_id ILIKE");
+ expect(result.params).toEqual({ searchString: "%test%" });
+ });
+
+ it("should not double-prefix columns that already have a qualifier", () => {
+ const result = clickhouseSearchCondition("test", undefined, "e", [
+ "t.user_id",
+ "name",
+ ]);
+
+ expect(result.query).toContain("t.user_id ILIKE");
+ expect(result.query).toContain("e.name ILIKE");
+ });
+
+ it("should use default columns with prefix when no custom columns provided", () => {
+ const result = clickhouseSearchCondition("test", undefined, "o");
+
+ expect(result.query).toContain("o.id ILIKE");
+ expect(result.query).toContain("o.name ILIKE");
+ expect(result.query).toContain("t.user_id ILIKE");
+ });
+
+ it("should return empty query and params when no search query provided", () => {
+ const result = clickhouseSearchCondition(undefined, undefined, "e", [
+ "name",
+ ]);
+
+ expect(result.query).toBe("");
+ expect(result.params).toEqual({});
+ });
+
+ it("should include content search when searchType includes content", () => {
+ const result = clickhouseSearchCondition("test", ["id", "content"], "e", [
+ "name",
+ ]);
+
+ expect(result.query).toContain("e.name ILIKE");
+ expect(result.query).toContain("e.input ILIKE");
+ expect(result.query).toContain("e.output ILIKE");
+ });
+});
diff --git a/web/src/__tests__/server/repositories/event-repository.servertest.ts b/web/src/__tests__/server/repositories/event-repository.servertest.ts
index 2e1eb06810..ce3ed49b24 100644
--- a/web/src/__tests__/server/repositories/event-repository.servertest.ts
+++ b/web/src/__tests__/server/repositories/event-repository.servertest.ts
@@ -348,6 +348,71 @@ describe("Clickhouse Events Repository Test", () => {
});
});
+ maybe("Search Query Tests", () => {
+ it("should not throw ambiguous column error when searchQuery is combined with score filter", async () => {
+ const uniqueProjectId = randomUUID();
+ const traceId = randomUUID();
+ const spanId = randomUUID();
+
+ const event = createEvent({
+ id: spanId,
+ span_id: spanId,
+ project_id: uniqueProjectId,
+ trace_id: traceId,
+ type: "SPAN",
+ name: "test-span",
+ });
+
+ 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.
+ const scoreFilter: FilterCondition = {
+ type: "numberObject" as const,
+ column: "scores_avg",
+ operator: ">" as const,
+ key: "Clinical Risk",
+ value: 0,
+ };
+
+ const count = await getObservationsCountFromEventsTable({
+ projectId: uniqueProjectId,
+ filter: [scoreFilter],
+ searchQuery: "test",
+ });
+
+ expect(count).toBeGreaterThanOrEqual(0);
+ });
+
+ it("should not throw ambiguous column error when searchQuery triggers traces JOIN", async () => {
+ const uniqueProjectId = randomUUID();
+ const traceId = randomUUID();
+ const spanId = randomUUID();
+
+ const event = createEvent({
+ id: spanId,
+ span_id: spanId,
+ project_id: uniqueProjectId,
+ trace_id: traceId,
+ type: "SPAN",
+ name: "test-span",
+ });
+
+ await createEventsCh([event]);
+
+ // searchQuery alone triggers a LEFT JOIN with traces CTE which also has a "name" column.
+ const count = await getObservationsCountFromEventsTable({
+ projectId: uniqueProjectId,
+ filter: [],
+ searchQuery: "test",
+ });
+
+ expect(count).toBeGreaterThanOrEqual(0);
+ });
+ });
+
maybe("Filter Tests", () => {
describe("Timestamp Filters", () => {
it("should filter observations by start time with >= operator", async () => {