Commit baf4b59
committed
[SPARK-51482][SQL] Support cast from string to time
### What changes were proposed in this pull request?
In the PR, I propose to support `CAST` from `STRING` to `TIME(n)`. The format of input strings should match to:
```
[h]h:[m]m:[s]s.[ms][ms][ms][us][us][us]
```
### Why are the changes needed?
Before the changes, such casting allowed by the SQL standard failed w/ the error:
```scala
scala> Seq("17:18:19").toDS.select($"value".cast(TimeType())).show()
org.apache.spark.sql.catalyst.ExtendedAnalysisException: [DATATYPE_MISMATCH.CAST_WITHOUT_SUGGESTION] Cannot resolve "CAST(value AS TIME(6))" due to data type mismatch: cannot cast "STRING" to "TIME(6)". SQLSTATE: 42K09;
```
### Does this PR introduce _any_ user-facing change?
Yes. After the changes, the cast above works as expected:
```scala
scala> Seq("17:18:19").toDS.select($"value".cast(TimeType())).show()
+--------+
| value|
+--------+
|17:18:19|
+--------+
```
### How was this patch tested?
By running the new tests:
```
$ build/sbt "test:testOnly *CastWithAnsiOffSuite"
$ build/sbt "test:testOnly *CastWithAnsiOnSuite"
```
### Was this patch authored or co-authored using generative AI tooling?
No.
Closes #50236 from MaxGekk/string-to-time.
Authored-by: Max Gekk <max.gekk@gmail.com>
Signed-off-by: Max Gekk <max.gekk@gmail.com>1 parent d87851f commit baf4b59
File tree
4 files changed
+67
-0
lines changed- sql/catalyst/src
- main/scala/org/apache/spark/sql/catalyst/expressions
- test/scala/org/apache/spark/sql/catalyst/expressions
4 files changed
+67
-0
lines changedLines changed: 41 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
119 | 119 | | |
120 | 120 | | |
121 | 121 | | |
| 122 | + | |
122 | 123 | | |
123 | 124 | | |
124 | 125 | | |
| |||
219 | 220 | | |
220 | 221 | | |
221 | 222 | | |
| 223 | + | |
222 | 224 | | |
223 | 225 | | |
224 | 226 | | |
| |||
727 | 729 | | |
728 | 730 | | |
729 | 731 | | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
730 | 741 | | |
731 | 742 | | |
732 | 743 | | |
| |||
1134 | 1145 | | |
1135 | 1146 | | |
1136 | 1147 | | |
| 1148 | + | |
1137 | 1149 | | |
1138 | 1150 | | |
1139 | 1151 | | |
| |||
1241 | 1253 | | |
1242 | 1254 | | |
1243 | 1255 | | |
| 1256 | + | |
1244 | 1257 | | |
1245 | 1258 | | |
1246 | 1259 | | |
| |||
1335 | 1348 | | |
1336 | 1349 | | |
1337 | 1350 | | |
| 1351 | + | |
| 1352 | + | |
| 1353 | + | |
| 1354 | + | |
| 1355 | + | |
| 1356 | + | |
| 1357 | + | |
| 1358 | + | |
| 1359 | + | |
| 1360 | + | |
| 1361 | + | |
| 1362 | + | |
| 1363 | + | |
| 1364 | + | |
| 1365 | + | |
| 1366 | + | |
| 1367 | + | |
| 1368 | + | |
| 1369 | + | |
| 1370 | + | |
| 1371 | + | |
| 1372 | + | |
| 1373 | + | |
| 1374 | + | |
| 1375 | + | |
| 1376 | + | |
| 1377 | + | |
| 1378 | + | |
1338 | 1379 | | |
1339 | 1380 | | |
1340 | 1381 | | |
| |||
Lines changed: 12 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1469 | 1469 | | |
1470 | 1470 | | |
1471 | 1471 | | |
| 1472 | + | |
| 1473 | + | |
| 1474 | + | |
| 1475 | + | |
| 1476 | + | |
| 1477 | + | |
| 1478 | + | |
| 1479 | + | |
| 1480 | + | |
| 1481 | + | |
| 1482 | + | |
| 1483 | + | |
1472 | 1484 | | |
Lines changed: 6 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
901 | 901 | | |
902 | 902 | | |
903 | 903 | | |
| 904 | + | |
| 905 | + | |
| 906 | + | |
| 907 | + | |
| 908 | + | |
| 909 | + | |
904 | 910 | | |
Lines changed: 8 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
737 | 737 | | |
738 | 738 | | |
739 | 739 | | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
740 | 748 | | |
0 commit comments