Search in sources :

Example 1 with RowKeyOrderedAggregateResultIterator

use of org.apache.phoenix.iterate.RowKeyOrderedAggregateResultIterator in project phoenix by apache.

the class AggregatePlan method newIterator.

@Override
protected ResultIterator newIterator(ParallelScanGrouper scanGrouper, Scan scan) throws SQLException {
    if (groupBy.isEmpty()) {
        UngroupedAggregateRegionObserver.serializeIntoScan(scan);
    } else {
        // Set attribute with serialized expressions for coprocessor
        GroupedAggregateRegionObserver.serializeIntoScan(scan, groupBy.getScanAttribName(), groupBy.getKeyExpressions());
        if (limit != null && orderBy.getOrderByExpressions().isEmpty() && having == null && ((statement.isDistinct() && !statement.isAggregate()) || (!statement.isDistinct() && (context.getAggregationManager().isEmpty() || BaseScannerRegionObserver.KEY_ORDERED_GROUP_BY_EXPRESSIONS.equals(groupBy.getScanAttribName()))))) {
            /*
                 * Optimization to early exit from the scan for a GROUP BY or DISTINCT with a LIMIT.
                 * We may exit early according to the LIMIT specified if the query has:
                 * 1) No ORDER BY clause (or the ORDER BY was optimized out). We cannot exit
                 *    early if there's an ORDER BY because the first group may be found last
                 *    in the scan.
                 * 2) No HAVING clause, since we execute the HAVING on the client side. The LIMIT
                 *    needs to be evaluated *after* the HAVING.
                 * 3) DISTINCT clause with no GROUP BY. We cannot exit early if there's a
                 *    GROUP BY, as the GROUP BY is processed on the client-side post aggregation
                 *    if a DISTNCT has a GROUP BY. Otherwise, since there are no aggregate
                 *    functions in a DISTINCT, we can exit early regardless of if the
                 *    groups are in row key order or unordered.
                 * 4) GROUP BY clause with no aggregate functions. This is in the same category
                 *    as (3). If we're using aggregate functions, we need to look at all the
                 *    rows, as otherwise we'd exit early with incorrect aggregate function
                 *    calculations.
                 * 5) GROUP BY clause along the pk axis, as the rows are processed in row key
                 *    order, so we can early exit, even when aggregate functions are used, as
                 *    the rows in the group are contiguous.
                 */
            scan.setAttribute(BaseScannerRegionObserver.GROUP_BY_LIMIT, PInteger.INSTANCE.toBytes(limit + (offset == null ? 0 : offset)));
        }
    }
    BaseResultIterators iterators = isSerial ? new SerialIterators(this, null, null, wrapParallelIteratorFactory(), scanGrouper, scan) : new ParallelIterators(this, null, wrapParallelIteratorFactory(), scan, false);
    estimatedRows = iterators.getEstimatedRowCount();
    estimatedSize = iterators.getEstimatedByteCount();
    splits = iterators.getSplits();
    scans = iterators.getScans();
    AggregatingResultIterator aggResultIterator;
    // No need to merge sort for ungrouped aggregation
    if (groupBy.isEmpty() || groupBy.isUngroupedAggregate()) {
        aggResultIterator = new UngroupedAggregatingResultIterator(new ConcatResultIterator(iterators), aggregators);
    // If salted or local index we still need a merge sort as we'll potentially have multiple group by keys that aren't contiguous.
    } else if (groupBy.isOrderPreserving() && !(this.getTableRef().getTable().getBucketNum() != null || this.getTableRef().getTable().getIndexType() == IndexType.LOCAL)) {
        aggResultIterator = new RowKeyOrderedAggregateResultIterator(iterators, aggregators);
    } else {
        aggResultIterator = new GroupedAggregatingResultIterator(new MergeSortRowKeyResultIterator(iterators, 0, this.getOrderBy() == OrderBy.REV_ROW_KEY_ORDER_BY), aggregators);
    }
    if (having != null) {
        aggResultIterator = new FilterAggregatingResultIterator(aggResultIterator, having);
    }
    if (statement.isDistinct() && statement.isAggregate()) {
        // Dedup on client if select distinct and aggregation
        aggResultIterator = new DistinctAggregatingResultIterator(aggResultIterator, getProjector());
    }
    ResultIterator resultScanner = aggResultIterator;
    if (orderBy.getOrderByExpressions().isEmpty()) {
        if (offset != null) {
            resultScanner = new OffsetResultIterator(aggResultIterator, offset);
        }
        if (limit != null) {
            resultScanner = new LimitingResultIterator(resultScanner, limit);
        }
    } else {
        int thresholdBytes = context.getConnection().getQueryServices().getProps().getInt(QueryServices.SPOOL_THRESHOLD_BYTES_ATTRIB, QueryServicesOptions.DEFAULT_SPOOL_THRESHOLD_BYTES);
        resultScanner = new OrderedAggregatingResultIterator(aggResultIterator, orderBy.getOrderByExpressions(), thresholdBytes, limit, offset);
    }
    if (context.getSequenceManager().getSequenceCount() > 0) {
        resultScanner = new SequenceResultIterator(resultScanner, context.getSequenceManager());
    }
    return resultScanner;
}
Also used : ParallelIterators(org.apache.phoenix.iterate.ParallelIterators) MergeSortRowKeyResultIterator(org.apache.phoenix.iterate.MergeSortRowKeyResultIterator) OffsetResultIterator(org.apache.phoenix.iterate.OffsetResultIterator) GroupedAggregatingResultIterator(org.apache.phoenix.iterate.GroupedAggregatingResultIterator) SerialIterators(org.apache.phoenix.iterate.SerialIterators) SequenceResultIterator(org.apache.phoenix.iterate.SequenceResultIterator) RowKeyOrderedAggregateResultIterator(org.apache.phoenix.iterate.RowKeyOrderedAggregateResultIterator) MergeSortRowKeyResultIterator(org.apache.phoenix.iterate.MergeSortRowKeyResultIterator) OrderedResultIterator(org.apache.phoenix.iterate.OrderedResultIterator) SpoolingResultIterator(org.apache.phoenix.iterate.SpoolingResultIterator) UngroupedAggregatingResultIterator(org.apache.phoenix.iterate.UngroupedAggregatingResultIterator) ConcatResultIterator(org.apache.phoenix.iterate.ConcatResultIterator) AggregatingResultIterator(org.apache.phoenix.iterate.AggregatingResultIterator) GroupedAggregatingResultIterator(org.apache.phoenix.iterate.GroupedAggregatingResultIterator) FilterAggregatingResultIterator(org.apache.phoenix.iterate.FilterAggregatingResultIterator) ResultIterator(org.apache.phoenix.iterate.ResultIterator) DistinctAggregatingResultIterator(org.apache.phoenix.iterate.DistinctAggregatingResultIterator) PeekingResultIterator(org.apache.phoenix.iterate.PeekingResultIterator) SequenceResultIterator(org.apache.phoenix.iterate.SequenceResultIterator) LimitingResultIterator(org.apache.phoenix.iterate.LimitingResultIterator) RowKeyOrderedAggregateResultIterator(org.apache.phoenix.iterate.RowKeyOrderedAggregateResultIterator) OrderedAggregatingResultIterator(org.apache.phoenix.iterate.OrderedAggregatingResultIterator) OffsetResultIterator(org.apache.phoenix.iterate.OffsetResultIterator) BaseResultIterators(org.apache.phoenix.iterate.BaseResultIterators) LimitingResultIterator(org.apache.phoenix.iterate.LimitingResultIterator) UngroupedAggregatingResultIterator(org.apache.phoenix.iterate.UngroupedAggregatingResultIterator) AggregatingResultIterator(org.apache.phoenix.iterate.AggregatingResultIterator) GroupedAggregatingResultIterator(org.apache.phoenix.iterate.GroupedAggregatingResultIterator) FilterAggregatingResultIterator(org.apache.phoenix.iterate.FilterAggregatingResultIterator) DistinctAggregatingResultIterator(org.apache.phoenix.iterate.DistinctAggregatingResultIterator) OrderedAggregatingResultIterator(org.apache.phoenix.iterate.OrderedAggregatingResultIterator) ConcatResultIterator(org.apache.phoenix.iterate.ConcatResultIterator) DistinctAggregatingResultIterator(org.apache.phoenix.iterate.DistinctAggregatingResultIterator) UngroupedAggregatingResultIterator(org.apache.phoenix.iterate.UngroupedAggregatingResultIterator) FilterAggregatingResultIterator(org.apache.phoenix.iterate.FilterAggregatingResultIterator) OrderedAggregatingResultIterator(org.apache.phoenix.iterate.OrderedAggregatingResultIterator)

