FANNG1 commented on code in PR #7067:
URL: https://github.com/apache/gravitino/pull/7067#discussion_r2078785529
##########
spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/hive/SparkHiveCatalogIT.java:
##########
@@ -129,6 +129,41 @@ void testCreateHiveFormatPartitionTable() {
checkPartitionDirExists(tableInfo);
}
+ @Test
+ void testManagePartitionTable() {
+ String tableName = "hive_partition_ops_table";
+
+ dropTableIfExists(tableName);
+ String createTableSQL = getCreateSimpleTableString(tableName);
+ createTableSQL = createTableSQL + "PARTITIONED BY (age_p1 INT, age_p2
STRING)";
+ sql(createTableSQL);
+
+ List<Object[]> partitionInfo = getTablePartitions(tableName);
+ Assertions.assertEquals(0, partitionInfo.size());
+
+ sql("ALTER TABLE " + tableName + " ADD PARTITION (age_p1=20,
age_p2='twenty')");
+ sql("ALTER TABLE " + tableName + " ADD PARTITION (age_p1=21,
age_p2='twenty one')");
+ partitionInfo = getTablePartitions(tableName);
+ Assertions.assertEquals(2, partitionInfo.size());
+ Assertions.assertEquals("age_p1=20/age_p2=twenty",
partitionInfo.get(0)[0]);
+ Assertions.assertEquals("age_p1=21/age_p2=twenty one",
partitionInfo.get(1)[0]);
Review Comment:
Besides showing all partitions, could you get partial partition like `SHOW
PARTITIONS xx PARTITION ( age_p1=20)`?
##########
spark-connector/spark-common/src/main/java/org/apache/gravitino/spark/connector/utils/HiveGravitinoOperationOperator.java:
##########
@@ -0,0 +1,142 @@
+/*
+ * 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 java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import org.apache.gravitino.exceptions.NoSuchPartitionException;
+import org.apache.gravitino.rel.expressions.literals.Literal;
+import org.apache.gravitino.rel.partitions.Partition;
+import org.apache.gravitino.rel.partitions.Partitions;
+import org.apache.spark.sql.catalyst.InternalRow;
+import org.apache.spark.sql.catalyst.expressions.GenericInternalRow;
+import org.apache.spark.sql.types.DataType;
+import org.apache.spark.sql.types.StructField;
+import org.apache.spark.sql.types.StructType;
+import org.jetbrains.annotations.NotNull;
+
+public class HiveGravitinoOperationOperator {
+
+ private org.apache.gravitino.rel.Table gravitinoTable;
+ private static final String PARTITION_NAME_DELIMITER = "/";
+ private static final String PARTITION_VALUE_DELIMITER = "=";
+
+ public HiveGravitinoOperationOperator(org.apache.gravitino.rel.Table
gravitinoTable) {
+ this.gravitinoTable = gravitinoTable;
+ }
+
+ public void createPartition(
+ InternalRow ident, Map<String, String> properties, StructType
partitionSchema) {
+ List<String[]> fields = new ArrayList<>();
+ List<Literal<?>> values = new ArrayList<>();
+
+ int numFields = ident.numFields();
+ for (int i = 0; i < numFields; i++) {
+ StructField structField = partitionSchema.apply(i);
+ DataType dataType = structField.dataType();
+ fields.add(new String[] {structField.name()});
+ values.add(SparkPartitionUtils.toGravitinoLiteral(ident, i, dataType));
+ }
+
+ Partition partition =
+ Partitions.identity(
+ null, fields.toArray(new String[0][0]), values.toArray(new
Literal[0]), properties);
+
+ gravitinoTable.supportPartitions().addPartition(partition);
+ }
+
+ public boolean dropPartition(InternalRow ident, StructType partitionSchema) {
+ String partitionName = getPartitionName(ident, partitionSchema);
+ return gravitinoTable.supportPartitions().dropPartition(partitionName);
+ }
+
+ public InternalRow[] listPartitionIdentifiers(
+ String[] names, InternalRow ident, StructType partitionSchema) {
+ // Get all partitions
+ if (names != null && names.length == 0) {
+ String[] allPartitions =
gravitinoTable.supportPartitions().listPartitionNames();
+ return Arrays.stream(allPartitions)
+ .map(
+ e -> {
+ String[] splits = e.split(PARTITION_NAME_DELIMITER);
Review Comment:
Could you use a func like `toSparkPartition` to encapsulation this logic?
rename `getPartitionName` to `toHivePartitionName`?
##########
spark-connector/spark-common/src/main/java/org/apache/gravitino/spark/connector/utils/HiveGravitinoOperationOperator.java:
##########
@@ -0,0 +1,142 @@
+/*
+ * 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 java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import org.apache.gravitino.exceptions.NoSuchPartitionException;
+import org.apache.gravitino.rel.expressions.literals.Literal;
+import org.apache.gravitino.rel.partitions.Partition;
+import org.apache.gravitino.rel.partitions.Partitions;
+import org.apache.spark.sql.catalyst.InternalRow;
+import org.apache.spark.sql.catalyst.expressions.GenericInternalRow;
+import org.apache.spark.sql.types.DataType;
+import org.apache.spark.sql.types.StructField;
+import org.apache.spark.sql.types.StructType;
+import org.jetbrains.annotations.NotNull;
+
+public class HiveGravitinoOperationOperator {
+
+ private org.apache.gravitino.rel.Table gravitinoTable;
+ private static final String PARTITION_NAME_DELIMITER = "/";
+ private static final String PARTITION_VALUE_DELIMITER = "=";
+
+ public HiveGravitinoOperationOperator(org.apache.gravitino.rel.Table
gravitinoTable) {
+ this.gravitinoTable = gravitinoTable;
+ }
+
+ public void createPartition(
+ InternalRow ident, Map<String, String> properties, StructType
partitionSchema) {
+ List<String[]> fields = new ArrayList<>();
+ List<Literal<?>> values = new ArrayList<>();
+
+ int numFields = ident.numFields();
+ for (int i = 0; i < numFields; i++) {
+ StructField structField = partitionSchema.apply(i);
+ DataType dataType = structField.dataType();
+ fields.add(new String[] {structField.name()});
+ values.add(SparkPartitionUtils.toGravitinoLiteral(ident, i, dataType));
+ }
+
+ Partition partition =
+ Partitions.identity(
+ null, fields.toArray(new String[0][0]), values.toArray(new
Literal[0]), properties);
+
+ gravitinoTable.supportPartitions().addPartition(partition);
Review Comment:
Could you process Gravitino `PartitionAlreadyExistsException`?
##########
spark-connector/spark-common/src/main/java/org/apache/gravitino/spark/connector/hive/SparkHiveTable.java:
##########
@@ -76,4 +83,55 @@ public Map<String, String> properties() {
public Transform[] partitioning() {
return gravitinoTableInfoHelper.partitioning();
}
+
Review Comment:
could you override `partitionExists` to avoid invoking
`listPartitionIdentifiers` in Spark side.
```java
default boolean partitionExists(InternalRow ident) {
String[] partitionNames = partitionSchema().names();
if (ident.numFields() == partitionNames.length) {
return listPartitionIdentifiers(partitionNames, ident).length > 0;
} else {
throw new IllegalArgumentException("The number of fields (" +
ident.numFields() +
") in the partition identifier is not equal to the partition
schema length (" +
partitionNames.length + "). The identifier might not refer to one
partition.");
}
}
```
--
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]