rahil-c commented on code in PR #17475: URL: https://github.com/apache/hudi/pull/17475#discussion_r2600621872
########## hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/hudi/TestHoodieSchemaConversionUtils.scala: ########## @@ -0,0 +1,539 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hudi + +import org.apache.hudi.common.schema.{HoodieSchema, HoodieSchemaField, HoodieSchemaType} +import org.apache.hudi.internal.schema.HoodieSchemaException + +import org.apache.spark.sql.types._ +import org.scalatest.{FunSuite, Matchers} + +import scala.collection.JavaConverters._ + +class TestHoodieSchemaConversionUtils extends FunSuite with Matchers { + + test("test all primitive types conversion") { + val struct = new StructType() + .add("bool_field", BooleanType, false) + .add("byte_field", ByteType, false) + .add("short_field", ShortType, false) + .add("int_field", IntegerType, false) + .add("long_field", LongType, false) + .add("float_field", FloatType, false) + .add("double_field", DoubleType, false) + .add("string_field", StringType, false) + .add("binary_field", BinaryType, false) + + val hoodieSchema = HoodieSchemaConversionUtils.convertStructTypeToHoodieSchema( + struct, "PrimitiveTypes", "test") + + // Verify all primitive type conversions + assert(hoodieSchema.getField("bool_field").get().schema().getType == HoodieSchemaType.BOOLEAN) + assert(hoodieSchema.getField("byte_field").get().schema().getType == HoodieSchemaType.INT) + assert(hoodieSchema.getField("short_field").get().schema().getType == HoodieSchemaType.INT) + assert(hoodieSchema.getField("int_field").get().schema().getType == HoodieSchemaType.INT) + assert(hoodieSchema.getField("long_field").get().schema().getType == HoodieSchemaType.LONG) + assert(hoodieSchema.getField("float_field").get().schema().getType == HoodieSchemaType.FLOAT) + assert(hoodieSchema.getField("double_field").get().schema().getType == HoodieSchemaType.DOUBLE) + assert(hoodieSchema.getField("string_field").get().schema().getType == HoodieSchemaType.STRING) + assert(hoodieSchema.getField("binary_field").get().schema().getType == HoodieSchemaType.BYTES) + + // Verify roundtrip + val convertedStruct = HoodieSchemaConversionUtils.convertHoodieSchemaToStructType(hoodieSchema) + assert(convertedStruct.fields.length == 9) + assert(convertedStruct.fields(0).dataType == BooleanType) + assert(convertedStruct.fields(1).dataType == IntegerType) // Byte → Int + assert(convertedStruct.fields(2).dataType == IntegerType) // Short → Int + assert(convertedStruct.fields(3).dataType == IntegerType) + assert(convertedStruct.fields(4).dataType == LongType) + assert(convertedStruct.fields(5).dataType == FloatType) + assert(convertedStruct.fields(6).dataType == DoubleType) + assert(convertedStruct.fields(7).dataType == StringType) + assert(convertedStruct.fields(8).dataType == BinaryType) + } + + test("test HoodieSchema to Spark conversion for all primitive types") { + // Create HoodieSchema with all primitive types + val fields = java.util.Arrays.asList( + HoodieSchemaField.of("bool", HoodieSchema.create(HoodieSchemaType.BOOLEAN)), + HoodieSchemaField.of("int", HoodieSchema.create(HoodieSchemaType.INT)), + HoodieSchemaField.of("long", HoodieSchema.create(HoodieSchemaType.LONG)), + HoodieSchemaField.of("float", HoodieSchema.create(HoodieSchemaType.FLOAT)), + HoodieSchemaField.of("double", HoodieSchema.create(HoodieSchemaType.DOUBLE)), + HoodieSchemaField.of("string", HoodieSchema.create(HoodieSchemaType.STRING)), + HoodieSchemaField.of("bytes", HoodieSchema.create(HoodieSchemaType.BYTES)), + HoodieSchemaField.of("null", HoodieSchema.create(HoodieSchemaType.NULL)) + ) + val hoodieSchema = HoodieSchema.createRecord("AllPrimitives", "test", null, fields) + + val structType = HoodieSchemaConversionUtils.convertHoodieSchemaToStructType(hoodieSchema) + + assert(structType.fields.length == 8) + assert(structType.fields(0).dataType == BooleanType) + assert(structType.fields(1).dataType == IntegerType) + assert(structType.fields(2).dataType == LongType) + assert(structType.fields(3).dataType == FloatType) + assert(structType.fields(4).dataType == DoubleType) + assert(structType.fields(5).dataType == StringType) + assert(structType.fields(6).dataType == BinaryType) + assert(structType.fields(7).dataType == NullType) + assert(structType.fields(7).nullable) // Null type is always nullable + } + + test("test logical types conversion - date, timestamp, decimal") { + val struct = new StructType() + .add("date_field", DateType, false) + .add("timestamp_field", TimestampType, true) + .add("decimal_field", DecimalType(10, 2), false) + .add("decimal_field2", DecimalType(20, 5), true) + + val hoodieSchema = HoodieSchemaConversionUtils.convertStructTypeToHoodieSchema( + struct, "LogicalTypes", "test") + + // Verify DATE logical type + val dateField = hoodieSchema.getField("date_field").get() + assert(dateField.schema().getType == HoodieSchemaType.DATE) + assert(!dateField.isNullable()) + + // Verify TIMESTAMP logical type + val timestampField = hoodieSchema.getField("timestamp_field").get() + assert(timestampField.isNullable()) + val timestampSchema = timestampField.schema().getNonNullType() + assert(timestampSchema.getType == HoodieSchemaType.TIMESTAMP) + assert(timestampSchema.isInstanceOf[HoodieSchema.Timestamp]) + assert(timestampSchema.asInstanceOf[HoodieSchema.Timestamp].isUtcAdjusted) + + // Verify DECIMAL logical type + val decimalField = hoodieSchema.getField("decimal_field").get() + assert(decimalField.schema().getType == HoodieSchemaType.DECIMAL) + assert(decimalField.schema().isInstanceOf[HoodieSchema.Decimal]) + val decimalSchema = decimalField.schema().asInstanceOf[HoodieSchema.Decimal] + assert(decimalSchema.getPrecision == 10) + assert(decimalSchema.getScale == 2) + + // Verify roundtrip preserves logical types + val convertedStruct = HoodieSchemaConversionUtils.convertHoodieSchemaToStructType(hoodieSchema) + assert(convertedStruct.fields(0).dataType == DateType) + assert(convertedStruct.fields(1).dataType == TimestampType) + assert(convertedStruct.fields(2).dataType == DecimalType(10, 2)) + assert(convertedStruct.fields(3).dataType == DecimalType(20, 5)) + } + + test("test HoodieSchema to Spark conversion for logical types") { + val fields = java.util.Arrays.asList( Review Comment: @the-other-tim-brown When taking a look at the original avro converter I do not think there is handling for `TIME` type. I think maybe this is due to fact that Spark does not have a native Time type? * https://github.com/apache/hudi/blob/master/hudi-spark-datasource/hudi-spark-common/src/main/scala/org/apache/spark/sql/avro/SchemaConverters.scala#L81 * https://spark.apache.org/docs/latest/sql-ref-datatypes.html Similarly I did not see any handling for UUID either. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
