use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.BlockBuilder in project calcite by apache.
the class RexExecutorImpl method compile.
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 List<Expression> expressions = RexToLixTranslator.translateProjects(programBuilder.getProgram(), javaTypeFactory, 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;
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.BlockBuilder in project samza by apache.
the class RexToJavaCompiler method compile.
/**
* Compiles a relational expression to a instance of {@link Expression}
*
* for e.g.
* Query : select id from profile
* where profile table has relational schema with id(NUMBER) and name(VARCHAR) columns.
* This query will result in the following relational plan
* LogicalProject(id=[$1])
* LogicalTableScan(table=[[profile]])
*
* And the corresponding expressions are
* inputs : EnumerableTableScan (Which is the output of LogicalTableScan)
* nodes : [$1] Which essentially means take pick the first column from the input
*
* This function converts the LogicalProject expression "[$1]" with input RexNode which is an output of TableScan
* to a java code that implements the interface {@link Expression}
*
* @param inputs Input relations/time-varying relations for this row expression
* @param nodes relational expressions that needs to be converted to java code.
* @return compiled expression of type {@link org.apache.samza.sql.data.Expression}
*/
public org.apache.samza.sql.data.Expression compile(List<RelNode> inputs, List<RexNode> nodes) {
/*
* In case there are multiple input relations, we build a single input row type combining types of all the inputs.
*/
final RelDataTypeFactory.FieldInfoBuilder fieldBuilder = rexBuilder.getTypeFactory().builder();
for (RelNode input : inputs) {
fieldBuilder.addAll(input.getRowType().getFieldList());
}
final RelDataType inputRowType = fieldBuilder.build();
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 sqlContext = Expressions.parameter(SamzaSqlExecutionContext.class, "sqlContext");
final ParameterExpression context = Expressions.parameter(Context.class, "context");
final ParameterExpression root = DataContext.ROOT;
final ParameterExpression inputValues = Expressions.parameter(Object[].class, "inputValues");
final ParameterExpression outputValues = Expressions.parameter(Object[].class, "outputValues");
final JavaTypeFactoryImpl javaTypeFactory = new SamzaSqlJavaTypeFactoryImpl(rexBuilder.getTypeFactory().getTypeSystem());
// public void execute(Object[] inputValues, Object[] outputValues)
final RexToLixTranslator.InputGetter inputGetter = new RexToLixTranslator.InputGetterImpl(ImmutableList.of(Pair.of(Expressions.variable(Object[].class, "inputValues"), PhysTypeImpl.of(javaTypeFactory, inputRowType, JavaRowFormat.ARRAY, false))));
final List<org.apache.calcite.linq4j.tree.Expression> list = RexToLixTranslator.translateProjects(program, javaTypeFactory, SqlConformanceEnum.DEFAULT, builder, null, DataContext.ROOT, inputGetter, null);
for (int i = 0; i < list.size(); i++) {
builder.add(Expressions.statement(Expressions.assign(Expressions.arrayIndex(outputValues, Expressions.constant(i)), list.get(i))));
}
return createSamzaExpressionFromCalcite(sqlContext, context, root, inputValues, outputValues, builder.toBlock());
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.BlockBuilder in project storm by apache.
the class RexNodeToJavaCodeCompiler method compile.
public String compile(final RexProgram program, String className) {
final ParameterExpression context_ = Expressions.parameter(Context.class, "context");
final ParameterExpression outputValues_ = Expressions.parameter(Object[].class, "outputValues");
BlockBuilder builder = compileToBlock(program, context_, outputValues_);
return baz(context_, outputValues_, builder.toBlock(), className);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.BlockBuilder in project storm by apache.
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>() {
@Override
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 storm by apache.
the class RexNodeToJavaCodeCompiler method baz.
/**
* Given a method that implements {@link ExecutableExpression#execute(Context, Object[])}, adds a bridge method that
* implements {@link ExecutableExpression#execute(Context)}, and compiles.
*/
static String baz(ParameterExpression context, ParameterExpression outputValues, BlockStatement block, String className) {
final List<MemberDeclaration> declarations = Lists.newArrayList();
// public void execute(Context, Object[] outputValues)
declarations.add(Expressions.methodDecl(Modifier.PUBLIC, void.class, StormBuiltInMethod.EXPR_EXECUTE2.method.getName(), ImmutableList.of(context, outputValues), block));
// public Object execute(Context)
final BlockBuilder builder = new BlockBuilder();
final Expression values_ = builder.append("values", Expressions.newArrayBounds(Object.class, 1, Expressions.constant(1)));
builder.add(Expressions.statement(Expressions.call(Expressions.parameter(ExecutableExpression.class, "this"), StormBuiltInMethod.EXPR_EXECUTE2.method, context, values_)));
builder.add(Expressions.return_(null, Expressions.arrayIndex(values_, Expressions.constant(0))));
declarations.add(Expressions.methodDecl(Modifier.PUBLIC, Object.class, StormBuiltInMethod.EXPR_EXECUTE1.method.getName(), ImmutableList.of(context), builder.toBlock()));
final ClassDeclaration classDeclaration = Expressions.classDecl(Modifier.PUBLIC, className, null, ImmutableList.<Type>of(ExecutableExpression.class), declarations);
return Expressions.toString(Lists.newArrayList(classDeclaration), "\n", false);
}
Aggregations