Java Code Examples for org.apache.calcite.adapter.enumerable.RexToLixTranslator#translateProjects()

The following examples show how to use org.apache.calcite.adapter.enumerable.RexToLixTranslator#translateProjects() . 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: HiveRexExecutorImpl.java    From marble with Apache License 2.0 5 votes vote down vote up
private String compile(RexBuilder rexBuilder, List<RexNode> constExps,
    RexToLixTranslator.InputGetter getter, RelDataType rowType) {
  final RexProgramBuilder programBuilder =
      new RexProgramBuilder(rowType, rexBuilder);
  for (RexNode node : constExps) {
    programBuilder.addProject(
        node, "c" + programBuilder.getProjectList().size());
  }
  final JavaTypeFactoryImpl javaTypeFactory =
      new JavaTypeFactoryImpl(rexBuilder.getTypeFactory().getTypeSystem());
  final BlockBuilder blockBuilder = new BlockBuilder();
  final ParameterExpression root0_ =
      Expressions.parameter(Object.class, "root0");
  final ParameterExpression root_ = DataContext.ROOT;
  blockBuilder.add(
      Expressions.declare(
          Modifier.FINAL, root_,
          Expressions.convert_(root0_, DataContext.class)));
  final SqlConformance conformance = SqlConformanceEnum.HIVE;
  final RexProgram program = programBuilder.getProgram();
  final List<Expression> expressions =
      RexToLixTranslator.translateProjects(program, javaTypeFactory,
          conformance, blockBuilder, null, root_, getter, null);
  blockBuilder.add(
      Expressions.return_(null,
          Expressions.newArrayInit(Object[].class, expressions)));
  final MethodDeclaration methodDecl =
      Expressions.methodDecl(Modifier.PUBLIC, Object[].class,
          BuiltInMethod.FUNCTION1_APPLY.method.getName(),
          ImmutableList.of(root0_), blockBuilder.toBlock());
  String code = Expressions.toString(methodDecl);
  if (CalcitePrepareImpl.DEBUG) {
    Util.debugCode(System.out, code);
  }
  return code;
}
 
Example 2
Source File: ExpressionCompiler.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
/**
 * Create quasi-Java expression from given {@link RexNode}
 *
 * @param node Expression in the form of {@link RexNode}
 * @param inputRowType Input Data type to expression in the form of {@link RelDataType}
 * @param outputRowType Output data type of expression in the form of {@link RelDataType}
 *
 * @return Returns quasi-Java expression
 */
public String getExpression(RexNode node, RelDataType inputRowType, RelDataType outputRowType)
{
  final RexProgramBuilder programBuilder = new RexProgramBuilder(inputRowType, rexBuilder);
  programBuilder.addProject(node, null);
  final RexProgram program = programBuilder.getProgram();

  final BlockBuilder builder = new BlockBuilder();
  final JavaTypeFactory javaTypeFactory = (JavaTypeFactory)rexBuilder.getTypeFactory();

  final RexToLixTranslator.InputGetter inputGetter = new RexToLixTranslator.InputGetterImpl(ImmutableList
      .of(Pair.<Expression, PhysType>of(Expressions.variable(Object[].class, "inputValues"),
      PhysTypeImpl.of(javaTypeFactory, inputRowType, JavaRowFormat.ARRAY, false))));
  final Function1<String, RexToLixTranslator.InputGetter> correlates =
      new Function1<String, RexToLixTranslator.InputGetter>()
    {
      public RexToLixTranslator.InputGetter apply(String a0)
      {
        throw new UnsupportedOperationException();
      }
    };

  final List<Expression> list = RexToLixTranslator.translateProjects(program, javaTypeFactory, builder,
      PhysTypeImpl.of(javaTypeFactory, outputRowType, JavaRowFormat.ARRAY, false), null, inputGetter, correlates);

  for (int i = 0; i < list.size(); i++) {
    Statement statement = Expressions.statement(list.get(i));
    builder.add(statement);
  }

  return finalizeExpression(builder.toBlock(), inputRowType);
}
 
