: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
chore(hogql): pbt parser equivalence compares rust-py vs cpp-json (#60765)
Robbie46bee37a9e3e
diff --git a/posthog/hogql/test/test_parser_pbt.py b/posthog/hogql/test/test_parser_pbt.pyindex 452fbb77a5aa..4259b1647e18 100644--- a/posthog/hogql/test/test_parser_pbt.py+++ b/posthog/hogql/test/test_parser_pbt.py@@ -6,8 +6,8 @@ printer normalises some constructs (e.g. ``1 + 2`` → ``plus(1, 2)``), wecompare at the *printed-string* level: ``print(parse(print(ast))) == print(ast)``.As a bonus this also tests **parser backend equivalence**: every generated-string is parsed with both the Python and C++ backends and the results are-compared.+string is parsed with both the rust-py and cpp-json backends and the results+are compared."""import os@@ -312,21 +312,21 @@ def _print_hogql(node: ast.Expr) -> str:def _parse_both_backends(hogql_string: str) -> tuple[ast.Expr | None, ast.Expr | None]:- """Parse with both backends, returning (python_ast, cpp_ast).+ """Parse with both backends, returning (rust_ast, cpp_ast).Returns None for a backend if parsing fails."""- py_ast: ast.Expr | None = None+ rust_ast: ast.Expr | None = Nonecpp_ast: ast.Expr | None = Nonetry:- py_ast = parse_expr(hogql_string, backend="python")+ rust_ast = parse_expr(hogql_string, backend="rust-py")except BaseHogQLError:passtry:cpp_ast = parse_expr(hogql_string, backend="cpp-json")except BaseHogQLError:pass- return py_ast, cpp_ast+ return rust_ast, cpp_astdef _roundtrip_check(expr: ast.Expr) -> None:@@ -363,37 +363,37 @@ class TestParserBackendEquivalence:@given(expr=_expr_strategy())@settings(max_examples=2000, deadline=None, suppress_health_check=[HealthCheck.too_slow])- def test_python_and_cpp_backends_agree(self, expr: ast.Expr) -> None:+ def test_rust_py_and_cpp_backends_agree(self, expr: ast.Expr) -> None:try:hogql_string = _print_hogql(expr)except BaseHogQLError:assume(False)return # type: ignore[unreachable]- py_ast, cpp_ast = _parse_both_backends(hogql_string)+ rust_ast, cpp_ast = _parse_both_backends(hogql_string)- if py_ast is None and cpp_ast is None:+ if rust_ast is None and cpp_ast is None:assume(False)return # type: ignore[unreachable]# If one succeeds and the other fails, that's a bug- assert (py_ast is None) == (cpp_ast is None), (+ assert (rust_ast is None) == (cpp_ast is None), (f"Backend disagreement on parsability of: {hogql_string!r}\n"- f" Python: {'failed' if py_ast is None else 'ok'}\n"- f" C++: {'failed' if cpp_ast is None else 'ok'}"+ f" Rust: {'failed' if rust_ast is None else 'ok'}\n"+ f" C++: {'failed' if cpp_ast is None else 'ok'}")- assert py_ast is not None and cpp_ast is not None+ assert rust_ast is not None and cpp_ast is not None# Compare by printing both back to HogQL — this normalises away# any minor structural differences (like start/end positions).- py_printed = _print_hogql(py_ast)+ rust_printed = _print_hogql(rust_ast)cpp_printed = _print_hogql(cpp_ast)- assert py_printed == cpp_printed, (+ assert rust_printed == cpp_printed, (f"Backend outputs differ for: {hogql_string!r}\n"- f" Python backend: {py_printed!r}\n"- f" C++ backend: {cpp_printed!r}"+ f" Rust backend: {rust_printed!r}\n"+ f" C++ backend: {cpp_printed!r}")