org.apache.calcite.linq4j.function.Function1 Java Examples

The following examples show how to use org.apache.calcite.linq4j.function.Function1. 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
/**
 * Produces the set intersection of two sequences by
 * using the specified {@code EqualityComparer<TSource>} to compare
 * values, using {@code all} to indicate whether to eliminate duplicates.
 */
public static <TSource> Enumerable<TSource> intersect(
    Enumerable<TSource> source0, Enumerable<TSource> source1,
    EqualityComparer<TSource> comparer, boolean all) {
  if (comparer == Functions.identityComparer()) {
    return intersect(source0, source1, all);
  }
  Collection<Wrapped<TSource>> collection = all ? HashMultiset.create() : new HashSet<>();
  Function1<TSource, Wrapped<TSource>> wrapper = wrapperFor(comparer);
  source1.select(wrapper).into(collection);
  Collection<Wrapped<TSource>> resultCollection = all ? HashMultiset.create() : new HashSet<>();
  try (Enumerator<Wrapped<TSource>> os = source0.select(wrapper).enumerator()) {
    while (os.moveNext()) {
      Wrapped<TSource> o = os.current();
      if (collection.remove(o)) {
        resultCollection.add(o);
      }
    }
  }
  Function1<Wrapped<TSource>, TSource> unwrapper = unwrapper();
  return Linq4j.asEnumerable(resultCollection).select(unwrapper);
}
 
Example #2
Source File: EnumerableDefaults.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Correlates the elements of two sequences based on
 * matching keys. A specified {@code EqualityComparer<TSource>} is used to
 * compare keys.
 */
public static <TSource, TInner, TKey, TResult> Enumerable<TResult> hashJoin(
    Enumerable<TSource> outer, Enumerable<TInner> inner,
    Function1<TSource, TKey> outerKeySelector,
    Function1<TInner, TKey> innerKeySelector,
    Function2<TSource, TInner, TResult> resultSelector,
    EqualityComparer<TKey> comparer, boolean generateNullsOnLeft,
    boolean generateNullsOnRight) {
  return hashEquiJoin_(
      outer,
      inner,
      outerKeySelector,
      innerKeySelector,
      resultSelector,
      comparer,
      generateNullsOnLeft,
      generateNullsOnRight);
}
 
Example #3
Source File: EnumerableDefaults.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Sorts the elements of a sequence in ascending
 * order by using a specified comparer.
 */
public static <TSource, TKey> Enumerable<TSource> orderBy(
    Enumerable<TSource> source, Function1<TSource, TKey> keySelector,
    Comparator<TKey> comparator) {
  return new AbstractEnumerable<TSource>() {
    @Override public Enumerator<TSource> enumerator() {
      // NOTE: TreeMap allows null comparator. But the caller of this method
      // must supply a comparator if the key does not extend Comparable.
      // Otherwise there will be a ClassCastException while retrieving.
      final Map<TKey, List<TSource>> map = new TreeMap<>(comparator);
      final LookupImpl<TKey, TSource> lookup = toLookup_(map, source, keySelector,
          Functions.identitySelector());
      return lookup.valuesEnumerable().enumerator();
    }
  };
}
 
Example #4
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 #5
Source File: EnumerableDefaults.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Correlates the elements of two sequences based on
 * matching keys. A specified {@code EqualityComparer<TSource>} is used to
 * compare keys.
 */
public static <TSource, TInner, TKey, TResult> Enumerable<TResult> hashJoin(
    Enumerable<TSource> outer, Enumerable<TInner> inner,
    Function1<TSource, TKey> outerKeySelector,
    Function1<TInner, TKey> innerKeySelector,
    Function2<TSource, TInner, TResult> resultSelector,
    EqualityComparer<TKey> comparer) {
  return hashJoin(
      outer,
      inner,
      outerKeySelector,
      innerKeySelector,
      resultSelector,
      comparer,
      false,
      false);
}
 
