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


##########
server/src/main/java/org/apache/gravitino/server/web/rest/StatisticOperations.java:
##########
@@ -200,4 +214,253 @@ public Response dropStatistics(
           OperationType.DROP, StringUtils.join(request.getNames(), ","), 
fullName, e);
     }
   }
+
+  @GET
+  @Path("/partitions")
+  @Produces("application/vnd.gravitino.v1+json")
+  @Timed(name = "list-partition-stats." + MetricNames.HTTP_PROCESS_DURATION, 
absolute = true)
+  @ResponseMetered(name = "list-partition-stats", absolute = true)
+  public Response listPartitionStatistics(
+      @PathParam("metalake") String metalake,
+      @PathParam("type") String type,
+      @PathParam("fullName") String fullName,
+      @QueryParam("from") String fromPartitionName,
+      @QueryParam("to") String toPartitionName,
+      @QueryParam("fromExclusive") @DefaultValue("false") boolean 
fromExclusive,
+      @QueryParam("toExclusive") @DefaultValue("false") boolean toExclusive) {
+
+    String formattedFromPartitionName =
+        getFormattedFromPartitionName(fromPartitionName, fromExclusive);
+    String formattedToPartitionName = 
getFormattedToPartitionName(toPartitionName, toExclusive);
+
+    LOG.info(
+        "Listing partition statistics for table: {} in the metalake {} from {} 
to {}",
+        fullName,
+        metalake,
+        formattedFromPartitionName,
+        formattedToPartitionName);
+    try {
+      return Utils.doAs(
+          httpRequest,
+          () -> {
+            MetadataObject object =
+                MetadataObjects.parse(
+                    fullName, 
MetadataObject.Type.valueOf(type.toUpperCase(Locale.ROOT)));
+            if (object.type() != MetadataObject.Type.TABLE) {
+              throw new UnsupportedOperationException(
+                  "Listing partition statistics is only supported for tables 
now.");
+            }
+
+            if (fromPartitionName == null && toPartitionName == null) {
+              throw new IllegalArgumentException(
+                  "Both 'from' and 'to' parameters cannot be null at the same 
time.");
+            }
+
+            MetadataObjectUtil.checkMetadataObject(metalake, object);
+
+            PartitionRange range;
+            PartitionRange.BoundType fromBoundType = 
getFromBoundType(fromExclusive);
+            PartitionRange.BoundType toBoundType = 
getFromBoundType(toExclusive);
+            if (fromPartitionName != null && toPartitionName != null) {
+              range =
+                  PartitionRange.between(
+                      fromPartitionName, fromBoundType, toPartitionName, 
toBoundType);
+            } else if (fromPartitionName != null) {
+              range = PartitionRange.downTo(fromPartitionName, fromBoundType);
+            } else {
+              range = PartitionRange.upTo(toPartitionName, toBoundType);
+            }
+
+            List<PartitionStatistics> statistics =
+                statisticManager.listPartitionStatistics(metalake, object, 
range);
+
+            PartitionStatisticsDTO[] partitionStatistics =
+                statistics.stream()
+                    .map(
+                        partitionStatistic ->
+                            PartitionStatisticsDTO.of(
+                                partitionStatistic.partitionName(),
+                                
DTOConverters.toDTOs(partitionStatistic.statistics())))
+                    .toArray(PartitionStatisticsDTO[]::new);
+
+            return Utils.ok(new 
PartitionStatisticsListResponse(partitionStatistics));
+          });
+    } catch (Exception e) {
+      LOG.error(
+          "Error listing {},{} partition statistics for table: {} in the 
metalake {}.",
+          formattedFromPartitionName,
+          formattedToPartitionName,
+          fullName,
+          metalake,
+          e);
+      return ExceptionHandlers.handlePartitionStatsException(
+          OperationType.LIST,
+          formattedFromPartitionName + "," + formattedToPartitionName,
+          fullName,
+          e);
+    }
+  }
+
+  @PUT
+  @Path("/partitions")
+  @Produces("application/vnd.gravitino.v1+json")
+  @Timed(name = "update-partitions-stats." + 
MetricNames.HTTP_PROCESS_DURATION, absolute = true)
+  @ResponseMetered(name = "update-partitions-stats", absolute = true)
+  public Response updatePartitionStatistics(
+      @PathParam("metalake") String metalake,
+      @PathParam("type") String type,
+      @PathParam("fullName") String fullName,
+      PartitionStatisticsUpdateRequest request) {
+    LOG.info("Updating partition statistics for table: {} in the metalake {}", 
fullName, metalake);
+    try {
+      return Utils.doAs(
+          httpRequest,
+          () -> {
+            request.validate();
+
+            MetadataObject object =
+                MetadataObjects.parse(
+                    fullName, 
MetadataObject.Type.valueOf(type.toUpperCase(Locale.ROOT)));
+            if (object.type() != MetadataObject.Type.TABLE) {
+              throw new UnsupportedOperationException(
+                  "Updating partition statistics is only supported for tables 
now.");
+            }
+
+            List<PartitionStatisticsUpdateDTO> updates = request.getUpdates();
+            for (PartitionStatisticsUpdateDTO update : updates) {
+              update
+                  .statistics()
+                  .keySet()
+                  .forEach(
+                      statistic -> {
+                        if (!statistic.startsWith(Statistic.CUSTOM_PREFIX)) {
+                          // Current we only support custom statistics
+                          throw new IllegalStatisticNameException(

Review Comment:
   Did you handle this exception in exception handle?



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