You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by mb...@apache.org on 2025/01/10 17:53:00 UTC

(calcite) branch main updated: [CALCITE-6777] Conversion between UUID and BINARY produces wrong results

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

mbudiu pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite.git


The following commit(s) were added to refs/heads/main by this push:
     new 737dc1efb2 [CALCITE-6777] Conversion between UUID and BINARY produces wrong results
737dc1efb2 is described below

commit 737dc1efb2b6f1e2bd9cb40c3237037e8dbf0713
Author: Mihai Budiu <mb...@feldera.com>
AuthorDate: Thu Jan 9 20:52:35 2025 -0800

    [CALCITE-6777] Conversion between UUID and BINARY produces wrong results
    
    Signed-off-by: Mihai Budiu <mb...@feldera.com>
---
 .../main/java/org/apache/calcite/runtime/SqlFunctions.java  | 13 ++++++++++---
 core/src/test/resources/sql/misc.iq                         |  4 ++--
 2 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
index 2be3293916..1f8a125816 100644
--- a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
+++ b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
@@ -60,7 +60,6 @@ import org.apache.commons.codec.binary.Hex;
 import org.apache.commons.codec.digest.DigestUtils;
 import org.apache.commons.codec.language.Soundex;
 import org.apache.commons.lang.StringEscapeUtils;
-import org.apache.commons.lang3.Conversion;
 import org.apache.commons.math3.util.CombinatoricsUtils;
 import org.apache.commons.text.similarity.LevenshteinDistance;
 
@@ -284,12 +283,20 @@ public class SqlFunctions {
   }
 
   public static UUID binaryToUuid(ByteString bytes) {
-    return Conversion.byteArrayToUuid(bytes.getBytes(), 0);
+    if (bytes.length() < 16) {
+      throw new IllegalArgumentException("Need at least 16 bytes for UUID");
+    }
+    ByteBuffer byteBuffer = ByteBuffer.wrap(bytes.getBytes());
+    long mostSignificantBits = byteBuffer.getLong();
+    long leastSignificantBits = byteBuffer.getLong();
+    return new UUID(mostSignificantBits, leastSignificantBits);
   }
 
   public static ByteString uuidToBinary(UUID uuid) {
     byte[] dest = new byte[16];
-    Conversion.uuidToByteArray(uuid, dest, 0, 16);
+    ByteBuffer byteBuffer = ByteBuffer.wrap(dest);
+    byteBuffer.putLong(uuid.getMostSignificantBits());
+    byteBuffer.putLong(uuid.getLeastSignificantBits());
     return new ByteString(dest);
   }
 
diff --git a/core/src/test/resources/sql/misc.iq b/core/src/test/resources/sql/misc.iq
index 83d045f1bd..293470029d 100644
--- a/core/src/test/resources/sql/misc.iq
+++ b/core/src/test/resources/sql/misc.iq
@@ -98,13 +98,13 @@ SELECT CAST(UUID '123e4567-e89b-12d3-a456-426655440000' AS VARBINARY);
 +----------------------------------+
 | EXPR$0                           |
 +----------------------------------+
-| d3129be867453e1200004455664256a4 |
+| 123e4567e89b12d3a456426655440000 |
 +----------------------------------+
 (1 row)
 
 !ok
 
-SELECT CAST(x'd3129be867453e1200004455664256a4' AS UUID);
+SELECT CAST(x'123e4567e89b12d3a456426655440000' AS UUID);
 +--------------------------------------+
 | EXPR$0                               |
 +--------------------------------------+