Search in sources :

Example 1 with GotoStatement

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);
}
Also used : GotoStatement(org.apache.calcite.linq4j.tree.GotoStatement) Statement(org.apache.calcite.linq4j.tree.Statement) BlockStatement(org.apache.calcite.linq4j.tree.BlockStatement) ConditionalStatement(org.apache.calcite.linq4j.tree.ConditionalStatement) MemberDeclaration(org.apache.calcite.linq4j.tree.MemberDeclaration) ArrayList(java.util.ArrayList) Bindable(org.apache.calcite.runtime.Bindable) BlockStatement(org.apache.calcite.linq4j.tree.BlockStatement) GotoStatement(org.apache.calcite.linq4j.tree.GotoStatement) NewArrayExpression(org.apache.calcite.linq4j.tree.NewArrayExpression) Expression(org.apache.calcite.linq4j.tree.Expression) NewExpression(org.apache.calcite.linq4j.tree.NewExpression) MethodCallExpression(org.apache.calcite.linq4j.tree.MethodCallExpression) ConstantExpression(org.apache.calcite.linq4j.tree.ConstantExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) Enumerable(org.apache.calcite.linq4j.Enumerable) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Aggregations

ArrayList (java.util.ArrayList)1 Enumerable (org.apache.calcite.linq4j.Enumerable)1 BlockBuilder (org.apache.calcite.linq4j.tree.BlockBuilder)1 BlockStatement (org.apache.calcite.linq4j.tree.BlockStatement)1 ConditionalStatement (org.apache.calcite.linq4j.tree.ConditionalStatement)1 ConstantExpression (org.apache.calcite.linq4j.tree.ConstantExpression)1 Expression (org.apache.calcite.linq4j.tree.Expression)1 GotoStatement (org.apache.calcite.linq4j.tree.GotoStatement)1 MemberDeclaration (org.apache.calcite.linq4j.tree.MemberDeclaration)1 MethodCallExpression (org.apache.calcite.linq4j.tree.MethodCallExpression)1 NewArrayExpression (org.apache.calcite.linq4j.tree.NewArrayExpression)1 NewExpression (org.apache.calcite.linq4j.tree.NewExpression)1 ParameterExpression (org.apache.calcite.linq4j.tree.ParameterExpression)1 Statement (org.apache.calcite.linq4j.tree.Statement)1 Bindable (org.apache.calcite.runtime.Bindable)1