FANNG1 commented on code in PR #7067:
URL: https://github.com/apache/gravitino/pull/7067#discussion_r2086837071


##########
spark-connector/spark-common/src/main/java/org/apache/gravitino/spark/connector/utils/HiveGravitinoOperationOperator.java:
##########
@@ -0,0 +1,157 @@
+/*
+ * 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.analysis.PartitionAlreadyExistsException;
+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)
+      throws PartitionAlreadyExistsException {
+    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);
+
+    try {
+      gravitinoTable.supportPartitions().addPartition(partition);
+    } catch (org.apache.gravitino.exceptions.PartitionAlreadyExistsException 
e) {
+      throw new 
org.apache.spark.sql.catalyst.analysis.PartitionAlreadyExistsException(
+          e.getMessage());
+    }
+  }
+
+  public boolean dropPartition(InternalRow ident, StructType partitionSchema) {
+    String partitionName = getHivePartitionName(ident, partitionSchema);
+    return gravitinoTable.supportPartitions().dropPartition(partitionName);
+  }
+
+  public InternalRow[] listPartitionIdentifiers(
+      String[] names, InternalRow ident, StructType partitionSchema) {
+    // Get all partitions
+    String[] allPartitions = 
gravitinoTable.supportPartitions().listPartitionNames();
+
+    boolean isNeedAll = names != null && names.length == 0 ? true : false;
+    String partitionName = getHivePartitionName(names, ident, partitionSchema);
+    return Arrays.stream(allPartitions)
+        .filter(e -> isNeedAll ? true : e.contains(partitionName))

Review Comment:
   we should do match the partition key and value exactly.  contains may 
leading wrong partition like there are two partitions  `p=A`  and `p=A1`, `show 
partitions xx partition(p=A)` should get `p=A` exactly.



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