Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#7975] improvement(validateFieldExistence): empty or null check for fieldName (#7990)

Sambhavi Pandey813ca6587b4e
diff --git a/common/src/main/java/org/apache/gravitino/dto/rel/PartitionUtils.java b/common/src/main/java/org/apache/gravitino/dto/rel/PartitionUtils.java
index 603a15818c..e7655d45fe 100644
--- a/common/src/main/java/org/apache/gravitino/dto/rel/PartitionUtils.java
+++ b/common/src/main/java/org/apache/gravitino/dto/rel/PartitionUtils.java
@@ -38,6 +38,8 @@ public class PartitionUtils {
public static void validateFieldExistence(ColumnDTO[] columns, String[] fieldName)
throws IllegalArgumentException {
Preconditions.checkArgument(ArrayUtils.isNotEmpty(columns), "columns cannot be null or empty");
+ Preconditions.checkArgument(
+ ArrayUtils.isNotEmpty(fieldName), "fieldName cannot be null or empty");
List<ColumnDTO> partitionColumn =
Arrays.stream(columns)
diff --git a/common/src/test/java/org/apache/gravitino/dto/rel/TestPartitionUtils.java b/common/src/test/java/org/apache/gravitino/dto/rel/TestPartitionUtils.java
new file mode 100644
index 0000000000..840cfd7bbf
--- /dev/null
+++ b/common/src/test/java/org/apache/gravitino/dto/rel/TestPartitionUtils.java
@@ -0,0 +1,75 @@
+/*
+ * 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.gravitino.dto.rel;
+
+import org.apache.gravitino.rel.types.Types;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class TestPartitionUtils {
+
+ private static ColumnDTO column(String name) {
+ return ColumnDTO.builder().withName(name).withDataType(Types.StringType.get()).build();
+ }
+
+ @Test
+ void testValidateFieldExistence_success() {
+ ColumnDTO[] columns = new ColumnDTO[] {column("id"), column("name")};
+ String[] field = new String[] {"name"};
+
+ Assertions.assertDoesNotThrow(() -> PartitionUtils.validateFieldExistence(columns, field));
+ }
+
+ @Test
+ void testValidateFieldExistence_caseInsensitive() {
+ ColumnDTO[] columns = new ColumnDTO[] {column("UserId")};
+ String[] field = new String[] {"userid"};
+
+ Assertions.assertDoesNotThrow(() -> PartitionUtils.validateFieldExistence(columns, field));
+ }
+
+ @Test
+ void testValidateFieldExistence_nullColumns() {
+ IllegalArgumentException ex =
+ Assertions.assertThrows(
+ IllegalArgumentException.class,
+ () -> PartitionUtils.validateFieldExistence(null, new String[] {"a"}));
+ Assertions.assertEquals("columns cannot be null or empty", ex.getMessage());
+ }
+
+ @Test
+ void testValidateFieldExistence_emptyFieldName() {
+ IllegalArgumentException ex =
+ Assertions.assertThrows(
+ IllegalArgumentException.class,
+ () -> PartitionUtils.validateFieldExistence(new ColumnDTO[] {column("a")}, null));
+ Assertions.assertEquals("fieldName cannot be null or empty", ex.getMessage());
+ }
+
+ @Test
+ void testValidateFieldExistence_fieldNotFound() {
+ ColumnDTO[] columns = new ColumnDTO[] {column("a"), column("b")};
+ IllegalArgumentException ex =
+ Assertions.assertThrows(
+ IllegalArgumentException.class,
+ () -> PartitionUtils.validateFieldExistence(columns, new String[] {"c"}));
+ Assertions.assertEquals("Field 'c' not found in table", ex.getMessage());
+ }
+}