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): rust parser parity in FILTER (WHERE) + PBT grind tooling fixes (#64081)

Robbie2cc6f03cea7e
diff --git a/posthog/hogql/scripts/pbt_diagnostic.py b/posthog/hogql/scripts/pbt_diagnostic.py
index fdaf71be3496..19fc4c174bef 100644
--- a/posthog/hogql/scripts/pbt_diagnostic.py
+++ b/posthog/hogql/scripts/pbt_diagnostic.py
@@ -2,10 +2,13 @@
Reuses the strategies from `test_parser_grammar_pbt.py` (the
auto-generated grammar PBT introduced in PR #58627) but runs as an
-ad-hoc CLI rather than a pytest collection. Backend-agnostic:
-defaults to `cpp-json` vs `rust-json` (the two compiled backends);
-point `--candidate` at `python` or any other backend in a feature
-branch that adds one.
+ad-hoc CLI rather than a pytest collection. Defaults to `cpp-json` vs
+`rust-py` (the primary parity target — `rust-json` was a stepping
+stone and may end up in a future wasm build, but isn't the primary
+production candidate). The `python` antlr4 visitor is intentionally
+excluded from `--oracle` / `--candidate` — it's order-of-magnitude
+slower and useless for the tight diagnose-then-fix loop the grind is
+built for.
Distinct from the pytest PBT in five ways:
@@ -38,7 +41,7 @@ The shared parse / AST-diff / divergence-shape vocabulary lives in
Typical usage:
- # Default: cpp-json vs python (works in master out of the box)
+ # Default: cpp-json vs rust-py
PYTHONPATH=. python posthog/hogql/scripts/pbt_diagnostic.py --n 5000
# With auto-shrinking on every failure
@@ -81,6 +84,9 @@ django.setup()
from hypothesis import assume, given, settings
+# Eager so a missing `hogql-parser-parity` dep group fails at script-load,
+# not mid-grind where shrink_to_shape would lose every finding so far.
+from posthog.hogql.scripts import _shrink # noqa: F401
from posthog.hogql.scripts._diagnostic_common import (
DivergenceShape,
_ast_mismatch_shape,
@@ -179,15 +185,19 @@ def main() -> int:
"so the match denominator is exactly --n."
),
)
+ # `python` excluded — too slow for the diagnose-then-fix loop (see module docstring).
+ _BACKENDS = ("cpp-json", "rust-json", "rust-py")
parser.add_argument(
"--oracle",
default=os.environ.get("ORACLE_BACKEND", "cpp-json"),
+ choices=_BACKENDS,
help="Source-of-truth backend (default: cpp-json)",
)
parser.add_argument(
"--candidate",
- default=os.environ.get("CANDIDATE_BACKEND", "rust-json"),
- help="Backend under test (default: rust-json; override in feature branches that add a fourth backend)",
+ default=os.environ.get("CANDIDATE_BACKEND", "rust-py"),
+ choices=_BACKENDS,
+ help="Backend under test (default: rust-py)",
)
parser.add_argument(
"--max-mismatch-samples",
@@ -391,8 +401,8 @@ def main() -> int:
"oracle": oracle,
"candidate": candidate,
"query": query,
- "oracle_root": bucket[0],
- "candidate_root": bucket[1],
+ "oracle_root": mismatch_bucket[0],
+ "candidate_root": mismatch_bucket[1],
"diff_path": steps,
},
shrunk,
diff --git a/posthog/hogql/test/_test_parser.py b/posthog/hogql/test/_test_parser.py
index ea926a77ad6f..b4d8be2d3c85 100644
--- a/posthog/hogql/test/_test_parser.py
+++ b/posthog/hogql/test/_test_parser.py
@@ -7897,6 +7897,39 @@ def parser_test_factory(backend: HogQLParserBackend):
with self.assertRaises(BaseHogQLError):
parse_expr("count() filter ()", backend=backend)
+ def test_filter_where_body_grammar_parsed_visitor_discarded(self):
+ # cpp's window FILTER (with OVER) grammar-parses the WHERE body but
+ # never visits it, so DATE/TIMESTAMP/INTERVAL string literals and
+ # ColumnTypeExprEnum cast types accept; aggregate FILTER (no OVER)
+ # visits and rejects them. Pin both arms of the cpp/rust boundary.
+ # python is excluded: its visitor visits the window-FILTER body
+ # (diverging from cpp) and has a pre-existing `interval ''` crash
+ # (`text.split(" ")` ValueError), neither of which this rust-parity
+ # test is about.
+ if backend == "python":
+ self.skipTest(
+ "python visits the window-FILTER body and pre-existing interval-'' crash; cpp/rust are the parity targets"
+ )
+ for query in (
+ "f() FILTER (WHERE date '') OVER w",
+ "f() FILTER (WHERE timestamp '') OVER w",
+ "f() FILTER (WHERE interval '') OVER w",
+ "f() FILTER (WHERE cast(1 as q('a' = 1))) OVER w",
+ ):
+ self.assertEqual(
+ parse_expr(query, backend="cpp-json"),
+ parse_expr(query, backend=backend),
+ msg=query,
+ )
+ for query in (
+ "f() FILTER (WHERE date '')",
+ "f() FILTER (WHERE timestamp '')",
+ "f() FILTER (WHERE interval '')",
+ "f() FILTER (WHERE cast(1 as q('a' = 1)))",
+ ):
+ with self.assertRaises(BaseHogQLError, msg=query):
+ parse_expr(query, backend=backend)
+
def test_cast_type_enum_vs_param_fallback(self):
# A cast type `q(...)` is `ColumnTypeExprEnum` only when every entry is a
# `string '=' numberLiteral` (the visitor then rejects it as unsupported).
diff --git a/pyproject.toml b/pyproject.toml
index 35f34ca43e8d..132daf1d81b9 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -68,7 +68,7 @@ dependencies = [
"grpcio~=1.71.0",
"gspread==6.2.1",
"hogql-parser==1.3.69",
- "hogql-parser-rs==1.3.88", # built from rust/hogql/parser via the build-hogql-parser-rs workflow + published to PyPI; rebuild locally with `uv pip install -e rust/hogql/parser` when iterating
+ "hogql-parser-rs==1.3.90", # built from rust/hogql/parser via the build-hogql-parser-rs workflow + published to PyPI; rebuild locally with `uv pip install -e rust/hogql/parser` when iterating
"humanize~=4.12.3",
"infi-clickhouse-orm",
"jsonref==1.1.0",
diff --git a/rust/Cargo.lock b/rust/Cargo.lock
index 52cb45b8518e..4c1941d0562c 100644
--- a/rust/Cargo.lock
+++ b/rust/Cargo.lock
@@ -4349,7 +4349,7 @@ dependencies = [
[[package]]
name = "hogql_parser_rs"
-version = "1.3.88"
+version = "1.3.90"
dependencies = [
"pyo3",
"serde_json",
diff --git a/rust/hogql/parser/Cargo.toml b/rust/hogql/parser/Cargo.toml
index 34eeff8e6f52..8a6f8e024f1a 100644
--- a/rust/hogql/parser/Cargo.toml
+++ b/rust/hogql/parser/Cargo.toml
@@ -1,7 +1,7 @@
[package]
name = "hogql_parser_rs"
-# Versioned independently from the cpp parser in `common/hogql_parser/setup.py`. Both started from the same HogQL grammar revision, but the Rust port is bumped on its own whenever the hand-rolled parser changes (cpp is currently 1.3.69, this is 1.3.88); each ships as its own PyPI wheel.
-version = "1.3.88"
+# Versioned independently from the cpp parser in `common/hogql_parser/setup.py`. Both started from the same HogQL grammar revision, but the Rust port is bumped on its own whenever the hand-rolled parser changes (cpp is currently 1.3.69, this is 1.3.90); each ships as its own PyPI wheel.
+version = "1.3.90"
edition = "2021"
publish = false
diff --git a/rust/hogql/parser/pyproject.toml b/rust/hogql/parser/pyproject.toml
index 0500aff8cd87..a5cc08eedab8 100644
--- a/rust/hogql/parser/pyproject.toml
+++ b/rust/hogql/parser/pyproject.toml
@@ -8,8 +8,8 @@ build-backend = "maturin"
[project]
name = "hogql_parser_rs"
-# Mirrors `rust/hogql/parser/Cargo.toml` (always identical). Tracks but is NOT locked to `common/hogql_parser/setup.py`: the Rust port is versioned independently and bumped on its own whenever the hand-rolled parser changes (cpp is currently 1.3.69, this is 1.3.88).
-version = "1.3.88"
+# Mirrors `rust/hogql/parser/Cargo.toml` (always identical). Tracks but is NOT locked to `common/hogql_parser/setup.py`: the Rust port is versioned independently and bumped on its own whenever the hand-rolled parser changes (cpp is currently 1.3.69, this is 1.3.90).
+version = "1.3.90"
description = "Hand-rolled Rust HogQL parser with C++-parity AST output"
readme = "README.md"
requires-python = ">=3.12"
diff --git a/rust/hogql/parser/src/parse/expr.rs b/rust/hogql/parser/src/parse/expr.rs
index f2b3e4358127..6b8d2943eb52 100644
--- a/rust/hogql/parser/src/parse/expr.rs
+++ b/rust/hogql/parser/src/parse/expr.rs
@@ -3281,22 +3281,36 @@ impl<'a, E: Emitter + Clone> Parser<'a, E> {
if !matches!(self.peek(), TokenKind::Keyword(Kw::Filter)) {
return Ok(None);
}
+ // cpp's window FILTER (`VISIT(ColumnExprWinFunction)`) grammar-parses
+ // the WHERE body but never visits it; aggregate FILTER does visit.
+ // We can't peek `OVER` until the body is consumed, so optimistically
+ // parse with both suppressions (window form) and re-parse strictly
+ // below if no OVER follows.
+ let cp_before_filter = self.checkpoint();
self.bump()?;
self.expect(TokenKind::LParen, "(")?;
self.expect_kw(Kw::Where, "WHERE")?;
- // cpp's `VISIT(ColumnExprWinFunction)` parses the FILTER
- // where-expression at the grammar level but never visits it
- // into the AST — so the SelectStmt-visitor semantic checks
- // (ARRAY JOIN without FROM / unaliased ARRAY JOIN arrays)
- // never fire for a subquery nested in here. Suppress them
- // during this parse so `f() FILTER (WHERE (SELECT 1 ARRAY
- // JOIN 2)) OVER w` is accepted the same way.
- let prev = self.suppress_array_join_checks;
+ let prev_array_join = self.suppress_array_join_checks;
+ let prev_unvisited = self.suppress_unvisited_clause_checks;
self.suppress_array_join_checks = true;
+ self.suppress_unvisited_clause_checks = true;
let result = self.parse_expr_bp(0);
- self.suppress_array_join_checks = prev;
+ self.suppress_array_join_checks = prev_array_join;
+ self.suppress_unvisited_clause_checks = prev_unvisited;
let expr = result?;
self.expect(TokenKind::RParen, ")")?;
+ // No OVER → aggregate FILTER. Re-parse strictly so cpp's visit-time
+ // rejections (ARRAY JOIN, DATE/TIMESTAMP/INTERVAL, ColumnTypeExprEnum)
+ // fire.
+ if !matches!(self.peek(), TokenKind::Keyword(Kw::Over)) {
+ self.restore(cp_before_filter)?;
+ self.bump()?;
+ self.expect(TokenKind::LParen, "(")?;
+ self.expect_kw(Kw::Where, "WHERE")?;
+ let strict_expr = self.parse_expr_bp(0)?;
+ self.expect(TokenKind::RParen, ")")?;
+ return Ok(Some(strict_expr));
+ }
Ok(Some(expr))
}
@@ -3498,6 +3512,14 @@ impl<'a, E: Emitter + Clone> Parser<'a, E> {
// cpp ACCEPTS. So gate the reject on the full enum-list shape and let
// the Param path below handle the rest.
if self.paren_body_is_enum_value_list() {
+ if self.suppress_unvisited_clause_checks {
+ // Unvisited clause: consume the enum body, return a placeholder.
+ while self.peek() != TokenKind::RParen {
+ self.bump()?;
+ }
+ self.expect(TokenKind::RParen, ")")?;
+ return Ok(format!("{head_name}(<discarded-enum>)"));
+ }
let start = self.peek0.start;
let end = self.peek0.end;
// Fatal so the outer `try_alt`'s parse_ident_lead
diff --git a/uv.lock b/uv.lock
index 2a8756bd6363..fac761243f9f 100644
--- a/uv.lock
+++ b/uv.lock
@@ -3145,16 +3145,16 @@ wheels = [
[[package]]
name = "hogql-parser-rs"
-version = "1.3.88"
+version = "1.3.90"
source = { registry = "https://pypi.org/simple" }
-sdist = { url = "https://files.pythonhosted.org/packages/0f/66/027b3cd67a113b813c53374fd3e960d341b1cf1ad9b19985cc2ed25ba781/hogql_parser_rs-1.3.88.tar.gz", hash = "sha256:d51d23450b11fee08e2ad35f7df71eae0731d02e8568dcd315e5f5f6a90caa8b", size = 289394, upload-time = "2026-06-16T15:16:23.447Z" }
+sdist = { url = "https://files.pythonhosted.org/packages/4e/de/e28a4ca99c2aefed6bbba57afa8e0b15bd9eab5be79696f9076f775c9bdd/hogql_parser_rs-1.3.90.tar.gz", hash = "sha256:2b9a74bc4c9a8c011c755de091f34e7cecd177377e3babee9b11690209ff44da", size = 289552, upload-time = "2026-06-16T20:27:34.178Z" }
wheels = [
- { url = "https://files.pythonhosted.org/packages/49/26/f170891fc0c66a725c1bb78a45a788ab09f08d9b94d9df1c1d5320a71518/hogql_parser_rs-1.3.88-cp312-abi3-macosx_10_12_x86_64.whl", hash = "sha256:a71a91174e841581b7579d868dc2f00ec0da64ad68823445b8f56140b9bba546", size = 728865, upload-time = "2026-06-16T15:16:14.172Z" },
- { url = "https://files.pythonhosted.org/packages/df/ae/a54a9a96b021b9a9fbea7b3a34f1c135c1e4d05071e14d160096e8a28039/hogql_parser_rs-1.3.88-cp312-abi3-macosx_11_0_arm64.whl", hash = "sha256:306df747402d902d096321529d1bc1043e25f021efbba5a568fd0eb72724759a", size = 686870, upload-time = "2026-06-16T15:16:15.779Z" },
- { url = "https://files.pythonhosted.org/packages/78/25/31cff3e3ffa17036a130b810d1f68ea304252f0cce3cf7291144e53fd65f/hogql_parser_rs-1.3.88-cp312-abi3-manylinux_2_28_aarch64.whl", hash = "sha256:ed90abd0818306fed28bf7666da26e4aca6702ab23f695736848ed3e9771ad9b", size = 2477042, upload-time = "2026-06-16T15:16:17.271Z" },
- { url = "https://files.pythonhosted.org/packages/9b/fc/67bdd23adf5b6cc777775a9bff96f91c9bdc44f7aa5f080bc100a7e3acd5/hogql_parser_rs-1.3.88-cp312-abi3-manylinux_2_28_x86_64.whl", hash = "sha256:eafdc5d68a8ba36a5789f39c0fb37dcdc871c92082321a20ee0533d9ee23d682", size = 2736240, upload-time = "2026-06-16T15:16:18.719Z" },
- { url = "https://files.pythonhosted.org/packages/9c/25/0acda0ac9c6cf5d46a816bb81469dcc0491ba3db2c5bfa2cc4d8fc86a2bb/hogql_parser_rs-1.3.88-cp312-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:d0cbb8ad5689b45ee2226cf95fcb7ab262072809c6375c3b37dcf5047c0061d3", size = 2648922, upload-time = "2026-06-16T15:16:20.318Z" },
- { url = "https://files.pythonhosted.org/packages/83/55/ee96470a9d239e35a043c0280fa9cc6dd733fd5f43ec1f0155f5702fd154/hogql_parser_rs-1.3.88-cp312-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:d80a92568058f31e5e459a84a71054e2b401e0cf4fbc21563bd2abc41e327e84", size = 2698769, upload-time = "2026-06-16T15:16:21.969Z" },
+ { url = "https://files.pythonhosted.org/packages/aa/fa/1f396004a667366e8b62da89a8702622f42b93f0122315c4a7b883378c4f/hogql_parser_rs-1.3.90-cp312-abi3-macosx_10_12_x86_64.whl", hash = "sha256:b16010069b8b9f09b88cd10b95f23c2e718d6f2be752812e6b046f287cc7643b", size = 729298, upload-time = "2026-06-16T20:27:24.548Z" },
+ { url = "https://files.pythonhosted.org/packages/0b/10/f4dab3f9d2a7fb85057119ba464fec5e63efa4146ccab5376b96c830291e/hogql_parser_rs-1.3.90-cp312-abi3-macosx_11_0_arm64.whl", hash = "sha256:fe9b4e3020249c9c061433d6a033359288c149d5f775564ab82d6111a9d78008", size = 687560, upload-time = "2026-06-16T20:27:25.936Z" },
+ { url = "https://files.pythonhosted.org/packages/2a/17/d3f414a0153bfd9a643e315cf20c0ae544cd4cbec14c48f1ac18f459dab8/hogql_parser_rs-1.3.90-cp312-abi3-manylinux_2_28_aarch64.whl", hash = "sha256:aee72e22e44d4daa3d087316776301c75089c9c2c226f5c03bfb6f46495126bd", size = 2478851, upload-time = "2026-06-16T20:27:27.31Z" },
+ { url = "https://files.pythonhosted.org/packages/0d/9d/87523980d18b0910d6406ff361de96ac6f49cf290911ca622da753d71aac/hogql_parser_rs-1.3.90-cp312-abi3-manylinux_2_28_x86_64.whl", hash = "sha256:f58065e375a4bcb44de0296522ccb1f2c943eb5ae441925f4f498c4c670661f1", size = 2736526, upload-time = "2026-06-16T20:27:28.831Z" },
+ { url = "https://files.pythonhosted.org/packages/cd/1e/4cc970229cac131ff80a063136c8e538784e14d151a71b17ab6a432d90e3/hogql_parser_rs-1.3.90-cp312-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:b0cd61840bcbe3b2e7ee03867fe5e14350bf759788051b8e5a7a204d57ac69c2", size = 2649416, upload-time = "2026-06-16T20:27:30.216Z" },
+ { url = "https://files.pythonhosted.org/packages/01/28/14eeec15820b309761dc7cd3851cd0370ed91d46b57db00ed96c93b750b3/hogql_parser_rs-1.3.90-cp312-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:2ea61245830b998a0319bdb41f5389e38a60bf1083fadfb01d4549f3d5dd62bb", size = 2700447, upload-time = "2026-06-16T20:27:31.714Z" },
]
[[package]]
@@ -5864,7 +5864,7 @@ requires-dist = [
{ name = "grpcio", specifier = "~=1.71.0" },
{ name = "gspread", specifier = "==6.2.1" },
{ name = "hogql-parser", specifier = "==1.3.69" },
- { name = "hogql-parser-rs", specifier = "==1.3.88" },
+ { name = "hogql-parser-rs", specifier = "==1.3.90" },
{ name = "humanize", specifier = "~=4.12.3" },
{ name = "infi-clickhouse-orm", git = "https://github.com/PostHog/infi.clickhouse_orm?rev=4e3996becf66bf5c26580aff12a80425d9d56fe5" },
{ name = "jsonref", specifier = "==1.1.0" },