Search in sources :

Example 1 with SortOrder

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

the class TimestampSubtractExpression method evaluate.

@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    BigDecimal finalResult = BigDecimal.ZERO;
    for (int i = 0; i < children.size(); i++) {
        if (!children.get(i).evaluate(tuple, ptr)) {
            return false;
        }
        if (ptr.getLength() == 0) {
            return true;
        }
        BigDecimal value;
        PDataType type = children.get(i).getDataType();
        SortOrder sortOrder = children.get(i).getSortOrder();
        if (type == PTimestamp.INSTANCE || type == PUnsignedTimestamp.INSTANCE) {
            value = (BigDecimal) (PDecimal.INSTANCE.toObject(ptr, type, sortOrder));
        } else if (type.isCoercibleTo(PDecimal.INSTANCE)) {
            value = (((BigDecimal) PDecimal.INSTANCE.toObject(ptr, type, sortOrder)).multiply(BD_MILLIS_IN_DAY)).setScale(6, RoundingMode.HALF_UP);
        } else if (type.isCoercibleTo(PDouble.INSTANCE)) {
            value = ((BigDecimal.valueOf(type.getCodec().decodeDouble(ptr, sortOrder))).multiply(BD_MILLIS_IN_DAY)).setScale(6, RoundingMode.HALF_UP);
        } else {
            value = BigDecimal.valueOf(type.getCodec().decodeLong(ptr, sortOrder));
        }
        if (i == 0) {
            finalResult = value;
        } else {
            finalResult = finalResult.subtract(value);
        }
    }
    Timestamp ts = DateUtil.getTimestamp(finalResult);
    byte[] resultPtr = new byte[getDataType().getByteSize()];
    PTimestamp.INSTANCE.toBytes(ts, resultPtr, 0);
    ptr.set(resultPtr);
    return true;
}
Also used : PDataType(org.apache.phoenix.schema.types.PDataType) SortOrder(org.apache.phoenix.schema.SortOrder) Timestamp(java.sql.Timestamp) PUnsignedTimestamp(org.apache.phoenix.schema.types.PUnsignedTimestamp) PTimestamp(org.apache.phoenix.schema.types.PTimestamp) BigDecimal(java.math.BigDecimal)

Example 2 with SortOrder

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

the class LikeExpression method evaluate.

@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    AbstractBasePattern pattern = this.pattern;
    if (pattern == null) {
        // TODO: don't allow? this is going to be slooowwww
        if (!getPatternExpression().evaluate(tuple, ptr)) {
            if (logger.isTraceEnabled()) {
                logger.trace("LIKE is FALSE: pattern is null");
            }
            return false;
        }
        String value = (String) PVarchar.INSTANCE.toObject(ptr, getPatternExpression().getSortOrder());
        pattern = compilePattern(value);
        if (logger.isTraceEnabled()) {
            logger.trace("LIKE pattern is expression: " + pattern.pattern());
        }
    }
    Expression strExpression = getStrExpression();
    SortOrder strSortOrder = strExpression.getSortOrder();
    PVarchar strDataType = PVarchar.INSTANCE;
    if (!strExpression.evaluate(tuple, ptr)) {
        if (logger.isTraceEnabled()) {
            logger.trace("LIKE is FALSE: child expression is null");
        }
        return false;
    }
    String value = null;
    if (logger.isTraceEnabled()) {
        value = (String) strDataType.toObject(ptr, strSortOrder);
    }
    strDataType.coerceBytes(ptr, strDataType, strSortOrder, SortOrder.ASC);
    pattern.matches(ptr);
    if (logger.isTraceEnabled()) {
        boolean matched = ((Boolean) PBoolean.INSTANCE.toObject(ptr)).booleanValue();
        logger.trace("LIKE(value='" + value + "'pattern='" + pattern.pattern() + "' is " + matched);
    }
    return true;
}
Also used : AbstractBasePattern(org.apache.phoenix.expression.util.regex.AbstractBasePattern) SortOrder(org.apache.phoenix.schema.SortOrder) PVarchar(org.apache.phoenix.schema.types.PVarchar) PBoolean(org.apache.phoenix.schema.types.PBoolean)

Example 3 with SortOrder

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

the class ReverseFunction method evaluate.

@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    Expression arg = getChildren().get(0);
    if (!arg.evaluate(tuple, ptr)) {
        return false;
    }
    int targetOffset = ptr.getLength();
    if (targetOffset == 0) {
        return true;
    }
    byte[] source = ptr.get();
    byte[] target = new byte[targetOffset];
    int sourceOffset = ptr.getOffset();
    int endOffset = sourceOffset + ptr.getLength();
    SortOrder sortOrder = arg.getSortOrder();
    while (sourceOffset < endOffset) {
        int nBytes = StringUtil.getBytesInChar(source[sourceOffset], sortOrder);
        targetOffset -= nBytes;
        System.arraycopy(source, sourceOffset, target, targetOffset, nBytes);
        sourceOffset += nBytes;
    }
    ptr.set(target);
    return true;
}
Also used : Expression(org.apache.phoenix.expression.Expression) SortOrder(org.apache.phoenix.schema.SortOrder)

