Skip to content

Commit

Permalink
Avoid crashing on empty or non-existent timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaeelaudibert committed Nov 26, 2024
1 parent 3a98085 commit 4a43be8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion posthog/hogql_queries/web_analytics/stats_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ def _counts_breakdown_value(self):
# Get the difference between the UNIX timestamp at UTC and the UNIX timestamp at the event's timezone
# Value is in milliseconds, turn it to hours, works even for fractional timezone offsets (I'm looking at you, Australia)
return parse_expr(
"if(isNull(properties.$timezone), NULL, (toUnixTimestamp64Milli(parseDateTimeBestEffort(assumeNotNull(toString(timestamp, properties.$timezone)))) - toUnixTimestamp64Milli(timestamp)) / 3600000)"
"if(or(isNull(properties.$timezone), empty(properties.$timezone)), NULL, (toUnixTimestamp64Milli(parseDateTimeBestEffort(assumeNotNull(toString(timestamp, properties.$timezone)))) - toUnixTimestamp64Milli(timestamp)) / 3600000)"
)
case _:
raise NotImplementedError("Breakdown not implemented")
Expand Down
46 changes: 46 additions & 0 deletions posthog/hogql_queries/web_analytics/test/test_web_stats_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1099,3 +1099,49 @@ def test_timezone_filter_with_invalid_timezone(self):
None,
breakdown_by=WebStatsBreakdown.TIMEZONE,
)

def test_timezone_filter_with_empty_timezone(self):
did = "id"
sid = str(uuid7("2019-02-17"))

_create_person(
team_id=self.team.pk,
distinct_ids=[did],
properties={"name": sid, "email": f"[email protected]"},
)

# Key not exists
_create_event(
team=self.team,
event="$pageview",
distinct_id=did,
timestamp=f"2019-02-17 00:00:00",
properties={"$session_id": sid, "$pathname": f"/path1"},
)

# Key exists, it's null
_create_event(
team=self.team,
event="$pageview",
distinct_id=did,
timestamp=f"2019-02-17 00:00:00",
properties={"$session_id": sid, "$pathname": f"/path1", "$timezone": None},
)

# Key exists, it's empty string
_create_event(
team=self.team,
event="$pageview",
distinct_id=did,
timestamp=f"2019-02-17 00:00:00",
properties={"$session_id": sid, "$pathname": f"/path1", "$timezone": ""},
)

results = self._run_web_stats_table_query(
"all",
None,
breakdown_by=WebStatsBreakdown.TIMEZONE,
).results

# Don't crash, treat all of them null
assert results == []

0 comments on commit 4a43be8

Please sign in to comment.