codope commented on code in PR #11154:
URL: https://github.com/apache/hudi/pull/11154#discussion_r1608630384
##########
hudi-spark-datasource/hudi-spark-common/src/main/scala/org/apache/hudi/HoodieSchemaUtils.scala:
##########
@@ -93,14 +93,14 @@ object HoodieSchemaUtils {
// in the table's one we want to proceed aligning nullability
constraints w/ the table's schema
// Also, we promote types to the latest table schema if possible.
val shouldCanonicalizeSchema = opts.getOrElse(CANONICALIZE_SCHEMA.key,
CANONICALIZE_SCHEMA.defaultValue.toString).toBoolean
+ val shouldReconcileSchema =
opts.getOrElse(DataSourceWriteOptions.RECONCILE_SCHEMA.key(),
+
DataSourceWriteOptions.RECONCILE_SCHEMA.defaultValue().toString).toBoolean
val canonicalizedSourceSchema = if (shouldCanonicalizeSchema) {
- canonicalizeSchema(sourceSchema, latestTableSchema, opts)
+ canonicalizeSchema(sourceSchema, latestTableSchema, opts,
!shouldReconcileSchema)
Review Comment:
why shouldn't we reorder columns when reconcile schema is true? Can you
please add a note in the comment regarding this?
##########
hudi-common/src/main/java/org/apache/hudi/internal/schema/visitor/NameToPositionVisitor.java:
##########
@@ -0,0 +1,115 @@
+/*
+ * 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.internal.schema.visitor;
+
+import org.apache.hudi.internal.schema.InternalSchema;
+import org.apache.hudi.internal.schema.Type;
+import org.apache.hudi.internal.schema.Types;
+
+import java.util.Deque;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import static
org.apache.hudi.internal.schema.utils.InternalSchemaUtils.createFullName;
+
+/**
+ * Schema visitor to produce name -> id map for internalSchema.
+ */
+public class NameToPositionVisitor extends InternalSchemaVisitor<Map<String,
Integer>> {
+ private final Deque<String> fieldNames = new LinkedList<>();
+ private final Map<String, Integer> nameToId = new HashMap<>();
+ private final AtomicInteger position = new AtomicInteger(0);
+
+ @Override
+ public void beforeField(Types.Field field) {
+ fieldNames.push(field.name());
+ }
+
+ @Override
+ public void afterField(Types.Field field) {
+ fieldNames.pop();
+ }
+
+ @Override
+ public void beforeArrayElement(Types.Field elementField) {
+ fieldNames.push(elementField.name());
+ }
+
+ @Override
+ public void afterArrayElement(Types.Field elementField) {
+ fieldNames.pop();
+ }
+
+ @Override
+ public void beforeMapKey(Types.Field keyField) {
+ fieldNames.push(keyField.name());
+ }
+
+ @Override
+ public void afterMapKey(Types.Field keyField) {
+ fieldNames.pop();
+ }
+
+ @Override
+ public void beforeMapValue(Types.Field valueField) {
+ fieldNames.push(valueField.name());
+ }
+
+ @Override
+ public void afterMapValue(Types.Field valueField) {
+ fieldNames.pop();
+ }
+
+ @Override
+ public Map<String, Integer> schema(InternalSchema schema, Map<String,
Integer> recordResult) {
+ return nameToId;
+ }
+
+ @Override
+ public Map<String, Integer> record(Types.RecordType record, List<Map<String,
Integer>> fieldResults) {
+ return nameToId;
+ }
+
+ @Override
+ public Map<String, Integer> field(Types.Field field, Map<String, Integer>
fieldResult) {
+ nameToId.put(createFullName(field.name(), fieldNames),
position.getAndIncrement());
+ return nameToId;
+ }
+
+ @Override
+ public Map<String, Integer> array(Types.ArrayType array, Map<String,
Integer> elementResult) {
+ nameToId.put(createFullName(InternalSchema.ARRAY_ELEMENT, fieldNames),
position.getAndIncrement());
+ return nameToId;
+ }
+
+ @Override
+ public Map<String, Integer> map(Types.MapType map, Map<String, Integer>
keyResult, Map<String, Integer> valueResult) {
+ nameToId.put(createFullName(InternalSchema.MAP_KEY, fieldNames),
position.getAndIncrement());
+ nameToId.put(createFullName(InternalSchema.MAP_VALUE, fieldNames),
position.getAndIncrement());
+ return nameToId;
+ }
+
+ @Override
+ public Map<String, Integer> primitive(Type.PrimitiveType primitive) {
+ return nameToId;
+ }
+}
Review Comment:
nit: newline at the end
##########
hudi-common/src/main/java/org/apache/hudi/internal/schema/utils/AvroSchemaEvolutionUtils.java:
##########
@@ -145,8 +146,9 @@ public static Schema reconcileSchemaRequirements(Schema
sourceSchema, Schema tar
return targetSchema;
}
- InternalSchema sourceInternalSchema = convert(sourceSchema);
InternalSchema targetInternalSchema = convert(targetSchema);
+ // Use existing fieldIds for consistent field ordering between commits
when shouldReorderColumns is true
+ InternalSchema sourceInternalSchema = convert(sourceSchema,
shouldReorderColumns ? targetInternalSchema.getNameToPosition() :
Collections.emptyMap());
Review Comment:
why only source schema? wny not reorder target schema too?
##########
hudi-common/src/main/java/org/apache/hudi/internal/schema/InternalSchemaBuilder.java:
##########
@@ -67,6 +68,10 @@ public Map<String, Integer> buildNameToId(Type type) {
return visit(type, new NameToIDVisitor());
}
+ Map<String, Integer> buildNameToPosition(Type type) {
Review Comment:
High level question: Do we use the InternalSchemaBuilder even when schema on
read is disabled?
##########
hudi-common/src/main/java/org/apache/hudi/internal/schema/visitor/NameToPositionVisitor.java:
##########
@@ -0,0 +1,115 @@
+/*
+ * 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.internal.schema.visitor;
+
+import org.apache.hudi.internal.schema.InternalSchema;
+import org.apache.hudi.internal.schema.Type;
+import org.apache.hudi.internal.schema.Types;
+
+import java.util.Deque;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import static
org.apache.hudi.internal.schema.utils.InternalSchemaUtils.createFullName;
+
+/**
+ * Schema visitor to produce name -> id map for internalSchema.
Review Comment:
```suggestion
* Schema visitor to produce name -> position map for internalSchema, where
position indicates position of the field in the schema.
```
--
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]