wombatu-kun commented on code in PR #19204: URL: https://github.com/apache/hudi/pull/19204#discussion_r3555964162
########## hudi-common/src/test/java/org/apache/hudi/common/bloom/TestKey.java: ########## @@ -0,0 +1,144 @@ +/* + * 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.hudi.common.bloom; + +import org.junit.jupiter.api.Test; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Tests {@link Key}. + */ +public class TestKey { + + @Test + public void testDefaultWeightIsOne() { + Key key = new Key("abc".getBytes()); + assertEquals(1.0, key.getWeight()); + } + + @Test + public void testConstructorWithExplicitWeight() { + Key key = new Key("abc".getBytes(), 2.5); + assertEquals(2.5, key.getWeight()); + assertArrayEquals("abc".getBytes(), key.getBytes()); + } + + @Test + public void testSetWithNullValueThrows() { + Key key = new Key(); + assertThrows(IllegalArgumentException.class, () -> key.set(null, 1.0)); + } + + @Test + public void testIncrementWeightByAmount() { + Key key = new Key("abc".getBytes(), 1.0); + key.incrementWeight(2.0); + assertEquals(3.0, key.getWeight()); + } + + @Test + public void testIncrementWeightByOne() { + Key key = new Key("abc".getBytes(), 1.0); + key.incrementWeight(); + assertEquals(2.0, key.getWeight()); + } + + @Test + public void testEqualsAndHashCodeForIdenticalKeys() { + Key key1 = new Key("abc".getBytes(), 1.0); + Key key2 = new Key("abc".getBytes(), 1.0); + assertTrue(key1.equals(key2)); + assertEquals(key1.hashCode(), key2.hashCode()); + } + + @Test + public void testEqualsIsFalseForDifferentBytes() { + Key key1 = new Key("abc".getBytes(), 1.0); + Key key2 = new Key("xyz".getBytes(), 1.0); + assertFalse(key1.equals(key2)); + } + + @Test + public void testEqualsIsFalseForDifferentWeight() { + Key key1 = new Key("abc".getBytes(), 1.0); + Key key2 = new Key("abc".getBytes(), 2.0); + assertFalse(key1.equals(key2)); Review Comment: `Key#compareTo` ends with `result = (int) (this.weight - other.weight)`, so any weight delta below 1.0 truncates to 0. Two keys with the same bytes and weights 1.0 and 1.5 therefore compare equal, and `equals` reports them equal since it delegates to `compareTo`, while `hashCode` XORs in `Double.hashCode(weight)` and returns different values - an equals/hashCode contract violation. The deltas used across these three tests (1.0 and 2.0) are exactly the ones that hide it. Worth adding a `new Key("abc".getBytes(), 1.5)` case, either asserting the current behavior explicitly or driving `compareTo` to `Double.compare`. ########## hudi-common/src/test/java/org/apache/hudi/common/expression/TestBindVisitor.java: ########## @@ -0,0 +1,322 @@ +/* + * 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.hudi.common.expression; + +import org.apache.hudi.common.schema.internal.Types; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.Arrays; + +/** + * Tests {@link BindVisitor}. + */ +public class TestBindVisitor { + + private static Types.RecordType schema; + + @BeforeAll + public static void init() { + ArrayList<Types.Field> fields = new ArrayList<>(3); + fields.add(Types.Field.get(0, true, "a", Types.StringType.get())); + fields.add(Types.Field.get(1, true, "c", Types.IntType.get())); + fields.add(Types.Field.get(2, true, "d", Types.LongType.get())); + schema = Types.RecordType.get(fields, "schema"); + } + + @Test + public void testVisitNameReferenceBindsExistingField() { + BindVisitor bindVisitor = new BindVisitor(schema, true); + Expression bound = new NameReference("a").accept(bindVisitor); + + Assertions.assertTrue(bound instanceof BoundReference); + Assertions.assertEquals(Types.StringType.get(), bound.getDataType()); + } + + @Test + public void testVisitNameReferenceThrowsForMissingField() { + BindVisitor bindVisitor = new BindVisitor(schema, true); + NameReference unbound = new NameReference("unknown"); + + IllegalArgumentException e = Assertions.assertThrows(IllegalArgumentException.class, + () -> unbound.accept(bindVisitor)); + Assertions.assertTrue(e.getMessage().contains("cannot be bound from schema")); + } + + @Test + public void testVisitNameReferenceCaseSensitiveFailsOnDifferentCase() { + BindVisitor bindVisitor = new BindVisitor(schema, true); + NameReference upperCase = new NameReference("A"); + + Assertions.assertThrows(IllegalArgumentException.class, () -> upperCase.accept(bindVisitor)); + } + + @Test + public void testVisitNameReferenceCaseInsensitiveBindsDifferentCase() { + BindVisitor bindVisitor = new BindVisitor(schema, false); + Expression bound = new NameReference("A").accept(bindVisitor); + + Assertions.assertTrue(bound instanceof BoundReference); + } + + @Test + public void testVisitLiteralReturnsSameInstance() { + BindVisitor bindVisitor = new BindVisitor(schema, true); + Literal<String> literal = Literal.from("value"); + + Assertions.assertSame(literal, literal.accept(bindVisitor)); + } + + @Test + public void testVisitBoundReferenceReturnsSameInstance() { + BindVisitor bindVisitor = new BindVisitor(schema, true); + BoundReference boundReference = new BoundReference(0, Types.StringType.get()); + + Assertions.assertSame(boundReference, boundReference.accept(bindVisitor)); + } + + @Test + public void testVisitAndBothOperandsBindAndEval() { + BindVisitor bindVisitor = new BindVisitor(schema, true); + Predicates.And and = Predicates.and( + Predicates.eq(new NameReference("a"), Literal.from("Jane")), + Predicates.gt(new NameReference("c"), Literal.from(10))); + Expression bound = and.accept(bindVisitor); + + Assertions.assertTrue((Boolean) bound.eval(new ArrayData(Arrays.asList("Jane", 15, 5L)))); + Assertions.assertFalse((Boolean) bound.eval(new ArrayData(Arrays.asList("Jane", 5, 5L)))); + } + + @Test + public void testVisitAndShortCircuitsWhenLeftIsFalseExpression() { + BindVisitor bindVisitor = new BindVisitor(schema, true); + Predicates.And and = Predicates.and(Predicates.alwaysFalse(), + Predicates.eq(new NameReference("a"), Literal.from("Jane"))); + + Assertions.assertTrue(and.accept(bindVisitor) instanceof Predicates.FalseExpression); + } + + @Test + public void testVisitAndShortCircuitsWhenRightIsFalseExpression() { + BindVisitor bindVisitor = new BindVisitor(schema, true); + Predicates.And and = Predicates.and( + Predicates.eq(new NameReference("a"), Literal.from("Jane")), Predicates.alwaysFalse()); + + Assertions.assertTrue(and.accept(bindVisitor) instanceof Predicates.FalseExpression); + } + + @Test + public void testVisitAndSimplifiesWhenBothBoundOperandsAreAlwaysTrue() { + BindVisitor bindVisitor = new BindVisitor(schema, true); + Predicates.And and = Predicates.and(Predicates.alwaysTrue(), Predicates.alwaysTrue()); + + Assertions.assertTrue(and.accept(bindVisitor) instanceof Predicates.TrueExpression); + } + + @Test + public void testVisitAndSimplifiesWhenLeftBoundOperandIsAlwaysTrue() { + BindVisitor bindVisitor = new BindVisitor(schema, true); + Predicates.BinaryComparison rightPredicate = Predicates.eq(new NameReference("a"), Literal.from("Jane")); + Predicates.And and = Predicates.and(Predicates.alwaysTrue(), rightPredicate); + + Expression bound = and.accept(bindVisitor); + Assertions.assertTrue((Boolean) bound.eval(new ArrayData(Arrays.asList("Jane", 15, 5L)))); + Assertions.assertFalse((Boolean) bound.eval(new ArrayData(Arrays.asList("Lone", 15, 5L)))); + } + + @Test + public void testVisitAndSimplifiesWhenRightBoundOperandIsAlwaysTrue() { + BindVisitor bindVisitor = new BindVisitor(schema, true); + Predicates.BinaryComparison leftPredicate = Predicates.eq(new NameReference("a"), Literal.from("Jane")); + Predicates.And and = Predicates.and(leftPredicate, Predicates.alwaysTrue()); + + Expression bound = and.accept(bindVisitor); + Assertions.assertTrue((Boolean) bound.eval(new ArrayData(Arrays.asList("Jane", 15, 5L)))); + Assertions.assertFalse((Boolean) bound.eval(new ArrayData(Arrays.asList("Lone", 15, 5L)))); + } + + @Test + public void testVisitOrBothOperandsBindAndEval() { + BindVisitor bindVisitor = new BindVisitor(schema, true); + Predicates.Or or = Predicates.or( + Predicates.eq(new NameReference("a"), Literal.from("Jane")), + Predicates.gt(new NameReference("c"), Literal.from(10))); + Expression bound = or.accept(bindVisitor); + + Assertions.assertTrue((Boolean) bound.eval(new ArrayData(Arrays.asList("Lone", 15, 5L)))); + Assertions.assertFalse((Boolean) bound.eval(new ArrayData(Arrays.asList("Lone", 5, 5L)))); + } + + @Test + public void testVisitOrShortCircuitsWhenLeftIsTrueExpression() { + BindVisitor bindVisitor = new BindVisitor(schema, true); + Predicates.Or or = Predicates.or(Predicates.alwaysTrue(), + Predicates.eq(new NameReference("a"), Literal.from("Jane"))); + + Assertions.assertTrue(or.accept(bindVisitor) instanceof Predicates.TrueExpression); + } + + @Test + public void testVisitOrShortCircuitsWhenRightIsTrueExpression() { + BindVisitor bindVisitor = new BindVisitor(schema, true); + Predicates.Or or = Predicates.or( + Predicates.eq(new NameReference("a"), Literal.from("Jane")), Predicates.alwaysTrue()); + + Assertions.assertTrue(or.accept(bindVisitor) instanceof Predicates.TrueExpression); + } + + @Test + public void testVisitOrSimplifiesWhenBothBoundOperandsAreAlwaysFalse() { + BindVisitor bindVisitor = new BindVisitor(schema, true); + Predicates.Or or = Predicates.or(Predicates.alwaysFalse(), Predicates.alwaysFalse()); + + Assertions.assertTrue(or.accept(bindVisitor) instanceof Predicates.FalseExpression); + } + + @Test + public void testVisitOrSimplifiesWhenLeftBoundOperandIsAlwaysFalse() { + BindVisitor bindVisitor = new BindVisitor(schema, true); + Predicates.BinaryComparison rightPredicate = Predicates.eq(new NameReference("a"), Literal.from("Jane")); + Predicates.Or or = Predicates.or(Predicates.alwaysFalse(), rightPredicate); + + Expression bound = or.accept(bindVisitor); + Assertions.assertTrue((Boolean) bound.eval(new ArrayData(Arrays.asList("Jane", 15, 5L)))); + Assertions.assertFalse((Boolean) bound.eval(new ArrayData(Arrays.asList("Lone", 15, 5L)))); + } + + @Test + public void testVisitOrSimplifiesWhenRightBoundOperandIsAlwaysFalse() { + BindVisitor bindVisitor = new BindVisitor(schema, true); + Predicates.BinaryComparison leftPredicate = Predicates.eq(new NameReference("a"), Literal.from("Jane")); + Predicates.Or or = Predicates.or(leftPredicate, Predicates.alwaysFalse()); + + Expression bound = or.accept(bindVisitor); + Assertions.assertTrue((Boolean) bound.eval(new ArrayData(Arrays.asList("Jane", 15, 5L)))); + Assertions.assertFalse((Boolean) bound.eval(new ArrayData(Arrays.asList("Lone", 15, 5L)))); + } + + @Test + public void testVisitPredicateNot() { + BindVisitor bindVisitor = new BindVisitor(schema, true); + Predicates.Not not = Predicates.not(Predicates.eq(new NameReference("a"), Literal.from("Jane"))); + Expression bound = not.accept(bindVisitor); + + Assertions.assertFalse((Boolean) bound.eval(new ArrayData(Arrays.asList("Jane", 15, 5L)))); + Assertions.assertTrue((Boolean) bound.eval(new ArrayData(Arrays.asList("Lone", 15, 5L)))); + } + + @Test + public void testVisitPredicateNotOfAlwaysTrueChildIsAlwaysFalse() { + BindVisitor bindVisitor = new BindVisitor(schema, true); + Predicates.Not not = Predicates.not(Predicates.alwaysTrue()); + + Assertions.assertTrue(not.accept(bindVisitor) instanceof Predicates.FalseExpression); + } + + @Test + public void testVisitPredicateNotOfAlwaysFalseChildIsAlwaysTrue() { + BindVisitor bindVisitor = new BindVisitor(schema, true); + Predicates.Not not = Predicates.not(Predicates.alwaysFalse()); + + Assertions.assertTrue(not.accept(bindVisitor) instanceof Predicates.TrueExpression); + } + + @Test + public void testVisitPredicateBinaryComparison() { + BindVisitor bindVisitor = new BindVisitor(schema, true); + Predicates.BinaryComparison eq = Predicates.eq(new NameReference("c"), Literal.from(15)); + Expression bound = eq.accept(bindVisitor); + + Assertions.assertTrue(bound instanceof Predicates.BinaryComparison); + Assertions.assertTrue((Boolean) bound.eval(new ArrayData(Arrays.asList("Jane", 15, 5L)))); + } + + @Test + public void testVisitPredicateIn() { + BindVisitor bindVisitor = new BindVisitor(schema, true); + Predicates.In in = Predicates.in(new NameReference("c"), + Arrays.asList(Literal.from(10), Literal.from(15))); + Expression bound = in.accept(bindVisitor); + + Assertions.assertTrue((Boolean) bound.eval(new ArrayData(Arrays.asList("Jane", 15, 5L)))); + Assertions.assertFalse((Boolean) bound.eval(new ArrayData(Arrays.asList("Jane", 99, 5L)))); + } + + @Test + public void testVisitPredicateIsNull() { + BindVisitor bindVisitor = new BindVisitor(schema, true); + Predicates.IsNull isNull = Predicates.isNull(new NameReference("a")); + Expression bound = isNull.accept(bindVisitor); + + Assertions.assertTrue((Boolean) bound.eval(new ArrayData(Arrays.asList(null, 15, 5L)))); + Assertions.assertFalse((Boolean) bound.eval(new ArrayData(Arrays.asList("Jane", 15, 5L)))); + } + + @Test + public void testVisitPredicateIsNotNull() { + BindVisitor bindVisitor = new BindVisitor(schema, true); + Predicates.IsNotNull isNotNull = Predicates.isNotNull(new NameReference("a")); + Expression bound = isNotNull.accept(bindVisitor); + + Assertions.assertFalse((Boolean) bound.eval(new ArrayData(Arrays.asList(null, 15, 5L)))); + Assertions.assertTrue((Boolean) bound.eval(new ArrayData(Arrays.asList("Jane", 15, 5L)))); + } + + @Test + public void testVisitPredicateStringStartsWith() { + BindVisitor bindVisitor = new BindVisitor(schema, true); + Predicates.StringStartsWith startsWith = Predicates.startsWith(new NameReference("a"), Literal.from("Ja")); + Expression bound = startsWith.accept(bindVisitor); + + Assertions.assertTrue((Boolean) bound.eval(new ArrayData(Arrays.asList("Jane", 15, 5L)))); + Assertions.assertFalse((Boolean) bound.eval(new ArrayData(Arrays.asList("Lone", 15, 5L)))); + } + + @Test + public void testVisitPredicateStringContains() { + BindVisitor bindVisitor = new BindVisitor(schema, true); + Predicates.StringContains contains = Predicates.contains(new NameReference("a"), Literal.from("an")); + Expression bound = contains.accept(bindVisitor); + + Assertions.assertTrue((Boolean) bound.eval(new ArrayData(Arrays.asList("Jane", 15, 5L)))); + Assertions.assertFalse((Boolean) bound.eval(new ArrayData(Arrays.asList("Lone", 15, 5L)))); + } + + @Test + public void testVisitPredicateThrowsForMissingFieldInsideBinaryComparison() { + BindVisitor bindVisitor = new BindVisitor(schema, true); + Predicates.BinaryComparison eq = Predicates.eq(new NameReference("unknown"), Literal.from("Jane")); + + Assertions.assertThrows(IllegalArgumentException.class, () -> eq.accept(bindVisitor)); + } + + @Test + public void testVisitPredicateThrowsForUnsupportedPredicateType() { + BindVisitor bindVisitor = new BindVisitor(schema, true); + Predicates.StringStartsWithAny startsWithAny = Predicates.startsWithAny(new NameReference("a"), + Arrays.asList(Literal.from("Ja"))); + + IllegalArgumentException e = Assertions.assertThrows(IllegalArgumentException.class, + () -> startsWithAny.accept(bindVisitor)); + Assertions.assertTrue(e.getMessage().contains("cannot be visited as predicate")); Review Comment: This assertion passes even though `BindVisitor#visitPredicate` builds its message as `"The expression " + this + "cannot be visited as predicate"`, interpolating the visitor instead of the offending predicate and dropping the space before `cannot`. `PartialBindVisitor#visitPredicate` gets this right by interpolating `predicate`. This is the first test to reach that throw, so asserting on the expression rather than the tail of the sentence would pin the intended contract and surface the mixup: `assertTrue(e.getMessage().contains("StringStartsWithAny"))` fails today. ########## hudi-common/src/test/java/org/apache/hudi/common/expression/TestBindVisitor.java: ########## @@ -0,0 +1,322 @@ +/* + * 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.hudi.common.expression; + +import org.apache.hudi.common.schema.internal.Types; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.Arrays; + +/** + * Tests {@link BindVisitor}. + */ +public class TestBindVisitor { + + private static Types.RecordType schema; + + @BeforeAll + public static void init() { + ArrayList<Types.Field> fields = new ArrayList<>(3); + fields.add(Types.Field.get(0, true, "a", Types.StringType.get())); + fields.add(Types.Field.get(1, true, "c", Types.IntType.get())); + fields.add(Types.Field.get(2, true, "d", Types.LongType.get())); + schema = Types.RecordType.get(fields, "schema"); + } + + @Test + public void testVisitNameReferenceBindsExistingField() { + BindVisitor bindVisitor = new BindVisitor(schema, true); + Expression bound = new NameReference("a").accept(bindVisitor); + + Assertions.assertTrue(bound instanceof BoundReference); + Assertions.assertEquals(Types.StringType.get(), bound.getDataType()); + } + + @Test + public void testVisitNameReferenceThrowsForMissingField() { + BindVisitor bindVisitor = new BindVisitor(schema, true); + NameReference unbound = new NameReference("unknown"); + + IllegalArgumentException e = Assertions.assertThrows(IllegalArgumentException.class, + () -> unbound.accept(bindVisitor)); + Assertions.assertTrue(e.getMessage().contains("cannot be bound from schema")); + } + + @Test + public void testVisitNameReferenceCaseSensitiveFailsOnDifferentCase() { + BindVisitor bindVisitor = new BindVisitor(schema, true); + NameReference upperCase = new NameReference("A"); + + Assertions.assertThrows(IllegalArgumentException.class, () -> upperCase.accept(bindVisitor)); + } + + @Test + public void testVisitNameReferenceCaseInsensitiveBindsDifferentCase() { + BindVisitor bindVisitor = new BindVisitor(schema, false); + Expression bound = new NameReference("A").accept(bindVisitor); + + Assertions.assertTrue(bound instanceof BoundReference); + } + + @Test + public void testVisitLiteralReturnsSameInstance() { + BindVisitor bindVisitor = new BindVisitor(schema, true); + Literal<String> literal = Literal.from("value"); + + Assertions.assertSame(literal, literal.accept(bindVisitor)); + } + + @Test + public void testVisitBoundReferenceReturnsSameInstance() { + BindVisitor bindVisitor = new BindVisitor(schema, true); + BoundReference boundReference = new BoundReference(0, Types.StringType.get()); + + Assertions.assertSame(boundReference, boundReference.accept(bindVisitor)); + } + + @Test + public void testVisitAndBothOperandsBindAndEval() { + BindVisitor bindVisitor = new BindVisitor(schema, true); + Predicates.And and = Predicates.and( + Predicates.eq(new NameReference("a"), Literal.from("Jane")), + Predicates.gt(new NameReference("c"), Literal.from(10))); + Expression bound = and.accept(bindVisitor); + + Assertions.assertTrue((Boolean) bound.eval(new ArrayData(Arrays.asList("Jane", 15, 5L)))); + Assertions.assertFalse((Boolean) bound.eval(new ArrayData(Arrays.asList("Jane", 5, 5L)))); + } + + @Test + public void testVisitAndShortCircuitsWhenLeftIsFalseExpression() { + BindVisitor bindVisitor = new BindVisitor(schema, true); + Predicates.And and = Predicates.and(Predicates.alwaysFalse(), + Predicates.eq(new NameReference("a"), Literal.from("Jane"))); + + Assertions.assertTrue(and.accept(bindVisitor) instanceof Predicates.FalseExpression); Review Comment: Passing `Predicates.alwaysFalse()` as a direct operand is caught by the pre-bind check at the top of `visitAnd`, so this never reaches the second `alwaysFalse()` return, the one that fires when an operand becomes a `FalseExpression` only after `accept`. That post-bind branch stays uncovered, while its `visitOr` counterpart is already exercised by `TestPartialBindVisitor#testPartialBindIfFieldMissing`. `Predicates.and(Predicates.not(Predicates.alwaysTrue()), Predicates.eq(new NameReference("a"), Literal.from("Jane")))` binds the left operand into a `FalseExpression` and covers 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]
