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);
}
}
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;
}
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;
}
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));
}
}
}
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));
}
}
}
Aggregations