|
| 1 | +from pyspark.sql.types import StructType, ShortType, IntegerType, LongType, FloatType, DoubleType, \ |
| 2 | + BooleanType, StringType, StructField, ByteType, BinaryType, DecimalType |
| 3 | +from tests.integration.feature_store.test_base import FeatureStoreTestCase |
| 4 | +from ads.feature_store.input_feature_detail import FeatureDetail, FeatureType |
| 5 | +from ads.feature_store.feature_group import FeatureGroup |
| 6 | +from ads.feature_store.feature_group_job import FeatureGroupJob |
| 7 | +import pandas as pd |
| 8 | +import numpy as np |
| 9 | +import pytest |
| 10 | +from ads.feature_store.common.spark_session_singleton import SparkSessionSingleton |
| 11 | + |
| 12 | + |
| 13 | +class TestInputSchema(FeatureStoreTestCase): |
| 14 | + input_feature_details = [ |
| 15 | + FeatureDetail("A").with_feature_type(FeatureType.STRING).with_order_number(1), |
| 16 | + FeatureDetail("B").with_feature_type(FeatureType.INTEGER).with_order_number(2) |
| 17 | + ] |
| 18 | + |
| 19 | + a = ["value1", "value2"] |
| 20 | + b = [25, 60] |
| 21 | + c = [30, 50] |
| 22 | + pandas_basic_df = pd.DataFrame( |
| 23 | + { |
| 24 | + "A": a, |
| 25 | + "B": b, |
| 26 | + "C": c |
| 27 | + } |
| 28 | + ) |
| 29 | + |
| 30 | + schema = StructType( |
| 31 | + [StructField("string_col", StringType(), True), |
| 32 | + StructField("int_col", IntegerType(), True), |
| 33 | + StructField("long_col", LongType(), True)] |
| 34 | + ) |
| 35 | + |
| 36 | + input_feature_details_spark = [ |
| 37 | + FeatureDetail("string_col").with_feature_type(FeatureType.STRING).with_order_number(1), |
| 38 | + FeatureDetail("int_col").with_feature_type(FeatureType.INTEGER).with_order_number(2), |
| 39 | + FeatureDetail("C").with_feature_type(FeatureType.INTEGER).with_order_number(2), |
| 40 | + FeatureDetail("B").with_feature_type(FeatureType.INTEGER).with_order_number(2), |
| 41 | + ] |
| 42 | + |
| 43 | + data = [ |
| 44 | + ("value1", 100, 1000), |
| 45 | + ("value2", 200, 2000) |
| 46 | + ] |
| 47 | + spark = SparkSessionSingleton(FeatureStoreTestCase.METASTORE_ID).get_spark_session() |
| 48 | + basic_df = spark.createDataFrame(data, schema) |
| 49 | + |
| 50 | + def define_feature_group_resource_with_pandas_schema( |
| 51 | + self, entity_id, feature_store_id |
| 52 | + ) -> "FeatureGroup": |
| 53 | + feature_group_pandas_array = ( |
| 54 | + FeatureGroup() |
| 55 | + .with_description("feature group resource for pandas array types") |
| 56 | + .with_compartment_id(self.COMPARTMENT_ID) |
| 57 | + .with_name(self.get_name("feature_group_pandas_array")) |
| 58 | + .with_entity_id(entity_id) |
| 59 | + .with_feature_store_id(feature_store_id) |
| 60 | + .with_primary_keys([]) |
| 61 | + .with_input_feature_details(self.input_feature_details) |
| 62 | + ) |
| 63 | + return feature_group_pandas_array |
| 64 | + |
| 65 | + def define_feature_group_resource_with_spark_schema( |
| 66 | + self, entity_id, feature_store_id |
| 67 | + ) -> "FeatureGroup": |
| 68 | + feature_group_spark_schema = ( |
| 69 | + FeatureGroup() |
| 70 | + .with_description("feature group resource for pandas array types") |
| 71 | + .with_compartment_id(self.COMPARTMENT_ID) |
| 72 | + .with_name(self.get_name("feature_group_spark_schema")) |
| 73 | + .with_entity_id(entity_id) |
| 74 | + .with_feature_store_id(feature_store_id) |
| 75 | + .with_primary_keys([]) |
| 76 | + .with_input_feature_details(self.input_feature_details_spark) |
| 77 | + ) |
| 78 | + return feature_group_spark_schema |
| 79 | + |
| 80 | + def test_feature_group_pandas_schema_mismatch(self): |
| 81 | + """Tests pandas schema""" |
| 82 | + fs = self.define_feature_store_resource().create() |
| 83 | + assert fs.oci_fs.id |
| 84 | + |
| 85 | + entity = self.create_entity_resource(fs) |
| 86 | + assert entity.oci_fs_entity.id |
| 87 | + |
| 88 | + feature_group = self.define_feature_group_resource_with_pandas_schema( |
| 89 | + entity.oci_fs_entity.id, fs.oci_fs.id |
| 90 | + ) |
| 91 | + feature_group.create() |
| 92 | + feature_group.materialise(self.pandas_basic_df) |
| 93 | + |
| 94 | + |
| 95 | + df = feature_group.select().read() |
| 96 | + assert len(df.columns) == 2 |
| 97 | + |
| 98 | + self.clean_up_feature_group(feature_group) |
| 99 | + self.clean_up_entity(entity) |
| 100 | + self.clean_up_feature_store(fs) |
| 101 | + |
| 102 | + def test_feature_group_spark_schema_mismatch(self): |
| 103 | + """Tests pandas date time data types""" |
| 104 | + fs = self.define_feature_store_resource().create() |
| 105 | + assert fs.oci_fs.id |
| 106 | + |
| 107 | + entity = self.create_entity_resource(fs) |
| 108 | + assert entity.oci_fs_entity.id |
| 109 | + |
| 110 | + feature_group = self.define_feature_group_resource_with_spark_schema( |
| 111 | + entity.oci_fs_entity.id, fs.oci_fs.id |
| 112 | + ) |
| 113 | + feature_group.create() |
| 114 | + feature_group.materialise(self.basic_df) |
| 115 | + |
| 116 | + df = feature_group.select().read() |
| 117 | + assert len(df.columns) == 2 |
| 118 | + |
| 119 | + self.clean_up_feature_group(feature_group) |
| 120 | + self.clean_up_entity(entity) |
| 121 | + self.clean_up_feature_store(fs) |
| 122 | + |
| 123 | + |
0 commit comments