Example 4 with SortOrder

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

Example 5 with SortOrder

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

the class RTrimFunction method newKeyPart.

@Override
public KeyPart newKeyPart(final KeyPart childPart) {
    return new KeyPart() {

        @Override
        public KeyRange getKeyRange(CompareOp op, Expression rhs) {
            byte[] lowerRange = KeyRange.UNBOUND;
            byte[] upperRange = KeyRange.UNBOUND;
            boolean lowerInclusive = true;
            boolean upperInclusive = false;
            PDataType type = getColumn().getDataType();
            SortOrder sortOrder = getColumn().getSortOrder();
            switch(op) {
                case LESS_OR_EQUAL:
                    lowerInclusive = false;
                case EQUAL:
                    upperRange = evaluateExpression(rhs);
                    if (op == CompareOp.EQUAL) {
                        lowerRange = upperRange;
                    }
                    if (sortOrder == SortOrder.ASC || !getTable().rowKeyOrderOptimizable()) {
                        upperRange = Arrays.copyOf(upperRange, upperRange.length + 1);
                        upperRange[upperRange.length - 1] = StringUtil.SPACE_UTF8;
                        ByteUtil.nextKey(upperRange, upperRange.length);
                    } else {
                        upperInclusive = true;
                        if (op == CompareOp.LESS_OR_EQUAL) {
                            // will be the RHS value.
                            break;
                        }
                        /*
                         * Somewhat tricky to get the range correct for the DESC equality case.
                         * The lower range is the RHS value followed by any number of inverted spaces.
                         * We need to add a zero byte as the lower range will have an \xFF byte
                         * appended to it and otherwise we'd skip past any rows where there is more
                         * than one space following the RHS.
                         * The upper range should span up to and including the RHS value. We need
                         * to add our own \xFF as otherwise this will look like a degenerate query
                         * since the lower would be bigger than the upper range.
                         */
                        lowerRange = Arrays.copyOf(lowerRange, lowerRange.length + 2);
                        lowerRange[lowerRange.length - 2] = StringUtil.INVERTED_SPACE_UTF8;
                        lowerRange[lowerRange.length - 1] = QueryConstants.SEPARATOR_BYTE;
                        upperRange = Arrays.copyOf(upperRange, upperRange.length + 1);
                        upperRange[upperRange.length - 1] = QueryConstants.DESC_SEPARATOR_BYTE;
                    }
                    break;
                default:
                    // TOOD: Is this ok for DESC?
                    return childPart.getKeyRange(op, rhs);
            }
            Integer length = getColumn().getMaxLength();
            if (type.isFixedWidth() && length != null) {
                // *after* rows with no padding.
                if (lowerRange != KeyRange.UNBOUND) {
                    lowerRange = type.pad(lowerRange, length, SortOrder.ASC);
                }
                if (upperRange != KeyRange.UNBOUND) {
                    upperRange = type.pad(upperRange, length, SortOrder.ASC);
                }
            }
            return KeyRange.getKeyRange(lowerRange, lowerInclusive, upperRange, upperInclusive);
        }

        @Override
        public List<Expression> getExtractNodes() {
            // non blank characters such as 'foo  bar' where the RHS constant is 'foo'.
            return Collections.<Expression>emptyList();
        }

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

        @Override
        public PTable getTable() {
            return childPart.getTable();
        }
    };
}
Also used : PDataType(org.apache.phoenix.schema.types.PDataType) Expression(org.apache.phoenix.expression.Expression) KeyPart(org.apache.phoenix.compile.KeyPart) SortOrder(org.apache.phoenix.schema.SortOrder) CompareOp(org.apache.hadoop.hbase.filter.CompareFilter.CompareOp)

Aggregations

SortOrder (org.apache.phoenix.schema.SortOrder)43 PDataType (org.apache.phoenix.schema.types.PDataType)29 BigDecimal (java.math.BigDecimal)8 Expression (org.apache.phoenix.expression.Expression)8 PDatum (org.apache.phoenix.schema.PDatum)7 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)6 LiteralExpression (org.apache.phoenix.expression.LiteralExpression)5 Timestamp (java.sql.Timestamp)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 DataExceedsCapacityException (org.apache.phoenix.exception.DataExceedsCapacityException)4 CoerceExpression (org.apache.phoenix.expression.CoerceExpression)4 KeyValueColumnExpression (org.apache.phoenix.expression.KeyValueColumnExpression)4 Field (org.apache.phoenix.schema.ValueSchema.Field)4 PTimestamp (org.apache.phoenix.schema.types.PTimestamp)4 PUnsignedTimestamp (org.apache.phoenix.schema.types.PUnsignedTimestamp)4 IOException (java.io.IOException)3 SingleCellColumnExpression (org.apache.phoenix.expression.SingleCellColumnExpression)3 SingleCellConstructorExpression (org.apache.phoenix.expression.SingleCellConstructorExpression)3 RowKeySchemaBuilder (org.apache.phoenix.schema.RowKeySchema.RowKeySchemaBuilder)3