MaxGekk commented on code in PR #47522:
URL: https://github.com/apache/spark/pull/47522#discussion_r1740156972


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/objects/objects.scala:
##########
@@ -2057,14 +2055,17 @@ case class ValidateExternalType(child: Expression, 
expected: DataType, externalD
     if (checkType(input)) {
       input
     } else {
-      throw new RuntimeException(s"${input.getClass.getName}$errMsg")
+      throw new SparkRuntimeException("INVALID_EXTERNAL_TYPE",
+        Map(
+          "actualType" -> input.getClass.getName,
+          "expectedType" -> expected.simpleString,
+          "childExpression" -> child.toString

Review Comment:
   ```suggestion
             "childExpression" -> toSQLExpr(child)
   ```



##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ObjectExpressionsSuite.scala:
##########
@@ -556,6 +556,43 @@ class ObjectExpressionsSuite extends SparkFunSuite with 
ExpressionEvalHelper {
       "java.lang.Integer is not a valid external type for schema of double")
   }
 
+  test("SPARK-49044 ValidateExternalType should return child in error") {
+    val inputObject = BoundReference(0, ObjectType(classOf[Row]), nullable = 
true)
+    Seq(
+      (true, BooleanType),
+      (2.toByte, ByteType),
+      (5.toShort, ShortType),
+      (23, IntegerType),
+      (61L, LongType),
+      (1.0f, FloatType),
+      (10.0, DoubleType),
+      ("abcd".getBytes, BinaryType),
+      ("abcd", StringType),
+      (BigDecimal.valueOf(10), DecimalType.IntDecimal),
+      (IntervalUtils.stringToInterval(UTF8String.fromString("interval 3 day")),
+        CalendarIntervalType),
+      (java.math.BigDecimal.valueOf(10), DecimalType.BigIntDecimal),
+      (Array(3, 2, 1), ArrayType(IntegerType))
+    ).foreach { case (input, dt) =>
+      val enc = RowEncoder.encoderForDataType(dt, lenient = false)
+      val validateType = ValidateExternalType(
+        GetExternalRowField(inputObject, index = 0, fieldName = "c0"),
+        dt,
+        EncoderUtils.lenientExternalDataTypeFor(enc))
+      checkObjectExprEvaluation(validateType, input, 
InternalRow.fromSeq(Seq(Row(input))))
+    }
+
+    checkExceptionInExpression[SparkRuntimeException](
+      ValidateExternalType(

Review Comment:
   BTW, can you trigger the error from an user code because if you cannot do 
that, we should consider to convert the error to an internal one.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/objects/objects.scala:
##########
@@ -2057,14 +2055,17 @@ case class ValidateExternalType(child: Expression, 
expected: DataType, externalD
     if (checkType(input)) {
       input
     } else {
-      throw new RuntimeException(s"${input.getClass.getName}$errMsg")
+      throw new SparkRuntimeException("INVALID_EXTERNAL_TYPE",
+        Map(
+          "actualType" -> input.getClass.getName,
+          "expectedType" -> expected.simpleString,

Review Comment:
   Could you quote the expected type by `toSQLType()`, please.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/objects/objects.scala:
##########
@@ -2057,14 +2055,17 @@ case class ValidateExternalType(child: Expression, 
expected: DataType, externalD
     if (checkType(input)) {
       input
     } else {
-      throw new RuntimeException(s"${input.getClass.getName}$errMsg")
+      throw new SparkRuntimeException("INVALID_EXTERNAL_TYPE",

Review Comment:
   Please, create the exception in `QueryExecutionErrors`, and invoke the 
method from codegen and non-codegen.



##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ObjectExpressionsSuite.scala:
##########
@@ -556,6 +556,43 @@ class ObjectExpressionsSuite extends SparkFunSuite with 
ExpressionEvalHelper {
       "java.lang.Integer is not a valid external type for schema of double")
   }
 
+  test("SPARK-49044 ValidateExternalType should return child in error") {
+    val inputObject = BoundReference(0, ObjectType(classOf[Row]), nullable = 
true)
+    Seq(
+      (true, BooleanType),
+      (2.toByte, ByteType),
+      (5.toShort, ShortType),
+      (23, IntegerType),
+      (61L, LongType),
+      (1.0f, FloatType),
+      (10.0, DoubleType),
+      ("abcd".getBytes, BinaryType),
+      ("abcd", StringType),
+      (BigDecimal.valueOf(10), DecimalType.IntDecimal),
+      (IntervalUtils.stringToInterval(UTF8String.fromString("interval 3 day")),
+        CalendarIntervalType),
+      (java.math.BigDecimal.valueOf(10), DecimalType.BigIntDecimal),
+      (Array(3, 2, 1), ArrayType(IntegerType))
+    ).foreach { case (input, dt) =>
+      val enc = RowEncoder.encoderForDataType(dt, lenient = false)
+      val validateType = ValidateExternalType(
+        GetExternalRowField(inputObject, index = 0, fieldName = "c0"),
+        dt,
+        EncoderUtils.lenientExternalDataTypeFor(enc))
+      checkObjectExprEvaluation(validateType, input, 
InternalRow.fromSeq(Seq(Row(input))))
+    }
+
+    checkExceptionInExpression[SparkRuntimeException](

Review Comment:
   Please, use `checkErrorInExpression` for checking error class/condition and 
its parameters.



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