This is an automated email from the ASF dual-hosted git repository.
xiangfu 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 b4c46ef0b8 Add hex decimal to long scalar functions (#14435)
b4c46ef0b8 is described below
commit b4c46ef0b8469cdc1d2b231eaccffafc9b920f8f
Author: Xiang Fu <[email protected]>
AuthorDate: Wed Nov 13 13:24:40 2024 -0800
Add hex decimal to long scalar functions (#14435)
---
.../scalar/DataTypeConversionFunctions.java | 23 ++++++++
.../scalar/DataTypeConversionFunctionsTest.java | 61 ++++++++++++++++++++++
2 files changed, 84 insertions(+)
diff --git
a/pinot-common/src/main/java/org/apache/pinot/common/function/scalar/DataTypeConversionFunctions.java
b/pinot-common/src/main/java/org/apache/pinot/common/function/scalar/DataTypeConversionFunctions.java
index c74325017e..46cca542e8 100644
---
a/pinot-common/src/main/java/org/apache/pinot/common/function/scalar/DataTypeConversionFunctions.java
+++
b/pinot-common/src/main/java/org/apache/pinot/common/function/scalar/DataTypeConversionFunctions.java
@@ -129,4 +129,27 @@ public class DataTypeConversionFunctions {
public static byte[] base64Decode(byte[] input) {
return Base64.getDecoder().decode(input);
}
+
+ /**
+ * Converts a hex string to the corresponded long value.
+ * @param input hex decimal string
+ * @return decoded long value
+ */
+ @ScalarFunction
+ public static long hexDecimalToLong(String input) {
+ if (input.startsWith("0x")) {
+ return Long.parseLong(input, 2, input.length(), 16);
+ }
+ return Long.parseLong(input, 16);
+ }
+
+ /**
+ * Converts a long value to corresponded hex decimal string
+ * @param input long value
+ * @return encoded hex decimal string
+ */
+ @ScalarFunction
+ public static String longToHexDecimal(long input) {
+ return Long.toHexString(input);
+ }
}
diff --git
a/pinot-common/src/test/java/org/apache/pinot/common/function/scalar/DataTypeConversionFunctionsTest.java
b/pinot-common/src/test/java/org/apache/pinot/common/function/scalar/DataTypeConversionFunctionsTest.java
index 981e39e2ad..a4fa2a4439 100644
---
a/pinot-common/src/test/java/org/apache/pinot/common/function/scalar/DataTypeConversionFunctionsTest.java
+++
b/pinot-common/src/test/java/org/apache/pinot/common/function/scalar/DataTypeConversionFunctionsTest.java
@@ -21,6 +21,8 @@ package org.apache.pinot.common.function.scalar;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
+import static
org.apache.pinot.common.function.scalar.DataTypeConversionFunctions.hexDecimalToLong;
+import static
org.apache.pinot.common.function.scalar.DataTypeConversionFunctions.longToHexDecimal;
import static org.testng.Assert.assertEquals;
@@ -60,4 +62,63 @@ public class DataTypeConversionFunctionsTest {
public void test(Object value, String type, Object expected) {
assertEquals(DataTypeConversionFunctions.cast(value, type), expected);
}
+
+ @Test
+ public void testHexDecimalToLong() {
+ assertEquals(hexDecimalToLong("0"), 0L);
+ assertEquals(hexDecimalToLong("1"), 1L);
+ assertEquals(hexDecimalToLong("10"), 16L);
+ assertEquals(hexDecimalToLong("100"), 256L);
+ assertEquals(hexDecimalToLong("1000"), 4096L);
+ assertEquals(hexDecimalToLong("10000"), 65536L);
+ assertEquals(hexDecimalToLong("100000"), 1048576L);
+ assertEquals(hexDecimalToLong("1000000"), 16777216L);
+ assertEquals(hexDecimalToLong("10000000"), 268435456L);
+ assertEquals(hexDecimalToLong("100000000"), 4294967296L);
+ assertEquals(hexDecimalToLong("1000000000"), 68719476736L);
+ assertEquals(hexDecimalToLong("10000000000"), 1099511627776L);
+ assertEquals(hexDecimalToLong("100000000000"), 17592186044416L);
+ assertEquals(hexDecimalToLong("1000000000000"), 281474976710656L);
+ assertEquals(hexDecimalToLong("10000000000000"), 4503599627370496L);
+ assertEquals(hexDecimalToLong("100000000000000"), 72057594037927936L);
+ assertEquals(hexDecimalToLong("1000000000000000"), 1152921504606846976L);
+ assertEquals(hexDecimalToLong("0x0"), 0L);
+ assertEquals(hexDecimalToLong("0x1"), 1L);
+ assertEquals(hexDecimalToLong("0x10"), 16L);
+ assertEquals(hexDecimalToLong("0x100"), 256L);
+ assertEquals(hexDecimalToLong("0x1000"), 4096L);
+ assertEquals(hexDecimalToLong("0x10000"), 65536L);
+ assertEquals(hexDecimalToLong("0x100000"), 1048576L);
+ assertEquals(hexDecimalToLong("0x1000000"), 16777216L);
+ assertEquals(hexDecimalToLong("0x10000000"), 268435456L);
+ assertEquals(hexDecimalToLong("0x100000000"), 4294967296L);
+ assertEquals(hexDecimalToLong("0x1000000000"), 68719476736L);
+ assertEquals(hexDecimalToLong("0x10000000000"), 1099511627776L);
+ assertEquals(hexDecimalToLong("0x100000000000"), 17592186044416L);
+ assertEquals(hexDecimalToLong("0x1000000000000"), 281474976710656L);
+ assertEquals(hexDecimalToLong("0x10000000000000"), 4503599627370496L);
+ assertEquals(hexDecimalToLong("0x100000000000000"), 72057594037927936L);
+ assertEquals(hexDecimalToLong("0x1000000000000000"), 1152921504606846976L);
+ }
+
+ @Test
+ public void testLongToHexDecimal() {
+ assertEquals(longToHexDecimal(0L), "0");
+ assertEquals(longToHexDecimal(1L), "1");
+ assertEquals(longToHexDecimal(16L), "10");
+ assertEquals(longToHexDecimal(256L), "100");
+ assertEquals(longToHexDecimal(4096L), "1000");
+ assertEquals(longToHexDecimal(65536L), "10000");
+ assertEquals(longToHexDecimal(1048576L), "100000");
+ assertEquals(longToHexDecimal(16777216L), "1000000");
+ assertEquals(longToHexDecimal(268435456L), "10000000");
+ assertEquals(longToHexDecimal(4294967296L), "100000000");
+ assertEquals(longToHexDecimal(68719476736L), "1000000000");
+ assertEquals(longToHexDecimal(1099511627776L), "10000000000");
+ assertEquals(longToHexDecimal(17592186044416L), "100000000000");
+ assertEquals(longToHexDecimal(281474976710656L), "1000000000000");
+ assertEquals(longToHexDecimal(4503599627370496L), "10000000000000");
+ assertEquals(longToHexDecimal(72057594037927936L), "100000000000000");
+ assertEquals(longToHexDecimal(1152921504606846976L), "1000000000000000");
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]