Java Code Examples for org.apache.calcite.linq4j.function.Function1#apply()

The following examples show how to use org.apache.calcite.linq4j.function.Function1#apply() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EnumerableDefaults.java    From calcite with Apache License 2.0 6 votes vote down vote up
private static <TSource, TKey, TAccumulate, TResult> Enumerable<TResult> groupBy_(
    final Map<TKey, TAccumulate> map, Enumerable<TSource> enumerable,
    Function1<TSource, TKey> keySelector,
    Function0<TAccumulate> accumulatorInitializer,
    Function2<TAccumulate, TSource, TAccumulate> accumulatorAdder,
    final Function2<TKey, TAccumulate, TResult> resultSelector) {
  try (Enumerator<TSource> os = enumerable.enumerator()) {
    while (os.moveNext()) {
      TSource o = os.current();
      TKey key = keySelector.apply(o);
      TAccumulate accumulator = map.get(key);
      if (accumulator == null) {
        accumulator = accumulatorInitializer.apply();
        accumulator = accumulatorAdder.apply(accumulator, o);
        map.put(key, accumulator);
      } else {
        TAccumulate accumulator0 = accumulator;
        accumulator = accumulatorAdder.apply(accumulator, o);
        if (accumulator != accumulator0) {
          map.put(key, accumulator);
        }
      }
    }
  }
  return new LookupResultEnumerable<>(map, resultSelector);
}
 
Example 2
Source File: EnumerableDefaults.java    From calcite with Apache License 2.0 6 votes vote down vote up
static <TSource, TKey, TElement> LookupImpl<TKey, TElement> toLookup_(
    Map<TKey, List<TElement>> map, Enumerable<TSource> source,
    Function1<TSource, TKey> keySelector,
    Function1<TSource, TElement> elementSelector) {
  try (Enumerator<TSource> os = source.enumerator()) {
    while (os.moveNext()) {
      TSource o = os.current();
      final TKey key = keySelector.apply(o);
      List<TElement> list = map.get(key);
      if (list == null) {
        // for first entry, use a singleton list to save space
        list = Collections.singletonList(elementSelector.apply(o));
      } else {
        if (list.size() == 1) {
          // when we go from 1 to 2 elements, switch to array list
          TElement element = list.get(0);
          list = new ArrayList<>();
          list.add(element);
        }
        list.add(elementSelector.apply(o));
      }
      map.put(key, list);
    }
  }
  return new LookupImpl<>(map);
}
 
Example 3
Source File: Linq4j.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Applies a function to each element of an Enumerator.
 *
 * @param enumerator Backing enumerator
 * @param func Transform function
 * @param <F> Backing element type
 * @param <E> Element type
 * @return Enumerator
 */
public static <F, E> Enumerator<E> transform(Enumerator<F> enumerator,
    final Function1<F, E> func) {
  return new TransformedEnumerator<F, E>(enumerator) {
    protected E transform(F from) {
      return func.apply(from);
    }
  };
}
 
Example 4
Source File: DefaultEnumerable.java    From calcite with Apache License 2.0 5 votes vote down vote up
public <R> R foreach(Function1<T, R> func) {
  R result = null;
  try (Enumerator<T> enumerator = enumerator()) {
    while (enumerator.moveNext()) {
      T t = enumerator.current();
      result = func.apply(t);
    }
    return result;
  }
}
 
Example 5
Source File: EnumerableDefaults.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Applies an accumulator function over a
 * sequence. The specified seed value is used as the initial
 * accumulator value, and the specified function is used to select
 * the result value.
 */
public static <TSource, TAccumulate, TResult> TResult aggregate(
    Enumerable<TSource> source, TAccumulate seed,
    Function2<TAccumulate, TSource, TAccumulate> func,
    Function1<TAccumulate, TResult> selector) {
  TAccumulate accumulate = seed;
  try (Enumerator<TSource> os = source.enumerator()) {
    while (os.moveNext()) {
      TSource o = os.current();
      accumulate = func.apply(accumulate, o);
    }
    return selector.apply(accumulate);
  }
}
 
Example 6
Source File: EnumerableDefaults.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static <TSource, TKey, TAccumulate, TResult> Enumerable<TResult> groupByMultiple_(
    final Map<TKey, TAccumulate> map, Enumerable<TSource> enumerable,
    List<Function1<TSource, TKey>> keySelectors,
    Function0<TAccumulate> accumulatorInitializer,
    Function2<TAccumulate, TSource, TAccumulate> accumulatorAdder,
    final Function2<TKey, TAccumulate, TResult> resultSelector) {
  try (Enumerator<TSource> os = enumerable.enumerator()) {
    while (os.moveNext()) {
      for (Function1<TSource, TKey> keySelector : keySelectors) {
        TSource o = os.current();
        TKey key = keySelector.apply(o);
        TAccumulate accumulator = map.get(key);
        if (accumulator == null) {
          accumulator = accumulatorInitializer.apply();
          accumulator = accumulatorAdder.apply(accumulator, o);
          map.put(key, accumulator);
        } else {
          TAccumulate accumulator0 = accumulator;
          accumulator = accumulatorAdder.apply(accumulator, o);
          if (accumulator != accumulator0) {
            map.put(key, accumulator);
          }
        }
      }
    }
  }
  return new LookupResultEnumerable<>(map, resultSelector);
}
 
