Search in sources :

Example 1 with ItemSum

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);
    }
}
Also used : Order(com.actiontech.dble.plan.Order) ItemSum(com.actiontech.dble.plan.common.item.function.sumfunc.ItemSum) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) OrderedGroupByHandler(com.actiontech.dble.backend.mysql.nio.handler.query.impl.groupby.OrderedGroupByHandler) DirectGroupByHandler(com.actiontech.dble.backend.mysql.nio.handler.query.impl.groupby.DirectGroupByHandler)

Example 2 with ItemSum

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);
}
Also used : Item(com.actiontech.dble.plan.common.item.Item) ItemSum(com.actiontech.dble.plan.common.item.function.sumfunc.ItemSum)

Example 3 with ItemSum

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;
    }
}
Also used : Field(com.actiontech.dble.plan.common.field.Field) ItemSum(com.actiontech.dble.plan.common.item.function.sumfunc.ItemSum) RowDataPacket(com.actiontech.dble.net.mysql.RowDataPacket) ArrayList(java.util.ArrayList)

Example 4 with ItemSum

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);
}
Also used : ItemSum(com.actiontech.dble.plan.common.item.function.sumfunc.ItemSum) RowDataPacket(com.actiontech.dble.net.mysql.RowDataPacket)

Example 5 with ItemSum

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);
}
Also used : Field(com.actiontech.dble.plan.common.field.Field) ItemSum(com.actiontech.dble.plan.common.item.function.sumfunc.ItemSum) RowDataComparator(com.actiontech.dble.backend.mysql.nio.handler.util.RowDataComparator)

Aggregations

ItemSum (com.actiontech.dble.plan.common.item.function.sumfunc.ItemSum)23 DGRowPacket (com.actiontech.dble.backend.mysql.nio.handler.query.impl.groupby.directgroupby.DGRowPacket)8 Item (com.actiontech.dble.plan.common.item.Item)6 RowDataPacket (com.actiontech.dble.net.mysql.RowDataPacket)5 ArrayList (java.util.ArrayList)4 RowDataComparator (com.actiontech.dble.backend.mysql.nio.handler.util.RowDataComparator)3 Field (com.actiontech.dble.plan.common.field.Field)3 FieldPacket (com.actiontech.dble.net.mysql.FieldPacket)2 Order (com.actiontech.dble.plan.Order)2 ItemFunc (com.actiontech.dble.plan.common.item.function.ItemFunc)2 DirectGroupByHandler (com.actiontech.dble.backend.mysql.nio.handler.query.impl.groupby.DirectGroupByHandler)1 OrderedGroupByHandler (com.actiontech.dble.backend.mysql.nio.handler.query.impl.groupby.OrderedGroupByHandler)1 GroupByBucket (com.actiontech.dble.backend.mysql.nio.handler.query.impl.groupby.directgroupby.GroupByBucket)1 DistinctLocalResult (com.actiontech.dble.backend.mysql.store.DistinctLocalResult)1 GroupByLocalResult (com.actiontech.dble.backend.mysql.store.GroupByLocalResult)1 ResultStore (com.actiontech.dble.plan.common.external.ResultStore)1 ItemField (com.actiontech.dble.plan.common.item.ItemField)1 ItemInt (com.actiontech.dble.plan.common.item.ItemInt)1 ItemScalarSubQuery (com.actiontech.dble.plan.common.item.subquery.ItemScalarSubQuery)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1