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(data-warehouse): coerce numeric warehouse join keys compared to strings (#130046)

Thiago Salvatorecd705d2b56b3
diff --git a/posthog/hogql/database/test/test_lazy_join_resolvers.py b/posthog/hogql/database/test/test_lazy_join_resolvers.py
index 8403fc94776e..452957e62848 100644
--- a/posthog/hogql/database/test/test_lazy_join_resolvers.py
+++ b/posthog/hogql/database/test/test_lazy_join_resolvers.py
@@ -22,6 +22,10 @@ from posthog.hogql.database.warehouse_join_resolvers import (
resolve_data_warehouse_join,
)
from posthog.hogql.errors import QueryError, ResolutionError
+from posthog.hogql.parser import parse_select
+from posthog.hogql.printer import prepare_and_print_ast
+
+from posthog.models import Organization, Team
from products.data_tools.backend.models.join import DataWarehouseJoin
@@ -144,6 +148,65 @@ class TestLazyJoinResolvers(SimpleTestCase):
(ast.CompareOperationOp.LtEq, "2023-02-01"),
]
+ @parameterized.expand(
+ [
+ (
+ "string_source_numeric_joining",
+ "events",
+ "distinct_id",
+ "static_cohort_people",
+ "cohort_id",
+ "ON equals(events.distinct_id, toString(events__linked.events__linked___cohort_id))",
+ ),
+ (
+ "numeric_source_string_joining",
+ "static_cohort_people",
+ "cohort_id",
+ "events",
+ "distinct_id",
+ "ON equals(toString(person_static_cohort.cohort_id), "
+ "static_cohort_people__linked.static_cohort_people__linked___distinct_id)",
+ ),
+ ]
+ )
+ def test_data_warehouse_join_compares_a_numeric_key_to_a_string_key_as_strings(
+ self,
+ _name: str,
+ source_table: str,
+ source_key: str,
+ joining_table: str,
+ joining_key: str,
+ expected_constraint: str,
+ ) -> None:
+ database = Database()
+ database.get_table(source_table).fields["linked"] = LazyJoin(
+ from_field=[source_key],
+ to_field=[joining_key],
+ join_table=joining_table,
+ resolver=DATA_WAREHOUSE,
+ resolver_params=data_warehouse_resolver_params(
+ source_table_key=source_key,
+ joining_table_key=joining_key,
+ joining_table_name=joining_table,
+ ),
+ )
+ context = HogQLContext(
+ team_id=1,
+ team=Team(id=1, organization=Organization()),
+ database=database,
+ enable_select_queries=True,
+ use_new_events_schema=False,
+ apply_events_retention_floor=False,
+ modifiers=HogQLQueryModifiers(),
+ restricted_properties=set(),
+ )
+
+ printed, _ = prepare_and_print_ast(
+ parse_select(f"SELECT linked.{joining_key} FROM {source_table}"), context, "clickhouse"
+ )
+
+ assert expected_constraint in printed
+
def _walk_lazy_joins(database: Database) -> Iterator[tuple[str, LazyJoin]]:
"""Yield every LazyJoin reachable from the database's table tree, with its path for error messages."""
diff --git a/posthog/hogql/transforms/lazy_tables.py b/posthog/hogql/transforms/lazy_tables.py
index 6ed3db4982c3..a115d8332f37 100644
--- a/posthog/hogql/transforms/lazy_tables.py
+++ b/posthog/hogql/transforms/lazy_tables.py
@@ -5,6 +5,7 @@ from posthog.hogql import ast
from posthog.hogql.base import _T_AST
from posthog.hogql.constants import HogQLDialect
from posthog.hogql.context import HogQLContext
+from posthog.hogql.database.lazy_join_tags import DATA_WAREHOUSE, DATA_WAREHOUSE_EXPERIMENTS
from posthog.hogql.database.models import LazyJoinToAdd, LazyTableToAdd, UnexpandedScan
from posthog.hogql.database.schema.events_with_flag_calls import EventsWithFlagCallsTable
from posthog.hogql.errors import ResolutionError
@@ -97,6 +98,39 @@ def find_field_chains(node: ast.AST | None) -> list[list[str | int]]:
return finder.field_chains
+DATA_WAREHOUSE_RESOLVERS = frozenset({DATA_WAREHOUSE, DATA_WAREHOUSE_EXPERIMENTS})
+
+
+def _constant_type_of(expr: ast.Expr, context: HogQLContext) -> ast.ConstantType | None:
+ return expr.type.resolve_constant_type(context) if expr.type is not None else None
+
+
+def _as_string(expr: ast.Expr, integer_type: ast.ConstantType) -> ast.Call:
+ return ast.Call(name="toString", args=[expr], type=ast.StringType(nullable=integer_type.nullable))
+
+
+def _stringify_integer_side_compared_to_string(comparison: ast.CompareOperation, context: HogQLContext) -> None:
+ if comparison.op != ast.CompareOperationOp.Eq:
+ return
+ left_type = _constant_type_of(comparison.left, context)
+ right_type = _constant_type_of(comparison.right, context)
+ if isinstance(left_type, ast.StringType) and isinstance(right_type, ast.IntegerType):
+ comparison.right = _as_string(comparison.right, right_type)
+ elif isinstance(right_type, ast.StringType) and isinstance(left_type, ast.IntegerType):
+ comparison.left = _as_string(comparison.left, left_type)
+
+
+def _stringify_integer_join_keys_compared_to_strings(
+ constraint: ast.JoinConstraint | None, context: HogQLContext
+) -> None:
+ if constraint is None:
+ return
+ comparisons = constraint.expr.exprs if isinstance(constraint.expr, ast.And) else [constraint.expr]
+ for comparison in comparisons:
+ if isinstance(comparison, ast.CompareOperation):
+ _stringify_integer_side_compared_to_string(comparison, context)
+
+
def get_table_alias_for_lazy_join(lazy_join_type: ast.LazyJoinType) -> str | None:
table_type = lazy_join_type.table_type
while table_type:
@@ -821,6 +855,8 @@ class LazyTableResolver(TraversingVisitor):
context=self.context,
setTimeZones=False,
).visit(join_to_add)
+ if join_scope.lazy_join.resolver in DATA_WAREHOUSE_RESOLVERS:
+ _stringify_integer_join_keys_compared_to_strings(join_to_add.constraint, self.context)
if join_to_add.type is not None:
select_type.tables[to_table] = join_to_add.type