Commit 270327d
[SPARK-49044][SQL] ValidateExternalType should return child in error
### What changes were proposed in this pull request?
In this PR was an extended error message for checkType in ValidateExternalType with information about child nodes.
### Why are the changes needed?
When we have mixed schema rows, the error message "{actual} is not a valid external type for schema of {expected}" doesn't help to understand the column with the problem. I suggest adding information about the source column.
After fix error message may contain extra info
```
The external type [B is not valid for the type "STRING" at the expression "getexternalrowfield(assertnotnull(input[0, org.apache.spark.sql.Row, true]), 0, f3)".
```
#### Example
```scala
class ErrorMsgSuite extends AnyFunSuite with SharedSparkContext {
test("shouldThrowSchemaError") {
val seq: Seq[Row] = Seq(
Row(
toBytes("0"),
toBytes(""),
1L,
),
Row(
toBytes("0"),
toBytes(""),
1L,
),
) val schema: StructType = new StructType()
.add("f1", BinaryType)
.add("f3", StringType)
.add("f2", LongType) val df = sqlContext.createDataFrame(sqlContext.sparkContext.parallelize(seq), schema) val exception = intercept[RuntimeException] {
df.show()
} assert(
exception.getCause.getMessage
.contains("[B is not a valid external type for schema of string")
)
assertResult(
"[B is not a valid external type for schema of string"
)(exception.getCause.getMessage)
}
def toBytes(x: String): Array[Byte] = x.toCharArray.map(_.toByte)
}
```
After fix error message may contain extra info
```
The external type [B is not valid for the type "STRING" at the expression "getexternalrowfield(assertnotnull(input[0, org.apache.spark.sql.Row, true]), 0, f3)".
```
Example: https://github.com/mrk-andreev/example-spark-schema/blob/main/spark_4.0.0/src/test/scala/ErrorMsgSuite.scala
### Does this PR introduce _any_ user-facing change?
This changes extends error message in case of "not a valid external type".
#### Example before
```
java.lang.Integer is not a valid external type for schema of double
```
#### Example after
```
The external type [B is not valid for the type "STRING" at the expression "getexternalrowfield(assertnotnull(input[0, org.apache.spark.sql.Row, true]), 0, c0)".
```
### How was this patch tested?
This changes covered by new unit test `test("SPARK-49044 ValidateExternalType should return child in error")` and by new integration test `test("SPARK-49044 ValidateExternalType should be user visible")`
### Was this patch authored or co-authored using generative AI tooling?
No
Closes #47522 from mrk-andreev/improve-error-for-cast-v2.
Authored-by: Mark Andreev <mark.andreev@gmail.com>
Signed-off-by: Max Gekk <max.gekk@gmail.com>1 parent 2a6080c commit 270327d
File tree
7 files changed
+133
-13
lines changed- common/utils/src/main/resources/error
- sql
- catalyst/src
- main/scala/org/apache/spark/sql
- catalyst/expressions/objects
- errors
- test/scala/org/apache/spark/sql/catalyst
- encoders
- expressions
- core/src/test/scala/org/apache/spark/sql/catalyst/expressions
7 files changed
+133
-13
lines changedLines changed: 6 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2177 | 2177 | | |
2178 | 2178 | | |
2179 | 2179 | | |
| 2180 | + | |
| 2181 | + | |
| 2182 | + | |
| 2183 | + | |
| 2184 | + | |
| 2185 | + | |
2180 | 2186 | | |
2181 | 2187 | | |
2182 | 2188 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4625 | 4625 | | |
4626 | 4626 | | |
4627 | 4627 | | |
| 4628 | + | |
| 4629 | + | |
| 4630 | + | |
| 4631 | + | |
| 4632 | + | |
| 4633 | + | |
4628 | 4634 | | |
4629 | 4635 | | |
4630 | 4636 | | |
| |||
Lines changed: 11 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2023 | 2023 | | |
2024 | 2024 | | |
2025 | 2025 | | |
2026 | | - | |
2027 | | - | |
2028 | 2026 | | |
2029 | 2027 | | |
2030 | 2028 | | |
| |||
2057 | 2055 | | |
2058 | 2056 | | |
2059 | 2057 | | |
2060 | | - | |
| 2058 | + | |
| 2059 | + | |
2061 | 2060 | | |
2062 | 2061 | | |
2063 | 2062 | | |
2064 | 2063 | | |
2065 | | - | |
2066 | | - | |
2067 | | - | |
2068 | 2064 | | |
2069 | 2065 | | |
2070 | 2066 | | |
| |||
2090 | 2086 | | |
2091 | 2087 | | |
2092 | 2088 | | |
| 2089 | + | |
| 2090 | + | |
| 2091 | + | |
| 2092 | + | |
| 2093 | + | |
| 2094 | + | |
| 2095 | + | |
2093 | 2096 | | |
2094 | 2097 | | |
2095 | 2098 | | |
2096 | 2099 | | |
2097 | 2100 | | |
2098 | 2101 | | |
2099 | 2102 | | |
2100 | | - | |
| 2103 | + | |
| 2104 | + | |
2101 | 2105 | | |
2102 | 2106 | | |
2103 | 2107 | | |
| |||
Lines changed: 14 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
478 | 478 | | |
479 | 479 | | |
480 | 480 | | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
481 | 495 | | |
482 | 496 | | |
483 | 497 | | |
| |||
Lines changed: 8 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
288 | 288 | | |
289 | 289 | | |
290 | 290 | | |
291 | | - | |
| 291 | + | |
| 292 | + | |
292 | 293 | | |
293 | 294 | | |
294 | 295 | | |
295 | 296 | | |
296 | 297 | | |
297 | 298 | | |
298 | | - | |
| 299 | + | |
| 300 | + | |
299 | 301 | | |
300 | 302 | | |
301 | 303 | | |
302 | 304 | | |
303 | 305 | | |
304 | 306 | | |
305 | 307 | | |
306 | | - | |
| 308 | + | |
| 309 | + | |
307 | 310 | | |
308 | 311 | | |
309 | 312 | | |
310 | 313 | | |
311 | 314 | | |
312 | 315 | | |
313 | | - | |
| 316 | + | |
| 317 | + | |
314 | 318 | | |
315 | 319 | | |
316 | 320 | | |
| |||
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ObjectExpressionsSuite.scala
Lines changed: 44 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
547 | 547 | | |
548 | 548 | | |
549 | 549 | | |
550 | | - | |
| 550 | + | |
551 | 551 | | |
552 | 552 | | |
553 | 553 | | |
554 | 554 | | |
555 | 555 | | |
556 | | - | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
557 | 599 | | |
558 | 600 | | |
559 | 601 | | |
| |||
Lines changed: 44 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
0 commit comments