Search in sources :

Example 26 with Tuple

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);
}
Also used : Expression(org.apache.phoenix.expression.Expression) ProjectedColumnExpression(org.apache.phoenix.expression.ProjectedColumnExpression) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) SingleKeyValueTuple(org.apache.phoenix.schema.tuple.SingleKeyValueTuple) Tuple(org.apache.phoenix.schema.tuple.Tuple) SingleKeyValueTuple(org.apache.phoenix.schema.tuple.SingleKeyValueTuple)

Example 27 with Tuple

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());
}
Also used : PColumn(org.apache.phoenix.schema.PColumn) ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) ResultIterator(org.apache.phoenix.iterate.ResultIterator) ProjectedColumnExpression(org.apache.phoenix.expression.ProjectedColumnExpression) QueryPlan(org.apache.phoenix.compile.QueryPlan) Tuple(org.apache.phoenix.schema.tuple.Tuple) SingleKeyValueTuple(org.apache.phoenix.schema.tuple.SingleKeyValueTuple)

Example 28 with Tuple

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;
        }
    };
}
Also used : ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) ValueBitSet(org.apache.phoenix.schema.ValueBitSet) SQLException(java.sql.SQLException) ResultIterator(org.apache.phoenix.iterate.ResultIterator) List(java.util.List) ProjectedValueTuple(org.apache.phoenix.execute.TupleProjector.ProjectedValueTuple) IOException(java.io.IOException) SQLExceptionInfo(org.apache.phoenix.exception.SQLExceptionInfo) Tuple(org.apache.phoenix.schema.tuple.Tuple) ProjectedValueTuple(org.apache.phoenix.execute.TupleProjector.ProjectedValueTuple)

Example 29 with Tuple

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;
}
Also used : Aggregator(org.apache.phoenix.expression.aggregator.Aggregator) Tuple(org.apache.phoenix.schema.tuple.Tuple)

Example 30 with 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);
                }
            }
        }
    };
}
Also used : KeyValue(org.apache.hadoop.hbase.KeyValue) SQLException(java.sql.SQLException) ResultTuple(org.apache.phoenix.schema.tuple.ResultTuple) ArrayList(java.util.ArrayList) Region(org.apache.hadoop.hbase.regionserver.Region) BaseRegionScanner(org.apache.phoenix.coprocessor.BaseRegionScanner) List(java.util.List) ArrayList(java.util.ArrayList) Tuple(org.apache.phoenix.schema.tuple.Tuple) ResultTuple(org.apache.phoenix.schema.tuple.ResultTuple) Result(org.apache.hadoop.hbase.client.Result)

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