use of org.apache.phoenix.schema.SortOrder in project phoenix by apache.
the class IndexTestUtil method coerceDataValueToIndexValue.
private static void coerceDataValueToIndexValue(PColumn dataColumn, PColumn indexColumn, ImmutableBytesWritable ptr) {
PDataType dataType = dataColumn.getDataType();
// TODO: push to RowKeySchema?
SortOrder dataModifier = dataColumn.getSortOrder();
PDataType indexType = indexColumn.getDataType();
SortOrder indexModifier = indexColumn.getSortOrder();
// We know ordinal position will match pk position, because you cannot
// alter an index table.
indexType.coerceBytes(ptr, dataType, dataModifier, indexModifier);
}
use of org.apache.phoenix.schema.SortOrder in project phoenix by apache.
the class ComparisonExpression method evaluate.
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
if (!children.get(0).evaluate(tuple, ptr)) {
return false;
}
if (ptr.getLength() == 0) {
// null comparison evals to null
return true;
}
byte[] lhsBytes = ptr.get();
int lhsOffset = ptr.getOffset();
int lhsLength = ptr.getLength();
PDataType lhsDataType = children.get(0).getDataType();
SortOrder lhsSortOrder = children.get(0).getSortOrder();
if (!children.get(1).evaluate(tuple, ptr)) {
return false;
}
if (ptr.getLength() == 0) {
// null comparison evals to null
return true;
}
byte[] rhsBytes = ptr.get();
int rhsOffset = ptr.getOffset();
int rhsLength = ptr.getLength();
PDataType rhsDataType = children.get(1).getDataType();
SortOrder rhsSortOrder = children.get(1).getSortOrder();
if (rhsDataType == PChar.INSTANCE) {
rhsLength = StringUtil.getUnpaddedCharLength(rhsBytes, rhsOffset, rhsLength, rhsSortOrder);
}
if (lhsDataType == PChar.INSTANCE) {
lhsLength = StringUtil.getUnpaddedCharLength(lhsBytes, lhsOffset, lhsLength, lhsSortOrder);
}
int comparisonResult = lhsDataType.compareTo(lhsBytes, lhsOffset, lhsLength, lhsSortOrder, rhsBytes, rhsOffset, rhsLength, rhsSortOrder, rhsDataType);
ptr.set(ByteUtil.compare(op, comparisonResult) ? PDataType.TRUE_BYTES : PDataType.FALSE_BYTES);
return true;
}
use of org.apache.phoenix.schema.SortOrder in project phoenix by apache.
the class DecimalAddExpression 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.add(bd);
}
}
if (maxLength != null || scale != null) {
result = NumberUtil.setDecimalWidthAndScale(result, maxLength, scale);
}
if (result == null) {
throw new DataExceedsCapacityException(PDecimal.INSTANCE, maxLength, scale);
}
ptr.set(PDecimal.INSTANCE.toBytes(result));
return true;
}
use of org.apache.phoenix.schema.SortOrder in project phoenix by apache.
the class DecimalMultiplyExpression 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 = children.get(i).getDataType();
SortOrder childSortOrder = children.get(i).getSortOrder();
BigDecimal bd = (BigDecimal) PDecimal.INSTANCE.toObject(ptr, childType, childSortOrder);
if (result == null) {
result = bd;
} else {
result = result.multiply(bd);
}
}
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;
}
use of org.apache.phoenix.schema.SortOrder in project phoenix by apache.
the class DecimalSubtractExpression 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();
boolean isDate = childType.isCoercibleTo(PDate.INSTANCE);
SortOrder childSortOrder = childExpr.getSortOrder();
BigDecimal bd = isDate ? BigDecimal.valueOf(childType.getCodec().decodeLong(ptr, childSortOrder)) : (BigDecimal) PDecimal.INSTANCE.toObject(ptr, childType, childSortOrder);
if (result == null) {
result = bd;
} else {
result = result.subtract(bd);
/*
* Special case for date subtraction - note that only first two expression may be dates.
* We need to convert the date to a unit of "days" because that's what sql expects.
*/
if (isDate) {
result = result.divide(BD_MILLIS_IN_DAY, PDataType.DEFAULT_MATH_CONTEXT);
}
}
}
if (maxLength != null || scale != null) {
result = NumberUtil.setDecimalWidthAndScale(result, maxLength, scale);
}
if (result == null) {
throw new DataExceedsCapacityException(PDecimal.INSTANCE, maxLength, scale);
}
ptr.set(PDecimal.INSTANCE.toBytes(result));
return true;
}
Aggregations