This is an automated email from the ASF dual-hosted git repository.

ravindra pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/master by this push:
     new 48b7c9c  ARROW-6156: [Java] Support compare semantics for 
ArrowBufPointer
48b7c9c is described below

commit 48b7c9c690f9f3f29e01ae9414647c692d308484
Author: liyafan82 <fan_li...@foxmail.com>
AuthorDate: Wed Aug 14 14:43:24 2019 +0530

    ARROW-6156: [Java] Support compare semantics for ArrowBufPointer
    
    Compare two arrow buffer pointers by their content in lexicographic order.
    
    null is smaller and shorter buffer is smaller.
    
    Closes #5030 from liyafan82/fly_0807_cmp and squashes the following commits:
    
    4b09bb278 <liyafan82>  Support compare semantics for ArrowBufPointer
    
    Authored-by: liyafan82 <fan_li...@foxmail.com>
    Signed-off-by: Pindikura Ravindra <ravin...@dremio.com>
---
 .../algorithm/sort/DefaultVectorComparators.java   | 35 +++++++++-------------
 .../apache/arrow/memory/util/ArrowBufPointer.java  | 31 +++++++++++++++++++
 .../arrow/memory/util/TestArrowBufPointer.java     | 33 ++++++++++++++++++++
 3 files changed, 78 insertions(+), 21 deletions(-)

diff --git 
a/java/algorithm/src/main/java/org/apache/arrow/algorithm/sort/DefaultVectorComparators.java
 
b/java/algorithm/src/main/java/org/apache/arrow/algorithm/sort/DefaultVectorComparators.java
index a2d2f78..fe44ef0 100644
--- 
a/java/algorithm/src/main/java/org/apache/arrow/algorithm/sort/DefaultVectorComparators.java
+++ 
b/java/algorithm/src/main/java/org/apache/arrow/algorithm/sort/DefaultVectorComparators.java
@@ -17,8 +17,7 @@
 
 package org.apache.arrow.algorithm.sort;
 
-import static org.apache.arrow.vector.BaseVariableWidthVector.OFFSET_WIDTH;
-
+import org.apache.arrow.memory.util.ArrowBufPointer;
 import org.apache.arrow.vector.BaseFixedWidthVector;
 import org.apache.arrow.vector.BaseVariableWidthVector;
 import org.apache.arrow.vector.BigIntVector;
