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

Use a self-join for UPDATE with outer joins #1323

Merged
merged 2 commits into from
Apr 3, 2025
Merged

Conversation

aidanharan
Copy link
Contributor

@aidanharan aidanharan commented Apr 3, 2025

Port of rails/rails#54722 for MSSQL

Fix following tests from CI run https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/actions/runs/14035778078/job/39293342462

  • UpdateAllTest#test_update_all_with_left_joins_composite_primary_key_can_reference_joined_table:
  • UpdateAllTest#test_update_all_with_self_left_joins_can_reference_joined_table:
  • UpdateAllTest#test_update_all_with_string_joins_can_reference_joined_table:
  • UpdateAllTest#test_update_all_with_left_outer_joins_can_reference_joined_table:

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

A port of Rails changes to support UPDATE statements with self-joins for outer joins on MSSQL.

  • Update the UPDATE syntax to include an alias for the target table.
  • Refactor join injection logic and the prepare_update_statement method to add a self-join.
Comments suppressed due to low confidence (2)

lib/arel/visitors/sqlserver.rb:43

  • [nitpick] The alias '__active_record_update_alias' is introduced here, but later in prepare_update_statement the alias is not applied. Consider clarifying the alias usage for consistency between the UPDATE syntax and the self-join approach.
#   UPDATE t1 AS __active_record_update_alias

lib/arel/visitors/sqlserver.rb:71

  • Prepending the target table to the join sources may affect join ordering. Validate that this reordering does not interfere with other parts of the query processing that rely on a specific join sequence.
stmt.relation.right = [stmt.relation.left, *stmt.relation.right]

@aidanharan aidanharan marked this pull request as ready for review April 3, 2025 18:39
@aidanharan aidanharan merged commit 048734d into main Apr 3, 2025
3 of 6 checks passed
@aidanharan aidanharan deleted the repeat-in-join-clause branch April 3, 2025 18:40
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.

1 participant