Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/arel/visitors/sqlserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ def visit_Arel_Nodes_SelectStatementDistinctNonPresentOrders(o, a)
groups = core.groups
orders = o.orders.uniq

# split out any projections that may have > 1 specified (comma-separated)
projections = projections.each_with_object(',').map(&:split).flatten

select_frags = projections.map do |x|
frag = projection_to_sql_remove_distinct(x, core, a)
# Remove the table specifier
Expand All @@ -76,6 +79,9 @@ def visit_Arel_Nodes_SelectStatementDistinctNonPresentOrders(o, a)

projection_list = projections.map { |x| projection_to_sql_remove_distinct(x, core, a) }.join(', ')

# strip aliases from projection list for PARTITION BY value expression
partitions = projection_list.gsub(/\s+AS\s+[^,]*/i, '')

sql = [
('SELECT'),
(visit(core.set_quantifier, a) if core.set_quantifier && !o.offset),
Expand All @@ -90,7 +96,7 @@ def visit_Arel_Nodes_SelectStatementDistinctNonPresentOrders(o, a)
("ORDER BY #{orders.map { |x| visit(x, a) }.join(', ')}" unless orders.empty?),
(') AS __order'),
(', ROW_NUMBER() OVER ('),
("PARTITION BY #{projection_list}" if !orders.empty?),
("PARTITION BY #{partitions}" if !orders.empty?),
(" ORDER BY #{orders.map { |x| visit(x, a) }.join(', ')}" unless orders.empty?),
(') AS __joined_row_num')
].join('')
Expand Down
17 changes: 17 additions & 0 deletions test/cases/finder_test_sqlserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ def test_find_with_order_on_included_associations_with_construct_finder_sql_for_
)
end

def test_multiple_select_with_count_distinct_and_order
posts = Post.joins(:authors)
.select('COUNT(DISTINCT authors.id) as total')
.select('title as x, body as y')
.group(:title, :body)
.order(:title).to_a

# quick sanity check, known number of records were returned
assert_equal(3, posts.size)

# the defined aliases should be present
post = posts.first
assert_respond_to(post, :total)
assert_respond_to(post, :x)
assert_respond_to(post, :y)
end

def test_coerced_exists_does_not_select_columns_without_alias
assert_sql(/SELECT TOP \(1\) 1 AS one FROM \[topics\]/i) do
Topic.exists?
Expand Down