Search in sources :

Example 21 with Expression

use of org.apache.phoenix.expression.Expression 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)

Example 22 with Expression

use of org.apache.phoenix.expression.Expression in project phoenix by apache.

the class RoundDecimalExpression method create.

/**
     * Creates a {@link RoundDecimalExpression} with rounding scale given by @param scale.
     *
     */
public static Expression create(Expression expr, int scale) throws SQLException {
    if (expr.getDataType().isCoercibleTo(PLong.INSTANCE)) {
        return expr;
    }
    Expression scaleExpr = LiteralExpression.newConstant(scale, PInteger.INSTANCE, Determinism.ALWAYS);
    List<Expression> expressions = Lists.newArrayList(expr, scaleExpr);
    return new RoundDecimalExpression(expressions);
}
Also used : Expression(org.apache.phoenix.expression.Expression) LiteralExpression(org.apache.phoenix.expression.LiteralExpression)

Example 23 with Expression

use of org.apache.phoenix.expression.Expression in project phoenix by apache.

the class RegexpSplitFunction method init.

private void init() {
    ImmutableBytesWritable ptr = new ImmutableBytesWritable();
    Expression e = getPatternStrExpression();
    if (e.isStateless() && e.getDeterminism() == Determinism.ALWAYS && e.evaluate(null, ptr)) {
        String pattern = (String) TYPE.toObject(ptr, TYPE, e.getSortOrder());
        if (pattern != null) {
            initializedSplitter = compilePatternSpec(pattern);
        }
    }
}
Also used : ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) Expression(org.apache.phoenix.expression.Expression)

Example 24 with Expression

use of org.apache.phoenix.expression.Expression in project phoenix by apache.

the class RegexpSubstrFunction method init.

private void init() {
    ImmutableBytesWritable ptr = new ImmutableBytesWritable();
    Expression patternExpr = getPatternExpression();
    if (patternExpr.isStateless() && patternExpr.getDeterminism() == Determinism.ALWAYS && patternExpr.evaluate(null, ptr)) {
        String patternStr = (String) patternExpr.getDataType().toObject(ptr, patternExpr.getSortOrder());
        if (patternStr != null) {
            pattern = compilePatternSpec(patternStr);
        }
    }
    // If the source string has a fixed width, then the max length would be the length 
    // of the source string minus the offset, or the absolute value of the offset if 
    // it's negative. Offset number is a required argument. However, if the source string
    // is not fixed width, the maxLength would be null.
    Expression offsetExpr = getOffsetExpression();
    if (offsetExpr.isStateless() && offsetExpr.getDeterminism() == Determinism.ALWAYS && offsetExpr.evaluate(null, ptr)) {
        offset = (Integer) PInteger.INSTANCE.toObject(ptr, offsetExpr.getDataType(), offsetExpr.getSortOrder());
        if (offset != null) {
            PDataType type = getSourceStrExpression().getDataType();
            if (type.isFixedWidth()) {
                if (offset >= 0) {
                    Integer maxLength = getSourceStrExpression().getMaxLength();
                    this.maxLength = maxLength - offset - (offset == 0 ? 0 : 1);
                } else {
                    this.maxLength = -offset;
                }
            }
        }
    }
}
Also used : PInteger(org.apache.phoenix.schema.types.PInteger) ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) PDataType(org.apache.phoenix.schema.types.PDataType) Expression(org.apache.phoenix.expression.Expression)

Example 25 with Expression

use of org.apache.phoenix.expression.Expression in project phoenix by apache.

the class SubstrFunction method evaluate.

@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    Expression offsetExpression = getOffsetExpression();
    if (!offsetExpression.evaluate(tuple, ptr)) {
        return false;
    }
    int offset = offsetExpression.getDataType().getCodec().decodeInt(ptr, offsetExpression.getSortOrder());
    int length = -1;
    if (hasLengthExpression) {
        Expression lengthExpression = getLengthExpression();
        if (!lengthExpression.evaluate(tuple, ptr)) {
            return false;
        }
        length = lengthExpression.getDataType().getCodec().decodeInt(ptr, lengthExpression.getSortOrder());
        if (length <= 0) {
            return false;
        }
    }
    if (!getStrExpression().evaluate(tuple, ptr)) {
        return false;
    }
    boolean isCharType = getStrExpression().getDataType() == PChar.INSTANCE;
    SortOrder sortOrder = getStrExpression().getSortOrder();
    int strlen = isCharType ? ptr.getLength() : StringUtil.calculateUTF8Length(ptr.get(), ptr.getOffset(), ptr.getLength(), sortOrder);
    // Account for 1 versus 0-based offset
    offset = offset - (offset <= 0 ? 0 : 1);
    if (offset < 0) {
        // Offset < 0 means get from end
        offset = strlen + offset;
    }
    if (offset < 0 || offset >= strlen) {
        return false;
    }
    int maxLength = strlen - offset;
    length = length == -1 ? maxLength : Math.min(length, maxLength);
    int byteOffset = isCharType ? offset : StringUtil.getByteLengthForUtf8SubStr(ptr.get(), ptr.getOffset(), offset, sortOrder);
    int byteLength = isCharType ? length : StringUtil.getByteLengthForUtf8SubStr(ptr.get(), ptr.getOffset() + byteOffset, length, sortOrder);
    ptr.set(ptr.get(), ptr.getOffset() + byteOffset, byteLength);
    return true;
}
Also used : LiteralExpression(org.apache.phoenix.expression.LiteralExpression) Expression(org.apache.phoenix.expression.Expression) SortOrder(org.apache.phoenix.schema.SortOrder)

Aggregations

Expression (org.apache.phoenix.expression.Expression)182 LiteralExpression (org.apache.phoenix.expression.LiteralExpression)101 PDataType (org.apache.phoenix.schema.types.PDataType)54 RowKeyColumnExpression (org.apache.phoenix.expression.RowKeyColumnExpression)39 CoerceExpression (org.apache.phoenix.expression.CoerceExpression)37 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)33 KeyValueColumnExpression (org.apache.phoenix.expression.KeyValueColumnExpression)30 PTable (org.apache.phoenix.schema.PTable)25 ParseNode (org.apache.phoenix.parse.ParseNode)23 RowValueConstructorExpression (org.apache.phoenix.expression.RowValueConstructorExpression)22 Test (org.junit.Test)22 ComparisonExpression (org.apache.phoenix.expression.ComparisonExpression)21 AndExpression (org.apache.phoenix.expression.AndExpression)20 SingleCellColumnExpression (org.apache.phoenix.expression.SingleCellColumnExpression)20 PColumn (org.apache.phoenix.schema.PColumn)20 ArrayList (java.util.ArrayList)19 InListExpression (org.apache.phoenix.expression.InListExpression)17 IsNullExpression (org.apache.phoenix.expression.IsNullExpression)16 OrExpression (org.apache.phoenix.expression.OrExpression)16 LikeExpression (org.apache.phoenix.expression.LikeExpression)15