Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

feat: cast filter values

Carl-Gerhard Lindesvärd577a316ae273
diff --git a/apps/start/src/components/report/sidebar/ReportSeriesItem.tsx b/apps/start/src/components/report/sidebar/ReportSeriesItem.tsx
index 57bbf5584..6f8a22115 100644
--- a/apps/start/src/components/report/sidebar/ReportSeriesItem.tsx
+++ b/apps/start/src/components/report/sidebar/ReportSeriesItem.tsx
@@ -101,6 +101,7 @@ export function ReportSeriesItem({
name: action.value,
operator: 'is',
value: [],
+ type: 'string',
},
],
}),
diff --git a/apps/start/src/components/report/sidebar/filters/FilterItem.tsx b/apps/start/src/components/report/sidebar/filters/FilterItem.tsx
index 03e769f48..1f67d7805 100644
--- a/apps/start/src/components/report/sidebar/filters/FilterItem.tsx
+++ b/apps/start/src/components/report/sidebar/filters/FilterItem.tsx
@@ -1,5 +1,6 @@
import { ColorSquare } from '@/components/color-square';
import { FilterOperatorSelect } from '@/components/report/sidebar/filters/FilterOperatorSelect';
+import { FilterTypeSelect } from '@/components/report/sidebar/filters/FilterTypeSelect';
import { RenderDots } from '@/components/ui/RenderDots';
import { Button } from '@/components/ui/button';
import { Combobox } from '@/components/ui/combobox';
@@ -10,16 +11,43 @@ import { useAppParams } from '@/hooks/use-app-params';
import { useEventNames } from '@/hooks/use-event-names';
import { usePropertyValues } from '@/hooks/use-property-values';
import { useDispatch } from '@/redux';
+import { getOperatorsForType } from '@openpanel/constants';
import type {
IChartEvent,
IChartEventFilter,
IChartEventFilterOperator,
IChartEventFilterValue,
+ IChartFilterValueType,
} from '@openpanel/validation';
import { SlidersHorizontal, Trash } from 'lucide-react';
import { changeEvent } from '../../reportSlice';
+// Client-side sanity check: can this raw value possibly match the chosen cast
+// type? Mirrors the SQL casts in packages/db filter-cast.ts. Returns an error
+// message to show inline, or undefined when valid (or empty / untyped).
+function validateFilterValue(
+ value: string,
+ type: IChartFilterValueType | undefined,
+): string | undefined {
+ if (!value) {
+ return undefined;
+ }
+ switch (type) {
+ case 'number':
+ return Number.isFinite(Number(value)) ? undefined : 'Not a valid number';
+ case 'date':
+ case 'datetime':
+ return Number.isNaN(Date.parse(value)) ? 'Not a valid date' : undefined;
+ case 'boolean':
+ return value === 'true' || value === 'false'
+ ? undefined
+ : 'Use "true" or "false"';
+ default:
+ return undefined;
+ }
+}
+
interface FilterProps {
event: IChartEvent;
filter: IChartEventFilter;
@@ -37,6 +65,12 @@ interface PureFilterProps {
operator: IChartEventFilterOperator,
filter: IChartEventFilter,
) => void;
+ // Optional: surfaces the cast-type select. Callers that don't pass it (the
+ // overview/table/cohort modals) simply don't render the control.
+ onChangeType?: (
+ type: IChartFilterValueType,
+ filter: IChartEventFilter,
+ ) => void;
className?: string;
immediateInput?: boolean;
}
@@ -97,6 +131,45 @@ export function FilterItem({ filter, event }: FilterProps) {
);
};
+ const onChangeType = (
+ type: IChartFilterValueType,
+ { id }: IChartEventFilter,
+ ) => {
+ dispatch(
+ changeEvent({
+ ...event,
+ type: 'event',
+ filters: event.filters.map((item) => {
+ if (item.id !== id) {
+ return item;
+ }
+
+ // Keep the current operator if it's still valid for the new type,
+ // otherwise fall back to the first allowed one (and trim the value
+ // like onChangeOperator does, since the input shape may change).
+ const allowed = getOperatorsForType(type);
+ const operator = (allowed as readonly string[]).includes(
+ item.operator,
+ )
+ ? item.operator
+ : allowed[0]!;
+
+ return {
+ ...item,
+ type,
+ operator,
+ value:
+ operator === item.operator
+ ? item.value
+ : item.value
+ ? item.value.filter(Boolean).slice(0, 1)
+ : [],
+ };
+ }),
+ }),
+ );
+ };
+
const dispatch = useDispatch();
return (
<PureFilterItem
@@ -105,6 +178,7 @@ export function FilterItem({ filter, event }: FilterProps) {
onRemove={onRemove}
onChangeValue={onChangeValue}
onChangeOperator={onChangeOperator}
+ onChangeType={onChangeType}
className="px-4 py-2 shadow-[inset_6px_0_0] shadow-def-300 first:border-t"
/>
);
@@ -121,6 +195,7 @@ export function PureFilterItem({
onRemove,
onChangeValue,
onChangeOperator,
+ onChangeType,
className,
immediateInput,
}: PureFilterProps) {
@@ -128,6 +203,23 @@ export function PureFilterItem({
const isBooleanSessionFilter = filter.name === 'session.is_bounce';
const isPerformedEventFilter = filter.name === 'session.performed_event';
+ // The session.* filters above have a fixed shape (yes/no, event select), so
+ // the cast-type control doesn't apply to them. Also requires the caller to
+ // opt in by passing onChangeType.
+ const showTypeSelect =
+ !!onChangeType && !isBooleanSessionFilter && !isPerformedEventFilter;
+ // Only the free-text input path (gt/gte/lt/lte) carries a single typed value
+ // we can validate inline; is/isNot use a multi-value combobox.
+ const usesTypedInput =
+ showTypeSelect &&
+ filter.operator !== 'is' &&
+ filter.operator !== 'isNot';
+ const valueError = usesTypedInput
+ ? validateFilterValue(
+ filter.value[0] != null ? String(filter.value[0]) : '',
+ filter.type,
+ )
+ : undefined;
const potentialValues = usePropertyValues({
event: eventName,
@@ -163,6 +255,10 @@ export function PureFilterItem({
onChangeOperator(operator, filter);
};
+ const changeFilterType = (type: IChartFilterValueType) => {
+ onChangeType?.(type, filter);
+ };
+
const renderValueControl = () => {
if (isBooleanSessionFilter) {
return (
@@ -208,6 +304,7 @@ export function PureFilterItem({
value={filter.value[0] ? String(filter.value[0]) : ''}
onChangeValue={(value) => changeFilterValue([value])}
immediate={immediateInput}
+ error={valueError}
/>
);
};
@@ -226,12 +323,22 @@ export function PureFilterItem({
</Button>
</div>
<div className="flex gap-1">
+ {showTypeSelect && (
+ <FilterTypeSelect
+ value={filter.type}
+ onChange={changeFilterType}
+ />
+ )}
<FilterOperatorSelect
value={filter.operator}
onChange={changeFilterOperator}
+ type={filter.type}
/>
{renderValueControl()}
</div>
+ {valueError && (
+ <p className="mt-1 text-destructive text-xs">{valueError}</p>
+ )}
</div>
);
}
diff --git a/apps/start/src/components/report/sidebar/filters/FilterOperatorSelect.tsx b/apps/start/src/components/report/sidebar/filters/FilterOperatorSelect.tsx
index 39a17dd9b..d0bdb58e4 100644
--- a/apps/start/src/components/report/sidebar/filters/FilterOperatorSelect.tsx
+++ b/apps/start/src/components/report/sidebar/filters/FilterOperatorSelect.tsx
@@ -1,36 +1,47 @@
import { Button } from '@/components/ui/button';
import { DropdownMenuComposed } from '@/components/ui/dropdown-menu';
-import { operators } from '@openpanel/constants';
-import type { IChartEventFilterOperator } from '@openpanel/validation';
-import { mapKeys } from '@openpanel/validation';
+import {
+ getOperatorsForType,
+ operators,
+ operatorsShort,
+} from '@openpanel/constants';
+import type {
+ IChartEventFilterOperator,
+ IChartFilterValueType,
+} from '@openpanel/validation';
interface FilterOperatorSelectProps {
value: IChartEventFilterOperator;
onChange: (operator: IChartEventFilterOperator) => void;
+ // The filter's declared value type. Constrains which operators are offered
+ // (e.g. a Number can't `contains`, a Boolean only `is`/`isNot`).
+ type?: IChartFilterValueType;
children?: React.ReactNode;
}
export function FilterOperatorSelect({
value,
onChange,
+ type,
children,
}: FilterOperatorSelectProps) {
const trigger = children ?? (
<Button variant="outline" className="whitespace-nowrap">
- {operators[value]}
+ {operatorsShort[value]}
</Button>
);
return (
<DropdownMenuComposed
onChange={onChange}
- items={mapKeys(operators)
- // Cohort operators are surfaced via CohortFilterItem, not here.
- .filter((key) => key !== 'inCohort' && key !== 'notInCohort')
- .map((key) => ({
- value: key,
- label: operators[key],
- }))}
+ items={getOperatorsForType(type).map((key) => ({
+ value: key,
+ label: operatorsShort[key],
+ // Only show the descriptive sub-line when it adds info beyond the
+ // short label (i.e. the symbol operators).
+ description:
+ operatorsShort[key] === operators[key] ? undefined : operators[key],
+ }))}
label="Operator"
>
{trigger}
diff --git a/apps/start/src/components/report/sidebar/filters/FilterTypeSelect.tsx b/apps/start/src/components/report/sidebar/filters/FilterTypeSelect.tsx
new file mode 100644
index 000000000..63b2e257c
--- /dev/null
+++ b/apps/start/src/components/report/sidebar/filters/FilterTypeSelect.tsx
@@ -0,0 +1,39 @@
+import { Button } from '@/components/ui/button';
+import { DropdownMenuComposed } from '@/components/ui/dropdown-menu';
+import { filterValueTypes } from '@openpanel/constants';
+import type { IChartFilterValueType } from '@openpanel/validation';
+import { mapKeys } from '@openpanel/validation';
+
+interface FilterTypeSelectProps {
+ value: IChartFilterValueType | undefined;
+ onChange: (type: IChartFilterValueType) => void;
+ children?: React.ReactNode;
+}
+
+// Cast type for the filter value/column. Drives which operators are available
+// (via getOperatorsForType) and how the value/column are cast in SQL. Defaults
+// to the "Text" label when unset (legacy filters).
+export function FilterTypeSelect({
+ value,
+ onChange,
+ children,
+}: FilterTypeSelectProps) {
+ const trigger = children ?? (
+ <Button variant="outline" className="whitespace-nowrap">
+ {filterValueTypes[value ?? 'string']}
+ </Button>
+ );
+
+ return (
+ <DropdownMenuComposed
+ onChange={onChange}
+ items={mapKeys(filterValueTypes).map((key) => ({
+ value: key,
+ label: filterValueTypes[key],
+ }))}
+ label="Value type"
+ >
+ {trigger}
+ </DropdownMenuComposed>
+ );
+}
diff --git a/apps/start/src/components/ui/dropdown-menu.tsx b/apps/start/src/components/ui/dropdown-menu.tsx
index 9f0b95d78..3d6121d85 100644
--- a/apps/start/src/components/ui/dropdown-menu.tsx
+++ b/apps/start/src/components/ui/dropdown-menu.tsx
@@ -187,6 +187,8 @@ interface DropdownProps<Value> {
items: {
label: string;
value: Value;
+ // Optional secondary line rendered below the label in smaller muted text.
+ description?: string;
}[];
onChange?: (value: Value) => void;
}
@@ -216,7 +218,16 @@ export function DropdownMenuComposed<Value extends string>({
onChange?.(item.value);
}}
>
- {item.label}
+ {item.description ? (
+ <div className="flex flex-col gap-0.5">
+ <span>{item.label}</span>
+ <span className="text-muted-foreground text-xs">
+ {item.description}
+ </span>
+ </div>
+ ) : (
+ item.label
+ )}
</DropdownMenuItem>
))}
</DropdownMenuGroup>
diff --git a/packages/constants/index.ts b/packages/constants/index.ts
index 10ae31cee..a7c2820fb 100644
--- a/packages/constants/index.ts
+++ b/packages/constants/index.ts
@@ -107,6 +107,87 @@ export const operators = {
notInCohort: 'Not in cohort',
} as const;
+// Compact labels for the operator trigger button. The comparison operators
+// have very long names ("Greater than or equal to"), so we show a symbol on
+// the trigger (reads naturally next to the value, e.g. "≥ 2019-01-01") and
+// surface the full `operators` text as a sub-line in the dropdown.
+export const operatorsShort: Record<keyof typeof operators, string> = {
+ is: 'Is',
+ isNot: 'Is not',
+ contains: 'Contains',
+ doesNotContain: 'Not contains',
+ startsWith: 'Starts with',
+ endsWith: 'Ends with',
+ regex: 'Regex',
+ isNull: 'Is null',
+ isNotNull: 'Is not null',
+ gt: '>',
+ lt: '<',
+ gte: '≥',
+ lte: '≤',
+ inCohort: 'In cohort',
+ notInCohort: 'Not in cohort',
+};
+
+// Cast type a filter value/column should be coerced to before comparing.
+// `string` (the default) keeps raw text comparison; the others wrap both sides
+// of the comparison in the matching ClickHouse cast (see packages/db
+// filter-cast.ts) so e.g. a date property compares as a date instead of
+// crashing `toFloat64('2019-01-01')`.
+export const filterValueTypes = {
+ string: 'Text',
+ number: 'Number',
+ date: 'Date',
+ datetime: 'Date & time',
+ boolean: 'Boolean',
+} as const;
+
+export type IFilterValueType = keyof typeof filterValueTypes;
+
+// Operators that make sense for each value type. The type constrains the
+// operator list in the UI (a Number can't `contains`, a Boolean only
+// `is`/`isNot`). Cohort operators are excluded everywhere — they're surfaced by
+// CohortFilterItem, not the standard operator select.
+const STRING_OPERATORS = [
+ 'is',
+ 'isNot',
+ 'contains',
+ 'doesNotContain',
+ 'startsWith',
+ 'endsWith',
+ 'regex',
+ 'isNull',
+ 'isNotNull',
+] as const;
+
+const ORDERED_OPERATORS = [
+ 'is',
+ 'isNot',
+ 'gt',
+ 'gte',
+ 'lt',
+ 'lte',
+ 'isNull',
+ 'isNotNull',
+] as const;
+
+const BOOLEAN_OPERATORS = ['is', 'isNot', 'isNull', 'isNotNull'] as const;
+
+export function getOperatorsForType(
+ type?: IFilterValueType,
+): readonly (keyof typeof operators)[] {
+ switch (type) {
+ case 'number':
+ case 'date':
+ case 'datetime':
+ return ORDERED_OPERATORS;
+ case 'boolean':
+ return BOOLEAN_OPERATORS;
+ default:
+ return STRING_OPERATORS;
+ }
+}
+
export const chartTypes = {
linear: 'Linear',
bar: 'Bar',
diff --git a/packages/db/src/services/chart-sql.test.ts b/packages/db/src/services/chart-sql.test.ts
index dd0063fe8..7a7ecf2f3 100644
--- a/packages/db/src/services/chart-sql.test.ts
+++ b/packages/db/src/services/chart-sql.test.ts
@@ -221,6 +221,74 @@ describe('chart.service / getChartSql', () => {
await explain(sql);
});
+ // Filter value type casting (filter.type). A date property compared with
+ // `gte` used to crash with `toFloat64('2019-01-01')`; declaring the type
+ // routes both column and value through the matching ClickHouse cast.
+ itCH('date-typed gte filter casts via best-effort toDate, not toFloat64', async () => {
+ const sql = await getChartSql({
+ event: event({
+ filters: [
+ {
+ name: 'properties.cook',
+ operator: 'gte',
+ value: ['2019-01-01'],
+ type: 'date',
+ },
+ ],
+ }),
+ breakdowns: [],
+ interval: 'day',
+ startDate: START,
+ endDate: END,
+ projectId: PROJECT_ID,
+ timezone: 'UTC',
+ });
+ expect(sql).toContain('toDate(parseDateTimeBestEffortOrNull');
+ expect(sql).not.toContain('toFloat64');
+ await explain(sql);
+ });
+
+ itCH('number-typed gte filter casts via toFloat64OrNull', async () => {
+ const sql = await getChartSql({
+ event: event({
+ filters: [
+ {
+ name: 'properties.age',
+ operator: 'gte',
+ value: ['5'],
+ type: 'number',
+ },
+ ],
+ }),
+ breakdowns: [],
+ interval: 'day',
+ startDate: START,
+ endDate: END,
+ projectId: PROJECT_ID,
+ timezone: 'UTC',
+ });
+ expect(sql).toContain('toFloat64OrNull');
+ await explain(sql);
+ });
+
+ itCH('untyped gte filter keeps legacy toFloat64 casting', async () => {
+ const sql = await getChartSql({
+ event: event({
+ filters: [
+ { name: 'properties.age', operator: 'gte', value: ['5'] },
+ ],
+ }),
+ breakdowns: [],
+ interval: 'day',
+ startDate: START,
+ endDate: END,
+ projectId: PROJECT_ID,
+ timezone: 'UTC',
+ });
+ expect(sql).toContain('toFloat64OrZero');
+ await explain(sql);
+ });
+
itCH('one_event_per_user segment still parses', async () => {
const sql = await getChartSql({
event: event({ segment: 'one_event_per_user' }),
diff --git a/packages/db/src/services/chart.service.ts b/packages/db/src/services/chart.service.ts
index 839bb3655..ab00f319b 100644
--- a/packages/db/src/services/chart.service.ts
+++ b/packages/db/src/services/chart.service.ts
@@ -12,6 +12,7 @@ import sqlstring from 'sqlstring';
import { formatClickhouseDate, TABLE_NAMES } from '../clickhouse/client';
import { db } from '../prisma-client';
import { createSqlBuilder } from '../sql-builder';
+import { buildTypedClause, hasTypedCast, isTypedOperator } from './filter-cast';
// Top-level columns on the events table. Derived from the migration in
// packages/db/code-migrations/3-init-ch.ts (+ revenue added in 6-add-revenue-
@@ -1188,6 +1189,10 @@ export function getEventFiltersWhereClause(
// Handle group. prefixed filters (requires ARRAY JOIN + _g JOIN in query)
if (name.startsWith('group.') && projectId) {
const whereFrom = getGroupPropertySql(name);
+ if (hasTypedCast(filter.type) && isTypedOperator(operator)) {
+ where[id] = buildTypedClause(whereFrom, operator, value, filter.type);
+ return;
+ }
switch (operator) {
case 'is': {
if (value.length === 1) {
@@ -1260,6 +1265,17 @@ export function getEventFiltersWhereClause(
const isWildcard = propertyKey.includes('%');
const whereFrom = propertyKey;
+ // Typed cast (number/date/datetime/boolean) short-circuit. Casts both the
+ // column and each value so e.g. `>= '2019-01-01'` compares as dates
+ // instead of crashing `toFloat64('2019-01-01')`. Untyped/string filters
+ // fall through to the legacy switch below.
+ if (hasTypedCast(filter.type) && isTypedOperator(operator)) {
+ where[id] = isWildcard
+ ? `arrayExists(x -> ${buildTypedClause('x', operator, value, filter.type)}, ${whereFrom})`
+ : buildTypedClause(whereFrom, operator, value, filter.type);
+ return;
+ }
+
switch (operator) {
case 'is': {
if (isWildcard) {
@@ -1475,6 +1491,12 @@ export function getEventFiltersWhereClause(
if (tableScope === 'events' && !EVENT_TOP_LEVEL_COLUMNS.has(name)) {
return;
}
+ // Typed cast short-circuit (see property branch above). Supersedes the
+ // `isNumericColumn` auto-detect when the user declared an explicit type.
+ if (hasTypedCast(filter.type) && isTypedOperator(operator)) {
+ where[id] = buildTypedClause(name, operator, value, filter.type);
+ return;
+ }
switch (operator) {
case 'is': {
if (value.length === 1) {
diff --git a/packages/db/src/services/filter-cast.ts b/packages/db/src/services/filter-cast.ts
new file mode 100644
index 000000000..69e4a0e8c
--- /dev/null
+++ b/packages/db/src/services/filter-cast.ts
@@ -0,0 +1,93 @@
+/** biome-ignore-all lint/style/useDefaultSwitchClause: switch cases are exhaustive by design */
+import type {
+ IChartEventFilterOperator,
+ IChartEventFilterValue,
+ IChartFilterValueType,
+} from '@openpanel/validation';
+import sqlstring from 'sqlstring';
+
+/**
+ * Wrap a SQL expression (a column accessor or an already-escaped value literal)
+ * in the ClickHouse cast matching the filter's declared value type.
+ *
+ * `toString(...)` first so the helper works uniformly on String properties
+ * (`properties['x']`) and already-numeric columns (`duration`, `revenue`).
+ * The `*OrNull` variants mean an unparseable value becomes NULL (no match)
+ * instead of throwing and crashing the whole query — e.g. `toFloat64('abc')`
+ * would error, `toFloat64OrNull('abc')` yields NULL.
+ */
+export function castSql(expr: string, type?: IChartFilterValueType): string {
+ switch (type) {
+ case 'number':
+ return `toFloat64OrNull(toString(${expr}))`;
+ case 'date':
+ // Parse with best-effort first, then truncate to a date. `toDateOrNull`
+ // is strict — it rejects loose formats like 'YYYY-MM-DD HH:MM' (no
+ // seconds) and returns NULL, which silently drops every row. Best-effort
+ // handles those, and toDate(NULL) stays NULL so bad values still no-match.
+ return `toDate(parseDateTimeBestEffortOrNull(toString(${expr})))`;
+ case 'datetime':
+ return `parseDateTimeBestEffortOrNull(toString(${expr}))`;
+ case 'boolean':
+ return `if(lower(trim(toString(${expr}))) IN ('true', '1', 'yes'), 1, 0)`;
+ case 'string':
+ case undefined:
+ return expr;
+ }
+}
+
+/**
+ * True when a non-string cast should be applied. `string` and `undefined`
+ * (legacy filters with no declared type) fall through to the existing raw
+ * comparison logic so behavior is unchanged.
+ */
+export function hasTypedCast(
+ type?: IChartFilterValueType,
+): type is IChartFilterValueType {
+ return !!type && type !== 'string';
+}
+
+// Equality + comparison operators where a declared cast type changes the SQL.
+// String-only operators (contains/startsWith/regex/…) and null checks are
+// unaffected and keep their existing handling.
+const TYPED_SQL_OPERATOR: Partial<Record<IChartEventFilterOperator, string>> = {
+ is: '=',
+ isNot: '!=',
+ gt: '>',
+ gte: '>=',
+ lt: '<',
+ lte: '<=',
+};
+
+export function isTypedOperator(operator: IChartEventFilterOperator): boolean {
+ return operator in TYPED_SQL_OPERATOR;
+}
+
+/**
+ * Build a parenthesized comparison clause where both the column expression and
+ * every value are cast to `type`. `leftExpr` is the raw column accessor (e.g.
+ * `e.properties['cook']`, a bare column name, or `x` inside an arrayExists
+ * lambda). Values are escaped here.
+ *
+ * `is`/`gt`/`gte`/`lt`/`lte` OR the per-value predicates together (match any);
+ * `isNot` ANDs them (must differ from all), matching the NOT-IN semantics of
+ * the untyped path.
+ */
+export function buildTypedClause(
+ leftExpr: string,
+ operator: IChartEventFilterOperator,
+ value: IChartEventFilterValue[],
+ type: IChartFilterValueType,
+): string {
+ const sqlOp = TYPED_SQL_OPERATOR[operator] ?? '=';
+ const left = castSql(leftExpr, type);
+ const joiner = operator === 'isNot' ? ' AND ' : ' OR ';
+ return `(${value
+ .map((val) => {
+ const escaped = sqlstring.escape(
+ typeof val === 'string' ? val.trim() : val,
+ );
+ return `${left} ${sqlOp} ${castSql(escaped, type)}`;
+ })
+ .join(joiner)})`;
+}
diff --git a/packages/db/src/services/filter-where.service.ts b/packages/db/src/services/filter-where.service.ts
index 6c51ea220..e51872f84 100644
--- a/packages/db/src/services/filter-where.service.ts
+++ b/packages/db/src/services/filter-where.service.ts
@@ -1,7 +1,12 @@
/** biome-ignore-all lint/style/useDefaultSwitchClause: switch cases are exhaustive by design */
-import { getCohortIds, type IChartEventFilter } from '@openpanel/validation';
+import {
+ getCohortIds,
+ type IChartEventFilter,
+ type IChartFilterValueType,
+} from '@openpanel/validation';
import sqlstring from 'sqlstring';
import { formatClickhouseDate, TABLE_NAMES } from '../clickhouse/client';
+import { buildTypedClause, hasTypedCast, isTypedOperator } from './filter-cast';
export type FilterTableContext = {
/** Outer query's primary table. */
@@ -51,7 +56,7 @@ function compileScalarClause(
column: string,
operator: IChartEventFilter['operator'],
value: IChartEventFilter['value'],
- options: { numeric?: boolean } = {},
+ options: { numeric?: boolean; type?: IChartFilterValueType } = {},
): string | null {
if (
value.length === 0 &&
@@ -61,6 +66,12 @@ function compileScalarClause(
return null;
}
+ // Explicit cast type wins over the column-name `numeric` auto-detect. Casts
+ // both the column and each value consistently (see filter-cast.ts).
+ if (hasTypedCast(options.type) && isTypedOperator(operator)) {
+ return buildTypedClause(column, operator, value, options.type!);
+ }
+
const numeric = options.numeric === true;
switch (operator) {
@@ -218,7 +229,9 @@ function buildGroupClause(
): string | null {
if (!ctx.groupsExpr) return null;
const column = groupColumnSql(filter.name);
- const inner = compileScalarClause(column, filter.operator, filter.value);
+ const inner = compileScalarClause(column, filter.operator, filter.value, {
+ type: filter.type,
+ });
if (!inner) return null;
const projectClause = `project_id = ${sqlstring.escape(projectId)}`;
return `arrayExists(g -> g IN (SELECT id FROM ${TABLE_NAMES.groups} FINAL WHERE ${projectClause} AND ${inner}), ${ctx.groupsExpr})`;
@@ -233,6 +246,7 @@ function buildProfileClause(
const numeric = column === 'created_at' || column === 'last_seen_at';
const inner = compileScalarClause(column, filter.operator, filter.value, {
numeric,
+ type: filter.type,
});
if (!inner) return null;
if (ctx.selfTable === 'profiles') {
@@ -279,6 +293,7 @@ function buildSessionClause(
if (!column) return null;
return compileScalarClause(column, filter.operator, filter.value, {
numeric: SESSION_NUMERIC_COLUMNS.has(column),
+ type: filter.type,
});
}
diff --git a/packages/validation/src/index.ts b/packages/validation/src/index.ts
index b02813cff..97c9c3503 100644
--- a/packages/validation/src/index.ts
+++ b/packages/validation/src/index.ts
@@ -3,6 +3,7 @@ import { z } from 'zod';
import {
chartSegments,
chartTypes,
+ filterValueTypes,
intervals,
lineTypes,
metrics,
@@ -28,6 +29,13 @@ export const zChartEventFilter = z.object({
value: z
.array(z.string().or(z.number()).or(z.boolean()).or(z.null()))
.describe('The values to filter on'),
+ type: z
+ .enum(objectToZodEnums(filterValueTypes))
+ .optional()
+ .describe(
+ 'Cast type for the column/value in equality & comparison operators ' +
+ '(string/number/date/datetime/boolean). Absent = legacy behavior.',
+ ),
cohortId: z
.string()
.optional()
diff --git a/packages/validation/src/types.validation.ts b/packages/validation/src/types.validation.ts
index bccedcbc2..de5ab51af 100644
--- a/packages/validation/src/types.validation.ts
+++ b/packages/validation/src/types.validation.ts
@@ -44,6 +44,9 @@ export type IChartEventFilterValue =
IChartEvent['filters'][number]['value'][number];
export type IChartEventFilterOperator =
IChartEvent['filters'][number]['operator'];
+export type IChartFilterValueType = NonNullable<
+ IChartEvent['filters'][number]['type']
+>;
export type IChartBreakdown = z.infer<typeof zChartBreakdown>;
export type IInterval = z.infer<typeof zTimeInterval>;
export type IChartType = z.infer<typeof zChartType>;