: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(marketing-analytics): resolve date_from='all' in warehouse trends path (#62451)
Javier Bahamondes8536b02ccdb5
diff --git a/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/logic/marketingAnalyticsTilesLogic.ts b/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/logic/marketingAnalyticsTilesLogic.tsindex eb39e4aa8e87..c61ea774558d 100644--- a/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/logic/marketingAnalyticsTilesLogic.ts+++ b/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/logic/marketingAnalyticsTilesLogic.ts@@ -163,6 +163,7 @@ export const marketingAnalyticsTilesLogic = kea<marketingAnalyticsTilesLogicTypeaggregationAxisPostfix:isCurrency && !currencyIsPrefix ? ` ${currencySymbol}` : undefined,},+ tags: MARKETING_ANALYTICS_DEFAULT_QUERY_TAGS,},},showIntervalSelect: true,diff --git a/posthog/hogql_queries/utils/test/test_timestamp_utils.py b/posthog/hogql_queries/utils/test/test_timestamp_utils.pyindex 4f660cfcd821..dac59d930c95 100644--- a/posthog/hogql_queries/utils/test/test_timestamp_utils.py+++ b/posthog/hogql_queries/utils/test/test_timestamp_utils.py@@ -1,16 +1,25 @@import datetime+from zoneinfo import ZoneInfofrom posthog.test.base import APIBaseTest, ClickhouseDestroyTablesMixin, _create_event, flush_persons_and_events+from unittest.mock import patchfrom django.core.cache import cachefrom django.test import override_settingsfrom dateutil import parser+from parameterized import parameterized-from posthog.schema import ActionsNode, DateRange, EventsNode, IntervalType+from posthog.schema import ActionsNode, DataWarehouseNode, DateRange, EventsNode, IntervalType+from posthog.clickhouse.query_tagging import Feature, Product, get_query_tags, tags_contextfrom posthog.hogql_queries.utils.query_date_range import QueryDateRange-from posthog.hogql_queries.utils.timestamp_utils import format_label_date, get_earliest_timestamp_from_series+from posthog.hogql_queries.utils.timestamp_utils import (+ EARLIEST_EVENT_TIMESTAMP,+ _coerce_to_datetime,+ format_label_date,+ get_earliest_timestamp_from_series,+)from posthog.models.team import WeekStartDayfrom products.actions.backend.models.action import Action@@ -211,7 +220,7 @@ class TestTimestampUtils(APIBaseTest, ClickhouseDestroyTablesMixin):series = [EventsNode(event="$pageview"),]- earliest_timestamp = get_earliest_timestamp_from_series(self.team, series) # type: ignore+ earliest_timestamp = get_earliest_timestamp_from_series(self.team, series)self.assertEqual(earliest_timestamp, datetime.datetime(2021, 1, 1, 12, 0, 0, tzinfo=datetime.UTC))@@ -234,7 +243,7 @@ class TestTimestampUtils(APIBaseTest, ClickhouseDestroyTablesMixin):EventsNode(event="$pageview"),EventsNode(event="$pageleave"),]- earliest_timestamp = get_earliest_timestamp_from_series(self.team, series) # type: ignore+ earliest_timestamp = get_earliest_timestamp_from_series(self.team, series)self.assertEqual(earliest_timestamp, datetime.datetime(2020, 1, 1, 12, 0, 0, tzinfo=datetime.UTC))earliest_timestamp_pageview = get_earliest_timestamp_from_series(self.team, [EventsNode(event="$pageview")])@@ -282,7 +291,7 @@ class TestTimestampUtils(APIBaseTest, ClickhouseDestroyTablesMixin):flush_persons_and_events()series = [ActionsNode(id=action1.id), ActionsNode(id=action2.id)]- earliest_timestamp = get_earliest_timestamp_from_series(self.team, series) # type: ignore+ earliest_timestamp = get_earliest_timestamp_from_series(self.team, series)self.assertEqual(earliest_timestamp, datetime.datetime(2020, 1, 1, 12, 0, 0, tzinfo=datetime.UTC))def test_returns_earliest_timestamp_mixed_nodes(self):@@ -303,8 +312,8 @@ class TestTimestampUtils(APIBaseTest, ClickhouseDestroyTablesMixin):)flush_persons_and_events()- series = [ActionsNode(id=action.id), EventsNode(event="$pageleave")]- earliest_timestamp = get_earliest_timestamp_from_series(self.team, series) # type: ignore+ series: list[ActionsNode | EventsNode] = [ActionsNode(id=action.id), EventsNode(event="$pageleave")]+ earliest_timestamp = get_earliest_timestamp_from_series(self.team, series)self.assertEqual(earliest_timestamp, datetime.datetime(2019, 1, 1, 12, 0, 0, tzinfo=datetime.UTC))def test_caches_earliest_timestamp(self):@@ -325,7 +334,7 @@ class TestTimestampUtils(APIBaseTest, ClickhouseDestroyTablesMixin):series = [EventsNode(event="$pageview"),]- earliest_timestamp = get_earliest_timestamp_from_series(self.team, series) # type: ignore+ earliest_timestamp = get_earliest_timestamp_from_series(self.team, series)# create an earlier event to test caching_create_event(@@ -337,5 +346,113 @@ class TestTimestampUtils(APIBaseTest, ClickhouseDestroyTablesMixin):flush_persons_and_events()# should still return the earliest timestamp from the first query- cached_earliest_timestamp = get_earliest_timestamp_from_series(self.team, series) # type: ignore+ cached_earliest_timestamp = get_earliest_timestamp_from_series(self.team, series)self.assertEqual(cached_earliest_timestamp, earliest_timestamp)++ @parameterized.expand(+ [+ # Naive inputs are interpreted in the passed (team) timezone, not UTC.+ (+ "naive_datetime",+ datetime.datetime(2023, 5, 1, 12, 30, 0),+ datetime.datetime(2023, 5, 1, 12, 30, 0, tzinfo=ZoneInfo("America/New_York")),+ ),+ (+ "aware_datetime",+ datetime.datetime(2023, 5, 1, 12, 30, 0, tzinfo=datetime.UTC),+ datetime.datetime(2023, 5, 1, 12, 30, 0, tzinfo=datetime.UTC),+ ),+ (+ "date",+ datetime.date(2023, 5, 1),+ datetime.datetime(2023, 5, 1, 0, 0, 0, tzinfo=ZoneInfo("America/New_York")),+ ),+ (+ "string",+ "2023-05-01 12:30:00",+ datetime.datetime(2023, 5, 1, 12, 30, 0, tzinfo=ZoneInfo("America/New_York")),+ ),+ (+ "date_string",+ "2023-05-01",+ datetime.datetime(2023, 5, 1, 0, 0, 0, tzinfo=ZoneInfo("America/New_York")),+ ),+ ("none", None, EARLIEST_EVENT_TIMESTAMP),+ ("unsupported", 12345, EARLIEST_EVENT_TIMESTAMP),+ ("unparseable_na", "N/A", EARLIEST_EVENT_TIMESTAMP),+ ("unparseable_null", "null", EARLIEST_EVENT_TIMESTAMP),+ ("unparseable_empty", "", EARLIEST_EVENT_TIMESTAMP),+ ("unparseable_freeform", "not a date at all", EARLIEST_EVENT_TIMESTAMP),+ ]+ )+ def test_coerce_to_datetime(self, _name, value, expected):+ result = _coerce_to_datetime(value, ZoneInfo("America/New_York"))+ self.assertEqual(result, expected)+ # Must be timezone-aware so it can be compared against the tz-aware date_to.+ self.assertIsNotNone(result.tzinfo)++ @parameterized.expand(+ [+ ("string_timestamp", "2022-03-15 08:00:00", datetime.datetime(2022, 3, 15, 8, 0, 0, tzinfo=datetime.UTC)),+ ("date_only_string", "2022-03-15", datetime.datetime(2022, 3, 15, 0, 0, 0, tzinfo=datetime.UTC)),+ ("date_object", datetime.date(2022, 3, 15), datetime.datetime(2022, 3, 15, 0, 0, 0, tzinfo=datetime.UTC)),+ ]+ )+ def test_data_warehouse_all_time_resolves_string_timestamp(self, _name, raw_value, expected):+ # Data warehouse tables can return a non-datetime min(timestamp); it must be+ # coerced before reaching QueryDateRange, which calls .strftime() and compares with <.+ node = DataWarehouseNode(+ id="dw_table",+ table_name="dw_table",+ id_field="id",+ distinct_id_field="distinct_id",+ timestamp_field="ts",+ )++ with patch("posthog.hogql_queries.utils.timestamp_utils.execute_hogql_query") as mock_execute:+ mock_execute.return_value.results = [[raw_value]]++ earliest_timestamp = get_earliest_timestamp_from_series(self.team, [node])++ self.assertIsInstance(earliest_timestamp, datetime.datetime)+ self.assertEqual(earliest_timestamp, expected)++ query_date_range = QueryDateRange(+ team=self.team,+ date_range=DateRange(date_from="all"),+ interval=IntervalType.DAY,+ now=parser.isoparse("2025-06-21T00:00:00.000Z"),+ earliest_timestamp_fallback=earliest_timestamp,+ )++ # These previously raised AttributeError / TypeError when date_from="all"+ # resolved to a str instead of a datetime.+ self.assertEqual(query_date_range.date_from(), expected)+ self.assertEqual(query_date_range.date_from_str, expected.strftime("%Y-%m-%d %H:%M:%S"))+ # A Date-typed column yields a naive datetime; comparing it against the tz-aware+ # date_to raised "can't compare offset-naive and offset-aware datetimes" here.+ self.assertGreater(len(query_date_range.all_values()), 0)++ @override_settings(IN_UNIT_TESTING=False)+ @patch("posthog.hogql_queries.utils.timestamp_utils._get_earliest_timestamp_from_node")+ def test_multi_node_propagates_query_tags_to_threads(self, mock_node):+ # Multiple nodes resolve their earliest timestamp via ThreadPoolExecutor, which does not+ # inherit contextvars. Without copying the context, the worker threads' sync_execute calls+ # run untagged and raise UntaggedQueryError in dev (DEBUG and not TEST).+ captured: dict[str, object] = {}++ def capture(team, node):+ captured[node.table_name] = get_query_tags().product+ return datetime.datetime(2020, 1, 1, tzinfo=datetime.UTC)++ mock_node.side_effect = capture+ nodes = [+ DataWarehouseNode(id="a", table_name="a", id_field="id", distinct_id_field="id", timestamp_field="ts"),+ DataWarehouseNode(id="b", table_name="b", id_field="id", distinct_id_field="id", timestamp_field="ts"),+ ]++ with tags_context(product=Product.MARKETING_ANALYTICS, feature=Feature.QUERY):+ get_earliest_timestamp_from_series(self.team, nodes)++ self.assertEqual(captured["a"], Product.MARKETING_ANALYTICS)+ self.assertEqual(captured["b"], Product.MARKETING_ANALYTICS)diff --git a/posthog/hogql_queries/utils/timestamp_utils.py b/posthog/hogql_queries/utils/timestamp_utils.pyindex ea4ed4d5ce29..57f6292268f7 100644--- a/posthog/hogql_queries/utils/timestamp_utils.py+++ b/posthog/hogql_queries/utils/timestamp_utils.py@@ -1,10 +1,13 @@+import contextvars+from collections.abc import Sequencefrom concurrent.futures import ThreadPoolExecutor-from datetime import date, datetime, timedelta-from typing import Union+from datetime import date, datetime, timedelta, tzinfo+from typing import Any, Unionfrom django.conf import settingsfrom django.core.cache import cache+from dateutil.parser import parse as parse_datetimefrom dateutil.relativedelta import MO, SU, relativedeltafrom posthog.schema import (@@ -124,6 +127,34 @@ def _get_earliest_timestamp_cache_key(raise ValueError(f"Unsupported node type: {type(node)}")+def _coerce_to_datetime(value: Any, tz: tzinfo) -> datetime:+ """Normalize a min(timestamp) result to a timezone-aware ``datetime``.++ Data warehouse tables may declare their timestamp field as a String or Date+ column, so the query can return a ``str`` or ``date`` instead of a ``datetime``.+ Leaving those unconverted breaks downstream date math (``.strftime()`` and+ ``<`` comparisons), so we resolve them to a concrete ``datetime`` here.++ The result must be timezone-aware: this value becomes the "all time" date_from,+ which is compared against the timezone-aware date_to. A naive datetime would+ raise "can't compare offset-naive and offset-aware datetimes". Naive values+ (e.g. from a ``Date`` column) are interpreted in the team's timezone — the same+ timezone QueryDateRange buckets in — so the lower bound lines up with the day+ boundaries the rest of the range uses, rather than UTC midnight.+ """+ if isinstance(value, datetime):+ return value if value.tzinfo is not None else value.replace(tzinfo=tz)+ if isinstance(value, date):+ return datetime(value.year, value.month, value.day, tzinfo=tz)+ if isinstance(value, str):+ try:+ parsed = parse_datetime(value)+ except (ValueError, TypeError, OverflowError):+ return EARLIEST_EVENT_TIMESTAMP+ return parsed if parsed.tzinfo is not None else parsed.replace(tzinfo=tz)+ return EARLIEST_EVENT_TIMESTAMP++def _get_earliest_timestamp_from_node(team: Team,node: Union[EventsNode, ActionsNode, DataWarehouseNode, FunnelsDataWarehouseNode, LifecycleDataWarehouseNode],@@ -151,8 +182,8 @@ def _get_earliest_timestamp_from_node(earliest_timestamp = EARLIEST_EVENT_TIMESTAMPresult = execute_hogql_query(query=query, team=team)- if result and len(result.results) > 0 and len(result.results[0]) > 0:- earliest_timestamp = result.results[0][0]+ if result and len(result.results) > 0 and len(result.results[0]) > 0 and result.results[0][0] is not None:+ earliest_timestamp = _coerce_to_datetime(result.results[0][0], team.timezone_info)cache.set(cache_key, earliest_timestamp, timeout=EARLIEST_TIMESTAMP_CACHE_TTL)return earliest_timestamp@@ -160,7 +191,7 @@ def _get_earliest_timestamp_from_node(def get_earliest_timestamp_from_series(team: Team,- series: list[+ series: Sequence[Union[EventsNode, ActionsNode, DataWarehouseNode, FunnelsDataWarehouseNode, LifecycleDataWarehouseNode, GroupNode]@@ -191,7 +222,12 @@ def get_earliest_timestamp_from_series(else:with ThreadPoolExecutor(max_workers=min(len(nodes), 4)) as executor:- futures = [executor.submit(_get_earliest_timestamp_from_node, team, node) for node in nodes]+ # ThreadPoolExecutor does not inherit contextvars (query tags) by default; copy the+ # current context into each worker so tagged sync_execute calls don't fail untagged.+ futures = [+ executor.submit(contextvars.copy_context().run, _get_earliest_timestamp_from_node, team, node)+ for node in nodes+ ]timestamps = [future.result() for future in futures]return min(timestamps)