dawidwys commented on code in PR #24704:
URL: https://github.com/apache/flink/pull/24704#discussion_r1610035745


##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/SqlJsonUtils.java:
##########
@@ -235,39 +251,85 @@ private static String jsonQuery(
                 }
             }
             if (value == null || context.mode == PathMode.LAX && 
isScalarObject(value)) {
-                switch (emptyBehavior) {
-                    case ERROR:
-                        throw emptyResultOfJsonQueryFuncNotAllowed();
-                    case NULL:
-                        return null;
-                    case EMPTY_ARRAY:
-                        return "[]";
-                    case EMPTY_OBJECT:
-                        return "{}";
-                    default:
-                        throw 
illegalEmptyBehaviorInJsonQueryFunc(emptyBehavior.toString());
-                }
+                return emptyResultForJsonQuery(emptyBehavior, returnType);
             } else if (context.mode == PathMode.STRICT && 
isScalarObject(value)) {
                 exc = 
arrayOrObjectValueRequiredInStrictModeOfJsonQueryFunc(value.toString());
             } else {
                 try {
-                    return jsonize(value);
+                    switch (returnType) {
+                        case STRING:
+                            return jsonize(value);
+                        case ARRAY:
+                            return new GenericArrayData(
+                                    ((List<Object>) value)
+                                            .stream()
+                                                    .map(
+                                                            o ->
+                                                                    o != null
+                                                                            ? 
StringData.fromString(
+                                                                               
     o.toString())
+                                                                            : 
null)
+                                                    
.toArray(StringData[]::new));
+                        default:
+                            throw new RuntimeException("illegal return type");
+                    }
                 } catch (Exception e) {
                     exc = e;
                 }
             }
         }
-        switch (errorBehavior) {
+        return errorResultForJsonQuery(errorBehavior, returnType, exc);
+    }
+
+    private static Object emptyResultForJsonQuery(
+            JsonQueryOnEmptyOrError emptyBehavior, JsonQueryReturnType 
returnType) {
+        switch (emptyBehavior) {
+            case ERROR:
+                throw emptyResultOfJsonQueryFuncNotAllowed();
+            case NULL:
+                return null;
+            case EMPTY_ARRAY:
+                switch (returnType) {
+                    case ARRAY:
+                        return new GenericArrayData(new StringData[0]);
+                    case STRING:
+                        return "[]";
+                    default:
+                        throw new RuntimeException("illegal return type");

Review Comment:
   Good point. The PR was created before the `TableRuntimeException` was 
introduced.



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

Reply via email to