@@ -52,7 +52,11 @@ use arrow::datatypes::{DataType, Field, Fields, Schema, SchemaRef};
52
52
use datafusion_common:: display:: ToStringifiedPlan ;
53
53
use datafusion_common:: file_options:: file_type:: FileType ;
54
54
use datafusion_common:: {
55
- exec_err, get_target_functional_dependencies, internal_err, not_impl_err, plan_datafusion_err, plan_err, Column , DFSchema , DFSchemaRef , DataFusionError , FieldsSpans , FunctionalDependencies , Result , ScalarValue , TableReference , ToDFSchema , UnnestOptions
55
+ exec_err, get_target_functional_dependencies, internal_err, not_impl_err,
56
+ plan_datafusion_err, plan_err, Column , DFSchema , DFSchemaRef , DataFusionError ,
57
+ Diagnostic , DiagnosticEntry , DiagnosticEntryKind , FieldsSpans ,
58
+ FunctionalDependencies , Result , ScalarValue , TableReference , ToDFSchema ,
59
+ UnnestOptions ,
56
60
} ;
57
61
use datafusion_expr_common:: type_coercion:: binary:: type_union_resolution;
58
62
@@ -1436,9 +1440,12 @@ pub fn build_join_schema(
1436
1440
join_type,
1437
1441
left. fields ( ) . len ( ) ,
1438
1442
) ;
1439
- let left_fields_spans: FieldsSpans = left. iter ( ) . map ( |( _, _, spans) | spans) . cloned ( ) . collect ( ) ;
1440
- let right_fields_spans: FieldsSpans = right. iter ( ) . map ( |( _, _, spans) | spans) . cloned ( ) . collect ( ) ;
1441
- let fields_spans = left_fields_spans. join ( & right_fields_spans, join_type, left. fields ( ) . len ( ) ) ;
1443
+ let left_fields_spans: FieldsSpans =
1444
+ left. iter ( ) . map ( |( _, _, spans) | spans) . cloned ( ) . collect ( ) ;
1445
+ let right_fields_spans: FieldsSpans =
1446
+ right. iter ( ) . map ( |( _, _, spans) | spans) . cloned ( ) . collect ( ) ;
1447
+ let fields_spans =
1448
+ left_fields_spans. join ( & right_fields_spans, join_type, left. fields ( ) . len ( ) ) ;
1442
1449
let metadata = left
1443
1450
. metadata ( )
1444
1451
. clone ( )
@@ -1526,7 +1533,32 @@ pub fn union(left_plan: LogicalPlan, right_plan: LogicalPlan) -> Result<LogicalP
1526
1533
left has {} columns whereas right has {} columns",
1527
1534
left_plan. schema( ) . fields( ) . len( ) ,
1528
1535
right_plan. schema( ) . fields( ) . len( )
1529
- ) ;
1536
+ )
1537
+ . map_err ( |err| {
1538
+ err. with_diagnostic ( |_| {
1539
+ Diagnostic :: new ( [
1540
+ DiagnosticEntry :: new (
1541
+ "UNION queries have different number of columns" ,
1542
+ DiagnosticEntryKind :: Error ,
1543
+ Span :: empty ( ) ,
1544
+ ) ,
1545
+ DiagnosticEntry :: new (
1546
+ format ! ( "This side has {} columns" , left_plan. schema( ) . fields( ) . len( ) ) ,
1547
+ DiagnosticEntryKind :: Note ,
1548
+ Span :: union_iter (
1549
+ left_plan. schema ( ) . fields_spans ( ) . iter ( ) . flatten ( ) . copied ( ) ,
1550
+ ) ,
1551
+ ) ,
1552
+ DiagnosticEntry :: new (
1553
+ format ! ( "This side has {} columns" , right_plan. schema( ) . fields( ) . len( ) ) ,
1554
+ DiagnosticEntryKind :: Note ,
1555
+ Span :: union_iter (
1556
+ right_plan. schema ( ) . fields_spans ( ) . iter ( ) . flatten ( ) . copied ( ) ,
1557
+ ) ,
1558
+ ) ,
1559
+ ] )
1560
+ } )
1561
+ } ) ;
1530
1562
}
1531
1563
1532
1564
// Temporarily use the schema from the left input and later rely on the analyzer to
0 commit comments