Search in sources :

Example 21 with BlockBuilder

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.BlockBuilder in project calcite by apache.

the class EnumerableLimit method implement.

public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
    final BlockBuilder builder = new BlockBuilder();
    final EnumerableRel child = (EnumerableRel) getInput();
    final Result result = implementor.visitChild(this, 0, child, pref);
    final PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), getRowType(), result.format);
    Expression v = builder.append("child", result.block);
    if (offset != null) {
        v = builder.append("offset", Expressions.call(v, BuiltInMethod.SKIP.method, getExpression(offset)));
    }
    if (fetch != null) {
        v = builder.append("fetch", Expressions.call(v, BuiltInMethod.TAKE.method, getExpression(fetch)));
    }
    builder.add(Expressions.return_(null, v));
    return implementor.result(physType, builder.toBlock());
}
Also used : Expression(org.apache.calcite.linq4j.tree.Expression) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Example 22 with BlockBuilder

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.BlockBuilder in project calcite by apache.

the class EnumerableMergeJoin method implement.

public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
    BlockBuilder builder = new BlockBuilder();
    final Result leftResult = implementor.visitChild(this, 0, (EnumerableRel) left, pref);
    final Expression leftExpression = builder.append("left", leftResult.block);
    final ParameterExpression left_ = Expressions.parameter(leftResult.physType.getJavaRowType(), "left");
    final Result rightResult = implementor.visitChild(this, 1, (EnumerableRel) right, pref);
    final Expression rightExpression = builder.append("right", rightResult.block);
    final ParameterExpression right_ = Expressions.parameter(rightResult.physType.getJavaRowType(), "right");
    final JavaTypeFactory typeFactory = implementor.getTypeFactory();
    final PhysType physType = PhysTypeImpl.of(typeFactory, getRowType(), pref.preferArray());
    final List<Expression> leftExpressions = new ArrayList<>();
    final List<Expression> rightExpressions = new ArrayList<>();
    for (Pair<Integer, Integer> pair : Pair.zip(leftKeys, rightKeys)) {
        final RelDataType keyType = typeFactory.leastRestrictive(ImmutableList.of(left.getRowType().getFieldList().get(pair.left).getType(), right.getRowType().getFieldList().get(pair.right).getType()));
        final Type keyClass = typeFactory.getJavaClass(keyType);
        leftExpressions.add(Types.castIfNecessary(keyClass, leftResult.physType.fieldReference(left_, pair.left)));
        rightExpressions.add(Types.castIfNecessary(keyClass, rightResult.physType.fieldReference(right_, pair.right)));
    }
    final PhysType leftKeyPhysType = leftResult.physType.project(leftKeys, JavaRowFormat.LIST);
    final PhysType rightKeyPhysType = rightResult.physType.project(rightKeys, JavaRowFormat.LIST);
    return implementor.result(physType, builder.append(Expressions.call(BuiltInMethod.MERGE_JOIN.method, Expressions.list(leftExpression, rightExpression, Expressions.lambda(leftKeyPhysType.record(leftExpressions), left_), Expressions.lambda(rightKeyPhysType.record(rightExpressions), right_), EnumUtils.joinSelector(joinType, physType, ImmutableList.of(leftResult.physType, rightResult.physType)), Expressions.constant(joinType.generatesNullsOnLeft()), Expressions.constant(joinType.generatesNullsOnRight())))).toBlock());
}
Also used : RelDataType(org.apache.calcite.rel.type.RelDataType) Type(java.lang.reflect.Type) JoinRelType(org.apache.calcite.rel.core.JoinRelType) Expression(org.apache.calcite.linq4j.tree.Expression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Example 23 with BlockBuilder

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.BlockBuilder 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)

Example 24 with BlockBuilder

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.BlockBuilder in project calcite by apache.

the class NestedBlockBuilderImpl method nestBlock.

/**
 * Starts nested code block. The resulting block can optimize expressions
 * and reuse already calculated values from the parent blocks.
 * @return new code block that can optimize expressions and reuse already
 * calculated values from the parent blocks.
 */
