: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
fix(hogql): parse hex, octal, and +inf number literals correctly (#58610)
Robbieae217f5ba149
diff --git a/common/hogql_parser/package.json b/common/hogql_parser/package.jsonindex e3e40c39df44..23843c9ea30c 100644--- a/common/hogql_parser/package.json+++ b/common/hogql_parser/package.json@@ -1,6 +1,6 @@{"name": "@posthog/hogql-parser",- "version": "1.3.40",+ "version": "1.3.42","description": "WebAssembly HogQL Parser - Parse HogQL queries in JavaScript/TypeScript","keywords": ["antlr4",diff --git a/common/hogql_parser/parser_json.cpp b/common/hogql_parser/parser_json.cppindex 7ff1464c7121..e3f031d384ff 100644--- a/common/hogql_parser/parser_json.cpp+++ b/common/hogql_parser/parser_json.cpp@@ -2819,6 +2819,20 @@ class HogQLParseTreeJSONConverter : public HogQLParserBaseVisitor {string text = ctx->getText();to_lower(text);+ // Grammar admits an optional leading sign on every numberLiteral, including INF/NAN.+ // Strip a leading '+' so downstream comparisons (and stoll's sign handling) work uniformly.+ if (!text.empty() && text[0] == '+') {+ text.erase(0, 1);+ }++ // Detect hex literals (possibly negative) so they skip the float branch below: hex+ // digits include 'e', so "0xfe" would otherwise route through stod and return a+ // double (e.g. 254.0). C++17 stod even accepts hex-float syntax, which hides the+ // bug at small magnitudes but rounds large values incorrectly near 2^53. Routing+ // hex through the integer branch keeps the result as exact int64_t.+ bool is_hex = (text.compare(0, 2, "0x") == 0) ||+ (text.size() > 2 && text[0] == '-' && text.compare(1, 2, "0x") == 0);+if (text.find("inf") != string::npos || text.find("nan") != string::npos) {// Handle special number cases (infinity and NaN)// Mark these with value_type="number" so the deserializer knows to convert them@@ -2830,7 +2844,7 @@ class HogQLParseTreeJSONConverter : public HogQLParserBaseVisitor {json["value"] = "NaN";}json["value_type"] = "number";- } else if (text.find(".") != string::npos || text.find("e") != string::npos) {+ } else if (!is_hex && (text.find(".") != string::npos || text.find("e") != string::npos)) {try {json["value"] = Json(stod(text)); // Float} catch (const std::out_of_range&) {@@ -2840,7 +2854,9 @@ class HogQLParseTreeJSONConverter : public HogQLParserBaseVisitor {return json;} else {try {- json["value"] = static_cast<int64_t>(stoll(text)); // Integer+ // Base 0: auto-detect radix from prefix (0x → hex, leading 0 → octal, else decimal),+ // so HEXADECIMAL_LITERAL / OCTAL_LITERAL tokens aren't silently truncated to 0.+ json["value"] = static_cast<int64_t>(stoll(text, nullptr, 0)); // Integer} catch (const std::out_of_range&) {try {json["value"] = Json(stod(text)); // Too large for int64, use floatdiff --git a/common/hogql_parser/setup.py b/common/hogql_parser/setup.pyindex ff9053970778..d32ded6a6951 100644--- a/common/hogql_parser/setup.py+++ b/common/hogql_parser/setup.py@@ -36,7 +36,7 @@ module = Extension(setup(name="hogql_parser",- version="1.3.40",+ version="1.3.42",url="https://github.com/PostHog/posthog/tree/master/common/hogql_parser",description="HogQL parser for internal PostHog use",author="PostHog Inc.",diff --git a/frontend/package.json b/frontend/package.jsonindex 6c881d6152b6..01e1746487b8 100644--- a/frontend/package.json+++ b/frontend/package.json@@ -66,7 +66,7 @@"@posthog/docs-onboarding": "workspace:*","@posthog/esbuilder": "workspace:*","@posthog/hedgehog-mode": "^0.0.48",- "@posthog/hogql-parser": "1.3.40",+ "@posthog/hogql-parser": "1.3.42","@posthog/hogvm": "workspace:*","@posthog/icons": "^0.36.6","@posthog/products-access-control": "workspace:*",diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex d114e20118aa..615e0273adc9 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -445,7 +445,7 @@ importers:version: 7.6.24([email protected])([email protected]([email protected]))([email protected])([email protected])'@storybook/react-webpack5':specifier: ^7.6.4- version: 7.6.4(@babel/[email protected])(@swc/[email protected])([email protected])([email protected])([email protected]([email protected]))([email protected])([email protected])([email protected])([email protected])([email protected])+ version: 7.6.4(@babel/[email protected])(@swc/[email protected])([email protected])([email protected]([email protected]))([email protected])([email protected])([email protected])([email protected])([email protected])'@storybook/test-runner':specifier: ^0.16.0version: 0.16.0(@types/[email protected])([email protected])@@ -499,7 +499,7 @@ importers:version: 2.0.0([email protected])webpack:specifier: ^5.88.2+ version: 5.88.2(@swc/[email protected])([email protected])webpack-cli:specifier: ^5.1.4version: 5.1.4([email protected])@@ -587,8 +587,8 @@ importers:specifier: ^0.0.48'@posthog/hogql-parser':- specifier: 1.3.40- version: 1.3.40+ specifier: 1.3.42+ version: 1.3.42'@posthog/hogvm':specifier: workspace:*version: link:../common/hogvm/typescript@@ -8915,8 +8915,8 @@ packages:react: 18.3.1react-dom: 18.3.1- '@posthog/[email protected]':- resolution: {integrity: sha512-OKEpuT1iqoL75sG/1Ims4RxoLQsrqVsK7GKO7BUZmtbl5J83qTFL8enQs4UJaJ2+aEcUB2zcJqMvqb1L1q+A2Q==}+ '@posthog/[email protected]':+ resolution: {integrity: sha512-i6UMWrf3q38VyL+qD8SI416Pp2v7Td0c/xricuS+8sBQ/djayqSMqKCoazfka2xLVoXJNorOg4u+QUkYBQviUw==}'@posthog/[email protected]':resolution: {integrity: sha512-xnusbGNz/5vSMUWdEViWuai7D/gxiMZSDDnP6s/acndMqzN+W4asPUgUNnTUps7OyAS1IKaO9zxhdikFrtooJA==}@@ -31881,7 +31881,7 @@ snapshots:react-refresh: 0.14.0schema-utils: 3.3.0source-map: 0.7.6+ webpack: 5.88.2(@swc/[email protected])([email protected])optionalDependencies:type-fest: 4.41.0webpack-hot-middleware: 2.25.4@@ -31927,7 +31927,7 @@ snapshots:transitivePeerDependencies:- prop-types- '@posthog/[email protected]': {}+ '@posthog/[email protected]': {}dependencies:@@ -33944,7 +33944,7 @@ snapshots:- encoding- supports-color- '@storybook/[email protected]([email protected])([email protected])([email protected])([email protected])':dependencies:'@babel/core': 7.29.0'@storybook/channels': 7.6.4@@ -33974,12 +33974,12 @@ snapshots:semver: 7.7.4style-loader: 3.3.3([email protected])swc-loader: 0.2.3(@swc/[email protected])([email protected])+ terser-webpack-plugin: 5.3.9(@swc/[email protected])([email protected])ts-dedent: 2.2.0url: 0.11.1util: 0.12.5util-deprecate: 1.0.2+ webpack: 5.88.2(@swc/[email protected])([email protected])webpack-dev-middleware: 6.1.1([email protected])webpack-hot-middleware: 2.25.4webpack-virtual-modules: 0.5.0@@ -34050,7 +34050,7 @@ snapshots:get-port: 5.1.1giget: 1.1.2globby: 11.1.0- jscodeshift: 0.15.1(@babel/[email protected](@babel/[email protected]))+ jscodeshift: 0.15.1(@babel/[email protected](@babel/[email protected]))leven: 3.1.0ora: 5.4.1prettier: 2.8.8@@ -34093,7 +34093,7 @@ snapshots:'@types/cross-spawn': 6.0.2cross-spawn: 7.0.6globby: 11.1.0- jscodeshift: 0.15.1(@babel/[email protected](@babel/[email protected]))+ jscodeshift: 0.15.1(@babel/[email protected](@babel/[email protected]))lodash: 4.18.1prettier: 2.8.8recast: 0.23.11@@ -34385,7 +34385,7 @@ snapshots:'@storybook/[email protected]': {}- '@storybook/[email protected](@babel/[email protected])(@swc/[email protected])([email protected])([email protected])([email protected]([email protected]))([email protected])([email protected])([email protected])([email protected])([email protected])':+ '@storybook/[email protected](@babel/[email protected])(@swc/[email protected])([email protected])([email protected]([email protected]))([email protected])([email protected])([email protected])([email protected])([email protected])':dependencies:'@babel/preset-flow': 7.23.3(@babel/[email protected])'@babel/preset-react': 7.23.3(@babel/[email protected])@@ -34405,7 +34405,7 @@ snapshots:react-dom: 18.3.1([email protected])react-refresh: 0.14.0semver: 7.7.4+ webpack: 5.88.2(@swc/[email protected])([email protected])optionalDependencies:'@babel/core': 7.26.0typescript: 5.9.3@@ -34488,7 +34488,7 @@ snapshots:react-docgen-typescript: 2.2.2([email protected])tslib: 2.8.1typescript: 5.9.3+ webpack: 5.88.2(@swc/[email protected])([email protected])transitivePeerDependencies:- supports-color@@ -34522,10 +34522,10 @@ snapshots:- typescript- vite-plugin-glimmerx- '@storybook/[email protected](@babel/[email protected])(@swc/[email protected])([email protected])([email protected])([email protected]([email protected]))([email protected])([email protected])([email protected])([email protected])([email protected])':+ '@storybook/[email protected](@babel/[email protected])(@swc/[email protected])([email protected])([email protected]([email protected]))([email protected])([email protected])([email protected])([email protected])([email protected])':dependencies:- '@storybook/builder-webpack5': 7.6.4([email protected])([email protected])([email protected])([email protected])- '@storybook/preset-react-webpack': 7.6.4(@babel/[email protected])(@swc/[email protected])([email protected])([email protected])([email protected]([email protected]))([email protected])([email protected])([email protected])([email protected])([email protected])+ '@storybook/preset-react-webpack': 7.6.4(@babel/[email protected])(@swc/[email protected])([email protected])([email protected]([email protected]))([email protected])([email protected])([email protected])([email protected])([email protected])'@storybook/react': 7.6.4([email protected])([email protected]([email protected]))([email protected])([email protected])'@types/node': 18.19.130react: 18.3.1@@ -34665,10 +34665,10 @@ snapshots:jest-circus: 29.7.0jest-environment-node: 29.7.0jest-junit: 16.0.0- jest-playwright-preset: 4.0.0([email protected])([email protected])([email protected])([email protected](@types/[email protected])([email protected](@swc/[email protected])(@types/[email protected])([email protected])))+ jest-playwright-preset: 4.0.0([email protected])([email protected])([email protected])([email protected])jest-runner: 29.7.0jest-serializer-html: 7.1.0- jest-watch-typeahead: 2.2.2([email protected](@types/[email protected])([email protected](@swc/[email protected])(@types/[email protected])([email protected])))+ jest-watch-typeahead: 2.2.2([email protected])node-fetch: 2.7.0([email protected])playwright: 1.45.0read-pkg-up: 7.0.1@@ -36932,17 +36932,17 @@ snapshots:dependencies:+ webpack: 5.88.2(@swc/[email protected])([email protected])webpack-cli: 5.1.4([email protected])dependencies:+ webpack: 5.88.2(@swc/[email protected])([email protected])webpack-cli: 5.1.4([email protected])dependencies:+ webpack: 5.88.2(@swc/[email protected])([email protected])webpack-cli: 5.1.4([email protected])'@xmldom/[email protected]': {}@@ -37549,14 +37549,14 @@ snapshots:loader-utils: 2.0.4make-dir: 3.1.0schema-utils: 2.7.1+ webpack: 5.88.2(@swc/[email protected])([email protected])dependencies:'@babel/core': 7.29.0find-cache-dir: 4.0.0schema-utils: 4.2.0+ webpack: 5.88.2(@swc/[email protected])([email protected])@@ -38738,7 +38738,7 @@ snapshots:postcss-value-parser: 4.2.0schema-utils: 2.7.1semver: 6.3.1+ webpack: 5.88.2(@swc/[email protected])([email protected])dependencies:@@ -38750,7 +38750,7 @@ snapshots:postcss-modules-values: 4.0.0([email protected])postcss-value-parser: 4.2.0semver: 7.7.4+ webpack: 5.88.2(@swc/[email protected])([email protected])dependencies:@@ -40487,7 +40487,7 @@ snapshots:dependencies:loader-utils: 2.0.4schema-utils: 3.3.0+ webpack: 5.88.2(@swc/[email protected])([email protected])dependencies:@@ -40631,7 +40631,7 @@ snapshots:semver: 7.7.4tapable: 2.3.0typescript: 5.9.3+ webpack: 5.88.2(@swc/[email protected])([email protected])dependencies:@@ -41310,7 +41310,7 @@ snapshots:dependencies:html-webpack-plugin: 5.5.3([email protected])+ webpack: 5.88.2(@swc/[email protected])([email protected])dependencies:@@ -41319,7 +41319,7 @@ snapshots:lodash: 4.17.21pretty-error: 4.0.0tapable: 2.2.1+ webpack: 5.88.2(@swc/[email protected])([email protected])[email protected]([email protected]([email protected]))([email protected])([email protected])([email protected])([email protected])([email protected]):dependencies:@@ -42569,7 +42569,7 @@ snapshots:'@types/node': 22.18.8jest-util: 30.0.5- [email protected]([email protected])([email protected])([email protected])([email protected](@types/[email protected])([email protected](@swc/[email protected])(@types/[email protected])([email protected]))):dependencies:expect-playwright: 0.8.0jest: 29.7.0(@types/[email protected])([email protected](@swc/[email protected])(@types/[email protected])([email protected]))@@ -42992,7 +42992,7 @@ snapshots:leven: 3.1.0pretty-format: 30.0.5- [email protected]([email protected](@types/[email protected])([email protected](@swc/[email protected])(@types/[email protected])([email protected]))):dependencies:ansi-escapes: 6.0.0chalk: 5.6.2@@ -43138,7 +43138,7 @@ snapshots:dependencies:'@babel/core': 7.29.0'@babel/parser': 7.29.2@@ -43161,7 +43161,7 @@ snapshots:temp: 0.8.4write-file-atomic: 2.4.3optionalDependencies:- '@babel/preset-env': 7.23.5(@babel/[email protected])+ '@babel/preset-env': 7.23.5(@babel/[email protected])transitivePeerDependencies:- supports-color@@ -43435,7 +43435,7 @@ snapshots:less: 4.2.2loader-utils: 2.0.4schema-utils: 3.3.0+ webpack: 5.88.2(@swc/[email protected])([email protected])dependencies:@@ -45931,7 +45931,7 @@ snapshots:postcss: 8.5.6schema-utils: 3.3.0semver: 7.7.0+ webpack: 5.88.2(@swc/[email protected])([email protected])dependencies:@@ -46917,7 +46917,7 @@ snapshots:dependencies:loader-utils: 2.0.4schema-utils: 3.3.0+ webpack: 5.88.2(@swc/[email protected])([email protected])dependencies:@@ -48054,7 +48054,7 @@ snapshots:neo-async: 2.6.2schema-utils: 3.3.0semver: 7.7.0+ webpack: 5.88.2(@swc/[email protected])([email protected])optionalDependencies:sass: 1.56.0@@ -48762,11 +48762,11 @@ snapshots:dependencies:loader-utils: 2.0.4schema-utils: 3.3.0+ webpack: 5.88.2(@swc/[email protected])([email protected])dependencies:+ webpack: 5.88.2(@swc/[email protected])([email protected])@@ -48970,7 +48970,7 @@ snapshots:dependencies:'@swc/core': 1.15.18+ webpack: 5.88.2(@swc/[email protected])([email protected])@@ -49164,17 +49164,16 @@ snapshots:'@swc/core': 1.15.18esbuild: 0.27.7dependencies:'@jridgewell/trace-mapping': 0.3.29jest-worker: 27.5.1schema-utils: 3.3.0serialize-javascript: 6.0.1terser: 5.19.1+ webpack: 5.88.2(@swc/[email protected])([email protected])optionalDependencies:'@swc/core': 1.15.18- esbuild: 0.18.20dependencies:@@ -50349,7 +50348,7 @@ snapshots:import-local: 3.1.0interpret: 3.1.1rechoir: 0.8.0+ webpack: 5.88.2(@swc/[email protected])([email protected])webpack-merge: 5.8.0@@ -50360,7 +50359,7 @@ snapshots:range-parser: 1.2.1schema-utils: 4.2.0optionalDependencies:+ webpack: 5.88.2(@swc/[email protected])([email protected])dependencies:@@ -50411,7 +50410,7 @@ snapshots:- esbuild- uglify-jsdependencies:'@types/eslint-scope': 3.7.4'@types/estree': 1.0.1@@ -50434,7 +50433,7 @@ snapshots:neo-async: 2.6.2schema-utils: 3.3.0tapable: 2.2.1+ terser-webpack-plugin: 5.3.9(@swc/[email protected])([email protected])watchpack: 2.4.0webpack-sources: 3.2.3optionalDependencies:diff --git a/posthog/hogql/parser.py b/posthog/hogql/parser.pyindex ace1369f966e..2ffbd2ad72c6 100644--- a/posthog/hogql/parser.py+++ b/posthog/hogql/parser.py@@ -1826,9 +1826,25 @@ class HogQLParseTreeConverter(ParseTreeVisitor):def visitNumberLiteral(self, ctx: HogQLParser.NumberLiteralContext):text = ctx.getText().lower()- if "." in text or "e" in text or text == "-inf" or text == "inf" or text == "nan":+ # Grammar allows an optional leading sign on every numberLiteral, including INF/NAN.+ # Strip a leading '+' so the sign-stripping below is uniform and "+inf" → "inf".+ if text.startswith("+"):+ text = text[1:]+ sign = 1+ abs_text = text+ if abs_text.startswith("-"):+ sign = -1+ abs_text = abs_text[1:]+ # Hex must be dispatched BEFORE the float guard: hex digits include 'e', so+ # "0xfe" would otherwise route through float() and raise ValueError.+ if abs_text.startswith("0x"):+ return ast.Constant(value=sign * int(abs_text, 16))+ if "." in abs_text or "e" in abs_text or abs_text == "inf" or abs_text == "nan":return ast.Constant(value=float(text))- return ast.Constant(value=int(text))+ # Octal literals (leading '0' followed by more digits) must use base 8.+ if len(abs_text) > 1 and abs_text[0] == "0":+ return ast.Constant(value=sign * int(abs_text, 8))+ return ast.Constant(value=sign * int(abs_text))def visitLiteral(self, ctx: HogQLParser.LiteralContext):if ctx.NULL_SQL():diff --git a/posthog/hogql/test/_test_parser.py b/posthog/hogql/test/_test_parser.pyindex 45f28e1a77bc..a8c2209d24b5 100644--- a/posthog/hogql/test/_test_parser.py+++ b/posthog/hogql/test/_test_parser.py@@ -79,6 +79,39 @@ def parser_test_factory(backend: HogQLParserBackend):self.assertEqual(self._expr("1e-18"), ast.Constant(value=1e-18))self.assertEqual(self._expr("2.34e+20"), ast.Constant(value=2.34e20))+ @parameterized.expand(+ [+ # Hex: HEXADECIMAL_LITERAL tokens were being parsed via base-10 stoll/int,+ # which stops at the 'x' and silently yielded 0.+ ("hex_positive", "0x1F", 31),+ ("hex_zero", "0x0", 0),+ ("hex_ff", "0xff", 255),+ ("hex_negative", "-0x1F", -31),+ ("hex_positive_sign", "+0x1F", 31),+ # Hex digits include 'e' — must be dispatched before the float guard,+ # or "0xfe" routes through float()/stod and either raises (Python)+ # or silently returns a double (C++) instead of an int64.+ ("hex_with_e_digit", "0xfe", 254),+ ("hex_negative_with_e_digit", "-0xae", -174),+ # Catches the C++ structural bug specifically: stod handles hex floats,+ # so "0xfe" → 254.0 compares equal to 254 by coincidence. Near 2^60 the+ # double mantissa is 8 bits short, so this value rounds to 0x1000000000000000+ # as a double — different from the exact int64 by 14, and Python int/float+ # equality is exact (no implicit conversion) so the test catches it.+ ("hex_breaks_double_precision", "0x100000000000000e", 0x100000000000000E),+ # Octal: OCTAL_LITERAL tokens were being parsed as base-10 integers,+ # e.g. "017" → 17 instead of 15.+ ("octal_positive", "017", 15),+ ("octal_negative", "-017", -15),+ ("octal_positive_sign", "+017", 15),+ # +inf: grammar admits `(PLUS | DASH)? INF`, but visitor only matched+ # "inf" and "-inf", so "+inf" fell through to NaN.+ ("positive_inf", "+inf", float("inf")),+ ]+ )+ def test_signed_radix_number_literals(self, _name: str, expr: str, expected: int | float):+ self.assertEqual(self._expr(expr), ast.Constant(value=expected))+def test_booleans(self):self.assertEqual(self._expr("true"), ast.Constant(value=True))self.assertEqual(self._expr("TRUE"), ast.Constant(value=True))