|
1 | 1 | import tempfile
|
2 | 2 | import warnings
|
3 | 3 | from datetime import datetime
|
4 |
| -from typing import Dict, List, Optional, Tuple, Union |
| 4 | +from typing import Any, Callable, Dict, List, Optional, Tuple, Union |
5 | 5 |
|
6 | 6 | import numpy as np
|
7 | 7 | import pandas
|
@@ -191,6 +191,68 @@ def get_historical_features(
|
191 | 191 | ),
|
192 | 192 | )
|
193 | 193 |
|
| 194 | + @staticmethod |
| 195 | + def offline_write_batch( |
| 196 | + config: RepoConfig, |
| 197 | + feature_view: FeatureView, |
| 198 | + table: pyarrow.Table, |
| 199 | + progress: Optional[Callable[[int], Any]], |
| 200 | + ): |
| 201 | + if not feature_view.batch_source: |
| 202 | + raise ValueError( |
| 203 | + "feature view does not have a batch source to persist offline data" |
| 204 | + ) |
| 205 | + if not isinstance(config.offline_store, SparkOfflineStoreConfig): |
| 206 | + raise ValueError( |
| 207 | + f"offline store config is of type {type(config.offline_store)} when spark type required" |
| 208 | + ) |
| 209 | + if not isinstance(feature_view.batch_source, SparkSource): |
| 210 | + raise ValueError( |
| 211 | + f"feature view batch source is {type(feature_view.batch_source)} not spark source" |
| 212 | + ) |
| 213 | + |
| 214 | + pa_schema, column_names = offline_utils.get_pyarrow_schema_from_batch_source( |
| 215 | + config, feature_view.batch_source |
| 216 | + ) |
| 217 | + if column_names != table.column_names: |
| 218 | + raise ValueError( |
| 219 | + f"The input pyarrow table has schema {table.schema} with the incorrect columns {table.column_names}. " |
| 220 | + f"The schema is expected to be {pa_schema} with the columns (in this exact order) to be {column_names}." |
| 221 | + ) |
| 222 | + |
| 223 | + spark_session = get_spark_session_or_start_new_with_repoconfig( |
| 224 | + store_config=config.offline_store |
| 225 | + ) |
| 226 | + |
| 227 | + if feature_view.batch_source.path: |
| 228 | + # write data to disk so that it can be loaded into spark (for preserving column types) |
| 229 | + with tempfile.NamedTemporaryFile(suffix=".parquet") as tmp_file: |
| 230 | + print(tmp_file.name) |
| 231 | + pq.write_table(table, tmp_file.name) |
| 232 | + |
| 233 | + # load data |
| 234 | + df_batch = spark_session.read.parquet(tmp_file.name) |
| 235 | + |
| 236 | + # load existing data to get spark table schema |
| 237 | + df_existing = spark_session.read.format( |
| 238 | + feature_view.batch_source.file_format |
| 239 | + ).load(feature_view.batch_source.path) |
| 240 | + |
| 241 | + # cast columns if applicable |
| 242 | + df_batch = _cast_data_frame(df_batch, df_existing) |
| 243 | + |
| 244 | + df_batch.write.format(feature_view.batch_source.file_format).mode( |
| 245 | + "append" |
| 246 | + ).save(feature_view.batch_source.path) |
| 247 | + elif feature_view.batch_source.query: |
| 248 | + raise NotImplementedError( |
| 249 | + "offline_write_batch not implemented for batch sources specified by query" |
| 250 | + ) |
| 251 | + else: |
| 252 | + raise NotImplementedError( |
| 253 | + "offline_write_batch not implemented for batch sources specified by a table" |
| 254 | + ) |
| 255 | + |
194 | 256 | @staticmethod
|
195 | 257 | @log_exceptions_and_usage(offline_store="spark")
|
196 | 258 | def pull_all_from_table_or_query(
|
@@ -388,6 +450,24 @@ def _format_datetime(t: datetime) -> str:
|
388 | 450 | return dt
|
389 | 451 |
|
390 | 452 |
|
| 453 | +def _cast_data_frame( |
| 454 | + df_new: pyspark.sql.DataFrame, df_existing: pyspark.sql.DataFrame |
| 455 | +) -> pyspark.sql.DataFrame: |
| 456 | + """Convert new dataframe's columns to the same types as existing dataframe while preserving the order of columns""" |
| 457 | + existing_dtypes = {k: v for k, v in df_existing.dtypes} |
| 458 | + new_dtypes = {k: v for k, v in df_new.dtypes} |
| 459 | + |
| 460 | + select_expression = [] |
| 461 | + for col, new_type in new_dtypes.items(): |
| 462 | + existing_type = existing_dtypes[col] |
| 463 | + if new_type != existing_type: |
| 464 | + select_expression.append(f"cast({col} as {existing_type}) as {col}") |
| 465 | + else: |
| 466 | + select_expression.append(col) |
| 467 | + |
| 468 | + return df_new.selectExpr(*select_expression) |
| 469 | + |
| 470 | + |
391 | 471 | MULTIPLE_FEATURE_VIEW_POINT_IN_TIME_JOIN = """
|
392 | 472 | /*
|
393 | 473 | Compute a deterministic hash for the `left_table_query_string` that will be used throughout
|
|
0 commit comments