PDataType implements DataType<T>, Comparable<PDataType<?>> { public final int compareTo(byte[] lhs, int lhsOffset, int lhsLength, SortOrder lhsSortOrder, byte[] rhs, int rhsOffset, int rhsLength, SortOrder rhsSortOrder, PDataType rhsType) { Preconditions.checkNotNull(lhsSortOrder); Preconditions.checkNotNull(rhsSortOrder); if (this.isBytesComparableWith(rhsType)) { return compareTo(lhs, lhsOffset, lhsLength, lhsSortOrder, rhs, rhsOffset, rhsLength, rhsSortOrder); } PDataCodec lhsCodec = this.getCodec(); if (lhsCodec == null) { byte[] rhsConverted = this.toBytes(this.toObject(rhs, rhsOffset, rhsLength, rhsType, rhsSortOrder)); if (rhsSortOrder == SortOrder.DESC) { rhsSortOrder = SortOrder.ASC; } if (lhsSortOrder == SortOrder.DESC) { lhs = SortOrder.invert(lhs, lhsOffset, new byte[lhsLength], 0, lhsLength); } return Bytes.compareTo(lhs, lhsOffset, lhsLength, rhsConverted, 0, rhsConverted.length); } PDataCodec rhsCodec = rhsType.getCodec(); if (rhsCodec == null) { byte[] lhsConverted = rhsType.toBytes(rhsType.toObject(lhs, lhsOffset, lhsLength, this, lhsSortOrder)); if (lhsSortOrder == SortOrder.DESC) { lhsSortOrder = SortOrder.ASC; } if (rhsSortOrder == SortOrder.DESC) { rhs = SortOrder.invert(rhs, rhsOffset, new byte[rhsLength], 0, rhsLength); } return Bytes.compareTo(lhsConverted, 0, lhsConverted.length, rhs, rhsOffset, rhsLength); } if (this.isCoercibleTo(PLong.INSTANCE) && rhsType .isCoercibleTo(PLong.INSTANCE)) { return Longs.compare(this.getCodec().decodeLong(lhs, lhsOffset, lhsSortOrder), rhsType.getCodec().decodeLong(rhs, rhsOffset, rhsSortOrder)); } else if (isDoubleOrFloat(this) && isDoubleOrFloat( rhsType)) { return Doubles.compare(this.getCodec().decodeDouble(lhs, lhsOffset, lhsSortOrder), rhsType.getCodec().decodeDouble(rhs, rhsOffset, rhsSortOrder)); } else { float fvalue = 0.0F; double dvalue = 0.0; long lvalue = 0; boolean isFloat = false; int invert = 1; if (this.isCoercibleTo(PLong.INSTANCE)) { lvalue = this.getCodec().decodeLong(lhs, lhsOffset, lhsSortOrder); } else if (this.getClass() == PFloat.class) { isFloat = true; fvalue = this.getCodec().decodeFloat(lhs, lhsOffset, lhsSortOrder); } else if (this.isCoercibleTo(PDouble.INSTANCE)) { dvalue = this.getCodec().decodeDouble(lhs, lhsOffset, lhsSortOrder); } if (rhsType.isCoercibleTo(PLong.INSTANCE)) { lvalue = rhsType.getCodec().decodeLong(rhs, rhsOffset, rhsSortOrder); } else if (rhsType == PFloat.INSTANCE) { invert = -1; isFloat = true; fvalue = rhsType.getCodec().decodeFloat(rhs, rhsOffset, rhsSortOrder); } else if (rhsType.isCoercibleTo(PDouble.INSTANCE)) { invert = -1; dvalue = rhsType.getCodec().decodeDouble(rhs, rhsOffset, rhsSortOrder); } return invert * (isFloat ? compareFloatToLong(fvalue, lvalue) : compareDoubleToLong(dvalue, lvalue)); } } protected  PDataType(String sqlTypeName, int sqlType, Class clazz, PDataCodec codec, int ordinal); @Deprecated static PDataType[] values(); @Deprecated int ordinal(); @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,
      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); 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); @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); 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); 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; static final int ARRAY_TYPE_BASE; static final String ARRAY_TYPE_SUFFIX; }
@Test public void testIntVersusLong() { long l = -1L; int i = -1; assertTrue(PLong.INSTANCE.compareTo(l, i, PInteger.INSTANCE)==0); assertTrue(PInteger.INSTANCE.compareTo(i, l, PLong.INSTANCE)==0); }