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


##########
pinot-core/src/test/java/org/apache/pinot/queries/TransformQueriesTest.java:
##########
@@ -282,4 +287,77 @@ public void testGroovyTransformQuery() {
   private void runAndVerifyInterSegmentQuery(String query, double 
expectedResult) {
     
assertEquals(getBrokerResponse(query).getResultTable().getRows().get(0)[0], 
expectedResult);
   }
+
+  @Test
+  public void testArrayLiteralQueries() {

Review Comment:
   MINOR: Could we split this method into focused tests for basic arrays, the 
alias, logical types, whitespace handling, and null rejection? It now covers 
all five scenarios, and separate tests would make failures easier to identify. 
This is an optional cleanup.



##########
pinot-core/src/main/java/org/apache/pinot/core/operator/transform/function/ArrayLiteralTransformFunction.java:
##########
@@ -134,86 +239,252 @@ public 
ArrayLiteralTransformFunction(List<ExpressionContext> literalContexts) {
       _longArrayLiteral = new long[0];
       _floatArrayLiteral = new float[0];
       _doubleArrayLiteral = new double[0];
+      _bigDecimalArrayLiteral = new BigDecimal[0];
       _stringArrayLiteral = new String[0];
       _bytesArrayLiteral = new byte[0][];
       return;
     }
+    List<LiteralContext> contexts = new ArrayList<>(literalContexts.size());
     for (ExpressionContext literalContext : literalContexts) {
-      Preconditions.checkState(literalContext.getType() == 
ExpressionContext.Type.LITERAL,
-          "ArrayLiteralTransformFunction only takes literals as arguments, 
found: %s", literalContext);
+      LiteralContext context = toLiteralContext(literalContext);
+      Preconditions.checkArgument(!context.isNull(), "Null element in array 
literal is not supported");

Review Comment:
   MINOR: The same null value is checked here, in `toLiteralContext`, and again 
in `castLiteralContext`. Keeping the check in `toLiteralContext` is sufficient 
for this path: it rejects bare nulls and null CAST sources before any value 
getters run. We could remove this check and the redundant check at the start of 
`castLiteralContext`; the separate checks for array-backed inputs should stay.



##########
pinot-core/src/main/java/org/apache/pinot/core/operator/transform/function/ArrayLiteralTransformFunction.java:
##########
@@ -134,86 +239,252 @@ public 
ArrayLiteralTransformFunction(List<ExpressionContext> literalContexts) {
       _longArrayLiteral = new long[0];
       _floatArrayLiteral = new float[0];
       _doubleArrayLiteral = new double[0];
+      _bigDecimalArrayLiteral = new BigDecimal[0];
       _stringArrayLiteral = new String[0];
       _bytesArrayLiteral = new byte[0][];
       return;
     }
+    List<LiteralContext> contexts = new ArrayList<>(literalContexts.size());
     for (ExpressionContext literalContext : literalContexts) {
-      Preconditions.checkState(literalContext.getType() == 
ExpressionContext.Type.LITERAL,
-          "ArrayLiteralTransformFunction only takes literals as arguments, 
found: %s", literalContext);
+      LiteralContext context = toLiteralContext(literalContext);
+      Preconditions.checkArgument(!context.isNull(), "Null element in array 
literal is not supported");
+      contexts.add(context);
     }
-    _dataType = literalContexts.get(0).getLiteral().getType();
+    _dataType = contexts.get(0).getType();
     switch (_dataType) {
       case INT:
-        _intArrayLiteral = new int[literalContexts.size()];
+        _intArrayLiteral = new int[contexts.size()];
         for (int i = 0; i < _intArrayLiteral.length; i++) {
-          _intArrayLiteral[i] = 
literalContexts.get(i).getLiteral().getIntValue();
+          _intArrayLiteral[i] = contexts.get(i).getIntValue();
         }
         _longArrayLiteral = null;
         _floatArrayLiteral = null;
         _doubleArrayLiteral = null;
+        _bigDecimalArrayLiteral = null;
         _stringArrayLiteral = null;
         _bytesArrayLiteral = null;
         break;
       case LONG:
-        _longArrayLiteral = new long[literalContexts.size()];
+        _longArrayLiteral = new long[contexts.size()];
         for (int i = 0; i < _longArrayLiteral.length; i++) {
-          _longArrayLiteral[i] = 
literalContexts.get(i).getLiteral().getLongValue();
+          _longArrayLiteral[i] = contexts.get(i).getLongValue();
         }
         _intArrayLiteral = null;
         _floatArrayLiteral = null;
         _doubleArrayLiteral = null;
+        _bigDecimalArrayLiteral = null;
         _stringArrayLiteral = null;
         _bytesArrayLiteral = null;
         break;
       case FLOAT:
-        _floatArrayLiteral = new float[literalContexts.size()];
+        _floatArrayLiteral = new float[contexts.size()];
         for (int i = 0; i < _floatArrayLiteral.length; i++) {
-          _floatArrayLiteral[i] = 
literalContexts.get(i).getLiteral().getFloatValue();
+          _floatArrayLiteral[i] = contexts.get(i).getFloatValue();
         }
         _intArrayLiteral = null;
         _longArrayLiteral = null;
         _doubleArrayLiteral = null;
+        _bigDecimalArrayLiteral = null;
         _stringArrayLiteral = null;
         _bytesArrayLiteral = null;
         break;
       case DOUBLE:
-        _doubleArrayLiteral = new double[literalContexts.size()];
+        _doubleArrayLiteral = new double[contexts.size()];
         for (int i = 0; i < _doubleArrayLiteral.length; i++) {
-          _doubleArrayLiteral[i] = 
literalContexts.get(i).getLiteral().getDoubleValue();
+          _doubleArrayLiteral[i] = contexts.get(i).getDoubleValue();
         }
         _intArrayLiteral = null;
         _longArrayLiteral = null;
         _floatArrayLiteral = null;
+        _bigDecimalArrayLiteral = null;
+        _stringArrayLiteral = null;
+        _bytesArrayLiteral = null;
+        break;
+      case BIG_DECIMAL:
+        _bigDecimalArrayLiteral = new BigDecimal[contexts.size()];
+        for (int i = 0; i < _bigDecimalArrayLiteral.length; i++) {
+          _bigDecimalArrayLiteral[i] = contexts.get(i).getBigDecimalValue();
+        }
+        _intArrayLiteral = null;
+        _longArrayLiteral = null;
+        _floatArrayLiteral = null;
+        _doubleArrayLiteral = null;
+        _stringArrayLiteral = null;
+        _bytesArrayLiteral = null;
+        break;
+      case BOOLEAN:
+        _intArrayLiteral = new int[contexts.size()];
+        for (int i = 0; i < _intArrayLiteral.length; i++) {
+          _intArrayLiteral[i] = contexts.get(i).getBooleanValue() ? 1 : 0;
+        }
+        _longArrayLiteral = null;
+        _floatArrayLiteral = null;
+        _doubleArrayLiteral = null;
+        _bigDecimalArrayLiteral = null;
+        _stringArrayLiteral = null;
+        _bytesArrayLiteral = null;
+        break;
+      case TIMESTAMP:
+        _longArrayLiteral = new long[contexts.size()];
+        for (int i = 0; i < _longArrayLiteral.length; i++) {
+          _longArrayLiteral[i] = contexts.get(i).getLongValue();
+        }
+        _intArrayLiteral = null;
+        _floatArrayLiteral = null;
+        _doubleArrayLiteral = null;
+        _bigDecimalArrayLiteral = null;
         _stringArrayLiteral = null;
         _bytesArrayLiteral = null;
         break;
       case STRING:
-        _stringArrayLiteral = new String[literalContexts.size()];
+        _stringArrayLiteral = new String[contexts.size()];
         for (int i = 0; i < _stringArrayLiteral.length; i++) {
-          _stringArrayLiteral[i] = 
literalContexts.get(i).getLiteral().getStringValue();
+          _stringArrayLiteral[i] = contexts.get(i).getStringValue();
         }
         _intArrayLiteral = null;
         _longArrayLiteral = null;
         _floatArrayLiteral = null;
         _doubleArrayLiteral = null;
+        _bigDecimalArrayLiteral = null;
         _bytesArrayLiteral = null;
         break;
       case BYTES:
-        _bytesArrayLiteral = new byte[literalContexts.size()][];
+      case UUID:
+        _bytesArrayLiteral = new byte[contexts.size()][];
         for (int i = 0; i < _bytesArrayLiteral.length; i++) {
-          _bytesArrayLiteral[i] = 
literalContexts.get(i).getLiteral().getBytesValue();
+          _bytesArrayLiteral[i] = contexts.get(i).getBytesValue();
         }
         _intArrayLiteral = null;
         _longArrayLiteral = null;
         _floatArrayLiteral = null;
         _doubleArrayLiteral = null;
+        _bigDecimalArrayLiteral = null;
         _stringArrayLiteral = null;
         break;
       default:
         throw new IllegalStateException(
             "Illegal data type for ArrayLiteralTransformFunction: " + 
_dataType + ", literal contexts: "
-                + Arrays.toString(literalContexts.toArray()));
+                + Arrays.toString(contexts.toArray()));
+    }
+  }
+
+  private static LiteralContext toLiteralContext(ExpressionContext context) {
+    if (context.getType() == ExpressionContext.Type.LITERAL) {
+      LiteralContext literal = context.getLiteral();
+      Preconditions.checkArgument(!literal.isNull(), "Null element in array 
literal is not supported");
+      return literal;
+    }
+    if (context.getType() == ExpressionContext.Type.FUNCTION) {
+      FunctionContext function = context.getFunction();
+      if (function.getFunctionName().equalsIgnoreCase("cast")) {
+        List<ExpressionContext> arguments = function.getArguments();
+        Preconditions.checkArgument(arguments.size() == 2,
+            "CAST function inside array literal constructor expects 2 
arguments, got: %s", arguments.size());
+        LiteralContext sourceLiteral = toLiteralContext(arguments.get(0));
+        ExpressionContext targetTypeExpr = arguments.get(1);
+        Preconditions.checkArgument(targetTypeExpr.getType() == 
ExpressionContext.Type.LITERAL,
+            "CAST target type must be a literal string, got: %s", 
targetTypeExpr);
+        String targetType = 
targetTypeExpr.getLiteral().getStringValue().toUpperCase();
+        return castLiteralContext(sourceLiteral, targetType);
+      }
+    }
+    throw new IllegalStateException(
+        "ArrayLiteralTransformFunction only takes literals or cast functions 
as arguments, found: " + context);
+  }
+
+  private static LiteralContext castLiteralContext(LiteralContext source, 
String targetType) {

Review Comment:
   MINOR: Could we reuse the shared CAST conversion logic from 
`DataTypeConversionFunctions.cast` where possible, keeping logical-type 
preservation in this helper? This switch duplicates the existing conversions, 
which makes it easier for their behavior to diverge again, as happened with 
whitespace handling. This is an optional cleanup.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to