hudi-agent commented on code in PR #19204: URL: https://github.com/apache/hudi/pull/19204#discussion_r3724685803
########## 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: 🤖 Agreed the compareTo/equals/hashCode split is a genuine contract violation. Worth adding that it's latent in Hudi today: both `SimpleBloomFilter` and `HoodieDynamicBoundedBloomFilter` only ever call `new Key(bytes)` (weight 1.0), and `incrementWeight` has no production callers, so weights are always whole numbers and the sub-1.0 truncation never actually bites in current usage. A `new Key("abc".getBytes(), 1.5)` case that pins the current behavior (or switching `compareTo` to `Double.compare`) is still a good idea to lock the contract. ########## hudi-common/src/test/java/org/apache/hudi/common/expression/TestPartialBindVisitor.java: ########## @@ -81,4 +81,160 @@ public void testPartialBindIfFieldMissing() { Assertions.assertFalse((Boolean) binded.eval(new ArrayData(Arrays.asList("Lone", "2023-04-02", 15, 5L, false)))); Assertions.assertFalse((Boolean) binded.eval(new ArrayData(Arrays.asList("Lone", "2023-04-02", 10, 5L, false)))); } + + @Test + public void testPartialBindNotWithAllFieldsPresent() { + PartialBindVisitor partialBindVisitor = new PartialBindVisitor(schema, false); + Predicates.Not expr = Predicates.not(Predicates.eq(new NameReference("a"), Literal.from("Jane"))); Review Comment: 🤖 nit: could you rename `binded` to `bound` throughout the new tests? `bind` is a strong verb whose past participle is `bound`, and `TestBindVisitor` already uses `bound` consistently for the same concept — mixing the two names makes cross-file reading a bit jarring. <sub><i>⚠️ AI-generated; verify before applying. React 👍/👎 to flag quality.</i></sub> ########## 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: 🤖 I may be looking at a different revision, but in the current `BindVisitor#visitPredicate` the throw at line 185 is `"The expression " + predicate + " cannot be visited as predicate"` — it already interpolates `predicate` (not `this`) and keeps the space, identical to `PartialBindVisitor`. So a `StringStartsWithAny` falling through here should print its own toString, and `contains("StringStartsWithAny")` would actually pass rather than fail today. Asserting on the predicate text is still worthwhile to lock the contract, but the `this`/missing-space mixup doesn't seem to be present in this file. -- 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]
