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


##########
hudi-flink-datasource/hudi-flink/src/main/java/org/apache/hudi/table/format/SchemaEvolutionContext.java:
##########
@@ -0,0 +1,114 @@
+/*
+ * 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.hudi.common.fs.FSUtils;
+import org.apache.hudi.common.table.HoodieTableMetaClient;
+import org.apache.hudi.common.table.TableSchemaResolver;
+import org.apache.hudi.common.util.InternalSchemaCache;
+import org.apache.hudi.common.util.Option;
+import org.apache.hudi.configuration.FlinkOptions;
+import org.apache.hudi.internal.schema.InternalSchema;
+import org.apache.hudi.internal.schema.Types;
+import org.apache.hudi.internal.schema.action.InternalSchemaMerger;
+import org.apache.hudi.internal.schema.convert.AvroInternalSchemaConverter;
+import org.apache.hudi.table.format.mor.MergeOnReadInputSplit;
+import org.apache.hudi.util.AvroSchemaConverter;
+import org.apache.hudi.util.StreamerUtil;
+
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.core.fs.FileInputSplit;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.logical.LogicalType;
+
+import java.io.Serializable;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * This class is responsible for calculating names and types of fields that 
are actual at a certain point in time.
+ * If field is renamed in queried schema, its old name will be returned, which 
is relevant at the provided time.
+ * If type of field is changed, its old type will be returned, and projection 
will be created that will convert the old type to the queried one.
+ */
+public final class SchemaEvolutionContext implements Serializable {
+  private static final long serialVersionUID = 1L;
+
+  private final HoodieTableMetaClient metaClient;
+  private final InternalSchema querySchema;
+
+  public static Option<SchemaEvolutionContext> of(Configuration conf) {
+    if (conf.getBoolean(FlinkOptions.SCHEMA_EVOLUTION_ENABLED)) {
+      HoodieTableMetaClient metaClient = StreamerUtil.createMetaClient(conf);
+      return new TableSchemaResolver(metaClient)
+          .getTableInternalSchemaFromCommitMetadata()
+          .map(schema -> new SchemaEvolutionContext(metaClient, schema));
+    } else {
+      return Option.empty();
+    }
+  }
+
+  public SchemaEvolutionContext(HoodieTableMetaClient metaClient, 
InternalSchema querySchema) {
+    this.metaClient = metaClient;
+    this.querySchema = querySchema;
+  }
+
+  public InternalSchema getQuerySchema() {
+    return querySchema;
+  }
+
+  public InternalSchema getActualSchema(FileInputSplit fileSplit) {
+    return 
getActualSchema(FSUtils.getCommitTime(fileSplit.getPath().getName()));
+  }
+
+  public InternalSchema getActualSchema(MergeOnReadInputSplit split) {
+    String commitTime = split.getBasePath()
+        .map(FSUtils::getCommitTime)

Review Comment:
   Good catch, thanks



##########
hudi-flink-datasource/hudi-flink/src/test/java/org/apache/hudi/sink/ITTestSchemaEvolution.java:
##########
@@ -0,0 +1,329 @@
+/*
+ * 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.sink;
+
+import org.apache.hudi.client.HoodieFlinkWriteClient;
+import org.apache.hudi.common.table.HoodieTableConfig;
+import org.apache.hudi.common.util.Option;
+import org.apache.hudi.config.HoodieWriteConfig;
+import org.apache.hudi.configuration.FlinkOptions;
+import org.apache.hudi.internal.schema.Types;
+import org.apache.hudi.keygen.ComplexAvroKeyGenerator;
+import org.apache.hudi.keygen.constant.KeyGeneratorOptions;
+import org.apache.hudi.table.HoodieTableFactory;
+import org.apache.hudi.util.AvroSchemaConverter;
+import org.apache.hudi.util.StreamerUtil;
+
+import org.apache.avro.Schema;
+import org.apache.avro.SchemaBuilder;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
+import org.apache.flink.table.api.TableResult;
+import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
+import org.apache.flink.table.factories.FactoryUtil;
+import org.apache.flink.test.util.AbstractTestBase;
+import org.apache.flink.types.Row;
+import org.apache.flink.util.CloseableIterator;
+import org.apache.flink.util.Preconditions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import java.io.File;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import static 
org.apache.hudi.internal.schema.action.TableChange.ColumnPositionChange.ColumnPositionType.AFTER;
+import static org.apache.hudi.utils.TestConfigurations.ROW_TYPE;
+import static org.apache.hudi.utils.TestConfigurations.ROW_TYPE_EVOLUTION;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class ITTestSchemaEvolution extends AbstractTestBase {
+  @TempDir File tempFile;
+  StreamExecutionEnvironment env;
+  StreamTableEnvironment tEnv;
+
+  String[] expectedMergedResult = new String[] {
+      "+I[Danny, 10000.1, 23]",
+      "+I[Stephen, null, 33]",
+      "+I[Julian, 30000.3, 53]",
+      "+I[Fabian, null, 31]",
+      "+I[Sophia, null, 18]",
+      "+I[Emma, null, 20]",
+      "+I[Bob, null, 44]",
+      "+I[Han, null, 56]",
+      "+I[Alice, 90000.9, unknown]"
+  };
+
+  String[] expectedUnMergedResult = new String[] {
+      "+I[Danny, null, 23]",
+      "+I[Stephen, null, 33]",
+      "+I[Julian, null, 53]",
+      "+I[Fabian, null, 31]",
+      "+I[Sophia, null, 18]",
+      "+I[Emma, null, 20]",
+      "+I[Bob, null, 44]",
+      "+I[Han, null, 56]",
+      "+I[Alice, 90000.9, unknown]",
+      "+I[Danny, 10000.1, 23]",
+      "+I[Julian, 30000.3, 53]"
+  };
+
+  @BeforeEach
+  public void setUp() {
+    env = StreamExecutionEnvironment.getExecutionEnvironment();
+    env.setParallelism(1);
+    tEnv = StreamTableEnvironment.create(env);
+  }
+
+  @Test
+  public void testCopyOnWriteInputFormat() throws Exception {
+    testRead(defaultOptionMap(tempFile.getAbsolutePath()));
+  }
+
+  @Test
+  public void testMergeOnReadInputFormatBaseFileOnlyIterator() throws 
Exception {
+    OptionMap optionMap = defaultOptionMap(tempFile.getAbsolutePath());
+    optionMap.put(FlinkOptions.READ_AS_STREAMING.key(), true);
+    optionMap.put(FlinkOptions.READ_START_COMMIT.key(), 
FlinkOptions.START_COMMIT_EARLIEST);
+    testRead(optionMap);
+  }
+
+  @Test
+  public void testMergeOnReadInputFormatBaseFileOnlyFilteringIterator() throws 
Exception {
+    OptionMap optionMap = defaultOptionMap(tempFile.getAbsolutePath());
+    optionMap.put(FlinkOptions.READ_AS_STREAMING.key(), true);
+    optionMap.put(FlinkOptions.READ_START_COMMIT.key(), 1);
+    testRead(optionMap);
+  }
+
+  @Test
+  public void 
testMergeOnReadInputFormatLogFileOnlyIteratorGetLogFileIterator() throws 
Exception {
+    OptionMap optionMap = defaultOptionMap(tempFile.getAbsolutePath());
+    optionMap.put(FlinkOptions.TABLE_TYPE.key(), 
FlinkOptions.TABLE_TYPE_MERGE_ON_READ);
+    testRead(optionMap);
+  }
+
+  @Test
+  public void 
testMergeOnReadInputFormatLogFileOnlyIteratorGetUnMergedLogFileIterator() 
throws Exception {
+    OptionMap optionMap = defaultOptionMap(tempFile.getAbsolutePath());
+    optionMap.put(FlinkOptions.TABLE_TYPE.key(), 
FlinkOptions.TABLE_TYPE_MERGE_ON_READ);
+    optionMap.put(FlinkOptions.READ_AS_STREAMING.key(), true);
+    optionMap.put(FlinkOptions.READ_START_COMMIT.key(), 
FlinkOptions.START_COMMIT_EARLIEST);
+    optionMap.put(FlinkOptions.CHANGELOG_ENABLED.key(), true);
+    testRead(optionMap, expectedUnMergedResult);
+  }
+
+  @Test
+  public void testMergeOnReadInputFormatMergeIterator() throws Exception {
+    OptionMap optionMap = defaultOptionMap(tempFile.getAbsolutePath());
+    optionMap.put(FlinkOptions.TABLE_TYPE.key(), 
FlinkOptions.TABLE_TYPE_MERGE_ON_READ);
+    optionMap.put(FlinkOptions.COMPACTION_DELTA_COMMITS.key(), 1);
+    testRead(optionMap, true);
+  }
+
+  @Test
+  public void testMergeOnReadInputFormatSkipMergeIterator() throws Exception {
+    OptionMap optionMap = defaultOptionMap(tempFile.getAbsolutePath());
+    optionMap.put(FlinkOptions.TABLE_TYPE.key(), 
FlinkOptions.TABLE_TYPE_MERGE_ON_READ);
+    optionMap.put(FlinkOptions.COMPACTION_DELTA_COMMITS.key(), 1);
+    optionMap.put(FlinkOptions.MERGE_TYPE.key(), 
FlinkOptions.REALTIME_SKIP_MERGE);
+    testRead(optionMap, true, expectedUnMergedResult);
+  }
+
+  @SuppressWarnings({"SqlDialectInspection", "SqlNoDataSourceInspection"})
+  @Test
+  public void testCompaction() throws Exception {
+    OptionMap optionMap = defaultOptionMap(tempFile.getAbsolutePath());
+    optionMap.put(FlinkOptions.TABLE_TYPE.key(), 
FlinkOptions.TABLE_TYPE_MERGE_ON_READ);
+    optionMap.put(FlinkOptions.COMPACTION_DELTA_COMMITS.key(), 1);
+    testRead(optionMap, new String[0]);

Review Comment:
   fixed



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