Search in sources :

Example 1 with SingleKeyValueTuple

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

the class FirstLastValueBaseClientAggregator method aggregate.

@Override
public void aggregate(Tuple tuple, ImmutableBytesWritable ptr) {
    //if is called cause aggregation in ORDER BY clausule
    if (tuple instanceof SingleKeyValueTuple) {
        topValue = ptr.copyBytes();
        return;
    }
    FirstLastNthValueDataContainer payload = new FirstLastNthValueDataContainer();
    payload.setPayload(ptr.copyBytes());
    isAscending = payload.getIsAscending();
    TreeMap<byte[], LinkedList<byte[]>> serverAggregatorResult = payload.getData();
    if (useOffset) {
        //merge topValues
        for (Entry<byte[], LinkedList<byte[]>> entry : serverAggregatorResult.entrySet()) {
            byte[] itemKey = entry.getKey();
            LinkedList<byte[]> itemList = entry.getValue();
            if (topValues.containsKey(itemKey)) {
                topValues.get(itemKey).addAll(itemList);
            } else {
                topValues.put(itemKey, itemList);
            }
        }
    } else {
        Entry<byte[], LinkedList<byte[]>> valueEntry = serverAggregatorResult.firstEntry();
        byte[] currentOrder = valueEntry.getKey();
        boolean isBetter;
        if (isAscending) {
            isBetter = topOrder.compareTo(currentOrder) > 0;
        } else {
            //desc
            isBetter = topOrder.compareTo(currentOrder) < 0;
        }
        if (topOrder.getValue().length < 1 || isBetter) {
            topOrder = new BinaryComparator(currentOrder);
            topValue = valueEntry.getValue().getFirst();
        }
    }
}
Also used : FirstLastNthValueDataContainer(org.apache.phoenix.util.FirstLastNthValueDataContainer) SingleKeyValueTuple(org.apache.phoenix.schema.tuple.SingleKeyValueTuple) LinkedList(java.util.LinkedList) BinaryComparator(org.apache.hadoop.hbase.filter.BinaryComparator)

Example 2 with SingleKeyValueTuple

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

the class UngroupedAggregatingResultIterator method next.

@Override
public Tuple next() throws SQLException {
    Tuple result = super.next();
    // Ensure ungrouped aggregregation always returns a row, even if the underlying iterator doesn't.
    if (result == null && !hasRows) {
        // We should reset ClientAggregators here in case they are being reused in a new ResultIterator.
        aggregators.reset(aggregators.getAggregators());
        byte[] value = aggregators.toBytes(aggregators.getAggregators());
        result = new SingleKeyValueTuple(KeyValueUtil.newKeyValue(UNGROUPED_AGG_ROW_KEY, SINGLE_COLUMN_FAMILY, SINGLE_COLUMN, AGG_TIMESTAMP, value));
    }
    hasRows = true;
    return result;
}
Also used : SingleKeyValueTuple(org.apache.phoenix.schema.tuple.SingleKeyValueTuple) Tuple(org.apache.phoenix.schema.tuple.Tuple) SingleKeyValueTuple(org.apache.phoenix.schema.tuple.SingleKeyValueTuple)

Example 3 with SingleKeyValueTuple

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

the class ConcatResultIteratorTest method testMergeSort.

