trushev commented on code in PR #5443:
URL: https://github.com/apache/hudi/pull/5443#discussion_r874315880


##########
hudi-flink-datasource/hudi-flink/src/main/java/org/apache/hudi/table/format/CastMap.java:
##########
@@ -0,0 +1,246 @@
+/*
+ * 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.table.format;
+
+import org.apache.avro.Schema;
+import org.apache.flink.annotation.VisibleForTesting;
+import org.apache.flink.table.data.DecimalData;
+import org.apache.flink.table.data.binary.BinaryStringData;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.logical.DecimalType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.LogicalTypeRoot;
+import org.apache.flink.util.Preconditions;
+import org.apache.hudi.internal.schema.InternalSchema;
+import org.apache.hudi.internal.schema.Type;
+import org.apache.hudi.internal.schema.Types;
+import org.apache.hudi.internal.schema.convert.AvroInternalSchemaConverter;
+import org.apache.hudi.internal.schema.utils.InternalSchemaUtils;
+import org.apache.hudi.util.AvroSchemaConverter;
+
+import java.math.BigDecimal;
+import java.time.LocalDate;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+import static org.apache.flink.table.types.logical.LogicalTypeRoot.BIGINT;
+import static org.apache.flink.table.types.logical.LogicalTypeRoot.DATE;
+import static org.apache.flink.table.types.logical.LogicalTypeRoot.DECIMAL;
+import static org.apache.flink.table.types.logical.LogicalTypeRoot.DOUBLE;
+import static org.apache.flink.table.types.logical.LogicalTypeRoot.FLOAT;
+import static org.apache.flink.table.types.logical.LogicalTypeRoot.INTEGER;
+import static org.apache.flink.table.types.logical.LogicalTypeRoot.VARCHAR;
+import static org.apache.hudi.common.model.HoodieRecord.HOODIE_META_COLUMNS;
+
+/**
+ * CastMap is responsible for type conversion when full schema evolution 
enabled.
+ */
+public final class CastMap {
+  // Maps position (column number) to corresponding cast
+  private final Map<Integer, Cast> castMap = new HashMap<>();
+
+  /**
+   * Creates CastMap by comparing two schemes. Cast of a specific column is 
created if its type has changed.
+   */
+  public static CastMap of(String tableName, InternalSchema querySchema, 
InternalSchema actualSchema) {
+    DataType queryType = internalSchemaToDataType(tableName, querySchema);
+    DataType actualType = internalSchemaToDataType(tableName, actualSchema);
+    int metaColumnsSize = HOODIE_META_COLUMNS.size();
+    CastMap castMap = new CastMap();
+    InternalSchemaUtils.collectTypeChangedCols(querySchema, 
actualSchema).entrySet()
+            .stream()
+            .filter(e -> e.getKey() >= metaColumnsSize)
+            .filter(e -> !isSameType(e.getValue().getLeft(), 
e.getValue().getRight()))
+            .forEach(e -> {
+              int pos = e.getKey();
+              LogicalType target = 
queryType.getChildren().get(pos).getLogicalType();
+              LogicalType actual = 
actualType.getChildren().get(pos).getLogicalType();
+              castMap.add(pos - metaColumnsSize, actual, target);
+            });
+    return castMap;
+  }
+
+  public Object castIfNeed(int pos, Object val) {
+    Cast cast = castMap.get(pos);
+    if (cast == null) {
+      return val;
+    }
+    return cast(val, cast.from(), cast.to());
+  }
+
+  private Object cast(Object val, LogicalType fromType, LogicalType toType) {
+    LogicalTypeRoot from = fromType.getTypeRoot();
+    LogicalTypeRoot to = toType.getTypeRoot();
+    switch (to) {
+      case BIGINT: {
+        // Integer => Long

Review Comment:
   Assume schema evolution DDL
   ```sql
   alter table t1 alter column val type bigint
   ```
   which changes type of val from int to bigint
   
   We want to be able to read old data. To do it we need to cast val from int 
to long
   
   otherwise, an exception will be thrown
   ```java
   java.lang.ClassCastException: java.lang.Integer cannot be cast to 
java.lang.Long
        at 
org.apache.flink.table.data.GenericRowData.getLong(GenericRowData.java:154)
   ```
   
   This class is an analogue of 
`org.apache.hudi.client.utils.SparkInternalSchemaConverter#convertColumnVectorType`
 which converts Spark's types



-- 
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]

Reply via email to