rails / arel

A Relational Algebra
2.06k stars 390 forks source link

Handle empty arrays in grouping_any and grouping_all #480

Closed Dantemss closed 6 years ago

Dantemss commented 7 years ago

This PR allows ARel to handle empty arrays in grouping_any and grouping_all.

On master, when given an empty array:

This PR causes grouping_any to return Nodes::False.new and grouping_all to return Nodes::True.new instead. The reason for that is that is the behavior of [].any? and [].all? (with a block or not) and seems intuitive for normal use cases.

Note: We could instead wrap the returned true/false nodes with a Nodes::Grouping. That would cause the TRUE or FALSE in the generated SQL to become (TRUE) or (FALSE).

The issue I was attempting to fix originally can be found here: https://github.com/activerecord-hackery/squeel/issues/376 but after investigating I concluded this was an ARel behavior, not a squeel behavior.

rails-bot commented 7 years ago

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @tenderlove (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

Dantemss commented 7 years ago

Oh also, while adding the new tests, I noticed that the eq_all test was repeated, so I removed the second instance of it.

alexmobo commented 7 years ago

Could you use a shared example on the tests?

danielpclark commented 7 years ago

I believe this is similar to https://github.com/rails/arel/issues/368

Dantemss commented 7 years ago

Yes, I believe this may work as a patch for the issue reported in #368. I will attempt to convert these examples to shared examples... probably during the weekend though.

matthewd commented 6 years ago

Per #523, Arel development is moving to rails/rails.

If this PR is still relevant, please consider reopening it over there.