1use std::sync::Arc;
8
9use datafusion_common::JoinType;
10use datafusion_common::tree_node::Transformed;
11use datafusion_common::{Column, DFSchemaRef, Result, ScalarValue, plan_err};
12use datafusion_expr::logical_plan::LogicalPlan;
13use datafusion_expr::{EmptyRelation, Expr, GroupingSet, Projection, Union, cast, lit};
14
15use crate::optimizer::ApplyOrder;
16use crate::{OptimizerConfig, OptimizerRule};
17
18#[derive(Default, Debug)]
20pub struct PropagateEmptyRelation;
21
22impl PropagateEmptyRelation {
23 #[expect(missing_docs)]
24 pub fn new() -> Self {
25 Self {}
26 }
27}
28
29impl OptimizerRule for PropagateEmptyRelation {
30 fn name(&self) -> &str {
31 "propagate_empty_relation"
32 }
33
34 fn apply_order(&self) -> Option<ApplyOrder> {
35 Some(ApplyOrder::BottomUp)
36 }
37
38 fn rewrite(
39 &self,
40 plan: LogicalPlan,
41 _config: &dyn OptimizerConfig,
42 ) -> Result<Transformed<LogicalPlan>> {
43 match plan {
44 LogicalPlan::EmptyRelation(_) => Ok(Transformed::no(plan)),
45 LogicalPlan::Projection(_)
46 | LogicalPlan::Filter(_)
47 | LogicalPlan::Window(_)
48 | LogicalPlan::Sort(_)
49 | LogicalPlan::SubqueryAlias(_)
50 | LogicalPlan::Repartition(_)
51 | LogicalPlan::Limit(_) => {
52 let empty = empty_child(&plan)?;
53 if let Some(empty_plan) = empty {
54 return Ok(Transformed::yes(empty_plan));
55 }
56 Ok(Transformed::no(plan))
57 }
58 LogicalPlan::Join(ref join) => {
59 let (left_empty, right_empty) = binary_plan_children_is_empty(&plan)?;
60 let left_field_count = join.left.schema().fields().len();
61
62 match join.join_type {
63 JoinType::Full if left_empty && right_empty => Ok(Transformed::yes(
65 LogicalPlan::EmptyRelation(EmptyRelation {
66 produce_one_row: false,
67 schema: Arc::clone(&join.schema),
68 }),
69 )),
70 JoinType::Full if right_empty => {
73 Ok(Transformed::yes(build_null_padded_projection(
74 Arc::clone(&join.left),
75 &join.schema,
76 left_field_count,
77 true,
78 )?))
79 }
80 JoinType::Full if left_empty => {
81 Ok(Transformed::yes(build_null_padded_projection(
82 Arc::clone(&join.right),
83 &join.schema,
84 left_field_count,
85 false,
86 )?))
87 }
88 JoinType::Inner if left_empty || right_empty => Ok(Transformed::yes(
89 LogicalPlan::EmptyRelation(EmptyRelation {
90 produce_one_row: false,
91 schema: Arc::clone(&join.schema),
92 }),
93 )),
94 JoinType::Left if left_empty => Ok(Transformed::yes(
95 LogicalPlan::EmptyRelation(EmptyRelation {
96 produce_one_row: false,
97 schema: Arc::clone(&join.schema),
98 }),
99 )),
100 JoinType::Left if right_empty => {
103 Ok(Transformed::yes(build_null_padded_projection(
104 Arc::clone(&join.left),
105 &join.schema,
106 left_field_count,
107 true,
108 )?))
109 }
110 JoinType::Right if right_empty => Ok(Transformed::yes(
111 LogicalPlan::EmptyRelation(EmptyRelation {
112 produce_one_row: false,
113 schema: Arc::clone(&join.schema),
114 }),
115 )),
116 JoinType::Right if left_empty => {
119 Ok(Transformed::yes(build_null_padded_projection(
120 Arc::clone(&join.right),
121 &join.schema,
122 left_field_count,
123 false,
124 )?))
125 }
126 JoinType::LeftSemi if left_empty || right_empty => Ok(
127 Transformed::yes(LogicalPlan::EmptyRelation(EmptyRelation {
128 produce_one_row: false,
129 schema: Arc::clone(&join.schema),
130 })),
131 ),
132 JoinType::RightSemi if left_empty || right_empty => Ok(
133 Transformed::yes(LogicalPlan::EmptyRelation(EmptyRelation {
134 produce_one_row: false,
135 schema: Arc::clone(&join.schema),
136 })),
137 ),
138 JoinType::LeftAnti if left_empty => Ok(Transformed::yes(
139 LogicalPlan::EmptyRelation(EmptyRelation {
140 produce_one_row: false,
141 schema: Arc::clone(&join.schema),
142 }),
143 )),
144 JoinType::LeftAnti if right_empty => {
145 Ok(Transformed::yes((*join.left).clone()))
146 }
147 JoinType::RightAnti if left_empty => {
148 Ok(Transformed::yes((*join.right).clone()))
149 }
150 JoinType::RightAnti if right_empty => Ok(Transformed::yes(
151 LogicalPlan::EmptyRelation(EmptyRelation {
152 produce_one_row: false,
153 schema: Arc::clone(&join.schema),
154 }),
155 )),
156 _ => Ok(Transformed::no(plan)),
157 }
158 }
159 LogicalPlan::Aggregate(ref agg) => {
160 if !agg.group_expr.is_empty()
166 && !has_empty_grouping_set(&agg.group_expr)
167 && let Some(empty_plan) = empty_child(&plan)?
168 {
169 return Ok(Transformed::yes(empty_plan));
170 }
171 Ok(Transformed::no(LogicalPlan::Aggregate(agg.clone())))
172 }
173 LogicalPlan::Union(ref union) => {
174 let new_inputs = union
175 .inputs
176 .iter()
177 .filter(|input| match &***input {
178 LogicalPlan::EmptyRelation(empty) => empty.produce_one_row,
179 _ => true,
180 })
181 .cloned()
182 .collect::<Vec<_>>();
183
184 if new_inputs.len() == union.inputs.len() {
185 Ok(Transformed::no(plan))
186 } else if new_inputs.is_empty() {
187 Ok(Transformed::yes(LogicalPlan::EmptyRelation(
188 EmptyRelation {
189 produce_one_row: false,
190 schema: Arc::clone(plan.schema()),
191 },
192 )))
193 } else if new_inputs.len() == 1 {
194 let mut new_inputs = new_inputs;
195 let input_plan = new_inputs.pop().unwrap(); let child = Arc::unwrap_or_clone(input_plan);
197 if child.schema().eq(plan.schema()) {
198 Ok(Transformed::yes(child))
199 } else {
200 let exprs = child
204 .schema()
205 .columns()
206 .into_iter()
207 .zip(plan.schema().iter())
208 .map(|(col, (qualifier, field))| {
209 Expr::Column(col)
210 .alias_qualified(qualifier.cloned(), field.name())
211 })
212 .collect::<Vec<_>>();
213 Ok(Transformed::yes(LogicalPlan::Projection(
214 Projection::try_new(exprs, Arc::new(child))?,
215 )))
216 }
217 } else {
218 Ok(Transformed::yes(LogicalPlan::Union(Union {
219 inputs: new_inputs,
220 schema: Arc::clone(&union.schema),
221 })))
222 }
223 }
224
225 _ => Ok(Transformed::no(plan)),
226 }
227 }
228}
229
230fn binary_plan_children_is_empty(plan: &LogicalPlan) -> Result<(bool, bool)> {
231 match plan.inputs()[..] {
232 [left, right] => {
233 let left_empty = match left {
234 LogicalPlan::EmptyRelation(empty) => !empty.produce_one_row,
235 _ => false,
236 };
237 let right_empty = match right {
238 LogicalPlan::EmptyRelation(empty) => !empty.produce_one_row,
239 _ => false,
240 };
241 Ok((left_empty, right_empty))
242 }
243 _ => plan_err!("plan just can have two child"),
244 }
245}
246
247fn empty_child(plan: &LogicalPlan) -> Result<Option<LogicalPlan>> {
248 match plan.inputs()[..] {
249 [child] => match child {
250 LogicalPlan::EmptyRelation(empty) => {
251 if !empty.produce_one_row {
252 Ok(Some(LogicalPlan::EmptyRelation(EmptyRelation {
253 produce_one_row: false,
254 schema: Arc::clone(plan.schema()),
255 })))
256 } else {
257 Ok(None)
258 }
259 }
260 _ => Ok(None),
261 },
262 _ => plan_err!("plan just can have one child"),
263 }
264}
265
266fn build_null_padded_projection(
286 surviving_plan: Arc<LogicalPlan>,
287 join_schema: &DFSchemaRef,
288 left_field_count: usize,
289 empty_side_is_right: bool,
290) -> Result<LogicalPlan> {
291 let exprs = join_schema
292 .iter()
293 .enumerate()
294 .map(|(i, (qualifier, field))| {
295 let on_empty_side = if empty_side_is_right {
296 i >= left_field_count
297 } else {
298 i < left_field_count
299 };
300
301 if on_empty_side {
302 cast(lit(ScalarValue::Null), field.data_type().clone())
303 .alias_qualified(qualifier.cloned(), field.name())
304 } else {
305 Expr::Column(Column::new(qualifier.cloned(), field.name()))
306 }
307 })
308 .collect::<Vec<_>>();
309
310 Ok(LogicalPlan::Projection(Projection::try_new_with_schema(
311 exprs,
312 surviving_plan,
313 Arc::clone(join_schema),
314 )?))
315}
316
317fn has_empty_grouping_set(group_expr: &[Expr]) -> bool {
330 match group_expr.first() {
331 Some(Expr::GroupingSet(GroupingSet::GroupingSets(groups))) => {
332 groups.iter().any(|g| g.is_empty())
333 }
334 Some(Expr::GroupingSet(GroupingSet::Rollup(_)))
336 | Some(Expr::GroupingSet(GroupingSet::Cube(_))) => true,
337 _ => false,
338 }
339}
340
341#[cfg(test)]
342mod tests {
343
344 use arrow::datatypes::{DataType, Field, Metadata, Schema};
345
346 use datafusion_common::{Column, DFSchema};
347 use datafusion_expr::logical_plan::table_scan;
348 use datafusion_expr::{
349 Operator, binary_expr, col, lit, logical_plan::builder::LogicalPlanBuilder,
350 };
351
352 use crate::OptimizerContext;
353 use crate::assert_optimized_plan_eq_snapshot;
354 use crate::eliminate_filter::EliminateFilter;
355 use crate::optimize_unions::OptimizeUnions;
356 use crate::test::{
357 assert_optimized_plan_with_rules, test_table_scan, test_table_scan_fields,
358 test_table_scan_with_name,
359 };
360
361 use super::*;
362
363 macro_rules! assert_optimized_plan_equal {
364 (
365 $plan:expr,
366 @ $expected:literal $(,)?
367 ) => {{
368 let optimizer_ctx = OptimizerContext::new().with_max_passes(1);
369 let rules: Vec<Arc<dyn crate::OptimizerRule + Send + Sync>> = vec![Arc::new(PropagateEmptyRelation::new())];
370 assert_optimized_plan_eq_snapshot!(
371 optimizer_ctx,
372 rules,
373 $plan,
374 @ $expected,
375 )
376 }};
377 }
378
379 fn assert_together_optimized_plan(
380 plan: LogicalPlan,
381 expected: &str,
382 eq: bool,
383 ) -> Result<()> {
384 assert_optimized_plan_with_rules(
385 vec![
386 Arc::new(EliminateFilter::new()),
387 Arc::new(OptimizeUnions::new()),
388 Arc::new(PropagateEmptyRelation::new()),
389 ],
390 plan,
391 expected,
392 eq,
393 )
394 }
395
396 #[test]
397 fn propagate_empty() -> Result<()> {
398 let plan = LogicalPlanBuilder::empty(false)
399 .filter(lit(true))?
400 .limit(10, None)?
401 .project(vec![binary_expr(lit(1), Operator::Plus, lit(1))])?
402 .build()?;
403
404 assert_optimized_plan_equal!(plan, @"EmptyRelation: rows=0")
405 }
406
407 #[test]
408 fn cooperate_with_eliminate_filter() -> Result<()> {
409 let table_scan = test_table_scan()?;
410 let left = LogicalPlanBuilder::from(table_scan).build()?;
411 let right_table_scan = test_table_scan_with_name("test2")?;
412 let right = LogicalPlanBuilder::from(right_table_scan)
413 .project(vec![col("a")])?
414 .filter(lit(false))?
415 .build()?;
416
417 let plan = LogicalPlanBuilder::from(left)
418 .join_using(
419 right,
420 JoinType::Inner,
421 vec![Column::from_name("a".to_string())],
422 )?
423 .filter(col("a").lt_eq(lit(1i64)))?
424 .build()?;
425
426 let expected = "EmptyRelation: rows=0";
427 assert_together_optimized_plan(plan, expected, true)
428 }
429
430 #[test]
431 fn propagate_union_empty() -> Result<()> {
432 let left = LogicalPlanBuilder::from(test_table_scan()?).build()?;
433 let right = LogicalPlanBuilder::from(test_table_scan_with_name("test2")?)
434 .filter(lit(false))?
435 .build()?;
436
437 let plan = LogicalPlanBuilder::from(left).union(right)?.build()?;
438
439 let expected =
440 "Projection: test.a AS a, test.b AS b, test.c AS c\n TableScan: test";
441 assert_together_optimized_plan(plan, expected, true)
442 }
443
444 #[test]
445 fn propagate_union_multi_empty() -> Result<()> {
446 let one =
447 LogicalPlanBuilder::from(test_table_scan_with_name("test1")?).build()?;
448 let two = LogicalPlanBuilder::from(test_table_scan_with_name("test2")?)
449 .filter(lit(false))?
450 .build()?;
451 let three = LogicalPlanBuilder::from(test_table_scan_with_name("test3")?)
452 .filter(lit(false))?
453 .build()?;
454 let four =
455 LogicalPlanBuilder::from(test_table_scan_with_name("test4")?).build()?;
456
457 let plan = LogicalPlanBuilder::from(one)
458 .union(two)?
459 .union(three)?
460 .union(four)?
461 .build()?;
462
463 let expected = "Union\
464 \n TableScan: test1\
465 \n TableScan: test4";
466 assert_together_optimized_plan(plan, expected, true)
467 }
468
469 #[test]
470 fn propagate_union_all_empty() -> Result<()> {
471 let one = LogicalPlanBuilder::from(test_table_scan_with_name("test1")?)
472 .filter(lit(false))?
473 .build()?;
474 let two = LogicalPlanBuilder::from(test_table_scan_with_name("test2")?)
475 .filter(lit(false))?
476 .build()?;
477 let three = LogicalPlanBuilder::from(test_table_scan_with_name("test3")?)
478 .filter(lit(false))?
479 .build()?;
480 let four = LogicalPlanBuilder::from(test_table_scan_with_name("test4")?)
481 .filter(lit(false))?
482 .build()?;
483
484 let plan = LogicalPlanBuilder::from(one)
485 .union(two)?
486 .union(three)?
487 .union(four)?
488 .build()?;
489
490 let expected = "EmptyRelation: rows=0";
491 assert_together_optimized_plan(plan, expected, true)
492 }
493
494 #[test]
495 fn propagate_union_children_different_schema() -> Result<()> {
496 let one_schema = Schema::new(vec![Field::new("t1a", DataType::UInt32, false)]);
497 let t1_scan = table_scan(Some("test1"), &one_schema, None)?.build()?;
498 let one = LogicalPlanBuilder::from(t1_scan)
499 .filter(lit(false))?
500 .build()?;
501
502 let two_schema = Schema::new(vec![Field::new("t2a", DataType::UInt32, false)]);
503 let t2_scan = table_scan(Some("test2"), &two_schema, None)?.build()?;
504 let two = LogicalPlanBuilder::from(t2_scan).build()?;
505
506 let three_schema = Schema::new(vec![Field::new("t3a", DataType::UInt32, false)]);
507 let t3_scan = table_scan(Some("test3"), &three_schema, None)?.build()?;
508 let three = LogicalPlanBuilder::from(t3_scan).build()?;
509
510 let plan = LogicalPlanBuilder::from(one)
511 .union(two)?
512 .union(three)?
513 .build()?;
514
515 let expected = "Union\
516 \n TableScan: test2\
517 \n TableScan: test3";
518 assert_together_optimized_plan(plan, expected, true)
519 }
520
521 #[test]
522 fn propagate_union_alias() -> Result<()> {
523 let left = LogicalPlanBuilder::from(test_table_scan()?).build()?;
524 let right = LogicalPlanBuilder::from(test_table_scan_with_name("test2")?)
525 .filter(lit(false))?
526 .build()?;
527
528 let plan = LogicalPlanBuilder::from(left).union(right)?.build()?;
529
530 let expected =
531 "Projection: test.a AS a, test.b AS b, test.c AS c\n TableScan: test";
532 assert_together_optimized_plan(plan, expected, true)
533 }
534
535 #[test]
536 fn propagate_union_remaining_input_with_other_names() -> Result<()> {
537 let left = LogicalPlanBuilder::from(test_table_scan()?)
543 .filter(lit(false))?
544 .build()?;
545 let right = LogicalPlanBuilder::from(test_table_scan_with_name("test2")?)
546 .project(vec![
547 col("a").alias("x"),
548 col("b").alias("y"),
549 col("c").alias("z"),
550 ])?
551 .build()?;
552
553 let plan = LogicalPlanBuilder::from(left).union(right)?.build()?;
554
555 let expected = "Projection: x AS a, y AS b, z AS c\
556 \n Projection: test2.a AS x, test2.b AS y, test2.c AS z\
557 \n TableScan: test2";
558 assert_together_optimized_plan(plan, expected, true)
559 }
560
561 #[test]
562 fn cross_join_empty() -> Result<()> {
563 let table_scan = test_table_scan()?;
564 let left = LogicalPlanBuilder::from(table_scan).build()?;
565 let right = LogicalPlanBuilder::empty(false).build()?;
566
567 let plan = LogicalPlanBuilder::from(left)
568 .cross_join(right)?
569 .filter(col("a").lt_eq(lit(1i64)))?
570 .build()?;
571
572 let expected = "EmptyRelation: rows=0";
573 assert_together_optimized_plan(plan, expected, true)
574 }
575
576 fn assert_empty_left_empty_right_lp(
577 left_empty: bool,
578 right_empty: bool,
579 join_type: JoinType,
580 eq: bool,
581 ) -> Result<()> {
582 let left_lp = if left_empty {
583 let left_table_scan = test_table_scan()?;
584
585 LogicalPlanBuilder::from(left_table_scan)
586 .filter(lit(false))?
587 .build()
588 } else {
589 let scan = test_table_scan_with_name("left").unwrap();
590 LogicalPlanBuilder::from(scan).build()
591 }?;
592
593 let right_lp = if right_empty {
594 let right_table_scan = test_table_scan_with_name("right")?;
595
596 LogicalPlanBuilder::from(right_table_scan)
597 .filter(lit(false))?
598 .build()
599 } else {
600 let scan = test_table_scan_with_name("right").unwrap();
601 LogicalPlanBuilder::from(scan).build()
602 }?;
603
604 let plan = LogicalPlanBuilder::from(left_lp)
605 .join_using(
606 right_lp,
607 join_type,
608 vec![Column::from_name("a".to_string())],
609 )?
610 .build()?;
611
612 let expected = "EmptyRelation: rows=0";
613 assert_together_optimized_plan(plan, expected, eq)
614 }
615
616 fn assert_anti_join_empty_join_table_is_base_table(
618 anti_left_join: bool,
619 ) -> Result<()> {
620 let (left, right, join_type, expected) = if anti_left_join {
622 let left = test_table_scan()?;
623 let right = LogicalPlanBuilder::from(test_table_scan()?)
624 .filter(lit(false))?
625 .build()?;
626 let expected = left.display_indent().to_string();
627 (left, right, JoinType::LeftAnti, expected)
628 } else {
629 let right = test_table_scan()?;
630 let left = LogicalPlanBuilder::from(test_table_scan()?)
631 .filter(lit(false))?
632 .build()?;
633 let expected = right.display_indent().to_string();
634 (left, right, JoinType::RightAnti, expected)
635 };
636
637 let plan = LogicalPlanBuilder::from(left)
638 .join_using(right, join_type, vec![Column::from_name("a".to_string())])?
639 .build()?;
640
641 assert_together_optimized_plan(plan, &expected, true)
642 }
643
644 #[test]
645 fn test_join_empty_propagation_rules() -> Result<()> {
646 assert_empty_left_empty_right_lp(true, true, JoinType::Full, true)?;
648
649 assert_empty_left_empty_right_lp(true, false, JoinType::Left, true)?;
651
652 assert_empty_left_empty_right_lp(false, true, JoinType::Right, true)?;
654
655 assert_empty_left_empty_right_lp(true, false, JoinType::LeftSemi, true)?;
657
658 assert_empty_left_empty_right_lp(false, true, JoinType::LeftSemi, true)?;
660
661 assert_empty_left_empty_right_lp(true, false, JoinType::RightSemi, true)?;
663
664 assert_empty_left_empty_right_lp(false, true, JoinType::RightSemi, true)?;
666
667 assert_empty_left_empty_right_lp(true, false, JoinType::LeftAnti, true)?;
669
670 assert_empty_left_empty_right_lp(false, true, JoinType::RightAnti, true)?;
672
673 assert_anti_join_empty_join_table_is_base_table(true)?;
675
676 assert_anti_join_empty_join_table_is_base_table(false)
678 }
679
680 #[test]
681 fn test_join_empty_propagation_rules_noop() -> Result<()> {
682 assert_empty_left_empty_right_lp(false, true, JoinType::Left, false)?;
686
687 assert_empty_left_empty_right_lp(true, false, JoinType::Right, false)?;
689
690 assert_empty_left_empty_right_lp(false, false, JoinType::LeftSemi, false)?;
692
693 assert_empty_left_empty_right_lp(false, false, JoinType::RightSemi, false)?;
695
696 assert_empty_left_empty_right_lp(false, false, JoinType::LeftAnti, false)?;
698
699 assert_empty_left_empty_right_lp(false, true, JoinType::LeftAnti, false)?;
701
702 assert_empty_left_empty_right_lp(false, false, JoinType::RightAnti, false)?;
704
705 assert_empty_left_empty_right_lp(true, false, JoinType::RightAnti, false)
707 }
708
709 #[test]
710 fn test_left_join_right_empty_null_pad() -> Result<()> {
711 let left =
712 LogicalPlanBuilder::from(test_table_scan_with_name("left")?).build()?;
713 let right_empty = LogicalPlanBuilder::from(test_table_scan_with_name("right")?)
714 .filter(lit(false))?
715 .build()?;
716
717 let plan = LogicalPlanBuilder::from(left)
718 .join_using(
719 right_empty,
720 JoinType::Left,
721 vec![Column::from_name("a".to_string())],
722 )?
723 .build()?;
724
725 let expected = "Projection: left.a, left.b, left.c, CAST(NULL AS UInt32) AS a, CAST(NULL AS UInt32) AS b, CAST(NULL AS UInt32) AS c\n TableScan: left";
726 assert_together_optimized_plan(plan, expected, true)
727 }
728
729 #[test]
730 fn test_right_join_left_empty_null_pad() -> Result<()> {
731 let left_empty = LogicalPlanBuilder::from(test_table_scan_with_name("left")?)
732 .filter(lit(false))?
733 .build()?;
734 let right =
735 LogicalPlanBuilder::from(test_table_scan_with_name("right")?).build()?;
736
737 let plan = LogicalPlanBuilder::from(left_empty)
738 .join_using(
739 right,
740 JoinType::Right,
741 vec![Column::from_name("a".to_string())],
742 )?
743 .build()?;
744
745 let expected = "Projection: CAST(NULL AS UInt32) AS a, CAST(NULL AS UInt32) AS b, CAST(NULL AS UInt32) AS c, right.a, right.b, right.c\n TableScan: right";
746 assert_together_optimized_plan(plan, expected, true)
747 }
748
749 #[test]
750 fn test_full_join_right_empty_null_pad() -> Result<()> {
751 let left =
752 LogicalPlanBuilder::from(test_table_scan_with_name("left")?).build()?;
753 let right_empty = LogicalPlanBuilder::from(test_table_scan_with_name("right")?)
754 .filter(lit(false))?
755 .build()?;
756
757 let plan = LogicalPlanBuilder::from(left)
758 .join_using(
759 right_empty,
760 JoinType::Full,
761 vec![Column::from_name("a".to_string())],
762 )?
763 .build()?;
764
765 let expected = "Projection: left.a, left.b, left.c, CAST(NULL AS UInt32) AS a, CAST(NULL AS UInt32) AS b, CAST(NULL AS UInt32) AS c\n TableScan: left";
766 assert_together_optimized_plan(plan, expected, true)
767 }
768
769 #[test]
770 fn test_full_join_left_empty_null_pad() -> Result<()> {
771 let left_empty = LogicalPlanBuilder::from(test_table_scan_with_name("left")?)
772 .filter(lit(false))?
773 .build()?;
774 let right =
775 LogicalPlanBuilder::from(test_table_scan_with_name("right")?).build()?;
776
777 let plan = LogicalPlanBuilder::from(left_empty)
778 .join_using(
779 right,
780 JoinType::Full,
781 vec![Column::from_name("a".to_string())],
782 )?
783 .build()?;
784
785 let expected = "Projection: CAST(NULL AS UInt32) AS a, CAST(NULL AS UInt32) AS b, CAST(NULL AS UInt32) AS c, right.a, right.b, right.c\n TableScan: right";
786 assert_together_optimized_plan(plan, expected, true)
787 }
788
789 #[test]
790 fn test_left_join_complex_on_right_empty_null_pad() -> Result<()> {
791 let left =
792 LogicalPlanBuilder::from(test_table_scan_with_name("left")?).build()?;
793 let right_empty = LogicalPlanBuilder::from(test_table_scan_with_name("right")?)
794 .filter(lit(false))?
795 .build()?;
796
797 let plan = LogicalPlanBuilder::from(left)
799 .join(
800 right_empty,
801 JoinType::Left,
802 (
803 vec![Column::from_name("a".to_string())],
804 vec![Column::from_name("a".to_string())],
805 ),
806 Some(col("left.b").gt(col("right.b"))),
807 )?
808 .build()?;
809
810 let expected = "Projection: left.a, left.b, left.c, CAST(NULL AS UInt32) AS a, CAST(NULL AS UInt32) AS b, CAST(NULL AS UInt32) AS c\n TableScan: left";
811 assert_together_optimized_plan(plan, expected, true)
812 }
813
814 #[test]
815 fn test_empty_with_non_empty() -> Result<()> {
816 let table_scan = test_table_scan()?;
817
818 let fields = test_table_scan_fields();
819
820 let empty = LogicalPlan::EmptyRelation(EmptyRelation {
821 produce_one_row: false,
822 schema: Arc::new(DFSchema::from_unqualified_fields(
823 fields.into(),
824 Metadata::new(),
825 )?),
826 });
827
828 let one = LogicalPlanBuilder::from(empty.clone()).build()?;
829 let two = LogicalPlanBuilder::from(table_scan).build()?;
830 let three = LogicalPlanBuilder::from(empty).build()?;
831
832 let plan = LogicalPlanBuilder::from(one)
837 .union(two)?
838 .union(three)?
839 .build()?;
840
841 let expected = "Projection: test.a AS a, test.b AS b, test.c AS c\
842 \n TableScan: test";
843
844 assert_together_optimized_plan(plan, expected, true)
845 }
846}