Search in sources :

Example 1 with BlockStatement

use of org.apache.calcite.linq4j.tree.BlockStatement 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());
}
Also used : Expression(org.apache.calcite.linq4j.tree.Expression) NewExpression(org.apache.calcite.linq4j.tree.NewExpression) FunctionExpression(org.apache.calcite.linq4j.tree.FunctionExpression) MethodCallExpression(org.apache.calcite.linq4j.tree.MethodCallExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) TreeSet(java.util.TreeSet) BlockStatement(org.apache.calcite.linq4j.tree.BlockStatement) Shuttle(org.apache.calcite.linq4j.tree.Shuttle) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder) Comparator(java.util.Comparator) Test(org.junit.Test)

Example 2 with BlockStatement

use of org.apache.calcite.linq4j.tree.BlockStatement in project calcite by apache.

the class ExpressionTest method testWriteAnonymousClass.

@Test
public void testWriteAnonymousClass() {
    // final List<String> baz = Arrays.asList("foo", "bar");
    // new AbstractList<String>() {
    // public int size() {
    // return baz.size();
    // }
    // public String get(int index) {
    // return ((String) baz.get(index)).toUpperCase();
    // }
    // }
    final ParameterExpression bazParameter = Expressions.parameter(Types.of(List.class, String.class), "baz");
    final ParameterExpression indexParameter = Expressions.parameter(Integer.TYPE, "index");
    BlockStatement e = Expressions.block(Expressions.declare(Modifier.FINAL, bazParameter, Expressions.call(Arrays.class, "asList", Arrays.<Expression>asList(Expressions.constant("foo"), Expressions.constant("bar")))), Expressions.statement(Expressions.new_(Types.of(AbstractList.class, String.class), Collections.<Expression>emptyList(), Arrays.<MemberDeclaration>asList(Expressions.fieldDecl(Modifier.PUBLIC | Modifier.FINAL, Expressions.parameter(String.class, "qux"), Expressions.constant("xyzzy")), Expressions.methodDecl(Modifier.PUBLIC, Integer.TYPE, "size", Collections.<ParameterExpression>emptyList(), Blocks.toFunctionBlock(Expressions.call(bazParameter, "size", Collections.<Expression>emptyList()))), Expressions.methodDecl(Modifier.PUBLIC, String.class, "get", Arrays.asList(indexParameter), Blocks.toFunctionBlock(Expressions.call(Expressions.convert_(Expressions.call(bazParameter, "get", Arrays.<Expression>asList(indexParameter)), String.class), "toUpperCase", Collections.<Expression>emptyList())))))));
    assertEquals("{\n" + "  final java.util.List<String> baz = java.util.Arrays.asList(\"foo\", \"bar\");\n" + "  new java.util.AbstractList<String>(){\n" + "    public final String qux = \"xyzzy\";\n" + "    public int size() {\n" + "      return baz.size();\n" + "    }\n" + "\n" + "    public String get(int index) {\n" + "      return ((String) baz.get(index)).toUpperCase();\n" + "    }\n" + "\n" + "  };\n" + "}\n", Expressions.toString(e));
}
Also used : AbstractList(java.util.AbstractList) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) AbstractList(java.util.AbstractList) List(java.util.List) BlockStatement(org.apache.calcite.linq4j.tree.BlockStatement) Test(org.junit.Test)

Example 3 with BlockStatement

use of org.apache.calcite.linq4j.tree.BlockStatement in project calcite by apache.

the class BlockBuilderBase method optimizeStatement.

public static BlockStatement optimizeStatement(Statement statement) {
    BlockBuilder b = new BlockBuilder(true);
    if (!(statement instanceof BlockStatement)) {
        b.add(statement);
    } else {
        BlockStatement bs = (BlockStatement) statement;
        for (Statement stmt : bs.statements) {
            b.add(stmt);
        }
    }
    BlockStatement bs = b.toBlock();
    return bs;
}
Also used : BlockStatement(org.apache.calcite.linq4j.tree.BlockStatement) Statement(org.apache.calcite.linq4j.tree.Statement) BlockStatement(org.apache.calcite.linq4j.tree.BlockStatement) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Example 4 with BlockStatement