Example #6
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 #7
Source File: EnumerableDefaults.java    From calcite with Apache License 2.0 5 votes vote down vote up
public static <TSource, TInner, TKey> Enumerable<TSource> antiJoin(
    final Enumerable<TSource> outer, final Enumerable<TInner> inner,
    final Function1<TSource, TKey> outerKeySelector,
    final Function1<TInner, TKey> innerKeySelector,
    final EqualityComparer<TKey> comparer) {
  return semiEquiJoin_(outer, inner, outerKeySelector, innerKeySelector, comparer,
      true);
}
 
Example #8
Source File: EnumerableDefaults.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Returns elements of {@code outer} for which there is NOT a member of
 * {@code inner} with a matching key.
 */
public static <TSource, TInner, TKey> Enumerable<TSource> antiJoin(
    final Enumerable<TSource> outer, final Enumerable<TInner> inner,
    final Function1<TSource, TKey> outerKeySelector,
    final Function1<TInner, TKey> innerKeySelector) {
  return semiEquiJoin_(outer, inner, outerKeySelector, innerKeySelector, null,
      true);
}
 
Example #9
Source File: QueryableRecorder.java    From calcite with Apache License 2.0 5 votes vote down vote up
public <TKey extends Comparable<TKey>> OrderedQueryable<T> thenByDescending(
    final OrderedQueryable<T> source,
    final FunctionExpression<Function1<T, TKey>> keySelector) {
  return new NonLeafReplayableQueryable<T>(source) {
    public void replay(QueryableFactory<T> factory) {
      factory.thenByDescending(source, keySelector);
    }
  };
}
 
Example #10
Source File: DefaultEnumerable.java    From calcite with Apache License 2.0 5 votes vote down vote up
public <TKey, TAccumulate, TResult> Enumerable<TResult> groupBy(
    Function1<T, TKey> keySelector,
    Function0<TAccumulate> accumulatorInitializer,
    Function2<TAccumulate, T, TAccumulate> accumulatorAdder,
    Function2<TKey, TAccumulate, TResult> resultSelector,
    EqualityComparer<TKey> comparer) {
  return EnumerableDefaults.groupBy(getThis(), keySelector,
      accumulatorInitializer, accumulatorAdder, resultSelector, comparer);
}
 
Example #11
Source File: EnumerableQueryable.java    From calcite with Apache License 2.0 5 votes vote down vote up
public <TCollection, TResult> Queryable<TResult> selectManyN(
    FunctionExpression<Function1<T, Enumerable<TCollection>>>
      collectionSelector,
    FunctionExpression<Function2<T, TCollection, TResult>> resultSelector) {
  return EnumerableDefaults.selectMany(getThis(),
      collectionSelector.getFunction(), resultSelector.getFunction())
      .asQueryable();
}
 
Example #12
Source File: QueryableRecorder.java    From calcite with Apache License 2.0 5 votes vote down vote up
public <TKey> OrderedQueryable<T> orderByDescending(final Queryable<T> source,
    final FunctionExpression<Function1<T, TKey>> keySelector,
    final Comparator<TKey> comparator) {
  return new NonLeafReplayableQueryable<T>(source) {
    public void replay(QueryableFactory<T> factory) {
      factory.orderByDescending(source, keySelector, comparator);
    }
  };
}
 
