|
1 | | -from typing import Any |
| 1 | +from typing import Any, Dict, Optional, Union, cast |
2 | 2 |
|
| 3 | +import pandas as pd |
| 4 | +import pyspark.sql |
| 5 | + |
| 6 | +from feast.infra.compute_engines.spark.utils import get_or_create_new_spark_session |
3 | 7 | from feast.transformation.base import Transformation |
| 8 | +from feast.transformation.mode import TransformationMode |
4 | 9 |
|
5 | 10 |
|
6 | 11 | class SparkTransformation(Transformation): |
7 | | - def transform(self, inputs: Any) -> Any: |
8 | | - pass |
| 12 | + r""" |
| 13 | + SparkTransformation can be used to define a transformation using a Spark UDF or SQL query. |
| 14 | + The current spark session will be used or a new one will be created if not available. |
| 15 | + E.g.: |
| 16 | + spark_transformation = SparkTransformation( |
| 17 | + mode=TransformationMode.SPARK, |
| 18 | + udf=remove_extra_spaces, |
| 19 | + udf_string="remove extra spaces", |
| 20 | + ) |
| 21 | + OR |
| 22 | + spark_transformation = Transformation( |
| 23 | + mode=TransformationMode.SPARK_SQL, |
| 24 | + udf=remove_extra_spaces_sql, |
| 25 | + udf_string="remove extra spaces sql", |
| 26 | + ) |
| 27 | + OR |
| 28 | + @transformation(mode=TransformationMode.SPARK) |
| 29 | + def remove_extra_spaces_udf(df: pd.DataFrame) -> pd.DataFrame: |
| 30 | + return df.assign(name=df['name'].str.replace('\s+', ' ')) |
| 31 | + """ |
| 32 | + |
| 33 | + def __new__( |
| 34 | + cls, |
| 35 | + mode: Union[TransformationMode, str], |
| 36 | + udf: Any, |
| 37 | + udf_string: str, |
| 38 | + spark_config: Dict[str, Any] = {}, |
| 39 | + name: Optional[str] = None, |
| 40 | + tags: Optional[Dict[str, str]] = None, |
| 41 | + description: str = "", |
| 42 | + owner: str = "", |
| 43 | + *args, |
| 44 | + **kwargs, |
| 45 | + ) -> "SparkTransformation": |
| 46 | + """ |
| 47 | + Creates a SparkTransformation |
| 48 | + Args: |
| 49 | + mode: (required) The mode of the transformation. Choose one from TransformationMode.SPARK or TransformationMode.SPARK_SQL. |
| 50 | + udf: (required) The user-defined transformation function. |
| 51 | + udf_string: (required) The string representation of the udf. The dill get source doesn't |
| 52 | + spark_config: (optional) The spark configuration to use for the transformation. |
| 53 | + name: (optional) The name of the transformation. |
| 54 | + tags: (optional) Metadata tags for the transformation. |
| 55 | + description: (optional) A description of the transformation. |
| 56 | + owner: (optional) The owner of the transformation. |
| 57 | + """ |
| 58 | + instance = super(SparkTransformation, cls).__new__( |
| 59 | + cls, |
| 60 | + mode=mode, |
| 61 | + spark_config=spark_config, |
| 62 | + udf=udf, |
| 63 | + udf_string=udf_string, |
| 64 | + name=name, |
| 65 | + tags=tags, |
| 66 | + description=description, |
| 67 | + owner=owner, |
| 68 | + ) |
| 69 | + return cast(SparkTransformation, instance) |
| 70 | + |
| 71 | + def __init__( |
| 72 | + self, |
| 73 | + mode: Union[TransformationMode, str], |
| 74 | + udf: Any, |
| 75 | + udf_string: str, |
| 76 | + spark_config: Dict[str, Any] = {}, |
| 77 | + name: Optional[str] = None, |
| 78 | + tags: Optional[Dict[str, str]] = None, |
| 79 | + description: str = "", |
| 80 | + owner: str = "", |
| 81 | + *args, |
| 82 | + **kwargs, |
| 83 | + ): |
| 84 | + super().__init__( |
| 85 | + mode=mode, |
| 86 | + udf=udf, |
| 87 | + name=name, |
| 88 | + udf_string=udf_string, |
| 89 | + tags=tags, |
| 90 | + description=description, |
| 91 | + owner=owner, |
| 92 | + ) |
| 93 | + self.spark_session = get_or_create_new_spark_session(spark_config) |
| 94 | + |
| 95 | + def transform( |
| 96 | + self, |
| 97 | + *inputs: Union[str, pd.DataFrame], |
| 98 | + ) -> pd.DataFrame: |
| 99 | + if self.mode == TransformationMode.SPARK_SQL: |
| 100 | + return self._transform_spark_sql(*inputs) |
| 101 | + else: |
| 102 | + return self._transform_spark_udf(*inputs) |
| 103 | + |
| 104 | + @staticmethod |
| 105 | + def _create_temp_view_for_dataframe(df: pyspark.sql.DataFrame, name: str): |
| 106 | + df_temp_view = f"feast_transformation_temp_view_{name}" |
| 107 | + df.createOrReplaceTempView(df_temp_view) |
| 108 | + return df_temp_view |
| 109 | + |
| 110 | + def _transform_spark_sql( |
| 111 | + self, *inputs: Union[pyspark.sql.DataFrame, str] |
| 112 | + ) -> pd.DataFrame: |
| 113 | + inputs_str = [ |
| 114 | + self._create_temp_view_for_dataframe(v, f"index_{i}") |
| 115 | + if isinstance(v, pyspark.sql.DataFrame) |
| 116 | + else v |
| 117 | + for i, v in enumerate(inputs) |
| 118 | + ] |
| 119 | + return self.spark_session.sql(self.udf(*inputs_str)) |
| 120 | + |
| 121 | + def _transform_spark_udf(self, *inputs: Any) -> pd.DataFrame: |
| 122 | + return self.udf(*inputs) |
9 | 123 |
|
10 | 124 | def infer_features(self, *args, **kwargs) -> Any: |
11 | 125 | pass |
0 commit comments