Search in sources :

Example 6 with Order

use of com.actiontech.dble.plan.Order in project dble by actiontech.

the class DistinctHandler method fieldEofResponse.

/**
 * treat all the data from parent as Field Type
 */
public void fieldEofResponse(byte[] headerNull, List<byte[]> fieldsNull, final List<FieldPacket> fieldPackets, byte[] eofNull, boolean isLeft, BackendConnection conn) {
    if (terminate.get())
        return;
    if (this.pool == null)
        this.pool = DbleServer.getInstance().getBufferPool();
    this.fieldPackets = fieldPackets;
    List<Field> sourceFields = HandlerTool.createFields(this.fieldPackets);
    if (this.distinctCols == null) {
        // eg:show tables
        this.distinctCols = new ArrayList<>();
        for (FieldPacket fp : this.fieldPackets) {
            Item sel = HandlerTool.createItemField(fp);
            this.distinctCols.add(sel);
        }
    }
    List<Order> orders = this.fixedOrders;
    if (orders == null)
        orders = HandlerTool.makeOrder(this.distinctCols);
    RowDataComparator comparator = new RowDataComparator(this.fieldPackets, orders, this.isAllPushDown(), type());
    localResult = new DistinctLocalResult(pool, sourceFields.size(), comparator, CharsetUtil.getJavaCharset(conn.getCharset().getResults())).setMemSizeController(session.getOtherBufferMC());
    nextHandler.fieldEofResponse(null, null, this.fieldPackets, null, this.isLeft, conn);
}
Also used : Order(com.actiontech.dble.plan.Order) Field(com.actiontech.dble.plan.common.field.Field) Item(com.actiontech.dble.plan.common.item.Item) DistinctLocalResult(com.actiontech.dble.backend.mysql.store.DistinctLocalResult) FieldPacket(com.actiontech.dble.net.mysql.FieldPacket) RowDataComparator(com.actiontech.dble.backend.mysql.nio.handler.util.RowDataComparator)

Example 7 with Order

use of com.actiontech.dble.plan.Order in project dble by actiontech.

the class HandlerTool method makeOrder.

/**
 * make order by from distinct
 *
 * @param selects
 * @return
 */
public static List<Order> makeOrder(List<Item> selects) {
    List<Order> orders = new ArrayList<>();
    for (Item sel : selects) {
        Order order = new Order(sel, SQLOrderingSpecification.ASC);
        orders.add(order);
    }
    return orders;
}
Also used : Order(com.actiontech.dble.plan.Order) Item(com.actiontech.dble.plan.common.item.Item) ArrayList(java.util.ArrayList)

Example 8 with Order

use of com.actiontech.dble.plan.Order in project dble by actiontech.

the class ItemFuncGroupConcat method toExpression.

@Override
public SQLExpr toExpression() {
    SQLAggregateExpr aggregate = new SQLAggregateExpr(funcName());
    if (hasWithDistinct()) {
        aggregate.setOption(SQLAggregateOption.DISTINCT);
    }
    if (orders != null) {
        SQLOrderBy orderBy = new SQLOrderBy();
        for (Order order : orders) {
            SQLSelectOrderByItem orderItem = new SQLSelectOrderByItem(order.getItem().toExpression());
            orderItem.setType(order.getSortOrder());
            orderBy.addItem(orderItem);
        }
        aggregate.putAttribute(ItemFuncKeyWord.ORDER_BY, orderBy);
    }
    for (Item arg : args) {
        aggregate.addArgument(arg.toExpression());
    }
    if (seperator != null) {
        SQLCharExpr sep = new SQLCharExpr(seperator);
        aggregate.putAttribute(ItemFuncKeyWord.SEPARATOR, sep);
    }
    return aggregate;
}
Also used : Order(com.actiontech.dble.plan.Order) Item(com.actiontech.dble.plan.common.item.Item) SQLSelectOrderByItem(com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) SQLOrderBy(com.alibaba.druid.sql.ast.SQLOrderBy) SQLSelectOrderByItem(com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem) SQLAggregateExpr(com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)

Example 9 with Order

use of com.actiontech.dble.plan.Order in project dble by actiontech.

the class OrderByPusher method getJoinColumnOrders.

/**
 * generatejoinOnFilters,if joinOn's orderBy can change to match implicitOrders ,return true
 *
 * @param joinOnFilters  in
 * @param leftOnOrders   out
 * @param rightOnOrders  out
 * @param implicitOrders in
 * @return
 */
