You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "mkleen (via GitHub)" <gi...@apache.org> on 2026/07/15 10:12:45 UTC
[PR] fix: Preserve schema and metadata when a cross-join is swapped [datafusion]
mkleen opened a new pull request, #23605:
URL: https://github.com/apache/datafusion/pull/23605
## Which issue does this PR close?
Relates to #23461 but resolves it only for cross-joins. The same problem exisists for Hash/NL joins. There will be follow-ups.
## Rationale for this change
When a `cross-join` swaps it's sides, the metadata is rebuilded with based on the inverted inputs. This can cause a change of metadata when the metadata has conflicting values e.g.:
```rust
// left: 1000 rows, schema metadata {"key": "left"}
// right: 2 rows, schema metadata {"key": "right"}
ctx.sql("select * from left cross join right").await?.collect().await?;
// -> Internal error: ... 'join_selection' failed. Schema mismatch.
// Metadata should be {"key" : "right" } but changed to {"key" : "left" }
```
The cross-join should preserve it's original metdata when it's swapped.
## What changes are included in this PR?
- This pr avoids recalculating the metadata based on the swapped inputs and uses the original metadata instead
- Test
## Are these changes tested?
Yes
## Are there any user-facing changes?
No
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: Preserve metadata when a cross-join is swapped [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #23605:
URL: https://github.com/apache/datafusion/pull/23605#discussion_r3597726860
##########
datafusion/physical-plan/src/joins/cross_join.rs:
##########
@@ -186,8 +186,32 @@ impl CrossJoinExec {
/// operators on the join's children. Check [`super::HashJoinExec::swap_inputs`]
/// for more details.
pub fn swap_inputs(&self) -> Result<Arc<dyn ExecutionPlan>> {
- let new_join =
- CrossJoinExec::new(Arc::clone(&self.right), Arc::clone(&self.left));
+ // Rebuild schema with columns from right to left, preserve existing metadata
Review Comment:
I wonder if we can make a helper (as a follow on) to handle this logic rather than sprinkling the same fix through the code in many places 🤔
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: Preserve metadata when a cross-join is swapped [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #23605:
URL: https://github.com/apache/datafusion/pull/23605#issuecomment-4994939045
FYI @fangchenli
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: Preserve metadata when a cross-join is swapped [datafusion]
Posted by "mkleen (via GitHub)" <gi...@apache.org>.
mkleen commented on code in PR #23605:
URL: https://github.com/apache/datafusion/pull/23605#discussion_r3607792220
##########
datafusion/physical-plan/src/joins/cross_join.rs:
##########
@@ -186,8 +186,32 @@ impl CrossJoinExec {
/// operators on the join's children. Check [`super::HashJoinExec::swap_inputs`]
/// for more details.
pub fn swap_inputs(&self) -> Result<Arc<dyn ExecutionPlan>> {
- let new_join =
- CrossJoinExec::new(Arc::clone(&self.right), Arc::clone(&self.left));
+ // Rebuild schema with columns from right to left, preserve existing metadata
Review Comment:
Do you want the helper as part of this PR?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: Preserve metadata when a cross-join is swapped [datafusion]
Posted by "mkleen (via GitHub)" <gi...@apache.org>.
mkleen commented on PR #23605:
URL: https://github.com/apache/datafusion/pull/23605#issuecomment-5071685735
> Do we have an end-to-end bug reproducer (SQL/query level)? This looks like a major bug, and I think the given UT can't guarantee the test goal, here is the reason: [#23759 (comment)](https://github.com/apache/datafusion/pull/23759#discussion_r3642387124)
>
> I can help to look into it further.
@2010YOUY01 See https://github.com/apache/datafusion/pull/23857
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: Preserve metadata when a cross-join is swapped [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #23605:
URL: https://github.com/apache/datafusion/pull/23605#discussion_r3634258243
##########
datafusion/physical-plan/src/joins/cross_join.rs:
##########
@@ -186,8 +186,32 @@ impl CrossJoinExec {
/// operators on the join's children. Check [`super::HashJoinExec::swap_inputs`]
/// for more details.
pub fn swap_inputs(&self) -> Result<Arc<dyn ExecutionPlan>> {
- let new_join =
- CrossJoinExec::new(Arc::clone(&self.right), Arc::clone(&self.left));
+ // Rebuild schema with columns from right to left, preserve existing metadata
Review Comment:
We can do it as a follow on PR perhaps
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: Preserve metadata when a cross-join is swapped [datafusion]
Posted by "2010YOUY01 (via GitHub)" <gi...@apache.org>.
2010YOUY01 commented on PR #23605:
URL: https://github.com/apache/datafusion/pull/23605#issuecomment-5065282520
Do we have an end-to-end bug reproducer (SQL/query level)? This looks like a major bug, and I think the given UT can't guarantee the test goal, here is the reason: https://github.com/apache/datafusion/pull/23759#discussion_r3642387124
I can help to look into it further.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: Preserve metadata when a cross-join is swapped [datafusion]
Posted by "fangchenli (via GitHub)" <gi...@apache.org>.
fangchenli commented on PR #23605:
URL: https://github.com/apache/datafusion/pull/23605#issuecomment-5027766227
Sorry, I didn't get a chance to look at it. Thanks for the PR @mkleen and thanks for the review @alamb
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: Preserve metadata when a cross-join is swapped [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #23605:
URL: https://github.com/apache/datafusion/pull/23605#issuecomment-5027371230
Thanks @mkleen
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: Preserve metadata when a cross-join is swapped [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #23605:
URL: https://github.com/apache/datafusion/pull/23605
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: Preserve metadata when a cross-join is swapped [datafusion]
Posted by "mkleen (via GitHub)" <gi...@apache.org>.
mkleen commented on PR #23605:
URL: https://github.com/apache/datafusion/pull/23605#issuecomment-4999634192
@alamb Thanks for the review!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: Preserve metadata when a cross-join is swapped [datafusion]
Posted by "mkleen (via GitHub)" <gi...@apache.org>.
mkleen commented on code in PR #23605:
URL: https://github.com/apache/datafusion/pull/23605#discussion_r3600798166
##########
datafusion/physical-plan/src/joins/cross_join.rs:
##########
@@ -186,8 +186,32 @@ impl CrossJoinExec {
/// operators on the join's children. Check [`super::HashJoinExec::swap_inputs`]
/// for more details.
pub fn swap_inputs(&self) -> Result<Arc<dyn ExecutionPlan>> {
- let new_join =
- CrossJoinExec::new(Arc::clone(&self.right), Arc::clone(&self.left));
+ // Rebuild schema with columns from right to left, preserve existing metadata
Review Comment:
Yes!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: Preserve metadata when a cross-join is swapped [datafusion]
Posted by "mkleen (via GitHub)" <gi...@apache.org>.
mkleen commented on code in PR #23605:
URL: https://github.com/apache/datafusion/pull/23605#discussion_r3589308766
##########
datafusion/physical-plan/src/joins/cross_join.rs:
##########
@@ -186,8 +186,32 @@ impl CrossJoinExec {
/// operators on the join's children. Check [`super::HashJoinExec::swap_inputs`]
/// for more details.
pub fn swap_inputs(&self) -> Result<Arc<dyn ExecutionPlan>> {
- let new_join =
- CrossJoinExec::new(Arc::clone(&self.right), Arc::clone(&self.left));
+ // Rebuild schema with columns from right to left, preserve existing metadata
Review Comment:
This rebuilds the schema in the same way as `CrossJoinExec::new` does and preserves the metadata.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org