mihaibudiu commented on code in PR #5233: URL: https://github.com/apache/calcite/pull/5233#discussion_r3942600822
########## linq4j/src/test/java/org/apache/calcite/linq4j/test/IEJoinTest.java: ########## @@ -0,0 +1,196 @@ +/* + * 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.calcite.linq4j.test; + +import org.apache.calcite.linq4j.Enumerable; +import org.apache.calcite.linq4j.EnumerableDefaults; +import org.apache.calcite.linq4j.Enumerator; +import org.apache.calcite.linq4j.InequalityOperator; +import org.apache.calcite.linq4j.Linq4j; + +import org.checkerframework.checker.nullness.qual.Nullable; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.NoSuchElementException; +import java.util.Random; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** Tests for IEJoin. */ +class IEJoinTest { + private static final List<Point> LEFT = + Arrays.asList(new Point("l0", 2, 8), + new Point("l1", 5, 4), + new Point("l2", 5, 4), + new Point("lnx", null, 3), + new Point("lny", 3, null)); + + private static final List<Point> RIGHT = + Arrays.asList(new Point("r0", 4, 3), + new Point("r1", 7, 6), + new Point("r2", 5, 4), + new Point("rnx", null, 2), + new Point("rny", 6, null)); + + @Test void testAllOperatorCombinationsAgainstNestedLoop() { + for (InequalityOperator operator1 : InequalityOperator.values()) { + for (InequalityOperator operator2 : InequalityOperator.values()) { + assertMatches("fixed input", LEFT, RIGHT, operator1, operator2); + } + } + } + + @Test void testRandomInputsAgainstNestedLoop() { + final Random random = new Random(0); + for (int trial = 0; trial < 200; trial++) { + final List<Point> left = new ArrayList<>(); + final List<Point> right = new ArrayList<>(); + final int leftCount = random.nextInt(9); + final int rightCount = random.nextInt(9); + for (int i = 0; i < leftCount; i++) { + left.add( + new Point("l" + i, + random.nextInt(5) == 0 ? null : random.nextInt(7) - 3, + random.nextInt(5) == 0 ? null : random.nextInt(7) - 3)); + } + for (int i = 0; i < rightCount; i++) { + right.add( + new Point("r" + i, + random.nextInt(5) == 0 ? null : random.nextInt(7) - 3, + random.nextInt(5) == 0 ? null : random.nextInt(7) - 3)); + } + for (InequalityOperator operator1 : InequalityOperator.values()) { + for (InequalityOperator operator2 : InequalityOperator.values()) { + assertMatches("trial " + trial, left, right, operator1, operator2); + } + } + } + } + + @Test void testEmptyAndSameInputs() { + assertMatches("empty left", Collections.emptyList(), RIGHT, + InequalityOperator.LESS_THAN, InequalityOperator.GREATER_THAN); + assertMatches("empty right", LEFT, Collections.emptyList(), + InequalityOperator.LESS_THAN, InequalityOperator.GREATER_THAN); + assertMatches("same input", LEFT, LEFT, + InequalityOperator.LESS_THAN_OR_EQUAL, + InequalityOperator.GREATER_THAN_OR_EQUAL); + } + + @Test void testReset() { + final Enumerable<String> join = + ieJoin(LEFT, RIGHT, InequalityOperator.LESS_THAN, + InequalityOperator.GREATER_THAN); + final List<String> first = new ArrayList<>(); + final List<String> second = new ArrayList<>(); + try (Enumerator<String> enumerator = join.enumerator()) { + assertThrows(NoSuchElementException.class, enumerator::current); + while (enumerator.moveNext()) { + first.add(enumerator.current()); + } + assertThrows(NoSuchElementException.class, enumerator::current); + enumerator.reset(); + assertThrows(NoSuchElementException.class, enumerator::current); + while (enumerator.moveNext()) { + second.add(enumerator.current()); + } + } + assertThat(second, is(first)); + assertThat(join.toList(), is(first)); + } + + private static void assertMatches(String context, List<Point> left, + List<Point> right, InequalityOperator operator1, + InequalityOperator operator2) { + final List<String> expected = nestedLoop(left, right, operator1, operator2); + final List<String> actual = ieJoin(left, right, operator1, operator2).toList(); + Collections.sort(actual); + assertThat(context + ", " + operator1 + "/" + operator2 + + ", left=" + left + ", right=" + right, + actual, is(expected)); + } + + private static Enumerable<String> ieJoin(List<Point> left, List<Point> right, + InequalityOperator operator1, InequalityOperator operator2) { + return EnumerableDefaults.ieJoin( + Linq4j.asEnumerable(left), Linq4j.asEnumerable(right), + point -> point.x, point -> point.x, + point -> point.y, point -> point.y, + Comparator.naturalOrder(), Comparator.naturalOrder(), + operator1, operator2, + (leftPoint, rightPoint) -> leftPoint.name + ":" + rightPoint.name); + } + + private static List<String> nestedLoop(List<Point> leftRows, Review Comment: I assume this computes the expected values. A little JavaDoc would help ########## linq4j/src/main/java/org/apache/calcite/linq4j/InequalityOperator.java: ########## @@ -0,0 +1,33 @@ +/* + * 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.calcite.linq4j; + +/** Comparison operator applied to the left and right keys of an inequality Review Comment: I think this class is fully generic, maybe it can find uses outside of joins. So it's enough to characterize it as "comparison operator". That being said, is there a benefit reusing the kind? ########## linq4j/src/main/java/org/apache/calcite/linq4j/IEJoinEnumerator.java: ########## @@ -0,0 +1,228 @@ +/* + * 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.calcite.linq4j; + +import org.apache.calcite.linq4j.function.Function1; +import org.apache.calcite.linq4j.function.Function2; + +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.ArrayList; +import java.util.BitSet; +import java.util.Comparator; +import java.util.List; +import java.util.NoSuchElementException; + +import static org.apache.calcite.linq4j.Nullness.castNonNull; + +/** Enumerator that implements IEJoin for two inequality predicates. + * + * <p>Implements the union-array algorithm described by Khayyat et al. in + * <a href="https://doi.org/10.14778/2831360.2831362">"Lightning Fast and Space + * Efficient Inequality Joins," PVLDB 8(13), 2015</a>. + * + * <p>In Section 4.2, {@code firstOrder}, temporary {@code secondOrder}, + * {@code permutation}, and {@code activeRights} correspond to merged + * {@code L1/L1'}, merged {@code L2/L2'}, merged {@code P/P'}, and extended + * {@code B'}, respectively. {@code Entry.isLeft} records the source input. + * + * @param <TLeft> Left row type + * @param <TRight> Right row type + * @param <TKey1> First key type + * @param <TKey2> Second key type + * @param <TResult> Result row type + */ +final class IEJoinEnumerator<TLeft, TRight, TKey1, TKey2, TResult> + implements Enumerator<TResult> { + private final List<TLeft> leftRows = new ArrayList<>(); + private final List<TRight> rightRows = new ArrayList<>(); + private final List<Entry<TKey1, TKey2>> firstOrder; Review Comment: some of these fields are not obvious, can you please document them? ########## linq4j/src/test/java/org/apache/calcite/linq4j/test/IEJoinTest.java: ########## @@ -0,0 +1,196 @@ +/* + * 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.calcite.linq4j.test; + +import org.apache.calcite.linq4j.Enumerable; +import org.apache.calcite.linq4j.EnumerableDefaults; +import org.apache.calcite.linq4j.Enumerator; +import org.apache.calcite.linq4j.InequalityOperator; +import org.apache.calcite.linq4j.Linq4j; + +import org.checkerframework.checker.nullness.qual.Nullable; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.NoSuchElementException; +import java.util.Random; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** Tests for IEJoin. */ +class IEJoinTest { + private static final List<Point> LEFT = + Arrays.asList(new Point("l0", 2, 8), + new Point("l1", 5, 4), + new Point("l2", 5, 4), + new Point("lnx", null, 3), + new Point("lny", 3, null)); + + private static final List<Point> RIGHT = + Arrays.asList(new Point("r0", 4, 3), + new Point("r1", 7, 6), + new Point("r2", 5, 4), + new Point("rnx", null, 2), + new Point("rny", 6, null)); + + @Test void testAllOperatorCombinationsAgainstNestedLoop() { + for (InequalityOperator operator1 : InequalityOperator.values()) { + for (InequalityOperator operator2 : InequalityOperator.values()) { + assertMatches("fixed input", LEFT, RIGHT, operator1, operator2); + } + } + } + + @Test void testRandomInputsAgainstNestedLoop() { + final Random random = new Random(0); + for (int trial = 0; trial < 200; trial++) { + final List<Point> left = new ArrayList<>(); + final List<Point> right = new ArrayList<>(); + final int leftCount = random.nextInt(9); + final int rightCount = random.nextInt(9); + for (int i = 0; i < leftCount; i++) { + left.add( + new Point("l" + i, + random.nextInt(5) == 0 ? null : random.nextInt(7) - 3, + random.nextInt(5) == 0 ? null : random.nextInt(7) - 3)); + } + for (int i = 0; i < rightCount; i++) { + right.add( + new Point("r" + i, + random.nextInt(5) == 0 ? null : random.nextInt(7) - 3, + random.nextInt(5) == 0 ? null : random.nextInt(7) - 3)); + } + for (InequalityOperator operator1 : InequalityOperator.values()) { + for (InequalityOperator operator2 : InequalityOperator.values()) { + assertMatches("trial " + trial, left, right, operator1, operator2); + } + } + } + } + + @Test void testEmptyAndSameInputs() { + assertMatches("empty left", Collections.emptyList(), RIGHT, + InequalityOperator.LESS_THAN, InequalityOperator.GREATER_THAN); + assertMatches("empty right", LEFT, Collections.emptyList(), + InequalityOperator.LESS_THAN, InequalityOperator.GREATER_THAN); + assertMatches("same input", LEFT, LEFT, + InequalityOperator.LESS_THAN_OR_EQUAL, + InequalityOperator.GREATER_THAN_OR_EQUAL); + } + + @Test void testReset() { + final Enumerable<String> join = + ieJoin(LEFT, RIGHT, InequalityOperator.LESS_THAN, + InequalityOperator.GREATER_THAN); + final List<String> first = new ArrayList<>(); + final List<String> second = new ArrayList<>(); + try (Enumerator<String> enumerator = join.enumerator()) { + assertThrows(NoSuchElementException.class, enumerator::current); + while (enumerator.moveNext()) { + first.add(enumerator.current()); + } + assertThrows(NoSuchElementException.class, enumerator::current); + enumerator.reset(); + assertThrows(NoSuchElementException.class, enumerator::current); + while (enumerator.moveNext()) { + second.add(enumerator.current()); + } + } + assertThat(second, is(first)); + assertThat(join.toList(), is(first)); + } + + private static void assertMatches(String context, List<Point> left, + List<Point> right, InequalityOperator operator1, + InequalityOperator operator2) { + final List<String> expected = nestedLoop(left, right, operator1, operator2); + final List<String> actual = ieJoin(left, right, operator1, operator2).toList(); + Collections.sort(actual); + assertThat(context + ", " + operator1 + "/" + operator2 + + ", left=" + left + ", right=" + right, + actual, is(expected)); + } + + private static Enumerable<String> ieJoin(List<Point> left, List<Point> right, Review Comment: I assume this computes the result using the new enumerator. Add JavaDoc ########## linq4j/src/main/java/org/apache/calcite/linq4j/IEJoinEnumerator.java: ########## @@ -0,0 +1,228 @@ +/* + * 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.calcite.linq4j; + +import org.apache.calcite.linq4j.function.Function1; +import org.apache.calcite.linq4j.function.Function2; + +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.ArrayList; +import java.util.BitSet; +import java.util.Comparator; +import java.util.List; +import java.util.NoSuchElementException; + +import static org.apache.calcite.linq4j.Nullness.castNonNull; + +/** Enumerator that implements IEJoin for two inequality predicates. + * + * <p>Implements the union-array algorithm described by Khayyat et al. in + * <a href="https://doi.org/10.14778/2831360.2831362">"Lightning Fast and Space + * Efficient Inequality Joins," PVLDB 8(13), 2015</a>. + * + * <p>In Section 4.2, {@code firstOrder}, temporary {@code secondOrder}, + * {@code permutation}, and {@code activeRights} correspond to merged + * {@code L1/L1'}, merged {@code L2/L2'}, merged {@code P/P'}, and extended + * {@code B'}, respectively. {@code Entry.isLeft} records the source input. + * + * @param <TLeft> Left row type + * @param <TRight> Right row type + * @param <TKey1> First key type + * @param <TKey2> Second key type + * @param <TResult> Result row type + */ +final class IEJoinEnumerator<TLeft, TRight, TKey1, TKey2, TResult> + implements Enumerator<TResult> { + private final List<TLeft> leftRows = new ArrayList<>(); + private final List<TRight> rightRows = new ArrayList<>(); + private final List<Entry<TKey1, TKey2>> firstOrder; + private int[] permutation; + private final BitSet activeRights = new BitSet(); + private final Function2<? super TLeft, ? super TRight, TResult> resultSelector; + + private int secondPosition; + private int nextBit; + private @Nullable Entry<TKey1, TKey2> currentLeft; + private @Nullable TResult current; + private boolean hasCurrent; + + IEJoinEnumerator(Enumerable<TLeft> left, Enumerable<TRight> right, Review Comment: This needs some JavaDoc. -- 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]
