Search in sources :

Example 1 with DeclarationStatement

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

the class ExpressionTest method testWriteWhile.

@Test
public void testWriteWhile() {
    DeclarationStatement xDecl;
    DeclarationStatement yDecl;
    Node node = Expressions.block(xDecl = Expressions.declare(0, "x", Expressions.constant(10)), yDecl = Expressions.declare(0, "y", Expressions.constant(0)), Expressions.while_(Expressions.lessThan(xDecl.parameter, Expressions.constant(5)), Expressions.statement(Expressions.preIncrementAssign(yDecl.parameter))));
    assertEquals("{\n" + "  int x = 10;\n" + "  int y = 0;\n" + "  while (x < 5) {\n" + "    ++y;\n" + "  }\n" + "}\n", Expressions.toString(node));
}
Also used : Node(org.apache.calcite.linq4j.tree.Node) DeclarationStatement(org.apache.calcite.linq4j.tree.DeclarationStatement) Test(org.junit.Test)

Example 2 with DeclarationStatement

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

the class InlinerTest method testInlineConstant.

@Test
public void testInlineConstant() {
    DeclarationStatement decl = Expressions.declare(16, "x", ONE);
    b.add(decl);
    b.add(Expressions.return_(null, Expressions.add(decl.parameter, decl.parameter)));
    assertEquals("{\n  return 1 + 1;\n}\n", b.toBlock().toString());
}
Also used : DeclarationStatement(org.apache.calcite.linq4j.tree.DeclarationStatement) Test(org.junit.Test)

Example 3 with DeclarationStatement

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

the class InlinerTest method testInlineSingleUsage.

@Test
public void testInlineSingleUsage() {
    DeclarationStatement decl = Expressions.declare(16, "x", Expressions.add(ONE, TWO));
    b.add(decl);
    b.add(Expressions.return_(null, decl.parameter));
    assertEquals("{\n  return 1 + 2;\n}\n", b.toBlock().toString());
}
Also used : DeclarationStatement(org.apache.calcite.linq4j.tree.DeclarationStatement) Test(org.junit.Test)

Example 4 with DeclarationStatement

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

the class InlinerTest method testInlineParameter.

@Test
public void testInlineParameter() {
    ParameterExpression pe = Expressions.parameter(int.class, "p");
    DeclarationStatement decl = Expressions.declare(16, "x", pe);
    b.add(decl);
    b.add(Expressions.return_(null, Expressions.add(decl.parameter, decl.parameter)));
    assertEquals("{\n  return p + p;\n}\n", b.toBlock().toString());
}
Also used : ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) DeclarationStatement(org.apache.calcite.linq4j.tree.DeclarationStatement) Test(org.junit.Test)

Example 5 with DeclarationStatement

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

the class EnumerableWindow method getPartitionIterator.

private Pair<Expression, Expression> getPartitionIterator(BlockBuilder builder, Expression source_, PhysType inputPhysType, Group group, Expression comparator_) {
    // 
    if (group.keys.isEmpty()) {
        // If partition key is empty, no need to partition.
        // 
        // final List<Employee> tempList =
        // source.into(new ArrayList<Employee>());
        // Iterator<Employee[]> iterator =
        // SortedMultiMap.singletonArrayIterator(comparator, tempList);
        // final List<Xxx> list = new ArrayList<Xxx>(tempList.size());
        final Expression tempList_ = builder.append("tempList", Expressions.convert_(Expressions.call(source_, BuiltInMethod.INTO.method, Expressions.new_(ArrayList.class)), List.class));
        return Pair.of(tempList_, builder.append("iterator", Expressions.call(null, BuiltInMethod.SORTED_MULTI_MAP_SINGLETON.method, comparator_, tempList_)));
    }
    Expression multiMap_ = builder.append("multiMap", Expressions.new_(SortedMultiMap.class));
    final BlockBuilder builder2 = new BlockBuilder();
    final ParameterExpression v_ = Expressions.parameter(inputPhysType.getJavaRowType(), builder2.newName("v"));
    Pair<Type, List<Expression>> selector = inputPhysType.selector(v_, group.keys.asList(), JavaRowFormat.CUSTOM);
    final ParameterExpression key_;
    if (selector.left instanceof Types.RecordType) {
        Types.RecordType keyJavaType = (Types.RecordType) selector.left;
        List<Expression> initExpressions = selector.right;
        key_ = Expressions.parameter(keyJavaType, "key");
        builder2.add(Expressions.declare(0, key_, null));
        builder2.add(Expressions.statement(Expressions.assign(key_, Expressions.new_(keyJavaType))));
        List<Types.RecordField> fieldList = keyJavaType.getRecordFields();
        for (int i = 0; i < initExpressions.size(); i++) {
            Expression right = initExpressions.get(i);
            builder2.add(Expressions.statement(Expressions.assign(Expressions.field(key_, fieldList.get(i)), right)));
        }
    } else {
        DeclarationStatement declare = Expressions.declare(0, "key", selector.right.get(0));
        builder2.add(declare);
        key_ = declare.parameter;
    }
    builder2.add(Expressions.statement(Expressions.call(multiMap_, BuiltInMethod.SORTED_MULTI_MAP_PUT_MULTI.method, key_, v_)));
    builder2.add(Expressions.return_(null, Expressions.constant(null)));
    builder.add(Expressions.statement(Expressions.call(source_, BuiltInMethod.ENUMERABLE_FOREACH.method, Expressions.lambda(builder2.toBlock(), v_))));
    return Pair.of(multiMap_, builder.append("iterator", Expressions.call(multiMap_, BuiltInMethod.SORTED_MULTI_MAP_ARRAYS.method, comparator_)));
}
Also used : Types(org.apache.calcite.linq4j.tree.Types) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) Type(java.lang.reflect.Type) BinaryExpression(org.apache.calcite.linq4j.tree.BinaryExpression) Expression(org.apache.calcite.linq4j.tree.Expression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) SortedMultiMap(org.apache.calcite.runtime.SortedMultiMap) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder) DeclarationStatement(org.apache.calcite.linq4j.tree.DeclarationStatement)

Aggregations

DeclarationStatement (org.apache.calcite.linq4j.tree.DeclarationStatement)7 Test (org.junit.Test)5 ParameterExpression (org.apache.calcite.linq4j.tree.ParameterExpression)4 ImmutableList (com.google.common.collect.ImmutableList)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 BinaryExpression (org.apache.calcite.linq4j.tree.BinaryExpression)2 BlockBuilder (org.apache.calcite.linq4j.tree.BlockBuilder)2 Expression (org.apache.calcite.linq4j.tree.Expression)2 RelDataType (org.apache.calcite.rel.type.RelDataType)2 Function (com.google.common.base.Function)1 Type (java.lang.reflect.Type)1 WinAggResetContextImpl (org.apache.calcite.adapter.enumerable.impl.WinAggResetContextImpl)1 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)1 BlockStatement (org.apache.calcite.linq4j.tree.BlockStatement)1 Node (org.apache.calcite.linq4j.tree.Node)1 Statement (org.apache.calcite.linq4j.tree.Statement)1 Types (org.apache.calcite.linq4j.tree.Types)1 AggregateCall (org.apache.calcite.rel.core.AggregateCall)1 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)1