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