Example 7
Source File: EnumerableDefaults.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static <TSource, TKey, TResult> Enumerable<TResult> groupBy_(
    final Set<TKey> map, Enumerable<TSource> enumerable,
    Function1<TSource, TKey> keySelector,
    final Function1<TKey, TResult> resultSelector) {
  try (Enumerator<TSource> os = enumerable.enumerator()) {
    while (os.moveNext()) {
      TSource o = os.current();
      TKey key = keySelector.apply(o);
      map.add(key);
    }
  }
  return Linq4j.asEnumerable(map).select(resultSelector);
}
 
Example 8
Source File: EnumerableDefaults.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static <TSource, TInner, TKey> Enumerable<TSource> semiJoinWithPredicate_(
    final Enumerable<TSource> outer, final Enumerable<TInner> inner,
    final Function1<TSource, TKey> outerKeySelector,
    final Function1<TInner, TKey> innerKeySelector,
    final EqualityComparer<TKey> comparer,
    final boolean anti,
    final Predicate2<TSource, TInner> nonEquiPredicate) {

  return new AbstractEnumerable<TSource>() {
    public Enumerator<TSource> enumerator() {
      // CALCITE-2909 Delay the computation of the innerLookup until the
      // moment when we are sure
      // that it will be really needed, i.e. when the first outer
      // enumerator item is processed
      final Supplier<Lookup<TKey, TInner>> innerLookup = Suppliers.memoize(
          () ->
              comparer == null
                  ? inner.toLookup(innerKeySelector)
                  : inner.toLookup(innerKeySelector, comparer));

      final Predicate1<TSource> predicate = v0 -> {
        TKey key = outerKeySelector.apply(v0);
        if (!innerLookup.get().containsKey(key)) {
          return anti;
        }
        Enumerable<TInner> innersOfKey = innerLookup.get().get(key);
        try (Enumerator<TInner> os = innersOfKey.enumerator()) {
          while (os.moveNext()) {
            TInner v1 = os.current();
            if (nonEquiPredicate.apply(v0, v1)) {
              return !anti;
            }
          }
          return anti;
        }
      };
      return EnumerableDefaults.where(outer.enumerator(), predicate);
    }
  };
}
 
Example 9
Source File: EnumerableDefaults.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Projects each element of a sequence into a new form.
 */
public static <TSource, TResult> Enumerable<TResult> select(
    final Enumerable<TSource> source,
    final Function1<TSource, TResult> selector) {
  if (selector == Functions.identitySelector()) {
    //noinspection unchecked
    return (Enumerable<TResult>) source;
  }
  return new AbstractEnumerable<TResult>() {
    public Enumerator<TResult> enumerator() {
      return new Enumerator<TResult>() {
        final Enumerator<TSource> enumerator = source.enumerator();

        public TResult current() {
          return selector.apply(enumerator.current());
        }

        public boolean moveNext() {
          return enumerator.moveNext();
        }

        public void reset() {
          enumerator.reset();
        }

        public void close() {
          enumerator.close();
        }
      };
    }
  };
}
 
Example 10
Source File: CalcitePrepareImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
<T> CalciteSignature<T> prepare_(
    Context context,
    Query<T> query,
    Type elementType,
    long maxRowCount) {
  if (SIMPLE_SQLS.contains(query.sql)) {
    return simplePrepare(context, query.sql);
  }
  final JavaTypeFactory typeFactory = context.getTypeFactory();
  CalciteCatalogReader catalogReader =
      new CalciteCatalogReader(
          context.getRootSchema(),
          context.getDefaultSchemaPath(),
          typeFactory,
          context.config());
  final List<Function1<Context, RelOptPlanner>> plannerFactories =
      createPlannerFactories();
  if (plannerFactories.isEmpty()) {
    throw new AssertionError("no planner factories");
  }
  RuntimeException exception = Util.FoundOne.NULL;
  for (Function1<Context, RelOptPlanner> plannerFactory : plannerFactories) {
    final RelOptPlanner planner = plannerFactory.apply(context);
    if (planner == null) {
      throw new AssertionError("factory returned null planner");
    }
    try {
      return prepare2_(context, query, elementType, maxRowCount,
          catalogReader, planner);
    } catch (RelOptPlanner.CannotPlanException e) {
      exception = e;
    }
  }
  throw exception;
}
 
