You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@paimon.apache.org by "YannByron (via GitHub)" <gi...@apache.org> on 2023/07/27 17:13:31 UTC

[GitHub] [incubator-paimon] YannByron opened a new pull request, #1678: [spark] supports schema merging when write

YannByron opened a new pull request, #1678:
URL: https://github.com/apache/incubator-paimon/pull/1678

   <!-- Please specify the module before the PR name: [core] ... or [flink] ... -->
   
   ### Purpose
   
   <!-- Linking this pull request to the issue -->
   Linked issue: close #xxx
   
   <!-- What is the purpose of the change -->
   
   ### Tests
   
   <!-- List UT and IT cases to verify this change -->
   
   ### API and Format
   
   <!-- Does this change affect API or storage format -->
   
   ### Documentation
   
   <!-- Does this change introduce a new feature -->
   


-- 
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: issues-unsubscribe@paimon.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-paimon] JingsongLi commented on a diff in pull request #1678: [spark] supports schema merging when write

Posted by "JingsongLi (via GitHub)" <gi...@apache.org>.
JingsongLi commented on code in PR #1678:
URL: https://github.com/apache/incubator-paimon/pull/1678#discussion_r1280024092


##########
paimon-common/src/main/java/org/apache/paimon/types/DataField.java:
##########
@@ -76,6 +77,11 @@ public DataType type() {
         return type;
     }
 
+    @VisibleForTesting

Review Comment:
   A newId method looks good to me.
   We can just remove `VisibleForTesting`. (Public class should not have public VisibleForTesting method)



##########
paimon-core/src/main/java/org/apache/paimon/AbstractFileStore.java:
##########
@@ -142,6 +142,11 @@ public CoreOptions options() {
         return options;
     }
 
+    @Override
+    public boolean mergeSchema(RowType rowType, Boolean allowExplicitCast) {

Review Comment:
   boolean instead of Boolean?



-- 
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: issues-unsubscribe@paimon.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-paimon] JingsongLi commented on a diff in pull request #1678: [spark] supports schema merging when write

Posted by "JingsongLi (via GitHub)" <gi...@apache.org>.
JingsongLi commented on code in PR #1678:
URL: https://github.com/apache/incubator-paimon/pull/1678#discussion_r1278754288


##########
paimon-core/src/main/java/org/apache/paimon/schema/SchemaMergingUtils.java:
##########
@@ -0,0 +1,196 @@
+/*
+ * 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.paimon.schema;
+
+import org.apache.paimon.types.ArrayType;
+import org.apache.paimon.types.DataField;
+import org.apache.paimon.types.DataType;
+import org.apache.paimon.types.DataTypeCasts;
+import org.apache.paimon.types.DecimalType;
+import org.apache.paimon.types.ILength;
+import org.apache.paimon.types.IPrecision;
+import org.apache.paimon.types.MapType;
+import org.apache.paimon.types.MultisetType;
+import org.apache.paimon.types.ReassignFieldId;
+import org.apache.paimon.types.RowType;
+
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+/** The util class for merging the schemas. */
+public class SchemaMergingUtils {

Review Comment:
   Maybe we can merge this class to `SchemaManager`?
   And introduce a method to `mergeSchema(RowType rowType)` to `FileStore`?



##########
paimon-common/src/main/java/org/apache/paimon/types/ILength.java:
##########
@@ -0,0 +1,25 @@
+/*
+ * 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.paimon.types;
+
+/** An interface with a getLength method. */
+public interface ILength {

Review Comment:
   Do you think https://github.com/apache/incubator-paimon/pull/1685 works for you?



##########
paimon-core/src/main/java/org/apache/paimon/schema/SchemaMergingUtils.java:
##########
@@ -0,0 +1,196 @@
+/*
+ * 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.paimon.schema;
+
+import org.apache.paimon.types.ArrayType;
+import org.apache.paimon.types.DataField;
+import org.apache.paimon.types.DataType;
+import org.apache.paimon.types.DataTypeCasts;
+import org.apache.paimon.types.DecimalType;
+import org.apache.paimon.types.ILength;
+import org.apache.paimon.types.IPrecision;
+import org.apache.paimon.types.MapType;
+import org.apache.paimon.types.MultisetType;
+import org.apache.paimon.types.ReassignFieldId;
+import org.apache.paimon.types.RowType;
+
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+/** The util class for merging the schemas. */
+public class SchemaMergingUtils {
+
+    public static TableSchema mergeSchemas(TableSchema currentTableSchema, RowType dataFields) {
+        if (currentTableSchema.logicalRowType().equals(dataFields)) {
+            return currentTableSchema;
+        }
+
+        AtomicInteger highestFieldId = new AtomicInteger(currentTableSchema.highestFieldId());
+        RowType newRowType =
+                mergeSchemas(currentTableSchema.logicalRowType(), dataFields, highestFieldId);
+        return new TableSchema(
+                currentTableSchema.id() + 1,
+                newRowType.getFields(),
+                highestFieldId.get(),
+                currentTableSchema.partitionKeys(),
+                currentTableSchema.primaryKeys(),
+                currentTableSchema.options(),
+                currentTableSchema.comment());
+    }
+
+    public static RowType mergeSchemas(
+            RowType tableSchema, RowType dataSchema, AtomicInteger highestFieldId) {
+        return (RowType) merge(tableSchema, dataSchema, highestFieldId);
+    }
+
+    /**
+     * Merge the base data type and the update data type if possible.
+     *
+     * <p>For RowType, find the fields which exists in both the base schema and the update schema,
+     * and try to merge them by calling the method iteratively; remain those fields that are only in
+     * the base schema and append those fields that are only in the update schema.
+     *
+     * <p>For other complex type, try to merge the element types.
+     *
+     * <p>For primitive data type, we treat that's compatible if the original type can be safely
+     * cast to the new type.
+     */
+    static DataType merge(DataType base0, DataType update0, AtomicInteger highestFieldId) {
+        // Here we try t0 merge the base0 and update0 without regard to the nullability,
+        // and set the base0's nullability to the return's.
+        DataType base = base0.copy(true);
+        DataType update = update0.copy(true);
+
+        if (base.equals(update)) {
+            return base0;
+        } else if (base instanceof RowType && update instanceof RowType) {
+            List<DataField> baseFields = ((RowType) base).getFields();
+            List<DataField> updateFields = ((RowType) update).getFields();
+            Map<String, DataField> updateFieldMap =
+                    updateFields.stream()
+                            .collect(Collectors.toMap(DataField::name, Function.identity()));
+            List<DataField> updatedFields =
+                    baseFields.stream()
+                            .map(
+                                    baseField -> {
+                                        if (updateFieldMap.containsKey(baseField.name())) {
+                                            DataField updateField =
+                                                    updateFieldMap.get(baseField.name());
+                                            DataType updatedDataType =
+                                                    merge(
+                                                            baseField.type(),
+                                                            updateField.type(),
+                                                            highestFieldId);
+                                            return new DataField(
+                                                    baseField.id(),
+                                                    baseField.name(),
+                                                    updatedDataType,
+                                                    baseField.description());
+                                        } else {
+                                            return baseField;
+                                        }
+                                    })
+                            .collect(Collectors.toList());
+
+            Map<String, DataField> baseFieldMap =
+                    baseFields.stream()
+                            .collect(Collectors.toMap(DataField::name, Function.identity()));
+            List<DataField> newFields =
+                    updateFields.stream()
+                            .filter(field -> !baseFieldMap.containsKey(field.name()))
+                            .map(field -> assignIdForNewField(field, highestFieldId))
+                            .collect(Collectors.toList());
+
+            updatedFields.addAll(newFields);
+            return new RowType(base.isNullable(), updatedFields);
+        } else if (base instanceof MapType && update instanceof MapType) {
+            return new MapType(
+                    base.isNullable(),
+                    merge(
+                            ((MapType) base).getKeyType(),
+                            ((MapType) update).getKeyType(),
+                            highestFieldId),
+                    merge(
+                            ((MapType) base).getValueType(),
+                            ((MapType) update).getValueType(),
+                            highestFieldId));
+        } else if (base instanceof ArrayType && update instanceof ArrayType) {
+            return new ArrayType(
+                    base.isNullable(),
+                    merge(
+                            ((ArrayType) base).getElementType(),
+                            ((ArrayType) update).getElementType(),
+                            highestFieldId));
+        } else if (base instanceof MultisetType && update instanceof MultisetType) {
+            return new MultisetType(
+                    base.isNullable(),
+                    merge(
+                            ((MultisetType) base).getElementType(),
+                            ((MultisetType) update).getElementType(),
+                            highestFieldId));
+        } else if (base instanceof DecimalType && update instanceof DecimalType) {
+            if (base.equals(update)) {
+                return base0;
+            } else {
+                throw new UnsupportedOperationException(
+                        String.format(
+                                "Failed to merge decimal types with different precision or scale: %s and %s",
+                                base, update));
+            }
+        } else if (DataTypeCasts.supportsImplicitCast(base, update)) {

Review Comment:
   Currently, we have using `supportsExplicitCast` to manually casting (From DDL alter table).
   Do you think we should use `supportsExplicitCast`? Or provide an option to let user to choose?



##########
paimon-core/src/main/java/org/apache/paimon/schema/SchemaMergingUtils.java:
##########
@@ -0,0 +1,196 @@
+/*
+ * 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.paimon.schema;
+
+import org.apache.paimon.types.ArrayType;
+import org.apache.paimon.types.DataField;
+import org.apache.paimon.types.DataType;
+import org.apache.paimon.types.DataTypeCasts;
+import org.apache.paimon.types.DecimalType;
+import org.apache.paimon.types.ILength;
+import org.apache.paimon.types.IPrecision;
+import org.apache.paimon.types.MapType;
+import org.apache.paimon.types.MultisetType;
+import org.apache.paimon.types.ReassignFieldId;
+import org.apache.paimon.types.RowType;
+
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+/** The util class for merging the schemas. */
+public class SchemaMergingUtils {
+
+    public static TableSchema mergeSchemas(TableSchema currentTableSchema, RowType dataFields) {
+        if (currentTableSchema.logicalRowType().equals(dataFields)) {
+            return currentTableSchema;
+        }
+
+        AtomicInteger highestFieldId = new AtomicInteger(currentTableSchema.highestFieldId());
+        RowType newRowType =
+                mergeSchemas(currentTableSchema.logicalRowType(), dataFields, highestFieldId);
+        return new TableSchema(
+                currentTableSchema.id() + 1,
+                newRowType.getFields(),
+                highestFieldId.get(),
+                currentTableSchema.partitionKeys(),
+                currentTableSchema.primaryKeys(),
+                currentTableSchema.options(),
+                currentTableSchema.comment());
+    }
+
+    public static RowType mergeSchemas(
+            RowType tableSchema, RowType dataSchema, AtomicInteger highestFieldId) {
+        return (RowType) merge(tableSchema, dataSchema, highestFieldId);
+    }
+
+    /**
+     * Merge the base data type and the update data type if possible.
+     *
+     * <p>For RowType, find the fields which exists in both the base schema and the update schema,
+     * and try to merge them by calling the method iteratively; remain those fields that are only in
+     * the base schema and append those fields that are only in the update schema.
+     *
+     * <p>For other complex type, try to merge the element types.
+     *
+     * <p>For primitive data type, we treat that's compatible if the original type can be safely
+     * cast to the new type.
+     */
+    static DataType merge(DataType base0, DataType update0, AtomicInteger highestFieldId) {
+        // Here we try t0 merge the base0 and update0 without regard to the nullability,
+        // and set the base0's nullability to the return's.
+        DataType base = base0.copy(true);
+        DataType update = update0.copy(true);
+
+        if (base.equals(update)) {
+            return base0;
+        } else if (base instanceof RowType && update instanceof RowType) {

Review Comment:
   Maybe here can use `DataTypeDefaultVisitor`?



-- 
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: issues-unsubscribe@paimon.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-paimon] JingsongLi merged pull request #1678: [spark] supports schema merging when write

Posted by "JingsongLi (via GitHub)" <gi...@apache.org>.
JingsongLi merged PR #1678:
URL: https://github.com/apache/incubator-paimon/pull/1678


-- 
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: issues-unsubscribe@paimon.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-paimon] YannByron commented on a diff in pull request #1678: [spark] supports schema merging when write

Posted by "YannByron (via GitHub)" <gi...@apache.org>.
YannByron commented on code in PR #1678:
URL: https://github.com/apache/incubator-paimon/pull/1678#discussion_r1278765909


##########
paimon-core/src/main/java/org/apache/paimon/schema/SchemaMergingUtils.java:
##########
@@ -0,0 +1,196 @@
+/*
+ * 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.paimon.schema;
+
+import org.apache.paimon.types.ArrayType;
+import org.apache.paimon.types.DataField;
+import org.apache.paimon.types.DataType;
+import org.apache.paimon.types.DataTypeCasts;
+import org.apache.paimon.types.DecimalType;
+import org.apache.paimon.types.ILength;
+import org.apache.paimon.types.IPrecision;
+import org.apache.paimon.types.MapType;
+import org.apache.paimon.types.MultisetType;
+import org.apache.paimon.types.ReassignFieldId;
+import org.apache.paimon.types.RowType;
+
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+/** The util class for merging the schemas. */
+public class SchemaMergingUtils {

Review Comment:
   > Maybe we can merge this class to SchemaManager?
   
   Even though merge this to `SchemaManager`, all the methods will be `static`. i think there  is no different.
   
   > And introduce a method to mergeSchema(RowType rowType) to FileStore?
   
   yes, better than the current `commitSchema`.



-- 
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: issues-unsubscribe@paimon.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-paimon] YannByron commented on a diff in pull request #1678: [spark] supports schema merging when write

Posted by "YannByron (via GitHub)" <gi...@apache.org>.
YannByron commented on code in PR #1678:
URL: https://github.com/apache/incubator-paimon/pull/1678#discussion_r1278774405


##########
paimon-core/src/main/java/org/apache/paimon/schema/SchemaMergingUtils.java:
##########
@@ -0,0 +1,196 @@
+/*
+ * 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.paimon.schema;
+
+import org.apache.paimon.types.ArrayType;
+import org.apache.paimon.types.DataField;
+import org.apache.paimon.types.DataType;
+import org.apache.paimon.types.DataTypeCasts;
+import org.apache.paimon.types.DecimalType;
+import org.apache.paimon.types.ILength;
+import org.apache.paimon.types.IPrecision;
+import org.apache.paimon.types.MapType;
+import org.apache.paimon.types.MultisetType;
+import org.apache.paimon.types.ReassignFieldId;
+import org.apache.paimon.types.RowType;
+
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+/** The util class for merging the schemas. */
+public class SchemaMergingUtils {
+
+    public static TableSchema mergeSchemas(TableSchema currentTableSchema, RowType dataFields) {
+        if (currentTableSchema.logicalRowType().equals(dataFields)) {
+            return currentTableSchema;
+        }
+
+        AtomicInteger highestFieldId = new AtomicInteger(currentTableSchema.highestFieldId());
+        RowType newRowType =
+                mergeSchemas(currentTableSchema.logicalRowType(), dataFields, highestFieldId);
+        return new TableSchema(
+                currentTableSchema.id() + 1,
+                newRowType.getFields(),
+                highestFieldId.get(),
+                currentTableSchema.partitionKeys(),
+                currentTableSchema.primaryKeys(),
+                currentTableSchema.options(),
+                currentTableSchema.comment());
+    }
+
+    public static RowType mergeSchemas(
+            RowType tableSchema, RowType dataSchema, AtomicInteger highestFieldId) {
+        return (RowType) merge(tableSchema, dataSchema, highestFieldId);
+    }
+
+    /**
+     * Merge the base data type and the update data type if possible.
+     *
+     * <p>For RowType, find the fields which exists in both the base schema and the update schema,
+     * and try to merge them by calling the method iteratively; remain those fields that are only in
+     * the base schema and append those fields that are only in the update schema.
+     *
+     * <p>For other complex type, try to merge the element types.
+     *
+     * <p>For primitive data type, we treat that's compatible if the original type can be safely
+     * cast to the new type.
+     */
+    static DataType merge(DataType base0, DataType update0, AtomicInteger highestFieldId) {
+        // Here we try t0 merge the base0 and update0 without regard to the nullability,
+        // and set the base0's nullability to the return's.
+        DataType base = base0.copy(true);
+        DataType update = update0.copy(true);
+
+        if (base.equals(update)) {
+            return base0;
+        } else if (base instanceof RowType && update instanceof RowType) {

Review Comment:
   I think `DataTypeVisitor` is more used to extract some attributes, here I wanna know if a datatype is exactly what i want, and do some process if true. So maybe `instanceof` is clear enough.



-- 
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: issues-unsubscribe@paimon.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-paimon] YannByron commented on a diff in pull request #1678: [spark] supports schema merging when write

Posted by "YannByron (via GitHub)" <gi...@apache.org>.
YannByron commented on code in PR #1678:
URL: https://github.com/apache/incubator-paimon/pull/1678#discussion_r1278764345


##########
paimon-common/src/main/java/org/apache/paimon/types/ILength.java:
##########
@@ -0,0 +1,25 @@
+/*
+ * 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.paimon.types;
+
+/** An interface with a getLength method. */
+public interface ILength {

Review Comment:
   sure. very nice. I can't find it before.



-- 
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: issues-unsubscribe@paimon.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-paimon] YannByron commented on a diff in pull request #1678: [spark] supports schema merging when write

Posted by "YannByron (via GitHub)" <gi...@apache.org>.
YannByron commented on code in PR #1678:
URL: https://github.com/apache/incubator-paimon/pull/1678#discussion_r1278771828


##########
paimon-core/src/main/java/org/apache/paimon/schema/SchemaMergingUtils.java:
##########
@@ -0,0 +1,196 @@
+/*
+ * 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.paimon.schema;
+
+import org.apache.paimon.types.ArrayType;
+import org.apache.paimon.types.DataField;
+import org.apache.paimon.types.DataType;
+import org.apache.paimon.types.DataTypeCasts;
+import org.apache.paimon.types.DecimalType;
+import org.apache.paimon.types.ILength;
+import org.apache.paimon.types.IPrecision;
+import org.apache.paimon.types.MapType;
+import org.apache.paimon.types.MultisetType;
+import org.apache.paimon.types.ReassignFieldId;
+import org.apache.paimon.types.RowType;
+
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+/** The util class for merging the schemas. */
+public class SchemaMergingUtils {
+
+    public static TableSchema mergeSchemas(TableSchema currentTableSchema, RowType dataFields) {
+        if (currentTableSchema.logicalRowType().equals(dataFields)) {
+            return currentTableSchema;
+        }
+
+        AtomicInteger highestFieldId = new AtomicInteger(currentTableSchema.highestFieldId());
+        RowType newRowType =
+                mergeSchemas(currentTableSchema.logicalRowType(), dataFields, highestFieldId);
+        return new TableSchema(
+                currentTableSchema.id() + 1,
+                newRowType.getFields(),
+                highestFieldId.get(),
+                currentTableSchema.partitionKeys(),
+                currentTableSchema.primaryKeys(),
+                currentTableSchema.options(),
+                currentTableSchema.comment());
+    }
+
+    public static RowType mergeSchemas(
+            RowType tableSchema, RowType dataSchema, AtomicInteger highestFieldId) {
+        return (RowType) merge(tableSchema, dataSchema, highestFieldId);
+    }
+
+    /**
+     * Merge the base data type and the update data type if possible.
+     *
+     * <p>For RowType, find the fields which exists in both the base schema and the update schema,
+     * and try to merge them by calling the method iteratively; remain those fields that are only in
+     * the base schema and append those fields that are only in the update schema.
+     *
+     * <p>For other complex type, try to merge the element types.
+     *
+     * <p>For primitive data type, we treat that's compatible if the original type can be safely
+     * cast to the new type.
+     */
+    static DataType merge(DataType base0, DataType update0, AtomicInteger highestFieldId) {
+        // Here we try t0 merge the base0 and update0 without regard to the nullability,
+        // and set the base0's nullability to the return's.
+        DataType base = base0.copy(true);
+        DataType update = update0.copy(true);
+
+        if (base.equals(update)) {
+            return base0;
+        } else if (base instanceof RowType && update instanceof RowType) {
+            List<DataField> baseFields = ((RowType) base).getFields();
+            List<DataField> updateFields = ((RowType) update).getFields();
+            Map<String, DataField> updateFieldMap =
+                    updateFields.stream()
+                            .collect(Collectors.toMap(DataField::name, Function.identity()));
+            List<DataField> updatedFields =
+                    baseFields.stream()
+                            .map(
+                                    baseField -> {
+                                        if (updateFieldMap.containsKey(baseField.name())) {
+                                            DataField updateField =
+                                                    updateFieldMap.get(baseField.name());
+                                            DataType updatedDataType =
+                                                    merge(
+                                                            baseField.type(),
+                                                            updateField.type(),
+                                                            highestFieldId);
+                                            return new DataField(
+                                                    baseField.id(),
+                                                    baseField.name(),
+                                                    updatedDataType,
+                                                    baseField.description());
+                                        } else {
+                                            return baseField;
+                                        }
+                                    })
+                            .collect(Collectors.toList());
+
+            Map<String, DataField> baseFieldMap =
+                    baseFields.stream()
+                            .collect(Collectors.toMap(DataField::name, Function.identity()));
+            List<DataField> newFields =
+                    updateFields.stream()
+                            .filter(field -> !baseFieldMap.containsKey(field.name()))
+                            .map(field -> assignIdForNewField(field, highestFieldId))
+                            .collect(Collectors.toList());
+
+            updatedFields.addAll(newFields);
+            return new RowType(base.isNullable(), updatedFields);
+        } else if (base instanceof MapType && update instanceof MapType) {
+            return new MapType(
+                    base.isNullable(),
+                    merge(
+                            ((MapType) base).getKeyType(),
+                            ((MapType) update).getKeyType(),
+                            highestFieldId),
+                    merge(
+                            ((MapType) base).getValueType(),
+                            ((MapType) update).getValueType(),
+                            highestFieldId));
+        } else if (base instanceof ArrayType && update instanceof ArrayType) {
+            return new ArrayType(
+                    base.isNullable(),
+                    merge(
+                            ((ArrayType) base).getElementType(),
+                            ((ArrayType) update).getElementType(),
+                            highestFieldId));
+        } else if (base instanceof MultisetType && update instanceof MultisetType) {
+            return new MultisetType(
+                    base.isNullable(),
+                    merge(
+                            ((MultisetType) base).getElementType(),
+                            ((MultisetType) update).getElementType(),
+                            highestFieldId));
+        } else if (base instanceof DecimalType && update instanceof DecimalType) {
+            if (base.equals(update)) {
+                return base0;
+            } else {
+                throw new UnsupportedOperationException(
+                        String.format(
+                                "Failed to merge decimal types with different precision or scale: %s and %s",
+                                base, update));
+            }
+        } else if (DataTypeCasts.supportsImplicitCast(base, update)) {

Review Comment:
   that needs an option if we want to support Explicit-Casting.



-- 
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: issues-unsubscribe@paimon.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-paimon] YannByron commented on a diff in pull request #1678: [spark] supports schema merging when write

Posted by "YannByron (via GitHub)" <gi...@apache.org>.
YannByron commented on code in PR #1678:
URL: https://github.com/apache/incubator-paimon/pull/1678#discussion_r1280042134


##########
paimon-core/src/main/java/org/apache/paimon/AbstractFileStore.java:
##########
@@ -142,6 +142,11 @@ public CoreOptions options() {
         return options;
     }
 
+    @Override
+    public boolean mergeSchema(RowType rowType, Boolean allowExplicitCast) {

Review Comment:
   yep, my fault.



-- 
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: issues-unsubscribe@paimon.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-paimon] YannByron commented on a diff in pull request #1678: [spark] supports schema merging when write

Posted by "YannByron (via GitHub)" <gi...@apache.org>.
YannByron commented on code in PR #1678:
URL: https://github.com/apache/incubator-paimon/pull/1678#discussion_r1278765909


##########
paimon-core/src/main/java/org/apache/paimon/schema/SchemaMergingUtils.java:
##########
@@ -0,0 +1,196 @@
+/*
+ * 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.paimon.schema;
+
+import org.apache.paimon.types.ArrayType;
+import org.apache.paimon.types.DataField;
+import org.apache.paimon.types.DataType;
+import org.apache.paimon.types.DataTypeCasts;
+import org.apache.paimon.types.DecimalType;
+import org.apache.paimon.types.ILength;
+import org.apache.paimon.types.IPrecision;
+import org.apache.paimon.types.MapType;
+import org.apache.paimon.types.MultisetType;
+import org.apache.paimon.types.ReassignFieldId;
+import org.apache.paimon.types.RowType;
+
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+/** The util class for merging the schemas. */
+public class SchemaMergingUtils {

Review Comment:
   >> Maybe we can merge this class to SchemaManager?
   Even though merge this to `SchemaManager`, all the methods will be `static`. i think there  is no different.
   
   >> And introduce a method to mergeSchema(RowType rowType) to FileStore?
   yes, better than the current `commitSchema`.



-- 
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: issues-unsubscribe@paimon.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-paimon] JingsongLi commented on a diff in pull request #1678: [spark] supports schema merging when write

Posted by "JingsongLi (via GitHub)" <gi...@apache.org>.
JingsongLi commented on code in PR #1678:
URL: https://github.com/apache/incubator-paimon/pull/1678#discussion_r1280025676


##########
paimon-core/src/main/java/org/apache/paimon/AbstractFileStore.java:
##########
@@ -142,6 +142,11 @@ public CoreOptions options() {
         return options;
     }
 
+    @Override
+    public boolean mergeSchema(RowType rowType, Boolean allowExplicitCast) {

Review Comment:
   All related call methods use `boolean`. (Looks like you're confusing Java and scala?)



-- 
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: issues-unsubscribe@paimon.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-paimon] JingsongLi commented on a diff in pull request #1678: [spark] supports schema merging when write

Posted by "JingsongLi (via GitHub)" <gi...@apache.org>.
JingsongLi commented on code in PR #1678:
URL: https://github.com/apache/incubator-paimon/pull/1678#discussion_r1278762072


##########
paimon-common/src/main/java/org/apache/paimon/CoreOptions.java:
##########
@@ -818,6 +818,13 @@ public class CoreOptions implements Serializable {
                                     + " the value should be the user configured local time zone. The option value is either a full name"
                                     + " such as 'America/Los_Angeles', or a custom timezone id such as 'GMT-08:00'.");
 
+    public static final ConfigOption<Boolean> MERGE_SCHEMA =

Review Comment:
   Maybe we can introduce a `SparkConnectorOptions`? This is only for spark.



-- 
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: issues-unsubscribe@paimon.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-paimon] YannByron commented on a diff in pull request #1678: [spark] supports schema merging when write

Posted by "YannByron (via GitHub)" <gi...@apache.org>.
YannByron commented on code in PR #1678:
URL: https://github.com/apache/incubator-paimon/pull/1678#discussion_r1278763517


##########
paimon-common/src/main/java/org/apache/paimon/CoreOptions.java:
##########
@@ -818,6 +818,13 @@ public class CoreOptions implements Serializable {
                                     + " the value should be the user configured local time zone. The option value is either a full name"
                                     + " such as 'America/Los_Angeles', or a custom timezone id such as 'GMT-08:00'.");
 
+    public static final ConfigOption<Boolean> MERGE_SCHEMA =

Review Comment:
   I think so.



-- 
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: issues-unsubscribe@paimon.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-paimon] JingsongLi commented on a diff in pull request #1678: [spark] supports schema merging when write

Posted by "JingsongLi (via GitHub)" <gi...@apache.org>.
JingsongLi commented on code in PR #1678:
URL: https://github.com/apache/incubator-paimon/pull/1678#discussion_r1279013433


##########
paimon-core/src/main/java/org/apache/paimon/schema/SchemaMergingUtils.java:
##########
@@ -0,0 +1,196 @@
+/*
+ * 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.paimon.schema;
+
+import org.apache.paimon.types.ArrayType;
+import org.apache.paimon.types.DataField;
+import org.apache.paimon.types.DataType;
+import org.apache.paimon.types.DataTypeCasts;
+import org.apache.paimon.types.DecimalType;
+import org.apache.paimon.types.ILength;
+import org.apache.paimon.types.IPrecision;
+import org.apache.paimon.types.MapType;
+import org.apache.paimon.types.MultisetType;
+import org.apache.paimon.types.ReassignFieldId;
+import org.apache.paimon.types.RowType;
+
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+/** The util class for merging the schemas. */
+public class SchemaMergingUtils {
+
+    public static TableSchema mergeSchemas(TableSchema currentTableSchema, RowType dataFields) {
+        if (currentTableSchema.logicalRowType().equals(dataFields)) {
+            return currentTableSchema;
+        }
+
+        AtomicInteger highestFieldId = new AtomicInteger(currentTableSchema.highestFieldId());
+        RowType newRowType =
+                mergeSchemas(currentTableSchema.logicalRowType(), dataFields, highestFieldId);
+        return new TableSchema(
+                currentTableSchema.id() + 1,
+                newRowType.getFields(),
+                highestFieldId.get(),
+                currentTableSchema.partitionKeys(),
+                currentTableSchema.primaryKeys(),
+                currentTableSchema.options(),
+                currentTableSchema.comment());
+    }
+
+    public static RowType mergeSchemas(
+            RowType tableSchema, RowType dataSchema, AtomicInteger highestFieldId) {
+        return (RowType) merge(tableSchema, dataSchema, highestFieldId);
+    }
+
+    /**
+     * Merge the base data type and the update data type if possible.
+     *
+     * <p>For RowType, find the fields which exists in both the base schema and the update schema,
+     * and try to merge them by calling the method iteratively; remain those fields that are only in
+     * the base schema and append those fields that are only in the update schema.
+     *
+     * <p>For other complex type, try to merge the element types.
+     *
+     * <p>For primitive data type, we treat that's compatible if the original type can be safely
+     * cast to the new type.
+     */
+    static DataType merge(DataType base0, DataType update0, AtomicInteger highestFieldId) {
+        // Here we try t0 merge the base0 and update0 without regard to the nullability,
+        // and set the base0's nullability to the return's.
+        DataType base = base0.copy(true);
+        DataType update = update0.copy(true);
+
+        if (base.equals(update)) {
+            return base0;
+        } else if (base instanceof RowType && update instanceof RowType) {

Review Comment:
   Sounds reasonable to me.



-- 
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: issues-unsubscribe@paimon.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-paimon] JingsongLi commented on a diff in pull request #1678: [spark] supports schema merging when write

Posted by "JingsongLi (via GitHub)" <gi...@apache.org>.
JingsongLi commented on code in PR #1678:
URL: https://github.com/apache/incubator-paimon/pull/1678#discussion_r1280025676


##########
paimon-core/src/main/java/org/apache/paimon/AbstractFileStore.java:
##########
@@ -142,6 +142,11 @@ public CoreOptions options() {
         return options;
     }
 
+    @Override
+    public boolean mergeSchema(RowType rowType, Boolean allowExplicitCast) {

Review Comment:
   All related call links use `boolean`. (Looks like you're confusing Java and scala?)



-- 
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: issues-unsubscribe@paimon.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org