Search in sources :

Example 1 with MycatMergeSort

use of io.mycat.calcite.physical.MycatMergeSort in project Mycat2 by MyCATApache.

the class MycatView method implementMergeSort.

public Result implementMergeSort(MycatEnumerableRelImplementor implementor, Prefer pref, RelNode relNode) {
    MycatMergeSort mycatMergeSort = null;
    MycatView view = (MycatView) relNode;
    if (view.getDistribution().type() == Distribution.Type.SHARDING) {
        if (view.getRelNode() instanceof Sort) {
            Sort viewRelNode = (Sort) view.getRelNode();
            RexNode rexNode = (RexNode) viewRelNode.fetch;
            if (rexNode != null && rexNode.getKind() == SqlKind.PLUS) {
                RexCall plus = (RexCall) rexNode;
                mycatMergeSort = MycatMergeSort.create(viewRelNode.getTraitSet(), relNode, viewRelNode.getCollation(), plus.getOperands().get(0), plus.getOperands().get(1));
            } else {
                mycatMergeSort = MycatMergeSort.create(viewRelNode.getTraitSet(), relNode, viewRelNode.getCollation(), viewRelNode.offset, viewRelNode.fetch);
            }
        }
    } else {
        throw new IllegalArgumentException();
    }
    // MycatView view = (MycatView) relNode;
    // if (view.getDistribution().type() == Distribution.Type.Sharding) {
    // if (view.getRelNode() instanceof LogicalSort) {
    // LogicalSort viewRelNode = (LogicalSort) view.getRelNode();
    // RexNode rexNode = (RexNode) viewRelNode.fetch;
    // if (rexNode != null && rexNode.getKind() == SqlKind.PLUS) {
    // RexCall plus = (RexCall) rexNode;
    // return MycatMergeSort.create(viewRelNode.getTraitSet(), relNode, viewRelNode.getCollation(), plus.getOperands().get(0), plus.getOperands().get(1));
    // } else {
    // return MycatMergeSort.create(viewRelNode.getTraitSet(), relNode, viewRelNode.getCollation(), viewRelNode.offset, viewRelNode.fetch);
    // }
    // }
    // }
    // }
    final BlockBuilder builder = new BlockBuilder();
    final PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), getRowType(), JavaRowFormat.ARRAY);
    ParameterExpression root = implementor.getRootExpression();
    Expression mycatViewStash = Expressions.constant(relNode.getDigest());
    final PhysType inputPhysType = physType;
    final Pair<Expression, Expression> pair = inputPhysType.generateCollationKey(mycatMergeSort.collation.getFieldCollations());
    final Expression fetchVal;
    if (mycatMergeSort.fetch == null) {
        fetchVal = Expressions.constant(Integer.valueOf(Integer.MAX_VALUE));
    } else {
        fetchVal = getExpression(mycatMergeSort.fetch);
    }
    // builder.append("keySelector", pair.left))
    // .appendIfNotNull(builder.appendIfNotNull("comparator", pair.right))
    final Expression offsetVal = mycatMergeSort.offset == null ? Expressions.constant(Integer.valueOf(0)) : getExpression(mycatMergeSort.offset);
    Method getObservable = Types.lookupMethod(NewMycatDataContext.class, "getObservable", String.class, Function1.class, Comparator.class, int.class, int.class);
    builder.add(Expressions.call(root, getObservable, mycatViewStash, pair.left, pair.right, offsetVal, fetchVal));
    return implementor.result(physType, builder.toBlock());
}
Also used : PhysType(org.apache.calcite.adapter.enumerable.PhysType) MycatMergeSort(io.mycat.calcite.physical.MycatMergeSort) BuiltInMethod(org.apache.calcite.util.BuiltInMethod) Method(java.lang.reflect.Method) MycatMergeSort(io.mycat.calcite.physical.MycatMergeSort)

Aggregations

MycatMergeSort (io.mycat.calcite.physical.MycatMergeSort)1 Method (java.lang.reflect.Method)1 PhysType (org.apache.calcite.adapter.enumerable.PhysType)1 BuiltInMethod (org.apache.calcite.util.BuiltInMethod)1