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(insights): coerce aggregation_target type in multi-source funnels (#58711)

Sam Pennington166f313c3eb2
diff --git a/posthog/hogql_queries/insights/funnels/funnel_event_query.py b/posthog/hogql_queries/insights/funnels/funnel_event_query.py
index fa69e8eccea4..07656e0da13f 100644
--- a/posthog/hogql_queries/insights/funnels/funnel_event_query.py
+++ b/posthog/hogql_queries/insights/funnels/funnel_event_query.py
@@ -160,6 +160,15 @@ class FunnelEventQuery(DataWarehouseSchemaMixin):
if len(queries) == 1:
return queries[0]
+ # Multiple source tables are combined with UNION ALL, which needs a common
+ # column type. Different sources resolve aggregation_target to different
+ # types (e.g. a person_id UUID and a warehouse string column), so coerce
+ # every branch's aggregation_target to a string.
+ for query in queries:
+ for select_expr in query.select:
+ if isinstance(select_expr, ast.Alias) and select_expr.alias == "aggregation_target":
+ select_expr.expr = ast.Call(name="toString", args=[select_expr.expr])
+
# Take the field and alias names from the first query. UNION enforces identical column sets
# across all selects, which makes this reliable.
aliased_fields = alias_columns_in_select(queries[0].select, self.EVENT_TABLE_ALIAS)
diff --git a/posthog/hogql_queries/insights/funnels/test/__snapshots__/test_funnel_data_warehouse.ambr b/posthog/hogql_queries/insights/funnels/test/__snapshots__/test_funnel_data_warehouse.ambr
index ce8093d2b93b..eb6adc76762d 100644
--- a/posthog/hogql_queries/insights/funnels/test/__snapshots__/test_funnel_data_warehouse.ambr
+++ b/posthog/hogql_queries/insights/funnels/test/__snapshots__/test_funnel_data_warehouse.ambr
@@ -97,7 +97,7 @@
e.step_1 AS step_1
FROM
(SELECT toTimeZone(e.timestamp, 'UTC') AS timestamp,
- if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id) AS aggregation_target,
+ toString(if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id)) AS aggregation_target,
e.uuid AS uuid,
e.`$session_id` AS `$session_id`,
e.`$window_id` AS `$window_id`,
@@ -113,7 +113,7 @@
HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id_overrides.is_deleted), person_distinct_id_overrides.version), 1), 0), 0) SETTINGS optimize_aggregation_in_order=1) AS e__override ON equals(e.distinct_id, e__override.distinct_id)
WHERE and(equals(e.team_id, 99999), and(and(greaterOrEquals(e.timestamp, toDateTime64('explicit_redacted_timestamp', 6, 'UTC')), lessOrEquals(e.timestamp, toDateTime64('explicit_redacted_timestamp', 6, 'UTC'))), in(e.event, tuple('$pageview'))), ifNull(equals(step_0, 1), 0))
UNION ALL SELECT toTimeZone(e.created, 'UTC') AS timestamp,
- accurateCastOrNull(e.user_id, 'UUID') AS aggregation_target,
+ toString(accurateCastOrNull(e.user_id, 'UUID')) AS aggregation_target,
tupleElement(tuple(throwIf(isNull(e.uuid), 'Encountered a null value in posthog_test_test_table_1.uuid, but a non-null value is required. Please ensure this column contains no null values, or add a filter to exclude rows with null values.'), toUUIDOrDefault(toString(e.uuid), reinterpretAsUUID(hex(MD5(concat('posthog_test_test_table_1_0_', ifNull(toString(e.uuid), ''))))))), 2) AS uuid,
NULL AS `$session_id`,
NULL AS `$window_id`,
@@ -143,6 +143,168 @@
use_hive_partitioning=0
'''
# ---
+# name: TestFunnelDataWarehouse.test_funnels_data_warehouse_and_regular_nodes_string_aggregation_target
+ '''
+ SELECT sum(step_1) AS step_1,
+ sum(step_2) AS step_2,
+ arrayMap(x -> if(isNaN(x), NULL, x), [avgArrayOrNull(step_1_conversion_times)])[1] AS step_1_average_conversion_time,
+ arrayMap(x -> if(isNaN(x), NULL, x), [medianArrayOrNull(step_1_conversion_times)])[1] AS step_1_median_conversion_time,
+ groupArray(row_number) AS row_number,
+ final_prop AS final_prop
+ FROM
+ (SELECT countIf(ifNull(notEquals(bitAnd(steps_bitfield, 1), 0), 1)) AS step_1,
+ countIf(ifNull(notEquals(bitAnd(steps_bitfield, 2), 0), 1)) AS step_2,
+ groupArrayIf(timings[1], ifNull(greater(timings[1], 0), 0)) AS step_1_conversion_times,
+ rowNumberInAllBlocks() AS row_number,
+ breakdown AS final_prop
+ FROM
+ (SELECT arraySort(t -> t.1, groupArray(tuple(accurateCastOrNull(timestamp, 'Float64'), uuid, '', arrayFilter(x -> ifNull(notEquals(x, 0), 1), [multiply(1, step_0), multiply(2, step_1)])))) AS events_array,
+ [''] AS prop,
+ arrayJoin(aggregate_funnel(2, 1209600, 'first_touch', 'ordered', prop, [], arrayFilter((x, x_before, x_after) -> not(and(ifNull(lessOrEquals(length(x.4), 1), 0), ifNull(equals(x.4, x_before.4), isNull(x.4)
+ and isNull(x_before.4)), ifNull(equals(x.4, x_after.4), isNull(x.4)
+ and isNull(x_after.4)), ifNull(equals(x.3, x_before.3), isNull(x.3)
+ and isNull(x_before.3)), ifNull(equals(x.3, x_after.3), isNull(x.3)
+ and isNull(x_after.3)), ifNull(greater(x.1, x_before.1), 0), ifNull(less(x.1, x_after.1), 0))), events_array, arrayRotateRight(events_array, 1), arrayRotateLeft(events_array, 1)))) AS af_tuple,
+ af_tuple.1 AS step_reached,
+ plus(af_tuple.1, 1) AS steps,
+ af_tuple.2 AS breakdown,
+ af_tuple.3 AS timings,
+ af_tuple.5 AS steps_bitfield,
+ aggregation_target AS aggregation_target
+ FROM
+ (SELECT e.timestamp AS timestamp,
+ e.aggregation_target AS aggregation_target,
+ e.uuid AS uuid,
+ e.`$session_id` AS `$session_id`,
+ e.`$window_id` AS `$window_id`,
+ e.step_0 AS step_0,
+ e.step_1 AS step_1
+ FROM
+ (SELECT toTimeZone(e.timestamp, 'UTC') AS timestamp,
+ toString(if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id)) AS aggregation_target,
+ e.uuid AS uuid,
+ e.`$session_id` AS `$session_id`,
+ e.`$window_id` AS `$window_id`,
+ if(equals(e.event, '$pageview'), 1, 0) AS step_0,
+ 0 AS step_1
+ FROM events AS e
+ LEFT OUTER JOIN
+ (SELECT tupleElement(argMax(tuple(person_distinct_id_overrides.person_id), person_distinct_id_overrides.version), 1) AS person_id,
+ person_distinct_id_overrides.distinct_id AS distinct_id
+ FROM person_distinct_id_overrides
+ WHERE equals(person_distinct_id_overrides.team_id, 99999)
+ GROUP BY person_distinct_id_overrides.distinct_id
+ HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id_overrides.is_deleted), person_distinct_id_overrides.version), 1), 0), 0) SETTINGS optimize_aggregation_in_order=1) AS e__override ON equals(e.distinct_id, e__override.distinct_id)
+ WHERE and(equals(e.team_id, 99999), and(and(greaterOrEquals(e.timestamp, toDateTime64('explicit_redacted_timestamp', 6, 'UTC')), lessOrEquals(e.timestamp, toDateTime64('explicit_redacted_timestamp', 6, 'UTC'))), in(e.event, tuple('$pageview'))), ifNull(equals(step_0, 1), 0))
+ UNION ALL SELECT toTimeZone(e.created, 'UTC') AS timestamp,
+ toString(e.user_id) AS aggregation_target,
+ tupleElement(tuple(throwIf(isNull(e.uuid), 'Encountered a null value in posthog_test_test_table_1.uuid, but a non-null value is required. Please ensure this column contains no null values, or add a filter to exclude rows with null values.'), toUUIDOrDefault(toString(e.uuid), reinterpretAsUUID(hex(MD5(concat('posthog_test_test_table_1_0_', ifNull(toString(e.uuid), ''))))))), 2) AS uuid,
+ NULL AS `$session_id`,
+ NULL AS `$window_id`,
+ 0 AS step_0,
+ if(1, 1, 0) AS step_1
+ FROM s3('http://host.docker.internal:19000/posthog/test_storage_bucket-posthog.hogql_queries.insights.funnels.funnel_data_warehouse/posthog_test_test_table_1/*.csv', 'object_storage_root_user', 'object_storage_root_password', 'CSVWithNames', '`id` Int64, `uuid` String, `created` DateTime64(3, \'UTC\'), `user_id` String, `event_name` String, `id_decimal` Decimal(18, 2), `properties` Nullable(String), `id_with_nulls` Nullable(Int64)') AS e
+ WHERE and(and(greaterOrEquals(timestamp, toDateTime64('explicit_redacted_timestamp', 6, 'UTC')), lessOrEquals(timestamp, toDateTime64('explicit_redacted_timestamp', 6, 'UTC'))), ifNull(equals(step_1, 1), 0))) AS e)
+ GROUP BY aggregation_target
+ HAVING ifNull(greaterOrEquals(step_reached, 0), 0))
+ GROUP BY breakdown
+ ORDER BY step_2 DESC,
+ step_1 DESC)
+ GROUP BY final_prop
+ ORDER BY step_2 DESC,
+ step_1 DESC
+ LIMIT 100 SETTINGS join_algorithm='auto',
+ format_csv_allow_double_quotes=1,
+ readonly=2,
+ max_execution_time=60,
+ allow_experimental_object_type=1,
+ max_ast_elements=4000000,
+ max_expanded_ast_elements=4000000,
+ max_bytes_before_external_group_by=23622320128,
+ enable_analyzer=1,
+ transform_null_in=1,
+ optimize_min_equality_disjunction_chain_length=4294967295,
+ allow_experimental_join_condition=1,
+ use_hive_partitioning=0
+ '''
+# ---
+# name: TestFunnelDataWarehouse.test_funnels_data_warehouse_and_regular_nodes_string_aggregation_target_actors
+ '''
+ SELECT source.id,
+ source.id AS id
+ FROM
+ (SELECT aggregation_target AS actor_id,
+ actor_id AS id
+ FROM
+ (SELECT arraySort(t -> t.1, groupArray(tuple(accurateCastOrNull(timestamp, 'Float64'), uuid, '', arrayFilter(x -> ifNull(notEquals(x, 0), 1), [multiply(1, step_0), multiply(2, step_1)])))) AS events_array,
+ [''] AS prop,
+ arrayJoin(aggregate_funnel(2, 1209600, 'first_touch', 'ordered', prop, [], arrayFilter((x, x_before, x_after) -> not(and(ifNull(lessOrEquals(length(x.4), 1), 0), ifNull(equals(x.4, x_before.4), isNull(x.4)
+ and isNull(x_before.4)), ifNull(equals(x.4, x_after.4), isNull(x.4)
+ and isNull(x_after.4)), ifNull(equals(x.3, x_before.3), isNull(x.3)
+ and isNull(x_before.3)), ifNull(equals(x.3, x_after.3), isNull(x.3)
+ and isNull(x_after.3)), ifNull(greater(x.1, x_before.1), 0), ifNull(less(x.1, x_after.1), 0))), events_array, arrayRotateRight(events_array, 1), arrayRotateLeft(events_array, 1)))) AS af_tuple,
+ af_tuple.1 AS step_reached,
+ plus(af_tuple.1, 1) AS steps,
+ af_tuple.2 AS breakdown,
+ af_tuple.3 AS timings,
+ af_tuple.5 AS steps_bitfield,
+ aggregation_target AS aggregation_target
+ FROM
+ (SELECT e.timestamp AS timestamp,
+ e.aggregation_target AS aggregation_target,
+ e.uuid AS uuid,
+ e.`$session_id` AS `$session_id`,
+ e.`$window_id` AS `$window_id`,
+ e.step_0 AS step_0,
+ e.step_1 AS step_1
+ FROM
+ (SELECT toTimeZone(e.timestamp, 'UTC') AS timestamp,
+ toString(if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id)) AS aggregation_target,
+ e.uuid AS uuid,
+ e.`$session_id` AS `$session_id`,
+ e.`$window_id` AS `$window_id`,
+ if(equals(e.event, '$pageview'), 1, 0) AS step_0,
+ 0 AS step_1
+ FROM events AS e
+ LEFT OUTER JOIN
+ (SELECT tupleElement(argMax(tuple(person_distinct_id_overrides.person_id), person_distinct_id_overrides.version), 1) AS person_id,
+ person_distinct_id_overrides.distinct_id AS distinct_id
+ FROM person_distinct_id_overrides
+ WHERE equals(person_distinct_id_overrides.team_id, 99999)
+ GROUP BY person_distinct_id_overrides.distinct_id
+ HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id_overrides.is_deleted), person_distinct_id_overrides.version), 1), 0), 0) SETTINGS optimize_aggregation_in_order=1) AS e__override ON equals(e.distinct_id, e__override.distinct_id)
+ WHERE and(equals(e.team_id, 99999), and(and(greaterOrEquals(e.timestamp, toDateTime64('explicit_redacted_timestamp', 6, 'UTC')), lessOrEquals(e.timestamp, toDateTime64('explicit_redacted_timestamp', 6, 'UTC'))), in(e.event, tuple('$pageview'))), ifNull(equals(step_0, 1), 0))
+ UNION ALL SELECT toTimeZone(e.created, 'UTC') AS timestamp,
+ toString(e.user_id) AS aggregation_target,
+ tupleElement(tuple(throwIf(isNull(e.uuid), 'Encountered a null value in posthog_test_test_table_1.uuid, but a non-null value is required. Please ensure this column contains no null values, or add a filter to exclude rows with null values.'), toUUIDOrDefault(toString(e.uuid), reinterpretAsUUID(hex(MD5(concat('posthog_test_test_table_1_0_', ifNull(toString(e.uuid), ''))))))), 2) AS uuid,
+ NULL AS `$session_id`,
+ NULL AS `$window_id`,
+ 0 AS step_0,
+ if(1, 1, 0) AS step_1
+ FROM s3('http://host.docker.internal:19000/posthog/test_storage_bucket-posthog.hogql_queries.insights.funnels.funnel_data_warehouse/posthog_test_test_table_1/*.csv', 'object_storage_root_user', 'object_storage_root_password', 'CSVWithNames', '`id` Int64, `uuid` String, `created` DateTime64(3, \'UTC\'), `user_id` String, `event_name` String, `id_decimal` Decimal(18, 2), `properties` Nullable(String), `id_with_nulls` Nullable(Int64)') AS e
+ WHERE and(and(greaterOrEquals(timestamp, toDateTime64('explicit_redacted_timestamp', 6, 'UTC')), lessOrEquals(timestamp, toDateTime64('explicit_redacted_timestamp', 6, 'UTC'))), ifNull(equals(step_1, 1), 0))) AS e)
+ GROUP BY aggregation_target
+ HAVING ifNull(greaterOrEquals(step_reached, 0), 0))
+ WHERE bitTest(steps_bitfield, 1)
+ ORDER BY aggregation_target ASC SETTINGS join_algorithm='auto') AS source
+ ORDER BY source.id ASC
+ LIMIT 101
+ OFFSET 0 SETTINGS optimize_aggregation_in_order=1,
+ join_algorithm='auto',
+ format_csv_allow_double_quotes=1,
+ readonly=2,
+ max_execution_time=60,
+ allow_experimental_object_type=1,
+ max_ast_elements=4000000,
+ max_expanded_ast_elements=4000000,
+ max_bytes_before_external_group_by=0,
+ enable_analyzer=1,
+ transform_null_in=1,
+ optimize_min_equality_disjunction_chain_length=4294967295,
+ allow_experimental_join_condition=1,
+ use_hive_partitioning=0
+ '''
+# ---
# name: TestFunnelDataWarehouse.test_funnels_data_warehouse_non_uuid_id_column
'''
SELECT sum(step_1) AS step_1,
@@ -367,7 +529,7 @@
e.step_2 AS step_2
FROM
(SELECT toTimeZone(e.created_date, 'UTC') AS timestamp,
- coalesce(e.converted_opportunity_id, e.id) AS aggregation_target,
+ toString(coalesce(e.converted_opportunity_id, e.id)) AS aggregation_target,
tupleElement(tuple(throwIf(isNull(e.id), 'Encountered a null value in posthog_test_salesforce_lead.id, but a non-null value is required. Please ensure this column contains no null values, or add a filter to exclude rows with null values.'), toUUIDOrDefault(toString(e.id), reinterpretAsUUID(hex(MD5(concat('posthog_test_salesforce_lead_0_', ifNull(toString(e.id), ''))))))), 2) AS uuid,
NULL AS `$session_id`,
NULL AS `$window_id`,
@@ -377,7 +539,7 @@
FROM s3('http://host.docker.internal:19000/posthog/test_storage_bucket-posthog.hogql_queries.insights.funnels.funnel_data_warehouse/posthog_test_salesforce_lead/*.csv', 'object_storage_root_user', 'object_storage_root_password', 'CSVWithNames', '`id` String, `created_date` DateTime64(3, \'UTC\'), `converted_opportunity_id` Nullable(String)') AS e
WHERE and(and(greaterOrEquals(timestamp, toDateTime64('2024-05-01 00:00:00.000000', 6, 'UTC')), lessOrEquals(timestamp, toDateTime64('2024-06-30 23:59:59.999999', 6, 'UTC'))), ifNull(equals(step_0, 1), 0))
UNION ALL SELECT toTimeZone(e.created_date, 'UTC') AS timestamp,
- e.id AS aggregation_target,
+ toString(e.id) AS aggregation_target,
tupleElement(tuple(throwIf(isNull(e.id), 'Encountered a null value in posthog_test_salesforce_opportunity.id, but a non-null value is required. Please ensure this column contains no null values, or add a filter to exclude rows with null values.'), toUUIDOrDefault(toString(e.id), reinterpretAsUUID(hex(MD5(concat('posthog_test_salesforce_opportunity_1_', ifNull(toString(e.id), ''))))))), 2) AS uuid,
NULL AS `$session_id`,
NULL AS `$window_id`,
@@ -387,7 +549,7 @@
FROM s3('http://host.docker.internal:19000/posthog/test_storage_bucket-posthog.hogql_queries.insights.funnels.funnel_data_warehouse/posthog_test_salesforce_opportunity/*.csv', 'object_storage_root_user', 'object_storage_root_password', 'CSVWithNames', '`id` String, `close_date` Nullable(Date), `created_date` DateTime64(3, \'UTC\')') AS e
WHERE and(and(greaterOrEquals(timestamp, toDateTime64('2024-05-01 00:00:00.000000', 6, 'UTC')), lessOrEquals(timestamp, toDateTime64('2024-06-30 23:59:59.999999', 6, 'UTC'))), ifNull(equals(step_1, 1), 0))
UNION ALL SELECT e.close_date AS timestamp,
- e.id AS aggregation_target,
+ toString(e.id) AS aggregation_target,
tupleElement(tuple(throwIf(isNull(e.id), 'Encountered a null value in posthog_test_salesforce_opportunity.id, but a non-null value is required. Please ensure this column contains no null values, or add a filter to exclude rows with null values.'), toUUIDOrDefault(toString(e.id), reinterpretAsUUID(hex(MD5(concat('posthog_test_salesforce_opportunity_2_', ifNull(toString(e.id), ''))))))), 2) AS uuid,
NULL AS `$session_id`,
NULL AS `$window_id`,
diff --git a/posthog/hogql_queries/insights/funnels/test/breakdown_cases.py b/posthog/hogql_queries/insights/funnels/test/breakdown_cases.py
index 01c5230fcb53..a9a0b31c5c3b 100644
--- a/posthog/hogql_queries/insights/funnels/test/breakdown_cases.py
+++ b/posthog/hogql_queries/insights/funnels/test/breakdown_cases.py
@@ -4,7 +4,6 @@ from datetime import datetime
from pathlib import Path
from string import ascii_lowercase
from typing import Any, Literal, Optional, Union, cast
-from uuid import UUID
from freezegun import freeze_time
from posthog.test.base import (
@@ -3514,11 +3513,11 @@ def funnel_breakdown_test_factory(funnel_order_type: FunnelOrderType):
self.assertCountEqual(
self._get_actor_ids_at_step(funnels_query, 1, "Chrome"),
- [people["person1"].uuid],
+ [str(people["person1"].uuid)],
)
self.assertCountEqual(
self._get_actor_ids_at_step(funnels_query, 2, "Chrome"),
- [people["person1"].uuid],
+ [str(people["person1"].uuid)],
)
# unordered funnels include a '' breakdown value, as the data warehouse series can be the first event too
@@ -3544,10 +3543,10 @@ def funnel_breakdown_test_factory(funnel_order_type: FunnelOrderType):
self.assertCountEqual(
self._get_actor_ids_at_step(funnels_query, 1, ""),
[
- UUID("d1f7bc7b-8378-3015-4347-e60e2d2f6348"),
- UUID("4bee5d74-a588-a205-45ef-69db7f5e8bc2"),
- UUID("8cadb28f-1825-f158-73fa-3f228865b540"),
- UUID("cf6a408b-b00d-2458-7b24-9321c13033ec"),
+ "d1f7bc7b-8378-3015-4347-e60e2d2f6348",
+ "4bee5d74-a588-a205-45ef-69db7f5e8bc2",
+ "8cadb28f-1825-f158-73fa-3f228865b540",
+ "cf6a408b-b00d-2458-7b24-9321c13033ec",
],
)
self.assertCountEqual(
@@ -3573,7 +3572,7 @@ def funnel_breakdown_test_factory(funnel_order_type: FunnelOrderType):
self.assertCountEqual(
self._get_actor_ids_at_step(funnels_query, 1, "Firefox"),
- [people["person2"].uuid],
+ [str(people["person2"].uuid)],
)
self.assertCountEqual(
self._get_actor_ids_at_step(funnels_query, 2, "Firefox"),
@@ -3657,11 +3656,11 @@ def funnel_breakdown_test_factory(funnel_order_type: FunnelOrderType):
self.assertCountEqual(
self._get_actor_ids_at_step(funnels_query, 1, "val1"),
- [people["person1"].uuid],
+ [str(people["person1"].uuid)],
)
self.assertCountEqual(
self._get_actor_ids_at_step(funnels_query, 2, "val1"),
- [people["person1"].uuid],
+ [str(people["person1"].uuid)],
)
# unordered funnels include a '' breakdown value, as the data warehouse series can be the first event too
@@ -3687,10 +3686,10 @@ def funnel_breakdown_test_factory(funnel_order_type: FunnelOrderType):
self.assertCountEqual(
self._get_actor_ids_at_step(funnels_query, 1, ""),
[
- UUID("d1f7bc7b-8378-3015-4347-e60e2d2f6348"),
- UUID("4bee5d74-a588-a205-45ef-69db7f5e8bc2"),
- UUID("8cadb28f-1825-f158-73fa-3f228865b540"),
- UUID("cf6a408b-b00d-2458-7b24-9321c13033ec"),
+ "d1f7bc7b-8378-3015-4347-e60e2d2f6348",
+ "4bee5d74-a588-a205-45ef-69db7f5e8bc2",
+ "8cadb28f-1825-f158-73fa-3f228865b540",
+ "cf6a408b-b00d-2458-7b24-9321c13033ec",
],
)
self.assertCountEqual(
@@ -3716,7 +3715,7 @@ def funnel_breakdown_test_factory(funnel_order_type: FunnelOrderType):
self.assertCountEqual(
self._get_actor_ids_at_step(funnels_query, 1, "val2"),
- [people["person2"].uuid],
+ [str(people["person2"].uuid)],
)
self.assertCountEqual(
self._get_actor_ids_at_step(funnels_query, 2, "val2"),
@@ -3790,11 +3789,11 @@ def funnel_breakdown_test_factory(funnel_order_type: FunnelOrderType):
)
self.assertCountEqual(
self._get_actor_ids_at_step(funnels_query, 1, "payment_succeeded"),
- [people["person1"].uuid],
+ [str(people["person1"].uuid)],
)
self.assertCountEqual(
self._get_actor_ids_at_step(funnels_query, 2, "payment_succeeded"),
- [people["person1"].uuid],
+ [str(people["person1"].uuid)],
)
# '' breakdown
@@ -3818,9 +3817,9 @@ def funnel_breakdown_test_factory(funnel_order_type: FunnelOrderType):
self.assertCountEqual(
self._get_actor_ids_at_step(funnels_query, 1, ""),
[
- UUID("d1f7bc7b-8378-3015-4347-e60e2d2f6348"),
- UUID("4bee5d74-a588-a205-45ef-69db7f5e8bc2"),
- UUID("cf6a408b-b00d-2458-7b24-9321c13033ec"),
+ "d1f7bc7b-8378-3015-4347-e60e2d2f6348",
+ "4bee5d74-a588-a205-45ef-69db7f5e8bc2",
+ "cf6a408b-b00d-2458-7b24-9321c13033ec",
],
)
self.assertCountEqual(
@@ -3845,7 +3844,7 @@ def funnel_breakdown_test_factory(funnel_order_type: FunnelOrderType):
self.assertCountEqual(
self._get_actor_ids_at_step(funnels_query, 1, "payment_failed"),
- [UUID("8cadb28f-1825-f158-73fa-3f228865b540")],
+ ["8cadb28f-1825-f158-73fa-3f228865b540"],
)
self.assertCountEqual(
self._get_actor_ids_at_step(funnels_query, 2, "payment_failed"),
diff --git a/posthog/hogql_queries/insights/funnels/test/test_funnel_data_warehouse.py b/posthog/hogql_queries/insights/funnels/test/test_funnel_data_warehouse.py
index 8694857f2dd1..c5236067726a 100644
--- a/posthog/hogql_queries/insights/funnels/test/test_funnel_data_warehouse.py
+++ b/posthog/hogql_queries/insights/funnels/test/test_funnel_data_warehouse.py
@@ -13,10 +13,12 @@ from posthog.test.base import (
)
from posthog.schema import (
+ ActorsQuery,
DataWarehousePersonPropertyFilter,
DataWarehousePropertyFilter,
DateRange,
EventsNode,
+ FunnelsActorsQuery,
FunnelsDataWarehouseNode,
FunnelsFilter,
FunnelsQuery,
@@ -25,6 +27,7 @@ from posthog.schema import (
)
from posthog.errors import ExposedCHQueryError
+from posthog.hogql_queries.actors_query_runner import ActorsQueryRunner
from posthog.hogql_queries.insights.funnels.funnels_query_runner import FunnelsQueryRunner
from posthog.test.test_journeys import journeys_for
from posthog.types import AnyPropertyFilter
@@ -261,6 +264,75 @@ class TestFunnelDataWarehouse(ClickhouseTestMixin, BaseTest):
assert results[0]["count"] == 2
assert results[1]["count"] == 2
+ def _string_aggregation_target_funnels_query(self, table_name: str) -> FunnelsQuery:
+ return FunnelsQuery(
+ kind="FunnelsQuery",
+ dateRange=DateRange(date_from="2025-11-01"),
+ series=[
+ EventsNode(event="$pageview"),
+ FunnelsDataWarehouseNode(
+ id=table_name,
+ table_name=table_name,
+ id_field="uuid",
+ aggregation_target_field="user_id",
+ timestamp_field="created",
+ ),
+ ],
+ )
+
+ @snapshot_clickhouse_queries
+ def test_funnels_data_warehouse_and_regular_nodes_string_aggregation_target(self):
+ # A mixed funnel where the warehouse series aggregates by a plain string
+ # column (not cast to UUID) must not fail the UNION ALL with NO_COMMON_TYPE
+ # against the events series' person_id UUID.
+ table_name = self.setup_data_warehouse()
+ with freeze_time("2025-11-07"):
+ _create_person(
+ distinct_ids=["person1"],
+ team_id=self.team.pk,
+ uuid="bc53b62b-7cc4-b3b8-0688-c6ee3dfb8539",
+ )
+ journeys_for(
+ {"person1": [{"event": "$pageview", "timestamp": datetime(2025, 11, 1, 0, 0, 0)}]},
+ self.team,
+ create_people=False,
+ )
+
+ funnels_query = self._string_aggregation_target_funnels_query(table_name)
+ response = FunnelsQueryRunner(query=funnels_query, team=self.team, just_summarize=True).calculate()
+
+ results = response.results
+ assert results[0]["count"] == 1
+ assert results[1]["count"] == 1
+
+ @snapshot_clickhouse_queries
+ def test_funnels_data_warehouse_and_regular_nodes_string_aggregation_target_actors(self):
+ # The actors drill-down INNER-joins person.id (UUID) against the funnel
+ # actor_id, which is a String for a mixed funnel. Exercise it to confirm
+ # the stringified actor id still resolves to a person.
+ table_name = self.setup_data_warehouse()
+ with freeze_time("2025-11-07"):
+ _create_person(
+ distinct_ids=["person1"],
+ team_id=self.team.pk,
+ uuid="bc53b62b-7cc4-b3b8-0688-c6ee3dfb8539",
+ )
+ journeys_for(
+ {"person1": [{"event": "$pageview", "timestamp": datetime(2025, 11, 1, 0, 0, 0)}]},
+ self.team,
+ create_people=False,
+ )
+
+ funnels_query = self._string_aggregation_target_funnels_query(table_name)
+ actors_query = ActorsQuery(
+ source=FunnelsActorsQuery(source=funnels_query, funnelStep=2),
+ select=["id", "person"],
+ )
+ response = ActorsQueryRunner(query=actors_query, team=self.team).calculate()
+
+ actor_ids = [str(row[0]) for row in response.results]
+ assert actor_ids == ["bc53b62b-7cc4-b3b8-0688-c6ee3dfb8539"]
+
@snapshot_clickhouse_queries
def test_funnels_data_warehouse_non_uuid_id_column(self):
table_name = self.setup_data_warehouse()
diff --git a/posthog/hogql_queries/insights/funnels/test/test_funnel_event_query.py b/posthog/hogql_queries/insights/funnels/test/test_funnel_event_query.py
index be865b333f23..31b226eaa704 100644
--- a/posthog/hogql_queries/insights/funnels/test/test_funnel_event_query.py
+++ b/posthog/hogql_queries/insights/funnels/test/test_funnel_event_query.py
@@ -232,7 +232,7 @@ class TestFunnelEventQuery(ClickhouseTestMixin, APIBaseTest):
select_1 = format_query(funnel_event_query.select_from.table.initial_select_query) # type: ignore
expected_1 = dedent("""
SELECT e.timestamp AS timestamp,
- person_id AS aggregation_target,
+ toString(person_id) AS aggregation_target,
if(and(equals(event, '$pageview'), equals(properties.$browser, 'Opera')), 1, 0) AS step_0,
0 AS step_1,
0 AS step_2,
@@ -245,7 +245,7 @@ class TestFunnelEventQuery(ClickhouseTestMixin, APIBaseTest):
select_2 = format_query(funnel_event_query.select_from.table.subsequent_select_queries[0].select_query) # type: ignore
expected_2 = dedent("""
SELECT e.created_at AS timestamp,
- user_id AS aggregation_target,
+ toString(user_id) AS aggregation_target,
0 AS step_0,
if(and(1, equals(some_prop, 'some_value')), 1, 0) AS step_1,
0 AS step_2,
@@ -258,7 +258,7 @@ class TestFunnelEventQuery(ClickhouseTestMixin, APIBaseTest):
select_3 = format_query(funnel_event_query.select_from.table.subsequent_select_queries[1].select_query) # type: ignore
expected_3 = dedent("""
SELECT e.ts AS timestamp,
- some_user_id AS aggregation_target,
+ toString(some_user_id) AS aggregation_target,
0 AS step_0,
0 AS step_1,
if(and(1, equals(another_prop, 'another_value')), 1, 0) AS step_2,