Search in sources :

Example 6 with ValueBitSet

use of org.apache.phoenix.schema.ValueBitSet in project phoenix by apache.

the class PhoenixRuntime method decodeValues.

/**
     * 
     * @param conn connection that was used for reading/generating value.
     * @param fullTableName fully qualified table name
     * @param value byte value of the columns concatenated as a single byte array. @see {@link #encodeColumnValues(Connection, String, Object[], List)}
     * @param columns list of column names for the columns that have their respective values
     * present in the byte array. The column names should be in the same order as their values are in the byte array.
     * The column name includes both family name, if present, and column name.
     * @return decoded values for each column
     * @throws SQLException
     * 
     */
@Deprecated
public static Object[] decodeValues(Connection conn, String fullTableName, byte[] value, List<Pair<String, String>> columns) throws SQLException {
    PTable table = getTable(conn, fullTableName);
    KeyValueSchema kvSchema = buildKeyValueSchema(getPColumns(table, columns));
    ImmutableBytesWritable ptr = new ImmutableBytesWritable(value);
    ValueBitSet valueSet = ValueBitSet.newInstance(kvSchema);
    valueSet.clear();
    valueSet.or(ptr);
    int maxOffset = ptr.getOffset() + ptr.getLength();
    Boolean hasValue;
    kvSchema.iterator(ptr);
    int i = 0;
    List<Object> values = new ArrayList<Object>();
    while (hasValue = kvSchema.next(ptr, i, maxOffset, valueSet) != null) {
        if (hasValue) {
            values.add(kvSchema.getField(i).getDataType().toObject(ptr));
        }
        i++;
    }
    return values.toArray();
}
Also used : ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) ValueBitSet(org.apache.phoenix.schema.ValueBitSet) ArrayList(java.util.ArrayList) KeyValueSchema(org.apache.phoenix.schema.KeyValueSchema) PTable(org.apache.phoenix.schema.PTable)

Example 7 with ValueBitSet

use of org.apache.phoenix.schema.ValueBitSet in project phoenix by apache.

the class SpillManager method getAggregators.

// Instantiate Aggregators from a serialized byte array
private Aggregator[] getAggregators(byte[] data) throws IOException {
    DataInputStream input = null;
    try {
        input = new DataInputStream(new ByteArrayInputStream(data));
        // key length
        int keyLength = WritableUtils.readVInt(input);
        int vIntKeyLength = WritableUtils.getVIntSize(keyLength);
        ImmutableBytesPtr ptr = new ImmutableBytesPtr(data, vIntKeyLength, keyLength);
        // value length
        input.skip(keyLength);
        int valueLength = WritableUtils.readVInt(input);
        int vIntValLength = WritableUtils.getVIntSize(keyLength);
        KeyValue keyValue = KeyValueUtil.newKeyValue(ptr.get(), ptr.getOffset(), ptr.getLength(), QueryConstants.SINGLE_COLUMN_FAMILY, QueryConstants.SINGLE_COLUMN, QueryConstants.AGG_TIMESTAMP, data, vIntKeyLength + keyLength + vIntValLength, valueLength);
        Tuple result = new SingleKeyValueTuple(keyValue);
        TupleUtil.getAggregateValue(result, ptr);
        KeyValueSchema schema = aggregators.getValueSchema();
        ValueBitSet tempValueSet = ValueBitSet.newInstance(schema);
        tempValueSet.clear();
        tempValueSet.or(ptr);
        int i = 0, maxOffset = ptr.getOffset() + ptr.getLength();
        SingleAggregateFunction[] funcArray = aggregators.getFunctions();
        Aggregator[] sAggs = new Aggregator[funcArray.length];
        Boolean hasValue;
        schema.iterator(ptr);
        while ((hasValue = schema.next(ptr, i, maxOffset, tempValueSet)) != null) {
            SingleAggregateFunction func = funcArray[i];
            sAggs[i++] = hasValue ? func.newServerAggregator(conf, ptr) : func.newServerAggregator(conf);
        }
        return sAggs;
    } finally {
        Closeables.closeQuietly(input);
    }
}
Also used : KeyValue(org.apache.hadoop.hbase.KeyValue) ValueBitSet(org.apache.phoenix.schema.ValueBitSet) ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr) SingleKeyValueTuple(org.apache.phoenix.schema.tuple.SingleKeyValueTuple) Aggregator(org.apache.phoenix.expression.aggregator.Aggregator) DataInputStream(java.io.DataInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) SingleAggregateFunction(org.apache.phoenix.expression.function.SingleAggregateFunction) KeyValueSchema(org.apache.phoenix.schema.KeyValueSchema) Tuple(org.apache.phoenix.schema.tuple.Tuple) SingleKeyValueTuple(org.apache.phoenix.schema.tuple.SingleKeyValueTuple)

Example 8 with ValueBitSet

use of org.apache.phoenix.schema.ValueBitSet 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)

Aggregations

ValueBitSet (org.apache.phoenix.schema.ValueBitSet)8 KeyValueSchema (org.apache.phoenix.schema.KeyValueSchema)6 ArrayList (java.util.ArrayList)5 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)5 PTable (org.apache.phoenix.schema.PTable)5 Expression (org.apache.phoenix.expression.Expression)3 LiteralExpression (org.apache.phoenix.expression.LiteralExpression)3 PColumn (org.apache.phoenix.schema.PColumn)3 List (java.util.List)2 KeyValueColumnExpression (org.apache.phoenix.expression.KeyValueColumnExpression)2 OrderByExpression (org.apache.phoenix.expression.OrderByExpression)2 RowKeyColumnExpression (org.apache.phoenix.expression.RowKeyColumnExpression)2 Tuple (org.apache.phoenix.schema.tuple.Tuple)2 ImmutableList (com.google.common.collect.ImmutableList)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataInputStream (java.io.DataInputStream)1 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1