wecharyu commented on code in PR #6935:
URL: https://github.com/apache/hive/pull/6935#discussion_r4738031063
##########
standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/HiveAlterHandler.java:
##########
@@ -361,32 +360,25 @@ public void alterTable(RawStore msdb, Warehouse wh,
String catName, String dbnam
// alterPartition is only for changing the partition location in the
table rename
if (dataWasMoved) {
- PartitionsRequest req = new PartitionsRequest(newDbName,
newTblName);
- req.setCatName(catName);
- req.setMaxParts((short) -1);
- parts = handler.get_partitions_req(req).getPartitions();
-
- for (Partition part : parts) {
- String oldPartLoc = part.getSd().getLocation();
- if (oldPartLoc.contains(oldTblLocPath)) {
- URI oldUri = new Path(oldPartLoc).toUri();
- String newPath = oldUri.getPath().replace(oldTblLocPath,
newTblLocPath);
- Path newPartLocPath = new Path(oldUri.getScheme(),
oldUri.getAuthority(), newPath);
- part.getSd().setLocation(newPartLocPath.toString());
+ TableName renamedTable = new TableName(catalogName, newDbName,
newTblName);
+ PartitionsIterable partitionBatches = PartitionsIterable.create(
+ msdb, renamedTable, partitionBatchSize);
+ for (List<Partition> parts : partitionBatches) {
Review Comment:
Could we add a test renames a partitioned table across batches and checks
partition locations?
##########
standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/metastore/PartitionsIterable.java:
##########
@@ -0,0 +1,150 @@
+/*
+ * 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.metastore;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.Objects;
+
+import org.apache.hadoop.hive.common.TableName;
+import org.apache.hadoop.hive.metastore.RawStore;
+import org.apache.hadoop.hive.metastore.api.MetaException;
+import org.apache.hadoop.hive.metastore.api.Partition;
+import org.apache.hadoop.hive.metastore.client.builder.GetPartitionsArgs;
+import org.apache.hadoop.hive.metastore.metastore.iface.TableStore;
+
+/**
+ * PartitionsIterable - effectively a lazy
Iterable<List<Partition>>
+ * Similar to {@link org.apache.hadoop.hive.metastore.PartitionIterable}, but
+ * each iteration step returns up to {@code batchSize} partitions. Because a
metastore
+ * lookup by name can return fewer objects than names requested (missing
partitions),
+ * the iterator may perform multiple fetches in one {@code next()} until the
batch
+ * is full or partition names are exhausted.
+ * Note that remove() is an illegal call on this, and will result in an
+ * IllegalStateException.
+ */
+public class PartitionsIterable implements Iterable<List<Partition>> {
+
+ private final TableStore tableStore;
+ private final int batchSize;
+ private final TableName tableName;
+ private final List<String> partNames;
+
+ @Override
+ public Iterator<List<Partition>> iterator() {
+ if (partNames.isEmpty()) {
+ return Collections.emptyIterator();
+ }
+
+ return new Iterator<>() {
+ private final Iterator<String> partitionNamesIter = partNames.iterator();
+ private List<Partition> leftParts;
+
+ @Override
+ public boolean hasNext() {
+ return hasPendingPartitions() || partitionNamesIter.hasNext();
+ }
+
+ private boolean hasPendingPartitions() {
+ return leftParts != null && !leftParts.isEmpty();
+ }
+
+ @Override
+ public List<Partition> next() {
+ if (!hasNext()) {
+ throw new NoSuchElementException();
+ }
+ List<Partition> result = new ArrayList<>(batchSize);
+ if (hasPendingPartitions()) {
+ appendPartitions(result, leftParts);
+ leftParts = null;
+ }
+ while (result.size() < batchSize && partitionNamesIter.hasNext()) {
+ List<String> nameBatch = nextNameBatch();
+ appendPartitions(result, fetchBatch(nameBatch));
+ }
+ return result;
+ }
Review Comment:
The fill doesn't prevent empty batches: if the trailing names are all
dropped, `next()` still returns `[]` (`nullMetastoreResponseTreatedAsEmpty`).
If empties are the concern, skipping empty fetches in `hasNext()` would avoid
them with less code than `leftParts`/`appendPartitions`. WDYT?
--
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]