Skip to content

Commit e81e69d

Browse files
Nikhi00718ntkathole
authored andcommitted
fix: Materialize zone-aware nanosecond timestamps on DuckDB 2
The ibis pull functions cast the timestamp column to a zone-less timestamp for the window filter. DuckDB 2 refuses that cast for TIMESTAMPTZ_NS, which pandas writes by default, and ibis reports the type as unknown. Keep the zone when casting. Signed-off-by: NIKHIL <atnikhil06@gmail.com>
1 parent f540542 commit e81e69d

2 files changed

Lines changed: 71 additions & 6 deletions

File tree

‎sdk/python/feast/infra/offline_stores/ibis.py‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ def pull_latest_from_table_or_query_ibis(
9393

9494
table = table.filter(
9595
ibis.and_(
96-
table[timestamp_field].cast("timestamp") >= ibis.literal(start_date),
97-
table[timestamp_field].cast("timestamp") <= ibis.literal(end_date),
96+
_comparable_timestamp(table[timestamp_field]) >= ibis.literal(start_date),
97+
_comparable_timestamp(table[timestamp_field]) <= ibis.literal(end_date),
9898
)
9999
)
100100

@@ -117,6 +117,19 @@ def pull_latest_from_table_or_query_ibis(
117117
)
118118

119119

120+
def _comparable_timestamp(column):
121+
"""Cast a timestamp column for comparison with a window bound, keeping any zone.
122+
123+
DuckDB 2 refuses to cast TIMESTAMPTZ_NS, which is what pandas writes by default, to a
124+
TIMESTAMP without a zone, and ibis reports that column's type as unknown.
125+
"""
126+
column_type = column.type()
127+
if column_type.is_unknown():
128+
return column.cast(dt.Timestamp(timezone="UTC"))
129+
timezone = getattr(column_type, "timezone", None)
130+
return column.cast(dt.Timestamp(timezone=timezone) if timezone else "timestamp")
131+
132+
120133
def _get_entity_df_event_timestamp_range(
121134
entity_df: pd.DataFrame, entity_df_event_timestamp_col: str
122135
) -> Tuple[datetime, datetime]:
@@ -345,10 +358,10 @@ def pull_all_from_table_or_query_ibis(
345358

346359
table = table.filter(
347360
ibis.and_(
348-
table[timestamp_field].cast("timestamp") >= ibis.literal(start_date)
361+
_comparable_timestamp(table[timestamp_field]) >= ibis.literal(start_date)
349362
if start_date
350363
else ibis.literal(True),
351-
table[timestamp_field].cast("timestamp") <= ibis.literal(end_date)
364+
_comparable_timestamp(table[timestamp_field]) <= ibis.literal(end_date)
352365
if end_date
353366
else ibis.literal(True),
354367
)

‎sdk/python/tests/unit/infra/offline_stores/test_ibis.py‎

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1-
from datetime import datetime, timedelta
1+
from datetime import datetime, timedelta, timezone
22
from typing import Dict, List, Tuple
3+
from unittest.mock import MagicMock
34

45
import ibis
6+
import pandas as pd
57
import pyarrow as pa
68
import pyarrow.compute as pc
9+
import pytest
710

8-
from feast.infra.offline_stores.ibis import point_in_time_join
11+
from feast.infra.offline_stores.ibis import (
12+
point_in_time_join,
13+
pull_all_from_table_or_query_ibis,
14+
pull_latest_from_table_or_query_ibis,
15+
)
916

1017

1118
def pa_datetime(year, month, day):
@@ -226,3 +233,48 @@ def test_point_in_time_join_keeps_entity_rows_apart():
226233
for row in actual.to_pylist()
227234
}
228235
assert rows == {"1": (1.0, 110.0), "11": (11.0, 10.0), "7": (7.0, None)}
236+
237+
238+
@pytest.mark.parametrize(
239+
"dtype",
240+
["datetime64[ns, UTC]", "datetime64[us, UTC]", "datetime64[ns]", "datetime64[us]"],
241+
)
242+
@pytest.mark.parametrize(
243+
"pull", [pull_latest_from_table_or_query_ibis, pull_all_from_table_or_query_ibis]
244+
)
245+
def test_pull_filters_parquet_timestamps_of_any_precision(tmp_path, dtype, pull):
246+
"""Materialize pulls read what pandas writes: nanosecond and zone-aware included."""
247+
start = datetime(2026, 1, 1, tzinfo=timezone.utc)
248+
path = tmp_path / "source.parquet"
249+
pd.DataFrame(
250+
{
251+
"driver_id": [1, 2, 3, 4],
252+
"event_timestamp": [start + timedelta(hours=h) for h in range(4)],
253+
"trips": [10, 20, 30, 40],
254+
}
255+
).astype(
256+
{"event_timestamp": dtype if "UTC" in dtype else "datetime64[ns, UTC]"}
257+
).pipe(
258+
lambda df: (
259+
df
260+
if "UTC" in dtype
261+
else df.assign(
262+
event_timestamp=df.event_timestamp.dt.tz_localize(None).astype(dtype)
263+
)
264+
)
265+
).to_parquet(path)
266+
267+
table = pull(
268+
config=MagicMock(),
269+
data_source=MagicMock(),
270+
join_key_columns=["driver_id"],
271+
feature_name_columns=["trips"],
272+
timestamp_field="event_timestamp",
273+
created_timestamp_column=None,
274+
start_date=start + timedelta(hours=1),
275+
end_date=start + timedelta(hours=2),
276+
data_source_reader=lambda *_: ibis.read_parquet(path),
277+
data_source_writer=MagicMock(),
278+
).to_arrow()
279+
280+
assert sorted(table["driver_id"].to_pylist()) == [2, 3]

0 commit comments

Comments
 (0)