wecharyu commented on code in PR #6935: URL: https://github.com/apache/hive/pull/6935#discussion_r4748750420
########## 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 Javadoc says each lookup requests only as many names as the batch has room for, but `readBatch()` still calls `nextNames(batchSize)`. Sizing it to `batchSize - result.size()` would also let `remainder` go. ########## standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/TestHiveMetaStore.java: ########## @@ -3826,7 +3826,8 @@ public void testAlterTableCascadeExceedsPartitionLimits() throws Throwable { List<FieldSchema> cols = tbl.getSd().getCols(); cols.add(new FieldSchema("new_col", ColumnType.STRING_TYPE_NAME, "")); tbl.getSd().setCols(cols); - //add new column with cascade option + // With PartitionsIterable, alter_table doesn't load all partitions into + // memory, it will change the partition's schema in bulk. Review Comment: Makes sense. Could we drop `testAlterTableCascadeExceedsPartitionLimits` rather than keep it without assertions? -- 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]
