use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.BlockBuilder in project streamline by hortonworks.
the class RexNodeToJavaCodeCompiler method compileToBlock.
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;
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.BlockBuilder in project calcite by apache.
the class JaninoRexCompiler method compile.
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.<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 baz(context_, outputValues_, builder.toBlock());
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.BlockBuilder in project calcite by apache.
the class BlockBuilderTest method testReuseExpressionsFromUpperLevel.
@Test
public void testReuseExpressionsFromUpperLevel() {
Expression x = b.append("x", Expressions.add(ONE, TWO));
BlockBuilder nested = new BlockBuilder(true, b);
Expression y = nested.append("y", Expressions.add(ONE, TWO));
nested.add(Expressions.return_(null, Expressions.add(y, y)));
b.add(nested.toBlock());
assertEquals("{\n" + " final int x = 1 + 2;\n" + " {\n" + " return x + x;\n" + " }\n" + "}\n", b.toBlock().toString());
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.BlockBuilder in project calcite by apache.
the class BlockBuilderTest method testTestCustomOptimizer.
@Test
public void testTestCustomOptimizer() {
BlockBuilder b = new BlockBuilder() {
@Override
protected Shuttle createOptimizeShuttle() {
return new OptimizeShuttle() {
@Override
public Expression visit(BinaryExpression binary, Expression expression0, Expression expression1) {
if (binary.getNodeType() == ExpressionType.Add && ONE.equals(expression0) && TWO.equals(expression1)) {
return FOUR;
}
return super.visit(binary, expression0, expression1);
}
};
}
};
b.add(Expressions.return_(null, Expressions.add(ONE, TWO)));
assertEquals("{\n return 4;\n}\n", b.toBlock().toString());
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.BlockBuilder in project calcite by apache.
the class ExpressionTest method testBlockBuilder2.
@Test
public void testBlockBuilder2() {
BlockBuilder statements = new BlockBuilder();
Expression element = statements.append("element", Expressions.constant(null));
Expression comparator = statements.append("comparator", Expressions.constant(null, Comparator.class));
Expression treeSet = statements.append("treeSet", Expressions.new_(TreeSet.class, Arrays.asList(comparator)));
statements.add(Expressions.return_(null, Expressions.call(treeSet, "add", element)));
BlockStatement expression = statements.toBlock();
final String expected = "{\n" + " final java.util.TreeSet treeSet = new java.util.TreeSet(\n" + " (java.util.Comparator) null);\n" + " return treeSet.add(null);\n" + "}\n";
assertThat(Expressions.toString(expression), is(expected));
expression.accept(new Shuttle());
}
Aggregations