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(hogql): type trino plus/minus/multiply calls as float in toInt (#128159)

Jovan Sakovic1ad0869c4869
diff --git a/posthog/hogql/printer/test/test_trino_printer.py b/posthog/hogql/printer/test/test_trino_printer.py
index a2f3996aa683..1f19e047b606 100644
--- a/posthog/hogql/printer/test/test_trino_printer.py
+++ b/posthog/hogql/printer/test/test_trino_printer.py
@@ -742,6 +742,12 @@ def test_regex_character_classes_keep_clickhouse_meaning(pattern: str, expected:
"__hogql_accurate_cast_value",
"CAST(ceil(",
),
+ (
+ "SELECT toInt(ceil(plus(multiply(divide(minus(a, n), minus(n, a)), n), a))) "
+ "FROM (SELECT sum(id) AS a, count() AS n FROM users)",
+ "__hogql_accurate_cast_value",
+ "CAST(ceil(",
+ ),
# Trino reads ORDER BY names as output columns first, so the qualifier `t` would read the `0 AS t` column.
(
"WITH t AS (SELECT 1 AS x) SELECT x AS x, 0 AS t FROM t ORDER BY x + 0",
diff --git a/posthog/hogql/printer/trino.py b/posthog/hogql/printer/trino.py
index 6f0d9e9a1881..957e11f0ba5e 100644
--- a/posthog/hogql/printer/trino.py
+++ b/posthog/hogql/printer/trino.py
@@ -1545,18 +1545,22 @@ class TrinoPrinter(PostgresPrinter):
):
division_type = node.type.resolve_constant_type(self.context) if node.type is not None else None
return ast.FloatType(nullable=division_type.nullable if division_type is not None else True)
+ sides: tuple[ast.Expr, ast.Expr] | None = None
+ if isinstance(node, ast.ArithmeticOperation) and node.op in (
+ ast.ArithmeticOperationOp.Add,
+ ast.ArithmeticOperationOp.Sub,
+ ast.ArithmeticOperationOp.Mult,
+ ):
+ sides = (node.left, node.right)
+ elif isinstance(node, ast.Call) and node.name.lower() in {"plus", "minus", "multiply"} and len(node.args) == 2:
+ sides = (node.args[0], node.args[1])
if (
- isinstance(node, ast.ArithmeticOperation)
- and node.op
- in (ast.ArithmeticOperationOp.Add, ast.ArithmeticOperationOp.Sub, ast.ArithmeticOperationOp.Mult)
- and any(isinstance(self._resolve_type(side), ast.FloatType) for side in (node.left, node.right))
+ sides is not None
+ and any(isinstance(self._resolve_type(side), ast.FloatType) for side in sides)
# Date arithmetic prints as date_add, which keeps the date type.
- and not any(
- isinstance(self._resolve_type(side), (ast.DateType, ast.DateTimeType))
- for side in (node.left, node.right)
- )
+ and not any(isinstance(self._resolve_type(side), (ast.DateType, ast.DateTimeType)) for side in sides)
):
- side_types = [self._resolve_type(side) for side in (node.left, node.right)]
+ side_types = [self._resolve_type(side) for side in sides]
return ast.FloatType(nullable=any(side_type is None or side_type.nullable for side_type in side_types))
# ClickHouse rounding functions return their argument's type, but the function registry declares no signature.
if isinstance(node, ast.Call) and node.name.lower() in {"floor", "ceil", "trunc", "round"} and node.args: