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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations