|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright 2018-2020 The Feast Authors |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package feast.ingestion.validation |
| 18 | + |
| 19 | +import feast.ingestion.FeatureTable |
| 20 | +import feast.proto.types.ValueProto.ValueType |
| 21 | +import org.apache.spark.sql.types._ |
| 22 | + |
| 23 | +object TypeCheck { |
| 24 | + def typesMatch(defType: ValueType.Enum, columnType: DataType): Boolean = |
| 25 | + (defType, columnType) match { |
| 26 | + case (ValueType.Enum.BOOL, BooleanType) => true |
| 27 | + case (ValueType.Enum.INT32, IntegerType) => true |
| 28 | + case (ValueType.Enum.INT64, LongType) => true |
| 29 | + case (ValueType.Enum.FLOAT, FloatType) => true |
| 30 | + case (ValueType.Enum.DOUBLE, DoubleType) => true |
| 31 | + case (ValueType.Enum.STRING, StringType) => true |
| 32 | + case (ValueType.Enum.BYTES, BinaryType) => true |
| 33 | + case (ValueType.Enum.BOOL_LIST, ArrayType(_: BooleanType, _)) => true |
| 34 | + case (ValueType.Enum.INT32_LIST, ArrayType(_: IntegerType, _)) => true |
| 35 | + case (ValueType.Enum.INT64_LIST, ArrayType(_: LongType, _)) => true |
| 36 | + case (ValueType.Enum.FLOAT_LIST, ArrayType(_: FloatType, _)) => true |
| 37 | + case (ValueType.Enum.DOUBLE_LIST, ArrayType(_: DoubleType, _)) => true |
| 38 | + case (ValueType.Enum.STRING_LIST, ArrayType(_: StringType, _)) => true |
| 39 | + case (ValueType.Enum.BYTES_LIST, ArrayType(_: BinaryType, _)) => true |
| 40 | + case _ => false |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Verify whether types declared in FeatureTable match correspondent columns |
| 45 | + * @param schema Spark's dataframe columns |
| 46 | + * @param featureTable definition of expected schema |
| 47 | + * @return true if all match |
| 48 | + */ |
| 49 | + def allTypesMatch(schema: StructType, featureTable: FeatureTable): Either[String, Boolean] = { |
| 50 | + val typeByField = |
| 51 | + (featureTable.entities ++ featureTable.features).map(f => f.name -> f.`type`).toMap |
| 52 | + |
| 53 | + schema.fields |
| 54 | + .map(f => |
| 55 | + if (typeByField.contains(f.name) && !typesMatch(typeByField(f.name), f.dataType)) { |
| 56 | + Left( |
| 57 | + s"Feature ${f.name} has different type ${f.dataType} instead of expected ${typeByField(f.name)}" |
| 58 | + ) |
| 59 | + } else Right(true) |
| 60 | + ) |
| 61 | + .foldLeft[Either[String, Boolean]](Right(true)) { |
| 62 | + case (Left(error), _) => Left(error) |
| 63 | + case (Right(_), Left(error)) => Left(error) |
| 64 | + case _ => Right(true) |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments