This is an automated email from the ASF dual-hosted git repository.
lzljs3620320 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-paimon.git
The following commit(s) were added to refs/heads/master by this push:
new 0c117cce7 [core] Add validation in Schema#Builder (#1030)
0c117cce7 is described below
commit 0c117cce75adea52da796aff51c04419eddbccf9
Author: yuzelin <[email protected]>
AuthorDate: Fri May 5 13:47:21 2023 +0800
[core] Add validation in Schema#Builder (#1030)
---
.../main/java/org/apache/paimon/schema/Schema.java | 28 +++++++++
.../apache/paimon/schema/SchemaBuilderTest.java | 67 ++++++++++++++++++++++
2 files changed, 95 insertions(+)
diff --git a/paimon-core/src/main/java/org/apache/paimon/schema/Schema.java
b/paimon-core/src/main/java/org/apache/paimon/schema/Schema.java
index cd90a5d51..cecec0f58 100644
--- a/paimon-core/src/main/java/org/apache/paimon/schema/Schema.java
+++ b/paimon-core/src/main/java/org/apache/paimon/schema/Schema.java
@@ -28,6 +28,7 @@ import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@@ -94,7 +95,22 @@ public class Schema {
private static List<DataField> normalizeFields(
List<DataField> fields, List<String> primaryKeys, List<String>
partitionKeys) {
List<String> fieldNames =
fields.stream().map(DataField::name).collect(Collectors.toList());
+
+ Set<String> duplicateColumns = duplicate(fieldNames);
+ Preconditions.checkState(
+ duplicateColumns.isEmpty(),
+ "Table column %s must not contain duplicate fields. Found: %s",
+ fieldNames,
+ duplicateColumns);
+
Set<String> allFields = new HashSet<>(fieldNames);
+
+ duplicateColumns = duplicate(partitionKeys);
+ Preconditions.checkState(
+ duplicateColumns.isEmpty(),
+ "Partition key constraint %s must not contain duplicate
columns. Found: %s",
+ partitionKeys,
+ duplicateColumns);
Preconditions.checkState(
allFields.containsAll(partitionKeys),
"Table column %s should include all partition fields %s",
@@ -104,6 +120,12 @@ public class Schema {
if (primaryKeys.isEmpty()) {
return fields;
}
+ duplicateColumns = duplicate(primaryKeys);
+ Preconditions.checkState(
+ duplicateColumns.isEmpty(),
+ "Primary key constraint %s must not contain duplicate columns.
Found: %s",
+ primaryKeys,
+ duplicateColumns);
Preconditions.checkState(
allFields.containsAll(primaryKeys),
"Table column %s should include all primary key constraint %s",
@@ -133,6 +155,12 @@ public class Schema {
return newFields;
}
+ private static Set<String> duplicate(List<String> names) {
+ return names.stream()
+ .filter(name -> Collections.frequency(names, name) > 1)
+ .collect(Collectors.toSet());
+ }
+
@Override
public boolean equals(Object o) {
if (this == o) {
diff --git
a/paimon-core/src/test/java/org/apache/paimon/schema/SchemaBuilderTest.java
b/paimon-core/src/test/java/org/apache/paimon/schema/SchemaBuilderTest.java
new file mode 100644
index 000000000..864b77b67
--- /dev/null
+++ b/paimon-core/src/test/java/org/apache/paimon/schema/SchemaBuilderTest.java
@@ -0,0 +1,67 @@
+/*
+ * 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.testutils.assertj.AssertionUtils;
+import org.apache.paimon.types.DataTypes;
+
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
+
+/** Test for {@link Schema.Builder}. */
+public class SchemaBuilderTest {
+
+ @Test
+ public void testDuplicateColumns() {
+ Schema.Builder builder =
+ Schema.newBuilder().column("id", DataTypes.INT()).column("id",
DataTypes.INT());
+
+ assertThatThrownBy(builder::build)
+ .satisfies(
+ AssertionUtils.anyCauseMatches(
+ IllegalStateException.class,
+ "Table column [id, id] must not contain
duplicate fields. Found: [id]"));
+ }
+
+ @Test
+ public void testDuplicatePrimaryKeys() {
+ Schema.Builder builder =
+ Schema.newBuilder().column("id",
DataTypes.INT()).primaryKey("id", "id");
+
+ assertThatThrownBy(builder::build)
+ .satisfies(
+ AssertionUtils.anyCauseMatches(
+ IllegalStateException.class,
+ "Primary key constraint [id, id] must not
contain duplicate columns. Found: [id]"));
+ }
+
+ @Test
+ public void testDuplicatePartitionKeys() {
+ Schema.Builder builder =
+ Schema.newBuilder().column("id",
DataTypes.INT()).partitionKeys("id", "id");
+
+ assertThatThrownBy(builder::build)
+ .satisfies(
+ AssertionUtils.anyCauseMatches(
+ IllegalStateException.class,
+ "Partition key constraint [id, id] must not
contain duplicate columns. Found: [id]"
+ + ""));
+ }
+}