-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support for nested timestamp fields in Spark Offline store (#4740)
- Loading branch information
1 parent
9bbc1c6
commit d4d94f8
Showing
7 changed files
with
236 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
sdk/python/tests/unit/infra/offline_stores/contrib/spark_offline_store/test_spark.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
from datetime import datetime | ||
from unittest.mock import MagicMock, patch | ||
|
||
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.infra.offline_stores.offline_store import RetrievalJob | ||
from feast.repo_config import RepoConfig | ||
|
||
|
||
@patch( | ||
"feast.infra.offline_stores.contrib.spark_offline_store.spark.get_spark_session_or_start_new_with_repoconfig" | ||
) | ||
def test_pull_latest_from_table_with_nested_timestamp_or_query(mock_get_spark_session): | ||
mock_spark_session = MagicMock() | ||
mock_get_spark_session.return_value = mock_spark_session | ||
|
||
test_repo_config = RepoConfig( | ||
project="test_project", | ||
registry="test_registry", | ||
provider="local", | ||
offline_store=SparkOfflineStoreConfig(type="spark"), | ||
) | ||
|
||
test_data_source = SparkSource( | ||
name="test_nested_batch_source", | ||
description="test_nested_batch_source", | ||
table="offline_store_database_name.offline_store_table_name", | ||
timestamp_field="nested_timestamp", | ||
field_mapping={ | ||
"event_header.event_published_datetime_utc": "nested_timestamp", | ||
}, | ||
) | ||
|
||
# Define the parameters for the method | ||
join_key_columns = ["key1", "key2"] | ||
feature_name_columns = ["feature1", "feature2"] | ||
timestamp_field = "event_header.event_published_datetime_utc" | ||
created_timestamp_column = "created_timestamp" | ||
start_date = datetime(2021, 1, 1) | ||
end_date = datetime(2021, 1, 2) | ||
|
||
# Call the method | ||
retrieval_job = SparkOfflineStore.pull_latest_from_table_or_query( | ||
config=test_repo_config, | ||
data_source=test_data_source, | ||
join_key_columns=join_key_columns, | ||
feature_name_columns=feature_name_columns, | ||
timestamp_field=timestamp_field, | ||
created_timestamp_column=created_timestamp_column, | ||
start_date=start_date, | ||
end_date=end_date, | ||
) | ||
|
||
expected_query = """SELECT | ||
key1, key2, feature1, feature2, nested_timestamp, created_timestamp | ||
FROM ( | ||
SELECT key1, key2, feature1, feature2, event_header.event_published_datetime_utc AS nested_timestamp, created_timestamp, | ||
ROW_NUMBER() OVER(PARTITION BY key1, key2 ORDER BY event_header.event_published_datetime_utc DESC, created_timestamp DESC) AS feast_row_ | ||
FROM `offline_store_database_name`.`offline_store_table_name` t1 | ||
WHERE event_header.event_published_datetime_utc BETWEEN TIMESTAMP('2021-01-01 00:00:00.000000') AND TIMESTAMP('2021-01-02 00:00:00.000000') | ||
) t2 | ||
WHERE feast_row_ = 1""" # noqa: W293 | ||
|
||
assert isinstance(retrieval_job, RetrievalJob) | ||
assert retrieval_job.query.strip() == expected_query.strip() | ||
|
||
|
||
@patch( | ||
"feast.infra.offline_stores.contrib.spark_offline_store.spark.get_spark_session_or_start_new_with_repoconfig" | ||
) | ||
def test_pull_latest_from_table_without_nested_timestamp_or_query( | ||
mock_get_spark_session, | ||
): | ||
mock_spark_session = MagicMock() | ||
mock_get_spark_session.return_value = mock_spark_session | ||
|
||
test_repo_config = RepoConfig( | ||
project="test_project", | ||
registry="test_registry", | ||
provider="local", | ||
offline_store=SparkOfflineStoreConfig(type="spark"), | ||
) | ||
|
||
test_data_source = SparkSource( | ||
name="test_batch_source", | ||
description="test_nested_batch_source", | ||
table="offline_store_database_name.offline_store_table_name", | ||
timestamp_field="event_published_datetime_utc", | ||
) | ||
|
||
# Define the parameters for the method | ||
join_key_columns = ["key1", "key2"] | ||
feature_name_columns = ["feature1", "feature2"] | ||
timestamp_field = "event_published_datetime_utc" | ||
created_timestamp_column = "created_timestamp" | ||
start_date = datetime(2021, 1, 1) | ||
end_date = datetime(2021, 1, 2) | ||
|
||
# Call the method | ||
retrieval_job = SparkOfflineStore.pull_latest_from_table_or_query( | ||
config=test_repo_config, | ||
data_source=test_data_source, | ||
join_key_columns=join_key_columns, | ||
feature_name_columns=feature_name_columns, | ||
timestamp_field=timestamp_field, | ||
created_timestamp_column=created_timestamp_column, | ||
start_date=start_date, | ||
end_date=end_date, | ||
) | ||
|
||
expected_query = """SELECT | ||
key1, key2, feature1, feature2, event_published_datetime_utc, created_timestamp | ||
FROM ( | ||
SELECT key1, key2, feature1, feature2, event_published_datetime_utc, created_timestamp, | ||
ROW_NUMBER() OVER(PARTITION BY key1, key2 ORDER BY event_published_datetime_utc DESC, created_timestamp DESC) AS feast_row_ | ||
FROM `offline_store_database_name`.`offline_store_table_name` t1 | ||
WHERE event_published_datetime_utc BETWEEN TIMESTAMP('2021-01-01 00:00:00.000000') AND TIMESTAMP('2021-01-02 00:00:00.000000') | ||
) t2 | ||
WHERE feast_row_ = 1""" # noqa: W293 | ||
|
||
assert isinstance(retrieval_job, RetrievalJob) | ||
assert retrieval_job.query.strip() == expected_query.strip() |