Java Code Examples for org.apache.calcite.linq4j.Ord#forEach()

The following examples show how to use org.apache.calcite.linq4j.Ord#forEach() . 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: RelDecorrelator.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Projects all {@code input} output fields plus the additional expressions.
 *
 * @param input        Input relational expression
 * @param additionalExprs Additional expressions and names
 * @return the new Project
 */
private RelNode createProjectWithAdditionalExprs(
    RelNode input,
    List<Pair<RexNode, String>> additionalExprs) {
  final List<RelDataTypeField> fieldList =
      input.getRowType().getFieldList();
  List<Pair<RexNode, String>> projects = new ArrayList<>();
  Ord.forEach(fieldList, (field, i) ->
      projects.add(
          Pair.of(relBuilder.getRexBuilder().makeInputRef(field.getType(), i),
              field.getName())));
  projects.addAll(additionalExprs);
  return relBuilder.push(input)
      .projectNamed(Pair.left(projects), Pair.right(projects), true)
      .build();
}
 
Example 2
Source File: RelToSqlConverter.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Creates operands for a full AS operator. Format SqlNode AS alias(col_1, col_2,... ,col_n).
 *
 * @param rowType Row type of the SqlNode
 * @param leftOperand SqlNode
 * @param alias alias
 */
public List<SqlNode> createAsFullOperands(RelDataType rowType, SqlNode leftOperand,
    String alias) {
  final List<SqlNode> result = new ArrayList<>();
  result.add(leftOperand);
  result.add(new SqlIdentifier(alias, POS));
  Ord.forEach(rowType.getFieldNames(), (fieldName, i) -> {
    if (fieldName.toLowerCase(Locale.ROOT).startsWith("expr$")) {
      fieldName = "col_" + i;
    }
    result.add(new SqlIdentifier(fieldName, POS));
  });
  return result;
}
 
Example 3
Source File: UtilTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Tests {@link Ord#forEach(Iterable, ObjIntConsumer)}. */
@Test void testOrdForEach() {
  final String[] strings = {"ab", "", "cde"};
  final StringBuilder b = new StringBuilder();
  final String expected = "0:ab;1:;2:cde;";

  Ord.forEach(strings,
      (e, i) -> b.append(i).append(":").append(e).append(";"));
  assertThat(b.toString(), is(expected));
  b.setLength(0);

  final List<String> list = Arrays.asList(strings);
  Ord.forEach(list, (e, i) -> b.append(i).append(":").append(e).append(";"));
  assertThat(b.toString(), is(expected));
}
 
Example 4
Source File: EnumerableMatch.java    From calcite with Apache License 2.0 4 votes vote down vote up
private Expression implementEmitter(EnumerableRelImplementor implementor,
    PhysType physType, PhysType inputPhysType) {
  final ParameterExpression rows_ =
      Expressions.parameter(Types.of(List.class, inputPhysType.getJavaRowType()), "rows");
  final ParameterExpression rowStates_ =
      Expressions.parameter(List.class, "rowStates");
  final ParameterExpression symbols_ =
      Expressions.parameter(List.class, "symbols");
  final ParameterExpression match_ =
      Expressions.parameter(int.class, "match");
  final ParameterExpression consumer_ =
      Expressions.parameter(Consumer.class, "consumer");
  final ParameterExpression i_ = Expressions.parameter(int.class, "i");

  final ParameterExpression row_ =
      Expressions.parameter(inputPhysType.getJavaRowType(), "row");

  final BlockBuilder builder2 = new BlockBuilder();

  // Add loop variable initialization
  builder2.add(
      Expressions.declare(0, row_,
          EnumUtils.convert(
              Expressions.call(rows_, BuiltInMethod.LIST_GET.method, i_),
              inputPhysType.getJavaRowType())));

  RexBuilder rexBuilder = new RexBuilder(implementor.getTypeFactory());
  RexProgramBuilder rexProgramBuilder =
      new RexProgramBuilder(inputPhysType.getRowType(), rexBuilder);
  for (Map.Entry<String, RexNode> entry : measures.entrySet()) {
    rexProgramBuilder.addProject(entry.getValue(), entry.getKey());
  }

  final RexToLixTranslator translator = RexToLixTranslator.forAggregation(
      (JavaTypeFactory) getCluster().getTypeFactory(),
      builder2,
      new PassedRowsInputGetter(row_, rows_, inputPhysType),
      implementor.getConformance());

  final ParameterExpression result_ =
      Expressions.parameter(physType.getJavaRowType());

  builder2.add(
      Expressions.declare(Modifier.FINAL, result_,
          Expressions.new_(physType.getJavaRowType())));
  Ord.forEach(measures.values(), (measure, i) ->
      builder2.add(
          Expressions.statement(
              Expressions.assign(physType.fieldReference(result_, i),
                  implementMeasure(translator, rows_, symbols_, i_, row_,
                      measure)))));
  builder2.add(
      Expressions.statement(
          Expressions.call(consumer_, BuiltInMethod.CONSUMER_ACCEPT.method,
              result_)));

  final BlockBuilder builder = new BlockBuilder();

  // Loop Length

  // we have to use an explicit for (int i = ...) loop, as we need to know later
  // which of the matched rows are already passed (in MatchUtils), so foreach cannot be used
  builder.add(
      Expressions.for_(
          Expressions.declare(0, i_, Expressions.constant(0)),
          Expressions.lessThan(i_,
              Expressions.call(rows_, BuiltInMethod.COLLECTION_SIZE.method)),
          Expressions.preIncrementAssign(i_),
          builder2.toBlock()));

  return Expressions.new_(
      Types.of(Enumerables.Emitter.class), NO_EXPRS,
      Expressions.list(
          EnumUtils.overridingMethodDecl(
              BuiltInMethod.EMITTER_EMIT.method,
              ImmutableList.of(rows_, rowStates_, symbols_, match_,
                  consumer_),
              builder.toBlock())));
}