Skip to content

Commit cadb967

Browse files
committed
Fix single distinct projections for offset sql.
1 parent 9a7229f commit cadb967

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

lib/arel/visitors/sqlserver.rb

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ def visit_Arel_Nodes_SelectStatementWithOutOffset(o, windowed=false)
118118
projections = core.projections
119119
groups = core.groups
120120
orders = o.orders.reverse.uniq.reverse
121-
if windowed && !function_select_statement?(o)
122-
projections = projections.map { |x| projection_without_expression(x) }
121+
if windowed
122+
projections = function_select_statement?(o) ? projections : projections.map { |x| projection_without_expression(x) }
123123
elsif eager_limiting_select_statement?(o)
124124
raise "visit_Arel_Nodes_SelectStatementWithOutOffset - eager_limiting_select_statement?"
125125
groups = projections.map { |x| projection_without_expression(x) }
@@ -159,10 +159,9 @@ def visit_Arel_Nodes_SelectStatementWithOffset(o)
159159
end
160160

161161
def visit_Arel_Nodes_SelectStatementForComplexCount(o)
162-
# joins = correlated_safe_joins
163162
core = o.cores.first
164-
orders = rowtable_orders(o)
165163
o.limit.expr = o.limit.expr + (o.offset ? o.offset.expr : 0) if o.limit
164+
orders = rowtable_orders(o)
166165
[ "SELECT COUNT([count]) AS [count_id]",
167166
"FROM (",
168167
"SELECT",
@@ -234,14 +233,16 @@ def find_and_fix_uncorrelated_joins_in_select_statement(o)
234233
def rowtable_projections(o)
235234
core = o.cores.first
236235
if single_distinct_select_statement?(o)
237-
raise 'TODO: single_distinct_select_statement'
238-
# ::Array.wrap(relation.select_clauses.first.dup.tap do |sc|
239-
# sc.sub! 'DISTINCT', "DISTINCT #{taken_clause if relation.taken.present?}".strip
240-
# sc.sub! table_name_from_select_clause(sc), '__rnt'
241-
# sc.strip!
242-
# end)
243-
elsif false # relation.join? && all_select_clauses_aliased?
244-
raise 'TODO: relation.join? && all_select_clauses_aliased?'
236+
tn = table_name_from_select_statement(o)
237+
core.projections.map do |x|
238+
x.dup.tap do |p|
239+
p.sub! 'DISTINCT', "DISTINCT #{visit(o.limit)}".strip if o.limit
240+
p.sub! /\[#{tn}\]\./, '[__rnt].'
241+
p.strip!
242+
end
243+
end
244+
elsif false # join_in_select_statement?(o) && all_select_clauses_aliased?
245+
raise 'TODO: join_in_select_statement?(o) && all_select_clauses_aliased?'
245246
# relation.select_clauses.map do |sc|
246247
# sc.split(',').map { |c| c.split(' AS ').last.strip }.join(', ')
247248
# end
@@ -256,7 +257,8 @@ def rowtable_projections(o)
256257
def rowtable_orders(o)
257258
if !o.orders.empty?
258259
o.orders
259-
elsif false # TODO relation.join?
260+
elsif join_in_select_statement?(o)
261+
raise 'TODO: rowtable_orders - join_in_select_statement?(o)'
260262
# table_names_from_select_clauses.map { |tn| quote("#{tn}.#{pk_for_table(tn)}") }
261263
else
262264
tn = table_name_from_select_statement(o)

test/cases/scratch_test_sqlserver.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@ class ScratchTestSqlserver < ActiveRecord::TestCase
2020
:authors, :categorizations, :categories, :posts
2121

2222
should 'pass' do
23-
combined = Developer.find(:all, :order => 'developers.name, developers.salary')
24-
assert_equal combined, Developer.find(:all, :order => ['developers.name', 'developers.salary'])
23+
topics = Topic.send(:with_scope, :find => {:limit => 1, :offset => 1, :include => :replies}) do
24+
Topic.find(:all, :order => "topics.id")
25+
end
26+
assert_equal 1, topics.size
27+
assert_equal 2, topics.first.id
2528
end
2629

2730

31+
2832
end
2933

0 commit comments

Comments
 (0)