yihua commented on code in PR #20883:
URL: https://github.com/apache/hudi/pull/20883#discussion_r4724546990


##########
hudi-common/src/main/java/org/apache/hudi/common/FormatMetaColumn.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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.common;
+
+import org.apache.hudi.common.model.HoodieBaseFile;
+import org.apache.hudi.common.schema.HoodieSchemaField;
+import org.apache.hudi.common.table.HoodieTableMetaClient;
+import org.apache.hudi.exception.HoodieNotSupportedException;
+
+import java.io.Serializable;
+
+/**
+ * A per-row metadata column that an external table format declares for base 
files, see
+ * {@link HoodieTableFormat#getFormatMetaColumns()}.
+ *
+ * <p>A rewrite of a base file stores the column after the columns of the 
table and follows the rule of the Hudi
+ * meta fields: a record carried over unchanged from the base file keeps its 
value, resolved by
+ * {@link #resolverFor}, a record that replaces a record of the base file with 
a changed version gets
+ * {@link #valueOnUpdate}, and any other record written as new or changed gets 
{@link #valueOnWrite}. The column is
+ * not a column of the table: it is not in the table schema, the commit 
metadata schema or the records a reader
+ * returns.
+ */
+public interface FormatMetaColumn extends Serializable {
+
+  /**
+   * Returns the field of the column in the base files, a nullable long or int 
that defaults to null.
+   */
+  HoodieSchemaField field();
+
+  /**
+   * Returns the value of the column for a record that a write with the given 
instant writes into a base file as
+   * new or changed. For compaction and clustering, the instant is the one of 
the compaction or clustering.
+   *
+   * @param instantTime instant time of the write.
+   */
+  Object valueOnWrite(String instantTime);
+
+  /**
+   * Returns the value of the column for a record that a write with the given 
instant writes into a base file as a
+   * changed version of a record of the base file being rewritten, such as an 
update merged with the record it
+   * updates. Returns {@link #valueOnWrite} unless the table format overrides 
it, for example to keep the value of
+   * the replaced record.
+   *
+   * @param instantTime   instant time of the write.
+   * @param replacedValue the value of the column for the replaced record, as 
{@link #resolverFor} resolves it.
+   */
+  default Object valueOnUpdate(String instantTime, Object replacedValue) {
+    return valueOnWrite(instantTime);
+  }
+
+  /**
+   * Returns the resolver of the values of the column for the records of a 
base file being rewritten, possibly
+   * called from a distributed task. Throws if the table format cannot resolve 
the values for the base file, which
+   * fails the write, as the records carried over from the file would lose 
their values.
+   *
+   * @param metaClient metaClient of the table.
+   * @param baseFile   the base file being rewritten.
+   */
+  BaseFileColumnResolver resolverFor(HoodieTableMetaClient metaClient, 
HoodieBaseFile baseFile);

Review Comment:
   Documented the contract on `resolverFor`: cheap, serializable, and it throws 
for a file the format has not recorded, which fails the rewrite. I agree with 
the driver-side `prepare` from one snapshot and will do it as a follow-up 
before the interface ships in a release.



##########
hudi-common/src/main/java/org/apache/hudi/common/FormatMetaColumn.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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.common;
+
+import org.apache.hudi.common.model.HoodieBaseFile;
+import org.apache.hudi.common.schema.HoodieSchemaField;
+import org.apache.hudi.common.table.HoodieTableMetaClient;
+import org.apache.hudi.exception.HoodieNotSupportedException;
+
+import java.io.Serializable;
+
+/**
+ * A per-row metadata column that an external table format declares for base 
files, see
+ * {@link HoodieTableFormat#getFormatMetaColumns()}.
+ *
+ * <p>A rewrite of a base file stores the column after the columns of the 
table and follows the rule of the Hudi
+ * meta fields: a record carried over unchanged from the base file keeps its 
value, resolved by
+ * {@link #resolverFor}, a record that replaces a record of the base file with 
a changed version gets
+ * {@link #valueOnUpdate}, and any other record written as new or changed gets 
{@link #valueOnWrite}. The column is
+ * not a column of the table: it is not in the table schema, the commit 
metadata schema or the records a reader
+ * returns.
+ */
+public interface FormatMetaColumn extends Serializable {
+
+  /**
+   * Returns the field of the column in the base files, a nullable long or int 
that defaults to null.
+   */
+  HoodieSchemaField field();
+
+  /**
+   * Returns the value of the column for a record that a write with the given 
instant writes into a base file as
+   * new or changed. For compaction and clustering, the instant is the one of 
the compaction or clustering.
+   *
+   * @param instantTime instant time of the write.
+   */
+  Object valueOnWrite(String instantTime);
+
+  /**
+   * Returns the value of the column for a record that a write with the given 
instant writes into a base file as a
+   * changed version of a record of the base file being rewritten, such as an 
update merged with the record it
+   * updates. Returns {@link #valueOnWrite} unless the table format overrides 
it, for example to keep the value of
+   * the replaced record.
+   *
+   * @param instantTime   instant time of the write.
+   * @param replacedValue the value of the column for the replaced record, as 
{@link #resolverFor} resolves it.
+   */
+  default Object valueOnUpdate(String instantTime, Object replacedValue) {

Review Comment:
   Done, the Javadoc now says a row lineage format keeps `replacedValue` for 
the row ID and returns null for the sequence number.



##########
hudi-common/src/main/java/org/apache/hudi/common/table/read/FormatMetaColumnHandler.java:
##########
@@ -0,0 +1,248 @@
+/*
+ * 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.common.table.read;
+
+import org.apache.hudi.common.BaseFileColumnResolver;
+import org.apache.hudi.common.FormatMetaColumn;
+import org.apache.hudi.common.engine.RecordContext;
+import org.apache.hudi.common.model.HoodieBaseFile;
+import org.apache.hudi.common.schema.HoodieSchema;
+import org.apache.hudi.common.schema.HoodieSchemaField;
+import org.apache.hudi.common.table.HoodieTableMetaClient;
+import org.apache.hudi.common.util.Option;
+import org.apache.hudi.common.util.ValidationUtils;
+import org.apache.hudi.common.util.collection.ClosableIterator;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * Maintains the format meta columns, see {@link FormatMetaColumn}, of the 
records a rewrite reads from a file
+ * group and writes to a new base file.
+ *
+ * <p>A record carried over from the base file the way the file stores it gets 
the value the table format resolves
+ * from the position of the record in the file and the value the file stores 
for it. A changed version of the last
+ * record read from the base file gets the value on update of the column for 
the value resolved for that record, see
+ * {@link FormatMetaColumn#valueOnUpdate}, and any other record the rewrite 
inserts or changes gets the value on write
+ * of the column.
+ */
+public final class FormatMetaColumnHandler<T> {
+  private final RecordContext<T> recordContext;
+  private final List<FormatMetaColumn> columns;
+  private final String instantTime;
+  private final List<String> columnNames;
+  private final Object[] valuesOnWrite;
+  // Resolvers of the values for the records of the base file, empty without a 
base file
+  private final List<BaseFileColumnResolver> baseFileResolvers;
+  // Position in the base file of the next record read from it
+  private long baseFilePosition = 0;
+  // The last record read from the base file with its schema, its key once 
needed, and the values resolved for it
+  private T lastBaseFileRecord;
+  private HoodieSchema lastBaseFileRecordSchema;
+  private String lastBaseFileRecordKey;
+  private Object[] lastBaseFileValues;
+  // Positions of the columns in the schema of the records of the base file, 
resolved on the first record
+  private int[] baseFileRecordPositions;
+  // Positions of the columns in the schema of the last changed record, -1 for 
a column the schema does not have
+  private Integer changedRecordSchemaId;
+  private int[] changedRecordPositions;
+  // Positions of the columns in the schema of the last record given values on 
write
+  private HoodieSchema writtenRecordSchema;
+  private int[] writtenRecordPositions;
+
+  FormatMetaColumnHandler(RecordContext<T> recordContext, 
List<FormatMetaColumn> columns,
+                          List<BaseFileColumnResolver> baseFileResolvers, 
String instantTime) {
+    this.recordContext = recordContext;
+    this.columns = columns;
+    this.instantTime = instantTime;
+    this.columnNames = columns.stream().map(column -> 
column.field().name()).collect(Collectors.toList());
+    this.valuesOnWrite = columns.stream().map(column -> 
column.valueOnWrite(instantTime)).toArray();
+    this.baseFileResolvers = baseFileResolvers;
+  }
+
+  /**
+   * Returns a handler of the given format meta columns for a rewrite of a 
file group with the given base file, or
+   * an empty option without format meta columns. Fails if the table format 
cannot resolve the values of the
+   * columns for the base file.
+   *
+   * @param recordContext record context of the records
+   * @param columns       the format meta columns, or an empty list
+   * @param metaClient    meta client of the table
+   * @param baseFile      the base file of the file group, if any
+   * @param instantTime   instant time of the rewrite, for the values on write
+   */
+  public static <T> Option<FormatMetaColumnHandler<T>> create(RecordContext<T> 
recordContext, List<FormatMetaColumn> columns,
+                                                              
HoodieTableMetaClient metaClient, Option<HoodieBaseFile> baseFile, String 
instantTime) {
+    if (columns.isEmpty()) {
+      return Option.empty();
+    }
+    List<BaseFileColumnResolver> baseFileResolvers = baseFile.isPresent()
+        ? columns.stream().map(column -> column.resolverFor(metaClient, 
baseFile.get())).collect(Collectors.toList())
+        : Collections.emptyList();
+    return Option.of(new FormatMetaColumnHandler<>(recordContext, columns, 
baseFileResolvers, instantTime));
+  }
+
+  Set<String> getColumnNames() {
+    return new LinkedHashSet<>(columnNames);
+  }
+
+  /**
+   * Resolves the format meta columns of the records of the base file.
+   *
+   * @param baseFileRecords iterator over all the records of the base file, in 
the order of the file
+   * @param schema          schema of the records, which has the format meta 
columns
+   * @return an iterator over the same records with the format meta columns 
resolved
+   */
+  ClosableIterator<T> resolveBaseFileRecords(ClosableIterator<T> 
baseFileRecords, HoodieSchema schema) {
+    return new ClosableIterator<T>() {
+      @Override
+      public boolean hasNext() {
+        return baseFileRecords.hasNext();
+      }
+
+      @Override
+      public T next() {
+        return resolveNextBaseFileRecord(baseFileRecords.next(), schema);
+      }
+
+      @Override
+      public void close() {
+        baseFileRecords.close();
+      }
+    };
+  }
+
+  /**
+   * Resolves the format meta columns of the next record of the base file. 
Every record of the base file has to be
+   * passed, in the order of the file.
+   *
+   * @param record the next record of the base file
+   * @param schema schema of the record, which has the format meta columns
+   * @return the record with the format meta columns resolved, which is the 
given record when it can be updated in place
+   */
+  public T resolveNextBaseFileRecord(T record, HoodieSchema schema) {
+    if (baseFileRecordPositions == null) {
+      ValidationUtils.checkArgument(!baseFileResolvers.isEmpty(), "The format 
meta columns of a base file cannot be resolved without the base file");
+      baseFileRecordPositions = getPositions(schema);
+      
ValidationUtils.checkArgument(Arrays.stream(baseFileRecordPositions).allMatch(pos
 -> pos >= 0),
+          () -> "The format meta columns " + columnNames + " have to be read 
from the base file, got: " + schema);
+      lastBaseFileValues = new Object[columnNames.size()];
+    }
+    long position = baseFilePosition++;
+    T result = record;
+    for (int i = 0; i < columnNames.size(); i++) {
+      Object storedValue = recordContext.getValue(result, schema, 
columnNames.get(i));
+      Object value = baseFileResolvers.get(i).valueAt(position, storedValue);
+      if (value != storedValue) {
+        result = recordContext.setValue(result, baseFileRecordPositions[i], 
value);
+      }
+      lastBaseFileValues[i] = value;
+    }
+    lastBaseFileRecord = result;
+    lastBaseFileRecordSchema = schema;
+    lastBaseFileRecordKey = null;
+    return result;
+  }
+
+  /**
+   * Returns the values of the format meta columns for a record written as new 
or changed: the values on update for
+   * the values of the last record read from the base file if the record has 
its key, so is a changed version of it,
+   * and the values on write otherwise. A file group reader merges a record of 
the base file and returns the result
+   * before it reads the next one, so a record it returns as changed has the 
key of the last record read from the
+   * base file exactly when it replaces that record.
+   */
+  private Object[] valuesFor(String recordKey) {

Review Comment:
   Addressed by stating the invariant in the Javadoc of the handler field in 
`HoodieFileGroupReader`; passing the `UpdateProcessor` decision in is a 
follow-up.



##########
hudi-common/src/main/java/org/apache/hudi/common/table/read/FormatMetaColumnHandler.java:
##########
@@ -0,0 +1,248 @@
+/*
+ * 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.common.table.read;
+
+import org.apache.hudi.common.BaseFileColumnResolver;
+import org.apache.hudi.common.FormatMetaColumn;
+import org.apache.hudi.common.engine.RecordContext;
+import org.apache.hudi.common.model.HoodieBaseFile;
+import org.apache.hudi.common.schema.HoodieSchema;
+import org.apache.hudi.common.schema.HoodieSchemaField;
+import org.apache.hudi.common.table.HoodieTableMetaClient;
+import org.apache.hudi.common.util.Option;
+import org.apache.hudi.common.util.ValidationUtils;
+import org.apache.hudi.common.util.collection.ClosableIterator;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * Maintains the format meta columns, see {@link FormatMetaColumn}, of the 
records a rewrite reads from a file
+ * group and writes to a new base file.
+ *
+ * <p>A record carried over from the base file the way the file stores it gets 
the value the table format resolves
+ * from the position of the record in the file and the value the file stores 
for it. A changed version of the last
+ * record read from the base file gets the value on update of the column for 
the value resolved for that record, see
+ * {@link FormatMetaColumn#valueOnUpdate}, and any other record the rewrite 
inserts or changes gets the value on write
+ * of the column.
+ */
+public final class FormatMetaColumnHandler<T> {
+  private final RecordContext<T> recordContext;
+  private final List<FormatMetaColumn> columns;
+  private final String instantTime;
+  private final List<String> columnNames;
+  private final Object[] valuesOnWrite;
+  // Resolvers of the values for the records of the base file, empty without a 
base file
+  private final List<BaseFileColumnResolver> baseFileResolvers;
+  // Position in the base file of the next record read from it
+  private long baseFilePosition = 0;
+  // The last record read from the base file with its schema, its key once 
needed, and the values resolved for it
+  private T lastBaseFileRecord;
+  private HoodieSchema lastBaseFileRecordSchema;
+  private String lastBaseFileRecordKey;
+  private Object[] lastBaseFileValues;
+  // Positions of the columns in the schema of the records of the base file, 
resolved on the first record
+  private int[] baseFileRecordPositions;
+  // Positions of the columns in the schema of the last changed record, -1 for 
a column the schema does not have
+  private Integer changedRecordSchemaId;
+  private int[] changedRecordPositions;
+  // Positions of the columns in the schema of the last record given values on 
write
+  private HoodieSchema writtenRecordSchema;
+  private int[] writtenRecordPositions;
+
+  FormatMetaColumnHandler(RecordContext<T> recordContext, 
List<FormatMetaColumn> columns,
+                          List<BaseFileColumnResolver> baseFileResolvers, 
String instantTime) {
+    this.recordContext = recordContext;
+    this.columns = columns;
+    this.instantTime = instantTime;
+    this.columnNames = columns.stream().map(column -> 
column.field().name()).collect(Collectors.toList());
+    this.valuesOnWrite = columns.stream().map(column -> 
column.valueOnWrite(instantTime)).toArray();
+    this.baseFileResolvers = baseFileResolvers;
+  }
+
+  /**
+   * Returns a handler of the given format meta columns for a rewrite of a 
file group with the given base file, or
+   * an empty option without format meta columns. Fails if the table format 
cannot resolve the values of the
+   * columns for the base file.
+   *
+   * @param recordContext record context of the records
+   * @param columns       the format meta columns, or an empty list
+   * @param metaClient    meta client of the table
+   * @param baseFile      the base file of the file group, if any
+   * @param instantTime   instant time of the rewrite, for the values on write
+   */
+  public static <T> Option<FormatMetaColumnHandler<T>> create(RecordContext<T> 
recordContext, List<FormatMetaColumn> columns,
+                                                              
HoodieTableMetaClient metaClient, Option<HoodieBaseFile> baseFile, String 
instantTime) {
+    if (columns.isEmpty()) {
+      return Option.empty();
+    }
+    List<BaseFileColumnResolver> baseFileResolvers = baseFile.isPresent()
+        ? columns.stream().map(column -> column.resolverFor(metaClient, 
baseFile.get())).collect(Collectors.toList())
+        : Collections.emptyList();
+    return Option.of(new FormatMetaColumnHandler<>(recordContext, columns, 
baseFileResolvers, instantTime));
+  }
+
+  Set<String> getColumnNames() {
+    return new LinkedHashSet<>(columnNames);
+  }
+
+  /**
+   * Resolves the format meta columns of the records of the base file.
+   *
+   * @param baseFileRecords iterator over all the records of the base file, in 
the order of the file
+   * @param schema          schema of the records, which has the format meta 
columns
+   * @return an iterator over the same records with the format meta columns 
resolved
+   */
+  ClosableIterator<T> resolveBaseFileRecords(ClosableIterator<T> 
baseFileRecords, HoodieSchema schema) {
+    return new ClosableIterator<T>() {
+      @Override
+      public boolean hasNext() {
+        return baseFileRecords.hasNext();
+      }
+
+      @Override
+      public T next() {
+        return resolveNextBaseFileRecord(baseFileRecords.next(), schema);
+      }
+
+      @Override
+      public void close() {
+        baseFileRecords.close();
+      }
+    };
+  }
+
+  /**
+   * Resolves the format meta columns of the next record of the base file. 
Every record of the base file has to be
+   * passed, in the order of the file.
+   *
+   * @param record the next record of the base file
+   * @param schema schema of the record, which has the format meta columns
+   * @return the record with the format meta columns resolved, which is the 
given record when it can be updated in place
+   */
+  public T resolveNextBaseFileRecord(T record, HoodieSchema schema) {
+    if (baseFileRecordPositions == null) {
+      ValidationUtils.checkArgument(!baseFileResolvers.isEmpty(), "The format 
meta columns of a base file cannot be resolved without the base file");
+      baseFileRecordPositions = getPositions(schema);
+      
ValidationUtils.checkArgument(Arrays.stream(baseFileRecordPositions).allMatch(pos
 -> pos >= 0),
+          () -> "The format meta columns " + columnNames + " have to be read 
from the base file, got: " + schema);
+      lastBaseFileValues = new Object[columnNames.size()];
+    }
+    long position = baseFilePosition++;
+    T result = record;
+    for (int i = 0; i < columnNames.size(); i++) {
+      Object storedValue = recordContext.getValue(result, schema, 
columnNames.get(i));
+      Object value = baseFileResolvers.get(i).valueAt(position, storedValue);
+      if (value != storedValue) {
+        result = recordContext.setValue(result, baseFileRecordPositions[i], 
value);
+      }
+      lastBaseFileValues[i] = value;
+    }
+    lastBaseFileRecord = result;
+    lastBaseFileRecordSchema = schema;
+    lastBaseFileRecordKey = null;
+    return result;
+  }
+
+  /**
+   * Returns the values of the format meta columns for a record written as new 
or changed: the values on update for
+   * the values of the last record read from the base file if the record has 
its key, so is a changed version of it,
+   * and the values on write otherwise. A file group reader merges a record of 
the base file and returns the result
+   * before it reads the next one, so a record it returns as changed has the 
key of the last record read from the
+   * base file exactly when it replaces that record.
+   */
+  private Object[] valuesFor(String recordKey) {
+    if (lastBaseFileRecord == null) {
+      return valuesOnWrite;
+    }
+    if (lastBaseFileRecordKey == null) {
+      lastBaseFileRecordKey = recordContext.getRecordKey(lastBaseFileRecord, 
lastBaseFileRecordSchema);
+    }
+    if (!lastBaseFileRecordKey.equals(recordKey)) {
+      return valuesOnWrite;
+    }
+    Object[] values = new Object[columns.size()];
+    for (int i = 0; i < columns.size(); i++) {
+      values[i] = columns.get(i).valueOnUpdate(instantTime, 
lastBaseFileValues[i]);
+    }
+    return values;
+  }
+
+  /**
+   * Sets the format meta columns of the record to their values on update or 
on write, see {@link #valuesFor}, if
+   * the record is not returned the way it is stored in the base file.
+   */
+  void setValuesOnWriteIfChanged(BufferedRecord<T> record) {
+    if (record.getHoodieOperation() == null || record.isDelete() || 
record.isEmpty() || record.getSchemaId() == null) {

Review Comment:
   Addressed in the same Javadoc in `HoodieFileGroupReader`, which states that 
a returned record without an operation is a base-file record returned 
unchanged; passing the decision in is a follow-up.



##########
hudi-client/hudi-client-common/src/main/java/org/apache/hudi/io/HoodieWriteHandle.java:
##########
@@ -121,7 +124,9 @@ protected HoodieWriteHandle(HoodieWriteConfig config, 
String instantTime, String
     this.partitionPath = partitionPath;
     this.fileId = fileId;
     this.writeSchema = HoodieSchemaCache.intern(overriddenSchema.orElseGet(() 
-> getWriteSchema(config)));
-    this.writeSchemaWithMetaFields = 
HoodieSchemaCache.intern(HoodieSchemaUtils.addMetadataFields(writeSchema, 
config.allowOperationMetadataField()));
+    this.formatMetaColumns = getFormatMetaColumns();

Review Comment:
   Thanks, computing the columns once per write will come in a follow-up 
together with point 4; today it is one call per handle, not per record.



##########
hudi-client/hudi-client-common/src/main/java/org/apache/hudi/io/HoodieWriteMergeHandle.java:
##########
@@ -448,12 +490,43 @@ protected void writeToFile(HoodieKey key, HoodieRecord<T> 
record, HoodieSchema s
           : record;
       fileWriter.write(key.getRecordKey(), populatedRecord, 
writeSchemaWithMetaFields, props);
     } else {
+      if (!formatMetaColumns.isEmpty()) {
+        record = appendFormatMetaColumnValuesOnWrite(key.getRecordKey(), 
record, schema, props);

Review Comment:
   Thanks, the per-handle processor will be a follow-up (point 4).



##########
hudi-common/src/main/java/org/apache/hudi/common/table/read/HoodieFileGroupReader.java:
##########
@@ -356,6 +374,9 @@ boolean hasNext() throws IOException {
    */
   BufferedRecord<T> next() {
     BufferedRecord<T> nextVal = recordBuffer == null ? 
bufferedRecordConverter.convert(baseFileIterator.next()) : recordBuffer.next();
+    if (formatMetaColumnHandler != null) {
+      formatMetaColumnHandler.setValuesOnWriteIfChanged(nextVal);

Review Comment:
   Thanks, the `UpdateProcessor` decorator will be a follow-up (point 4).



##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieSchemaUtils.java:
##########
@@ -682,6 +684,43 @@ public static HoodieSchema 
appendFieldsToSchema(HoodieSchema schema, List<Hoodie
     return appendFieldsToSchemaBase(schema, newFields, false);
   }
 
+  /**
+   * Appends the metadata columns an external table format declares, see
+   * {@code HoodieTableFormat#getFormatMetaColumns}, at the end of the given 
schema, in their order. Returns the
+   * given schema as is when the table format declares none.
+   *
+   * @param schema  the schema to append the columns to
+   * @param columns the metadata columns of the table format, or an empty list
+   * @return the schema ending with the columns
+   * @throws IllegalArgumentException if a column is not a nullable long or 
int with a null default, has the name of
+   *                                  a Hudi meta field, or if two columns, or 
a column and a field of the schema,
+   *                                  have the same name
+   */
+  public static HoodieSchema appendFormatMetaColumns(HoodieSchema schema, 
List<FormatMetaColumn> columns) {
+    if (columns.isEmpty()) {
+      return schema;
+    }
+    List<HoodieSchemaField> newFields = new ArrayList<>(columns.size());
+    Set<String> names = new HashSet<>();
+    for (FormatMetaColumn column : columns) {
+      HoodieSchemaField field = column.field();
+      ValidationUtils.checkArgument(field.isNullable() && 
field.hasDefaultValue() && toJavaDefaultValue(field) == null,
+          () -> "A format meta column has to be nullable and default to null, 
got: " + field);
+      // The writers set the values of the columns in place, which an engine 
row supports for fixed-width types only
+      HoodieSchemaType type = field.schema().getNonNullType().getType();
+      ValidationUtils.checkArgument(type == HoodieSchemaType.LONG || type == 
HoodieSchemaType.INT,
+          () -> "A format meta column has to be of type long or int, got: " + 
field);
+      ValidationUtils.checkArgument(!isMetadataField(field.name()),
+          () -> "The format meta column " + field.name() + " has the name of a 
Hudi meta field");
+      ValidationUtils.checkArgument(schema.getField(field.name()).isEmpty() && 
names.add(field.name()),

Review Comment:
   Documented the declared names as reserved in the Javadoc of 
`HoodieTableFormat#getFormatMetaColumns`; a check when the schema evolves is a 
follow-up.



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