the-other-tim-brown commented on code in PR #14355: URL: https://github.com/apache/hudi/pull/14355#discussion_r2593238602
########## hudi-client/hudi-flink-client/src/main/java/org/apache/hudi/util/HoodieSchemaConverter.java: ########## @@ -0,0 +1,461 @@ +/* + * 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.util; + +import org.apache.hudi.common.schema.HoodieSchema; +import org.apache.hudi.common.schema.HoodieSchemaField; +import org.apache.hudi.common.schema.HoodieSchemaType; +import org.apache.hudi.common.util.ReflectionUtils; + +import org.apache.flink.api.common.typeinfo.TypeInformation; +import org.apache.flink.api.common.typeinfo.Types; +import org.apache.flink.table.api.DataTypes; +import org.apache.flink.table.types.DataType; +import org.apache.flink.table.types.logical.ArrayType; +import org.apache.flink.table.types.logical.DecimalType; +import org.apache.flink.table.types.logical.IntType; +import org.apache.flink.table.types.logical.LocalZonedTimestampType; +import org.apache.flink.table.types.logical.LogicalType; +import org.apache.flink.table.types.logical.LogicalTypeFamily; +import org.apache.flink.table.types.logical.MapType; +import org.apache.flink.table.types.logical.MultisetType; +import org.apache.flink.table.types.logical.RowType; +import org.apache.flink.table.types.logical.TimeType; +import org.apache.flink.table.types.logical.TimestampType; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +/** + * Converts Flink's LogicalType into HoodieSchema. + */ +public class HoodieSchemaConverter { + + /** + * Converts a Flink LogicalType into a HoodieSchema. + * + * <p>Uses "record" as the default type name for record types. + * + * @param logicalType Flink logical type definition + * @return HoodieSchema matching the logical type + */ + public static HoodieSchema convertToSchema(LogicalType logicalType) { + return convertToSchema(logicalType, "record"); + } + + /** + * Converts a Flink LogicalType into a HoodieSchema with specified record name. + * + * <p>The "{rowName}." is used as the nested row type name prefix in order to generate + * the right schema. Nested record types that only differ by type name are still compatible. + * + * @param logicalType Flink logical type + * @param rowName the record name + * @return HoodieSchema matching this logical type + */ + public static HoodieSchema convertToSchema(LogicalType logicalType, String rowName) { + int precision; + boolean nullable = logicalType.isNullable(); + + switch (logicalType.getTypeRoot()) { + case NULL: + return HoodieSchema.create(HoodieSchemaType.NULL); + + case BOOLEAN: + HoodieSchema bool = HoodieSchema.create(HoodieSchemaType.BOOLEAN); + return nullable ? HoodieSchema.createNullable(bool) : bool; Review Comment: To avoid possibility of missing null checks in one of the switch branches, you can instead have the switch assign the type and then finally do a single `nullable ? HoodieSchema.createNullable(type) : type)` -- 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]
