Copilot commented on code in PR #6311:
URL: https://github.com/apache/hive/pull/6311#discussion_r2785401859


##########
standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/HMSHandler.java:
##########
@@ -5299,116 +4888,52 @@ public int get_num_partitions_by_filter(final String 
dbName,
     return ret;
   }
 
-  private int getNumPartitionsByPs(final String catName, final String dbName,
-                                   final String tblName, List<String> partVals)
-          throws TException {
-    String[] parsedDbName = parseDbName(dbName, conf);
-    startTableFunction("getNumPartitionsByPs", parsedDbName[CAT_NAME],
-            parsedDbName[DB_NAME], tblName);
-
-    int ret = -1;
-    Exception ex = null;
-    try {
-      ret = getMS().getNumPartitionsByPs(catName, dbName, tblName, partVals);
-    } catch (Exception e) {
-      ex = e;
-      rethrowException(e);
-    } finally {
-      endFunction("getNumPartitionsByPs", ret != -1, ex, tblName);
-    }
-    return ret;
-  }
-
   @Override
   @Deprecated
   public List<Partition> get_partitions_by_names(final String dbName, final 
String tblName,
                                                  final List<String> partNames)
       throws TException {
-    return get_partitions_by_names(dbName, tblName, false, null, null, null,
-        new 
GetPartitionsArgs.GetPartitionsArgsBuilder().partNames(partNames).build());
+    if (partNames == null) {
+      throw new MetaException("The partNames is null");
+    }
+    String[] dbNameParts = parseDbName(dbName, conf);
+    TableName tableName = new TableName(dbNameParts[CAT_NAME], 
dbNameParts[DB_NAME], tblName);
+    return GetPartitionsHandler.getPartitionsResult(
+        t ->  startTableFunction("get_partitions_by_names", t.getCat(), 
t.getDb(), t.getTable()),
+        ex -> endFunction("get_partitions_by_names", ex == null, ex, 
tableName.toString()),
+        this, tableName,
+        new 
GetPartitionsArgs.GetPartitionsArgsBuilder().partNames(partNames).build()).result();
   }
 
   @Override
   public GetPartitionsByNamesResult 
get_partitions_by_names_req(GetPartitionsByNamesRequest gpbnr)
       throws TException {
-    List<Partition> partitions = get_partitions_by_names(gpbnr.getDb_name(),
-        gpbnr.getTbl_name(),
-        gpbnr.isSetGet_col_stats() && gpbnr.isGet_col_stats(), 
gpbnr.getEngine(),
-        gpbnr.getProcessorCapabilities(), gpbnr.getProcessorIdentifier(),
-        new GetPartitionsArgs.GetPartitionsArgsBuilder()
+    if (gpbnr.getNames() == null) {
+      throw new MetaException("The names in GetPartitionsByNamesRequest is 
null");
+    }
+    String[] dbNameParts = parseDbName(gpbnr.getDb_name(), conf);
+    TableName tableName = new TableName(dbNameParts[CAT_NAME], 
dbNameParts[DB_NAME], gpbnr.getTbl_name());
+    GetPartitionsHandler.GetPartitionsRequest request =
+        new GetPartitionsHandler.GetPartitionsRequest(tableName, new 
GetPartitionsArgs.GetPartitionsArgsBuilder()
             
.partNames(gpbnr.getNames()).skipColumnSchemaForPartition(gpbnr.isSkipColumnSchemaForPartition())
             .excludeParamKeyPattern(gpbnr.getExcludeParamKeyPattern())
-            .includeParamKeyPattern(gpbnr.getIncludeParamKeyPattern())
-            .build());
-    GetPartitionsByNamesResult result = new 
GetPartitionsByNamesResult(partitions);
-    return result;
-  }
-
-  private List<Partition> get_partitions_by_names(final String dbName, final 
String tblName,
-      boolean getColStats, String engine,
-      List<String> processorCapabilities, String processorId,
-      GetPartitionsArgs args) throws TException {
-
-    String[] dbNameParts = parseDbName(dbName, conf);
-    String parsedCatName = dbNameParts[CAT_NAME];
-    String parsedDbName = dbNameParts[DB_NAME];
-    List<Partition> ret = null;
-    Table table = null;
+            
.includeParamKeyPattern(gpbnr.getIncludeParamKeyPattern()).build());
+    request.setEngine(gpbnr.getEngine());
+    request.setGetColStats(gpbnr.isSetGet_col_stats() && 
gpbnr.isGet_col_stats());
+    request.setProcessorId(gpbnr.getProcessorIdentifier());
+    request.setProcessorCapabilities(request.getProcessorCapabilities());

Review Comment:
   get_partitions_by_names_req() sets processor capabilities via 
request.setProcessorCapabilities(request.getProcessorCapabilities()), which is 
a no-op and drops the capabilities provided by the caller (gpbnr). This will 
prevent metadata translation behavior that depends on processorCapabilities. 
Please set it from gpbnr.getProcessorCapabilities().
   ```suggestion
       request.setProcessorCapabilities(gpbnr.getProcessorCapabilities());
   ```



##########
standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/handler/GetPartitionsHandler.java:
##########
@@ -0,0 +1,506 @@
+/*
+ * 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.hadoop.hive.metastore.handler;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.function.Consumer;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hive.common.StatsSetupConst;
+import org.apache.hadoop.hive.common.TableName;
+import org.apache.hadoop.hive.metastore.HMSHandler;
+import org.apache.hadoop.hive.metastore.IHMSHandler;
+import org.apache.hadoop.hive.metastore.MetaStoreFilterHook;
+import org.apache.hadoop.hive.metastore.RawStore;
+import org.apache.hadoop.hive.metastore.Warehouse;
+import org.apache.hadoop.hive.metastore.api.ColumnStatistics;
+import org.apache.hadoop.hive.metastore.api.FieldSchema;
+import org.apache.hadoop.hive.metastore.api.GetTableRequest;
+import org.apache.hadoop.hive.metastore.api.MetaException;
+import org.apache.hadoop.hive.metastore.api.NoSuchObjectException;
+import org.apache.hadoop.hive.metastore.api.Partition;
+import org.apache.hadoop.hive.metastore.api.PartitionValuesResponse;
+import org.apache.hadoop.hive.metastore.api.Table;
+import org.apache.hadoop.hive.metastore.client.builder.GetPartitionsArgs;
+import org.apache.hadoop.hive.metastore.conf.MetastoreConf;
+import org.apache.hadoop.hive.metastore.events.PreReadTableEvent;
+import org.apache.hadoop.hive.metastore.utils.FilterUtils;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static 
org.apache.hadoop.hive.metastore.ExceptionHandler.handleException;
+import static 
org.apache.hadoop.hive.metastore.HMSHandler.PARTITION_NUMBER_EXCEED_LIMIT_MSG;
+import static 
org.apache.hadoop.hive.metastore.utils.StringUtils.normalizeIdentifier;
+
+// Collect get partitions APIs together
+@SuppressWarnings({"unchecked", "rawtypes"})
+@RequestHandler(requestBody = GetPartitionsHandler.GetPartitionsRequest.class)
+public class GetPartitionsHandler<T> extends 
AbstractRequestHandler<GetPartitionsHandler.GetPartitionsRequest,
+    GetPartitionsHandler.GetPartitionsResult<T>> {
+  private static final Logger LOG = 
LoggerFactory.getLogger(GetPartitionsHandler.class);
+  private static final String NO_FILTER_STRING = "";
+  private RawStore rs;
+  private String catName;
+  private String dbName;
+  private String tblName;
+  private GetPartitionsArgs args;
+  private Table table;
+  private Configuration conf;
+  private GetPartitionsMethod getMethod;
+  private MetaStoreFilterHook filterHook;
+  private boolean isServerFilterEnabled;
+
+  enum GetPartitionsMethod {
+    EXPR, NAMES, FILTER, PART_VALS, ALL, VALUES
+  }
+
+  GetPartitionsHandler(IHMSHandler handler, GetPartitionsRequest request) {
+    super(handler, false, request);
+  }
+
+  @Override
+  protected void beforeExecute() throws TException, IOException {
+    this.args = request.getGetPartitionsArgs();
+    if (request.isGetPartitionValues()) {
+      getMethod = GetPartitionsMethod.VALUES;
+    } else if (args.getExpr() != null) {
+      getMethod = GetPartitionsMethod.EXPR;
+    } else if (args.getFilter() != null) {
+      getMethod = GetPartitionsMethod.FILTER;
+    } else if (args.getPartNames() != null) {
+      getMethod = GetPartitionsMethod.NAMES;
+    } else if (args.getPart_vals() != null) {
+      getMethod = GetPartitionsMethod.PART_VALS;
+    } else {
+      getMethod = GetPartitionsMethod.ALL;
+    }
+    
+    this.catName = normalizeIdentifier(request.getTableName().getCat());
+    this.dbName = normalizeIdentifier(request.getTableName().getDb());
+    this.tblName = normalizeIdentifier(request.getTableName().getTable());
+    this.conf = handler.getConf();
+    this.rs = handler.getMS();
+    this.filterHook = handler.getMetaFilterHook();
+    this.isServerFilterEnabled = filterHook != null;
+    GetTableRequest getTableRequest = new GetTableRequest(dbName, tblName);
+    getTableRequest.setCatName(catName);
+    this.table = handler.get_table_core(getTableRequest);
+    ((HMSHandler) handler).firePreEvent(new PreReadTableEvent(table, handler));
+    authorizeTableForPartitionMetadata();
+
+    LOG.info("Starting to get {} of {} using {}", request.isFetchPartNames() ? 
"partition names" : "partitions",
+        TableName.getQualified(catName, dbName, tblName), getMethod);
+  }
+
+  @Override
+  protected GetPartitionsResult<T> execute() throws TException, IOException {
+    return (GetPartitionsResult<T>) switch (getMethod) {
+      case EXPR -> getPartitionsByExpr();
+      case FILTER -> getPartitionsByFilter();
+      case NAMES -> getPartitionsByNames();
+      case PART_VALS -> getPartitionsByVals();
+      case ALL -> getPartitions();
+      case VALUES -> getPartitionValues();
+    };
+  }
+
+  private GetPartitionsResult getPartitionsByVals() throws TException {
+    if (request.isFetchPartNames()) {
+      List<String> ret = rs.listPartitionNamesPs(catName, dbName, tblName,
+          args.getPart_vals(), (short) args.getMax());
+      ret = FilterUtils.filterPartitionNamesIfEnabled(isServerFilterEnabled,
+          filterHook, catName, dbName, tblName, ret);
+      return new GetPartitionsResult<>(ret, true);
+    } else {
+      List<Partition> ret;
+      if (args.getPart_vals() != null) {
+        checkLimitNumberOfPartitionsByPs(args.getPart_vals(), args.getMax());
+      } else {
+        checkLimitNumberOfPartitionsByFilter(NO_FILTER_STRING, args.getMax());
+      }
+      ret = rs.listPartitionsPsWithAuth(catName, dbName, tblName, args);
+      return new GetPartitionsResult(ret, true);
+    }
+  }
+
+  private GetPartitionsResult getPartitionValues() throws MetaException {
+    PartitionValuesResponse resp = rs.listPartitionValues(catName, dbName, 
tblName, request.getPartitionKeys(),
+        request.isApplyDistinct(), args.getFilter(), request.isAscending(),
+        request.getPartitionOrders(), args.getMax());
+    return new GetPartitionsResult<>(Arrays.asList(resp), true);
+  }
+
+  private void checkLimitNumberOfPartitionsByPs(List<String> partVals, int 
requestMax)
+      throws TException {
+    if (exceedsPartitionFetchLimit(requestMax)) {
+      checkLimitNumberOfPartitions(tblName, rs.getNumPartitionsByPs(catName, 
dbName, tblName,
+          partVals));
+    }
+  }
+
+  private GetPartitionsResult<Partition> getPartitionsByFilter() throws 
TException {
+    List<Partition> ret = null;
+    if (exceedsPartitionFetchLimit(args.getMax())) {
+      // Since partition limit is configured, we need fetch at most (limit + 
1) partition names
+      int max = MetastoreConf.getIntVar(conf, 
MetastoreConf.ConfVars.LIMIT_PARTITION_REQUEST) + 1;
+      args = new 
GetPartitionsArgs.GetPartitionsArgsBuilder(args).max(max).build();
+      List<String> partNames = rs.listPartitionNamesByFilter(catName, dbName, 
tblName, args);
+      checkLimitNumberOfPartitions(tblName, partNames.size());
+      ret = rs.getPartitionsByNames(catName, dbName, tblName,
+          new 
GetPartitionsArgs.GetPartitionsArgsBuilder(args).partNames(partNames).build());
+    } else {
+      ret = rs.getPartitionsByFilter(catName, dbName, tblName, args);
+    }
+
+    ret = FilterUtils.filterPartitionsIfEnabled(isServerFilterEnabled, 
filterHook, ret);
+    return new GetPartitionsResult<>(ret, true);
+  }
+
+  /**
+   * Check if user can access the table associated with the partition. If not, 
then throw exception
+   * so user cannot access partitions associated with this table
+   * We are not calling Pre event listener for authorization because it 
requires getting the
+   * table object from DB, more overhead. Instead ,we call filter hook to 
filter out table if user
+   * has no access. Filter hook only requires table name, not table object. 
That saves DB access for
+   * table object, and still achieve the same purpose: checking if user can 
access the specified
+   * table
+   *
+   * @throws NoSuchObjectException
+   * @throws MetaException
+   */
+  private void authorizeTableForPartitionMetadata()
+      throws NoSuchObjectException, MetaException {
+    FilterUtils.checkDbAndTableFilters(
+        isServerFilterEnabled, filterHook, catName, dbName, tblName);
+  }
+
+  private GetPartitionsResult<Partition> getPartitionsByNames() throws 
TException {
+    List<Partition> ret = null;
+    boolean success = false;
+    rs.openTransaction();
+    try {
+      checkLimitNumberOfPartitions(tblName, args.getPartNames().size());
+      ret = rs.getPartitionsByNames(catName, dbName, tblName, args);
+      ret = FilterUtils.filterPartitionsIfEnabled(isServerFilterEnabled, 
filterHook, ret);
+
+      // If requested add column statistics in each of the partition objects
+      if (request.isGetColStats()) {
+        // Since each partition may have stats collected for different set of 
columns, we
+        // request them separately.
+        for (Partition part: ret) {
+          String partName = Warehouse.makePartName(table.getPartitionKeys(), 
part.getValues());
+          List<ColumnStatistics> partColStatsList =
+              rs.getPartitionColumnStatistics(catName, dbName, tblName,
+                  Collections.singletonList(partName),
+                  StatsSetupConst.getColumnsHavingStats(part.getParameters()),
+                  request.getEngine());
+          if (partColStatsList != null && !partColStatsList.isEmpty()) {
+            ColumnStatistics partColStats = partColStatsList.get(0);
+            if (partColStats != null) {
+              part.setColStats(partColStats);
+            }
+          }
+        }
+      }
+
+      List<String> processorCapabilities = request.getProcessorCapabilities();
+      if (processorCapabilities == null || processorCapabilities.isEmpty() ||
+          processorCapabilities.contains("MANAGERAWMETADATA")) {
+        LOG.info("Skipping translation for processor with {}", 
request.getProcessorId());
+      } else {
+        if (handler.getMetadataTransformer() != null) {
+          ret = handler.getMetadataTransformer().transformPartitions(ret, 
table,
+              processorCapabilities, request.getProcessorId());
+        }
+      }
+      success = rs.commitTransaction();
+    } finally {
+      if (!success) {
+        rs.rollbackTransaction();
+      }
+    }
+    return new GetPartitionsResult<>(ret, success);
+  }
+
+  private GetPartitionsResult getPartitions() throws TException {
+    if (request.isFetchPartNames()) {
+      List<String> ret = rs.listPartitionNames(catName, dbName, tblName, 
(short) args.getMax());
+      ret = FilterUtils.filterPartitionNamesIfEnabled(isServerFilterEnabled,
+          filterHook, catName, dbName, tblName, ret);
+      return new GetPartitionsResult<>(ret, true);
+    } else {
+      List<Partition> ret;
+      checkLimitNumberOfPartitionsByFilter(NO_FILTER_STRING, args.getMax());
+      ret = rs.listPartitionsPsWithAuth(catName, dbName, tblName, args);
+      ret = FilterUtils.filterPartitionsIfEnabled(isServerFilterEnabled, 
filterHook, ret);
+      return new GetPartitionsResult<>(ret, true);
+    }
+  }
+
+  private void checkLimitNumberOfPartitionsByFilter(String filterString, int 
requestMax) throws TException {
+    if (exceedsPartitionFetchLimit(requestMax)) {
+      checkLimitNumberOfPartitions(tblName, 
rs.getNumPartitionsByFilter(catName, dbName, tblName, filterString));
+    }
+  }
+
+  private GetPartitionsResult getPartitionsByExpr() throws TException {
+    if (request.isFetchPartNames()) {
+      List<String> ret = rs.listPartitionNames(catName, dbName, tblName,
+          args.getDefaultPartName(), args.getExpr(), args.getOrder(), 
args.getMax());
+      ret = FilterUtils.filterPartitionNamesIfEnabled(isServerFilterEnabled,
+          filterHook, catName, dbName, tblName, ret);
+      return new GetPartitionsResult(ret, true);
+    } else {
+      List<Partition> partitions = new LinkedList<>();
+      boolean hasUnknownPartitions = false;
+      if (exceedsPartitionFetchLimit(args.getMax())) {
+        // Since partition limit is configured, we need fetch at most (limit + 
1) partition names
+        int max = MetastoreConf.getIntVar(handler.getConf(), 
MetastoreConf.ConfVars.LIMIT_PARTITION_REQUEST) + 1;
+        List<String> partNames = rs.listPartitionNames(catName, dbName, 
tblName, args.getDefaultPartName(),
+            args.getExpr(), null, max);
+        checkLimitNumberOfPartitions(tblName, partNames.size());
+        partitions = rs.getPartitionsByNames(catName, dbName, tblName,
+            new 
GetPartitionsArgs.GetPartitionsArgsBuilder(args).partNames(partNames).build());
+      } else {
+        hasUnknownPartitions = rs.getPartitionsByExpr(catName, dbName, 
tblName, partitions, args);
+      }
+      return new GetPartitionsResult<>(partitions, hasUnknownPartitions);

Review Comment:
   In getPartitionsByExpr(), the boolean passed into new 
GetPartitionsResult<>(partitions, hasUnknownPartitions) is being used to carry 
`hasUnknownPartitions`, but GetPartitionsResult implements Result and its 
`success()` is consumed by AbstractRequestHandler.success()/status handling. 
This makes handler success reporting incorrect (e.g., a successful call with no 
unknown partitions will report success=false). Please separate handler 
execution success from the expr-specific `hasUnknownPartitions` flag (e.g., add 
a dedicated field/type for hasUnknownPartitions while keeping Result.success() 
aligned with actual handler success).
   ```suggestion
         // The boolean parameter of GetPartitionsResult must represent handler 
success,
         // not hasUnknownPartitions; handler reached here without error, so 
success=true.
         return new GetPartitionsResult<>(partitions, true);
   ```



##########
standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/HMSHandler.java:
##########
@@ -3313,37 +3169,18 @@ public Partition get_partition(final String db_name, 
final String tbl_name,
     return ret;
   }
 
-  private Partition get_partition_core(final String db_name, final String 
tbl_name,
-                                 final List<String> part_vals) throws 
MetaException, NoSuchObjectException {
-    String[] parsedDbName = parseDbName(db_name, conf);
-    startPartitionFunction("get_partition_core", parsedDbName[CAT_NAME], 
parsedDbName[DB_NAME],
-            tbl_name, part_vals);
-
-    Partition ret = null;
-    Exception ex = null;
-    try {
-      authorizeTableForPartitionMetadata(parsedDbName[CAT_NAME], 
parsedDbName[DB_NAME], tbl_name);
-      fireReadTablePreEvent(parsedDbName[CAT_NAME], parsedDbName[DB_NAME], 
tbl_name);
-      ret = getMS().getPartition(parsedDbName[CAT_NAME], 
parsedDbName[DB_NAME], tbl_name, part_vals);
-      ret = FilterUtils.filterPartitionIfEnabled(isServerFilterEnabled, 
filterHook, ret);
-    } catch (Exception e) {
-      ex = e;
-      throw handleException(e).throwIfInstance(MetaException.class, 
NoSuchObjectException.class).defaultMetaException();
-    } finally {
-      endFunction("get_partition_core", ret != null, ex, tbl_name);
-    }
-    return ret;
-  }
-
   @Override
   public GetPartitionResponse get_partition_req(GetPartitionRequest req)
       throws MetaException, NoSuchObjectException, TException {
-    // TODO Move the logic from get_partition to here, as that method is 
getting deprecated
-    String dbName = MetaStoreUtils.prependCatalogToDbName(req.getCatName(), 
req.getDbName(), conf);
-    Partition p = get_partition_core(dbName, req.getTblName(), 
req.getPartVals());
-    GetPartitionResponse res = new GetPartitionResponse();
-    res.setPartition(p);
-    return res;
+    String catName = req.isSetCatName() ? req.getCatName() : 
getDefaultCatalog(conf);
+    TableName tableName = new TableName(catName, req.getDbName(), 
req.getTblName());
+    GetPartitionsHandler.validatePartVals(this, tableName, req.getPartVals());
+    List<Partition> partitions = GetPartitionsHandler.getPartitions(
+        t -> startTableFunction("get_partition_req", catName, t.getDb(), 
t.getCat()),

Review Comment:
   The pre-hook for get_partition_req calls 
startTableFunction("get_partition_req", catName, t.getDb(), t.getCat()). The 
3rd argument should be the table name (t.getTable()), but t.getCat() is being 
passed instead, which will produce incorrect audit/logging (and any listener 
context derived from it).
   ```suggestion
           t -> startTableFunction("get_partition_req", catName, t.getDb(), 
t.getTable()),
   ```



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to