RussellSpitzer commented on a change in pull request #3983: URL: https://github.com/apache/iceberg/pull/3983#discussion_r794180225
########## File path: core/src/main/java/org/apache/iceberg/util/ZOrderByteUtils.java ########## @@ -0,0 +1,128 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.iceberg.util; + +import java.util.Arrays; + +/** + * Within Z-Ordering the byte representations of objects being compared must be ordered, + * this requires several types to be transformed when converted to bytes. The goal is to + * map object's whose byte representation are not lexicographically ordered into representations + * that are lexicographically ordered. + * Most of these techniques are derived from + * https://aws.amazon.com/blogs/database/z-order-indexing-for-multifaceted-queries-in-amazon-dynamodb-part-2/ + */ +public class ZOrderByteUtils { + + private ZOrderByteUtils() { + + } + + /** + * 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. + */ + public static byte[] orderIntLikeBytes(byte[] intBytes, int size) { + if (intBytes == null) { + return new byte[size]; + } + intBytes[0] = (byte) (intBytes[0] ^ (1 << 7)); + return intBytes; + } + + /** + * IEEE 754 : + * “If two floating-point numbers in the same format are ordered (say, x < y), + * they are ordered the same way when their bits are reinterpreted as sign-magnitude integers.” + * + * Which means floats can be treated as sign magnitude integers which can then be converted into lexicographically + * comparable bytes + */ + public static byte[] orderFloatLikeBytes(byte[] floatBytes, int size) { + if (floatBytes == null) { + return new byte[size]; + } + if ((floatBytes[0] & (1 << 7)) == 0) { + // The signed magnitude is positive set the first bit (reversing the sign so positives order after negatives) + floatBytes[0] = (byte) (floatBytes[0] | (1 << 7)); + } else { + // The signed magnitude is negative so flip the first bit (reversing the sign so positives order after negatives) + // Then flip all remaining bits so numbers with greater negative magnitude come before those + // with less magnitude (reverse the order) + for (int i = 0; i < floatBytes.length; i++) { + floatBytes[i] = (byte) ~floatBytes[i]; + } Review comment: Yep, so the thing about sign magnitude integers is that the ordering of positive numbers is just what you expect. The larger the binary representation the larger the positive number. But for negatives the effect is the opposite. A negative number with a large magnitude is more negative than a negative number with a smaller magnitude. (This is different than the 2's complement representation we deal with above in orderIntLikeBytes) So basically if we drew out the ordering from smallest to largest byte representations it goes ``` 0000000 11111111 0--------------------> Most Positive 0 <--------------------Most Negative ``` So we take this and first flip the sign bit which changes it to ``` 0000000 11111111 0<---------------------Most Negative 0------------------------> Most Positive ``` But we still have the negatives in the wrong direction, twiddling those bits flips their direction (0010 -> 1101) (originally the most negative number) (0001 -> 1110) (0000 -> 1111) And our final range looks like this ``` 0000000 11111111 Most negative ---------------------> 0 0------------------------> Most Positive ``` Imagine we have 4 byte signed magnitude integers ``` 0000 = 0 ==> 1000 0001 = 1 ==> 1001 0010 = 2 ==> 1010 0011 = 3 ==> 1011 0100 = 4 ==> 1100 0101 = 5 ==> 1101 0111 = 6 ==> 1111 1000 = 0 ==> 0111 1001 = -1 ==> 0110 1010 = -2 ==> 0101 1011 = -3 ==> 0100 1100 = -4 ==> 0011 1101 = -5 ==> 0010 1111 = -6 ==> 0000 Which if we sort based on the transformed binary gives us 1111 = -6 ==> 0000 1101 = -5 ==> 0010 1100 = -4 ==> 0011 1011 = -3 ==> 0100 1010 = -2 ==> 0101 1001 = -1 ==> 0110 1000 = 0 ==> 0111 0000 = 0 ==> 1000 0001 = 1 ==> 1001 0010 = 2 ==> 1010 0011 = 3 ==> 1011 0100 = 4 ==> 1100 0101 = 5 ==> 1101 0111 = 6 ==> 1111 ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