@@ -205,28 +204,22 @@ public class DefaultVectorComparators {
    */
   public static class VariableWidthComparator extends 
VectorValueComparator<BaseVariableWidthVector> {
 
-    @Override
-    public int compareNotNull(int index1, int index2) {
-      int start1 = vector1.getOffsetBuffer().getInt(index1 * OFFSET_WIDTH);
-      int start2 = vector2.getOffsetBuffer().getInt(index2 * OFFSET_WIDTH);
-
-      int end1 = vector1.getOffsetBuffer().getInt((index1 + 1) * OFFSET_WIDTH);
-      int end2 = vector2.getOffsetBuffer().getInt((index2 + 1) * OFFSET_WIDTH);
-
-      int length1 = end1 - start1;
-      int length2 = end2 - start2;
+    private ArrowBufPointer reusablePointer1 = new ArrowBufPointer();
 
-      int minLength = length1 < length2 ? length1 : length2;
-      for (int i = 0; i < minLength; i++) {
-        byte b1 = vector1.getDataBuffer().getByte(start1 + i);
-        byte b2 = vector2.getDataBuffer().getByte(start2 + i);
+    private ArrowBufPointer reusablePointer2 = new ArrowBufPointer();
 
-        if (b1 != b2) {
-          return b1 - b2;
-        }
-      }
+    @Override
+    public int compare(int index1, int index2) {
+      vector1.getDataPointer(index1, reusablePointer1);
+      vector2.getDataPointer(index2, reusablePointer2);
+      return reusablePointer1.compareTo(reusablePointer2);
+    }
 
-      return length1 - length2;
+    @Override
+    public int compareNotNull(int index1, int index2) {
+      vector1.getDataPointer(index1, reusablePointer1);
+      vector2.getDataPointer(index2, reusablePointer2);
+      return reusablePointer1.compareTo(reusablePointer2);
     }
   }
 
diff --git 
a/java/memory/src/main/java/org/apache/arrow/memory/util/ArrowBufPointer.java 
b/java/memory/src/main/java/org/apache/arrow/memory/util/ArrowBufPointer.java
index 5c6352c..dd00c79 100644
--- 
a/java/memory/src/main/java/org/apache/arrow/memory/util/ArrowBufPointer.java
+++ 
b/java/memory/src/main/java/org/apache/arrow/memory/util/ArrowBufPointer.java
@@ -167,4 +167,35 @@ public final class ArrowBufPointer {
     hashCodeChanged = false;
     return hashCode;
   }
+
+  /**
+   * Compare two arrow buffer pointers.
+   * The comparison is based on lexicographic order.
+   * @param that the other pointer to compare.
+   * @return 0 if the two pointers are equal;
+   *     a positive integer if this pointer is larger;
+   *     a negative integer if this pointer is smaller.
+   */
+  public int compareTo(ArrowBufPointer that) {
+    if (this.buf == null || that.buf == null) {
+      if (this.buf == null && that.buf == null) {
+        return 0;
+      } else {
+        // null is smaller
+        return this.buf == null ? -1 : 1;
+      }
+    }
+
+    int length = this.length < that.length ? this.length : that.length;
+    for (int i = 0; i < length; i++) {
+      byte b1 = this.buf.getByte(this.offset + i);
+      byte b2 = that.buf.getByte(that.offset + i);
+      if (b1 != b2) {
+        return b1 - b2;
+      }
+    }
+
+    // the shorter buffer is smaller
+    return this.length - that.length;
+  }
 }
diff --git 
a/java/memory/src/test/java/org/apache/arrow/memory/util/TestArrowBufPointer.java
 
b/java/memory/src/test/java/org/apache/arrow/memory/util/TestArrowBufPointer.java
index 2296f52..f29b78c 100644
--- 
a/java/memory/src/test/java/org/apache/arrow/memory/util/TestArrowBufPointer.java
+++ 
b/java/memory/src/test/java/org/apache/arrow/memory/util/TestArrowBufPointer.java
@@ -152,6 +152,39 @@ public class TestArrowBufPointer {
     }
   }
 
+  @Test
+  public void testArrowBufPointersComparison() {
+    final int vectorLength = 100;
+    try (ArrowBuf buf1 = allocator.buffer(vectorLength);
+         ArrowBuf buf2 = allocator.buffer(vectorLength)) {
+      for (int i = 0; i < vectorLength; i++) {
+        buf1.setByte(i, i);
+        buf2.setByte(i, i);
+      }
+
+      ArrowBufPointer pointer1 = new ArrowBufPointer();
+      ArrowBufPointer pointer2 = new ArrowBufPointer();
+
+      pointer1.set(buf1, 0, 10);
+      pointer2.set(buf2, 0, 10);
+      assertEquals(0, pointer1.compareTo(pointer2));
+
+      pointer1.set(null, 0, 0);
+      pointer2.set(null, 0, 0);
+      assertEquals(0, pointer1.compareTo(pointer2));
+
+      pointer2.set(buf2, 0, 5);
+      assertTrue(pointer1.compareTo(pointer2) < 0);
+
+      pointer1.set(buf1, 0, 10);
+      assertTrue(pointer1.compareTo(pointer2) > 0);
+
+      pointer1.set(buf1, 1, 5);
+      pointer2.set(buf2, 3, 8);
+      assertTrue(pointer1.compareTo(pointer2) < 0);
+    }
+  }
+
   /**
    * Hasher with a counter that increments each time a hash code is calculated.
    * This is to validate that the hash code in {@link ArrowBufPointer} is 
reused.

Reply via email to