use of com.actiontech.dble.plan.common.item.function.sumfunc.ItemSum 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.common.item.function.sumfunc.ItemSum in project dble by actiontech.
the class PushDownVisitor method buildSelect.
protected void buildSelect(PlanNode query) {
sqlBuilder.append("select ");
if (query.isDistinct()) {
sqlBuilder.append("DISTINCT ");
}
List<Item> columns = query.getColumnsRefered();
if (columns.size() == 0) {
sqlBuilder.append("1");
return;
}
for (Item col : columns) {
if (existUnPushDownGroup && col.type().equals(Item.ItemType.SUM_FUNC_ITEM))
continue;
if ((col.type().equals(Item.ItemType.FUNC_ITEM) || col.type().equals(Item.ItemType.COND_ITEM)) && col.isWithSumFunc())
continue;
String pdName = visitPushDownNameSel(col);
if (// it's null when duplicate column
StringUtils.isEmpty(pdName))
continue;
if (col.type().equals(Item.ItemType.SUM_FUNC_ITEM)) {
ItemSum funCol = (ItemSum) col;
String funName = funCol.funcName().toUpperCase();
String colName = pdName;
ItemSum.SumFuncType i = funCol.sumType();
if (i == ItemSum.SumFuncType.AVG_FUNC) {
String colNameSum = colName.replace(funName + "(", "SUM(");
colNameSum = colNameSum.replace(getMadeAggAlias(funName), getMadeAggAlias("SUM"));
String colNameCount = colName.replace(funName + "(", "COUNT(");
colNameCount = colNameCount.replace(getMadeAggAlias(funName), getMadeAggAlias("COUNT"));
sqlBuilder.append(colNameSum).append(",").append(colNameCount).append(",");
continue;
} else if (i == ItemSum.SumFuncType.STD_FUNC || i == ItemSum.SumFuncType.VARIANCE_FUNC) {
String colNameCount = colName.replace(funName + "(", "COUNT(");
colNameCount = colNameCount.replace(getMadeAggAlias(funName), getMadeAggAlias("COUNT"));
String colNameSum = colName.replace(funName + "(", "SUM(");
colNameSum = colNameSum.replace(getMadeAggAlias(funName), getMadeAggAlias("SUM"));
String colNameVar = colName.replace(funName + "(", "VARIANCE(");
colNameVar = colNameVar.replace(getMadeAggAlias(funName), getMadeAggAlias("VARIANCE"));
sqlBuilder.append(colNameCount).append(",").append(colNameSum).append(",").append(colNameVar).append(",");
continue;
}
}
sqlBuilder.append(pdName);
sqlBuilder.append(",");
}
sqlBuilder.deleteCharAt(sqlBuilder.length() - 1);
}
use of com.actiontech.dble.plan.common.item.function.sumfunc.ItemSum in project dble by actiontech.
the class DirectGroupByHandler method sendGroupRowPacket.
private void sendGroupRowPacket(MySQLConnection conn) {
groupLocalResult.done();
RowDataPacket row = null;
List<Field> localFields = HandlerTool.createFields(localResultFps);
List<ItemSum> sendSums = new ArrayList<>();
for (ItemSum selSum : referredSumFunctions) {
ItemSum sum = (ItemSum) HandlerTool.createItem(selSum, localFields, 0, false, HandlerType.GROUPBY);
sendSums.add(sum);
}
prepareSumAggregators(sendSums, true);
while ((row = groupLocalResult.next()) != null) {
if (sendGroupRowPacket(conn, row, sendSums))
break;
}
}
use of com.actiontech.dble.plan.common.item.function.sumfunc.ItemSum in project dble by actiontech.
the class OrderedGroupByHandler method sendNoRowGroupRowPacket.
/**
* send data to next even no data here. eg: select count(*) from t2 ,if t2 empty,then send 0
*/
private void sendNoRowGroupRowPacket(MySQLConnection conn) {
RowDataPacket newRp = new RowDataPacket(this.fieldPackets.size() + this.sums.size());
// sumfuncs are front
for (ItemSum sum : this.sums) {
sum.noRowsInResult();
byte[] tmp = sum.getRowPacketByte();
newRp.add(tmp);
}
for (int i = 0; i < this.fieldPackets.size(); i++) {
newRp.add(null);
}
originRp = null;
nextHandler.rowResponse(null, newRp, this.isLeft, conn);
}
use of com.actiontech.dble.plan.common.item.function.sumfunc.ItemSum in project dble by actiontech.
the class OrderedGroupByHandler method fieldEofResponse.
@Override
public void fieldEofResponse(byte[] headerNull, List<byte[]> fieldsNull, final List<FieldPacket> fieldPackets, byte[] eofNull, boolean isLeft, BackendConnection conn) {
this.charset = CharsetUtil.getJavaCharset(conn.getCharset().getResults());
if (terminate.get())
return;
if (this.pool == null)
this.pool = DbleServer.getInstance().getBufferPool();
this.fieldPackets = fieldPackets;
List<Field> sourceFields = HandlerTool.createFields(this.fieldPackets);
for (ItemSum sumFunc : referredSumFunctions) {
ItemSum sum = (ItemSum) (HandlerTool.createItem(sumFunc, sourceFields, 0, this.isAllPushDown(), this.type()));
sums.add(sum);
}
comparator = new RowDataComparator(this.fieldPackets, this.groupBys, this.isAllPushDown(), this.type());
prepareSumAggregators(sums, this.referredSumFunctions, this.fieldPackets, this.isAllPushDown(), true, (MySQLConnection) conn);
setupSumFunctions(sums);
sendGroupFieldPackets(conn);
}
Aggregations