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!

Co-authored-by: Yongting You <2010youy01@gmail.com>
@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
codetyri0n pushed a commit to codetyri0n/datafusion that referenced this pull request Nov 11, 2025
apache#18481)

## Which issue does this PR close?

<!--
We generally require a GitHub issue to be filed for all bug fixes and
enhancements and this helps us generate change logs for our releases.
You can link an issue to this PR using the GitHub syntax. For example
`Closes apache#123` indicates that this PR will close issue apache#123.
-->

- Closes apache#18407

## Rationale for this change

<!--
Why are you proposing this change? If this is already explained clearly
in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand
your changes and offer better suggestions for fixes.
-->
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?

<!--
There is no need to duplicate the description in the issue here but it
is sometimes worth providing a summary of the individual changes in this
PR.
-->
Add selectivity metric to NestedLoopJoinExec for EXPLAIN ANALYZE

## Are these changes tested?

<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code

If tests are not included in your PR, please explain why (for example,
are they covered by existing tests)?
-->
Added tests

## Are there any user-facing changes?

<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.
-->
Yes, new metric in explain analyze
<!--
If there are any breaking changes to public APIs, please add the `api
change` label.
-->

---------

Co-authored-by: Yongting You <2010youy01@gmail.com>
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