use of org.apache.phoenix.schema.SortOrder in project phoenix by apache.
the class StringConcatExpression method evaluate.
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
byte[] result = ByteUtil.EMPTY_BYTE_ARRAY;
for (int i = 0; i < children.size(); i++) {
if (children.get(i).getDataType() == null) {
continue;
}
if (!children.get(i).evaluate(tuple, ptr)) {
// If not incremental evaluation, skip null children
if (tuple.isImmutable()) {
continue;
}
// have enough information yet.
return false;
}
PDataType childType = children.get(i).getDataType();
SortOrder sortOrder = children.get(i).getSortOrder();
// additional space here anyway.
if (childType.isCoercibleTo(PVarchar.INSTANCE)) {
result = ByteUtil.concat(result, ByteUtil.concat(sortOrder, ptr));
} else {
result = ByteUtil.concat(result, PVarchar.INSTANCE.toBytes(childType.toObject(ptr, sortOrder).toString()));
}
}
ptr.set(result);
return true;
}
use of org.apache.phoenix.schema.SortOrder in project phoenix by apache.
the class TimestampAddExpression 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(QueryConstants.BD_MILLIS_IN_DAY)).setScale(6, RoundingMode.HALF_UP);
} else if (type.isCoercibleTo(PDouble.INSTANCE)) {
value = ((BigDecimal.valueOf(type.getCodec().decodeDouble(ptr, sortOrder))).multiply(QueryConstants.BD_MILLIS_IN_DAY)).setScale(6, RoundingMode.HALF_UP);
} else {
value = BigDecimal.valueOf(type.getCodec().decodeLong(ptr, sortOrder));
}
finalResult = finalResult.add(value);
}
Timestamp ts = DateUtil.getTimestamp(finalResult);
byte[] resultPtr = new byte[getDataType().getByteSize()];
PTimestamp.INSTANCE.toBytes(ts, resultPtr, 0);
ptr.set(resultPtr);
return true;
}
use of org.apache.phoenix.schema.SortOrder in project phoenix by apache.
the class DateAddExpression method evaluate.
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
long finalResult = 0;
for (int i = 0; i < children.size(); i++) {
if (!children.get(i).evaluate(tuple, ptr)) {
return false;
}
if (ptr.getLength() == 0) {
return true;
}
long value;
PDataType type = children.get(i).getDataType();
SortOrder sortOrder = children.get(i).getSortOrder();
if (type == PDecimal.INSTANCE) {
BigDecimal bd = (BigDecimal) PDecimal.INSTANCE.toObject(ptr, type, sortOrder);
value = bd.multiply(BD_MILLIS_IN_DAY).longValue();
} else if (type.isCoercibleTo(PLong.INSTANCE)) {
value = type.getCodec().decodeLong(ptr, sortOrder) * QueryConstants.MILLIS_IN_DAY;
} else if (type.isCoercibleTo(PDouble.INSTANCE)) {
value = (long) (type.getCodec().decodeDouble(ptr, sortOrder) * QueryConstants.MILLIS_IN_DAY);
} else {
value = type.getCodec().decodeLong(ptr, sortOrder);
}
finalResult += value;
}
byte[] resultPtr = new byte[getDataType().getByteSize()];
getDataType().getCodec().encodeLong(finalResult, resultPtr, 0);
ptr.set(resultPtr);
return true;
}
use of org.apache.phoenix.schema.SortOrder in project phoenix by apache.
the class DateSubtractExpression method evaluate.
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
long finalResult = 0;
for (int i = 0; i < children.size(); i++) {
if (!children.get(i).evaluate(tuple, ptr) || ptr.getLength() == 0) {
return false;
}
long value;
PDataType type = children.get(i).getDataType();
SortOrder sortOrder = children.get(i).getSortOrder();
if (type == PDecimal.INSTANCE) {
BigDecimal bd = (BigDecimal) PDecimal.INSTANCE.toObject(ptr, type, sortOrder);
value = bd.multiply(BD_MILLIS_IN_DAY).longValue();
} else if (type.isCoercibleTo(PLong.INSTANCE)) {
value = type.getCodec().decodeLong(ptr, sortOrder) * QueryConstants.MILLIS_IN_DAY;
} else if (type.isCoercibleTo(PDouble.INSTANCE)) {
value = (long) (type.getCodec().decodeDouble(ptr, sortOrder) * QueryConstants.MILLIS_IN_DAY);
} else {
value = type.getCodec().decodeLong(ptr, sortOrder);
}
if (i == 0) {
finalResult = value;
} else {
finalResult -= value;
}
}
byte[] resultPtr = new byte[getDataType().getByteSize()];
getDataType().getCodec().encodeLong(finalResult, resultPtr, 0);
ptr.set(resultPtr);
return true;
}
use of org.apache.phoenix.schema.SortOrder in project phoenix by apache.
the class DecimalDivideExpression method evaluate.
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
BigDecimal result = null;
for (int i = 0; i < children.size(); i++) {
Expression childExpr = children.get(i);
if (!childExpr.evaluate(tuple, ptr)) {
return false;
}
if (ptr.getLength() == 0) {
return true;
}
PDataType childType = childExpr.getDataType();
SortOrder childSortOrder = childExpr.getSortOrder();
BigDecimal bd = (BigDecimal) PDecimal.INSTANCE.toObject(ptr, childType, childSortOrder);
if (result == null) {
result = bd;
} else {
result = result.divide(bd, PDataType.DEFAULT_MATH_CONTEXT);
}
}
if (getMaxLength() != null || getScale() != null) {
result = NumberUtil.setDecimalWidthAndScale(result, getMaxLength(), getScale());
}
if (result == null) {
throw new DataExceedsCapacityException(PDecimal.INSTANCE, getMaxLength(), getScale());
}
ptr.set(PDecimal.INSTANCE.toBytes(result));
return true;
}
Aggregations