Skip to content

Commit

Permalink
Fixed the ordering of optimizer hints in the generated SQL (#1255)
Browse files Browse the repository at this point in the history
  • Loading branch information
cherez authored Nov 8, 2024
1 parent 111d33b commit 47a6654
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- [#1244](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1244) Allow INSERT statements with SELECT notation
- [#1247](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1247) Fix queries with date and date-time placeholder conditions
- [#1249](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1249) Binary basic columns should be limitable
- [#1255](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1255)Fixed the ordering of optimizer hints in the generated SQL

## v7.2.1

Expand Down
14 changes: 6 additions & 8 deletions lib/arel/visitors/sqlserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,26 +134,24 @@ def visit_Arel_Nodes_HomogeneousIn(o, collector)

def visit_Arel_Nodes_SelectStatement(o, collector)
@select_statement = o
optimizer_hints = nil
distinct_One_As_One_Is_So_Not_Fetch o
if o.with
collector = visit o.with, collector
collector << " "
end
collector = o.cores.inject(collector) { |c, x|
visit_Arel_Nodes_SelectCore(x, c)
}
collector = o.cores.inject(collector) do |collect, core|
optimizer_hints = core.optimizer_hints if core.optimizer_hints
visit_Arel_Nodes_SelectCore(core, collect)
end
collector = visit_Orders_And_Let_Fetch_Happen o, collector
collector = visit_Make_Fetch_Happen o, collector
collector = maybe_visit optimizer_hints, collector
collector
ensure
@select_statement = nil
end

def visit_Arel_Nodes_SelectCore(o, collector)
collector = super
maybe_visit o.optimizer_hints, collector
end

def visit_Arel_Nodes_OptimizerHints(o, collector)
hints = o.expr.map { |v| sanitize_as_option_clause(v) }.join(", ")
collector << "OPTION (#{hints})"
Expand Down
9 changes: 9 additions & 0 deletions test/cases/optimizer_hints_test_sqlserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ class OptimizerHitsTestSQLServer < ActiveRecord::TestCase
end
end


it "support order" do
assert_queries_match(%r{\ASELECT .+ FROM .+ ORDER .+ OPTION .+\z}) do
companies = Company.optimizer_hints("LABEL='FindCompanies'")
companies = companies.order(:id)
companies.to_a
end
end

it "sanitize values" do
assert_queries_match(%r{\ASELECT .+ FROM .+ OPTION \(HASH GROUP\)\z}) do
companies = Company.optimizer_hints("OPTION (HASH GROUP)")
Expand Down

0 comments on commit 47a6654

Please sign in to comment.