@@ -1493,6 +1493,57 @@ class CometCastSuite extends CometTestBase with AdaptiveSparkPlanHelper {
14931493 }
14941494 }
14951495
1496+ test(" cast StructType with ArrayType field to StructType" ) {
1497+ testSingleLineQuery(
1498+ """
1499+ |SELECT named_struct(
1500+ | 'items', array(1, 2, cast(null as int)),
1501+ | 'label', 'first') AS s
1502+ |UNION ALL
1503+ |SELECT named_struct(
1504+ | 'items', cast(array() as array<int>),
1505+ | 'label', cast(null as string)) AS s
1506+ |UNION ALL
1507+ |SELECT cast(null as struct<items:array<int>,label:string>) AS s
1508+ |""" .stripMargin,
1509+ " SELECT CAST(s AS struct<items:array<string>,label:string>) FROM tbl" ,
1510+ testName = " cast_struct_array_field_to_struct" )
1511+ }
1512+
1513+ test(" cast deeply nested StructType to StructType and StringType" ) {
1514+ val input =
1515+ """
1516+ |SELECT named_struct(
1517+ | 'outer',
1518+ | named_struct(
1519+ | 'middle', named_struct('value', '1', 'flag', true),
1520+ | 'numbers', array('2', '3')),
1521+ | 'note', 'alpha') AS s
1522+ |UNION ALL
1523+ |SELECT named_struct(
1524+ | 'outer',
1525+ | named_struct(
1526+ | 'middle', named_struct('value', cast(null as string), 'flag', false),
1527+ | 'numbers', array(cast(null as string))),
1528+ | 'note', cast(null as string)) AS s
1529+ |UNION ALL
1530+ |SELECT cast(null as
1531+ | struct<outer:struct<middle:struct<value:string,flag:boolean>,numbers:array<string>>,
1532+ | note:string>) AS s
1533+ |""" .stripMargin
1534+
1535+ testSingleLineQuery(
1536+ input,
1537+ " SELECT CAST(s AS " +
1538+ " struct<outer:struct<middle:struct<value:int,flag:string>,numbers:array<int>>," +
1539+ " note:string>) FROM tbl" ,
1540+ testName = " cast_deep_struct_to_struct" )
1541+ testSingleLineQuery(
1542+ input,
1543+ " SELECT CAST(s AS string) FROM tbl" ,
1544+ testName = " cast_deep_struct_to_string" )
1545+ }
1546+
14961547 test(" cast between decimals with different precision and scale" ) {
14971548 val rowData = Seq (
14981549 Row (BigDecimal (" 12345.6789" )),
@@ -1607,6 +1658,23 @@ class CometCastSuite extends CometTestBase with AdaptiveSparkPlanHelper {
16071658 StringType )
16081659 }
16091660
1661+ test(" cast ArrayType(StructType) to StringType" ) {
1662+ testSingleLineQuery(
1663+ """
1664+ |SELECT array(
1665+ | named_struct('id', 1, 'score', '10'),
1666+ | named_struct('id', 2, 'score', cast(null as string))) AS a
1667+ |UNION ALL
1668+ |SELECT array(named_struct('id', cast(null as int), 'score', '30')) AS a
1669+ |UNION ALL
1670+ |SELECT cast(array() as array<struct<id:int,score:string>>) AS a
1671+ |UNION ALL
1672+ |SELECT cast(null as array<struct<id:int,score:string>>) AS a
1673+ |""" .stripMargin,
1674+ " SELECT CAST(a AS string) FROM tbl" ,
1675+ testName = " cast_array_struct_to_string" )
1676+ }
1677+
16101678 test(" cast ArrayType to ArrayType" ) {
16111679 val types = Seq (
16121680 BooleanType ,
@@ -1632,6 +1700,23 @@ class CometCastSuite extends CometTestBase with AdaptiveSparkPlanHelper {
16321700 testArrayCastMatrix(types, ArrayType (_), generateArrays(100 , _))
16331701 }
16341702
1703+ test(" cast ArrayType(StructType) to ArrayType(StructType)" ) {
1704+ testSingleLineQuery(
1705+ """
1706+ |SELECT array(
1707+ | named_struct('id', 1, 'score', '10'),
1708+ | named_struct('id', 2, 'score', cast(null as string))) AS a
1709+ |UNION ALL
1710+ |SELECT array(named_struct('id', cast(null as int), 'score', '30')) AS a
1711+ |UNION ALL
1712+ |SELECT cast(array() as array<struct<id:int,score:string>>) AS a
1713+ |UNION ALL
1714+ |SELECT cast(null as array<struct<id:int,score:string>>) AS a
1715+ |""" .stripMargin,
1716+ " SELECT CAST(a AS array<struct<id:bigint,score:int>>) FROM tbl" ,
1717+ testName = " cast_array_struct_to_array_struct" )
1718+ }
1719+
16351720 test(" cast ArrayType(DateType) to unsupported ArrayType falls back" ) {
16361721 val fromType = ArrayType (DateType )
16371722 val unsupportedElementTypes =
0 commit comments