@Test
public void testMergeSort() throws Throwable {
    Tuple[] results1 = new Tuple[] { new SingleKeyValueTuple(new KeyValue(C, SINGLE_COLUMN_FAMILY, SINGLE_COLUMN, Bytes.toBytes(1))) };
    Tuple[] results2 = new Tuple[] { new SingleKeyValueTuple(new KeyValue(B, SINGLE_COLUMN_FAMILY, SINGLE_COLUMN, Bytes.toBytes(2))) };
    Tuple[] results3 = new Tuple[] { new SingleKeyValueTuple(new KeyValue(A, SINGLE_COLUMN_FAMILY, SINGLE_COLUMN, Bytes.toBytes(3))), new SingleKeyValueTuple(new KeyValue(D, SINGLE_COLUMN_FAMILY, SINGLE_COLUMN, Bytes.toBytes(4))) };
    final List<PeekingResultIterator> results = new ArrayList<PeekingResultIterator>(Arrays.asList(new PeekingResultIterator[] { new MaterializedResultIterator(Arrays.asList(results1)), new MaterializedResultIterator(Arrays.asList(results2)), new MaterializedResultIterator(Arrays.asList(results3)) }));
    Tuple[] expectedResults = new Tuple[] { new SingleKeyValueTuple(new KeyValue(A, SINGLE_COLUMN_FAMILY, SINGLE_COLUMN, Bytes.toBytes(3))), new SingleKeyValueTuple(new KeyValue(B, SINGLE_COLUMN_FAMILY, SINGLE_COLUMN, Bytes.toBytes(2))), new SingleKeyValueTuple(new KeyValue(C, SINGLE_COLUMN_FAMILY, SINGLE_COLUMN, Bytes.toBytes(1))), new SingleKeyValueTuple(new KeyValue(D, SINGLE_COLUMN_FAMILY, SINGLE_COLUMN, Bytes.toBytes(4))) };
    ResultIterators iterators = new ResultIterators() {

        @Override
        public List<PeekingResultIterator> getIterators() throws SQLException {
            return results;
        }

        @Override
        public int size() {
            return results.size();
        }

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

        @Override
        public List<KeyRange> getSplits() {
            return Collections.emptyList();
        }

        @Override
        public List<List<Scan>> getScans() {
            return Collections.emptyList();
        }

        @Override
        public void close() throws SQLException {
        }
    };
    ResultIterator scanner = new MergeSortRowKeyResultIterator(iterators);
    AssertResults.assertResults(scanner, expectedResults);
}
Also used : KeyValue(org.apache.hadoop.hbase.KeyValue) KeyRange(org.apache.phoenix.query.KeyRange) SingleKeyValueTuple(org.apache.phoenix.schema.tuple.SingleKeyValueTuple) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Tuple(org.apache.phoenix.schema.tuple.Tuple) SingleKeyValueTuple(org.apache.phoenix.schema.tuple.SingleKeyValueTuple) Test(org.junit.Test)

Example 4 with SingleKeyValueTuple

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

the class MergeSortResultIteratorTest method testReverseMergeSort.

@Test
public void testReverseMergeSort() throws Throwable {
    Tuple[] results1 = new Tuple[] { new SingleKeyValueTuple(new KeyValue(A, SINGLE_COLUMN_FAMILY, SINGLE_COLUMN, Bytes.toBytes(1))) };
    Tuple[] results2 = new Tuple[] { new SingleKeyValueTuple(new KeyValue(B, SINGLE_COLUMN_FAMILY, SINGLE_COLUMN, Bytes.toBytes(1))) };
    Tuple[] results3 = new Tuple[] { new SingleKeyValueTuple(new KeyValue(B, SINGLE_COLUMN_FAMILY, SINGLE_COLUMN, Bytes.toBytes(1))), new SingleKeyValueTuple(new KeyValue(A, SINGLE_COLUMN_FAMILY, SINGLE_COLUMN, Bytes.toBytes(1))) };
    final List<PeekingResultIterator> results = new ArrayList<PeekingResultIterator>(Arrays.asList(new PeekingResultIterator[] { new MaterializedResultIterator(Arrays.asList(results1)), new MaterializedResultIterator(Arrays.asList(results2)), new MaterializedResultIterator(Arrays.asList(results3)) }));
    Tuple[] expectedResults = new Tuple[] { new SingleKeyValueTuple(new KeyValue(B, SINGLE_COLUMN_FAMILY, SINGLE_COLUMN, Bytes.toBytes(1))), new SingleKeyValueTuple(new KeyValue(B, SINGLE_COLUMN_FAMILY, SINGLE_COLUMN, Bytes.toBytes(1))), new SingleKeyValueTuple(new KeyValue(A, SINGLE_COLUMN_FAMILY, SINGLE_COLUMN, Bytes.toBytes(1))), new SingleKeyValueTuple(new KeyValue(A, SINGLE_COLUMN_FAMILY, SINGLE_COLUMN, Bytes.toBytes(1))) };
    ResultIterators iterators = new ResultIterators() {

        @Override
        public List<PeekingResultIterator> getIterators() throws SQLException {
            return results;
        }

        @Override
        public int size() {
            return results.size();
        }

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

        @Override
        public List<KeyRange> getSplits() {
            return Collections.emptyList();
        }

        @Override
        public List<List<Scan>> getScans() {
            return Collections.emptyList();
        }

        @Override
        public void close() throws SQLException {
        }
    };
    ResultIterator scanner = new MergeSortRowKeyResultIterator(iterators, 0, true);
    AssertResults.assertResults(scanner, expectedResults);
}
Also used : KeyValue(org.apache.hadoop.hbase.KeyValue) KeyRange(org.apache.phoenix.query.KeyRange) SingleKeyValueTuple(org.apache.phoenix.schema.tuple.SingleKeyValueTuple) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Tuple(org.apache.phoenix.schema.tuple.Tuple) SingleKeyValueTuple(org.apache.phoenix.schema.tuple.SingleKeyValueTuple) Test(org.junit.Test)

