use of org.apache.phoenix.schema.tuple.Tuple in project phoenix by apache.
the class LiteralResultIteratorPlanTest method newLiteralResultIterationPlan.
private QueryPlan newLiteralResultIterationPlan(Integer offset, Integer limit) throws SQLException {
List<Tuple> tuples = Lists.newArrayList();
Tuple baseTuple = new SingleKeyValueTuple(KeyValue.LOWESTKEY);
for (Object[] row : RELATION) {
Expression[] exprs = new Expression[row.length];
for (int i = 0; i < row.length; i++) {
exprs[i] = LiteralExpression.newConstant(row[i]);
}
TupleProjector projector = new TupleProjector(exprs);
tuples.add(projector.projectResults(baseTuple));
}
return new LiteralResultIterationPlan(tuples, CONTEXT, SelectStatement.SELECT_ONE, TableRef.EMPTY_TABLE_REF, RowProjector.EMPTY_PROJECTOR, limit, offset, OrderBy.EMPTY_ORDER_BY, null);
}
use of org.apache.phoenix.schema.tuple.Tuple in project phoenix by apache.
the class LiteralResultIteratorPlanTest method testLiteralResultIteratorPlan.
private void testLiteralResultIteratorPlan(Object[][] expectedResult, Integer offset, Integer limit) throws SQLException {
QueryPlan plan = newLiteralResultIterationPlan(offset, limit);
ResultIterator iter = plan.iterator();
ImmutableBytesWritable ptr = new ImmutableBytesWritable();
for (Object[] row : expectedResult) {
Tuple next = iter.next();
assertNotNull(next);
for (int i = 0; i < row.length; i++) {
PColumn column = table.getColumns().get(i);
boolean eval = new ProjectedColumnExpression(column, table, column.getName().getString()).evaluate(next, ptr);
Object o = eval ? column.getDataType().toObject(ptr) : null;
assertEquals(row[i], o);
}
}
assertNull(iter.next());
}
use of org.apache.phoenix.schema.tuple.Tuple in project phoenix by apache.
the class CorrelatePlan method iterator.
@Override
public ResultIterator iterator(final ParallelScanGrouper scanGrouper, final Scan scan) throws SQLException {
return new ResultIterator() {
private final ValueBitSet destBitSet = ValueBitSet.newInstance(joinedSchema);
private final ValueBitSet lhsBitSet = ValueBitSet.newInstance(lhsSchema);
private final ValueBitSet rhsBitSet = (joinType == JoinType.Semi || joinType == JoinType.Anti) ? ValueBitSet.EMPTY_VALUE_BITSET : ValueBitSet.newInstance(rhsSchema);
private final ResultIterator iter = delegate.iterator(scanGrouper, scan);
private ResultIterator rhsIter = null;
private Tuple current = null;
private boolean closed = false;
@Override
public void close() throws SQLException {
if (!closed) {
closed = true;
iter.close();
if (rhsIter != null) {
rhsIter.close();
}
}
}
@Override
public Tuple next() throws SQLException {
if (closed)
return null;
Tuple rhsCurrent = null;
if (rhsIter != null) {
rhsCurrent = rhsIter.next();
if (rhsCurrent == null) {
rhsIter.close();
rhsIter = null;
} else if (isSingleValueOnly) {
throw new SQLExceptionInfo.Builder(SQLExceptionCode.SINGLE_ROW_SUBQUERY_RETURNS_MULTIPLE_ROWS).build().buildException();
}
}
while (rhsIter == null) {
current = iter.next();
if (current == null) {
close();
return null;
}
runtimeContext.setCorrelateVariableValue(variableId, current);
rhsIter = rhs.iterator();
rhsCurrent = rhsIter.next();
if ((rhsCurrent == null && (joinType == JoinType.Inner || joinType == JoinType.Semi)) || (rhsCurrent != null && joinType == JoinType.Anti)) {
rhsIter.close();
rhsIter = null;
}
}
Tuple joined;
try {
joined = rhsBitSet == ValueBitSet.EMPTY_VALUE_BITSET ? current : TupleProjector.mergeProjectedValue(convertLhs(current), joinedSchema, destBitSet, rhsCurrent, rhsSchema, rhsBitSet, rhsFieldPosition, true);
} catch (IOException e) {
throw new SQLException(e);
}
if ((joinType == JoinType.Semi || rhsCurrent == null) && rhsIter != null) {
rhsIter.close();
rhsIter = null;
}
return joined;
}
@Override
public void explain(List<String> planSteps) {
}
private ProjectedValueTuple convertLhs(Tuple lhs) throws IOException {
ProjectedValueTuple t;
if (lhs instanceof ProjectedValueTuple) {
t = (ProjectedValueTuple) lhs;
} else {
ImmutableBytesWritable ptr = getContext().getTempPtr();
TupleProjector.decodeProjectedValue(lhs, ptr);
lhsBitSet.clear();
lhsBitSet.or(ptr);
int bitSetLen = lhsBitSet.getEstimatedLength();
t = new ProjectedValueTuple(lhs, lhs.getValue(0).getTimestamp(), ptr.get(), ptr.getOffset(), ptr.getLength(), bitSetLen);
}
return t;
}
};
}
use of org.apache.phoenix.schema.tuple.Tuple in project phoenix by apache.
the class BaseGroupedAggregatingResultIterator method next.
@Override
public Tuple next() throws SQLException {
Tuple result = resultIterator.next();
if (result == null) {
return null;
}
if (currentKey.get() == UNITIALIZED_KEY_BUFFER) {
getGroupingKey(result, currentKey);
}
Aggregator[] rowAggregators = aggregators.getAggregators();
aggregators.reset(rowAggregators);
while (true) {
aggregators.aggregate(rowAggregators, result);
Tuple nextResult = resultIterator.peek();
if (nextResult == null || !currentKey.equals(getGroupingKey(nextResult, nextKey))) {
break;
}
result = resultIterator.next();
}
byte[] value = aggregators.toBytes(rowAggregators);
Tuple tuple = wrapKeyValueAsResult(KeyValueUtil.newKeyValue(currentKey, SINGLE_COLUMN_FAMILY, SINGLE_COLUMN, AGG_TIMESTAMP, value, 0, value.length));
currentKey.set(nextKey.get(), nextKey.getOffset(), nextKey.getLength());
return tuple;
}
use of org.apache.phoenix.schema.tuple.Tuple in project phoenix by apache.
the class NonAggregateRegionScannerFactory method getOffsetScanner.
private RegionScanner getOffsetScanner(final RegionScanner s, final OffsetResultIterator iterator, final boolean isLastScan) throws IOException {
final Tuple firstTuple;
final Region region = getRegion();
region.startRegionOperation();
try {
Tuple tuple = iterator.next();
if (tuple == null && !isLastScan) {
List<KeyValue> kvList = new ArrayList<KeyValue>(1);
KeyValue kv = new KeyValue(QueryConstants.OFFSET_ROW_KEY_BYTES, QueryConstants.OFFSET_FAMILY, QueryConstants.OFFSET_COLUMN, PInteger.INSTANCE.toBytes(iterator.getRemainingOffset()));
kvList.add(kv);
Result r = new Result(kvList);
firstTuple = new ResultTuple(r);
} else {
firstTuple = tuple;
}
} catch (Throwable t) {
ServerUtil.throwIOException(getRegion().getRegionInfo().getRegionNameAsString(), t);
return null;
} finally {
region.closeRegionOperation();
}
return new BaseRegionScanner(s) {
private Tuple tuple = firstTuple;
@Override
public boolean isFilterDone() {
return tuple == null;
}
@Override
public boolean next(List<Cell> results) throws IOException {
try {
if (isFilterDone()) {
return false;
}
for (int i = 0; i < tuple.size(); i++) {
results.add(tuple.getValue(i));
}
tuple = iterator.next();
return !isFilterDone();
} catch (Throwable t) {
ServerUtil.throwIOException(getRegion().getRegionInfo().getRegionNameAsString(), t);
return false;
}
}
@Override
public void close() throws IOException {
try {
s.close();
} finally {
try {
if (iterator != null) {
iterator.close();
}
} catch (SQLException e) {
ServerUtil.throwIOException(getRegion().getRegionInfo().getRegionNameAsString(), e);
}
}
}
};
}
Aggregations