Faveod / arel-extensions

Extending Arel
MIT License
142 stars 24 forks source link

Grouped calculations return all nil values #39

Closed snex closed 3 years ago

snex commented 3 years ago

Steps to reproduce

Postgres 12

Given a table with three columns, id (primary key), fk_id (integer - in my app it is a foreign key but the problem appears even without extra tables) and result (integer or any numeric), enter in some data and then run the following in console:

Table.group(:fk_id).average(:result)

Expected behavior

Expected behavior should be a hash with a list of fk_ids as keys and the average of the result column for those fk_ids as values.

Actual behavior

What actually gets returned is a hash with the fk_ids as keys but nil for all values. If I run the auto-generated query in psql console, the results are correct. Other calculations such as sum, count, maximum, minimum, etc, all either incorrectly give 0s or nils as well.

System configuration

Rails version: 6.1

Ruby version: 2.7.1

jdelporte commented 3 years ago

Hi, It appears That Arel10 changed the behavior of the method as. It now has some edge effect on the receiver of the call to as (at least in aggregate functions) and Rails 6.1 exploits this new behavior. We will work on something to avoid ArelExtensions to break this new behavior, event if we consider it strange and somehow unwanted.

dannyvanhoof commented 3 years ago

@jdelporte I see you committed some changes to address this issue. Any idea when this will be released? - as this bug really stops us from using arel-extensions in combination with 6.1 :-(

jdelporte commented 3 years ago

@dannyvanhoof My preliminary tests were concluding. I will try to release a new gem before the end of the day.

jdelporte commented 3 years ago

The last pushed gem (v2.0.1) should fix this issue. But we are still perplex about the decision made that caused this issue.

snex commented 3 years ago

"Average" calculation seems to work but "sum" returns all 0s. "Maximum" and "minimum" seem to work ok also, so looks like only "sum" is still broken.

jdelporte commented 3 years ago

Thanks for the info. I will keep investigating. In the meantime, you can still do that as a workaround : Table.group(:fk_id).pluck(:fk_id, Table.arel_table[:result].sum).to_h

jdelporte commented 3 years ago

Check out for the gem v2.0.19, it should fix the sum issue. Let me know if some latent problems appear.

snex commented 3 years ago

Looks good :+1: