Skip to content
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

Harmonize average aggregation with minimum and maximum #41418

Merged
merged 1 commit into from
Feb 12, 2021

Conversation

schmijos
Copy link
Contributor

@schmijos schmijos commented Feb 11, 2021

The real problem behind the previous implementation of average
aggregation was not that float columns returned BigDecimal but that
average skipped ActiveModel type casting.

This change introduces handling for the only needed special case
of average: integers. Any integer based fields will be casted to
BigDecimal when aggregated with average now.

activerecord/CHANGELOG.md Outdated Show resolved Hide resolved
@@ -389,6 +394,11 @@ def execute_grouped_calculation(operation, column_name, distinct) #:nodoc:
result[key] = type_cast_calculated_value(row[column_alias], operation) do |value|
type ||= column.try(:type_caster) ||
lookup_cast_type_from_join_dependencies(column_name.to_s) || Type.default_value

if operation == "average" && type.is_a?(Type::Integer)
type = Type::Decimal.new
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type comes in from above. It will be kept while looping. Isn't that an issue?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is intentionally kept since the block will be evaluated multiple times and lookup_cast_type_from_join_dependencies is a bit expensive.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I took in the second (identical) proposal as well because memoization is only there for expensive lookup_cast_type_from_join_dependencies.

The real problem behind the previous implementation of average
aggregation was not that float columns returned `BigDecimal` but that
average skipped `ActiveModel` type casting.

This change introduces handling for the only needed special case
of average: integers. Now any fields based on
`ActiveRecord::Type::Integer` will be casted to `BigDecimal` when
aggregated with average.
@kamipo kamipo merged commit a19c9b3 into rails:main Feb 12, 2021
@schmijos schmijos deleted the regression-for-40351 branch February 12, 2021 13:35
kamipo added a commit to kamipo/rails that referenced this pull request Feb 13, 2021
Follow up to rails#40351 and rails#41418.

This fixes `average` on decimal and enum (on integer in general)
attributes to not do `type.deserialize`.

The precision and the scale on the column might be lower than the
calculated result.

And also, mapping the calculated result to enum label is quite
meaningless, the mapping result is almost nil.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants