Search in sources :

Example 31 with Tuple

use of org.apache.phoenix.schema.tuple.Tuple in project phoenix by apache.

the class OrderedResultIterator method getResultIterator.

private PeekingResultIterator getResultIterator() throws SQLException {
    if (resultIterator != null) {
        return resultIterator;
    }
    final int numSortKeys = orderByExpressions.size();
    List<Expression> expressions = Lists.newArrayList(Collections2.transform(orderByExpressions, TO_EXPRESSION));
    final Comparator<ResultEntry> comparator = buildComparator(orderByExpressions);
    try {
        final MappedByteBufferSortedQueue queueEntries = new MappedByteBufferSortedQueue(comparator, limit, thresholdBytes);
        resultIterator = new PeekingResultIterator() {

            int count = 0;

            @Override
            public Tuple next() throws SQLException {
                ResultEntry entry = queueEntries.poll();
                while (entry != null && offset != null && count < offset) {
                    count++;
                    if (entry.getResult() == null) {
                        return null;
                    }
                    entry = queueEntries.poll();
                }
                if (entry == null || (limit != null && count++ > limit)) {
                    resultIterator.close();
                    resultIterator = PeekingResultIterator.EMPTY_ITERATOR;
                    return null;
                }
                return entry.getResult();
            }

            @Override
            public Tuple peek() throws SQLException {
                ResultEntry entry = queueEntries.peek();
                while (entry != null && offset != null && count < offset) {
                    entry = queueEntries.poll();
                    count++;
                    if (entry == null) {
                        return null;
                    }
                }
                if (limit != null && count > limit) {
                    return null;
                }
                entry = queueEntries.peek();
                if (entry == null) {
                    return null;
                }
                return entry.getResult();
            }

            @Override
            public void explain(List<String> planSteps) {
            }

            @Override
            public void close() throws SQLException {
                queueEntries.close();
            }
        };
        for (Tuple result = delegate.next(); result != null; result = delegate.next()) {
            int pos = 0;
            ImmutableBytesWritable[] sortKeys = new ImmutableBytesWritable[numSortKeys];
            for (Expression expression : expressions) {
                final ImmutableBytesWritable sortKey = new ImmutableBytesWritable();
                boolean evaluated = expression.evaluate(result, sortKey);
                // set the sort key that failed to get evaluated with null
                sortKeys[pos++] = evaluated && sortKey.getLength() > 0 ? sortKey : null;
            }
            queueEntries.add(new ResultEntry(sortKeys, result));
        }
        this.byteSize = queueEntries.getByteSize();
    } catch (IOException e) {
        ServerUtil.createIOException(e.getMessage(), e);
    } finally {
        delegate.close();
    }
    return resultIterator;
}
Also used : ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) SQLException(java.sql.SQLException) IOException(java.io.IOException) OrderByExpression(org.apache.phoenix.expression.OrderByExpression) Expression(org.apache.phoenix.expression.Expression) Tuple(org.apache.phoenix.schema.tuple.Tuple)

Example 32 with Tuple

use of org.apache.phoenix.schema.tuple.Tuple in project phoenix by apache.

the class ConcatResultIterator method currentIterator.

private PeekingResultIterator currentIterator() throws SQLException {
    List<PeekingResultIterator> iterators = getIterators();
    while (index < iterators.size()) {
        PeekingResultIterator iterator = iterators.get(index);
        Tuple r = iterator.peek();
        if (r != null) {
            return iterator;
        }
        iterator.close();
        index++;
    }
    return EMPTY_ITERATOR;
}
Also used : Tuple(org.apache.phoenix.schema.tuple.Tuple)

Example 33 with Tuple

use of org.apache.phoenix.schema.tuple.Tuple in project phoenix by apache.

the class DistinctAggregatingResultIterator method getResultIterator.

private Iterator<ResultEntry> getResultIterator() throws SQLException {
    if (resultIterator != null) {
        return resultIterator;
    }
    // TODO: size?
    Set<ResultEntry> entries = Sets.<ResultEntry>newHashSet();
    try {
        for (Tuple result = delegate.next(); result != null; result = delegate.next()) {
            ResultEntry entry = new ResultEntry(result);
            entries.add(entry);
        }
    } finally {
        delegate.close();
    }
    resultIterator = entries.iterator();
    return resultIterator;
}
Also used : Tuple(org.apache.phoenix.schema.tuple.Tuple)

Example 34 with Tuple

use of org.apache.phoenix.schema.tuple.Tuple in project phoenix by apache.

the class LookAheadResultIterator method next.

@Override
public Tuple next() throws SQLException {
    init();
    Tuple next = this.next;
    this.next = advance();
    return next;
}
Also used : Tuple(org.apache.phoenix.schema.tuple.Tuple) ResultTuple(org.apache.phoenix.schema.tuple.ResultTuple)

Example 35 with Tuple

use of org.apache.phoenix.schema.tuple.Tuple in project phoenix by apache.

the class MaterializedComparableResultIterator method next.

@Override
public Tuple next() throws SQLException {
    Tuple next = delegate.next();
    this.current = delegate.peek();
    return next;
}
Also used : Tuple(org.apache.phoenix.schema.tuple.Tuple)

Aggregations

Tuple (org.apache.phoenix.schema.tuple.Tuple)48 SingleKeyValueTuple (org.apache.phoenix.schema.tuple.SingleKeyValueTuple)22 KeyValue (org.apache.hadoop.hbase.KeyValue)16 List (java.util.List)10 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)10 ArrayList (java.util.ArrayList)9 Test (org.junit.Test)9 Expression (org.apache.phoenix.expression.Expression)8 SQLException (java.sql.SQLException)7 Cell (org.apache.hadoop.hbase.Cell)6 LiteralExpression (org.apache.phoenix.expression.LiteralExpression)6 IOException (java.io.IOException)5 Region (org.apache.hadoop.hbase.regionserver.Region)5 ProjectedColumnExpression (org.apache.phoenix.expression.ProjectedColumnExpression)5 Aggregator (org.apache.phoenix.expression.aggregator.Aggregator)5 ResultIterator (org.apache.phoenix.iterate.ResultIterator)5 PColumn (org.apache.phoenix.schema.PColumn)5 ResultTuple (org.apache.phoenix.schema.tuple.ResultTuple)5 ClientAggregators (org.apache.phoenix.expression.aggregator.ClientAggregators)4 ImmutableBytesPtr (org.apache.phoenix.hbase.index.util.ImmutableBytesPtr)4