use of org.apache.calcite.linq4j.tree.BlockStatement in project calcite by apache.

the class StrictAggImplementor method implementResult.

public final Expression implementResult(AggContext info, final AggResultContext result) {
    if (!needTrackEmptySet) {
        return RexToLixTranslator.convert(implementNotNullResult(info, result), info.returnType());
    }
    String tmpName = result.accumulator().isEmpty() ? "ar" : (result.accumulator().get(0) + "$Res");
    ParameterExpression res = Expressions.parameter(0, info.returnType(), result.currentBlock().newName(tmpName));
    List<Expression> acc = result.accumulator();
    final BlockBuilder thenBlock = result.nestBlock();
    Expression nonNull = RexToLixTranslator.convert(implementNotNullResult(info, result), info.returnType());
    result.exitBlock();
    thenBlock.add(Expressions.statement(Expressions.assign(res, nonNull)));
    BlockStatement thenBranch = thenBlock.toBlock();
    Expression seenNotNullRows = trackNullsPerRow ? acc.get(acc.size() - 1) : ((WinAggResultContext) result).hasRows();
    if (thenBranch.statements.size() == 1) {
        return Expressions.condition(seenNotNullRows, nonNull, RexImpTable.getDefaultValue(res.getType()));
    }
    result.currentBlock().add(Expressions.declare(0, res, null));
    result.currentBlock().add(Expressions.ifThenElse(seenNotNullRows, thenBranch, Expressions.statement(Expressions.assign(res, RexImpTable.getDefaultValue(res.getType())))));
    return res;
}
Also used : Expression(org.apache.calcite.linq4j.tree.Expression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) BlockStatement(org.apache.calcite.linq4j.tree.BlockStatement) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Example 5 with BlockStatement

use of org.apache.calcite.linq4j.tree.BlockStatement in project calcite by apache.

the class EnumerableCalc method implement.

