Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 29 additions & 30 deletions core/src/main/java/org/apache/iceberg/util/ZOrderByteUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,57 +50,56 @@ static ByteBuffer allocatePrimitiveBuffer() {
return ByteBuffer.allocate(PRIMITIVE_BUFFER_SIZE);
}

/**
* Signed ints do not have their bytes in magnitude order because of the sign bit. To fix this,
* flip the sign bit so that all negatives are ordered before positives. This essentially shifts
* the 0 value so that we don't break our ordering when we cross the new 0 value.
*/
/** Internally just calls {@link #wholeNumberOrderedBytes(long, ByteBuffer)} */
public static ByteBuffer intToOrderedBytes(int val, ByteBuffer reuse) {
ByteBuffer bytes = ByteBuffers.reuse(reuse, PRIMITIVE_BUFFER_SIZE);
bytes.putLong(((long) val) ^ 0x8000000000000000L);
return bytes;
return wholeNumberOrderedBytes(val, reuse);
}

/** Internally just calls {@link #wholeNumberOrderedBytes(long, ByteBuffer)} */
public static ByteBuffer longToOrderedBytes(long val, ByteBuffer reuse) {
return wholeNumberOrderedBytes(val, reuse);
}

/** Internally just calls {@link #wholeNumberOrderedBytes(long, ByteBuffer)} */
public static ByteBuffer shortToOrderedBytes(short val, ByteBuffer reuse) {
return wholeNumberOrderedBytes(val, reuse);
}

/** Internally just calls {@link #wholeNumberOrderedBytes(long, ByteBuffer)} */
public static ByteBuffer tinyintToOrderedBytes(byte val, ByteBuffer reuse) {
return wholeNumberOrderedBytes(val, reuse);
}

/**
* Signed longs are treated the same as the signed ints in {@link #intToOrderedBytes(int,
* ByteBuffer)}
* Signed longs do not have their bytes in magnitude order because of the sign bit. To fix this,
* flip the sign bit so that all negatives are ordered before positives. This essentially shifts
* the 0 value so that we don't break our ordering when we cross the new 0 value.
*/
public static ByteBuffer longToOrderedBytes(long val, ByteBuffer reuse) {
public static ByteBuffer wholeNumberOrderedBytes(long val, ByteBuffer reuse) {
ByteBuffer bytes = ByteBuffers.reuse(reuse, PRIMITIVE_BUFFER_SIZE);
bytes.putLong(val ^ 0x8000000000000000L);
return bytes;
}

/**
* Signed shorts are treated the same as the signed ints in {@link #intToOrderedBytes(int,
* ByteBuffer)}
*/
public static ByteBuffer shortToOrderedBytes(short val, ByteBuffer reuse) {
return intToOrderedBytes(val, reuse);
/** Internally just calls {@link #floatingPointOrderedBytes(double, ByteBuffer)} */
public static ByteBuffer floatToOrderedBytes(float val, ByteBuffer reuse) {
return floatingPointOrderedBytes(val, reuse);
}

/**
* Signed tiny ints are treated the same as the signed ints in {@link #intToOrderedBytes(int,
* ByteBuffer)}
*/
public static ByteBuffer tinyintToOrderedBytes(byte val, ByteBuffer reuse) {
return intToOrderedBytes(val, reuse);
/** Internally just calls {@link #floatingPointOrderedBytes(double, ByteBuffer)} */
public static ByteBuffer doubleToOrderedBytes(double val, ByteBuffer reuse) {
return floatingPointOrderedBytes(val, reuse);
}

/**
* IEEE 754 : “If two floating-point numbers in the same format are ordered (say, x {@literal <}
* y), they are ordered the same way when their bits are reinterpreted as sign-magnitude
* integers.”
*
* <p>Which means floats can be treated as sign magnitude integers which can then be converted
* <p>Which means doubles can be treated as sign magnitude integers which can then be converted
* into lexicographically comparable bytes
*/
public static ByteBuffer floatToOrderedBytes(float val, ByteBuffer reuse) {
return doubleToOrderedBytes(val, reuse);
}

/** Doubles are treated the same as floats in {@link #floatToOrderedBytes(float, ByteBuffer)} */
public static ByteBuffer doubleToOrderedBytes(double val, ByteBuffer reuse) {
public static ByteBuffer floatingPointOrderedBytes(double val, ByteBuffer reuse) {
ByteBuffer bytes = ByteBuffers.reuse(reuse, PRIMITIVE_BUFFER_SIZE);
long lval = Double.doubleToLongBits(val);
lval ^= ((lval >> (Integer.SIZE - 1)) | Long.MIN_VALUE);
Expand Down