use of org.apache.calcite.linq4j.tree.GotoStatement in project calcite by apache.
the class EnumerableRelImplementor method implementRoot.
public ClassDeclaration implementRoot(EnumerableRel rootRel, EnumerableRel.Prefer prefer) {
EnumerableRel.Result result = rootRel.implement(this, prefer);
switch(prefer) {
case ARRAY:
if (result.physType.getFormat() == JavaRowFormat.ARRAY && rootRel.getRowType().getFieldCount() == 1) {
BlockBuilder bb = new BlockBuilder();
Expression e = null;
for (Statement statement : result.block.statements) {
if (statement instanceof GotoStatement) {
e = bb.append("v", ((GotoStatement) statement).expression);
} else {
bb.add(statement);
}
}
if (e != null) {
bb.add(Expressions.return_(null, Expressions.call(null, BuiltInMethod.SLICE0.method, e)));
}
result = new EnumerableRel.Result(bb.toBlock(), result.physType, JavaRowFormat.SCALAR);
}
}
final List<MemberDeclaration> memberDeclarations = new ArrayList<>();
new TypeRegistrar(memberDeclarations).go(result);
// The following is a workaround to
// http://jira.codehaus.org/browse/JANINO-169. Otherwise we'd remove the
// member variable, rename the "root0" parameter as "root", and reference it
// directly from inner classes.
final ParameterExpression root0_ = Expressions.parameter(Modifier.FINAL, DataContext.class, "root0");
// This creates the following code
// final Integer v1stashed = (Integer) root.get("v1stashed")
// It is convenient for passing non-literal "compile-time" constants
final Collection<Statement> stashed = Collections2.transform(stashedParameters.values(), new Function<ParameterExpression, Statement>() {
public Statement apply(ParameterExpression input) {
return Expressions.declare(Modifier.FINAL, input, Expressions.convert_(Expressions.call(DataContext.ROOT, BuiltInMethod.DATA_CONTEXT_GET.method, Expressions.constant(input.name)), input.type));
}
});
final BlockStatement block = Expressions.block(Iterables.concat(ImmutableList.of(Expressions.statement(Expressions.assign(DataContext.ROOT, root0_))), stashed, result.block.statements));
memberDeclarations.add(Expressions.fieldDecl(0, DataContext.ROOT, null));
memberDeclarations.add(Expressions.methodDecl(Modifier.PUBLIC, Enumerable.class, BuiltInMethod.BINDABLE_BIND.method.getName(), Expressions.list(root0_), block));
memberDeclarations.add(Expressions.methodDecl(Modifier.PUBLIC, Class.class, BuiltInMethod.TYPED_GET_ELEMENT_TYPE.method.getName(), Collections.<ParameterExpression>emptyList(), Blocks.toFunctionBlock(Expressions.return_(null, Expressions.constant(result.physType.getJavaRowType())))));
return Expressions.classDecl(Modifier.PUBLIC, "Baz", null, Collections.<Type>singletonList(Bindable.class), memberDeclarations);
}
Aggregations