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

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


The following commit(s) were added to refs/heads/master by this push:
     new 780e22686d PHOENIX-7617 BSON serialization should retain ByteBuffer 
offset (#2162)
780e22686d is described below

commit 780e22686ddd09146a93bf17e55cd90e4cea5d04
Author: Viraj Jasani <vjas...@apache.org>
AuthorDate: Tue May 20 10:27:50 2025 -0700

    PHOENIX-7617 BSON serialization should retain ByteBuffer offset (#2162)
---
 .../main/java/org/apache/phoenix/schema/types/PBson.java |  7 +++----
 .../src/main/java/org/apache/phoenix/util/ByteUtil.java  | 16 ++++++++++++++++
 .../org/apache/phoenix/util/json/BsonDataFormat.java     |  5 +++--
 3 files changed, 22 insertions(+), 6 deletions(-)

diff --git 
a/phoenix-core-client/src/main/java/org/apache/phoenix/schema/types/PBson.java 
b/phoenix-core-client/src/main/java/org/apache/phoenix/schema/types/PBson.java
index 1beacefa03..406a182a5a 100644
--- 
a/phoenix-core-client/src/main/java/org/apache/phoenix/schema/types/PBson.java
+++ 
b/phoenix-core-client/src/main/java/org/apache/phoenix/schema/types/PBson.java
@@ -22,7 +22,6 @@ import org.bson.BsonDocument;
 import org.bson.RawBsonDocument;
 import org.bson.codecs.BsonDocumentCodec;
 
-import org.apache.hadoop.hbase.util.Bytes;
 import org.apache.phoenix.schema.SortOrder;
 import org.apache.phoenix.util.ByteUtil;
 
@@ -61,11 +60,11 @@ public class PBson extends PVarbinary {
             throw new IllegalArgumentException("The object should be of type 
BsonDocument");
         }
         if (object instanceof RawBsonDocument) {
-            return Bytes.toBytes(((RawBsonDocument) 
object).getByteBuffer().asNIO());
+            return ByteUtil.toBytes(((RawBsonDocument) 
object).getByteBuffer().asNIO());
         } else {
             RawBsonDocument rawBsonDocument =
                 new RawBsonDocument((BsonDocument) object, new 
BsonDocumentCodec());
-            return Bytes.toBytes((rawBsonDocument).getByteBuffer().asNIO());
+            return ByteUtil.toBytes(rawBsonDocument.getByteBuffer().asNIO());
         }
     }
 
@@ -116,7 +115,7 @@ public class PBson extends PVarbinary {
 
     @Override
     public boolean isBytesComparableWith(@SuppressWarnings("rawtypes") 
PDataType otherType) {
-        return otherType == PVarbinary.INSTANCE;
+        return otherType == PVarbinary.INSTANCE || otherType == PBson.INSTANCE;
     }
 
     @Override
diff --git 
a/phoenix-core-client/src/main/java/org/apache/phoenix/util/ByteUtil.java 
b/phoenix-core-client/src/main/java/org/apache/phoenix/util/ByteUtil.java
index 9788fdbc67..4e1a1f9da8 100644
--- a/phoenix-core-client/src/main/java/org/apache/phoenix/util/ByteUtil.java
+++ b/phoenix-core-client/src/main/java/org/apache/phoenix/util/ByteUtil.java
@@ -23,6 +23,7 @@ import java.io.DataInputStream;
 import java.io.DataOutput;
 import java.io.DataOutputStream;
 import java.io.IOException;
+import java.nio.ByteBuffer;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -839,4 +840,19 @@ public class ByteUtil {
     public static byte[] closestPossibleRowAfter(byte[] row) {
         return Arrays.copyOf(row, row.length + 1);
     }
+
+    /**
+     * Returns a new byte array, copied from the given {@code buf}, from the 
buffer's current
+     * position to the limit (exclusive).
+     * The position and the other index parameters are not changed.
+     *
+     * @param buf a byte buffer
+     * @return the byte array
+     */
+    public static byte[] toBytes(ByteBuffer buf) {
+        ByteBuffer dup = buf.duplicate();
+        byte[] result = new byte[dup.remaining()];
+        dup.get(result);
+        return result;
+    }
 }
diff --git 
a/phoenix-core-client/src/main/java/org/apache/phoenix/util/json/BsonDataFormat.java
 
b/phoenix-core-client/src/main/java/org/apache/phoenix/util/json/BsonDataFormat.java
index 46ac53f387..bf4b0f1904 100644
--- 
a/phoenix-core-client/src/main/java/org/apache/phoenix/util/json/BsonDataFormat.java
+++ 
b/phoenix-core-client/src/main/java/org/apache/phoenix/util/json/BsonDataFormat.java
@@ -21,7 +21,7 @@ import com.jayway.jsonpath.Configuration;
 import com.jayway.jsonpath.JsonPath;
 import com.jayway.jsonpath.Option;
 import com.jayway.jsonpath.PathNotFoundException;
-import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.phoenix.util.ByteUtil;
 import org.bson.BsonBinaryReader;
 import org.bson.BsonDocument;
 import org.bson.BsonDocumentReader;
@@ -38,9 +38,10 @@ import java.util.List;
 import java.util.stream.Collectors;
 
 public class BsonDataFormat implements JsonDataFormat {
+
     @Override
     public byte[] toBytes(Object object) {
-        return Bytes.toBytes(((RawBsonDocument) 
object).getByteBuffer().asNIO());
+        return ByteUtil.toBytes(((RawBsonDocument) 
object).getByteBuffer().asNIO());
     }
 
     @Override

Reply via email to