From 632da298d44ceac20a86ac65e9610ac9e80433a2 Mon Sep 17 00:00:00 2001 From: Mrun25 <165297479+Mrun25@users.noreply.github.com> Date: Tue, 20 Oct 2026 20:49:34 +0530 Subject: [PATCH] fix: use table_format when writing to path-based SparkSource in offline_write_batch Signed-off-by: Mrun25 <165297479+Mrun25@users.noreply.github.com> --- .../contrib/spark_offline_store/spark.py | 21 +++-- .../test_spark_offline_write_batch.py | 94 +++++++++++++++++++ 2 files changed, 109 insertions(+), 6 deletions(-) create mode 100644 sdk/python/tests/unit/infra/offline_stores/contrib/spark_offline_store/test_spark_offline_write_batch.py diff --git a/sdk/python/feast/infra/offline_stores/contrib/spark_offline_store/spark.py b/sdk/python/feast/infra/offline_stores/contrib/spark_offline_store/spark.py index 048c65be1ad..168bb5c17f2 100644 --- a/sdk/python/feast/infra/offline_stores/contrib/spark_offline_store/spark.py +++ b/sdk/python/feast/infra/offline_stores/contrib/spark_offline_store/spark.py @@ -407,16 +407,25 @@ def offline_write_batch( df_batch = spark_session.read.parquet(tmp_file.name) # load existing data to get spark table schema - df_existing = spark_session.read.format( - feature_view.batch_source.file_format - ).load(feature_view.batch_source.path) + df_existing = feature_view.batch_source._load_dataframe_from_path( + spark_session + ) # cast columns if applicable df_batch = _cast_data_frame(df_batch, df_existing) - df_batch.write.format(feature_view.batch_source.file_format).mode( - "append" - ).save(feature_view.batch_source.path) + # Write with the same format the source is read with: the table + # format (and its options) when set, otherwise the file format. + table_format = feature_view.batch_source.table_format + if table_format is None: + writer = df_batch.write.format( + feature_view.batch_source.file_format + ) + else: + writer = df_batch.write.format(table_format.format_type.value) + for key, value in table_format.properties.items(): + writer = writer.option(key, value) + writer.mode("append").save(feature_view.batch_source.path) elif feature_view.batch_source.query: raise NotImplementedError( "offline_write_batch not implemented for batch sources specified by query" diff --git a/sdk/python/tests/unit/infra/offline_stores/contrib/spark_offline_store/test_spark_offline_write_batch.py b/sdk/python/tests/unit/infra/offline_stores/contrib/spark_offline_store/test_spark_offline_write_batch.py new file mode 100644 index 00000000000..b532a25e623 --- /dev/null +++ b/sdk/python/tests/unit/infra/offline_stores/contrib/spark_offline_store/test_spark_offline_write_batch.py @@ -0,0 +1,94 @@ +""" +Unit tests for SparkOfflineStore.offline_write_batch with path-based sources. + +Covers https://github.com/feast-dev/feast/issues/6955: offline_write_batch only +used `file_format`, so a source configured with `table_format` (and no +`file_format`) was read and written with `format(None)`. +""" + +from unittest.mock import MagicMock, patch + +import pyarrow as pa +import pytest + +from feast.infra.offline_stores.contrib.spark_offline_store import spark as spark_mod +from feast.infra.offline_stores.contrib.spark_offline_store.spark import ( + SparkOfflineStore, + SparkOfflineStoreConfig, +) +from feast.infra.offline_stores.contrib.spark_offline_store.spark_source import ( + SparkSource, +) +from feast.table_format import DeltaFormat + + +def _write_batch(source): + table = pa.table({"a": [1]}) + feature_view = MagicMock() + feature_view.batch_source = source + config = MagicMock() + config.offline_store = MagicMock(spec=SparkOfflineStoreConfig) + session = MagicMock() + with ( + patch.object( + spark_mod, + "get_spark_session_or_start_new_with_repoconfig", + return_value=session, + ), + patch.object( + spark_mod.offline_utils, + "get_pyarrow_schema_from_batch_source", + return_value=(table.schema, ["a"]), + ), + patch.object(spark_mod, "_cast_data_frame", side_effect=lambda b, e: b), + ): + SparkOfflineStore.offline_write_batch(config, feature_view, table, None) + return session + + +def test_offline_write_batch_uses_file_format(): + source = SparkSource( + name="s", path="/tmp/x", file_format="parquet", timestamp_field="ts" + ) + session = _write_batch(source) + + session.read.format.assert_called_once_with("parquet") + writer = session.read.parquet.return_value.write + writer.format.assert_called_once_with("parquet") + writer.format.return_value.mode.assert_called_once_with("append") + writer.format.return_value.mode.return_value.save.assert_called_once_with("/tmp/x") + + +def test_offline_write_batch_uses_table_format_without_file_format(): + source = SparkSource( + name="s", + path="/tmp/x", + table_format=DeltaFormat(checkpoint_location="/tmp/ckpt"), + timestamp_field="ts", + ) + session = _write_batch(source) + + session.read.format.assert_called_once_with("delta") + writer = session.read.parquet.return_value.write + writer.format.assert_called_once_with("delta") + writer.format.return_value.option.assert_called_once_with( + "delta.checkpointLocation", "/tmp/ckpt" + ) + option_writer = writer.format.return_value.option.return_value + option_writer.mode.assert_called_once_with("append") + option_writer.mode.return_value.save.assert_called_once_with("/tmp/x") + + +@pytest.mark.parametrize("file_format", [None, "parquet"]) +def test_offline_write_batch_table_format_takes_precedence(file_format): + source = SparkSource( + name="s", + path="/tmp/x", + file_format=file_format, + table_format=DeltaFormat(), + timestamp_field="ts", + ) + session = _write_batch(source) + + session.read.format.assert_called_once_with("delta") + session.read.parquet.return_value.write.format.assert_called_once_with("delta")