Example 11
Source File: LazyAggregateLambdaFactory.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Function1<LazySource<TSource>, TResult> singleGroupResultSelector(
    Function1<TOrigAccumulate, TResult> resultSelector) {
  return lazySource -> {
    final TOrigAccumulate accumulator = accumulatorInitializer.apply();
    for (LazyAccumulator<TOrigAccumulate, TSource> acc : accumulators) {
      acc.accumulate(lazySource, accumulator);
    }
    return resultSelector.apply(accumulator);
  };
}
 
Example 12
Source File: EnumUtils.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Apply tumbling per row from the enumerable input.
 */
public static <TSource, TResult> Enumerable<TResult> tumbling(
    Enumerable<TSource> inputEnumerable,
    Function1<TSource, TResult> outSelector) {
  return new AbstractEnumerable<TResult>() {
    // Applies tumbling on each element from the input enumerator and produces
    // exactly one element for each input element.
    @Override public Enumerator<TResult> enumerator() {
      return new Enumerator<TResult>() {
        Enumerator<TSource> inputs = inputEnumerable.enumerator();

        public TResult current() {
          return outSelector.apply(inputs.current());
        }

        public boolean moveNext() {
          return inputs.moveNext();
        }

        public void reset() {
          inputs.reset();
        }

        public void close() {
        }
      };
    }
  };
}
 
Example 13
Source File: EnumerableDefaults.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * For each row of the {@code outer} enumerable returns the correlated rows
 * from the {@code inner} enumerable.
 */
public static <TSource, TInner, TResult> Enumerable<TResult> correlateJoin(
    final JoinType joinType, final Enumerable<TSource> outer,
    final Function1<TSource, Enumerable<TInner>> inner,
    final Function2<TSource, TInner, TResult> resultSelector) {
  if (joinType == JoinType.RIGHT || joinType == JoinType.FULL) {
    throw new IllegalArgumentException("JoinType " + joinType + " is not valid for correlation");
  }

  return new AbstractEnumerable<TResult>() {
    public Enumerator<TResult> enumerator() {
      return new Enumerator<TResult>() {
        private Enumerator<TSource> outerEnumerator = outer.enumerator();
        private Enumerator<TInner> innerEnumerator;
        TSource outerValue;
        TInner innerValue;
        int state = 0; // 0 -- moving outer, 1 moving inner;

        public TResult current() {
          return resultSelector.apply(outerValue, innerValue);
        }

        public boolean moveNext() {
          while (true) {
            switch (state) {
            case 0:
              // move outer
              if (!outerEnumerator.moveNext()) {
                return false;
              }
              outerValue = outerEnumerator.current();
              // initial move inner
              Enumerable<TInner> innerEnumerable = inner.apply(outerValue);
              if (innerEnumerable == null) {
                innerEnumerable = Linq4j.emptyEnumerable();
              }
              if (innerEnumerator != null) {
                innerEnumerator.close();
              }
              innerEnumerator = innerEnumerable.enumerator();
              if (innerEnumerator.moveNext()) {
                switch (joinType) {
                case ANTI:
                  // For anti-join need to try next outer row
                  // Current does not match
                  continue;
                case SEMI:
                  return true; // current row matches
                }
                // INNER and LEFT just return result
                innerValue = innerEnumerator.current();
                state = 1; // iterate over inner results
                return true;
              }
              // No match detected
              innerValue = null;
              switch (joinType) {
              case LEFT:
              case ANTI:
                return true;
              }
              // For INNER and LEFT need to find another outer row
              continue;
            case 1:
              // subsequent move inner
              if (innerEnumerator.moveNext()) {
                innerValue = innerEnumerator.current();
                return true;
              }
              state = 0;
              // continue loop, move outer
            }
          }
        }

        public void reset() {
          state = 0;
          outerEnumerator.reset();
          closeInner();
        }

        public void close() {
          outerEnumerator.close();
          closeInner();
          outerValue = null;
        }

        private void closeInner() {
          innerValue = null;
          if (innerEnumerator != null) {
            innerEnumerator.close();
            innerEnumerator = null;
          }
        }
      };
    }
  };
}
 
Example 14
Source File: ResultSetEnumerable.java    From calcite with Apache License 2.0 4 votes vote down vote up
ResultSetEnumerator(
    ResultSet resultSet,
    Function1<ResultSet, Function0<T>> rowBuilderFactory) {
  this.resultSet = resultSet;
  this.rowBuilder = rowBuilderFactory.apply(resultSet);
}