You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pinot.apache.org by ja...@apache.org on 2023/11/14 17:15:05 UTC

(pinot) branch master updated: Adding byte functions for UUIDs (#11988)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 53883aedb5 Adding byte functions for UUIDs (#11988)
53883aedb5 is described below

commit 53883aedb5155a7fce7a4acad5547c95724e6dc4
Author: Malte Granderath <ma...@gmail.com>
AuthorDate: Tue Nov 14 18:14:59 2023 +0100

    Adding byte functions for UUIDs (#11988)
---
 .../common/function/scalar/StringFunctions.java    | 33 ++++++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/pinot-common/src/main/java/org/apache/pinot/common/function/scalar/StringFunctions.java b/pinot-common/src/main/java/org/apache/pinot/common/function/scalar/StringFunctions.java
index 52442590e8..5a49314943 100644
--- a/pinot-common/src/main/java/org/apache/pinot/common/function/scalar/StringFunctions.java
+++ b/pinot-common/src/main/java/org/apache/pinot/common/function/scalar/StringFunctions.java
@@ -21,9 +21,11 @@ package org.apache.pinot.common.function.scalar;
 import java.io.UnsupportedEncodingException;
 import java.net.URLDecoder;
 import java.net.URLEncoder;
+import java.nio.ByteBuffer;
 import java.nio.charset.StandardCharsets;
 import java.text.Normalizer;
 import java.util.Base64;
+import java.util.UUID;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import org.apache.commons.lang3.StringUtils;
@@ -493,6 +495,37 @@ public class StringFunctions {
     return input.getBytes(StandardCharsets.US_ASCII);
   }
 
+  /**
+   * @param input UUID as string
+   * @return bytearray
+   * returns bytes and null on exception
+   */
+  @ScalarFunction
+  public static byte[] toUUIDBytes(String input) {
+    try {
+      UUID uuid = UUID.fromString(input);
+      ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
+      bb.putLong(uuid.getMostSignificantBits());
+      bb.putLong(uuid.getLeastSignificantBits());
+      return bb.array();
+    } catch (IllegalArgumentException e) {
+      return null;
+    }
+  }
+
+  /**
+   * @param input UUID serialized to bytes
+   * @return String representation of UUID
+   * returns bytes and null on exception
+   */
+  @ScalarFunction
+  public static String fromUUIDBytes(byte[] input) {
+    ByteBuffer bb = ByteBuffer.wrap(input);
+    long firstLong = bb.getLong();
+    long secondLong = bb.getLong();
+    return new UUID(firstLong, secondLong).toString();
+  }
+
   /**
    * @see Normalizer#normalize(CharSequence, Normalizer.Form)
    * @param input


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org