timothyw553 commented on code in PR #18933: URL: https://github.com/apache/iceberg/pull/18933#discussion_r4726000772
########## api/src/main/java/org/apache/iceberg/expressions/PartitionedInLiterals.java: ########## @@ -0,0 +1,209 @@ +/* + * 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.PartitionSpecVisitor; +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 {@code bucket} partition field, a file can only contain + * literals that map to the file's bucket value. {@link #literalsFor(Set, StructLike)} returns that + * subset so an evaluator compares a file's metrics against only those literals. Other transforms + * are not indexed: order-preserving ones, such as {@code identity} and {@code truncate}, already + * bound the matching literals through file metrics. + * + * <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<>(); Review Comment: it looks like no test covers two `IN` predicates with equal literal sets on different columns (e.g. `and(in("id", 1, 100), in("other", 1, 100))`). if `IdentityHashMap` were swapped for `HashMap`, all existing tests would still pass while wrongly rejecting a matching file. could we add tests for both one-column-bucketed and both-columns-bucketed? ########## core/src/main/java/org/apache/iceberg/ManifestReader.java: ########## @@ -400,7 +400,7 @@ private Evaluator evaluator() { private InclusiveMetricsEvaluator metricsEvaluator() { if (lazyMetricsEvaluator == null) { this.lazyMetricsEvaluator = - new InclusiveMetricsEvaluator(spec.schema(), rowFilter, caseSensitive); + InclusiveMetricsEvaluator.forRowFilter(rowFilter, spec, caseSensitive); Review Comment: what happens if a caller uses `select(...)` or `project(...)` and omits or partially projects partition fields? missing fields look like null (so `OR` filters can drop matching files), and a partial subset like `partition.tag` after `bucket(id)` can throw `IllegalArgumentException` from a type mismatch. could we fall back to the schema-only evaluator when the projected partition type doesn't match `spec.rawPartitionType()` and add regression tests for both projection paths? ```diff if (lazyMetricsEvaluator == null) { + Schema projected = projection(fileSchema, fileProjection, columns, caseSensitive); this.lazyMetricsEvaluator = - InclusiveMetricsEvaluator.forRowFilter(rowFilter, spec, caseSensitive); + spec.rawPartitionType().equals(projected.findType(DataFile.PARTITION_ID)) + ? InclusiveMetricsEvaluator.forRowFilter(rowFilter, spec, caseSensitive) + : new InclusiveMetricsEvaluator(spec.schema(), rowFilter, caseSensitive); } ``` ########## api/src/main/java/org/apache/iceberg/expressions/PartitionedInLiterals.java: ########## @@ -0,0 +1,209 @@ +/* + * 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.PartitionSpecVisitor; +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 {@code bucket} partition field, a file can only contain + * literals that map to the file's bucket value. {@link #literalsFor(Set, StructLike)} returns that + * subset so an evaluator compares a file's metrics against only those literals. Other transforms + * are not indexed: order-preserving ones, such as {@code identity} and {@code truncate}, already + * bound the matching literals through file metrics. + * + * <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)) { Review Comment: we don't seem to have a test for an `IN` predicate whose term is already a transform like `bucket(id, 16) IN (4, 5)`. without this `BoundReference` guard, it hashes the bucket numbers again and wrongly drops a matching file. could we add a test for that? -- 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]
