Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #50812] Don't duplicate selects, inner and outer joins when merging with STI #50886

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

joshuay03
Copy link
Contributor

@joshuay03 joshuay03 commented Jan 26, 2024

Motivation / Background

Fixes #50812

Detail

This Pull Request changes #merge_select_values, #merge_joins and #merge_outer_joins in ActiveRecord::Relation::Merger to unionise selects, inner and outer joins when the two sources of a merge are related via STI.

Additional information

The only similar case left in ActiveRecord::Relation::Merger is when merging preloads.

I don't think that is similar to these cases since the preload values are simply used to preload associations. I thought I would just mention it anyway.

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Changes that are unrelated should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

comments = Comment.select(:id, :post_id).merge(SpecialComment.select(:id, :post_id))
assert_equal SpecialComment.pluck(:id).sort, comments.to_a.map(&:id).sort
assert_equal SpecialComment.pluck(:post_id).sort, comments.to_a.map(&:post_id).sort
assert_equal [:id, :post_id], comments.select_values
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Before patch:
Screenshot 2024-01-26 at 11 56 39 pm

def test_relation_merging_with_joins_and_sti
comments = Comment.joins(:post).merge(SpecialComment.joins(:post))
assert_equal SpecialComment.count, comments.count
assert_equal [:post], comments.joins_values
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Before patch:
Screenshot 2024-01-26 at 11 57 09 pm

def test_relation_merging_with_left_outer_joins_and_sti
comments = Comment.left_outer_joins(:post).merge(SpecialComment.left_outer_joins(:post))
assert_equal SpecialComment.count, comments.count
assert_equal [:post], comments.left_outer_joins_values
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Before patch:
Screenshot 2024-01-26 at 11 57 34 pm

Comment on lines +380 to +386
def base_class
self
end
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was necessary else this test now fails with:
Screenshot 2024-01-26 at 11 59 38 pm

@joshuay03 joshuay03 force-pushed the fix-duplicate-select-inner-and-outer-join-on-merge branch 2 times, most recently from 40566c5 to b2e4fad Compare January 26, 2024 14:02
@joshuay03 joshuay03 changed the title Don't duplicate selects, inner and outer joins when merging with STI [Fix #50812] Don't duplicate selects, inner and outer joins when merging with STI Jan 26, 2024
@joshuay03 joshuay03 force-pushed the fix-duplicate-select-inner-and-outer-join-on-merge branch from b2e4fad to 7c46503 Compare January 30, 2024 10:41
@joshuay03 joshuay03 force-pushed the fix-duplicate-select-inner-and-outer-join-on-merge branch from 7c46503 to d9946d2 Compare February 7, 2024 13:46
@joshuay03 joshuay03 force-pushed the fix-duplicate-select-inner-and-outer-join-on-merge branch 2 times, most recently from c58d2a1 to 0b9cb38 Compare February 21, 2024 20:43
@joshuay03 joshuay03 force-pushed the fix-duplicate-select-inner-and-outer-join-on-merge branch from 0b9cb38 to ebfbac6 Compare March 19, 2024 09:49
@joshuay03 joshuay03 force-pushed the fix-duplicate-select-inner-and-outer-join-on-merge branch from ebfbac6 to dd7d2e1 Compare March 19, 2024 10:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Redundant JOIN produced when merging relations from STI classes
1 participant