From 53883aedb5155a7fce7a4acad5547c95724e6dc4 Mon Sep 17 00:00:00 2001 From: Malte Granderath Date: Tue, 14 Nov 2023 18:14:59 +0100 Subject: [PATCH] Adding byte functions for UUIDs (#11988) --- .../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 52442590e82..5a49314943b 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 @@ 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 static byte[] toAscii(String input) { 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