Example 3
Source File: RexNodeToJavaCodeCompiler.java    From streamline with Apache License 2.0 5 votes vote down vote up
private BlockBuilder compileToBlock(final RexProgram program, ParameterExpression context_,
                                      ParameterExpression outputValues_) {
  RelDataType inputRowType = program.getInputRowType();
  final BlockBuilder builder = new BlockBuilder();
  final JavaTypeFactoryImpl javaTypeFactory =
          new JavaTypeFactoryImpl(rexBuilder.getTypeFactory().getTypeSystem());

  final RexToLixTranslator.InputGetter inputGetter =
          new RexToLixTranslator.InputGetterImpl(
                  ImmutableList.of(
                          Pair.<Expression, PhysType>of(
                                  Expressions.field(context_,
                                          BuiltInMethod.CONTEXT_VALUES.field),
                                  PhysTypeImpl.of(javaTypeFactory, inputRowType,
                                          JavaRowFormat.ARRAY, false))));
  final Function1<String, RexToLixTranslator.InputGetter> correlates =
          new Function1<String, RexToLixTranslator.InputGetter>() {
            public RexToLixTranslator.InputGetter apply(String a0) {
              throw new UnsupportedOperationException();
            }
          };
  final Expression root =
          Expressions.field(context_, BuiltInMethod.CONTEXT_ROOT.field);
  final List<Expression> list =
          RexToLixTranslator.translateProjects(program, javaTypeFactory, builder,
                  null, root, inputGetter, correlates);
  for (int i = 0; i < list.size(); i++) {
    builder.add(
            Expressions.statement(
                    Expressions.assign(
                            Expressions.arrayIndex(outputValues_,
                                    Expressions.constant(i)),
                            list.get(i))));
  }

  return builder;
}
 
Example 4
Source File: RexExecutorImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static String compile(RexBuilder rexBuilder, List<RexNode> constExps,
    RexToLixTranslator.InputGetter getter, RelDataType rowType) {
  final RexProgramBuilder programBuilder =
      new RexProgramBuilder(rowType, rexBuilder);
  for (RexNode node : constExps) {
    programBuilder.addProject(
        node, "c" + programBuilder.getProjectList().size());
  }
  final JavaTypeFactoryImpl javaTypeFactory =
      new JavaTypeFactoryImpl(rexBuilder.getTypeFactory().getTypeSystem());
  final BlockBuilder blockBuilder = new BlockBuilder();
  final ParameterExpression root0_ =
      Expressions.parameter(Object.class, "root0");
  final ParameterExpression root_ = DataContext.ROOT;
  blockBuilder.add(
      Expressions.declare(
          Modifier.FINAL, root_,
          Expressions.convert_(root0_, DataContext.class)));
  final SqlConformance conformance = SqlConformanceEnum.DEFAULT;
  final RexProgram program = programBuilder.getProgram();
  final List<Expression> expressions =
      RexToLixTranslator.translateProjects(program, javaTypeFactory,
          conformance, blockBuilder, null, root_, getter, null);
  blockBuilder.add(
      Expressions.return_(null,
          Expressions.newArrayInit(Object[].class, expressions)));
  final MethodDeclaration methodDecl =
      Expressions.methodDecl(Modifier.PUBLIC, Object[].class,
          BuiltInMethod.FUNCTION1_APPLY.method.getName(),
          ImmutableList.of(root0_), blockBuilder.toBlock());
  String code = Expressions.toString(methodDecl);
  if (CalciteSystemProperty.DEBUG.value()) {
    Util.debugCode(System.out, code);
  }
  return code;
}
 