Example 5 with SingleKeyValueTuple

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

the class RowKeyOrderedAggregateResultIteratorTest method testSpanThree.

@Test
public void testSpanThree() throws Exception {
    Tuple[] results1 = new Tuple[] { new SingleKeyValueTuple(new KeyValue(A, SINGLE_COLUMN_FAMILY, SINGLE_COLUMN, Bytes.toBytes(1L))), new SingleKeyValueTuple(new KeyValue(B, SINGLE_COLUMN_FAMILY, SINGLE_COLUMN, Bytes.toBytes(2L))) };
    Tuple[] results2 = new Tuple[] { new SingleKeyValueTuple(new KeyValue(B, SINGLE_COLUMN_FAMILY, SINGLE_COLUMN, Bytes.toBytes(3L))) };
    Tuple[] results3 = new Tuple[] { new SingleKeyValueTuple(new KeyValue(B, SINGLE_COLUMN_FAMILY, SINGLE_COLUMN, Bytes.toBytes(4L))), new SingleKeyValueTuple(new KeyValue(C, SINGLE_COLUMN_FAMILY, SINGLE_COLUMN, Bytes.toBytes(5L))) };
    final List<PeekingResultIterator> results = Arrays.asList(new PeekingResultIterator[] { new MaterializedResultIterator(Arrays.asList(results1)), new MaterializedResultIterator(Arrays.asList(results2)), new MaterializedResultIterator(Arrays.asList(results3)) });
    ResultIterators iterators = new MaterializedResultIterators(results);
    Tuple[] expectedResults = new Tuple[] { new SingleKeyValueTuple(new KeyValue(A, SINGLE_COLUMN_FAMILY, SINGLE_COLUMN, Bytes.toBytes(1L))), new SingleKeyValueTuple(new KeyValue(B, SINGLE_COLUMN_FAMILY, SINGLE_COLUMN, Bytes.toBytes(9L))), new SingleKeyValueTuple(new KeyValue(C, SINGLE_COLUMN_FAMILY, SINGLE_COLUMN, Bytes.toBytes(5L))) };
    ClientAggregators aggregators = TestUtil.getSingleSumAggregator(getUrl(), PropertiesUtil.deepCopy(TestUtil.TEST_PROPERTIES));
    ResultIterator scanner = new RowKeyOrderedAggregateResultIterator(iterators, aggregators);
    AssertResults.assertResults(scanner, expectedResults);
}
Also used : KeyValue(org.apache.hadoop.hbase.KeyValue) ClientAggregators(org.apache.phoenix.expression.aggregator.ClientAggregators) SingleKeyValueTuple(org.apache.phoenix.schema.tuple.SingleKeyValueTuple) Tuple(org.apache.phoenix.schema.tuple.Tuple) SingleKeyValueTuple(org.apache.phoenix.schema.tuple.SingleKeyValueTuple) Test(org.junit.Test) BaseConnectionlessQueryTest(org.apache.phoenix.query.BaseConnectionlessQueryTest)

Aggregations

SingleKeyValueTuple (org.apache.phoenix.schema.tuple.SingleKeyValueTuple)19 Tuple (org.apache.phoenix.schema.tuple.Tuple)17 KeyValue (org.apache.hadoop.hbase.KeyValue)12 Test (org.junit.Test)9 ClientAggregators (org.apache.phoenix.expression.aggregator.ClientAggregators)5 BaseConnectionlessQueryTest (org.apache.phoenix.query.BaseConnectionlessQueryTest)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 KeyRange (org.apache.phoenix.query.KeyRange)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 DataInputStream (java.io.DataInputStream)2 Expression (org.apache.phoenix.expression.Expression)2 LiteralExpression (org.apache.phoenix.expression.LiteralExpression)2 ProjectedColumnExpression (org.apache.phoenix.expression.ProjectedColumnExpression)2 Aggregator (org.apache.phoenix.expression.aggregator.Aggregator)2 ImmutableBytesPtr (org.apache.phoenix.hbase.index.util.ImmutableBytesPtr)2 PDataType (org.apache.phoenix.schema.types.PDataType)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 LinkedList (java.util.LinkedList)1