LuciferYang commented on code in PR #14295:
URL: https://github.com/apache/gravitino/pull/14295#discussion_r4490566137
##########
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:
Resolved in e41649408 the way you describe: both `getHivePartitionName`
overloads keep the lookup by name and pass `value == null ?
SparkPartitionUtils.HIVE_DEFAULT_PARTITION_NAME : value` to
`partitionNamePart`, and the new cases now live in the test class from #13997.
##########
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:
Added in 1767aae92: `testPartitionExistsWithNullInPartialSpec` checks that
`partitionExists` with a partial spec `region = null` asks for
`region=__HIVE_DEFAULT_PARTITION__`, and
`testNullValueInNonStringColumnUsesHiveDefaultMarker` checks the name
`loadPartitionMetadata` builds.
##########
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:
Added in 1767aae92: `testNullValueInNonStringColumnUsesHiveDefaultMarker`
puts the null in the `INT` column `dt`, and checks that `createPartition` sends
the marker and `loadPartitionMetadata` asks for
`dt=__HIVE_DEFAULT_PARTITION__/region=us`.
--
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]