15
15
// specific language governing permissions and limitations
16
16
// under the License.
17
17
18
- use arrow_schema:: * ;
18
+ use arrow_schema:: { DataType , Field , Schema } ;
19
19
use datafusion_common:: { assert_contains, DFSchema , DFSchemaRef , Result , TableReference } ;
20
20
use datafusion_expr:: test:: function_stub:: {
21
21
count_udaf, max_udaf, min_udaf, sum, sum_udaf,
22
22
} ;
23
23
use datafusion_expr:: {
24
- col, lit, table_scan, wildcard, Expr , Extension , LogicalPlan , LogicalPlanBuilder ,
25
- UserDefinedLogicalNode , UserDefinedLogicalNodeCore ,
24
+ col, lit, table_scan, wildcard, EmptyRelation , Expr , Extension , LogicalPlan ,
25
+ LogicalPlanBuilder , Union , UserDefinedLogicalNode , UserDefinedLogicalNodeCore ,
26
26
} ;
27
27
use datafusion_functions:: unicode;
28
28
use datafusion_functions_aggregate:: grouping:: grouping_udaf;
@@ -42,7 +42,7 @@ use std::{fmt, vec};
42
42
43
43
use crate :: common:: { MockContextProvider , MockSessionState } ;
44
44
use datafusion_expr:: builder:: {
45
- table_scan_with_filter_and_fetch, table_scan_with_filters,
45
+ project , table_scan_with_filter_and_fetch, table_scan_with_filters,
46
46
} ;
47
47
use datafusion_functions:: core:: planner:: CoreFunctionPlanner ;
48
48
use datafusion_functions_nested:: extract:: array_element_udf;
@@ -1615,3 +1615,51 @@ fn test_unparse_extension_to_sql() -> Result<()> {
1615
1615
}
1616
1616
Ok ( ( ) )
1617
1617
}
1618
+
1619
+ #[ test]
1620
+ fn test_unparse_optimized_multi_union ( ) -> Result < ( ) > {
1621
+ let unparser = Unparser :: default ( ) ;
1622
+
1623
+ let schema = Schema :: new ( vec ! [
1624
+ Field :: new( "x" , DataType :: Int32 , false ) ,
1625
+ Field :: new( "y" , DataType :: Utf8 , false ) ,
1626
+ ] ) ;
1627
+
1628
+ let dfschema = Arc :: new ( DFSchema :: try_from ( schema) ?) ;
1629
+
1630
+ let empty = LogicalPlan :: EmptyRelation ( EmptyRelation {
1631
+ produce_one_row : true ,
1632
+ schema : dfschema. clone ( ) ,
1633
+ } ) ;
1634
+
1635
+ let plan = LogicalPlan :: Union ( Union {
1636
+ inputs : vec ! [
1637
+ project( empty. clone( ) , vec![ lit( 1 ) . alias( "x" ) , lit( "a" ) . alias( "y" ) ] ) ?. into( ) ,
1638
+ project( empty. clone( ) , vec![ lit( 1 ) . alias( "x" ) , lit( "b" ) . alias( "y" ) ] ) ?. into( ) ,
1639
+ project( empty. clone( ) , vec![ lit( 2 ) . alias( "x" ) , lit( "a" ) . alias( "y" ) ] ) ?. into( ) ,
1640
+ project( empty. clone( ) , vec![ lit( 2 ) . alias( "x" ) , lit( "c" ) . alias( "y" ) ] ) ?. into( ) ,
1641
+ ] ,
1642
+ schema : dfschema. clone ( ) ,
1643
+ } ) ;
1644
+
1645
+ let sql = "SELECT 1 AS x, 'a' AS y UNION ALL SELECT 1 AS x, 'b' AS y UNION ALL SELECT 2 AS x, 'a' AS y UNION ALL SELECT 2 AS x, 'c' AS y" ;
1646
+
1647
+ assert_eq ! ( unparser. plan_to_sql( & plan) ?. to_string( ) , sql) ;
1648
+
1649
+ let plan = LogicalPlan :: Union ( Union {
1650
+ inputs : vec ! [ project(
1651
+ empty. clone( ) ,
1652
+ vec![ lit( 1 ) . alias( "x" ) , lit( "a" ) . alias( "y" ) ] ,
1653
+ ) ?
1654
+ . into( ) ] ,
1655
+ schema : dfschema. clone ( ) ,
1656
+ } ) ;
1657
+
1658
+ if let Some ( err) = plan_to_sql ( & plan) . err ( ) {
1659
+ assert_contains ! ( err. to_string( ) , "UNION operator requires at least 2 inputs" ) ;
1660
+ } else {
1661
+ panic ! ( "Expected error" )
1662
+ }
1663
+
1664
+ Ok ( ( ) )
1665
+ }
0 commit comments