: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
feat(hogql): compile cityhash64 to the trino fork's ch_cityhash64 (#129127)
Jovan Sakoviceacd8c31f85c
diff --git a/posthog/hogql/printer/test/test_trino_printer.py b/posthog/hogql/printer/test/test_trino_printer.pyindex 1f19e047b606..82e728495770 100644--- a/posthog/hogql/printer/test/test_trino_printer.py+++ b/posthog/hogql/printer/test/test_trino_printer.py@@ -637,7 +637,9 @@ def test_tracks_every_registered_function_without_a_trino_mapping(snapshot: Snap"TRINO_REGEX_GROUPS_UNSUPPORTED",),("replaceRegexpOne(user_id, 'a', user_id)", "TRINO_REGEX_CONSTANT_REQUIRED"),- ("cityHash64(user_id)", "TRINO_FUNCTION_UNSUPPORTED"),+ ("cityHash64(uuid)", "TRINO_FUNCTION_ARGUMENTS_UNSUPPORTED"),+ ("cityHash64(user_id, id)", "TRINO_FUNCTION_ARGUMENTS_UNSUPPORTED"),+ ("cityHash64(18446744073709551615)", "TRINO_FUNCTION_ARGUMENTS_UNSUPPORTED"),("toTypeName(user_id)", "TRINO_FUNCTION_UNSUPPORTED"),("bitNot(id)", "TRINO_FUNCTION_UNSUPPORTED"),("range(1, 10, id)", "TRINO_RANGE_STEP_UNSUPPORTED"),@@ -2451,6 +2453,10 @@ def test_prints_all_median_aggregate_combinators(name: str, window: str) -> None("toMonday(created_at)", 'CAST(date_trunc(\'week\', "users"."created_at") AS DATE)'),("yesterday()", "date_add('day', -1, CURRENT_DATE)"),("md5(user_id)", 'to_hex(md5(to_utf8(CAST("users"."user_id" AS VARCHAR))))'),+ ("cityHash64(user_id) % 10", 'ch_cityhash64("users"."user_id")'),+ ("cityHash64(id)", 'ch_cityhash64("users"."id")'),+ ("cityHash64(-1)", "ch_cityhash64(CAST(-1 AS TINYINT))"),+ ("cityHash64(id = 1)", 'ch_cityhash64(CAST(("users"."id" = 1) AS TINYINT))'),("power(2, 3)", "power(2, 3)"),("ln(2)", "ln(2)"),("log2(2)", "log2(2)"),diff --git a/posthog/hogql/printer/trino.py b/posthog/hogql/printer/trino.pyindex 957e11f0ba5e..78b69cf5bd4e 100644--- a/posthog/hogql/printer/trino.py+++ b/posthog/hogql/printer/trino.py@@ -908,11 +908,7 @@ class TrinoPrinter(PostgresPrinter):node,)if name == "cityhash64":- self._unsupported(- "TRINO_FUNCTION_UNSUPPORTED",- f"{node.name} has no semantics-preserving Trino implementation.",- node,- )+ return self._visit_city_hash_64(node)if name == "totypename":self._unsupported("TRINO_FUNCTION_UNSUPPORTED",@@ -3351,6 +3347,40 @@ class TrinoPrinter(PostgresPrinter):return f"IF({value} IS NULL, NULL, IF({value}, '1', '0'))"return f"CAST({value} AS VARCHAR)"+ def _visit_city_hash_64(self, node: ast.Call) -> str:+ # ch_cityhash64 comes from the trino-posthog-functions plugin in the PostHog Trino fork.+ # It returns DECIMAL(20,0), so values equal ClickHouse's UInt64.+ # ClickHouse hashes an integer as its unsigned bit pattern at the integer's own width,+ # so the plugin has one overload per Trino integer width.+ value = self._visit_unary_arg(node)+ value_type = self._resolve_type(node.args[0])+ if isinstance(value_type, ast.StringType):+ return f"ch_cityhash64({value})"+ if isinstance(value_type, ast.BooleanType):+ return f"ch_cityhash64(CAST({value} AS TINYINT))"+ if isinstance(value_type, ast.IntegerType):+ arg = node.args[0]+ if isinstance(arg, ast.Constant) and isinstance(arg.value, int):+ return f"ch_cityhash64(CAST({value} AS {self._city_hash_64_literal_type(node, arg.value)}))"+ return f"ch_cityhash64({value})"+ self._invalid_function_arguments(+ node, "cityHash64 in Trino mode supports only one String, integer or boolean argument."+ )++ def _city_hash_64_literal_type(self, node: ast.Call, value: int) -> str:+ # ClickHouse types an integer literal as the smallest type that fits it.+ if value > 2**63 - 1:+ self._invalid_function_arguments(+ node, "cityHash64 in Trino mode supports integer literals up to 9223372036854775807."+ )+ if value >= -(2**7):+ return "TINYINT" if value < 0 else "BIGINT"+ if value >= -(2**15):+ return "SMALLINT"+ if value >= -(2**31):+ return "INTEGER"+ return "BIGINT"+def _visit_to_timestamp(self, node: ast.Call) -> str:if len(node.args) != 1 or not isinstance(self._resolve_type(node.args[0]), ast.IntegerType):self._invalid_function_arguments(node, "to_timestamp requires one integer Unix timestamp in Trino mode.")