1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activerecord/lib
Ryuta Kamizono 63e35a1323 Fix GROUP BY queries to apply LIMIT/OFFSET after aggregations
If `eager_loading` is true, `apply_join_dependency` force applies
LIMIT/OFFSET before JOINs by `limited_ids_for` to keep parent records
count. But for aggregation queries, LIMIT/OFFSET should be applied after
aggregations the same as SQL semantics.

And also, we could not replace SELECT list by `limited_ids_for` when a
query has a GROUP BY clause. It had never been worked since it will
causes generating invalid SQL for MySQL, PostgreSQL, and probably most
backends.

```
% ARCONN=postgresql be ruby -w -Itest test/cases/calculations_test.rb -n test_group_by_with_limit
Using postgresql
Run options: -n test_group_by_with_limit --seed 20925

# Running:

E

Error:
CalculationsTest#test_group_by_with_limit:
ActiveRecord::StatementInvalid: PG::GroupingError: ERROR:  column "posts.id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT  DISTINCT "posts"."id", "posts"."type" AS alias_0 FRO...                         ^
: SELECT  DISTINCT "posts"."id", "posts"."type" AS alias_0 FROM "posts" LEFT OUTER JOIN "comments" ON "comments"."post_id" = "posts"."id" GROUP BY "posts"."type" ORDER BY "posts"."type" ASC LIMIT $1
```

Fixes #8103.
Closes #27249.
2018-06-07 09:58:20 +09:00
..
active_record Fix GROUP BY queries to apply LIMIT/OFFSET after aggregations 2018-06-07 09:58:20 +09:00
arel Fix: Arel Postgresql visitor generates invalid SQL for GROUPING SETS. 2018-05-28 12:11:57 +02:00
rails/generators Rails 6 requires Ruby 2.3+ 2018-02-17 10:03:37 -08:00
active_record.rb Remove ForeignKeys module which was introduced at #32299 2018-04-02 17:54:26 +09:00
arel.rb Arel: :nodoc: all 2018-02-24 18:11:47 +10:30