Skip to content

Apache DataFusion SQL Query Engine

fix: correct statistics for `NestedLoopJoinExec` (#17680)

Duong Cong Toai60827273b11e
diff --git a/datafusion/physical-plan/src/joins/nested_loop_join.rs b/datafusion/physical-plan/src/joins/nested_loop_join.rs
index ced9078d95..00d1613090 100644
--- a/datafusion/physical-plan/src/joins/nested_loop_join.rs
+++ b/datafusion/physical-plan/src/joins/nested_loop_join.rs
@@ -173,7 +173,8 @@ pub struct NestedLoopJoinExec {
pub(crate) filter: Option<JoinFilter>,
/// How the join is performed
pub(crate) join_type: JoinType,
- /// The schema once the join is applied
+ /// The full concatenated schema of left and right children should be distinct from
+ /// the output schema of the operator
join_schema: SchemaRef,
/// Future that consumes left input and buffers it in memory
///
@@ -550,7 +551,7 @@ impl ExecutionPlan for NestedLoopJoinExec {
self.right.partition_statistics(None)?,
vec![],
&self.join_type,
- &self.join_schema,
+ &self.schema(),
)
}
@@ -2268,6 +2269,26 @@ pub(crate) mod tests {
Ok(())
}
+ #[tokio::test]
+ async fn join_has_correct_stats() -> Result<()> {
+ let left = build_left_table();
+ let right = build_right_table();
+ let nested_loop_join = NestedLoopJoinExec::try_new(
+ left,
+ right,
+ None,
+ &JoinType::Left,
+ Some(vec![1, 2]),
+ )?;
+ let stats = nested_loop_join.partition_statistics(None)?;
+ assert_eq!(
+ nested_loop_join.schema().fields().len(),
+ stats.column_statistics.len(),
+ );
+ assert_eq!(2, stats.column_statistics.len());
+ Ok(())
+ }
+
#[rstest]
#[tokio::test]
async fn join_right_semi_with_filter(