raminqaf commented on code in PR #28758:
URL: https://github.com/apache/flink/pull/28758#discussion_r3613433686


##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/CastFunctionITCase.java:
##########
@@ -151,35 +151,47 @@ private static List<TestSetSpec> variantCasts() {
                                 "CAST(PARSE_JSON('42') AS INT)",
                                 42,
                                 INT().notNull())
-                        // Integer overflow wraps around (Java narrowing), 
like a regular numeric
-                        // cast.
+                        // Spark-style overflow: an integer value outside the 
target range fails
+                        // CAST and returns NULL for TRY_CAST, instead of 
wrapping around.
+                        .testTableApiRuntimeError(
+                                call("PARSE_JSON", "40000").cast(SMALLINT()), 
"overflowed")
+                        .testSqlRuntimeError("CAST(PARSE_JSON('40000') AS 
SMALLINT)", "overflowed")
                         .testResult(
-                                call("PARSE_JSON", "40000").cast(SMALLINT()),
-                                "CAST(PARSE_JSON('40000') AS SMALLINT)",
-                                (short) -25536,
-                                SMALLINT().notNull())
-                        .testResult(
-                                call("PARSE_JSON", "128").cast(TINYINT()),
-                                "CAST(PARSE_JSON('128') AS TINYINT)",
-                                (byte) -128,
-                                TINYINT().notNull())
+                                call("PARSE_JSON", 
"40000").tryCast(SMALLINT()),
+                                "TRY_CAST(PARSE_JSON('40000') AS SMALLINT)",
+                                null,
+                                SMALLINT())
+                        .testTableApiRuntimeError(
+                                call("PARSE_JSON", "128").cast(TINYINT()), 
"overflowed")

Review Comment:
   The idea here is that the user first casts (converts) the value from variant 
to the actual type it has and then performs an explicit cast if they want too. 
In this case the 128 is an `INT` so the user first needs to cast it `INT` and 
then if they want to explicitly cast to `TINYINT`. 



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