|
| 1 | +from datetime import datetime |
| 2 | +from unittest.mock import MagicMock, patch |
| 3 | + |
| 4 | +from feast.infra.offline_stores.contrib.spark_offline_store.spark import ( |
| 5 | + SparkOfflineStore, |
| 6 | + SparkOfflineStoreConfig, |
| 7 | +) |
| 8 | +from feast.infra.offline_stores.contrib.spark_offline_store.spark_source import ( |
| 9 | + SparkSource, |
| 10 | +) |
| 11 | +from feast.infra.offline_stores.offline_store import RetrievalJob |
| 12 | +from feast.repo_config import RepoConfig |
| 13 | + |
| 14 | + |
| 15 | +@patch( |
| 16 | + "feast.infra.offline_stores.contrib.spark_offline_store.spark.get_spark_session_or_start_new_with_repoconfig" |
| 17 | +) |
| 18 | +def test_pull_latest_from_table_with_nested_timestamp_or_query(mock_get_spark_session): |
| 19 | + mock_spark_session = MagicMock() |
| 20 | + mock_get_spark_session.return_value = mock_spark_session |
| 21 | + |
| 22 | + test_repo_config = RepoConfig( |
| 23 | + project="test_project", |
| 24 | + registry="test_registry", |
| 25 | + provider="local", |
| 26 | + offline_store=SparkOfflineStoreConfig(type="spark"), |
| 27 | + ) |
| 28 | + |
| 29 | + test_data_source = SparkSource( |
| 30 | + name="test_nested_batch_source", |
| 31 | + description="test_nested_batch_source", |
| 32 | + table="offline_store_database_name.offline_store_table_name", |
| 33 | + timestamp_field="nested_timestamp", |
| 34 | + field_mapping={ |
| 35 | + "event_header.event_published_datetime_utc": "nested_timestamp", |
| 36 | + }, |
| 37 | + ) |
| 38 | + |
| 39 | + # Define the parameters for the method |
| 40 | + join_key_columns = ["key1", "key2"] |
| 41 | + feature_name_columns = ["feature1", "feature2"] |
| 42 | + timestamp_field = "event_header.event_published_datetime_utc" |
| 43 | + created_timestamp_column = "created_timestamp" |
| 44 | + start_date = datetime(2021, 1, 1) |
| 45 | + end_date = datetime(2021, 1, 2) |
| 46 | + |
| 47 | + # Call the method |
| 48 | + retrieval_job = SparkOfflineStore.pull_latest_from_table_or_query( |
| 49 | + config=test_repo_config, |
| 50 | + data_source=test_data_source, |
| 51 | + join_key_columns=join_key_columns, |
| 52 | + feature_name_columns=feature_name_columns, |
| 53 | + timestamp_field=timestamp_field, |
| 54 | + created_timestamp_column=created_timestamp_column, |
| 55 | + start_date=start_date, |
| 56 | + end_date=end_date, |
| 57 | + ) |
| 58 | + |
| 59 | + expected_query = """SELECT |
| 60 | + key1, key2, feature1, feature2, nested_timestamp, created_timestamp |
| 61 | + |
| 62 | + FROM ( |
| 63 | + SELECT key1, key2, feature1, feature2, event_header.event_published_datetime_utc AS nested_timestamp, created_timestamp, |
| 64 | + ROW_NUMBER() OVER(PARTITION BY key1, key2 ORDER BY event_header.event_published_datetime_utc DESC, created_timestamp DESC) AS feast_row_ |
| 65 | + FROM `offline_store_database_name`.`offline_store_table_name` t1 |
| 66 | + 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') |
| 67 | + ) t2 |
| 68 | + WHERE feast_row_ = 1""" # noqa: W293 |
| 69 | + |
| 70 | + assert isinstance(retrieval_job, RetrievalJob) |
| 71 | + assert retrieval_job.query.strip() == expected_query.strip() |
| 72 | + |
| 73 | + |
| 74 | +@patch( |
| 75 | + "feast.infra.offline_stores.contrib.spark_offline_store.spark.get_spark_session_or_start_new_with_repoconfig" |
| 76 | +) |
| 77 | +def test_pull_latest_from_table_without_nested_timestamp_or_query( |
| 78 | + mock_get_spark_session, |
| 79 | +): |
| 80 | + mock_spark_session = MagicMock() |
| 81 | + mock_get_spark_session.return_value = mock_spark_session |
| 82 | + |
| 83 | + test_repo_config = RepoConfig( |
| 84 | + project="test_project", |
| 85 | + registry="test_registry", |
| 86 | + provider="local", |
| 87 | + offline_store=SparkOfflineStoreConfig(type="spark"), |
| 88 | + ) |
| 89 | + |
| 90 | + test_data_source = SparkSource( |
| 91 | + name="test_batch_source", |
| 92 | + description="test_nested_batch_source", |
| 93 | + table="offline_store_database_name.offline_store_table_name", |
| 94 | + timestamp_field="event_published_datetime_utc", |
| 95 | + ) |
| 96 | + |
| 97 | + # Define the parameters for the method |
| 98 | + join_key_columns = ["key1", "key2"] |
| 99 | + feature_name_columns = ["feature1", "feature2"] |
| 100 | + timestamp_field = "event_published_datetime_utc" |
| 101 | + created_timestamp_column = "created_timestamp" |
| 102 | + start_date = datetime(2021, 1, 1) |
| 103 | + end_date = datetime(2021, 1, 2) |
| 104 | + |
| 105 | + # Call the method |
| 106 | + retrieval_job = SparkOfflineStore.pull_latest_from_table_or_query( |
| 107 | + config=test_repo_config, |
| 108 | + data_source=test_data_source, |
| 109 | + join_key_columns=join_key_columns, |
| 110 | + feature_name_columns=feature_name_columns, |
| 111 | + timestamp_field=timestamp_field, |
| 112 | + created_timestamp_column=created_timestamp_column, |
| 113 | + start_date=start_date, |
| 114 | + end_date=end_date, |
| 115 | + ) |
| 116 | + |
| 117 | + expected_query = """SELECT |
| 118 | + key1, key2, feature1, feature2, event_published_datetime_utc, created_timestamp |
| 119 | + |
| 120 | + FROM ( |
| 121 | + SELECT key1, key2, feature1, feature2, event_published_datetime_utc, created_timestamp, |
| 122 | + ROW_NUMBER() OVER(PARTITION BY key1, key2 ORDER BY event_published_datetime_utc DESC, created_timestamp DESC) AS feast_row_ |
| 123 | + FROM `offline_store_database_name`.`offline_store_table_name` t1 |
| 124 | + WHERE event_published_datetime_utc BETWEEN TIMESTAMP('2021-01-01 00:00:00.000000') AND TIMESTAMP('2021-01-02 00:00:00.000000') |
| 125 | + ) t2 |
| 126 | + WHERE feast_row_ = 1""" # noqa: W293 |
| 127 | + |
| 128 | + assert isinstance(retrieval_job, RetrievalJob) |
| 129 | + assert retrieval_job.query.strip() == expected_query.strip() |
0 commit comments