Jackie-Jiang commented on code in PR #8535:
URL: https://github.com/apache/pinot/pull/8535#discussion_r849972261


##########
pinot-common/src/test/java/org/apache/pinot/common/function/scalar/DataTypeConversionFunctionsTest.java:
##########
@@ -0,0 +1,71 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.common.function.scalar;
+
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
+import static org.testng.Assert.assertEquals;
+
+
+public class DataTypeConversionFunctionsTest {
+
+  @DataProvider(name = "testCases")
+  public static Object[][] testCases() {
+    return new Object[][]{
+        {"a", "string", "a"},
+        {"10", "int", 10},
+        {"10", "long", 10L},
+        {"10", "float", 10F},
+        {"10", "double", 10D},
+        {"10.0", "int", 10},
+        {"10.0", "long", 10L},
+        {"10.0", "float", 10F},
+        {"10.0", "double", 10D},
+        {10, "string", "10"},
+        {10L, "string", "10"},
+        {10F, "string", "10.0"},
+        {10D, "string", "10.0"},
+        {"a", "string", "a"},
+        {"10", "int[]", new int[]{10}},
+        {"10", "long[]", new long[]{10L}},
+        {"10", "float[]", new float[]{10F}},
+        {"10", "double[]", new double[]{10D}},
+        {"10.0", "int[]", new int[]{10}},
+        {"10.0", "long[]", new long[]{10L}},
+        {"10.0", "float[]", new float[]{10F}},
+        {"10.0", "double[]", new double[]{10D}},
+        {10, "int", 10},
+        {10L, "long", 10L},
+        {10F, "float", 10F},
+        {10D, "double", 10D},
+        {10L, "int", 10},
+        {10, "long", 10L},
+        {10D, "float", 10F},
+        {10F, "double", 10D},
+        {"abc1", "bytes", new byte[]{(byte) 0xab, (byte) 0xc1}},
+        {new byte[]{(byte) 0xab, (byte) 0xc1}, "string", "abc1"}
+    };
+  }
+
+  @Test(dataProvider = "testCases")
+  public void test(Object value, String type, Object expected) {
+    assertEquals(DataTypeConversionFunctions.cast(value, type), expected);

Review Comment:
   We should also test the array type (primitive vs. boxed)



##########
pinot-common/src/main/java/org/apache/pinot/common/function/scalar/DataTypeConversionFunctions.java:
##########
@@ -32,6 +36,51 @@ public class DataTypeConversionFunctions {
   private DataTypeConversionFunctions() {
   }
 
+  @ScalarFunction(nullableParameters = true)
+  public static Object cast(Object value, String targetTypeLiteral) {
+    if (value == null || targetTypeLiteral == null) {
+      return value;
+    }
+    try {
+      Class<?> clazz = value.getClass();
+      boolean isMultiValue = clazz.isArray() & clazz != byte[].class;
+      PinotDataType sourceType = isMultiValue
+          ? PinotDataType.getMultiValueType(clazz)

Review Comment:
   We need to read the class for the first element in order to get the correct 
type. We might also need to differentiate the primitive value array and boxed 
value array (seems the input value is always primitive value array)



##########
pinot-common/src/main/java/org/apache/pinot/common/function/scalar/DataTypeConversionFunctions.java:
##########
@@ -32,6 +36,51 @@ public class DataTypeConversionFunctions {
   private DataTypeConversionFunctions() {
   }
 
+  @ScalarFunction(nullableParameters = true)
+  public static Object cast(Object value, String targetTypeLiteral) {
+    if (value == null || targetTypeLiteral == null) {
+      return value;
+    }

Review Comment:
   What I meant is to not allow null parameters and no need for the null check. 
When null parameters are not allowed, Pinot will return null when any argument 
is null, which is the expected behavior here (the only corner case is when user 
passes null target type, but not sure if we want to handle that by returning 
the value as is)
   ```suggestion
     @ScalarFunction
     public static Object cast(Object value, String targetTypeLiteral) {
   ```



##########
pinot-common/src/main/java/org/apache/pinot/common/function/scalar/DataTypeConversionFunctions.java:
##########
@@ -32,6 +36,51 @@ public class DataTypeConversionFunctions {
   private DataTypeConversionFunctions() {
   }
 
+  @ScalarFunction(nullableParameters = true)
+  public static Object cast(Object value, String targetTypeLiteral) {
+    if (value == null || targetTypeLiteral == null) {
+      return value;
+    }
+    try {
+      Class<?> clazz = value.getClass();
+      boolean isMultiValue = clazz.isArray() & clazz != byte[].class;
+      PinotDataType sourceType = isMultiValue
+          ? PinotDataType.getMultiValueType(clazz)
+          : PinotDataType.getSingleValueType(clazz);
+      PinotDataType targetDataType = 
convertTypeLiteralToDataType(targetTypeLiteral);
+      if (sourceType == STRING && EnumSet.of(INTEGER, LONG, INTEGER_ARRAY, 
LONG_ARRAY)
+          .contains(targetDataType)) {
+        if (String.valueOf(value).contains(".")) {
+          // convert integers via double to avoid parse errors
+          if (isMultiValue) {
+            return targetDataType.convert(DOUBLE_ARRAY.convert(value, 
sourceType), DOUBLE_ARRAY);
+          }
+          return targetDataType.convert(DOUBLE.convert(value, sourceType), 
DOUBLE);
+        }
+      }
+      return targetDataType.convert(value, sourceType);
+    } catch (IllegalArgumentException e) {
+      throw new IllegalArgumentException("Unknown data type: " + 
targetTypeLiteral);
+    }
+  }
+
+  private static PinotDataType convertTypeLiteralToDataType(String 
typeLiteral) {
+    String transformed = typeLiteral.toUpperCase();
+    if ("INT".equals(transformed)) {
+      return INTEGER;
+    }
+    if ("INT[]".equals(transformed)) {
+      return INTEGER_ARRAY;

Review Comment:
   The target data type should be primitive array



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


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

Reply via email to