Skip to content

: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.

fix(cdp): only run a warehouse destination for the table it selected (#84414)

Tom Owers67655c6d7f3d
diff --git a/nodejs/src/cdp/consumers/cdp-data-warehouse-events.consumer.test.ts b/nodejs/src/cdp/consumers/cdp-data-warehouse-events.consumer.test.ts
index 0e83f4af4101..39e3b158d51f 100644
--- a/nodejs/src/cdp/consumers/cdp-data-warehouse-events.consumer.test.ts
+++ b/nodejs/src/cdp/consumers/cdp-data-warehouse-events.consumer.test.ts
@@ -220,6 +220,36 @@ describe('CdpDatawarehouseEventsConsumer', () => {
expect(invocations).toHaveLength(1)
expect(invocations[0].teamId).toBe(fnWithDataWarehouseFilter.team_id)
})
+
+ it("should only invoke destinations subscribed to the row's table", async () => {
+ const fnForOrders = await insertHogFunction({
+ ...HOG_EXAMPLES.simple_fetch,
+ ...HOG_INPUTS_EXAMPLES.simple_fetch_data_warehouse_table,
+ filters: {
+ source: 'data-warehouse-table',
+ bytecode: ['_h', 29],
+ data_warehouse: [{ table_name: 'postgres.orders' }],
+ },
+ })
+
+ await insertHogFunction({
+ ...HOG_EXAMPLES.simple_fetch,
+ ...HOG_INPUTS_EXAMPLES.simple_fetch_data_warehouse_table,
+ filters: {
+ source: 'data-warehouse-table',
+ bytecode: ['_h', 29],
+ data_warehouse: [{ table_name: 'stripe.charge' }],
+ },
+ })
+
+ const messages = [createKafkaMessage(createDataWarehouseEvent(team.id, {}, 'postgres.orders'))]
+ const globals = await processor._parseKafkaBatch(messages)
+
+ const { invocations } = await processor.processBatch(globals)
+
+ expect(invocations).toHaveLength(1)
+ expect((invocations[0] as any).hogFunction.id).toBe(fnForOrders.id)
+ })
})
describe('processBatch', () => {
diff --git a/nodejs/src/cdp/consumers/cdp-data-warehouse-events.consumer.ts b/nodejs/src/cdp/consumers/cdp-data-warehouse-events.consumer.ts
index 550150bf2f9c..104e9b09bdb7 100644
--- a/nodejs/src/cdp/consumers/cdp-data-warehouse-events.consumer.ts
+++ b/nodejs/src/cdp/consumers/cdp-data-warehouse-events.consumer.ts
@@ -12,7 +12,12 @@ import { CdpDataWarehouseEvent, CdpDataWarehouseEventSchema } from '../schema'
import { HogFlowInvocationPipeline } from '../services/hog-flow-invocation-pipeline.service'
import { HogFunctionInvocationPipeline } from '../services/hog-function-invocation-pipeline.service'
import { JobQueue } from '../services/job-queue/job-queue.interface'
-import { CyclotronJobInvocation, HogFunctionInvocationGlobals, HogFunctionTypeType } from '../types'
+import {
+ CyclotronJobInvocation,
+ HogFunctionFilterDataWarehouse,
+ HogFunctionInvocationGlobals,
+ HogFunctionTypeType,
+} from '../types'
import { CdpConsumerBase, CdpConsumerBaseDeps } from './cdp-base.consumer'
import { counterParseError } from './metrics'
@@ -85,6 +90,11 @@ export class CdpDatawarehouseEventsConsumer extends CdpConsumerBase {
this.hogFunctionPipeline.buildInvocations(invocationGlobals, {
hogTypes: this.hogTypes,
filterFn: (fn) => (fn.filters?.source ?? 'events') === 'data-warehouse-table',
+ // Destinations pick their tables in `filters.data_warehouse`, which never reaches the
+ // filter bytecode, so without this every warehouse-triggered destination in the team
+ // ran for every table's rows.
+ invocationFilterFn: (fn, globals) =>
+ matchesSubscribedTable(fn.filters?.data_warehouse, sourceTableOf(globals)),
}),
// Source-compatibility matching lives in the consumer rather than the executor — the
// consumer knows it's serving warehouse rows, so it filters flows to only those whose
@@ -92,8 +102,7 @@ export class CdpDatawarehouseEventsConsumer extends CdpConsumerBase {
// evaluates filter bytecode on the matched flows.
this.hogFlowPipeline.buildInvocations(invocationGlobals, {
eligibilityFn: (flow, globals) =>
- flow.trigger.type === 'data-warehouse-table' &&
- flow.trigger.table_name === globals.event?.properties?.[DWH_SOURCE_TABLE_PROPERTY],
+ flow.trigger.type === 'data-warehouse-table' && flow.trigger.table_name === sourceTableOf(globals),
}),
])
@@ -189,6 +198,26 @@ export class CdpDatawarehouseEventsConsumer extends CdpConsumerBase {
}
}
+function sourceTableOf(globals: HogFunctionInvocationGlobals): string {
+ const table = globals.event?.properties?.[DWH_SOURCE_TABLE_PROPERTY]
+ return typeof table === 'string' ? table : ''
+}
+
+/**
+ * An empty or absent subscription list keeps the pre-existing "match every table" behavior.
+ * Destinations saved before per-table matching existed have no entries, and tightening them here
+ * would silently stop them firing.
+ */
+function matchesSubscribedTable(
+ subscriptions: HogFunctionFilterDataWarehouse[] | undefined,
+ sourceTable: string
+): boolean {
+ if (!subscriptions?.length) {
+ return true
+ }
+ return subscriptions.some((subscription) => subscription.table_name === sourceTable)
+}
+
function convertDataWarehouseEventToHogFunctionInvocationGlobals(
event: CdpDataWarehouseEvent,
team: Team,
diff --git a/nodejs/src/cdp/types.ts b/nodejs/src/cdp/types.ts
index 27f03ad4e487..80e6ab9b4205 100644
--- a/nodejs/src/cdp/types.ts
+++ b/nodejs/src/cdp/types.ts
@@ -44,10 +44,17 @@ export type HogFunctionMasking = {
threshold: number | null
}
+export type HogFunctionFilterDataWarehouse = {
+ table_name?: string
+}
+
export interface HogFunctionFilters {
source?: 'events' | 'person-updates' | 'data-warehouse-table' // Special case to identify what kind of thing this filters on
events?: HogFunctionFilterEvent[]
actions?: HogFunctionFilterAction[]
+ // Warehouse tables this function is subscribed to. Never compiled into bytecode, so the
+ // consumer has to match on it directly.
+ data_warehouse?: HogFunctionFilterDataWarehouse[]
properties?: Record<string, any>[] // Global property filters that apply to all events
filter_test_accounts?: boolean
bytecode?: HogBytecode