You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2022/02/06 23:43:48 UTC

[GitHub] [iceberg] rdblue commented on a change in pull request #3966: Core: Adds Utility Class for Implementing ZOrdering

rdblue commented on a change in pull request #3966:
URL: https://github.com/apache/iceberg/pull/3966#discussion_r800250355



##########
File path: core/src/main/java/org/apache/iceberg/util/ZOrderByteUtils.java
##########
@@ -0,0 +1,162 @@
+/*
+ * 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.nio.ByteBuffer;
+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/
+ *
+ * Some implementation is taken from
+ * https://github.com/apache/hbase/blob/master/hbase-common/src/main/java/org/apache/hadoop/hbase/util/OrderedBytes.java
+ */
+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[] intToOrderedBytes(int val) {
+    ByteBuffer bytes = ByteBuffer.allocate(Integer.BYTES);

Review comment:
       I'm okay with this if you want to get this through quickly, but in the long term I think we should avoid the allocation cost. What I'd recommend since this is a util method is to add a `reuse` argument:
   
   ```java
     // this should probably live in the ByteBuffers util class
     public static ByteBuffer reuseOrAllocate(ByteBuffer reuse, int length) {
       if (reuse.hasArray() && reuse.arrayOffset() == 0 && reuse.capacity() == length) {
         reuse.position(0);
         reuse.limit(length);
         return reuse;
       } else {
         return ByteBuffer.allocate(length);
       }
     }
   
     public static ByteBuffer intToOrderedBytes(int val, ByteBuffer reused) {
       ByteBuffer bytes = reuseOrAllocate(reused, 4);
       bytes.putInt(val ^ 0x80000000);
       return bytes;
     }
   ```
   
   Then the caller can always call `ByteBuffer.array()` to get the underlying bytes, but it can keep reusing the space:
   ```java
     v1Bytes = intToOrderedBytes(v1, v1Bytes);
     v2Bytes = intToOrderedBytes(v2, v2Bytes);
     zorderBytes = zorder(v1Bytes, v2Bytes, zorderBytes);
   ```




-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org