rdblue commented on code in PR #12311: URL: https://github.com/apache/iceberg/pull/12311#discussion_r1960172147
########## core/src/test/java/org/apache/iceberg/expressions/TestInclusiveMetricsEvaluatorWithExtract.java: ########## @@ -0,0 +1,687 @@ +/* + * 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 static org.apache.iceberg.expressions.Expressions.and; +import static org.apache.iceberg.expressions.Expressions.equal; +import static org.apache.iceberg.expressions.Expressions.extract; +import static org.apache.iceberg.expressions.Expressions.greaterThan; +import static org.apache.iceberg.expressions.Expressions.greaterThanOrEqual; +import static org.apache.iceberg.expressions.Expressions.in; +import static org.apache.iceberg.expressions.Expressions.isNaN; +import static org.apache.iceberg.expressions.Expressions.isNull; +import static org.apache.iceberg.expressions.Expressions.lessThan; +import static org.apache.iceberg.expressions.Expressions.lessThanOrEqual; +import static org.apache.iceberg.expressions.Expressions.not; +import static org.apache.iceberg.expressions.Expressions.notEqual; +import static org.apache.iceberg.expressions.Expressions.notIn; +import static org.apache.iceberg.expressions.Expressions.notNaN; +import static org.apache.iceberg.expressions.Expressions.notNull; +import static org.apache.iceberg.expressions.Expressions.notStartsWith; +import static org.apache.iceberg.expressions.Expressions.or; +import static org.apache.iceberg.expressions.Expressions.startsWith; +import static org.apache.iceberg.types.Types.NestedField.optional; +import static org.apache.iceberg.types.Types.NestedField.required; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.util.List; +import java.util.Map; +import org.apache.iceberg.DataFile; +import org.apache.iceberg.Schema; +import org.apache.iceberg.TestHelpers.Row; +import org.apache.iceberg.TestHelpers.TestDataFile; +import org.apache.iceberg.exceptions.ValidationException; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; +import org.apache.iceberg.relocated.com.google.common.collect.Lists; +import org.apache.iceberg.types.Types; +import org.apache.iceberg.types.Types.IntegerType; +import org.apache.iceberg.variants.PhysicalType; +import org.apache.iceberg.variants.VariantDataUtil; +import org.apache.iceberg.variants.Variants; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.FieldSource; + +public class TestInclusiveMetricsEvaluatorWithExtract { + private static final Schema SCHEMA = + new Schema( + required(1, "id", IntegerType.get()), + required(2, "variant", Types.VariantType.get()), + optional(3, "all_nulls", Types.VariantType.get())); + + private static final int INT_MIN_VALUE = 30; + private static final int INT_MAX_VALUE = 79; + + private static final DataFile FILE = + new TestDataFile( + "file.avro", + Row.of(), + 50, + // any value counts, including nulls + ImmutableMap.<Integer, Long>builder().put(1, 50L).put(2, 50L).put(3, 50L).buildOrThrow(), + // null value counts + ImmutableMap.<Integer, Long>builder() + .put(1, 0L) + .put(2, 0L) + .put(3, 50L) // all_nulls + .buildOrThrow(), + // nan value counts + null, + // lower bounds + ImmutableMap.of( + 2, + VariantDataUtil.serializeBounds( + Map.of( + "event_id", + Variants.of(PhysicalType.INT32, INT_MIN_VALUE), + "str", + Variants.of(PhysicalType.STRING, "abc")))), + // upper bounds + ImmutableMap.of( + 2, + VariantDataUtil.serializeBounds( + Map.of( + "event_id", + Variants.of(PhysicalType.INT32, INT_MAX_VALUE), + "str", + Variants.of(PhysicalType.STRING, "abe"))))); + + private boolean shouldRead(Expression expr) { + return shouldRead(expr, FILE); + } + + private boolean shouldRead(Expression expr, DataFile file) { + return shouldRead(expr, file, true); + } + + private boolean shouldReadCaseInsensitive(Expression expr) { + return shouldRead(expr, FILE, false); + } + + private boolean shouldRead(Expression expr, DataFile file, boolean caseSensitive) { + return new InclusiveMetricsEvaluator(SCHEMA, expr, caseSensitive).eval(file); + } + + @Test + public void testAllNulls() { + assertThat(shouldRead(isNull(extract("all_nulls", "$.event_id", "long")))) + .as("Should read: null values exist in all null column") + .isTrue(); + + assertThat(shouldRead(notNull(extract("all_nulls", "$.event_id", "long")))) + .as("Should skip: no non-null value in all null column") + .isFalse(); + + assertThat(shouldRead(lessThan(extract("all_nulls", "$.event_id", "long"), 30))) + .as("Should skip: lessThan on all null column") + .isFalse(); + + assertThat(shouldRead(lessThanOrEqual(extract("all_nulls", "$.event_id", "long"), 30))) + .as("Should skip: lessThanOrEqual on all null column") + .isFalse(); + + assertThat(shouldRead(greaterThan(extract("all_nulls", "$.event_id", "long"), 30))) + .as("Should skip: greaterThan on all null column") + .isFalse(); + + assertThat(shouldRead(greaterThanOrEqual(extract("all_nulls", "$.event_id", "long"), 30))) + .as("Should skip: greaterThanOrEqual on all null column") + .isFalse(); + + assertThat(shouldRead(equal(extract("all_nulls", "$.event_id", "long"), 30))) + .as("Should skip: equal on all null column") + .isFalse(); + + assertThat(shouldRead(notEqual(extract("all_nulls", "$.event_id", "long"), 30))) + .as("Should read: notEqual on all null column") + .isTrue(); + + assertThat(shouldRead(in(extract("all_nulls", "$.event_type", "string"), "abc", "def"))) + .as("Should skip: in on all nulls column") + .isFalse(); + + assertThat(shouldRead(notIn(extract("all_nulls", "$.event_type", "string"), "abc", "def"))) + .as("Should read: notIn on all nulls column") + .isTrue(); + + assertThat(shouldRead(startsWith(extract("all_nulls", "$.event_type", "string"), "a"))) + .as("Should skip: startsWith on all null column") + .isFalse(); + + assertThat(shouldRead(notStartsWith(extract("all_nulls", "$.event_type", "string"), "a"))) + .as("Should read: notStartsWith on all null column") + .isTrue(); + + assertThat(shouldRead(isNaN(extract("all_nulls", "$.measurement", "double")))) + .as("Should skip: no NaN value in all null column") + .isFalse(); + + assertThat(shouldRead(notNaN(extract("all_nulls", "$.measurement", "double")))) + .as("Should read: all null column has non-NaN values") + .isTrue(); + } + + @Test + public void testIsNaNAndNotNaN() { + assertThat(shouldRead(isNaN(extract("variant", "$.measurement", "double")))) + .as("Should read: variant may contain NaN") + .isTrue(); + + assertThat(shouldRead(notNaN(extract("variant", "$.measurement", "double")))) + .as("Should read: variant may contain non-NaN") + .isTrue(); + } + + @Test + public void testMissingColumn() { + assertThatThrownBy(() -> shouldRead(lessThan(extract("missing", "$", "int"), 5))) + .isInstanceOf(ValidationException.class) + .hasMessageContaining("Cannot find field 'missing'"); + } + + private static final Expression[] MISSING_STATS_EXPRESSIONS = + new Expression[] { + lessThan(extract("variant", "$.missing", "long"), 5), + lessThanOrEqual(extract("variant", "$.missing", "long"), 30), + equal(extract("variant", "$.missing", "long"), 70), + greaterThan(extract("variant", "$.missing", "long"), 78), + greaterThanOrEqual(extract("variant", "$.missing", "long"), 90), + notEqual(extract("variant", "$.missing", "long"), 101), + isNull(extract("variant", "$.missing", "string")), + notNull(extract("variant", "$.missing", "string")), + isNaN(extract("variant", "$.missing", "float")), + notNaN(extract("variant", "$.missing", "float")) + }; + + @ParameterizedTest + @FieldSource("MISSING_STATS_EXPRESSIONS") + public void testMissingStats(Expression expr) { Review Comment: If the bounds are in metadata, they are trusted. Bounds should be missing in cases where there are variant values with incompatible types. -- 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: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org