use of com.actiontech.dble.backend.mysql.nio.handler.query.impl.groupby.OrderedGroupByHandler 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);
}
}
Aggregations