Search in sources :

Example 16 with Order

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

the class OrderByPusher method buildImplicitOrderBys.

/**
 * generated node's order by. eg:no order by but exist groupby ,add orderby
 */
private static void buildImplicitOrderBys(PlanNode node) {
    // if  has order by,
    List<Order> newOrderBys = new ArrayList<>();
    if (!node.getOrderBys().isEmpty()) {
        if (!node.getGroupBys().isEmpty()) {
            // is order by contains group by
            for (Order orderBy : node.getOrderBys()) {
                if (findOrderByByColumn(node.getGroupBys(), orderBy.getItem()) != null) {
                    newOrderBys.add(orderBy.copy());
                } else {
                    if (newOrderBys.size() == node.getGroupBys().size()) {
                        // contains
                        // reorder group by
                        node.setGroupBys(newOrderBys);
                    } else {
                        return;
                    }
                }
            }
            for (Order groupBy : node.getGroupBys()) {
                if (findOrderByByColumn(newOrderBys, groupBy.getItem()) == null) {
                    // add field which is not in order by
                    newOrderBys.add(groupBy.copy());
                }
            }
            // reorder group by
            node.setGroupBys(newOrderBys);
            node.setOrderBys(newOrderBys);
        }
    } else {
        // no order by,copy group by
        if (!node.getGroupBys().isEmpty()) {
            for (Order orderBy : node.getGroupBys()) {
                newOrderBys.add(orderBy.copy());
            }
        }
        node.setOrderBys(newOrderBys);
    }
}
Also used : Order(com.actiontech.dble.plan.Order) ArrayList(java.util.ArrayList)

Example 17 with Order

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

the class SelectedProcessor method mergePushSelected.

// union's push is different , when toPushColumn.isEmpty,   merge's select can't be change
private static PlanNode mergePushSelected(MergeNode merge, Collection<Item> toPushColumns) {
    if (toPushColumns.isEmpty() && merge.getOrderBys().isEmpty()) {
        for (PlanNode child : merge.getChildren()) {
            pushSelected(child, new HashSet<Item>());
        }
        return merge;
    }
    boolean canOverload = mergeNodeChildsCheck(merge) && !toPushColumns.isEmpty();
    final Map<String, Integer> colIndexs = merge.getColIndexs();
    List<Item> mergeSelects = null;
    if (toPushColumns.isEmpty()) {
        // merge's select can't be change
        mergeSelects = new ArrayList<>();
        merge.setComeInFields(mergeSelects);
        mergeSelects.addAll(merge.getColumnsSelected());
    } else {
        mergeSelects = merge.getColumnsSelected();
    }
    if (canOverload) {
        mergeSelects.clear();
        mergeSelects.addAll(toPushColumns);
    } else {
        for (Item toPush : toPushColumns) {
            if (!mergeSelects.contains(toPush)) {
                mergeSelects.add(toPush);
            }
        }
    }
    // add order by
    for (Order orderby : merge.getOrderBys()) {
        Item orderSel = orderby.getItem();
        mergePushOrderBy(orderSel, mergeSelects);
    }
    // push down the merge's select
    List<List<Item>> allChildPushs = new ArrayList<>(toPushColumns.size());
    for (Item toPush : mergeSelects) {
        // union's order by must be found in selects
        if (toPush.getPushDownName() == null && !toPush.type().equals(Item.ItemType.FIELD_ITEM))
            toPush.setPushDownName(toPush.getItemName());
        List<Item> childPushs = PlanUtil.getPushItemsToUnionChild(merge, toPush, colIndexs);
        allChildPushs.add(childPushs);
    }
    // make all child's count of pushing down is equal
    for (int index = 0; index < merge.getChildren().size(); index++) {
        List<Item> colSels = merge.getChildren().get(index).getColumnsSelected();
        colSels.clear();
        for (List<Item> childPushs : allChildPushs) {
            colSels.add(childPushs.get(index));
        }
        pushSelected(merge.getChildren().get(index), new HashSet<Item>());
    }
    return merge;
}
Also used : Order(com.actiontech.dble.plan.Order) Item(com.actiontech.dble.plan.common.item.Item) PlanNode(com.actiontech.dble.plan.node.PlanNode)

Example 18 with Order

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

the class PlanUtil method getPushDownOrders.

/**
 * generate push orders from orders node
 *
 * @param node node
 * @param orders orders
 * @return List<Order>
 */
public static List<Order> getPushDownOrders(PlanNode node, List<Order> orders) {
    List<Order> pushOrders = new ArrayList<>();
    for (Order order : orders) {
        Item newSel = pushDownItem(node, order.getItem());
        Order newOrder = new Order(newSel, order.getSortOrder());
        pushOrders.add(newOrder);
    }
    return pushOrders;
}
Also used : Order(com.actiontech.dble.plan.Order)

Example 19 with Order

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

the class PlanNode method setUpGroupBy.

private void setUpGroupBy() {
    nameContext.setFindInSelect(true);
    nameContext.setSelectFirst(false);
    for (Order order : groups) {
        Item item = order.getItem();
        if (item.type() == Item.ItemType.INT_ITEM) {
            int index = item.valInt().intValue();
            if (index >= 1 && index <= getColumnsSelected().size())
                order.setItem(getColumnsSelected().get(index - 1));
            else
                throw new MySQLOutPutException(ErrorCode.ER_OPTIMIZER, "", "Unknown column '" + index + "' in group statement");
        } else {
            order.setItem(setUpItem(item));
        }
    }
}
Also used : Order(com.actiontech.dble.plan.Order) Item(com.actiontech.dble.plan.common.item.Item) MySQLOutPutException(com.actiontech.dble.plan.common.exception.MySQLOutPutException)

Example 20 with Order

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

the class PlanNode method setUpOrderBy.

private void setUpOrderBy() {
    nameContext.setFindInSelect(true);
    nameContext.setSelectFirst(true);
    for (Order order : orderBys) {
        Item item = order.getItem();
        if (item.type() == Item.ItemType.INT_ITEM) {
            int index = item.valInt().intValue();
            if (index >= 1 && index <= getColumnsSelected().size())
                order.setItem(getColumnsSelected().get(index - 1));
            else
                throw new MySQLOutPutException(ErrorCode.ER_OPTIMIZER, "", "Unknown column '" + index + "' in order statement");
        } else {
            order.setItem(setUpItem(item));
        }
    }
}
Also used : Order(com.actiontech.dble.plan.Order) Item(com.actiontech.dble.plan.common.item.Item) MySQLOutPutException(com.actiontech.dble.plan.common.exception.MySQLOutPutException)

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