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