fix(insights): keep toDateTime null-tolerant for non-date constants
Problem
A trends query that breaks down by (or filters on) a property typed as DateTime could crash with a hard ClickHouse exception — DB::Exception: Cannot read DateTime: unexpected word — instead of returning results, breaking the insight for any team that hit the path.
The actual failing query compared a DateTime-typed property against a non-date value, which the HogQL printer rendered as parseDateTime64BestEffort('mStart', 6, 'UTC'). That is the strict best-effort parser (no OrNull), so a value that isn't a date throws at runtime and fails the whole query rather than degrading to NULL.
Why: The root cause is in shared HogQL machinery, so any team with a DateTime-typed property used in a breakdown or filter against non-date values is exposed.
Changes
The ClickHouse printer was promoting toDateTime(<string constant>) from the null-tolerant parseDateTime64BestEffortOrNull to the strict parseDateTime64BestEffort for any string constant, then only upgrading to the index-friendly toDateTime/toDateTime64 when the value matched an exact date pattern. A constant that wasn't a recognized date (an unresolved query variable, the trends breakdown null sentinel $$_posthog_breakdown_null_$$, free text) was left stuck on the strict parser, which throws.
Now we only fast-path to the strict index-friendly functions when the constant matches an exact date format. Everything else keeps parseDateTime64BestEffortOrNull, so a bad value parses to NULL and the row drops out instead of failing the query. For valid date strings the result is unchanged — parseDateTime64BestEffortOrNull is the same best-effort parser, it just returns NULL rather than raising on unparseable input.
Note: the root cause was the printer's constant-folding optimization, not posthog/hogql/transforms/property_types.py (the property cast on the left-hand side was already correctly null-tolerant in the failing query).
How did you test this code?
I'm an agent. I did not run the suite manually (no database available in this environment). Automated tests:
- Added parameterized printer tests in
test_printer.py: non-date string constants (mStart, the breakdown null sentinel, free text) now printparseDateTime64BestEffortOrNulland never the strictparseDateTime64BestEffort; recognized exact date formats still fast-path totoDateTime/toDateTime64. This locks in the exact regression — if the unconditional strict promotion is restored, these fail. - Updated existing printer assertions and three SQL snapshots (
test_query.ambr,test_session_profile_query.ambr,test_error_tracking_query_runner.ambr) where non-exact-format date constants now render with the null-tolerant parser. The only change is the function name; arguments are identical.
Automatic notifications
- [ ] Publish to changelog?
- [ ] Alert Sales and Marketing teams?
🤖 Agent context
Autonomy: Fully autonomous
Investigated from an error-tracking issue surfaced via the PostHog inbox. Pulled the raw exception payload over MCP to recover the full failing SQL and stack trace, which pointed at parseDateTime64BestEffort in strict mode (ConvertFromStringExceptionMode=0) — not the location the original report hypothesized (property_types.py). Confirmed the strict parser name is only ever emitted by the printer's constant-folding optimization, so the fix and the snapshot updates are a clean, behavior-preserving change for valid dates.
Skills invoked: /writing-tests (to gate the regression test down to the printer level rather than a ClickHouse-backed trends test).
Created with PostHog Code from an inbox report.