jerryshao commented on code in PR #14295:
URL: https://github.com/apache/gravitino/pull/14295#discussion_r4490076822


##########
spark-connector/spark-common/src/main/java/org/apache/gravitino/spark/connector/utils/HiveGravitinoOperationOperator.java:
##########
@@ -156,13 +161,16 @@ private InternalRow toSparkPartition(String 
partitionName, StructType partitionS
       String[] names, InternalRow ident, StructType partitionSchema) {
     StringBuilder partitionName = new StringBuilder();
     Preconditions.checkArgument(names != null, "Partition column names must 
not be null");
+    // names can be any subset of the partition columns, so look each column 
up by name.
     for (int i = 0; i < names.length; i++) {
-      StructField structField = partitionSchema.apply(i);
+      StructField structField = partitionSchema.apply(names[i]);
       DataType dataType = structField.dataType();
+      String value = SparkPartitionUtils.getPartitionValueAsString(ident, i, 
dataType);
+      // Hive and Spark write a null partition value as 
__HIVE_DEFAULT_PARTITION__.
       partitionName.append(
           names[i]
               + PARTITION_VALUE_DELIMITER
-              + SparkPartitionUtils.getPartitionValueAsString(ident, i, 
dataType));
+              + (value == null ? 
SparkPartitionUtils.HIVE_DEFAULT_PARTITION_NAME : value));

Review Comment:
   Heads up: this PR currently conflicts with `main`. #13997 was merged and 
rewrote both `getHivePartitionName` overloads to go through 
`partitionNamePart(...)`, which escapes with 
`ExternalCatalogUtils.escapePathName(String.valueOf(value))`, and it also added 
a test class at the same path as the new 
`TestHiveGravitinoOperationOperator.java` here.
   
   When rebasing, I think the resolved code should keep the by-name column 
lookup and substitute the marker before escaping, e.g. 
`partitionNamePart(names[i], value == null ? 
SparkPartitionUtils.HIVE_DEFAULT_PARTITION_NAME : value)`, otherwise 
`String.valueOf(null)` brings `dt=null` back. The marker only contains letters 
and underscores, so escaping does not change it. The two test classes will also 
need to be merged into one.



##########
spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/utils/TestHiveGravitinoOperationOperator.java:
##########
@@ -0,0 +1,95 @@
+/*
+ *  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.spark.connector.utils;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.Collections;
+import org.apache.gravitino.rel.SupportsPartitions;
+import org.apache.gravitino.rel.Table;
+import org.apache.gravitino.rel.expressions.literals.Literal;
+import org.apache.gravitino.rel.partitions.IdentityPartition;
+import org.apache.gravitino.rel.partitions.Partition;
+import org.apache.spark.sql.catalyst.InternalRow;
+import org.apache.spark.sql.catalyst.expressions.GenericInternalRow;
+import org.apache.spark.sql.types.DataTypes;
+import org.apache.spark.sql.types.StructType;
+import org.apache.spark.unsafe.types.UTF8String;
+import org.junit.jupiter.api.Test;
+import org.mockito.ArgumentCaptor;
+
+/** Tests for {@link HiveGravitinoOperationOperator} partition-name building. 
*/
+public class TestHiveGravitinoOperationOperator {
+
+  private static final StructType SCHEMA =
+      new StructType()
+          .add("dt", DataTypes.IntegerType, true)
+          .add("region", DataTypes.StringType, true);
+
+  @Test
+  void testNullPartitionValueUsesHiveDefaultMarker() {
+    SupportsPartitions partitions = mock(SupportsPartitions.class);
+    ArgumentCaptor<String> name = ArgumentCaptor.forClass(String.class);
+    when(partitions.dropPartition(name.capture())).thenReturn(true);
+
+    InternalRow ident = new GenericInternalRow(new Object[] {20240101, null});
+    operator(partitions).dropPartition(ident, SCHEMA);
+
+    assertEquals("dt=20240101/region=__HIVE_DEFAULT_PARTITION__", 
name.getValue());
+  }
+
+  @Test
+  void testAddNullPartitionValueUsesHiveDefaultMarker() throws Exception {
+    SupportsPartitions partitions = mock(SupportsPartitions.class);
+    ArgumentCaptor<Partition> partition = 
ArgumentCaptor.forClass(Partition.class);
+    when(partitions.addPartition(partition.capture())).thenReturn(null);
+
+    InternalRow ident = new GenericInternalRow(new Object[] {20240101, null});
+    operator(partitions).createPartition(ident, Collections.emptyMap(), 
SCHEMA);
+
+    Literal<?>[] values = ((IdentityPartition) partition.getValue()).values();
+    assertEquals(20240101, values[0].value());
+    assertEquals("__HIVE_DEFAULT_PARTITION__", values[1].value());
+  }
+
+  @Test
+  void testPartialSpecResolvesColumnsByName() {

Review Comment:
   The description says the marker is used for drop, exists, load and list, but 
the unit tests only cover drop, add and a partial-spec list. Could we add a 
case for `partitionExists` or `loadPartitionMetadata` with a null value (and 
ideally a partial spec containing a null)? Those go through the same two 
`getHivePartitionName` overloads, so a small test would keep someone from 
changing only one of them later.



##########
spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/utils/TestHiveGravitinoOperationOperator.java:
##########
@@ -0,0 +1,95 @@
+/*
+ *  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.spark.connector.utils;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.Collections;
+import org.apache.gravitino.rel.SupportsPartitions;
+import org.apache.gravitino.rel.Table;
+import org.apache.gravitino.rel.expressions.literals.Literal;
+import org.apache.gravitino.rel.partitions.IdentityPartition;
+import org.apache.gravitino.rel.partitions.Partition;
+import org.apache.spark.sql.catalyst.InternalRow;
+import org.apache.spark.sql.catalyst.expressions.GenericInternalRow;
+import org.apache.spark.sql.types.DataTypes;
+import org.apache.spark.sql.types.StructType;
+import org.apache.spark.unsafe.types.UTF8String;
+import org.junit.jupiter.api.Test;
+import org.mockito.ArgumentCaptor;
+
+/** Tests for {@link HiveGravitinoOperationOperator} partition-name building. 
*/
+public class TestHiveGravitinoOperationOperator {
+
+  private static final StructType SCHEMA =
+      new StructType()
+          .add("dt", DataTypes.IntegerType, true)
+          .add("region", DataTypes.StringType, true);
+
+  @Test
+  void testNullPartitionValueUsesHiveDefaultMarker() {

Review Comment:
   Both null cases use a string column. `createPartition` builds a string 
literal for a null of any type, so it would be good to also cover a null in a 
non-string partition column (e.g. an `INT` or `DATE` column) and assert the 
resulting name is `dt=__HIVE_DEFAULT_PARTITION__` without a type error.



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