use of org.apache.phoenix.schema.IllegalDataException in project phoenix by apache.
the class PTimestamp method getKeyRange.
/**
* With timestamp, because our last 4 bytes store a value from [0 - 1000000), we need
* to detect when the boundary is crossed if we increment to the nextKey.
*/
@Override
public KeyRange getKeyRange(byte[] lowerRange, boolean lowerInclusive, byte[] upperRange, boolean upperInclusive) {
/*
* Force lower bound to be inclusive for fixed width keys because it makes comparisons less expensive when you
* can count on one bound or the other being inclusive. Comparing two fixed width exclusive bounds against each
* other is inherently more expensive, because you need to take into account if the bigger key is equal to the
* next key after the smaller key. For example: (A-B] compared against [A-B) An exclusive lower bound A is
* bigger than an exclusive upper bound B. Forcing a fixed width exclusive lower bound key to be inclusive
* prevents us from having to do this extra logic in the compare function.
*
*/
if (lowerRange != KeyRange.UNBOUND && !lowerInclusive && isFixedWidth()) {
if (lowerRange.length != MAX_TIMESTAMP_BYTES) {
throw new IllegalDataException("Unexpected size of " + lowerRange.length + " for " + this);
}
// Infer sortOrder based on most significant byte
SortOrder sortOrder = lowerRange[Bytes.SIZEOF_LONG] < 0 ? SortOrder.DESC : SortOrder.ASC;
int nanos = PUnsignedInt.INSTANCE.getCodec().decodeInt(lowerRange, Bytes.SIZEOF_LONG, sortOrder);
if ((sortOrder == SortOrder.DESC && nanos == 0) || (sortOrder == SortOrder.ASC && nanos == MAX_NANOS_VALUE_EXCLUSIVE - 1)) {
// With timestamp, because our last 4 bytes store a value from [0 - 1000000), we need
// to detect when the boundary is crossed with our nextKey
byte[] newLowerRange = new byte[MAX_TIMESTAMP_BYTES];
if (sortOrder == SortOrder.DESC) {
// Set nanos part as inverted 999999 as it needs to be the max nano value
// The millisecond part is moving to the previous value below
System.arraycopy(lowerRange, 0, newLowerRange, 0, Bytes.SIZEOF_LONG);
PUnsignedInt.INSTANCE.getCodec().encodeInt(MAX_NANOS_VALUE_EXCLUSIVE - 1, newLowerRange, Bytes.SIZEOF_LONG);
SortOrder.invert(newLowerRange, Bytes.SIZEOF_LONG, newLowerRange, Bytes.SIZEOF_LONG, Bytes.SIZEOF_INT);
} else {
// Leave nanos part as zero as the millisecond part is rolling over to the next value
System.arraycopy(lowerRange, 0, newLowerRange, 0, Bytes.SIZEOF_LONG);
}
// Increment millisecond part, but leave nanos alone
if (ByteUtil.nextKey(newLowerRange, Bytes.SIZEOF_LONG)) {
lowerRange = newLowerRange;
} else {
lowerRange = KeyRange.UNBOUND;
}
return KeyRange.getKeyRange(lowerRange, true, upperRange, upperInclusive);
}
}
return super.getKeyRange(lowerRange, lowerInclusive, upperRange, upperInclusive);
}
use of org.apache.phoenix.schema.IllegalDataException in project phoenix by apache.
the class LiteralExpression method newConstant.
// TODO: cache?
public static LiteralExpression newConstant(Object value, PDataType type, Integer maxLength, Integer scale, SortOrder sortOrder, Determinism determinism, boolean rowKeyOrderOptimizable) throws SQLException {
if (value == null) {
return (type == null) ? getNullLiteralExpression(determinism) : getTypedNullLiteralExpression(type, determinism);
} else if (value instanceof Boolean) {
return getBooleanLiteralExpression((Boolean) value, determinism);
}
PDataType actualType = PDataType.fromLiteral(value);
try {
value = type.toObject(value, actualType);
} catch (IllegalDataException e) {
throw TypeMismatchException.newException(type, actualType, value.toString());
}
byte[] b = type.isArrayType() ? ((PArrayDataType) type).toBytes(value, PArrayDataType.arrayBaseType(type), sortOrder, rowKeyOrderOptimizable) : type.toBytes(value, sortOrder);
if (type == PVarchar.INSTANCE || type == PChar.INSTANCE) {
if (type == PChar.INSTANCE && maxLength != null && b.length < maxLength) {
if (rowKeyOrderOptimizable) {
b = type.pad(b, maxLength, sortOrder);
} else {
b = StringUtil.padChar(b, maxLength);
}
} else if (value != null) {
maxLength = ((String) value).length();
}
} else if (type.isArrayType()) {
maxLength = ((PhoenixArray) value).getMaxLength();
}
if (b.length == 0) {
return getTypedNullLiteralExpression(type, determinism);
}
if (maxLength == null) {
maxLength = type == null || !type.isFixedWidth() ? null : type.getMaxLength(value);
}
return new LiteralExpression(value, type, b, maxLength, scale, sortOrder, determinism);
}
Aggregations