: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(hogvm): return null from toDate and toDateTime given an unparseable value (#106830)
Meikel Ratzfda8ed1f30cd
diff --git a/common/hogvm/typescript/src/__tests__/date.test.ts b/common/hogvm/typescript/src/__tests__/date.test.tsindex 587f763b5105..85b73bc8968f 100644--- a/common/hogvm/typescript/src/__tests__/date.test.ts+++ b/common/hogvm/typescript/src/__tests__/date.test.ts@@ -54,7 +54,7 @@ const REJECTED = [describe('date-like string grammar', () => {test.each(ACCEPTED)('accepts %s as %d', (input, expected) => {expect(dateStringToSeconds(input)).toBeCloseTo(expected, 3)- expect(toDateTime(input).dt).toBeCloseTo(expected, 3)+ expect(toDateTime(input)!.dt).toBeCloseTo(expected, 3)expect(toUnixTimestamp(input)).toBeCloseTo(expected, 3)})@@ -65,7 +65,7 @@ describe('date-like string grammar', () => {test('a naive string resolves to UTC regardless of the host timezone', () => {// Python's `datetime.timestamp()` resolved naive input in the host zone, so the same filter// gave different answers on a developer's laptop and in production. Pinned in all three VMs.- expect(toDateTime('2024-01-01').dt).toBe(1704067200)+ expect(toDateTime('2024-01-01')!.dt).toBe(1704067200)expect(toDate('2024-01-01')).toEqual({__hogDate__: true,year: 2024,@@ -74,19 +74,26 @@ describe('date-like string grammar', () => {})})+ test('an unparseable value is null, not a date made of NaN', () => {+ // A date of NaN fields throws out of the date library the moment anything compares it.+ for (const input of ['', 'nonsense', '2024-13-45']) {+ expect(toDate(input)).toBeNull()+ expect(toDateTime(input)).toBeNull()+ }+ })+test('an explicit zone applies only to input carrying no zone of its own', () => {- expect(toDateTime('2024-01-01 00:00:00', 'America/New_York').dt).toBe(1704085200)- expect(toDateTime('2024-01-01T00:00:00Z', 'America/New_York').dt).toBe(1704067200)+ expect(toDateTime('2024-01-01 00:00:00', 'America/New_York')!.dt).toBe(1704085200)+ expect(toDateTime('2024-01-01T00:00:00Z', 'America/New_York')!.dt).toBe(1704067200)})test('a number passes through as epoch seconds without parsing', () => {- expect(toDateTime(1700000000).dt).toBe(1700000000)+ expect(toDateTime(1700000000)!.dt).toBe(1700000000)})- test('unparseable input keeps the pre-existing NaN failure mode', () => {- // Not a good failure mode, but each VM's is different (Python raises, Rust errors into a- // null) and converging them is a separate change from converging what parses.- expect(toDateTime('not-a-date').dt).toBeNaN()+ test('unparseable input is null, which is what the Rust VM already returns', () => {+ // The Python VM still raises on this input.+ expect(toDateTime('not-a-date')).toBeNull()})test.each([[1700000000], [null], [{ a: 1 }], [['x']]])(@@ -100,8 +107,8 @@ describe('date-like string grammar', () => {})- test.each([[null], [{ a: 1 }], [['x']]])('toDateTime(%p) yields NaN rather than throwing', (input) => {- expect(toDateTime(input as any).dt).toBeNaN()+ test.each([[null], [{ a: 1 }], [['x']]])('toDateTime(%p) is null rather than throwing', (input) => {+ expect(toDateTime(input as any)).toBeNull()})test('an ambiguous local time takes the first of the DST fold', () => {diff --git a/common/hogvm/typescript/src/__tests__/execute.test.ts b/common/hogvm/typescript/src/__tests__/execute.test.tsindex 6ee9a251fd4a..2e3d3a7dc697 100644--- a/common/hogvm/typescript/src/__tests__/execute.test.ts+++ b/common/hogvm/typescript/src/__tests__/execute.test.ts@@ -313,6 +313,29 @@ describe('hogvm execute', () => {expect(execSync(['_h', op.NULL, op.INTEGER, 0, op.NOT_EQ], options)).toBe(true)})+ test('comparing today against an unparseable date is no match, not an error', () => {+ // A null guard written before the comparison does not save it: AND evaluates every operand+ // before combining them, so the comparison runs for a person without the property.+ const compare = (value: string, operation: number) => [+ '_h',+ op.STRING,+ value,+ op.CALL_GLOBAL,+ 'toDate',+ 1,+ op.CALL_GLOBAL,+ 'today',+ 0,+ operation,+ ]+ for (const operation of [op.LT_EQ, op.GT_EQ]) {+ expect(execSync(compare('', operation), {})).toBe(false)+ expect(execSync(compare('nonsense', operation), {})).toBe(false)+ }+ // The shape people actually write, and the one the guard does not save.+ expect(execSync(['_h', op.FALSE, ...compare('', op.GT_EQ).slice(1), op.AND, 2], {})).toBe(false)+ })+test('async limits', async () => {const callSleep = [33,diff --git a/common/hogvm/typescript/src/stl/date.ts b/common/hogvm/typescript/src/stl/date.tsindex 013701eff11f..f25859522bd6 100644--- a/common/hogvm/typescript/src/stl/date.ts+++ b/common/hogvm/typescript/src/stl/date.ts@@ -122,15 +122,21 @@ export function parseDateLike(input: string, zone?: string): DateTime | null {return dt.isValid ? dt : null}-// NOTE: unparseable input keeps each VM's *existing* failure mode — here an invalid luxon DateTime-// (so `.year`/`.toSeconds()` are NaN, as before), while Python raises and Rust errors into a null.-// Converging those three is a separate change; this only converges *what parses*.+// NOTE: `toUnixTimestamp` keeps each VM's own failure mode for unparseable input. Here it is an+// invalid luxon DateTime, so `.toSeconds()` is NaN. Python raises, and Rust returns null.+// `toDate` and `toDateTime` do not use this value. They return null for unparseable input, as Rust does.const INVALID = DateTime.invalid('not a date-like string')-export function toDate(input: string | number): HogDate {+export function toDate(input: string | number): HogDate | null {// Previously `DateTime.fromISO(input)` with no zone, i.e. the *system* zone — same class of// host-dependent bug as Python's, and a day off from `toDateTime` for offsets west of UTC.- const dt = typeof input === 'number' ? DateTime.fromSeconds(input) : (parseDateLike(input) ?? INVALID)+ //+ // Null rather than a date of NaN fields: comparing a NaN date builds a DateTime from NaN, which+ // throws out of the date library instead of reading as no match.+ const dt = typeof input === 'number' ? DateTime.fromSeconds(input) : parseDateLike(input)+ if (!dt?.isValid) {+ return null+ }return {__hogDate__: true,year: dt.year,@@ -139,11 +145,17 @@ export function toDate(input: string | number): HogDate {}}-export function toDateTime(input: string | number, zone?: string): HogDateTime {- const dt = typeof input === 'number' ? input : (parseDateLike(input, zone) ?? INVALID).toSeconds()+export function toDateTime(input: string | number, zone?: string): HogDateTime | null {+ if (typeof input === 'number') {+ return { __hogDateTime__: true, dt: input, zone: zone || 'UTC' }+ }+ const dt = parseDateLike(input, zone)+ if (!dt?.isValid) {+ return null+ }return {__hogDateTime__: true,- dt: dt,+ dt: dt.toSeconds(),zone: zone || 'UTC',}}