Skip to content

Remove ActiveRecord::Relation#calculate patch #1198

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

Merged
merged 2 commits into from
Jul 2, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,6 @@ module ConnectionAdapters
module SQLServer
module CoreExt
module Calculations
def calculate(operation, column_name)
klass.with_connection do |connection|
if connection.sqlserver?
_calculate(operation, column_name)
else
super
end
end
end

private

# Same as original `calculate` method except we don't perform PostgreSQL hack that removes ordering.
def _calculate(operation, column_name)
operation = operation.to_s.downcase

if @none
case operation
when "count", "sum"
result = group_values.any? ? Hash.new : 0
return @async ? Promise::Complete.new(result) : result
when "average", "minimum", "maximum"
result = group_values.any? ? Hash.new : nil
return @async ? Promise::Complete.new(result) : result
end
end

if has_include?(column_name)
relation = apply_join_dependency

if operation == "count"
unless distinct_value || distinct_select?(column_name || select_for_count)
relation.distinct!
relation.select_values = Array(klass.primary_key || table[Arel.star])
end
# PostgreSQL: ORDER BY expressions must appear in SELECT list when using DISTINCT
# Start of monkey-patch
# relation.order_values = [] if group_values.empty?
# End of monkey-patch
end

relation.calculate(operation, column_name)
else
perform_calculation(operation, column_name)
end
end

def build_count_subquery(relation, column_name, distinct)
klass.with_connection do |connection|
relation = relation.unscope(:order) if connection.sqlserver?
Expand Down
Loading