private static boolean getJoinColumnOrders(List<ItemFuncEqual> joinOnFilters, List<Order> leftOnOrders, List<Order> rightOnOrders, List<Order> implicitOrders) {
    List<Item> leftOnSels = new ArrayList<>();
    List<Item> rightOnSels = new ArrayList<>();
    for (ItemFuncEqual bf : joinOnFilters) {
        leftOnSels.add(bf.arguments().get(0));
        rightOnSels.add(bf.arguments().get(1));
    }
    // is on's orderBy can be changed to match implicitOrders
    boolean canMatch = false;
    if (implicitOrders.size() < leftOnSels.size())
        canMatch = false;
    else {
        Map<Integer, SQLOrderingSpecification> foundOnIndexs = new LinkedHashMap<>();
        for (Order orderby : implicitOrders) {
            Item orderSel = orderby.getItem();
            int index = -1;
            if ((index = leftOnSels.indexOf(orderSel)) >= 0) {
                foundOnIndexs.put(index, orderby.getSortOrder());
            } else if ((index = rightOnSels.indexOf(orderSel)) >= 0) {
                foundOnIndexs.put(index, orderby.getSortOrder());
            } else {
                // neither belong to leftOn nor belong to rightOn
                break;
            }
        }
        if (foundOnIndexs.size() == leftOnSels.size()) {
            canMatch = true;
            for (Map.Entry<Integer, SQLOrderingSpecification> entry : foundOnIndexs.entrySet()) {
                int foundOnIndex = entry.getKey();
                SQLOrderingSpecification sortOrder = entry.getValue();
                Item leftOn = leftOnSels.get(foundOnIndex);
                Item rightOn = rightOnSels.get(foundOnIndex);
                // add lefton order
                Order leftOnOrder = new Order(leftOn, sortOrder);
                leftOnOrders.add(leftOnOrder);
                // add righton order
                Order rightOnOrder = new Order(rightOn, sortOrder);
                rightOnOrders.add(rightOnOrder);
            }
            return canMatch;
        }
    }
    // can not match
    for (int index = 0; index < leftOnSels.size(); index++) {
        SQLOrderingSpecification sortOrder = SQLOrderingSpecification.ASC;
        Item leftOn = leftOnSels.get(index);
        Item rightOn = rightOnSels.get(index);
        // add lefton order
        Order leftOnOrder = new Order(leftOn, sortOrder);
        leftOnOrders.add(leftOnOrder);
        // add righton order
        Order rightOnOrder = new Order(rightOn, sortOrder);
        rightOnOrders.add(rightOnOrder);
    }
    return canMatch;
}
Also used : Order(com.actiontech.dble.plan.Order) ItemFuncEqual(com.actiontech.dble.plan.common.item.function.operator.cmpfunc.ItemFuncEqual) ArrayList(java.util.ArrayList) SQLOrderingSpecification(com.alibaba.druid.sql.ast.SQLOrderingSpecification) LinkedHashMap(java.util.LinkedHashMap) Item(com.actiontech.dble.plan.common.item.Item) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 10 with Order

use of com.actiontech.dble.plan.Order in project dble by actiontech.

the class OrderByPusher method tryPushOrderToChild.

/**
 * tryPushOrderToChild
 *
 * @param pOrders
 * @param child
 * @return false, if child's orders not match pOrders
 */
private static boolean tryPushOrderToChild(PlanNode parent, List<Order> pOrders, PlanNode child) {
    for (Order pOrder : pOrders) {
        if (!PlanUtil.canPush(pOrder.getItem(), child, parent))
            return false;
    }
    List<Order> pushedOrders = PlanUtil.getPushDownOrders(parent, pOrders);
    List<Order> childImplicitOrders = child.getOrderBys();
    boolean childOrderContains = PlanUtil.orderContains(childImplicitOrders, pushedOrders);
    if (child.getLimitTo() == -1) {
        // if child's orders more than parent's orders,keep child's ,or use parent's
        if (!childOrderContains)
            child.setOrderBys(pushedOrders);
        return true;
    } else {
        // has limit,order by can not be push down
        return childOrderContains;
    }
}
Also used : Order(com.actiontech.dble.plan.Order)

Aggregations

Order (com.actiontech.dble.plan.Order)27 Item (com.actiontech.dble.plan.common.item.Item)16 RowDataComparator (com.actiontech.dble.backend.mysql.nio.handler.util.RowDataComparator)4 MySQLOutPutException (com.actiontech.dble.plan.common.exception.MySQLOutPutException)4 ArrayList (java.util.ArrayList)4 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)3 DistinctLocalResult (com.actiontech.dble.backend.mysql.store.DistinctLocalResult)2 FieldPacket (com.actiontech.dble.net.mysql.FieldPacket)2 ItemField (com.actiontech.dble.plan.common.item.ItemField)2 ItemFuncEqual (com.actiontech.dble.plan.common.item.function.operator.cmpfunc.ItemFuncEqual)2 ItemSum (com.actiontech.dble.plan.common.item.function.sumfunc.ItemSum)2 PlanNode (com.actiontech.dble.plan.node.PlanNode)2 SQLSelectOrderByItem (com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem)2 DirectGroupByHandler (com.actiontech.dble.backend.mysql.nio.handler.query.impl.groupby.DirectGroupByHandler)1 OrderedGroupByHandler (com.actiontech.dble.backend.mysql.nio.handler.query.impl.groupby.OrderedGroupByHandler)1 ResultStore (com.actiontech.dble.plan.common.external.ResultStore)1 Field (com.actiontech.dble.plan.common.field.Field)1 JoinNode (com.actiontech.dble.plan.node.JoinNode)1 QueryNode (com.actiontech.dble.plan.node.QueryNode)1 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)1