use of org.apache.phoenix.schema.types.PDataType in project phoenix by apache.
the class DateScalarFunction method init.
private void init() {
PDataType returnType = getChildren().get(0).getDataType();
inputCodec = DateUtil.getCodecFor(returnType);
}
use of org.apache.phoenix.schema.types.PDataType in project phoenix by apache.
the class DayOfMonthFunction method evaluate.
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
Expression expression = getChildExpression();
if (!expression.evaluate(tuple, ptr)) {
return false;
}
if (ptr.getLength() == 0) {
//means null
return true;
}
long dateTime = inputCodec.decodeLong(ptr, expression.getSortOrder());
DateTime dt = new DateTime(dateTime);
int day = dt.getDayOfMonth();
PDataType returnType = getDataType();
byte[] byteValue = new byte[returnType.getByteSize()];
returnType.getCodec().encodeInt(day, byteValue, 0);
ptr.set(byteValue);
return true;
}
use of org.apache.phoenix.schema.types.PDataType in project phoenix by apache.
the class FloorDateExpression method evaluate.
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
if (children.get(0).evaluate(tuple, ptr)) {
PDataType dataType = getDataType();
long time = dataType.getCodec().decodeLong(ptr, children.get(0).getSortOrder());
long value = roundTime(time);
Date d = new Date(value);
byte[] byteValue = dataType.toBytes(d);
ptr.set(byteValue);
return true;
}
return false;
}
use of org.apache.phoenix.schema.types.PDataType in project phoenix by apache.
the class FloorDateExpression method create.
public static Expression create(List<Expression> children) throws SQLException {
Expression firstChild = children.get(0);
PDataType firstChildDataType = firstChild.getDataType();
if (firstChildDataType == PTimestamp.INSTANCE || firstChildDataType == PUnsignedTimestamp.INSTANCE) {
// Coerce TIMESTAMP to DATE, as the nanos has no affect
List<Expression> newChildren = Lists.newArrayListWithExpectedSize(children.size());
newChildren.add(CoerceExpression.create(firstChild, firstChildDataType == PTimestamp.INSTANCE ? PDate.INSTANCE : PUnsignedDate.INSTANCE));
newChildren.addAll(children.subList(1, children.size()));
children = newChildren;
}
Object timeUnitValue = ((LiteralExpression) children.get(1)).getValue();
TimeUnit timeUnit = TimeUnit.getTimeUnit(timeUnitValue != null ? timeUnitValue.toString() : null);
switch(timeUnit) {
case WEEK:
return new FloorWeekExpression(children);
case MONTH:
return new FloorMonthExpression(children);
case YEAR:
return new FloorYearExpression(children);
default:
return new FloorDateExpression(children);
}
}
use of org.apache.phoenix.schema.types.PDataType in project phoenix by apache.
the class DecodeFunction method evaluate.
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
Expression expression = getExpression();
if (!expression.evaluate(tuple, ptr)) {
return false;
}
if (ptr.getLength() == 0) {
// expression was evaluated, but evaluated to null
return true;
}
PDataType type = expression.getDataType();
String stringToDecode = (String) type.toObject(ptr);
Expression encodingExpression = getEncodingExpression();
if (!encodingExpression.evaluate(tuple, ptr)) {
return false;
}
if (ptr.getLength() == 0) {
throw new IllegalDataException(new SQLExceptionInfo.Builder(SQLExceptionCode.ILLEGAL_DATA).setMessage("Missing bytes encoding").build().buildException());
}
type = encodingExpression.getDataType();
String encoding = ((String) type.toObject(ptr)).toUpperCase();
byte[] out;
EncodeFormat format = EncodeFormat.valueOf(encoding);
switch(format) {
case HEX:
out = decodeHex(stringToDecode);
break;
default:
throw new IllegalDataException("Unsupported encoding \"" + encoding + "\"");
}
ptr.set(out);
return true;
}
Aggregations