dshogan-amzn commented on code in PR #18933: URL: https://github.com/apache/iceberg/pull/18933#discussion_r4615505755
########## api/src/main/java/org/apache/iceberg/expressions/PartitionedInLiterals.java: ########## @@ -0,0 +1,155 @@ +/* + * 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.iceberg.expressions; + +import java.util.Collection; +import java.util.Collections; +import java.util.IdentityHashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.function.Function; +import org.apache.iceberg.PartitionField; +import org.apache.iceberg.PartitionSpec; +import org.apache.iceberg.StructLike; +import org.apache.iceberg.expressions.ExpressionVisitors.ExpressionVisitor; +import org.apache.iceberg.relocated.com.google.common.collect.Maps; +import org.apache.iceberg.relocated.com.google.common.collect.Sets; +import org.apache.iceberg.transforms.Transform; + +/** + * Groups the literals of each bound {@code IN} predicate by the partition value they map to. + * + * <p>For a predicate on a column with a partition field whose transform does not preserve order, + * such as {@code bucket}, a file can only contain literals that map to the file's partition value. + * {@link #literalsFor(Set, StructLike)} returns that subset so an evaluator compares a file's + * metrics against only those literals. Order-preserving transforms, such as {@code identity} and + * {@code truncate}, are not indexed because their metrics already bound the matching literals. + * + * <p>Instances are built once per bound expression and partition spec, and queried once per file. + */ +class PartitionedInLiterals { + private static final PartitionedInLiterals EMPTY = + new PartitionedInLiterals(Collections.emptyMap()); + + // Keyed by identity: each bound IN predicate holds one literal set instance, which the visitor + // passes back to literals(), and two predicates with equal sets may reference different columns. + private final Map<Set<?>, Index> indexes; + + private PartitionedInLiterals(Map<Set<?>, Index> indexes) { + this.indexes = indexes; + } + + /** + * Index the {@code IN} predicates of a bound expression against a partition spec. + * + * @param boundExpr a bound expression + * @param spec the partition spec of the files that will be evaluated + * @return an index of IN literals by partition value + */ + @SuppressWarnings("IdentityHashMapUsage") + static PartitionedInLiterals build(Expression boundExpr, PartitionSpec spec) { + if (spec.isUnpartitioned()) { + return EMPTY; + } + + Map<Set<?>, Index> indexes = new IdentityHashMap<>(); + ExpressionVisitors.visit( + boundExpr, + new ExpressionVisitor<Void>() { + @Override + public <T> Void predicate(BoundPredicate<T> predicate) { + Index index = index(predicate, spec); + if (index != null) { + indexes.put(predicate.asSetPredicate().literalSet(), index); + } + return null; + } + }); + + return indexes.isEmpty() ? EMPTY : new PartitionedInLiterals(indexes); + } + + /** + * Return the literals of an {@code IN} predicate that may match a file with the given partition. + * + * @param literalSet the literal set of a bound IN predicate in the indexed expression + * @param partition the partition of the file being evaluated + * @return the literals that map to the file's partition value, or the full set if the predicate + * is not indexed + */ + <T> Collection<T> literalsFor(Set<T> literalSet, StructLike partition) { + Index index = indexes.get(literalSet); + return index != null ? index.literals(partition) : literalSet; + } + + @SuppressWarnings("unchecked") + private static <T> Index index(BoundPredicate<T> predicate, PartitionSpec spec) { + if (!predicate.isSetPredicate() + || predicate.op() != Expression.Operation.IN + || !(predicate.term() instanceof BoundReference)) { + return null; + } + + List<PartitionField> fields = spec.getFieldsBySourceId(predicate.ref().fieldId()); + if (fields == null) { + return null; + } + + for (PartitionField field : fields) { + Transform<T, ?> transform = (Transform<T, ?>) field.transform(); + if (transform.preservesOrder() || transform.project(field.name(), predicate) == null) { Review Comment: Good point. The project() call was only there to exclude transforms that can't narrow (void returns null, and unknown transforms can't be bound). I replaced it with a PartitionSpecVisitor(IsBucketTransform) that accepts only bucket fields, so the literals are hashed once, when building the index, and no projected predicate is built. Behavior is the same for every built-in transform. On the benchmark, the change didn't move allocation measurably on the 400-literal integer case (13.76 vs 13.74 MB/op), so most of the per-reader cost is the index maps themselves. String keys hash more expensively, so the saving there should be larger, but I haven't measured it. -- 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]
