@Test public void testIllegalUnsignedDateTime() { Date date1 = new Date(-1000); try { PUnsignedDate.INSTANCE.toBytes(date1); fail(); } catch (RuntimeException e) { assertTrue(e.getCause() instanceof SQLException); SQLException sqlE = (SQLException)e.getCause(); assertEquals(SQLExceptionCode.ILLEGAL_DATA.getErrorCode(), sqlE.getErrorCode()); } }
protected static int toBytes(BigDecimal v, byte[] result, final int offset, int length) { int signum = v.signum(); if (signum == 0) { result[offset] = ZERO_BYTE; return 1; } int index = offset + length; int scale = v.scale(); int expOffset = scale % 2 * (scale < 0 ? -1 : 1); int multiplyBy; BigInteger divideBy; if (expOffset == 0) { multiplyBy = 1; divideBy = ONE_HUNDRED; } else { multiplyBy = 10; divideBy = BigInteger.TEN; } int digitOffset; BigInteger compareAgainst; if (signum == 1) { digitOffset = POS_DIGIT_OFFSET; compareAgainst = MAX_LONG; scale -= (length - 2) * 2; result[offset] = (byte)((-(scale + expOffset) / 2 + EXP_BYTE_OFFSET) | 0x80); } else { digitOffset = NEG_DIGIT_OFFSET; compareAgainst = MIN_LONG; scale -= (length - 2 - 1) * 2; result[offset] = (byte)(~(-(scale + expOffset) / 2 + EXP_BYTE_OFFSET + 128) & 0x7F); if (length <= MAX_BIG_DECIMAL_BYTES) { result[--index] = NEG_TERMINAL_BYTE; } else { length = MAX_BIG_DECIMAL_BYTES; index = offset + length; } } BigInteger bi = v.unscaledValue(); while (bi.compareTo(compareAgainst) * signum > 0) { BigInteger[] dandr = bi.divideAndRemainder(divideBy); bi = dandr[0]; int digit = dandr[1].intValue(); result[--index] = (byte)(digit * multiplyBy + digitOffset); multiplyBy = 1; divideBy = ONE_HUNDRED; } long l = bi.longValue(); do { long divBy = 100 / multiplyBy; long digit = l % divBy; l /= divBy; result[--index] = (byte)(digit * multiplyBy + digitOffset); multiplyBy = 1; } while (l != 0); return length; }
PDataType implements DataType<T>, Comparable<PDataType<?>> { protected static int toBytes(BigDecimal v, byte[] result, final int offset, int length) { int signum = v.signum(); if (signum == 0) { result[offset] = ZERO_BYTE; return 1; } int index = offset + length; int scale = v.scale(); int expOffset = scale % 2 * (scale < 0 ? -1 : 1); int multiplyBy; BigInteger divideBy; if (expOffset == 0) { multiplyBy = 1; divideBy = ONE_HUNDRED; } else { multiplyBy = 10; divideBy = BigInteger.TEN; } int digitOffset; BigInteger compareAgainst; if (signum == 1) { digitOffset = POS_DIGIT_OFFSET; compareAgainst = MAX_LONG; scale -= (length - 2) * 2; result[offset] = (byte)((-(scale + expOffset) / 2 + EXP_BYTE_OFFSET) | 0x80); } else { digitOffset = NEG_DIGIT_OFFSET; compareAgainst = MIN_LONG; scale -= (length - 2 - 1) * 2; result[offset] = (byte)(~(-(scale + expOffset) / 2 + EXP_BYTE_OFFSET + 128) & 0x7F); if (length <= MAX_BIG_DECIMAL_BYTES) { result[--index] = NEG_TERMINAL_BYTE; } else { length = MAX_BIG_DECIMAL_BYTES; index = offset + length; } } BigInteger bi = v.unscaledValue(); while (bi.compareTo(compareAgainst) * signum > 0) { BigInteger[] dandr = bi.divideAndRemainder(divideBy); bi = dandr[0]; int digit = dandr[1].intValue(); result[--index] = (byte)(digit * multiplyBy + digitOffset); multiplyBy = 1; divideBy = ONE_HUNDRED; } long l = bi.longValue(); do { long divBy = 100 / multiplyBy; long digit = l % divBy; l /= divBy; result[--index] = (byte)(digit * multiplyBy + digitOffset); multiplyBy = 1; } while (l != 0); return length; } }
PDataType implements DataType<T>, Comparable<PDataType<?>> { protected static int toBytes(BigDecimal v, byte[] result, final int offset, int length) { int signum = v.signum(); if (signum == 0) { result[offset] = ZERO_BYTE; return 1; } int index = offset + length; int scale = v.scale(); int expOffset = scale % 2 * (scale < 0 ? -1 : 1); int multiplyBy; BigInteger divideBy; if (expOffset == 0) { multiplyBy = 1; divideBy = ONE_HUNDRED; } else { multiplyBy = 10; divideBy = BigInteger.TEN; } int digitOffset; BigInteger compareAgainst; if (signum == 1) { digitOffset = POS_DIGIT_OFFSET; compareAgainst = MAX_LONG; scale -= (length - 2) * 2; result[offset] = (byte)((-(scale + expOffset) / 2 + EXP_BYTE_OFFSET) | 0x80); } else { digitOffset = NEG_DIGIT_OFFSET; compareAgainst = MIN_LONG; scale -= (length - 2 - 1) * 2; result[offset] = (byte)(~(-(scale + expOffset) / 2 + EXP_BYTE_OFFSET + 128) & 0x7F); if (length <= MAX_BIG_DECIMAL_BYTES) { result[--index] = NEG_TERMINAL_BYTE; } else { length = MAX_BIG_DECIMAL_BYTES; index = offset + length; } } BigInteger bi = v.unscaledValue(); while (bi.compareTo(compareAgainst) * signum > 0) { BigInteger[] dandr = bi.divideAndRemainder(divideBy); bi = dandr[0]; int digit = dandr[1].intValue(); result[--index] = (byte)(digit * multiplyBy + digitOffset); multiplyBy = 1; divideBy = ONE_HUNDRED; } long l = bi.longValue(); do { long divBy = 100 / multiplyBy; long digit = l % divBy; l /= divBy; result[--index] = (byte)(digit * multiplyBy + digitOffset); multiplyBy = 1; } while (l != 0); return length; } protected  PDataType(String sqlTypeName, int sqlType, Class clazz, PDataCodec codec, int ordinal); }
PDataType implements DataType<T>, Comparable<PDataType<?>> { protected static int toBytes(BigDecimal v, byte[] result, final int offset, int length) { int signum = v.signum(); if (signum == 0) { result[offset] = ZERO_BYTE; return 1; } int index = offset + length; int scale = v.scale(); int expOffset = scale % 2 * (scale < 0 ? -1 : 1); int multiplyBy; BigInteger divideBy; if (expOffset == 0) { multiplyBy = 1; divideBy = ONE_HUNDRED; } else { multiplyBy = 10; divideBy = BigInteger.TEN; } int digitOffset; BigInteger compareAgainst; if (signum == 1) { digitOffset = POS_DIGIT_OFFSET; compareAgainst = MAX_LONG; scale -= (length - 2) * 2; result[offset] = (byte)((-(scale + expOffset) / 2 + EXP_BYTE_OFFSET) | 0x80); } else { digitOffset = NEG_DIGIT_OFFSET; compareAgainst = MIN_LONG; scale -= (length - 2 - 1) * 2; result[offset] = (byte)(~(-(scale + expOffset) / 2 + EXP_BYTE_OFFSET + 128) & 0x7F); if (length <= MAX_BIG_DECIMAL_BYTES) { result[--index] = NEG_TERMINAL_BYTE; } else { length = MAX_BIG_DECIMAL_BYTES; index = offset + length; } } BigInteger bi = v.unscaledValue(); while (bi.compareTo(compareAgainst) * signum > 0) { BigInteger[] dandr = bi.divideAndRemainder(divideBy); bi = dandr[0]; int digit = dandr[1].intValue(); result[--index] = (byte)(digit * multiplyBy + digitOffset); multiplyBy = 1; divideBy = ONE_HUNDRED; } long l = bi.longValue(); do { long divBy = 100 / multiplyBy; long digit = l % divBy; l /= divBy; result[--index] = (byte)(digit * multiplyBy + digitOffset); multiplyBy = 1; } while (l != 0); return length; } protected  PDataType(String sqlTypeName, int sqlType, Class clazz, PDataCodec codec, int ordinal); static PDataType[] values(); int ordinal(); @SuppressWarnings("unchecked") @Override Class<T> encodedClass(); boolean isCastableTo(PDataType targetType); final PDataCodec getCodec(); boolean isBytesComparableWith(PDataType otherType); int estimateByteSize(Object o); Integer getMaxLength(Object o); Integer getScale(Object o); Integer estimateByteSizeFromLength(Integer length); final String getSqlTypeName(); final int getSqlType(); final Class getJavaClass(); boolean isArrayType(); final int compareTo(byte[] lhs, int lhsOffset, int lhsLength, SortOrder lhsSortOrder, byte[] rhs,
            int rhsOffset, int rhsLength, SortOrder rhsSortOrder, PDataType rhsType); static boolean isDoubleOrFloat(PDataType type); @Override boolean equals(Object o); static boolean equalsAny(PDataType lhs, PDataType... rhs); boolean isCoercibleTo(PDataType targetType); boolean isComparableTo(PDataType targetType); boolean isCoercibleTo(PDataType targetType, Object value); boolean isSizeCompatible(ImmutableBytesWritable ptr, Object value, PDataType srcType, SortOrder sortOrder,
            Integer maxLength, Integer scale, Integer desiredMaxLength, Integer desiredScale); int compareTo(byte[] b1, byte[] b2); final int compareTo(ImmutableBytesWritable ptr1, ImmutableBytesWritable ptr2); final int compareTo(byte[] ba1, int offset1, int length1, SortOrder so1, byte[] ba2, int offset2,
            int length2, SortOrder so2); final int compareTo(ImmutableBytesWritable ptr1, SortOrder ptr1SortOrder, ImmutableBytesWritable ptr2,
            SortOrder ptr2SortOrder, PDataType type2); int compareTo(Object lhs, Object rhs); final boolean isNull(byte[] value); byte[] toBytes(Object object, SortOrder sortOrder); void coerceBytes(ImmutableBytesWritable ptr, Object o, PDataType actualType, Integer actualMaxLength,
            Integer actualScale, SortOrder actualModifier, Integer desiredMaxLength, Integer desiredScale,
            SortOrder expectedModifier, boolean expectedRowKeyOrderOptimizable); void coerceBytes(ImmutableBytesWritable ptr, Object o, PDataType actualType, Integer actualMaxLength,
            Integer actualScale, SortOrder actualModifier, Integer desiredMaxLength, Integer desiredScale,
            SortOrder expectedModifier); final void coerceBytes(ImmutableBytesWritable ptr, PDataType actualType, SortOrder actualModifier,
            SortOrder expectedModifier); final void coerceBytes(ImmutableBytesWritable ptr, PDataType actualType, SortOrder actualModifier,
            SortOrder expectedModifier, Integer desiredMaxLength); @Override boolean isNullable(); abstract Integer getByteSize(); @Override int encodedLength(T val); @Override int skip(PositionedByteRange pbr); @Override boolean isOrderPreserving(); @Override boolean isSkippable(); @Override Order getOrder(); abstract boolean isFixedWidth(); abstract int compareTo(Object lhs, Object rhs, PDataType rhsType); @Override int compareTo(PDataType<?> other); abstract int toBytes(Object object, byte[] bytes, int offset); @Override int encode(PositionedByteRange pbr, T val); @Override String toString(); abstract byte[] toBytes(Object object); abstract Object toObject(String value); abstract Object toObject(Object object, PDataType actualType); abstract Object toObject(byte[] bytes, int offset, int length, PDataType actualType, SortOrder sortOrder,
            Integer maxLength, Integer scale); @SuppressWarnings("unchecked") @Override T decode(PositionedByteRange pbr); abstract Object getSampleValue(Integer maxLength, Integer arrayLength); final Object getSampleValue(); final Object getSampleValue(Integer maxLength); final Object toObject(byte[] bytes, int offset, int length, PDataType actualType, SortOrder sortOrder); final Object toObject(byte[] bytes, int offset, int length, PDataType actualType); final Object toObject(ImmutableBytesWritable ptr, PDataType actualType); final Object toObject(ImmutableBytesWritable ptr, PDataType actualType, SortOrder sortOrder); final Object toObject(ImmutableBytesWritable ptr, PDataType actualType, SortOrder sortOrder,
            Integer maxLength, Integer scale); final Object toObject(ImmutableBytesWritable ptr, SortOrder sortOrder, Integer maxLength, Integer scale); final Object toObject(ImmutableBytesWritable ptr); final Object toObject(ImmutableBytesWritable ptr, SortOrder sortOrder); final Object toObject(byte[] bytes, int offset, int length); final Object toObject(byte[] bytes); final Object toObject(byte[] bytes, SortOrder sortOrder); final Object toObject(byte[] bytes, SortOrder sortOrder, PDataType actualType); static PDataType fromSqlTypeName(String sqlTypeName); static int sqlArrayType(String sqlTypeName); static PDataType fromTypeId(int typeId); String getJavaClassName(); byte[] getJavaClassNameBytes(); byte[] getSqlTypeNameBytes(); int getResultSetSqlType(); KeyRange getKeyRange(byte[] point); final String toStringLiteral(ImmutableBytesWritable ptr, Format formatter); final String toStringLiteral(byte[] b, Format formatter); String toStringLiteral(byte[] b, int offset, int length, Format formatter); String toStringLiteral(Object o, Format formatter); String toStringLiteral(Object o); PhoenixArrayFactory getArrayFactory(); static PhoenixArray instantiatePhoenixArray(PDataType actualType, Object[] elements); KeyRange getKeyRange(byte[] lowerRange, boolean lowerInclusive, byte[] upperRange, boolean upperInclusive); static PDataType fromLiteral(Object value); int getNanos(ImmutableBytesWritable ptr, SortOrder sortOrder); long getMillis(ImmutableBytesWritable ptr, SortOrder sortOrder); Object pad(Object object, Integer maxLength); void pad(ImmutableBytesWritable ptr, Integer maxLength, SortOrder sortOrder); byte[] pad(byte[] b, Integer maxLength, SortOrder sortOrder); static PDataType arrayBaseType(PDataType arrayType); }