Example #13
Source File: TpcdsSchema.java    From calcite with Apache License 2.0 5 votes vote down vote up
public <T> Queryable<T> asQueryable(final QueryProvider queryProvider,
    final SchemaPlus schema, final String tableName) {
  //noinspection unchecked
  return (Queryable) new AbstractTableQueryable<Object[]>(queryProvider,
      schema, this, tableName) {
    public Enumerator<Object[]> enumerator() {
      final Session session =
          Session.getDefaultSession()
              .withTable(tpcdsTable)
              .withScale(scaleFactor);
      final Results results = Results.constructResults(tpcdsTable, session);
      return Linq4j.asEnumerable(results)
          .selectMany(
              new Function1<List<List<String>>, Enumerable<Object[]>>() {
                final Column[] columns = tpcdsTable.getColumns();

                public Enumerable<Object[]> apply(
                    List<List<String>> inRows) {
                  final List<Object[]> rows = new ArrayList<>();
                  for (List<String> strings : inRows) {
                    final Object[] values = new Object[columns.length];
                    for (int i = 0; i < strings.size(); i++) {
                      values[i] = convert(strings.get(i), columns[i]);
                    }
                    rows.add(values);
                  }
                  return Linq4j.asEnumerable(rows);
                }

              })
          .enumerator();
    }
  };
}
 
Example #14
Source File: QueryableRelBuilder.java    From calcite with Apache License 2.0 5 votes vote down vote up
public <TInner, TKey, TResult> Queryable<TResult> groupJoin(
    Queryable<T> source,
    Enumerable<TInner> inner,
    FunctionExpression<Function1<T, TKey>> outerKeySelector,
    FunctionExpression<Function1<TInner, TKey>> innerKeySelector,
    FunctionExpression<Function2<T, Enumerable<TInner>, TResult>>
        resultSelector) {
  throw new UnsupportedOperationException();
}
 
Example #15
Source File: ResultSetEnumerable.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static Function1<ResultSet, Function0<Object>>
    primitiveRowBuilderFactory(final Primitive[] primitives) {
  return resultSet -> {
    final ResultSetMetaData metaData;
    final int columnCount;
    try {
      metaData = resultSet.getMetaData();
      columnCount = metaData.getColumnCount();
    } catch (SQLException e) {
      throw new RuntimeException(e);
    }
    assert columnCount == primitives.length;
    if (columnCount == 1) {
      return () -> {
        try {
          return resultSet.getObject(1);
        } catch (SQLException e) {
          throw new RuntimeException(e);
        }
      };
    }
    //noinspection unchecked
    return (Function0) () -> {
      try {
        final List<Object> list = new ArrayList<>();
        for (int i = 0; i < columnCount; i++) {
          list.add(primitives[i].jdbcGet(resultSet, i + 1));
        }
        return list.toArray();
      } catch (SQLException e) {
        throw new RuntimeException(e);
      }
    };
  };
}
 
Example #16
Source File: QueryableRelBuilder.java    From calcite with Apache License 2.0 5 votes vote down vote up
public <TKey, TElement, TResult> Queryable<TResult> groupBy(
    Queryable<T> source,
    FunctionExpression<Function1<T, TKey>> keySelector,
    FunctionExpression<Function1<T, TElement>> elementSelector,
    FunctionExpression<Function2<TKey, Enumerable<TElement>, TResult>>
        resultSelector) {
  throw new UnsupportedOperationException();
}
 
Example #17
Source File: QueryableRecorder.java    From calcite with Apache License 2.0 5 votes vote down vote up
public <TInner, TKey, TResult> Queryable<TResult> groupJoin(
    final Queryable<T> source, final Enumerable<TInner> inner,
    final FunctionExpression<Function1<T, TKey>> outerKeySelector,
    final FunctionExpression<Function1<TInner, TKey>> innerKeySelector,
    final FunctionExpression<Function2<T, Enumerable<TInner>, TResult>> resultSelector,
    final EqualityComparer<TKey> comparer) {
  return new NonLeafReplayableQueryable<T>(source) {
    public void replay(QueryableFactory<T> factory) {
      factory.groupJoin(source, inner, outerKeySelector, innerKeySelector,
          resultSelector, comparer);
    }
  }.castQueryable(); // CHECKSTYLE: IGNORE 0
}
 
Example #18
Source File: EnumerableDefaults.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Joins two inputs that are sorted on the key.
 * Inputs must sorted in ascending order, nulls last.
 */
