snuyanzin commented on code in PR #29063:
URL: https://github.com/apache/flink/pull/29063#discussion_r3941253345


##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/SqlJsonUtils.java:
##########
@@ -815,6 +830,271 @@ public String toString() {
         }
     }
 
+    // --- Type conversion for JSON_VALUE / JSON_QUERY RETURNING ---
+
+    private static final java.util.EnumSet<LogicalTypeRoot> 
SUPPORTED_JSON_RETURNING_TYPES =
+            java.util.EnumSet.of(
+                    LogicalTypeRoot.BOOLEAN,
+                    LogicalTypeRoot.TINYINT,
+                    LogicalTypeRoot.SMALLINT,
+                    LogicalTypeRoot.INTEGER,
+                    LogicalTypeRoot.BIGINT,
+                    LogicalTypeRoot.FLOAT,
+                    LogicalTypeRoot.DOUBLE,
+                    LogicalTypeRoot.DECIMAL);
+
+    public static boolean isSupportedJsonReturningType(LogicalTypeRoot 
typeRoot) {
+        return SUPPORTED_JSON_RETURNING_TYPES.contains(typeRoot);
+    }
+
+    public static Object convertJsonScalar(
+            Object raw,
+            LogicalTypeRoot typeRoot,
+            int precision,
+            int scale,
+            JsonValueOnEmptyOrError errorBehavior,
+            Object defaultValue) {
+        if (raw == null) {
+            return null;
+        }
+        try {
+            return convertToType(raw, typeRoot, precision, scale);
+        } catch (JsonConversionException e) {
+            switch (errorBehavior) {
+                case NULL:
+                    return null;
+                case DEFAULT:
+                    return convertDefault(defaultValue, typeRoot, precision, 
scale);
+                case ERROR:
+                    throw new TableRuntimeException(
+                            "Cannot cast " + raw.getClass().getName() + " to " 
+ typeRoot, e);
+                default:
+                    throw new TableRuntimeException(
+                            "Unreachable: unknown error behavior " + 
errorBehavior);
+            }
+        }
+    }
+
+    public static GenericArrayData convertJsonArray(
+            Object rawResult,
+            LogicalTypeRoot elementTypeRoot,
+            int precision,
+            int scale,
+            boolean elementNullable,
+            JsonQueryOnEmptyOrError errorBehavior) {
+        if (rawResult == null) {
+            return null;
+        }
+        try {
+            Object[] rawArr = (Object[]) rawResult;
+            Object[] converted = new Object[rawArr.length];
+            for (int i = 0; i < rawArr.length; i++) {
+                if (rawArr[i] != null) {
+                    converted[i] = convertToType(rawArr[i], elementTypeRoot, 
precision, scale);
+                } else if (!elementNullable) {
+                    throw new JsonConversionException(
+                            "Null element at index " + i + " in NOT NULL 
array");
+                }
+            }
+            return new GenericArrayData(converted);
+        } catch (JsonConversionException e) {
+            switch (errorBehavior) {
+                case NULL:
+                    return null;
+                case EMPTY_ARRAY:
+                    return new GenericArrayData(new Object[0]);
+                case ERROR:
+                    throw new TableRuntimeException("Array element type 
mismatch in JSON_QUERY", e);
+                default:
+                    return null;
+            }
+        }
+    }
+
+    private static Object convertToType(
+            Object raw, LogicalTypeRoot typeRoot, int precision, int scale) {
+        if (raw instanceof StringData) {
+            return convertToType(raw.toString(), typeRoot, precision, scale);
+        }
+        if (raw instanceof String) {
+            if (typeRoot == LogicalTypeRoot.BOOLEAN) {
+                return parseStringAsBoolean((String) raw);
+            }
+            try {
+                String trimmed = ((String) raw).trim();
+                return convertToType(new BigDecimal(trimmed), typeRoot, 
precision, scale);
+            } catch (NumberFormatException e) {
+                throw new JsonConversionException(
+                        "Cannot parse string '" + raw + "' as " + typeRoot, e);
+            }
+        }
+        try {
+            switch (typeRoot) {
+                case BOOLEAN:
+                    if (raw instanceof Number) {
+                        int v = ((Number) raw).intValue();
+                        if (v == 0) return false;
+                        if (v == 1) return true;
+                        throw new JsonConversionException(
+                                "Cannot convert " + raw + " to BOOLEAN");

Review Comment:
   this might be an issue
   it makes returning true for multiple cases.
   e.g. 
   ```sql
    SELECT JSON_VALUE('{"a": 1.9}', '$.a' RETURNING BOOLEAN);
   ```
   however it should be `null` , because  default is `NULL ON ERROR`
   also postgres returns `null`
   
   it should return `true` only if it is 1
   ```sql
    SELECT JSON_VALUE('{"a": 1}', '$.a' RETURNING BOOLEAN);
   ```
   and false for 0
   ```sql
    SELECT JSON_VALUE('{"a": 0}', '$.a' RETURNING BOOLEAN);
   ```



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