PDataType implements DataType<T>, Comparable<PDataType<?>> { protected static int toBytes(BigDecimal v, byte[] result, final int offset, int length) { int signum = v.signum(); if (signum == 0) { result[offset] = ZERO_BYTE; return 1; } int index = offset + length; int scale = v.scale(); int expOffset = scale % 2 * (scale < 0 ? -1 : 1); int multiplyBy; BigInteger divideBy; if (expOffset == 0) { multiplyBy = 1; divideBy = ONE_HUNDRED; } else { multiplyBy = 10; divideBy = BigInteger.TEN; } int digitOffset; BigInteger compareAgainst; if (signum == 1) { digitOffset = POS_DIGIT_OFFSET; compareAgainst = MAX_LONG; scale -= (length - 2) * 2; result[offset] = (byte)((-(scale + expOffset) / 2 + EXP_BYTE_OFFSET) | 0x80); } else { digitOffset = NEG_DIGIT_OFFSET; compareAgainst = MIN_LONG; scale -= (length - 2 - 1) * 2; result[offset] = (byte)(~(-(scale + expOffset) / 2 + EXP_BYTE_OFFSET + 128) & 0x7F); if (length <= MAX_BIG_DECIMAL_BYTES) { result[--index] = NEG_TERMINAL_BYTE; } else { length = MAX_BIG_DECIMAL_BYTES; index = offset + length; } } BigInteger bi = v.unscaledValue(); while (bi.compareTo(compareAgainst) * signum > 0) { BigInteger[] dandr = bi.divideAndRemainder(divideBy); bi = dandr[0]; int digit = dandr[1].intValue(); result[--index] = (byte)(digit * multiplyBy + digitOffset); multiplyBy = 1; divideBy = ONE_HUNDRED; } long l = bi.longValue(); do { long divBy = 100 / multiplyBy; long digit = l % divBy; l /= divBy; result[--index] = (byte)(digit * multiplyBy + digitOffset); multiplyBy = 1; } while (l != 0); return length; } protected  PDataType(String sqlTypeName, int sqlType, Class clazz, PDataCodec codec, int ordinal); static PDataType[] values(); int ordinal(); @SuppressWarnings("unchecked") @Override Class<T> encodedClass(); boolean isCastableTo(PDataType targetType); final PDataCodec getCodec(); boolean isBytesComparableWith(PDataType otherType); int estimateByteSize(Object o); Integer getMaxLength(Object o); Integer getScale(Object o); Integer estimateByteSizeFromLength(Integer length); final String getSqlTypeName(); final int getSqlType(); final Class getJavaClass(); boolean isArrayType(); final int compareTo(byte[] lhs, int lhsOffset, int lhsLength, SortOrder lhsSortOrder, byte[] rhs,
            int rhsOffset, int rhsLength, SortOrder rhsSortOrder, PDataType rhsType); static boolean isDoubleOrFloat(PDataType type); @Override boolean equals(Object o); static boolean equalsAny(PDataType lhs, PDataType... rhs); boolean isCoercibleTo(PDataType targetType); boolean isComparableTo(PDataType targetType); boolean isCoercibleTo(PDataType targetType, Object value); boolean isSizeCompatible(ImmutableBytesWritable ptr, Object value, PDataType srcType, SortOrder sortOrder,
            Integer maxLength, Integer scale, Integer desiredMaxLength, Integer desiredScale); int compareTo(byte[] b1, byte[] b2); final int compareTo(ImmutableBytesWritable ptr1, ImmutableBytesWritable ptr2); final int compareTo(byte[] ba1, int offset1, int length1, SortOrder so1, byte[] ba2, int offset2,
            int length2, SortOrder so2); final int compareTo(ImmutableBytesWritable ptr1, SortOrder ptr1SortOrder, ImmutableBytesWritable ptr2,
            SortOrder ptr2SortOrder, PDataType type2); int compareTo(Object lhs, Object rhs); final boolean isNull(byte[] value); byte[] toBytes(Object object, SortOrder sortOrder); void coerceBytes(ImmutableBytesWritable ptr, Object o, PDataType actualType, Integer actualMaxLength,
            Integer actualScale, SortOrder actualModifier, Integer desiredMaxLength, Integer desiredScale,
            SortOrder expectedModifier, boolean expectedRowKeyOrderOptimizable); void coerceBytes(ImmutableBytesWritable ptr, Object o, PDataType actualType, Integer actualMaxLength,
            Integer actualScale, SortOrder actualModifier, Integer desiredMaxLength, Integer desiredScale,
            SortOrder expectedModifier); final void coerceBytes(ImmutableBytesWritable ptr, PDataType actualType, SortOrder actualModifier,
            SortOrder expectedModifier); final void coerceBytes(ImmutableBytesWritable ptr, PDataType actualType, SortOrder actualModifier,
            SortOrder expectedModifier, Integer desiredMaxLength); @Override boolean isNullable(); abstract Integer getByteSize(); @Override int encodedLength(T val); @Override int skip(PositionedByteRange pbr); @Override boolean isOrderPreserving(); @Override boolean isSkippable(); @Override Order getOrder(); abstract boolean isFixedWidth(); abstract int compareTo(Object lhs, Object rhs, PDataType rhsType); @Override int compareTo(PDataType<?> other); abstract int toBytes(Object object, byte[] bytes, int offset); @Override int encode(PositionedByteRange pbr, T val); @Override String toString(); abstract byte[] toBytes(Object object); abstract Object toObject(String value); abstract Object toObject(Object object, PDataType actualType); abstract Object toObject(byte[] bytes, int offset, int length, PDataType actualType, SortOrder sortOrder,
            Integer maxLength, Integer scale); @SuppressWarnings("unchecked") @Override T decode(PositionedByteRange pbr); abstract Object getSampleValue(Integer maxLength, Integer arrayLength); final Object getSampleValue(); final Object getSampleValue(Integer maxLength); final Object toObject(byte[] bytes, int offset, int length, PDataType actualType, SortOrder sortOrder); final Object toObject(byte[] bytes, int offset, int length, PDataType actualType); final Object toObject(ImmutableBytesWritable ptr, PDataType actualType); final Object toObject(ImmutableBytesWritable ptr, PDataType actualType, SortOrder sortOrder); final Object toObject(ImmutableBytesWritable ptr, PDataType actualType, SortOrder sortOrder,
            Integer maxLength, Integer scale); final Object toObject(ImmutableBytesWritable ptr, SortOrder sortOrder, Integer maxLength, Integer scale); final Object toObject(ImmutableBytesWritable ptr); final Object toObject(ImmutableBytesWritable ptr, SortOrder sortOrder); final Object toObject(byte[] bytes, int offset, int length); final Object toObject(byte[] bytes); final Object toObject(byte[] bytes, SortOrder sortOrder); final Object toObject(byte[] bytes, SortOrder sortOrder, PDataType actualType); static PDataType fromSqlTypeName(String sqlTypeName); static int sqlArrayType(String sqlTypeName); static PDataType fromTypeId(int typeId); String getJavaClassName(); byte[] getJavaClassNameBytes(); byte[] getSqlTypeNameBytes(); int getResultSetSqlType(); KeyRange getKeyRange(byte[] point); final String toStringLiteral(ImmutableBytesWritable ptr, Format formatter); final String toStringLiteral(byte[] b, Format formatter); String toStringLiteral(byte[] b, int offset, int length, Format formatter); String toStringLiteral(Object o, Format formatter); String toStringLiteral(Object o); PhoenixArrayFactory getArrayFactory(); static PhoenixArray instantiatePhoenixArray(PDataType actualType, Object[] elements); KeyRange getKeyRange(byte[] lowerRange, boolean lowerInclusive, byte[] upperRange, boolean upperInclusive); static PDataType fromLiteral(Object value); int getNanos(ImmutableBytesWritable ptr, SortOrder sortOrder); long getMillis(ImmutableBytesWritable ptr, SortOrder sortOrder); Object pad(Object object, Integer maxLength); void pad(ImmutableBytesWritable ptr, Integer maxLength, SortOrder sortOrder); byte[] pad(byte[] b, Integer maxLength, SortOrder sortOrder); static PDataType arrayBaseType(PDataType arrayType); static final int MAX_PRECISION; static final int MIN_DECIMAL_AVG_SCALE; static final MathContext DEFAULT_MATH_CONTEXT; static final int DEFAULT_SCALE; static final byte[] FALSE_BYTES; static final byte[] TRUE_BYTES; static final byte[] NULL_BYTES; final static Integer ZERO; final static Integer INT_PRECISION; final static Integer LONG_PRECISION; final static Integer SHORT_PRECISION; final static Integer BYTE_PRECISION; final static Integer DOUBLE_PRECISION; static final int ARRAY_TYPE_BASE; static final String ARRAY_TYPE_SUFFIX; }