Search in sources :

Example 1 with AvgRowWidthVisitor

use of org.apache.phoenix.execute.visitor.AvgRowWidthVisitor in project phoenix by apache.

the class ClientAggregatePlan method getCost.

@Override
public Cost getCost() {
    Double outputBytes = this.accept(new ByteCountVisitor());
    Double inputRows = this.getDelegate().accept(new RowCountVisitor());
    Double rowWidth = this.accept(new AvgRowWidthVisitor());
    if (inputRows == null || outputBytes == null || rowWidth == null) {
        return Cost.UNKNOWN;
    }
    double inputBytes = inputRows * rowWidth;
    double rowsBeforeHaving = RowCountVisitor.aggregate(RowCountVisitor.filter(inputRows.doubleValue(), RowCountVisitor.stripSkipScanFilter(context.getScan().getFilter())), groupBy);
    double rowsAfterHaving = RowCountVisitor.filter(rowsBeforeHaving, having);
    double bytesBeforeHaving = rowWidth * rowsBeforeHaving;
    double bytesAfterHaving = rowWidth * rowsAfterHaving;
    int parallelLevel = CostUtil.estimateParallelLevel(false, context.getConnection().getQueryServices());
    Cost cost = CostUtil.estimateAggregateCost(inputBytes, bytesBeforeHaving, groupBy, parallelLevel);
    if (!orderBy.getOrderByExpressions().isEmpty()) {
        Cost orderByCost = CostUtil.estimateOrderByCost(bytesAfterHaving, outputBytes, parallelLevel);
        cost = cost.plus(orderByCost);
    }
    return super.getCost().plus(cost);
}
Also used : ByteCountVisitor(org.apache.phoenix.execute.visitor.ByteCountVisitor) RowCountVisitor(org.apache.phoenix.execute.visitor.RowCountVisitor) AvgRowWidthVisitor(org.apache.phoenix.execute.visitor.AvgRowWidthVisitor) Cost(org.apache.phoenix.optimize.Cost)

Example 2 with AvgRowWidthVisitor

use of org.apache.phoenix.execute.visitor.AvgRowWidthVisitor in project phoenix by apache.

the class HashJoinPlan method getCost.

@Override
public Cost getCost() {
    try {
        Long r = delegate.getEstimatedRowsToScan();
        Double w = delegate.accept(new AvgRowWidthVisitor());
        if (r == null || w == null) {
            return Cost.UNKNOWN;
        }
        int parallelLevel = CostUtil.estimateParallelLevel(true, getContext().getConnection().getQueryServices());
        double rowWidth = w;
        double rows = RowCountVisitor.filter(r.doubleValue(), RowCountVisitor.stripSkipScanFilter(delegate.getContext().getScan().getFilter()));
        double bytes = rowWidth * rows;
        Cost cost = Cost.ZERO;
        double rhsByteSum = 0.0;
        for (int i = 0; i < subPlans.length; i++) {
            double lhsBytes = bytes;
            Double rhsRows = subPlans[i].getInnerPlan().accept(new RowCountVisitor());
            Double rhsWidth = subPlans[i].getInnerPlan().accept(new AvgRowWidthVisitor());
            if (rhsRows == null || rhsWidth == null) {
                return Cost.UNKNOWN;
            }
            double rhsBytes = rhsWidth * rhsRows;
            rows = RowCountVisitor.join(rows, rhsRows, joinInfo.getJoinTypes()[i]);
            rowWidth = AvgRowWidthVisitor.join(rowWidth, rhsWidth, joinInfo.getJoinTypes()[i]);
            bytes = rowWidth * rows;
            cost = cost.plus(CostUtil.estimateHashJoinCost(lhsBytes, rhsBytes, bytes, subPlans[i].hasKeyRangeExpression(), parallelLevel));
            rhsByteSum += rhsBytes;
        }
        if (rhsByteSum > serverCacheLimit) {
            return Cost.UNKNOWN;
        }
        // Calculate the cost of aggregation and ordering that is performed with the HashJoinPlan
        if (delegate instanceof AggregatePlan) {
            AggregatePlan aggPlan = (AggregatePlan) delegate;
            double rowsBeforeHaving = RowCountVisitor.aggregate(rows, aggPlan.getGroupBy());
            double rowsAfterHaving = RowCountVisitor.filter(rowsBeforeHaving, aggPlan.getHaving());
            double bytesBeforeHaving = rowWidth * rowsBeforeHaving;
            double bytesAfterHaving = rowWidth * rowsAfterHaving;
            Cost aggCost = CostUtil.estimateAggregateCost(bytes, bytesBeforeHaving, aggPlan.getGroupBy(), parallelLevel);
            cost = cost.plus(aggCost);
            rows = rowsAfterHaving;
            bytes = bytesAfterHaving;
        }
        double outputRows = RowCountVisitor.limit(rows, delegate.getLimit());
        double outputBytes = rowWidth * outputRows;
        if (!delegate.getOrderBy().getOrderByExpressions().isEmpty()) {
            int parallelLevel2 = CostUtil.estimateParallelLevel(delegate instanceof ScanPlan, getContext().getConnection().getQueryServices());
            Cost orderByCost = CostUtil.estimateOrderByCost(bytes, outputBytes, parallelLevel);
            cost = cost.plus(orderByCost);
        }
        // Calculate the cost of child nodes
        Cost lhsCost = new Cost(0, 0, r.doubleValue() * w);
        Cost rhsCost = Cost.ZERO;
        for (SubPlan subPlan : subPlans) {
            rhsCost = rhsCost.plus(subPlan.getInnerPlan().getCost());
        }
        return cost.plus(lhsCost).plus(rhsCost);
    } catch (SQLException e) {
    }
    return Cost.UNKNOWN;
}
Also used : RowCountVisitor(org.apache.phoenix.execute.visitor.RowCountVisitor) AvgRowWidthVisitor(org.apache.phoenix.execute.visitor.AvgRowWidthVisitor) SQLException(java.sql.SQLException) AtomicLong(java.util.concurrent.atomic.AtomicLong) Cost(org.apache.phoenix.optimize.Cost)

