richardstartin commented on code in PR #8535:
URL: https://github.com/apache/pinot/pull/8535#discussion_r850189527


##########
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:
   I actually hadn't noticed there was also `PRIMITIVE_INTEGER_ARRAY` - let's 
just drop MV support to make this possible. Not sure how a user would even 
express an MV literal (though there could be compile time functions which 
produce MV values).



-- 
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