public static <TSource, TInner, TKey extends Comparable<TKey>, TResult> Enumerable<TResult>
    mergeJoin(final Enumerable<TSource> outer,
    final Enumerable<TInner> inner,
    final Function1<TSource, TKey> outerKeySelector,
    final Function1<TInner, TKey> innerKeySelector,
    final Function2<TSource, TInner, TResult> resultSelector,
    final JoinType joinType,
    final Comparator<TKey> comparator) {
  return mergeJoin(outer, inner, outerKeySelector, innerKeySelector, null, resultSelector,
      joinType, comparator);
}
 
Example #19
Source File: DefaultEnumerable.java    From calcite with Apache License 2.0 5 votes vote down vote up
public <TInner, TKey, TResult> Enumerable<TResult> hashJoin(
    Enumerable<TInner> inner, Function1<T, TKey> outerKeySelector,
    Function1<TInner, TKey> innerKeySelector,
    Function2<T, TInner, TResult> resultSelector) {
  return EnumerableDefaults.hashJoin(getThis(), inner, outerKeySelector,
      innerKeySelector, resultSelector);
}
 
Example #20
Source File: DefaultQueryable.java    From calcite with Apache License 2.0 5 votes vote down vote up
public <TKey, TElement, TResult> Queryable<TResult> groupBy(
    FunctionExpression<Function1<T, TKey>> keySelector,
    FunctionExpression<Function1<T, TElement>> elementSelector,
    FunctionExpression<Function2<TKey, Enumerable<TElement>, TResult>> resultSelector,
    EqualityComparer<TKey> comparer) {
  return factory.groupBy(getThis(), keySelector, elementSelector,
      resultSelector, comparer);
}
 
Example #21
Source File: DefaultQueryable.java    From calcite with Apache License 2.0 5 votes vote down vote up
public <TInner, TKey, TResult> Queryable<TResult> groupJoin(
    Enumerable<TInner> inner,
    FunctionExpression<Function1<T, TKey>> outerKeySelector,
    FunctionExpression<Function1<TInner, TKey>> innerKeySelector,
    FunctionExpression<Function2<T, Enumerable<TInner>, TResult>> resultSelector,
    EqualityComparer<TKey> comparer) {
  return factory.groupJoin(getThis(), inner, outerKeySelector,
      innerKeySelector, resultSelector, comparer);
}
 
Example #22
Source File: DefaultQueryable.java    From calcite with Apache License 2.0 5 votes vote down vote up
public <TInner, TKey, TResult> Queryable<TResult> join(
    Enumerable<TInner> inner,
    FunctionExpression<Function1<T, TKey>> outerKeySelector,
    FunctionExpression<Function1<TInner, TKey>> innerKeySelector,
    FunctionExpression<Function2<T, TInner, TResult>> resultSelector) {
  return factory.join(getThis(), inner, outerKeySelector, innerKeySelector,
      resultSelector);
}
 
Example #23
Source File: RexToLixTranslator.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RexToLixTranslator setCorrelates(
    Function1<String, InputGetter> correlates) {
  if (this.correlates == correlates) {
    return this;
  }
  return new RexToLixTranslator(program, typeFactory, root, inputGetter, list,
      builder, conformance, correlates);
}
 
Example #24
Source File: QueryableFactory.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Correlates the elements of two sequences based on
 * matching keys. A specified EqualityComparer is used to
 * compare keys.
 */
<TInner, TKey, TResult> Queryable<TResult> join(Queryable<T> source,
    Enumerable<TInner> inner,
    FunctionExpression<Function1<T, TKey>> outerKeySelector,
    FunctionExpression<Function1<TInner, TKey>> innerKeySelector,
    FunctionExpression<Function2<T, TInner, TResult>> resultSelector,
    EqualityComparer<TKey> comparer);
 