Example 5
Source File: SparkRules.java    From calcite with Apache License 2.0 4 votes vote down vote up
public Result implementSpark(Implementor implementor) {
  final JavaTypeFactory typeFactory = implementor.getTypeFactory();
  final BlockBuilder builder = new BlockBuilder();
  final SparkRel child = (SparkRel) getInput();

  final Result result = implementor.visitInput(this, 0, child);

  final PhysType physType =
      PhysTypeImpl.of(
          typeFactory, getRowType(), JavaRowFormat.CUSTOM);

  // final RDD<Employee> inputRdd = <<child adapter>>;
  // return inputRdd.flatMap(
  //   new FlatMapFunction<Employee, X>() {
  //          public List<X> call(Employee e) {
  //              if (!(e.empno < 10)) {
  //                  return Collections.emptyList();
  //              }
  //              return Collections.singletonList(
  //                  new X(...)));
  //          }
  //      })


  Type outputJavaType = physType.getJavaRowType();
  final Type rddType =
      Types.of(
          JavaRDD.class, outputJavaType);
  Type inputJavaType = result.physType.getJavaRowType();
  final Expression inputRdd_ =
      builder.append(
          "inputRdd",
          result.block);

  BlockBuilder builder2 = new BlockBuilder();

  final ParameterExpression e_ =
      Expressions.parameter(inputJavaType, "e");
  if (program.getCondition() != null) {
    Expression condition =
        RexToLixTranslator.translateCondition(
            program,
            typeFactory,
            builder2,
            new RexToLixTranslator.InputGetterImpl(
                Collections.singletonList(
                    Pair.of((Expression) e_, result.physType))),
            null, implementor.getConformance());
    builder2.add(
        Expressions.ifThen(
            Expressions.not(condition),
            Expressions.return_(null,
                Expressions.call(
                    BuiltInMethod.COLLECTIONS_EMPTY_LIST.method))));
  }

  final SqlConformance conformance = SqlConformanceEnum.DEFAULT;
  List<Expression> expressions =
      RexToLixTranslator.translateProjects(
          program,
          typeFactory,
          conformance,
          builder2,
          null,
          DataContext.ROOT,
          new RexToLixTranslator.InputGetterImpl(
              Collections.singletonList(
                  Pair.of((Expression) e_, result.physType))),
          null);
  builder2.add(
      Expressions.return_(null,
          Expressions.convert_(
              Expressions.call(
                  BuiltInMethod.COLLECTIONS_SINGLETON_LIST.method,
                  physType.record(expressions)),
              List.class)));

  final BlockStatement callBody = builder2.toBlock();
  builder.add(
      Expressions.return_(
          null,
          Expressions.call(
              inputRdd_,
              SparkMethod.RDD_FLAT_MAP.method,
              Expressions.lambda(
                  SparkRuntime.CalciteFlatMapFunction.class,
                  callBody,
                  e_))));
  return implementor.result(physType, builder.toBlock());
}
 
Example 6
Source File: JaninoRexCompiler.java    From calcite with Apache License 2.0 4 votes vote down vote up
public Scalar compile(List<RexNode> nodes, RelDataType inputRowType) {
  final RexProgramBuilder programBuilder =
      new RexProgramBuilder(inputRowType, rexBuilder);
  for (RexNode node : nodes) {
    programBuilder.addProject(node, null);
  }
  final RexProgram program = programBuilder.getProgram();

  final BlockBuilder builder = new BlockBuilder();
  final ParameterExpression context_ =
      Expressions.parameter(Context.class, "context");
  final ParameterExpression outputValues_ =
      Expressions.parameter(Object[].class, "outputValues");
  final JavaTypeFactoryImpl javaTypeFactory =
      new JavaTypeFactoryImpl(rexBuilder.getTypeFactory().getTypeSystem());

  // public void execute(Context, Object[] outputValues)
  final RexToLixTranslator.InputGetter inputGetter =
      new RexToLixTranslator.InputGetterImpl(
          ImmutableList.of(
              Pair.of(
                  Expressions.field(context_,
                      BuiltInMethod.CONTEXT_VALUES.field),
                  PhysTypeImpl.of(javaTypeFactory, inputRowType,
                      JavaRowFormat.ARRAY, false))));
  final Function1<String, RexToLixTranslator.InputGetter> correlates = a0 -> {
    throw new UnsupportedOperationException();
  };
  final Expression root =
      Expressions.field(context_, BuiltInMethod.CONTEXT_ROOT.field);
  final SqlConformance conformance =
      SqlConformanceEnum.DEFAULT; // TODO: get this from implementor
  final List<Expression> list =
      RexToLixTranslator.translateProjects(program, javaTypeFactory,
          conformance, builder, null, root, inputGetter, correlates);
  for (int i = 0; i < list.size(); i++) {
    builder.add(
        Expressions.statement(
            Expressions.assign(
                Expressions.arrayIndex(outputValues_,
                    Expressions.constant(i)),
                list.get(i))));
  }
  return baz(context_, outputValues_, builder.toBlock());
}