Search in sources :

Example 1 with Order

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

the class BaseHandlerBuilder method buildCommon.

/**
 * build common properties,like where,groupby,having,orderby,limit, and sendMakHandler(rename)
 */
protected void buildCommon() {
    if (node.getWhereFilter() != null && needWhereHandler) {
        WhereHandler wh = new WhereHandler(getSequenceId(), session, node.getWhereFilter());
        addHandler(wh);
    }
    /* need groupby handler */
    if (nodeHasGroupBy(node)) {
        boolean needOrderBy = (node.getGroupBys().size() > 0) && isOrderNeeded(node, node.getGroupBys());
        boolean canDirectGroupBy = true;
        List<ItemSum> sumRefs = new ArrayList<>();
        for (ItemSum funRef : node.getSumFuncs()) {
            if (funRef.hasWithDistinct() || funRef.sumType().equals(ItemSum.SumFuncType.GROUP_CONCAT_FUNC))
                canDirectGroupBy = false;
            sumRefs.add(funRef);
        }
        if (needOrderBy) {
            if (canDirectGroupBy) {
                // we go direct groupby
                DirectGroupByHandler gh = new DirectGroupByHandler(getSequenceId(), session, node.getGroupBys(), sumRefs);
                addHandler(gh);
            } else {
                OrderByHandler oh = new OrderByHandler(getSequenceId(), session, node.getGroupBys());
                addHandler(oh);
                OrderedGroupByHandler gh = new OrderedGroupByHandler(getSequenceId(), session, node.getGroupBys(), sumRefs);
                addHandler(gh);
            }
        } else {
            // @bug 1052 canDirectGroupby condition we use
            // directgroupby already
            OrderedGroupByHandler gh = new OrderedGroupByHandler(getSequenceId(), session, node.getGroupBys(), sumRefs);
            addHandler(gh);
        }
    }
    // having
    if (node.getHavingFilter() != null) {
        HavingHandler hh = new HavingHandler(getSequenceId(), session, node.getHavingFilter());
        addHandler(hh);
    }
    if (node.isDistinct() && node.getOrderBys().size() > 0) {
        // distinct and order by both exists
        List<Order> mergedOrders = mergeOrderBy(node.getColumnsSelected(), node.getOrderBys());
        if (mergedOrders == null) {
            // can not merge,need distinct then order by
            DistinctHandler dh = new DistinctHandler(getSequenceId(), session, node.getColumnsSelected());
            addHandler(dh);
            OrderByHandler oh = new OrderByHandler(getSequenceId(), session, node.getOrderBys());
            addHandler(oh);
        } else {
            DistinctHandler dh = new DistinctHandler(getSequenceId(), session, node.getColumnsSelected(), mergedOrders);
            addHandler(dh);
        }
    } else {
        if (node.isDistinct()) {
            DistinctHandler dh = new DistinctHandler(getSequenceId(), session, node.getColumnsSelected());
            addHandler(dh);
        }
        // order by
        if (node.getOrderBys().size() > 0) {
            if (node.getGroupBys().size() > 0) {
                if (!PlanUtil.orderContains(node.getGroupBys(), node.getOrderBys())) {
                    OrderByHandler oh = new OrderByHandler(getSequenceId(), session, node.getOrderBys());
                    addHandler(oh);
                }
            } else if (isOrderNeeded(node, node.getOrderBys())) {
                OrderByHandler oh = new OrderByHandler(getSequenceId(), session, node.getOrderBys());
                addHandler(oh);
            }
        }
    }
    if (node.getLimitTo() > 0) {
        LimitHandler lh = new LimitHandler(getSequenceId(), session, node.getLimitFrom(), node.getLimitTo());
        addHandler(lh);
    }
}
Also used : Order(com.actiontech.dble.plan.Order) ItemSum(com.actiontech.dble.plan.common.item.function.sumfunc.ItemSum) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) OrderedGroupByHandler(com.actiontech.dble.backend.mysql.nio.handler.query.impl.groupby.OrderedGroupByHandler) DirectGroupByHandler(com.actiontech.dble.backend.mysql.nio.handler.query.impl.groupby.DirectGroupByHandler)

Example 2 with Order

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

the class BaseHandlerBuilder method isJoinNodeOrderMatch.

/**
 * the order way of join node stored in left join on orders and right join on orders
 *
 * @param jn
 * @param orderBys
 * @return
 */
