Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(hogql): allow timezone overrides #21004

Merged
merged 2 commits into from
Mar 27, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
add override for functions with precision before tz
thmsobrmlr committed Mar 19, 2024

Verified

This commit was signed with the committer’s verified signature.
thmsobrmlr Thomas Obermüller
commit 78f3a4195834aec7ccdd66bdb5deb961d0c6f6cc
16 changes: 12 additions & 4 deletions posthog/hogql/printer.py
Original file line number Diff line number Diff line change
@@ -823,12 +823,20 @@ def visit_call(self, node: ast.Call):

if func_meta.tz_aware:
has_tz_override = len(node.args) == func_meta.max_args
if (relevant_clickhouse_name == "now64" and len(node.args) == 0) or (
relevant_clickhouse_name == "parseDateTime64BestEffortOrNull" and len(node.args) == 1
):
args.append("6") # These two CH functions require the precision argument before timezone

if not has_tz_override:
args.append(self.visit(ast.Constant(value=self._get_timezone())))

if (
relevant_clickhouse_name == "now64"
and (len(node.args) == 0 or (has_tz_override and len(node.args) == 1))
) or (
relevant_clickhouse_name == "parseDateTime64BestEffortOrNull"
and (len(node.args) == 1 or (has_tz_override and len(node.args) == 2))
):
# These two CH functions require a precision argument before timezone
args = args[:-1] + ["6"] + args[-1:]

if node.name == "toStartOfWeek" and len(node.args) == 1:
# If week mode hasn't been specified, use the project's default.
# For Monday-based weeks mode 3 is used (which is ISO 8601), for Sunday-based mode 0 (CH default)
8 changes: 6 additions & 2 deletions posthog/hogql/test/test_printer.py
Original file line number Diff line number Diff line change
@@ -1538,12 +1538,14 @@ def test_override_timezone(self):
"""
SELECT
toDateTime(timestamp),
toDateTime(timestamp, 'US/Pacific')
toDateTime(timestamp, 'US/Pacific'),
now(),
now('US/Pacific')
FROM events
""",
context,
),
f"SELECT toDateTime(toTimeZone(events.timestamp, %(hogql_val_0)s), %(hogql_val_1)s), toDateTime(toTimeZone(events.timestamp, %(hogql_val_2)s), %(hogql_val_3)s) FROM events WHERE equals(events.team_id, {self.team.pk}) LIMIT 10000",
f"SELECT toDateTime(toTimeZone(events.timestamp, %(hogql_val_0)s), %(hogql_val_1)s), toDateTime(toTimeZone(events.timestamp, %(hogql_val_2)s), %(hogql_val_3)s), now64(6, %(hogql_val_4)s), now64(6, %(hogql_val_5)s) FROM events WHERE equals(events.team_id, {self.team.pk}) LIMIT 10000",
)
self.assertEqual(
context.values,
@@ -1552,5 +1554,7 @@ def test_override_timezone(self):
"hogql_val_1": "UTC",
"hogql_val_2": "UTC",
"hogql_val_3": "US/Pacific",
"hogql_val_4": "UTC",
"hogql_val_5": "US/Pacific",
},
)