diqiu50 commented on code in PR #10829:
URL: https://github.com/apache/gravitino/pull/10829#discussion_r3158296860


##########
catalogs/catalog-glue/src/main/java/org/apache/gravitino/catalog/glue/GlueTableOperations.java:
##########
@@ -0,0 +1,266 @@
+/*
+ * 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.catalog.glue;
+
+import com.google.common.base.Preconditions;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import org.apache.gravitino.connector.TableOperations;
+import org.apache.gravitino.exceptions.NoSuchPartitionException;
+import org.apache.gravitino.exceptions.PartitionAlreadyExistsException;
+import org.apache.gravitino.rel.SupportsPartitions;
+import org.apache.gravitino.rel.expressions.literals.Literal;
+import org.apache.gravitino.rel.expressions.literals.Literals;
+import org.apache.gravitino.rel.partitions.IdentityPartition;
+import org.apache.gravitino.rel.partitions.Partition;
+import org.apache.gravitino.rel.partitions.Partitions;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import software.amazon.awssdk.services.glue.GlueClient;
+import software.amazon.awssdk.services.glue.model.AlreadyExistsException;
+import software.amazon.awssdk.services.glue.model.CreatePartitionRequest;
+import software.amazon.awssdk.services.glue.model.DeletePartitionRequest;
+import software.amazon.awssdk.services.glue.model.EntityNotFoundException;
+import software.amazon.awssdk.services.glue.model.GetPartitionRequest;
+import software.amazon.awssdk.services.glue.model.GetPartitionsRequest;
+import software.amazon.awssdk.services.glue.model.GetPartitionsResponse;
+import software.amazon.awssdk.services.glue.model.GlueException;
+import software.amazon.awssdk.services.glue.model.PartitionInput;
+import software.amazon.awssdk.services.glue.model.StorageDescriptor;
+
+/**
+ * Table-level partition operations for the AWS Glue Data Catalog.
+ *
+ * <p>Implements {@link SupportsPartitions} for Hive-format 
identity-partitioned tables. Partition
+ * names follow the Hive convention: {@code col=val/col2=val2}.
+ *
+ * <p>Only {@link IdentityPartition} is supported because the Glue partition 
model is Hive-style
+ * key=value, which maps directly to identity partitions. Other partition 
types (bucket, truncate,
+ * etc.) have no equivalent representation in the Glue partition API.
+ */
+class GlueTableOperations implements TableOperations, SupportsPartitions {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(GlueTableOperations.class);
+
+  private final GlueClient glueClient;
+  /** Nullable — when null, Glue uses the caller's AWS account ID. */
+  private final String catalogId;
+
+  private final String dbName;
+  private final String tableName;
+  /** Ordered partition column names, matching the table's {@code 
partitionKeys()} order. */
+  private final String[] partitionColNames;
+
+  GlueTableOperations(
+      GlueClient glueClient,
+      String catalogId,
+      String dbName,
+      String tableName,
+      String[] partitionColNames) {
+    this.glueClient = glueClient;
+    this.catalogId = catalogId;
+    this.dbName = dbName;
+    this.tableName = tableName;
+    this.partitionColNames = partitionColNames;
+  }
+
+  @Override
+  public String[] listPartitionNames() {
+    List<String> names = new ArrayList<>();
+    String nextToken = null;
+    try {
+      do {
+        GetPartitionsRequest.Builder req =
+            
GetPartitionsRequest.builder().databaseName(dbName).tableName(tableName);
+        GlueCatalogOperations.applyCatalogId(catalogId, req::catalogId);
+        if (nextToken != null) req.nextToken(nextToken);
+        GetPartitionsResponse resp = glueClient.getPartitions(req.build());
+        for (software.amazon.awssdk.services.glue.model.Partition p : 
resp.partitions()) {
+          names.add(buildPartitionName(p.values()));
+        }
+        nextToken = resp.nextToken();
+      } while (nextToken != null);
+    } catch (GlueException e) {
+      throw new RuntimeException("Failed to list partitions for table " + 
tableName, e);
+    }
+    return names.toArray(new String[0]);
+  }
+
+  @Override
+  public Partition[] listPartitions() {
+    List<Partition> partitions = new ArrayList<>();
+    String nextToken = null;
+    try {
+      do {
+        GetPartitionsRequest.Builder req =
+            
GetPartitionsRequest.builder().databaseName(dbName).tableName(tableName);
+        GlueCatalogOperations.applyCatalogId(catalogId, req::catalogId);
+        if (nextToken != null) req.nextToken(nextToken);
+        GetPartitionsResponse resp = glueClient.getPartitions(req.build());
+        for (software.amazon.awssdk.services.glue.model.Partition p : 
resp.partitions()) {
+          partitions.add(toGravitinoPartition(p));
+        }
+        nextToken = resp.nextToken();
+      } while (nextToken != null);
+    } catch (GlueException e) {
+      throw new RuntimeException("Failed to list partitions for table " + 
tableName, e);
+    }
+    return partitions.toArray(new Partition[0]);
+  }
+
+  @Override
+  public Partition getPartition(String partitionName) throws 
NoSuchPartitionException {
+    List<String> values = parsePartitionName(partitionName);
+    GetPartitionRequest.Builder req =
+        GetPartitionRequest.builder()
+            .databaseName(dbName)
+            .tableName(tableName)
+            .partitionValues(values);
+    GlueCatalogOperations.applyCatalogId(catalogId, req::catalogId);
+    try {
+      return 
toGravitinoPartition(glueClient.getPartition(req.build()).partition());
+    } catch (EntityNotFoundException e) {
+      throw new NoSuchPartitionException(
+          e, "Partition %s does not exist in table %s", partitionName, 
tableName);
+    } catch (GlueException e) {
+      throw new RuntimeException("Failed to get partition " + partitionName, 
e);
+    }
+  }
+
+  @Override
+  public Partition addPartition(Partition partition) throws 
PartitionAlreadyExistsException {
+    Preconditions.checkArgument(
+        partition instanceof IdentityPartition, "Glue only supports identity 
partitions");
+    IdentityPartition ip = (IdentityPartition) partition;
+    Preconditions.checkArgument(
+        ip.values().length == partitionColNames.length,
+        "Partition values count (%s) must match partition columns count (%s)",
+        ip.values().length,
+        partitionColNames.length);
+
+    List<String> values = new ArrayList<>(ip.values().length);
+    for (Literal<?> v : ip.values()) {
+      values.add(v.value() != null ? v.value().toString() : null);
+    }
+
+    PartitionInput input =

Review Comment:
   fixed



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