Skip to content

Commit 0356181

Browse files
authored
Refactor _convert_arrow_to_proto (#2085)
* Refactor `_convert_arrow_to_proto` Signed-off-by: Judah Rand <17158624+judahrand@users.noreply.github.com> * Add `benchmark-python-local` to Makefile Signed-off-by: Judah Rand <17158624+judahrand@users.noreply.github.com>
1 parent 352dd6c commit 0356181

2 files changed

Lines changed: 56 additions & 43 deletions

File tree

‎Makefile‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ install-python:
5555
benchmark-python:
5656
FEAST_USAGE=False IS_TEST=True python -m pytest --integration --benchmark --benchmark-autosave --benchmark-save-data sdk/python/tests
5757

58+
benchmark-python-local:
59+
FEAST_USAGE=False IS_TEST=True FEAST_IS_LOCAL_TEST=True python -m pytest --integration --benchmark --benchmark-autosave --benchmark-save-data sdk/python/tests
60+
5861
test-python:
5962
FEAST_USAGE=False IS_TEST=True python -m pytest -n 8 sdk/python/tests
6063

‎sdk/python/feast/infra/provider.py‎

Lines changed: 53 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -284,56 +284,66 @@ def _run_field_mapping(
284284
return table
285285

286286

287+
def _coerce_datetime(ts):
288+
"""
289+
Depending on underlying time resolution, arrow to_pydict() sometimes returns pandas
290+
timestamp type (for nanosecond resolution), and sometimes you get standard python datetime
291+
(for microsecond resolution).
292+
While pandas timestamp class is a subclass of python datetime, it doesn't always behave the
293+
same way. We convert it to normal datetime so that consumers downstream don't have to deal
294+
with these quirks.
295+
"""
296+
297+
if isinstance(ts, pandas.Timestamp):
298+
return ts.to_pydatetime()
299+
else:
300+
return ts
301+
302+
287303
def _convert_arrow_to_proto(
288304
table: Union[pyarrow.Table, pyarrow.RecordBatch],
289305
feature_view: FeatureView,
290306
join_keys: List[str],
291307
) -> List[Tuple[EntityKeyProto, Dict[str, ValueProto], datetime, Optional[datetime]]]:
292-
rows_to_write = []
293-
294-
def _coerce_datetime(ts):
295-
"""
296-
Depending on underlying time resolution, arrow to_pydict() sometimes returns pandas
297-
timestamp type (for nanosecond resolution), and sometimes you get standard python datetime
298-
(for microsecond resolution).
299-
300-
While pandas timestamp class is a subclass of python datetime, it doesn't always behave the
301-
same way. We convert it to normal datetime so that consumers downstream don't have to deal
302-
with these quirks.
303-
"""
308+
# Handle join keys
309+
join_key_values = {k: table.column(k).to_pylist() for k in join_keys}
310+
entity_keys = [
311+
EntityKeyProto(
312+
join_keys=join_keys,
313+
entity_values=[
314+
python_value_to_proto_value(join_key_values[k][idx]) for k in join_keys
315+
],
316+
)
317+
for idx in range(table.num_rows)
318+
]
304319

305-
if isinstance(ts, pandas.Timestamp):
306-
return ts.to_pydatetime()
307-
else:
308-
return ts
309-
310-
column_names_idx = {field.name: i for i, field in enumerate(table.schema)}
311-
for row in zip(*table.to_pydict().values()):
312-
entity_key = EntityKeyProto()
313-
for join_key in join_keys:
314-
entity_key.join_keys.append(join_key)
315-
idx = column_names_idx[join_key]
316-
value = python_value_to_proto_value(row[idx])
317-
entity_key.entity_values.append(value)
318-
feature_dict = {}
319-
for feature in feature_view.features:
320-
idx = column_names_idx[feature.name]
321-
value = python_value_to_proto_value(row[idx], feature.dtype)
322-
feature_dict[feature.name] = value
323-
event_timestamp_idx = column_names_idx[
324-
feature_view.batch_source.event_timestamp_column
320+
# Serialize the features per row
321+
feature_dict = {
322+
feature.name: [
323+
python_value_to_proto_value(val, feature.dtype)
324+
for val in table.column(feature.name).to_pylist()
325325
]
326-
event_timestamp = _coerce_datetime(row[event_timestamp_idx])
326+
for feature in feature_view.features
327+
}
328+
features = [dict(zip(feature_dict, vars)) for vars in zip(*feature_dict.values())]
329+
330+
# Convert event_timestamps
331+
event_timestamps = [
332+
_coerce_datetime(val)
333+
for val in table.column(
334+
feature_view.batch_source.event_timestamp_column
335+
).to_pylist()
336+
]
327337

328-
if feature_view.batch_source.created_timestamp_column:
329-
created_timestamp_idx = column_names_idx[
338+
# Convert created_timestamps if they exist
339+
if feature_view.batch_source.created_timestamp_column:
340+
created_timestamps = [
341+
_coerce_datetime(val)
342+
for val in table.column(
330343
feature_view.batch_source.created_timestamp_column
331-
]
332-
created_timestamp = _coerce_datetime(row[created_timestamp_idx])
333-
else:
334-
created_timestamp = None
344+
).to_pylist()
345+
]
346+
else:
347+
created_timestamps = [None] * table.num_rows
335348

336-
rows_to_write.append(
337-
(entity_key, feature_dict, event_timestamp, created_timestamp)
338-
)
339-
return rows_to_write
349+
return list(zip(entity_keys, features, event_timestamps, created_timestamps))

0 commit comments

Comments
 (0)