private boolean isJoinNodeOrderMatch(JoinNode jn, List<Order> orderBys) {
    // onCondition column in orderBys will be saved to onOrders,
    // eg: if jn.onCond = (t1.id=t2.id),
    // orderBys is t1.id,t2.id,t1.name, and onOrders = {t1.id,t2.id};
    List<Order> onOrders = new ArrayList<>();
    List<Order> leftOnOrders = jn.getLeftJoinOnOrders();
    List<Order> rightOnOrders = jn.getRightJoinOnOrders();
    for (Order orderBy : orderBys) {
        if (leftOnOrders.contains(orderBy) || rightOnOrders.contains(orderBy)) {
            onOrders.add(orderBy);
        } else {
            break;
        }
    }
    if (onOrders.isEmpty()) {
        // join node must order by joinOnCondition
        return false;
    } else {
        List<Order> remainOrders = orderBys.subList(onOrders.size(), orderBys.size());
        if (remainOrders.isEmpty()) {
            return true;
        } else {
            List<Order> pushedOrders = PlanUtil.getPushDownOrders(jn, remainOrders);
            if (jn.isLeftOrderMatch()) {
                List<Order> leftChildOrders = jn.getLeftNode().getOrderBys();
                List<Order> leftRemainOrders = leftChildOrders.subList(leftOnOrders.size(), leftChildOrders.size());
                if (PlanUtil.orderContains(leftRemainOrders, pushedOrders))
                    return true;
            } else if (jn.isRightOrderMatch()) {
                List<Order> rightChildOrders = jn.getRightNode().getOrderBys();
                List<Order> rightRemainOrders = rightChildOrders.subList(rightOnOrders.size(), rightChildOrders.size());
                if (PlanUtil.orderContains(rightRemainOrders, pushedOrders))
                    return true;
            }
            return false;
        }
    }
}
Also used : Order(com.actiontech.dble.plan.Order) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 3 with Order

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

the class GlobalVisitor method buildOrderBy.

protected void buildOrderBy(PlanNode query) {
    boolean first = true;
    if (query.getOrderBys() != null && !query.getOrderBys().isEmpty()) {
        sqlBuilder.append(" order by ");
        for (Order order : query.getOrderBys()) {
            if (first) {
                first = false;
            } else {
                sqlBuilder.append(",");
            }
            Item orderByCol = order.getItem();
            String pdName = "";
            if (orderByCol.basicConstItem())
                pdName = "'" + orderByCol.toString() + "'";
            if (pdName.isEmpty())
                pdName = visitUnSelPushDownName(orderByCol, true);
            sqlBuilder.append(pdName).append(" ").append(order.getSortOrder());
        }
    }
}
Also used : Order(com.actiontech.dble.plan.Order) Item(com.actiontech.dble.plan.common.item.Item)

Example 4 with Order

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

the class AllAnySubQueryHandler method fieldEofResponse.

@Override
public void fieldEofResponse(byte[] headerNull, List<byte[]> fieldsNull, List<FieldPacket> fieldPackets, byte[] eofNull, boolean isLeft, BackendConnection conn) {
    if (terminate.get()) {
        return;
    }
    lock.lock();
    try {
        // create field for first time
        if (this.fieldPackets.isEmpty()) {
            this.fieldPackets = fieldPackets;
            sourceField = HandlerTool.createField(this.fieldPackets.get(0));
            Item select = itemSubQuery.getSelect();
            select.setPushDownName(select.getAlias());
            Item tmpItem = HandlerTool.createItem(select, Collections.singletonList(this.sourceField), 0, isAllPushDown(), type());
            itemSubQuery.setFiled(tmpItem);
            rowComparator = new RowDataComparator(this.fieldPackets, Collections.singletonList(new Order(select)), this.isAllPushDown(), this.type());
        }
    } finally {
        lock.unlock();
    }
}
Also used : Order(com.actiontech.dble.plan.Order) Item(com.actiontech.dble.plan.common.item.Item) RowDataComparator(com.actiontech.dble.backend.mysql.nio.handler.util.RowDataComparator)

Example 5 with Order

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

the class PushDownVisitor method buildOrderBy.

protected void buildOrderBy(PlanNode query) {
    /* if group by exists,it must merge as "group by"'s order,so don't push down order */
    boolean realPush = query.getGroupBys().isEmpty();
    if (query.getOrderBys().size() > 0) {
        if (realPush)
            sqlBuilder.append(" ORDER BY ");
        for (Order order : query.getOrderBys()) {
            Item orderByCol = order.getItem();
            String pdName = "";
            if (orderByCol.basicConstItem())
                pdName = "'" + orderByCol.toString() + "'";
            if (pdName.isEmpty())
                pdName = visitUnSelPushDownName(orderByCol, true);
            if (realPush) {
                pushDownOrderBy.add(order.copy());
                sqlBuilder.append(pdName).append(" ").append(order.getSortOrder()).append(",");
            }
        }
        if (realPush)
            sqlBuilder.deleteCharAt(sqlBuilder.length() - 1);
    }
}
Also used : Order(com.actiontech.dble.plan.Order) Item(com.actiontech.dble.plan.common.item.Item)

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