Example 3 with AvgRowWidthVisitor

use of org.apache.phoenix.execute.visitor.AvgRowWidthVisitor in project phoenix by apache.

the class AggregatePlan method getCost.

@Override
public Cost getCost() {
    Double outputBytes = this.accept(new ByteCountVisitor());
    Double rowWidth = this.accept(new AvgRowWidthVisitor());
    Long inputRows = null;
    try {
        inputRows = getEstimatedRowsToScan();
    } catch (SQLException e) {
    // ignored.
    }
    if (inputRows == null || outputBytes == null || rowWidth == null) {
        return Cost.UNKNOWN;
    }
    double inputBytes = inputRows * rowWidth;
    double rowsBeforeHaving = RowCountVisitor.aggregate(RowCountVisitor.filter(inputRows.doubleValue(), RowCountVisitor.stripSkipScanFilter(context.getScan().getFilter())), groupBy);
    double rowsAfterHaving = RowCountVisitor.filter(rowsBeforeHaving, having);
    double bytesBeforeHaving = rowWidth * rowsBeforeHaving;
    double bytesAfterHaving = rowWidth * rowsAfterHaving;
    int parallelLevel = CostUtil.estimateParallelLevel(true, context.getConnection().getQueryServices());
    Cost cost = new Cost(0, 0, inputBytes);
    Cost aggCost = CostUtil.estimateAggregateCost(inputBytes, bytesBeforeHaving, groupBy, parallelLevel);
    cost = cost.plus(aggCost);
    if (!orderBy.getOrderByExpressions().isEmpty()) {
        parallelLevel = CostUtil.estimateParallelLevel(false, context.getConnection().getQueryServices());
        Cost orderByCost = CostUtil.estimateOrderByCost(bytesAfterHaving, outputBytes, parallelLevel);
        cost = cost.plus(orderByCost);
    }
    return cost;
}
Also used : ByteCountVisitor(org.apache.phoenix.execute.visitor.ByteCountVisitor) AvgRowWidthVisitor(org.apache.phoenix.execute.visitor.AvgRowWidthVisitor) SQLException(java.sql.SQLException) Cost(org.apache.phoenix.optimize.Cost)

Aggregations

AvgRowWidthVisitor (org.apache.phoenix.execute.visitor.AvgRowWidthVisitor)3 Cost (org.apache.phoenix.optimize.Cost)3 SQLException (java.sql.SQLException)2 ByteCountVisitor (org.apache.phoenix.execute.visitor.ByteCountVisitor)2 RowCountVisitor (org.apache.phoenix.execute.visitor.RowCountVisitor)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)1