Skip to content

Commit 059d244

Browse files
aidanharanAidan Haranwpolicarpo
authored
Fix removal of invalid ordering from select statements (#890)
Co-authored-by: Aidan Haran <aharan@fusioneer.com> Co-authored-by: Wanderson Policarpo <wpolicarpo@gmail.com>
1 parent c98ea79 commit 059d244

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- [#880](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/880) Handle any default column class when deduplicating
1212
- [#861](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/861) Fix Rails 6.1 database config
1313
- [#885](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/885) Fix the quoting of ActiveModel attributes
14+
- [#890](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/890) Fix removal of invalid ordering from select statements
1415
- [#881](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/881) Dump column collation to schema.rb and allow collation changes using column_change
1516

1617
#### Changed

lib/arel/visitors/sqlserver.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,11 @@ def visit_Arel_Nodes_OuterJoin(o, collector)
184184
end
185185
end
186186

187-
def collect_in_clause(left, right, collector)
188-
if Array === right
189-
right.each { |node| remove_invalid_ordering_from_select_statement(node) }
187+
def visit_Arel_Nodes_In(o, collector)
188+
if Array === o.right
189+
o.right.each { |node| remove_invalid_ordering_from_select_statement(node) }
190190
else
191-
remove_invalid_ordering_from_select_statement(right)
191+
remove_invalid_ordering_from_select_statement(o.right)
192192
end
193193

194194
super

test/cases/in_clause_test_sqlserver.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,31 @@ class InClauseTestSQLServer < ActiveRecord::TestCase
3333
assert_includes posts.to_sql, "ORDER BY [authors].[name]"
3434
assert_equal 8, posts.length
3535
end
36+
37+
it "removes ordering from 'not' subqueries" do
38+
authors_subquery = Author.where.not(name: ["Mary", "Bob"]).order(:name)
39+
posts = Post.where(author: authors_subquery)
40+
41+
assert_includes authors_subquery.to_sql, "ORDER BY [authors].[name]"
42+
assert_not_includes posts.to_sql, "ORDER BY [authors].[name]"
43+
assert_equal 5, posts.length
44+
end
45+
46+
it "does not remove ordering from 'not' subquery that includes a limit" do
47+
authors_subquery = Author.where.not(name: ["Ronan", "Mary", "Bob"]).order(:name).limit(2)
48+
posts = Post.where(author: authors_subquery)
49+
50+
assert_includes authors_subquery.to_sql, "ORDER BY [authors].[name]"
51+
assert_includes posts.to_sql, "ORDER BY [authors].[name]"
52+
assert_equal 5, posts.length
53+
end
54+
55+
it "does not remove ordering from 'not' subquery that includes an offset" do
56+
authors_subquery = Author.where.not(name: ["David", "Ronan", "Cian"]).order(:name).offset(1)
57+
posts = Post.where(author: authors_subquery)
58+
59+
assert_includes authors_subquery.to_sql, "ORDER BY [authors].[name]"
60+
assert_includes posts.to_sql, "ORDER BY [authors].[name]"
61+
assert_equal 3, posts.length
62+
end
3663
end

0 commit comments

Comments
 (0)