Example #25
Source File: EnumerableQueryable.java    From calcite with Apache License 2.0 5 votes vote down vote up
public <TInner, TKey, TResult> Queryable<TResult> groupJoin(
    Enumerable<TInner> inner,
    FunctionExpression<Function1<T, TKey>> outerKeySelector,
    FunctionExpression<Function1<TInner, TKey>> innerKeySelector,
    FunctionExpression<Function2<T, Enumerable<TInner>, TResult>> resultSelector,
    EqualityComparer<TKey> comparer) {
  return EnumerableDefaults.groupJoin(getThis(), inner,
      outerKeySelector.getFunction(), innerKeySelector.getFunction(),
      resultSelector.getFunction(), comparer).asQueryable();
}
 
Example #26
Source File: EnumerableDefaults.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Groups the elements of a sequence according to a
 * specified key selector function and creates a result value from
 * each group and its key. The keys are compared by using a
 * specified comparer.
 */
public static <TSource, TKey, TResult> Enumerable<TResult> groupBy(
    Enumerable<TSource> enumerable, Function1<TSource, TKey> keySelector,
    final Function2<TKey, Enumerable<TSource>, TResult> resultSelector,
    EqualityComparer<TKey> comparer) {
  return enumerable.toLookup(keySelector, comparer)
      .select(group -> resultSelector.apply(group.getKey(), group));
}
 
Example #27
Source File: EnumerableQueryable.java    From calcite with Apache License 2.0 5 votes vote down vote up
public <TKey, TElement, TResult> Queryable<TResult> groupBy(
    FunctionExpression<Function1<T, TKey>> keySelector,
    FunctionExpression<Function1<T, TElement>> elementSelector,
    FunctionExpression<Function2<TKey, Enumerable<TElement>, TResult>> resultSelector) {
  return EnumerableDefaults.groupBy(getThis(), keySelector.getFunction(),
      elementSelector.getFunction(), resultSelector.getFunction())
      .asQueryable();
}
 
Example #28
Source File: DefaultEnumerable.java    From calcite with Apache License 2.0 5 votes vote down vote up
public <TKey, TAccumulate, TResult> Enumerable<TResult> groupBy(
    Function1<T, TKey> keySelector,
    Function0<TAccumulate> accumulatorInitializer,
    Function2<TAccumulate, T, TAccumulate> accumulatorAdder,
    Function2<TKey, TAccumulate, TResult> resultSelector) {
  return EnumerableDefaults.groupBy(getThis(), keySelector,
      accumulatorInitializer, accumulatorAdder, resultSelector);
}
 
Example #29
Source File: EnumerableDefaults.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Groups the elements of a sequence according to a
 * specified key selector function, initializing an accumulator for each
 * group and adding to it each time an element with the same key is seen.
 * Creates a result value from each accumulator and its key using a
 * specified function.
 */
public static <TSource, TKey, TAccumulate, TResult> Enumerable<TResult> groupBy(
    Enumerable<TSource> enumerable, Function1<TSource, TKey> keySelector,
    Function0<TAccumulate> accumulatorInitializer,
    Function2<TAccumulate, TSource, TAccumulate> accumulatorAdder,
    final Function2<TKey, TAccumulate, TResult> resultSelector) {
  return groupBy_(new HashMap<>(), enumerable, keySelector,
      accumulatorInitializer, accumulatorAdder, resultSelector);
}
 
Example #30
Source File: PhysTypeImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Expression convertTo(Expression exp, JavaRowFormat targetFormat) {
  if (format == targetFormat) {
    return exp;
  }
  final ParameterExpression o_ =
      Expressions.parameter(javaRowClass, "o");
  final int fieldCount = rowType.getFieldCount();
  // The conversion must be strict so optimizations of the targetFormat should not be performed
  // by the code that follows. If necessary the target format can be optimized before calling
  // this method.
  PhysType targetPhysType = PhysTypeImpl.of(typeFactory, rowType, targetFormat, false);
  final Expression selector = Expressions.lambda(Function1.class,
      targetPhysType.record(fieldReferences(o_, Util.range(fieldCount))), o_);
  return Expressions.call(exp, BuiltInMethod.SELECT.method, selector);
}