|
1 | | -from typing import Any |
| 1 | +from typing import Any, Union, Dict, Optional, cast |
| 2 | + |
| 3 | +import pandas as pd |
| 4 | +import pyspark.sql |
2 | 5 |
|
3 | 6 | from feast.transformation.base import Transformation |
| 7 | +from feast.transformation.mode import TransformationMode |
| 8 | +from feast.infra.compute_engines.spark.utils import get_or_create_new_spark_session |
4 | 9 |
|
5 | 10 |
|
6 | 11 | class SparkTransformation(Transformation): |
7 | | - def transform(self, inputs: Any) -> Any: |
8 | | - pass |
9 | 12 |
|
10 | | - def infer_features(self, *args, **kwargs) -> Any: |
| 13 | + def __new__(cls, |
| 14 | + mode: Union[TransformationMode, str], |
| 15 | + udf: Any, |
| 16 | + udf_string: str, |
| 17 | + spark_config: Dict[str, Any] = {}, |
| 18 | + name: Optional[str] = None, |
| 19 | + tags: Optional[Dict[str, str]] = None, |
| 20 | + description: str = "", |
| 21 | + owner: str = "", |
| 22 | + *args, |
| 23 | + **kwargs) -> "SparkTransformation": |
| 24 | + instance = super(SparkTransformation, cls).__new__( |
| 25 | + cls, |
| 26 | + mode=mode, |
| 27 | + spark_config=spark_config, |
| 28 | + udf=udf, |
| 29 | + udf_string=udf_string, |
| 30 | + name=name, |
| 31 | + tags=tags, |
| 32 | + description=description, |
| 33 | + owner=owner, |
| 34 | + ) |
| 35 | + return cast(SparkTransformation, instance) |
| 36 | + |
| 37 | + def __init__(self, |
| 38 | + mode: Union[TransformationMode, str], |
| 39 | + udf: Any, |
| 40 | + udf_string: str, |
| 41 | + spark_config: Dict[str, Any] = {}, |
| 42 | + name: Optional[str] = None, |
| 43 | + tags: Optional[Dict[str, str]] = None, |
| 44 | + description: str = "", |
| 45 | + owner: str = "", |
| 46 | + *args, |
| 47 | + **kwargs): |
| 48 | + super().__init__( |
| 49 | + mode=mode, |
| 50 | + udf=udf, |
| 51 | + name=name, |
| 52 | + udf_string=udf_string, |
| 53 | + tags=tags, |
| 54 | + description=description, |
| 55 | + owner=owner, |
| 56 | + ) |
| 57 | + self.spark_session = get_or_create_new_spark_session(spark_config) |
| 58 | + |
| 59 | + def transform(self, |
| 60 | + *inputs: Union[str, pd.DataFrame], |
| 61 | + ) -> pd.DataFrame: |
| 62 | + if self.mode == TransformationMode.SPARK_SQL: |
| 63 | + return self._transform_spark_sql(*inputs) |
| 64 | + else: |
| 65 | + return self._transform_spark_udf(*inputs) |
| 66 | + |
| 67 | + @staticmethod |
| 68 | + def _create_temp_view_for_dataframe(df: pyspark.sql.DataFrame, |
| 69 | + name: str): |
| 70 | + df_temp_view = f"feast_transformation_temp_view_{name}" |
| 71 | + df.createOrReplaceTempView(df_temp_view) |
| 72 | + return df_temp_view |
| 73 | + |
| 74 | + def _transform_spark_sql(self, |
| 75 | + *inputs: Union[pyspark.sql.DataFrame, str] |
| 76 | + ) -> pd.DataFrame: |
| 77 | + inputs_str = [ |
| 78 | + self._create_temp_view_for_dataframe(v, f"index_{i}") |
| 79 | + if isinstance(v, pyspark.sql.DataFrame) else v |
| 80 | + for i, v in enumerate(inputs) |
| 81 | + ] |
| 82 | + return self.spark_session.sql(self.udf(*inputs_str)) |
| 83 | + |
| 84 | + def _transform_spark_udf(self, |
| 85 | + *inputs: Any) -> pd.DataFrame: |
| 86 | + return self.udf(*inputs) |
| 87 | + |
| 88 | + def infer_features(self, |
| 89 | + *args, |
| 90 | + **kwargs) -> Any: |
11 | 91 | pass |
0 commit comments