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



##########
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);

Review comment:
       It could be, but I'd lead towards leaving it as it is.
   `MergeUnionEnumerator` is "lazily created" by its parent Enumerable 
(`EnumerableDefaults#mergeUnion`), i.e. the enumerator is only created when/if 
`Enumerable#enumerator` method is called. At this point (when the enumerator of 
an enumerable is generated) I think it is fair to assume that the enumerator 
that has been created is going to be traversed, so the data would be accessed 
anyway.
   Thus I think that the refactoring that you propose would add more complexity 
to `moveNext` code without any gain for the vast majority of cases.
   This strategy of "lazy enumerator creation" in Enumerable + some 
initialization work in the enumerator construction is already in place by other 
algorithms, like `EnumerableDefaults#mergeJoin`.




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