Aggregations

AggregatingResultIterator (org.apache.phoenix.iterate.AggregatingResultIterator)1 BaseResultIterators (org.apache.phoenix.iterate.BaseResultIterators)1 ConcatResultIterator (org.apache.phoenix.iterate.ConcatResultIterator)1 DistinctAggregatingResultIterator (org.apache.phoenix.iterate.DistinctAggregatingResultIterator)1 FilterAggregatingResultIterator (org.apache.phoenix.iterate.FilterAggregatingResultIterator)1 GroupedAggregatingResultIterator (org.apache.phoenix.iterate.GroupedAggregatingResultIterator)1 LimitingResultIterator (org.apache.phoenix.iterate.LimitingResultIterator)1 MergeSortRowKeyResultIterator (org.apache.phoenix.iterate.MergeSortRowKeyResultIterator)1 OffsetResultIterator (org.apache.phoenix.iterate.OffsetResultIterator)1 OrderedAggregatingResultIterator (org.apache.phoenix.iterate.OrderedAggregatingResultIterator)1 OrderedResultIterator (org.apache.phoenix.iterate.OrderedResultIterator)1 ParallelIterators (org.apache.phoenix.iterate.ParallelIterators)1 PeekingResultIterator (org.apache.phoenix.iterate.PeekingResultIterator)1 ResultIterator (org.apache.phoenix.iterate.ResultIterator)1 RowKeyOrderedAggregateResultIterator (org.apache.phoenix.iterate.RowKeyOrderedAggregateResultIterator)1 SequenceResultIterator (org.apache.phoenix.iterate.SequenceResultIterator)1 SerialIterators (org.apache.phoenix.iterate.SerialIterators)1 SpoolingResultIterator (org.apache.phoenix.iterate.SpoolingResultIterator)1 UngroupedAggregatingResultIterator (org.apache.phoenix.iterate.UngroupedAggregatingResultIterator)1