Search in sources :

Example 1 with PDataCodec

use of org.apache.phoenix.schema.types.PDataType.PDataCodec in project phoenix by apache.

the class RoundDateExpression method newKeyPart.

/**
     * Form the key range from the key to the key right before or at the
     * next rounded value.
     */
@Override
public KeyPart newKeyPart(final KeyPart childPart) {
    return new KeyPart() {

        private final List<Expression> extractNodes = Collections.<Expression>singletonList(RoundDateExpression.this);

        @Override
        public PColumn getColumn() {
            return childPart.getColumn();
        }

        @Override
        public List<Expression> getExtractNodes() {
            return extractNodes;
        }

        @Override
        public KeyRange getKeyRange(CompareOp op, Expression rhs) {
            PDataType type = getColumn().getDataType();
            ImmutableBytesWritable ptr = new ImmutableBytesWritable();
            rhs.evaluate(null, ptr);
            byte[] key = ByteUtil.copyKeyBytesIfNecessary(ptr);
            // No need to take into account SortOrder, because ROUND
            // always forces the value to be in ascending order
            PDataCodec codec = getKeyRangeCodec(type);
            int offset = ByteUtil.isInclusive(op) ? 1 : 0;
            long value = codec.decodeLong(key, 0, SortOrder.getDefault());
            byte[] nextKey = new byte[type.getByteSize()];
            switch(op) {
                case EQUAL:
                    // boundary.
                    if (value % divBy != 0) {
                        return KeyRange.EMPTY_RANGE;
                    }
                    codec.encodeLong(value + divBy, nextKey, 0);
                    return type.getKeyRange(key, true, nextKey, false);
                case GREATER:
                case GREATER_OR_EQUAL:
                    codec.encodeLong((value + divBy - offset) / divBy * divBy, nextKey, 0);
                    return type.getKeyRange(nextKey, true, KeyRange.UNBOUND, false);
                case LESS:
                case LESS_OR_EQUAL:
                    codec.encodeLong((value + divBy - (1 - offset)) / divBy * divBy, nextKey, 0);
                    return type.getKeyRange(KeyRange.UNBOUND, false, nextKey, false);
                default:
                    return childPart.getKeyRange(op, rhs);
            }
        }

        @Override
        public PTable getTable() {
            return childPart.getTable();
        }
    };
}
Also used : ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) PDataCodec(org.apache.phoenix.schema.types.PDataType.PDataCodec) PDataType(org.apache.phoenix.schema.types.PDataType) Expression(org.apache.phoenix.expression.Expression) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) KeyPart(org.apache.phoenix.compile.KeyPart) List(java.util.List) CompareOp(org.apache.hadoop.hbase.filter.CompareFilter.CompareOp)

Aggregations

List (java.util.List)1 CompareOp (org.apache.hadoop.hbase.filter.CompareFilter.CompareOp)1 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)1 KeyPart (org.apache.phoenix.compile.KeyPart)1 Expression (org.apache.phoenix.expression.Expression)1 LiteralExpression (org.apache.phoenix.expression.LiteralExpression)1 PDataType (org.apache.phoenix.schema.types.PDataType)1 PDataCodec (org.apache.phoenix.schema.types.PDataType.PDataCodec)1