Davis-Zhang-Onehouse commented on code in PR #12781:
URL: https://github.com/apache/hudi/pull/12781#discussion_r1980442458


##########
hudi-client/hudi-client-common/src/main/java/org/apache/hudi/client/transaction/ConcurrentSchemaEvolutionTableSchemaGetter.java:
##########
@@ -0,0 +1,281 @@
+/*
+ * 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.client.transaction;
+
+import org.apache.hudi.avro.HoodieAvroUtils;
+import org.apache.hudi.common.HoodieSchemaNotFoundException;
+import org.apache.hudi.common.model.HoodieCommitMetadata;
+import org.apache.hudi.common.table.HoodieTableMetaClient;
+import org.apache.hudi.common.table.timeline.HoodieActiveTimeline;
+import org.apache.hudi.common.table.timeline.HoodieInstant;
+import org.apache.hudi.common.table.timeline.TimelineLayout;
+import org.apache.hudi.common.util.ClusteringUtils;
+import org.apache.hudi.common.util.Option;
+import org.apache.hudi.common.util.StringUtils;
+import org.apache.hudi.common.util.VisibleForTesting;
+import org.apache.hudi.common.util.collection.Pair;
+import org.apache.hudi.exception.HoodieException;
+import org.apache.hudi.internal.schema.HoodieSchemaException;
+import org.apache.hudi.util.Lazy;
+
+import org.apache.avro.JsonProperties;
+import org.apache.avro.Schema;
+import org.apache.avro.Schema.Field;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.function.Supplier;
+import java.util.stream.Stream;
+
+import static org.apache.hudi.avro.AvroSchemaUtils.appendFieldsToSchema;
+import static org.apache.hudi.avro.AvroSchemaUtils.containsFieldInSchema;
+import static org.apache.hudi.avro.AvroSchemaUtils.createNullableSchema;
+import static 
org.apache.hudi.common.table.timeline.HoodieTimeline.COMMIT_ACTION;
+import static 
org.apache.hudi.common.table.timeline.HoodieTimeline.DELTA_COMMIT_ACTION;
+import static 
org.apache.hudi.common.table.timeline.HoodieTimeline.REPLACE_COMMIT_ACTION;
+import static 
org.apache.hudi.common.table.timeline.InstantComparison.LESSER_THAN_OR_EQUALS;
+import static 
org.apache.hudi.common.table.timeline.InstantComparison.compareTimestamps;
+
+/**
+ * Helper class to read table schema. ONLY USE IT FOR 
SimpleConcurrentFileWritesConflictResolutionStrategy.
+ */
+class ConcurrentSchemaEvolutionTableSchemaGetter {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(ConcurrentSchemaEvolutionTableSchemaGetter.class);
+
+  protected final HoodieTableMetaClient metaClient;
+
+  private final Lazy<ConcurrentHashMap<HoodieInstant, Schema>> 
tableSchemaCache;
+
+  private Option<HoodieInstant> latestCommitWithValidSchema = Option.empty();
+
+  @VisibleForTesting
+  public ConcurrentHashMap<HoodieInstant, Schema> getTableSchemaCache() {
+    return tableSchemaCache.get();
+  }
+
+  public ConcurrentSchemaEvolutionTableSchemaGetter(HoodieTableMetaClient 
metaClient) {
+    this.metaClient = metaClient;
+    // Unbounded sized map. Should replace with some caching library.
+    this.tableSchemaCache = Lazy.lazily(ConcurrentHashMap::new);
+  }
+
+  /**
+   * Handles partition column logic for a given schema.
+   *
+   * @param schema the input schema to process
+   * @return the processed schema with partition columns handled appropriately
+   */
+  private Schema handlePartitionColumnsIfNeeded(Schema schema) {
+    if (metaClient.getTableConfig().shouldDropPartitionColumns()) {
+      return metaClient.getTableConfig().getPartitionFields()
+          .map(partitionFields -> appendPartitionColumns(schema, 
Option.ofNullable(partitionFields)))
+          .or(() -> Option.of(schema))
+          .get();
+    }
+    return schema;
+  }
+
+  public Option<Schema> getTableAvroSchemaIfPresent(boolean 
includeMetadataFields, Option<HoodieInstant> instant) {
+    return getTableAvroSchemaFromTimelineWithCache(instant) // Get table 
schema from schema evolution timeline.
+        .or(this::getTableCreateSchemaWithoutMetaField) // Fall back: read 
create schema from table config.
+        .map(tableSchema -> includeMetadataFields ? 
HoodieAvroUtils.addMetadataFields(tableSchema, false) : 
HoodieAvroUtils.removeMetadataFields(tableSchema))
+        .map(this::handlePartitionColumnsIfNeeded);
+  }
+
+  private Option<Schema> getTableCreateSchemaWithoutMetaField() {
+    return metaClient.getTableConfig().getTableCreateSchema();
+  }
+
+  private void setCachedLatestCommitWithValidSchema(Option<HoodieInstant> 
instantOption) {
+    latestCommitWithValidSchema = instantOption;
+  }
+
+  private Option<HoodieInstant> getCachedLatestCommitWithValidSchema() {
+    return latestCommitWithValidSchema;
+  }
+
+  @VisibleForTesting
+  Option<Schema> getTableAvroSchemaFromTimelineWithCache(Option<HoodieInstant> 
instantTime) {
+    return 
getTableAvroSchemaFromTimelineWithCache(computeSchemaEvolutionTimelineInReverseOrder(),
 instantTime);
+  }
+
+  Option<Schema> getTableAvroSchemaFromTimelineWithCache(Stream<HoodieInstant> 
reversedTimelineStream, Option<HoodieInstant> instantTime) {

Review Comment:
   https://issues.apache.org/jira/browse/HUDI-9112



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