Search in sources :

Example 21 with SortOrder

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

the class RoundTimestampExpression method evaluate.

@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    if (children.get(0).evaluate(tuple, ptr)) {
        SortOrder sortOrder = children.get(0).getSortOrder();
        PDataType dataType = getDataType();
        int nanos = dataType.getNanos(ptr, sortOrder);
        if (nanos >= HALF_OF_NANOS_IN_MILLI) {
            long timeMillis = dataType.getMillis(ptr, sortOrder);
            Timestamp roundedTs = new Timestamp(timeMillis + 1);
            byte[] byteValue = dataType.toBytes(roundedTs);
            ptr.set(byteValue);
        }
        // for timestamp we only support rounding up the milliseconds. 
        return true;
    }
    return false;
}
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)

Example 22 with SortOrder

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

the class LpadFunction method evaluate.

/**
     * Left pads a string with with the given fill expression.
     */
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    Expression outputStrLenExpr = getOutputStrLenExpr();
    if (!outputStrLenExpr.evaluate(tuple, ptr)) {
        return false;
    }
    int outputStrLen = outputStrLenExpr.getDataType().getCodec().decodeInt(ptr, outputStrLenExpr.getSortOrder());
    if (outputStrLen < 0) {
        return false;
    }
    Expression strExp = getStrExpr();
    if (!strExp.evaluate(tuple, ptr)) {
        return false;
    }
    boolean isStrCharType = getStrExpr().getDataType() == PChar.INSTANCE;
    boolean isFillCharType = getFillExpr().getDataType() == PChar.INSTANCE;
    SortOrder strSortOrder = getStrExpr().getSortOrder();
    SortOrder fillSortOrder = getFillExpr().getSortOrder();
    int inputStrLen = getUTF8Length(ptr, strSortOrder, isStrCharType);
    if (outputStrLen == inputStrLen) {
        // nothing to do
        return true;
    }
    if (outputStrLen < inputStrLen) {
        // truncate the string from the right
        int subStrByteLength = getSubstringByteLength(ptr, outputStrLen, strSortOrder, isStrCharType);
        ptr.set(ptr.get(), ptr.getOffset(), subStrByteLength);
        return true;
    }
    // left pad the input string with the fill chars
    Expression fillExpr = getFillExpr();
    ImmutableBytesWritable fillPtr = new ImmutableBytesWritable();
    if (!fillExpr.evaluate(tuple, fillPtr)) {
        return false;
    }
    int fillExprLen = fillPtr.getLength();
    if (fillExprLen < 1) {
        // return if fill is empty
        return false;
    }
    // if the padding to be added is not a multiple of the length of the
    // fill string then we need to use part of the fill string to pad
    // LPAD(ab, 5, xy) = xyxab
    // padLen = 3
    // numFillsPrepended = 1
    // numFillCharsPrepended = 1
    // length of the fill string
    int fillLen = getUTF8Length(fillPtr, fillSortOrder, isFillCharType);
    // length of padding to be added
    int padLen = outputStrLen - inputStrLen;
    // number of fill strings to be prepended
    int numFillsPrepended = padLen / fillLen;
    // number of chars from fill string to be prepended
    int numFillCharsPrepended = padLen % fillLen;
    // byte length of the input string
    int strByteLength = ptr.getLength();
    // byte length of the fill string
    int fillByteLength = getSubstringByteLength(fillPtr, fillPtr.getLength(), fillSortOrder, isFillCharType);
    // byte length of the full fills to be prepended
    int fullFillsByteLength = numFillsPrepended * fillByteLength;
    // byte length of the chars of fill string to be prepended
    int fillCharsByteLength = getSubstringByteLength(fillPtr, numFillCharsPrepended, fillSortOrder, isFillCharType);
    // byte length of the padded string =
    int strWithPaddingByteLength = fullFillsByteLength + fillCharsByteLength + strByteLength;
    // need to invert the fill string if the sort order of fill and
    // input are different
    boolean invertFill = fillSortOrder != strSortOrder;
    byte[] paddedStr = StringUtil.lpad(ptr.get(), ptr.getOffset(), ptr.getLength(), fillPtr.get(), fillPtr.getOffset(), fillPtr.getLength(), invertFill, strWithPaddingByteLength);
    ptr.set(paddedStr);
    return true;
}
Also used : ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) Expression(org.apache.phoenix.expression.Expression) SortOrder(org.apache.phoenix.schema.SortOrder)

Example 23 with SortOrder

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

the class RTrimFunction method evaluate.

@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    // that is below SPACE_UTF8 (space and control characters) or above (control chars).
    if (!getStringExpression().evaluate(tuple, ptr)) {
        return false;
    }
    if (ptr.getLength() == 0) {
        ptr.set(ByteUtil.EMPTY_BYTE_ARRAY);
        return true;
    }
    byte[] string = ptr.get();
    int offset = ptr.getOffset();
    int length = ptr.getLength();
    SortOrder sortOrder = getStringExpression().getSortOrder();
    int i = StringUtil.getFirstNonBlankCharIdxFromEnd(string, offset, length, sortOrder);
    if (i == offset - 1) {
        ptr.set(ByteUtil.EMPTY_BYTE_ARRAY);
        return true;
    }
    ptr.set(string, offset, i - offset + 1);
    return true;
}
Also used : SortOrder(org.apache.phoenix.schema.SortOrder)

Example 24 with SortOrder

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

the class CeilTimestampExpression method evaluate.

@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    if (children.get(0).evaluate(tuple, ptr)) {
        if (ptr.getLength() == 0) {
            // child evaluated to null
            return true;
        }
        SortOrder sortOrder = children.get(0).getSortOrder();
        PDataType dataType = getDataType();
        int nanos = dataType.getNanos(ptr, sortOrder);
        if (nanos > 0) {
            long millis = dataType.getMillis(ptr, sortOrder);
            Timestamp roundedTs = new Timestamp(millis + 1);
            byte[] byteValue = dataType.toBytes(roundedTs);
            ptr.set(byteValue);
        }
        // for timestamp we only support rounding up the milliseconds.
        return true;
    }
    return false;
}
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)

Example 25 with SortOrder

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

the class LTrimFunction method evaluate.

@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    // that is below SPACE_UTF8 (space and control characters) or 0x7f (control chars).
    if (!getStringExpression().evaluate(tuple, ptr)) {
        return false;
    }
    if (ptr.getLength() == 0) {
        ptr.set(ByteUtil.EMPTY_BYTE_ARRAY);
        return true;
    }
    byte[] string = ptr.get();
    int offset = ptr.getOffset();
    int length = ptr.getLength();
    SortOrder sortOrder = getStringExpression().getSortOrder();
    int i = StringUtil.getFirstNonBlankCharIdxFromStart(string, offset, length, sortOrder);
    if (i == offset + length) {
        ptr.set(ByteUtil.EMPTY_BYTE_ARRAY);
        return true;
    }
    ptr.set(string, i, offset + length - i);
    return true;
}
Also used : SortOrder(org.apache.phoenix.schema.SortOrder)

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