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): parse hex, octal, and +inf number literals correctly (#58610)

Robbieae217f5ba149
diff --git a/common/hogql_parser/package.json b/common/hogql_parser/package.json
index 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.cpp
index 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 float
diff --git a/common/hogql_parser/setup.py b/common/hogql_parser/setup.py
index 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.json
index 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.yaml
index d114e20118aa..615e0273adc9 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -445,7 +445,7 @@ importers:
'@storybook/react-webpack5':
specifier: ^7.6.4
'@storybook/test-runner':
specifier: ^0.16.0
version: 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.4
version: 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.1
react-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.0
schema-utils: 3.3.0
source-map: 0.7.6
+ webpack: 5.88.2(@swc/[email protected])([email protected])
optionalDependencies:
type-fest: 4.41.0
webpack-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
dependencies:
'@babel/core': 7.29.0
'@storybook/channels': 7.6.4
@@ -33974,12 +33974,12 @@ snapshots:
semver: 7.7.4
style-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])([email protected])
+ terser-webpack-plugin: 5.3.9(@swc/[email protected])([email protected])
ts-dedent: 2.2.0
url: 0.11.1
util: 0.12.5
util-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.4
webpack-virtual-modules: 0.5.0
@@ -34050,7 +34050,7 @@ snapshots:
get-port: 5.1.1
giget: 1.1.2
globby: 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.0
ora: 5.4.1
prettier: 2.8.8
@@ -34093,7 +34093,7 @@ snapshots:
'@types/cross-spawn': 6.0.2
cross-spawn: 7.0.6
globby: 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.1
prettier: 2.8.8
recast: 0.23.11
@@ -34385,7 +34385,7 @@ snapshots:
'@storybook/[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.0
semver: 7.7.4
+ webpack: 5.88.2(@swc/[email protected])([email protected])
optionalDependencies:
'@babel/core': 7.26.0
typescript: 5.9.3
@@ -34488,7 +34488,7 @@ snapshots:
react-docgen-typescript: 2.2.2([email protected])
tslib: 2.8.1
typescript: 5.9.3
+ webpack: 5.88.2(@swc/[email protected])([email protected])
transitivePeerDependencies:
- supports-color
@@ -34522,10 +34522,10 @@ snapshots:
- typescript
- vite-plugin-glimmerx
dependencies:
+ '@storybook/builder-webpack5': 7.6.4([email protected])([email protected])([email protected])
'@types/node': 18.19.130
react: 18.3.1
@@ -34665,10 +34665,10 @@ snapshots:
jest-circus: 29.7.0
jest-environment-node: 29.7.0
jest-junit: 16.0.0
jest-runner: 29.7.0
jest-serializer-html: 7.1.0
+ jest-watch-typeahead: 2.2.2([email protected])
node-fetch: 2.7.0([email protected])
playwright: 1.45.0
read-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.4
make-dir: 3.1.0
schema-utils: 2.7.1
+ webpack: 5.88.2(@swc/[email protected])([email protected])
dependencies:
'@babel/core': 7.29.0
find-cache-dir: 4.0.0
schema-utils: 4.2.0
+ webpack: 5.88.2(@swc/[email protected])([email protected])
@@ -38738,7 +38738,7 @@ snapshots:
postcss-value-parser: 4.2.0
schema-utils: 2.7.1
semver: 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.0
semver: 7.7.4
+ webpack: 5.88.2(@swc/[email protected])([email protected])
dependencies:
@@ -40487,7 +40487,7 @@ snapshots:
dependencies:
loader-utils: 2.0.4
schema-utils: 3.3.0
+ webpack: 5.88.2(@swc/[email protected])([email protected])
dependencies:
@@ -40631,7 +40631,7 @@ snapshots:
semver: 7.7.4
tapable: 2.3.0
typescript: 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.21
pretty-error: 4.0.0
tapable: 2.2.1
+ webpack: 5.88.2(@swc/[email protected])([email protected])
dependencies:
@@ -42569,7 +42569,7 @@ snapshots:
'@types/node': 22.18.8
jest-util: 30.0.5
dependencies:
expect-playwright: 0.8.0
@@ -42992,7 +42992,7 @@ snapshots:
leven: 3.1.0
pretty-format: 30.0.5
dependencies:
ansi-escapes: 6.0.0
chalk: 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.4
write-file-atomic: 2.4.3
optionalDependencies:
- '@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.2
loader-utils: 2.0.4
schema-utils: 3.3.0
+ webpack: 5.88.2(@swc/[email protected])([email protected])
dependencies:
@@ -45931,7 +45931,7 @@ snapshots:
postcss: 8.5.6
schema-utils: 3.3.0
semver: 7.7.0
+ webpack: 5.88.2(@swc/[email protected])([email protected])
dependencies:
@@ -46917,7 +46917,7 @@ snapshots:
dependencies:
loader-utils: 2.0.4
schema-utils: 3.3.0
+ webpack: 5.88.2(@swc/[email protected])([email protected])
dependencies:
@@ -48054,7 +48054,7 @@ snapshots:
neo-async: 2.6.2
schema-utils: 3.3.0
semver: 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.4
schema-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.18
esbuild: 0.27.7
dependencies:
'@jridgewell/trace-mapping': 0.3.29
jest-worker: 27.5.1
schema-utils: 3.3.0
serialize-javascript: 6.0.1
terser: 5.19.1
+ webpack: 5.88.2(@swc/[email protected])([email protected])
optionalDependencies:
'@swc/core': 1.15.18
- esbuild: 0.18.20
dependencies:
@@ -50349,7 +50348,7 @@ snapshots:
import-local: 3.1.0
interpret: 3.1.1
rechoir: 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.1
schema-utils: 4.2.0
optionalDependencies:
+ webpack: 5.88.2(@swc/[email protected])([email protected])
dependencies:
@@ -50411,7 +50410,7 @@ snapshots:
- esbuild
- uglify-js
dependencies:
'@types/eslint-scope': 3.7.4
'@types/estree': 1.0.1
@@ -50434,7 +50433,7 @@ snapshots:
neo-async: 2.6.2
schema-utils: 3.3.0
tapable: 2.2.1
- terser-webpack-plugin: 5.3.9(@swc/[email protected])([email protected])([email protected])
+ terser-webpack-plugin: 5.3.9(@swc/[email protected])([email protected])
watchpack: 2.4.0
webpack-sources: 3.2.3
optionalDependencies:
diff --git a/posthog/hogql/parser.py b/posthog/hogql/parser.py
index 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.py
index 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))