rubenada commented on a change in pull request #2329:
URL: https://github.com/apache/calcite/pull/2329#discussion_r568456474



##########
File path: 
linq4j/src/main/java/org/apache/calcite/linq4j/MergeUnionEnumerator.java
##########
@@ -0,0 +1,198 @@
+/*
+ * 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.EqualityComparer;
+import org.apache.calcite.linq4j.function.Function1;
+
+import org.checkerframework.checker.initialization.qual.UnknownInitialization;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.checkerframework.checker.nullness.qual.RequiresNonNull;
+
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.HashSet;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.Set;
+
+/**
+ * Performs a union (or union all) of all its inputs (which must be already 
sorted),
+ * respecting the order.
+ * @param <TSource> record type
+ * @param <TKey> sort key
+ */
+final class MergeUnionEnumerator<TSource, TKey> implements Enumerator<TSource> 
{
+  private final List<Enumerator<TSource>> inputs;
+  private final TSource[] currentInputsValues;
+  private final Function1<TSource, TKey> sortKeySelector;
+  private final Comparator<TKey> sortComparator;
+  private TSource currentValue;
+  private int activeInputs;
+
+  // Set to control duplicates, only used if "all" is false
+  private final @Nullable Set<EnumerableDefaults.Wrapped<TSource>> processed;
+  private final @Nullable Function1<TSource, 
EnumerableDefaults.Wrapped<TSource>> wrapper;
+
+  private static final Object NOT_INIT = new Object();
+  private static final Object FINISHED = new Object();

Review comment:
       changed: source state now tracked via boolean array `inputsFinished`

##########
File path: 
linq4j/src/main/java/org/apache/calcite/linq4j/MergeUnionEnumerator.java
##########
@@ -0,0 +1,198 @@
+/*
+ * 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.EqualityComparer;
+import org.apache.calcite.linq4j.function.Function1;
+
+import org.checkerframework.checker.initialization.qual.UnknownInitialization;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.checkerframework.checker.nullness.qual.RequiresNonNull;
+
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.HashSet;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.Set;
+
+/**
+ * Performs a union (or union all) of all its inputs (which must be already 
sorted),
+ * respecting the order.
+ * @param <TSource> record type
+ * @param <TKey> sort key
+ */
+final class MergeUnionEnumerator<TSource, TKey> implements Enumerator<TSource> 
{
+  private final List<Enumerator<TSource>> inputs;
+  private final TSource[] currentInputsValues;

Review comment:
       Changed: everything as array

##########
File path: 
linq4j/src/main/java/org/apache/calcite/linq4j/MergeUnionEnumerator.java
##########
@@ -0,0 +1,198 @@
+/*
+ * 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.EqualityComparer;
+import org.apache.calcite.linq4j.function.Function1;
+
+import org.checkerframework.checker.initialization.qual.UnknownInitialization;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.checkerframework.checker.nullness.qual.RequiresNonNull;
+
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.HashSet;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.Set;
+
+/**
+ * Performs a union (or union all) of all its inputs (which must be already 
sorted),
+ * respecting the order.
+ * @param <TSource> record type
+ * @param <TKey> sort key
+ */
+final class MergeUnionEnumerator<TSource, TKey> implements Enumerator<TSource> 
{
+  private final List<Enumerator<TSource>> inputs;
+  private final TSource[] currentInputsValues;
+  private final Function1<TSource, TKey> sortKeySelector;
+  private final Comparator<TKey> sortComparator;
+  private TSource currentValue;
+  private int activeInputs;
+
+  // Set to control duplicates, only used if "all" is false
+  private final @Nullable Set<EnumerableDefaults.Wrapped<TSource>> processed;
+  private final @Nullable Function1<TSource, 
EnumerableDefaults.Wrapped<TSource>> wrapper;
+
+  private static final Object NOT_INIT = new Object();
+  private static final Object FINISHED = new Object();
+
+  MergeUnionEnumerator(
+      List<Enumerable<TSource>> sources,
+      Function1<TSource, TKey> sortKeySelector,
+      Comparator<TKey> sortComparator,
+      boolean all,
+      EqualityComparer<TSource> equalityComparer) {
+    this.sortKeySelector = sortKeySelector;
+    this.sortComparator = sortComparator;
+
+    if (all) {
+      this.processed = null;
+      this.wrapper = null;
+    } else {
+      this.processed = new HashSet<>();
+      this.wrapper = EnumerableDefaults.wrapperFor(equalityComparer);
+    }
+
+    final int size = sources.size();
+    this.inputs = new ArrayList<>(size);
+    for (Enumerable<TSource> source : sources) {
+      this.inputs.add(source.enumerator());
+    }
+
+    //noinspection unchecked
+    this.currentInputsValues = (TSource[]) new Object[size];
+    this.activeInputs = this.currentInputsValues.length;
+    //noinspection unchecked
+    this.currentValue = (TSource) NOT_INIT;
+
+    initEnumerators();
+  }
+
+  @RequiresNonNull("inputs")
+  @SuppressWarnings("method.invocation.invalid")
+  private void initEnumerators(@UnknownInitialization 
MergeUnionEnumerator<TSource, TKey> this) {
+    for (int i = 0; i < inputs.size(); i++) {
+      moveEnumerator(i);
+    }
+  }
+
+  private void moveEnumerator(int i) {
+    final Enumerator<TSource> enumerator = inputs.get(i);
+    if (!enumerator.moveNext()) {
+      activeInputs--;
+      //noinspection unchecked
+      currentInputsValues[i] = (TSource) FINISHED;
+    } else {
+      currentInputsValues[i] = enumerator.current();
+    }
+  }
+
+  private boolean checkNotDuplicated(TSource value) {
+    if (processed == null) {
+      return true; // UNION ALL: no need to check duplicates
+    }
+
+    // check duplicates
+    @SuppressWarnings("dereference.of.nullable")
+    final EnumerableDefaults.Wrapped<TSource> wrapped = wrapper.apply(value);
+    if (!processed.contains(wrapped)) {
+      if (!processed.isEmpty()) {
+        // Since inputs are sorted, we do not need to keep in the set all the 
items that we
+        // have previously returned, just the ones with the same key, as soon 
as we see a new
+        // key, we can clear the set containing the items belonging to the 
previous key
+        final EnumerableDefaults.Wrapped<TSource> wrappedItemInSet = 
processed.iterator().next();

Review comment:
       Changed: keep `currentKeyInProcessedSet` variable




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to