Skip to content

Commit 809b30b

Browse files
committed
test: verify JoinSelection consults the statistics registry from context
1 parent 5d88752 commit 809b30b

1 file changed

Lines changed: 70 additions & 0 deletions

File tree

‎datafusion/core/tests/physical_optimizer/join_selection.rs‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,16 @@ use datafusion_physical_expr::expressions::{BinaryExpr, Column, NegativeExpr};
3737
use datafusion_physical_expr::intervals::utils::check_support;
3838
use datafusion_physical_expr::{EquivalenceProperties, Partitioning, PhysicalExpr};
3939
use datafusion_physical_expr_common::sort_expr::PhysicalSortExpr;
40+
use datafusion_physical_optimizer::PhysicalOptimizerContext;
4041
use datafusion_physical_optimizer::PhysicalOptimizerRule;
4142
use datafusion_physical_optimizer::join_selection::JoinSelection;
4243
use datafusion_physical_plan::displayable;
4344
use datafusion_physical_plan::joins::utils::ColumnIndex;
4445
use datafusion_physical_plan::joins::utils::JoinFilter;
4546
use datafusion_physical_plan::joins::{HashJoinExec, NestedLoopJoinExec, PartitionMode};
47+
use datafusion_physical_plan::operator_statistics::{
48+
ClosureStatisticsProvider, StatisticsRegistry, StatisticsResult,
49+
};
4650
use datafusion_physical_plan::projection::ProjectionExec;
4751
use datafusion_physical_plan::sorts::sort_preserving_merge::SortPreservingMergeExec;
4852
use datafusion_physical_plan::{
@@ -640,6 +644,72 @@ async fn test_null_aware_left_anti_with_filter_does_not_swap() -> Result<()> {
640644
Ok(())
641645
}
642646

647+
#[tokio::test]
648+
async fn test_join_swap_uses_context_statistics_registry() -> Result<()> {
649+
let left: Arc<dyn ExecutionPlan> = Arc::new(StatisticsExec::new(
650+
big_statistics(),
651+
Schema::new(vec![Field::new("left_col", DataType::Int32, false)]),
652+
));
653+
let right: Arc<dyn ExecutionPlan> = Arc::new(StatisticsExec::new(
654+
big_statistics(),
655+
Schema::new(vec![Field::new("right_col", DataType::Int32, false)]),
656+
));
657+
let join = HashJoinExec::try_new(
658+
Arc::clone(&left),
659+
Arc::clone(&right),
660+
vec![(
661+
Arc::new(Column::new_with_schema("left_col", &left.schema())?),
662+
Arc::new(Column::new_with_schema("right_col", &right.schema())?),
663+
)],
664+
None,
665+
&JoinType::LeftAnti,
666+
None,
667+
PartitionMode::Auto,
668+
NullEquality::NullEqualsNothing,
669+
false,
670+
)?;
671+
672+
// Reports `right` as much smaller than its real stats: the swap below only
673+
// happens if `context`'s registry is actually threaded through.
674+
let mut registry = StatisticsRegistry::new();
675+
registry.register(Arc::new(ClosureStatisticsProvider::with_matches(
676+
|plan| plan.schema().field(0).name() == "right_col",
677+
|_plan, _child_stats| Ok(StatisticsResult::Computed(small_statistics().into())),
678+
)));
679+
680+
struct ContextWithRegistry {
681+
config: ConfigOptions,
682+
registry: StatisticsRegistry,
683+
}
684+
685+
impl PhysicalOptimizerContext for ContextWithRegistry {
686+
fn config_options(&self) -> &ConfigOptions {
687+
&self.config
688+
}
689+
690+
fn statistics_registry(&self) -> Option<&StatisticsRegistry> {
691+
Some(&self.registry)
692+
}
693+
}
694+
695+
let context = ContextWithRegistry {
696+
config: ConfigOptions::new(),
697+
registry,
698+
};
699+
700+
let optimized_join =
701+
JoinSelection::new().optimize_with_context(Arc::new(join), &context)?;
702+
let swapped_join = optimized_join
703+
.downcast_ref::<HashJoinExec>()
704+
.expect("optimize_with_context should return a HashJoinExec");
705+
706+
assert_eq!(*swapped_join.join_type(), JoinType::RightAnti);
707+
assert_eq!(swapped_join.left().schema().field(0).name(), "right_col");
708+
assert_eq!(swapped_join.right().schema().field(0).name(), "left_col");
709+
710+
Ok(())
711+
}
712+
643713
#[tokio::test]
644714
async fn test_join_with_swap_mark() {
645715
let join_types = [JoinType::LeftMark, JoinType::RightMark];

0 commit comments

Comments
 (0)