public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
    final JavaTypeFactory typeFactory = implementor.getTypeFactory();
    final BlockBuilder builder = new BlockBuilder();
    final EnumerableRel child = (EnumerableRel) getInput();
    final Result result = implementor.visitChild(this, 0, child, pref);
    final PhysType physType = PhysTypeImpl.of(typeFactory, getRowType(), pref.prefer(result.format));
    // final Enumerable<Employee> inputEnumerable = <<child adapter>>;
    // return new Enumerable<IntString>() {
    // Enumerator<IntString> enumerator() {
    // return new Enumerator<IntString>() {
    // public void reset() {
    // ...
    Type outputJavaType = physType.getJavaRowType();
    final Type enumeratorType = Types.of(Enumerator.class, outputJavaType);
    Type inputJavaType = result.physType.getJavaRowType();
    ParameterExpression inputEnumerator = Expressions.parameter(Types.of(Enumerator.class, inputJavaType), "inputEnumerator");
    Expression input = RexToLixTranslator.convert(Expressions.call(inputEnumerator, BuiltInMethod.ENUMERATOR_CURRENT.method), inputJavaType);
    final RexBuilder rexBuilder = getCluster().getRexBuilder();
    final RelMetadataQuery mq = RelMetadataQuery.instance();
    final RelOptPredicateList predicates = mq.getPulledUpPredicates(child);
    final RexSimplify simplify = new RexSimplify(rexBuilder, predicates, false, RexUtil.EXECUTOR);
    final RexProgram program = this.program.normalize(rexBuilder, simplify);
    BlockStatement moveNextBody;
    if (program.getCondition() == null) {
        moveNextBody = Blocks.toFunctionBlock(Expressions.call(inputEnumerator, BuiltInMethod.ENUMERATOR_MOVE_NEXT.method));
    } else {
        final BlockBuilder builder2 = new BlockBuilder();
        Expression condition = RexToLixTranslator.translateCondition(program, typeFactory, builder2, new RexToLixTranslator.InputGetterImpl(Collections.singletonList(Pair.of(input, result.physType))), implementor.allCorrelateVariables);
        builder2.add(Expressions.ifThen(condition, Expressions.return_(null, Expressions.constant(true))));
        moveNextBody = Expressions.block(Expressions.while_(Expressions.call(inputEnumerator, BuiltInMethod.ENUMERATOR_MOVE_NEXT.method), builder2.toBlock()), Expressions.return_(null, Expressions.constant(false)));
    }
    final BlockBuilder builder3 = new BlockBuilder();
    List<Expression> expressions = RexToLixTranslator.translateProjects(program, typeFactory, builder3, physType, DataContext.ROOT, new RexToLixTranslator.InputGetterImpl(Collections.singletonList(Pair.of(input, result.physType))), implementor.allCorrelateVariables);
    builder3.add(Expressions.return_(null, physType.record(expressions)));
    BlockStatement currentBody = builder3.toBlock();
    final Expression inputEnumerable = builder.append("inputEnumerable", result.block, false);
    final Expression body = Expressions.new_(enumeratorType, NO_EXPRS, Expressions.list(Expressions.fieldDecl(Modifier.PUBLIC | Modifier.FINAL, inputEnumerator, Expressions.call(inputEnumerable, BuiltInMethod.ENUMERABLE_ENUMERATOR.method)), EnumUtils.overridingMethodDecl(BuiltInMethod.ENUMERATOR_RESET.method, NO_PARAMS, Blocks.toFunctionBlock(Expressions.call(inputEnumerator, BuiltInMethod.ENUMERATOR_RESET.method))), EnumUtils.overridingMethodDecl(BuiltInMethod.ENUMERATOR_MOVE_NEXT.method, NO_PARAMS, moveNextBody), EnumUtils.overridingMethodDecl(BuiltInMethod.ENUMERATOR_CLOSE.method, NO_PARAMS, Blocks.toFunctionBlock(Expressions.call(inputEnumerator, BuiltInMethod.ENUMERATOR_CLOSE.method))), Expressions.methodDecl(Modifier.PUBLIC, BRIDGE_METHODS ? Object.class : outputJavaType, "current", NO_PARAMS, currentBody)));
    builder.add(Expressions.return_(null, Expressions.new_(BuiltInMethod.ABSTRACT_ENUMERABLE_CTOR.constructor, // Collections.singletonList(inputRowType),
    NO_EXPRS, ImmutableList.<MemberDeclaration>of(Expressions.methodDecl(Modifier.PUBLIC, enumeratorType, BuiltInMethod.ENUMERABLE_ENUMERATOR.method.getName(), NO_PARAMS, Blocks.toFunctionBlock(body))))));
    return implementor.result(physType, builder.toBlock());
}
Also used : RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) RexProgram(org.apache.calcite.rex.RexProgram) BlockStatement(org.apache.calcite.linq4j.tree.BlockStatement) Type(java.lang.reflect.Type) Enumerator(org.apache.calcite.linq4j.Enumerator) Expression(org.apache.calcite.linq4j.tree.Expression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) RexSimplify(org.apache.calcite.rex.RexSimplify) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) RelOptPredicateList(org.apache.calcite.plan.RelOptPredicateList) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) RexBuilder(org.apache.calcite.rex.RexBuilder) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Aggregations

BlockStatement (org.apache.calcite.linq4j.tree.BlockStatement)9 BlockBuilder (org.apache.calcite.linq4j.tree.BlockBuilder)8 ParameterExpression (org.apache.calcite.linq4j.tree.ParameterExpression)8 Expression (org.apache.calcite.linq4j.tree.Expression)7 MethodCallExpression (org.apache.calcite.linq4j.tree.MethodCallExpression)4 NewExpression (org.apache.calcite.linq4j.tree.NewExpression)4 FunctionExpression (org.apache.calcite.linq4j.tree.FunctionExpression)3 Shuttle (org.apache.calcite.linq4j.tree.Shuttle)3 Statement (org.apache.calcite.linq4j.tree.Statement)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)2 Function (com.google.common.base.Function)1 ImmutableList (com.google.common.collect.ImmutableList)1 Type (java.lang.reflect.Type)1 AbstractList (java.util.AbstractList)1 Comparator (java.util.Comparator)1 TreeSet (java.util.TreeSet)1 WinAggResetContextImpl (org.apache.calcite.adapter.enumerable.impl.WinAggResetContextImpl)1