Skip to content

Conversation

@petern48
Copy link
Contributor

@petern48 petern48 commented Nov 4, 2025

Which issue does this PR close?

Rationale for this change

This new metric will give the user better visibility to see what portion of the possibilities is actually being matched.

What changes are included in this PR?

Add selectivity metric to NestedLoopJoinExec for EXPLAIN ANALYZE

Are these changes tested?

Added tests

Are there any user-facing changes?

Yes, new metric in explain analyze

@github-actions github-actions bot added core Core DataFusion crate physical-plan Changes to the physical-plan crate labels Nov 4, 2025
Comment on lines 1132 to 1139
// Selectivity Metric: Update total possibilities for the batch (left_rows * right_rows)
if let (Ok(left_data), Some(right_batch)) =
(self.get_left_data(), self.current_right_batch.as_ref())
{
let left_rows = left_data.batch().num_rows();
let right_rows = right_batch.num_rows();
self.metrics.selectivity.add_total(left_rows * right_rows);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I originally tried to get the values and calculate num_left * num_right at the end (in a drop() method, similar to how it was done here for BuildProbeJoinMetrics. But found it difficult to get the correctness consistently right since these are all multiple streams.

Instead, I'm doing it incrementally. The basic idea here is that for each right batch, we add_total() the size of the full left buffer times the num_rows in the current batch. Doing that num_batches times gives us the equivalent of num_right * num_left.

Comment on lines +1487 to +1491
self.metrics.join_metrics.output_batches.add(1);

// Update output rows for selectivity metric
let output_rows = batch.num_rows();
self.metrics.selectivity.add_part(output_rows);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update output_rows at flush time.

@petern48 petern48 marked this pull request as ready for review November 4, 2025 07:38
Copy link
Contributor

@2010YOUY01 2010YOUY01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you!

@2010YOUY01 2010YOUY01 added this pull request to the merge queue Nov 5, 2025
Merged via the queue into apache:main with commit 8363c89 Nov 5, 2025
32 checks passed
@petern48 petern48 deleted the nlj_selectivity branch November 5, 2025 15:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Core DataFusion crate physical-plan Changes to the physical-plan crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support selectivity metrics (for Explain Analyze) in Nested Loop Join

2 participants