attilapiros commented on code in PR #53458:
URL: https://github.com/apache/spark/pull/53458#discussion_r2756413686


##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CastWithAnsiOnSuite.scala:
##########
@@ -900,4 +900,83 @@ class CastWithAnsiOnSuite extends CastSuiteBase with 
QueryErrorsBase {
       )
     }
   }
+
+  test("ANSI mode: cast invalid UTF-8 binary to string should throw error") {
+    checkExceptionInExpression[SparkRuntimeException](
+      cast(invalidUtf8Literal, StringType),
+      "CAST_INVALID_INPUT")
+
+    // Valid UTF-8 should work
+    checkEvaluation(cast(validUtf8Literal, StringType), 
UTF8String.fromString("Hello"))
+
+    // Empty binary should work
+    checkEvaluation(cast(emptyBinaryLiteral, StringType), 
UTF8String.fromString(""))
+  }
+
+  test("ANSI mode: cast invalid UTF-8 with validation disabled (old 
behavior)") {
+    withSQLConf(SQLConf.VALIDATE_BINARY_TO_STRING_CAST.key -> "false") {
+      // With validation disabled, invalid UTF-8 passes through (old behavior)
+      val result = cast(invalidUtf8Literal, StringType).eval()
+      assert(result != null, "Should not throw when validation is disabled")
+      assert(!result.asInstanceOf[UTF8String].isValid(),
+        "Result should contain invalid UTF-8")
+
+      // Valid UTF-8 should still work
+      checkEvaluation(cast(validUtf8Literal, StringType), 
UTF8String.fromString("Hello"))
+
+      // Empty binary should work
+      checkEvaluation(cast(emptyBinaryLiteral, StringType), 
UTF8String.fromString(""))
+    }
+  }
+
+  test("ANSI mode: cast array with invalid UTF-8 should throw") {
+    // Only run this test when evalMode is actually ANSI, not TRY
+    // TryCastSuite inherits from CastWithAnsiOnSuite but overrides evalMode 
to TRY
+    if (evalMode == EvalMode.ANSI) {

Review Comment:
   Let's be consistent with the existing tests:
   
   ```suggestion
       if (!isTryCast) {
   ```



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