:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(insights): drop unresolvable property filters when switching series entity type (#74642)
Anders5083906c1b2e
diff --git a/frontend/src/scenes/insights/filters/ActionFilter/entityFilterLogic.test.ts b/frontend/src/scenes/insights/filters/ActionFilter/entityFilterLogic.test.tsindex a1042fd188f2..a877add315c6 100644--- a/frontend/src/scenes/insights/filters/ActionFilter/entityFilterLogic.test.ts+++ b/frontend/src/scenes/insights/filters/ActionFilter/entityFilterLogic.test.ts@@ -2,11 +2,11 @@ import { expectLogic } from 'kea-test-utils'import { TaxonomicFilterGroupType } from 'lib/components/TaxonomicFilter/types'import * as libUtils from 'lib/utils/dom'-import { entityFilterLogic, toLocalFilters } from 'scenes/insights/filters/ActionFilter/entityFilterLogic'+import { LocalFilter, entityFilterLogic, toLocalFilters } from 'scenes/insights/filters/ActionFilter/entityFilterLogic'import { useMocks } from '~/mocks/jest'import { initKeaTests } from '~/test/init'-import { FilterType } from '~/types'+import { AnyPropertyFilter, FilterType, PropertyFilterType, PropertyOperator } from '~/types'import eventDefinitionsJson from './__mocks__/event_definitions.json'import filtersJson from './__mocks__/filters.json'@@ -205,6 +205,158 @@ describe('entityFilterLogic', () => {})})+ describe('updateFilter across entity types', () => {+ const personProperty: AnyPropertyFilter = {+ key: 'email',+ value: '[email protected]',+ operator: PropertyOperator.Exact,+ type: PropertyFilterType.Person,+ }+ const extendedPersonProperty: AnyPropertyFilter = {+ key: 'customers.plan',+ value: ['pro'],+ operator: PropertyOperator.Exact,+ type: PropertyFilterType.DataWarehousePersonProperty,+ }+ const dwColumnProperty: AnyPropertyFilter = {+ key: 'status',+ value: ['complete'],+ operator: PropertyOperator.Exact,+ type: PropertyFilterType.DataWarehouse,+ }+ const hogqlProperty: AnyPropertyFilter = {+ key: 'amount > 0',+ type: PropertyFilterType.HogQL,+ }++ const eventFilter: LocalFilter = {+ id: '$pageview',+ name: '$pageview',+ type: 'events',+ order: 0,+ uuid: 'uuid-0',+ properties: [personProperty, extendedPersonProperty, dwColumnProperty, hogqlProperty],+ }+ const dataWarehouseFilter: LocalFilter = {+ id: 'payments',+ name: 'payments',+ type: 'data_warehouse',+ table_name: 'payments',+ order: 0,+ uuid: 'uuid-0',+ properties: [dwColumnProperty, hogqlProperty],+ }+ const switchToPayments = {+ type: 'data_warehouse',+ id: 'payments',+ name: 'payments',+ table_name: 'payments',+ }++ it.each([+ [+ 'drops person-scoped filters when an event becomes a data warehouse series',+ eventFilter,+ switchToPayments,+ [hogqlProperty],+ ],+ [+ 'drops column filters when a data warehouse series becomes an event',+ dataWarehouseFilter,+ { type: 'events', id: '$pageview', name: '$pageview' },+ [hogqlProperty],+ ],+ [+ 'drops column filters when the data warehouse table changes',+ dataWarehouseFilter,+ { type: 'data_warehouse', id: 'orders', name: 'orders', table_name: 'orders' },+ [hogqlProperty],+ ],+ [+ 'keeps column filters when the data warehouse table is unchanged',+ dataWarehouseFilter,+ { ...switchToPayments, timestamp_field: 'created_at' },+ [dwColumnProperty, hogqlProperty],+ ],+ ] as [string, LocalFilter, Record<string, any>, AnyPropertyFilter[]][])(+ '%s',+ async (_name, initialFilter, update, expectedProperties) => {+ logic.actions.setFilters([initialFilter])++ await expectLogic(logic, () => {+ logic.actions.updateFilter({ ...update, index: 0 } as Parameters<+ typeof logic.actions.updateFilter+ >[0])+ }).toDispatchActions(['updateFilter', 'setFilters'])++ expect(logic.values.localFilters[0].properties).toEqual(expectedProperties)+ }+ )++ const eventPropertyMath = {+ ...eventFilter,+ math: 'sum',+ math_property: 'revenue',+ math_property_type: TaxonomicFilterGroupType.NumericalEventProperties,+ }+ const dataWarehousePropertyMath = {+ ...dataWarehouseFilter,+ math: 'sum',+ math_property: 'amount',+ math_property_type: TaxonomicFilterGroupType.DataWarehouseProperties,+ }++ it.each([+ [+ 'drops property math when an event becomes a data warehouse series',+ eventPropertyMath,+ switchToPayments,+ { math: undefined, math_property: undefined, math_property_type: undefined },+ ],+ [+ 'drops property math when a data warehouse series becomes an event',+ dataWarehousePropertyMath,+ { type: 'events', id: '$pageview', name: '$pageview' },+ { math: undefined, math_property: undefined, math_property_type: undefined },+ ],+ [+ 'drops property math when the data warehouse table changes',+ dataWarehousePropertyMath,+ { type: 'data_warehouse', id: 'orders', name: 'orders', table_name: 'orders' },+ { math: undefined, math_property: undefined, math_property_type: undefined },+ ],+ [+ 'keeps property math when the data warehouse table is unchanged',+ dataWarehousePropertyMath,+ { ...switchToPayments, timestamp_field: 'created_at' },+ {+ math: 'sum',+ math_property: 'amount',+ math_property_type: TaxonomicFilterGroupType.DataWarehouseProperties,+ },+ ],+ [+ 'keeps math that does not depend on a property',+ { ...eventFilter, math: 'dau' },+ switchToPayments,+ { math: 'dau', math_property: undefined, math_property_type: undefined },+ ],+ ] as [string, LocalFilter, Record<string, any>, Record<string, any>][])(+ '%s',+ async (_name, initialFilter, update, expectedMath) => {+ logic.actions.setFilters([initialFilter])++ await expectLogic(logic, () => {+ logic.actions.updateFilter({ ...update, index: 0 } as Parameters<+ typeof logic.actions.updateFilter+ >[0])+ }).toDispatchActions(['updateFilter', 'setFilters'])++ expect(logic.values.localFilters[0]).toMatchObject(expectedMath)+ }+ )+ })+describe('duplicating filters', () => {it('preserves custom_name when duplicating', async () => {await expectLogic(logic, () => {diff --git a/frontend/src/scenes/insights/filters/ActionFilter/entityFilterLogic.ts b/frontend/src/scenes/insights/filters/ActionFilter/entityFilterLogic.tsindex 2664e928a4be..ee32e1008f0a 100644--- a/frontend/src/scenes/insights/filters/ActionFilter/entityFilterLogic.ts+++ b/frontend/src/scenes/insights/filters/ActionFilter/entityFilterLogic.ts@@ -19,6 +19,8 @@ import {EntityTypes,FilterLogicalOperator,FilterType,+ PropertyFilterType,+ PropertyMathType,} from '~/types'import type { TaxonomicFilterGroupType } from '../../../../lib/components/TaxonomicFilter/types'@@ -52,6 +54,19 @@ export function toLocalFilters(filters: Partial<FilterType>): LocalFilter[] {)}+const PROPERTY_VALUE_MATHS = new Set<string>(Object.values(PropertyMathType))++// math_property names a column or property on the entity the series aggregates, so it cannot+// survive an entity switch. Property-value math (sum, avg, percentiles) is dropped with it —+// the backend errors out on that math without a math_property.+function dropStaleMath(filter: LocalFilter): void {+ if (filter.math && PROPERTY_VALUE_MATHS.has(filter.math)) {+ filter.math = undefined+ }+ filter.math_property = undefined+ filter.math_property_type = undefined+}+export function toFilters(localFilters: LocalFilter[]): FilterType {const filters = localFilters.map((filter, index) => ({...filter,@@ -501,6 +516,27 @@ export const entityFilterLogic = kea<entityFilterLogicType>([)})+ // A data warehouse series resolves property filters against its own+ // table: there is no person in scope, so person-scoped filters carried+ // over from a previous event entity cannot resolve, and column filters+ // from a different table cannot either. SQL expression filters are+ // user-authored and stay visible in the UI, so they are kept.+ if (Array.isArray(filter.properties)) {+ updatedFilter.properties = filter.properties.filter(+ (property: AnyPropertyFilter) =>+ property.type === PropertyFilterType.HogQL ||+ (property.type === PropertyFilterType.DataWarehouse &&+ updatedFilter.table_name === filter.table_name)+ )+ }++ if (+ filter.type !== EntityTypes.DATA_WAREHOUSE ||+ updatedFilter.table_name !== filter.table_name+ ) {+ dropStaleMath(updatedFilter)+ }+return updatedFilter}@@ -524,6 +560,18 @@ export const entityFilterLogic = kea<entityFilterLogicType>([delete cleanedFilter[key]})+ // Data warehouse column filters reference the warehouse table, which an+ // event or action series does not select from, so they cannot carry over.+ if (Array.isArray(cleanedFilter.properties)) {+ cleanedFilter.properties = cleanedFilter.properties.filter(+ (property: AnyPropertyFilter) => property.type !== PropertyFilterType.DataWarehouse+ )+ }++ if (filter.type === EntityTypes.DATA_WAREHOUSE) {+ dropStaleMath(cleanedFilter)+ }+return {...cleanedFilter,id: typeof id === 'undefined' ? filter.id : id,