You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by GitBox <gi...@apache.org> on 2021/02/08 12:58:15 UTC

[GitHub] [calcite] vlsi commented on a change in pull request #2329: [CALCITE-3221] Add MergeUnion operator in Enumerable convention

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



##########
File path: linq4j/src/main/java/org/apache/calcite/linq4j/MergeUnionEnumerator.java
##########
@@ -0,0 +1,206 @@
+/*
+ * 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.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 Enumerator<TSource>[] inputs;
+  private final TSource[] currentInputsValues;
+  private final boolean[] inputsFinished;
+  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 @Nullable TKey currentKeyInProcessedSet;
+
+  private static final Object NOT_INIT = 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();
+    //noinspection unchecked
+    this.inputs = new Enumerator[size];
+    int i = 0;
+    for (Enumerable<TSource> source : sources) {
+      this.inputs[i++] = source.enumerator();
+    }
+
+    //noinspection unchecked
+    this.currentInputsValues = (TSource[]) new Object[size];
+    this.activeInputs = this.currentInputsValues.length;
+    this.inputsFinished = new boolean[size];
+    //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.length; i++) {
+      moveEnumerator(i);
+    }
+  }
+
+  private void moveEnumerator(int i) {
+    final Enumerator<TSource> enumerator = inputs[i];
+    if (!enumerator.moveNext()) {
+      activeInputs--;
+      inputsFinished[i] = true;

Review comment:
       A slight note: it might be a good time to perform `currentInputsValues[i] = null` to release memory.




----------------------------------------------------------------
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:
users@infra.apache.org