Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions sdk/python/feast/infra/compute_engines/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ class DataFrameBackend(ABC):
filter(df: Any, expr: str) -> Any
Apply a filter expression (string-based) to the DataFrame.

filter_by_mask(df: Any, mask: Any) -> Any
Keep the rows of the DataFrame for which the boolean mask is true.

to_timedelta_value(delta: timedelta) -> Any
Convert a Python timedelta object to a backend-compatible value
that can be subtracted from a timestamp column.
Expand Down Expand Up @@ -65,6 +68,9 @@ def groupby_agg(self, df, group_keys, agg_ops): ...
@abstractmethod
def filter(self, df, expr): ...

@abstractmethod
def filter_by_mask(self, df, mask): ...

@abstractmethod
def to_arrow(self, df): ...

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ def groupby_agg(self, df, group_keys, agg_ops):
def filter(self, df, expr):
return df.query(expr)

def filter_by_mask(self, df, mask):
return df[mask]

def to_arrow(self, df):
return pa.Table.from_pandas(df)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ def groupby_agg(self, df: pl.DataFrame, group_keys, agg_ops) -> pl.DataFrame:
def filter(self, df: pl.DataFrame, expr: str) -> pl.DataFrame:
return df.filter(pl.sql_expr(expr))

def filter_by_mask(self, df: pl.DataFrame, mask) -> pl.DataFrame:
return df.filter(mask)

def to_timedelta_value(self, delta: timedelta):
return pl.duration(milliseconds=delta.total_seconds() * 1000)

Expand Down
8 changes: 6 additions & 2 deletions sdk/python/feast/infra/compute_engines/local/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,18 @@ def execute(self, context: ExecutionContext) -> ArrowTableValue:

if ENTITY_TS_ALIAS in self.backend.columns(df):
# filter where feature.ts <= entity.event_timestamp
df = df[df[timestamp_column] <= df[ENTITY_TS_ALIAS]]
df = self.backend.filter_by_mask(
df, df[timestamp_column] <= df[ENTITY_TS_ALIAS]
)

# TTL: feature.ts >= entity.event_timestamp - ttl
if self.ttl:
lower_bound = df[ENTITY_TS_ALIAS] - self.backend.to_timedelta_value(
self.ttl
)
df = df[df[timestamp_column] >= lower_bound]
df = self.backend.filter_by_mask(
df, df[timestamp_column] >= lower_bound
)

# Optional user-defined filter expression (e.g., "value > 0")
if self.filter_expr:
Expand Down
50 changes: 50 additions & 0 deletions sdk/python/tests/unit/infra/compute_engines/local/test_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
LocalOutputNode,
LocalTransformationNode,
)
from feast.infra.compute_engines.utils import ENTITY_TS_ALIAS
from feast.repo_config import MaterializationConfig

backend = PandasBackend()
Expand Down Expand Up @@ -78,6 +79,55 @@ def test_local_filter_node():
assert result.data.num_rows == 3


@pytest.mark.parametrize("backend_name", ["pandas", "polars"])
@pytest.mark.parametrize(
"ttl, expected_values",
[(None, [20, 30, 40]), (timedelta(minutes=2), [20, 30])],
)
def test_local_filter_node_point_in_time(backend_name, ttl, expected_values):
if backend_name == "polars":
pytest.importorskip("polars")
from feast.infra.compute_engines.backends.factory import BackendFactory

# Rows as they look after the join with the entity dataframe: one row is
# newer than the entity timestamp and one is older than the 2 minute TTL.
joined_df = pd.DataFrame(
{
"entity_id": [1, 1, 2, 2],
"value": [10, 20, 30, 40],
"event_timestamp": [
now + timedelta(minutes=1),
now - timedelta(minutes=1),
now,
now - timedelta(minutes=5),
],
ENTITY_TS_ALIAS: [now] * 4,
}
)
context = create_context(
node_outputs={"source": ArrowTableValue(pa.Table.from_pandas(joined_df))}
)

filter_node = LocalFilterNode(
name="filter",
backend=BackendFactory.from_name(backend_name),
ttl=ttl,
column_info=ColumnInfo(
join_keys=["entity_id"],
feature_cols=["value"],
ts_col="event_timestamp",
created_ts_col=None,
),
)
filter_node.add_input(MagicMock())
filter_node.inputs[0].name = "source"

result = filter_node.execute(context)
assert isinstance(result, ArrowTableValue)
assert set(joined_df.columns) <= set(result.data.column_names)
assert result.data.column("value").to_pylist() == expected_values


def test_local_aggregation_node():
context = create_context(
node_outputs={"source": ArrowTableValue(pa.Table.from_pandas(sample_df))}
Expand Down
Loading