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();
}
}
}
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;
}
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);
}
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);
}
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);
}
Aggregations