public final BlockBuilder nestBlock() {
    BlockBuilder block = new BlockBuilder(true, currentBlock());
    nestBlock(block, Collections.<RexNode, Boolean>emptyMap());
    return block;
}
Also used : BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Example 25 with BlockBuilder

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.BlockBuilder in project calcite by apache.

the class EnumerableCorrelate method implement.

public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
    final BlockBuilder builder = new BlockBuilder();
    final Result leftResult = implementor.visitChild(this, 0, (EnumerableRel) left, pref);
    Expression leftExpression = builder.append("left", leftResult.block);
    final BlockBuilder corrBlock = new BlockBuilder();
    Type corrVarType = leftResult.physType.getJavaRowType();
    // correlate to be used in inner loop
    ParameterExpression corrRef;
    // argument to correlate lambda (must be boxed)
    ParameterExpression corrArg;
    if (!Primitive.is(corrVarType)) {
        corrArg = Expressions.parameter(Modifier.FINAL, corrVarType, getCorrelVariable());
        corrRef = corrArg;
    } else {
        corrArg = Expressions.parameter(Modifier.FINAL, Primitive.box(corrVarType), "$box" + getCorrelVariable());
        corrRef = (ParameterExpression) corrBlock.append(getCorrelVariable(), Expressions.unbox(corrArg));
    }
    implementor.registerCorrelVariable(getCorrelVariable(), corrRef, corrBlock, leftResult.physType);
    final Result rightResult = implementor.visitChild(this, 1, (EnumerableRel) right, pref);
    implementor.clearCorrelVariable(getCorrelVariable());
    corrBlock.add(rightResult.block);
    final PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), getRowType(), pref.prefer(JavaRowFormat.CUSTOM));
    Expression selector = EnumUtils.joinSelector(joinType.returnsJustFirstInput() ? joinType.toJoinType() : JoinRelType.INNER, physType, ImmutableList.of(leftResult.physType, rightResult.physType));
    builder.append(Expressions.call(leftExpression, BuiltInMethod.CORRELATE_JOIN.method, Expressions.constant(joinType.toLinq4j()), Expressions.lambda(corrBlock.toBlock(), corrArg), selector));
    return implementor.result(physType, builder.toBlock());
}
Also used : SemiJoinType(org.apache.calcite.sql.SemiJoinType) Type(java.lang.reflect.Type) JoinRelType(org.apache.calcite.rel.core.JoinRelType) Expression(org.apache.calcite.linq4j.tree.Expression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Aggregations

BlockBuilder (org.apache.calcite.linq4j.tree.BlockBuilder)67 Expression (org.apache.calcite.linq4j.tree.Expression)56 ParameterExpression (org.apache.calcite.linq4j.tree.ParameterExpression)40 RelDataType (org.apache.calcite.rel.type.RelDataType)15 PhysType (org.apache.calcite.adapter.enumerable.PhysType)14 ArrayList (java.util.ArrayList)12 MethodCallExpression (org.apache.calcite.linq4j.tree.MethodCallExpression)12 Test (org.junit.Test)10 BlockStatement (org.apache.calcite.linq4j.tree.BlockStatement)9 Type (java.lang.reflect.Type)8 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)8 MemberDeclaration (org.apache.calcite.linq4j.tree.MemberDeclaration)7 RexToLixTranslator (org.apache.calcite.adapter.enumerable.RexToLixTranslator)6 NewExpression (org.apache.calcite.linq4j.tree.NewExpression)6 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)5 BinaryExpression (org.apache.calcite.linq4j.tree.BinaryExpression)5 FunctionExpression (org.apache.calcite.linq4j.tree.FunctionExpression)5 RexNode (org.apache.calcite.rex.RexNode)5 EnumerableRelImplementor (org.apache.calcite.adapter.enumerable.EnumerableRelImplementor)4 Function1 (org.apache.calcite.linq4j.function.Function1)4