From c80a90460d7caab62ec2245823a74db81910b3d1 Mon Sep 17 00:00:00 2001 From: timgl Date: Sat, 5 Oct 2024 20:30:54 +0100 Subject: [PATCH] fix: Unsupported constant type (#25413) --- posthog/hogql/functions/mapping.py | 1 + posthog/hogql_queries/events_query_runner.py | 20 +++++++++---------- .../test/test_events_query_runner.py | 8 ++++---- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/posthog/hogql/functions/mapping.py b/posthog/hogql/functions/mapping.py index 4d81087524316..dfe50a5b67a2b 100644 --- a/posthog/hogql/functions/mapping.py +++ b/posthog/hogql/functions/mapping.py @@ -428,6 +428,7 @@ def compare_types(arg_types: list[ConstantType], sig_arg_types: tuple[ConstantTy "parseDateTime": HogQLFunctionMeta("parseDateTimeOrNull", 2, 3, tz_aware=True), "parseDateTimeBestEffort": HogQLFunctionMeta("parseDateTime64BestEffortOrNull", 1, 2, tz_aware=True), "toTypeName": HogQLFunctionMeta("toTypeName", 1, 1), + "cityHash64": HogQLFunctionMeta("cityHash64", 1, 1), # dates and times "toTimeZone": HogQLFunctionMeta("toTimeZone", 2, 2), "timeZoneOf": HogQLFunctionMeta("timeZoneOf", 1, 1), diff --git a/posthog/hogql_queries/events_query_runner.py b/posthog/hogql_queries/events_query_runner.py index e657485f4e1d8..72f843ef607a7 100644 --- a/posthog/hogql_queries/events_query_runner.py +++ b/posthog/hogql_queries/events_query_runner.py @@ -114,17 +114,15 @@ def to_query(self) -> ast.SelectQuery: Person.objects.filter(team=self.team), self.query.personId ).first() where_exprs.append( - parse_expr( - "cityHash64(distinct_id) in {list}", # Because the events table is partitioned by cityHash64(distinct_ids), using cityhash for the comparison is much quicker, - { - "list": ast.Constant( - value=[ - ast.Call(name="cityHash64", args=[ast.Constant(value=id)]) - for id in get_distinct_ids_for_subquery(person, self.team) - ] - ) - }, - timings=self.timings, + ast.CompareOperation( + left=ast.Call(name="cityHash64", args=[ast.Field(chain=["distinct_id"])]), + right=ast.Tuple( + exprs=[ + ast.Call(name="cityHash64", args=[ast.Constant(value=id)]) + for id in get_distinct_ids_for_subquery(person, self.team) + ] + ), + op=ast.CompareOperationOp.In, ) ) if self.query.filterTestAccounts: diff --git a/posthog/hogql_queries/test/test_events_query_runner.py b/posthog/hogql_queries/test/test_events_query_runner.py index dcb814339e943..6cdf57acebcdc 100644 --- a/posthog/hogql_queries/test/test_events_query_runner.py +++ b/posthog/hogql_queries/test/test_events_query_runner.py @@ -130,15 +130,15 @@ def test_person_id_expands_to_distinct_ids(self): # matching team query_ast = EventsQueryRunner(query=query, team=self.team).to_query() where_expr = cast(ast.CompareOperation, cast(ast.And, query_ast.where).exprs[0]) - right_expr = cast(ast.Constant, where_expr.right) - self.assertEqual([x.args[0].value for x in right_expr.value], ["id1", "id2"]) + right_expr = cast(ast.Tuple, where_expr.right) + self.assertEqual([x.args[0].value for x in right_expr.exprs], ["id1", "id2"]) # another team another_team = Team.objects.create(organization=Organization.objects.create()) query_ast = EventsQueryRunner(query=query, team=another_team).to_query() where_expr = cast(ast.CompareOperation, cast(ast.And, query_ast.where).exprs[0]) - right_expr = cast(ast.Constant, where_expr.right) - self.assertEqual(right_expr.value, []) + right_expr = cast(ast.Tuple, where_expr.right) + self.assertEqual(right_expr.exprs, []) def test_test_account_filters(self): self.team.test_account_filters = [