Skip to content

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

fix(filters): route legacy score filters by type (#14991)

Valery Meleshkine8f017460153
diff --git a/packages/shared/src/server/queries/clickhouse-sql/factory.ts b/packages/shared/src/server/queries/clickhouse-sql/factory.ts
index 58533611a7..9a69584399 100644
--- a/packages/shared/src/server/queries/clickhouse-sql/factory.ts
+++ b/packages/shared/src/server/queries/clickhouse-sql/factory.ts
@@ -32,6 +32,42 @@ export class QueryBuilderError extends Error {
}
}
+const LEGACY_SCORE_FILTER_COLUMNS: Partial<
+ Record<EventsTableFilterState[number]["type"], string>
+> = {
+ categoryOptions: "score_categories",
+ numberObject: "scores_avg",
+ booleanObject: "score_booleans",
+};
+
+const resolveLegacyScoreFilterColumn = (
+ filter: EventsTableFilterState[number],
+ columnMapping: UiColumnMappings,
+): string => {
+ if (filter.column.toLowerCase() !== "scores") {
+ return filter.column;
+ }
+
+ const typedColumn = LEGACY_SCORE_FILTER_COLUMNS[filter.type];
+
+ if (!typedColumn) {
+ throw new InvalidRequestError(
+ `Invalid filter type '${filter.type}' for legacy score column '${filter.column}'. Expected one of 'categoryOptions', 'numberObject', or 'booleanObject'.`,
+ );
+ }
+
+ // The legacy scores mapping aliases only the numeric score aggregate.
+ // Categorical and boolean filters must resolve to their typed mappings.
+ if (
+ filter.type === "numberObject" &&
+ !findUiColumnMapping(columnMapping, typedColumn)
+ ) {
+ return filter.column;
+ }
+
+ return typedColumn;
+};
+
// This function ensures that the user only selects valid columns from the clickhouse schema.
// The filter property in this column needs to be zod verified.
// User input for values (e.g. project_id = <value>) are sent to Clickhouse as parameters to prevent SQL injection
@@ -45,8 +81,16 @@ export const createFilterFromFilterState = (
);
return applicableFilters.map((frontEndFilter) => {
+ const filterColumn = resolveLegacyScoreFilterColumn(
+ frontEndFilter,
+ columnMapping,
+ );
// checks if the column exists in the clickhouse schema
- const column = matchAndVerifyTracesUiColumn(frontEndFilter, columnMapping);
+ const column = matchAndVerifyTracesUiColumn(
+ frontEndFilter,
+ columnMapping,
+ filterColumn,
+ );
if (columnDefinitions && frontEndFilter.type !== "null") {
const colDef = columnDefinitions.find((c) => c.id === column.uiTableId);
@@ -199,14 +243,15 @@ const validateEventsTableMatchesFilter = (
const matchAndVerifyTracesUiColumn = (
filter: EventsTableFilterState[number],
uiTableDefinitions: UiColumnMappings,
+ filterColumn = filter.column,
) => {
// tries to match the column name to the clickhouse table name
- const uiTable = findUiColumnMapping(uiTableDefinitions, filter.column);
+ const uiTable = findUiColumnMapping(uiTableDefinitions, filterColumn);
if (!uiTable) {
- const errorMessage = `Column ${filter.column} does not match a UI / CH table mapping.`;
+ const errorMessage = `Column ${filterColumn} does not match a UI / CH table mapping.`;
logger.error(errorMessage, {
- filterColumn: filter.column,
+ filterColumn,
filterType: filter.type,
availableColumns: uiTableDefinitions.map(
(col) => col.uiTableId ?? col.uiTableName,
diff --git a/web/src/__tests__/server/createFilterFromFilterState-filterTypeValidation.servertest.ts b/web/src/__tests__/server/createFilterFromFilterState-filterTypeValidation.servertest.ts
index e21fc185d7..9208865671 100644
--- a/web/src/__tests__/server/createFilterFromFilterState-filterTypeValidation.servertest.ts
+++ b/web/src/__tests__/server/createFilterFromFilterState-filterTypeValidation.servertest.ts
@@ -48,6 +48,24 @@ describe("createFilterFromFilterState filter type validation", () => {
clickhouseTableName: "scores",
clickhouseSelect: "s.score_categories",
},
+ scores: {
+ uiTableName: "Scores",
+ uiTableId: "scores",
+ clickhouseTableName: "scores",
+ clickhouseSelect: "s.scores_avg",
+ },
+ scores_avg: {
+ uiTableName: "Scores (numeric)",
+ uiTableId: "scores_avg",
+ clickhouseTableName: "scores",
+ clickhouseSelect: "s.scores_avg",
+ },
+ score_booleans: {
+ uiTableName: "Scores (boolean)",
+ uiTableId: "score_booleans",
+ clickhouseTableName: "scores",
+ clickhouseSelect: "s.score_booleans",
+ },
eventInput: {
uiTableName: "Input",
uiTableId: "input",
@@ -123,6 +141,18 @@ describe("createFilterFromFilterState filter type validation", () => {
internal: "s.score_categories",
options: [],
},
+ {
+ name: "Scores (numeric)",
+ id: "scores_avg",
+ type: "numberObject",
+ internal: "s.scores_avg",
+ },
+ {
+ name: "Scores (boolean)",
+ id: "score_booleans",
+ type: "booleanObject",
+ internal: "s.score_booleans",
+ },
{
name: "Input",
id: "input",
@@ -229,6 +259,114 @@ describe("createFilterFromFilterState filter type validation", () => {
);
});
+ it.each([
+ {
+ filter: {
+ type: "categoryOptions",
+ operator: "any of",
+ key: "attack_class",
+ value: ["Novel probe"],
+ },
+ expectedColumn: "s.score_categories",
+ },
+ {
+ filter: {
+ type: "numberObject",
+ operator: ">=",
+ key: "quality",
+ value: 0.5,
+ },
+ expectedColumn: "s.scores_avg",
+ },
+ {
+ filter: {
+ type: "booleanObject",
+ operator: "=",
+ key: "approved",
+ value: true,
+ },
+ expectedColumn: "s.score_booleans",
+ },
+ ] as const)(
+ "routes a legacy scores $filter.type filter to $expectedColumn",
+ ({ filter, expectedColumn }) => {
+ const [result] = createFilterFromFilterState(
+ [{ column: "scores", ...filter } as any],
+ Object.values(mappings),
+ columnDefinitions,
+ );
+
+ expect(result.apply().query).toContain(expectedColumn);
+ },
+ );
+
+ it("falls back to the legacy scores mapping when the numeric mapping is unavailable", () => {
+ const filters = [
+ {
+ column: "scores",
+ type: "numberObject",
+ operator: ">=",
+ key: "quality",
+ value: 0.5,
+ },
+ ] satisfies EventsTableFilterState;
+
+ const [result] = createFilterFromFilterState(filters, [mappings.scores]);
+
+ expect(result.apply().query).toContain("s.scores_avg");
+ });
+
+ it.each([
+ {
+ type: "categoryOptions",
+ operator: "any of",
+ key: "attack_class",
+ value: ["Novel probe"],
+ },
+ {
+ type: "booleanObject",
+ operator: "=",
+ key: "approved",
+ value: true,
+ },
+ ] as const)(
+ "rejects a legacy scores $type filter when its typed mapping is unavailable",
+ (filter) => {
+ expect(() =>
+ createFilterFromFilterState(
+ [{ column: "scores", ...filter } as any],
+ [mappings.scores],
+ columnDefinitions,
+ ),
+ ).toThrow(InvalidRequestError);
+ },
+ );
+
+ it.each([
+ {
+ type: "stringOptions",
+ operator: "any of",
+ value: ["quality:good"],
+ },
+ {
+ type: "null",
+ operator: "is null",
+ value: "",
+ },
+ ] as const)("rejects an unsupported legacy scores $type filter", (filter) => {
+ expect(() =>
+ createFilterFromFilterState(
+ [{ column: "scores", ...filter } as any],
+ Object.values(mappings),
+ columnDefinitions,
+ ),
+ ).toThrow(
+ new InvalidRequestError(
+ `Invalid filter type '${filter.type}' for legacy score column 'scores'. Expected one of 'categoryOptions', 'numberObject', or 'booleanObject'.`,
+ ),
+ );
+ });
+
it.each([
{
scenario: "matching filter type",