aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation/calculations.rb
Commit message (Collapse)AuthorAgeFilesLines
* Do not use aliases in GROUP BY clauseRyuta Kamizono2019-07-081-1/+1
| | | | | | | It appears that Oracle does not allow using aliases in GROUP BY clause unlike ORDER BY clause. Fixes #36613.
* Don't table name qualify aggrigate column for virtual attributeRyuta Kamizono2019-04-221-4/+2
| | | | Related 0ee96d13de29680e148ccb8e5b68025f29fd091c.
* Fix GROUP BY with calculate longer name field to respect `table_alias_length`Ryuta Kamizono2019-04-081-5/+6
| | | | Follow up of c9e4c848eeeb8999b778fa1ae52185ca5537fffe.
* Don't repeat same expression in SELECT and GROUP BY clausesRyuta Kamizono2019-04-061-26/+20
| | | | | | | | | | | | | | | | | | | | | This refactors `execute_grouped_calculation` and slightly changes generated GROUP BY queries, since I'd not prefer to repeat same expression in SELECT and GROUP BY clauses. Before: ``` SELECT COUNT(*) AS count_all, "topics"."author_name" AS topics_author_name, COALESCE(type, title) AS coalesce_type_title FROM "topics" GROUP BY "topics"."author_name", COALESCE(type, title) ``` After: ``` SELECT COUNT(*) AS count_all, "topics"."author_name" AS topics_author_name, COALESCE(type, title) AS coalesce_type_title FROM "topics" GROUP BY topics_author_name, coalesce_type_title ``` Although we generally don't guarantee to support Arel node constructed by user itself, this also fixes #24207.
* Fix `count(:all)` with eager loading and explicit select and orderRyuta Kamizono2019-04-041-4/+5
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This follows up ebc09ed9ad9a04338138739226a1a92c7a2707ee. We've still experienced a regression for `size` (`count(:all)`) with eager loading and explicit select and order when upgrading Rails to 5.1. In that case, the eager loading enforces `distinct` to subselect but still keep the custom select, it would cause the ORDER BY with DISTINCT issue. ``` % ARCONN=postgresql bundle exec ruby -w -Itest test/cases/relations_test.rb -n test_size_with_eager_loading_and_custom_select_and_order Using postgresql Run options: -n test_size_with_eager_loading_and_custom_select_and_order --seed 8356 # Running: E Error: RelationTest#test_size_with_eager_loading_and_custom_select_and_order: ActiveRecord::StatementInvalid: PG::InvalidColumnReference: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list LINE 1: ..." ON "comments"."post_id" = "posts"."id" ORDER BY comments.i... ^ ``` As another problem on `distinct` is enforced, the result of `count` becomes fewer than expected if `select` is given explicitly. e.g. ```ruby Post.select(:type).count # => 11 Post.select(:type).distinct.count # => 3 ``` As long as `distinct` is enforced, we need to care to keep the result of `count`. This fixes both the `count` with eager loading problems.
* Optimizer hints should be applied on Top level query as much as possibleRyuta Kamizono2019-04-041-3/+4
| | | | | I've experienced this issue in our app, some hints only works on Top level query (e.g. `MAX_EXECUTION_TIME`).
* fixes different `count` calculation when using `size` manual `select` with ↵jvillarejo2019-02-261-3/+8
| | | | | | | | DISTINCT When using `select` with `'DISTINCT( ... )'` if you use method `size` on a non loaded relation it overrides the column selected by passing `:all` so it returns different value than count. This fixes #35214
* Fix `pluck` and `select` with custom attributesRyuta Kamizono2019-02-131-4/+2
| | | | | | | | | Currently custom attributes are always qualified by the table name in the generated SQL wrongly even if the table doesn't have the named column, it would cause an invalid SQL error. Custom attributes should only be qualified if the table has the same named column.
* Do not allow passing the column name to `sum` when a block is passedRafael Mendonça França2019-01-171-6/+4
|
* Do not allow passing the column name to `count` when a block is passedRafael Mendonça França2019-01-171-6/+4
|
* Make average compatible accross Ruby versionsAlberto Almagro2019-01-041-1/+1
| | | | | | | Since Ruby 2.6.0 NilClass#to_d is returning `BigDecimal` 0.0, this breaks `average` compatibility with prior Ruby versions. This patch makes `average` return `nil` in all Ruby versions when there are no rows.
* Fix TypeError: no implicit conversion of Arel::Attributes::Attribute into ↵Ryuta Kamizono2019-01-021-1/+1
| | | | | | | | | | | | | String properly This reverts 27c6c07 since `arel_attr.to_s` is not right way to avoid the type error. That to_s returns `"#<struct Arel::Attributes::Attribute ...>"`, there is no reason to match the regex to the inspect form. And also, the regex path is not covered by our test cases. I've tweaked the regex for redundant part and added assertions for the regex path.
* Permit list usage cleanup and clearer documentationKevin Deisz2018-08-271-1/+1
|
* Convert over the rest of the whitelist referencesKevin Deisz2018-08-241-1/+1
|
* Fix TypeError: no implicit conversion of Arel::Attributes::Attribute into StringBart de Water2018-07-281-1/+1
|
* Enable Start/EndWith and RegexpMatch copsBart de Water2018-07-281-1/+1
| | | | | In cases where the MatchData object is not used, this provides a speed-up: https://github.com/JuanitoFatas/fast-ruby/#stringmatch-vs-stringmatch-vs-stringstart_withstringend_with-code-start-code-end
* Distinct with order #count regressionMax Schwenk2018-02-251-1/+1
|
* Fix formatting of `pick` [ci skip]yuuji.yaginuma2018-02-101-1/+1
|
* Add Relation#pick as short-hand for single-value plucks (#31941)David Heinemeier Hansson2018-02-091-0/+18
| | | * Add Relation#pick as short-hand for single-value plucks
* Avoid extra calls to to_sDaniel Colson2018-01-291-1/+1
| | | | | | | With #31615 `type_for_attribute` accepts either a symbol as well as a string. `has_attribute?` and `attribute_alias` also accept either. Since these methods call `to_s` on the argument, we no longer need to do that at the call site.
* Fix `count(:all)` with eager loading and having an order other than the ↵Ryuta Kamizono2018-01-251-1/+8
| | | | | | | | | | | | | | | | | | | driving table This is a regression caused by 6beb4de. In PostgreSQL, ORDER BY expressions must appear in SELECT list when using DISTINCT. When using `count(:all)` with eager loading, Active Record enforces DISTINCT to count the driving table records only. 6beb4de was caused the regression because `count(:all)` with DISTINCT path no longer removes ORDER BY. We need to ignore ORDER BY when DISTINCT is enforced, otherwise not always generated valid SQL for PostgreSQL. Fixes #31783.
* Fix `count(:all)` to correctly work `distinct` with custom SELECT listRyuta Kamizono2017-12-201-10/+8
| | | | | | | | | | | | | | Currently `count(:all)` with `distinct` doesn't work correctly because SELECT list is always replaced to `*` or primary key in that case even if having custom SELECT list. And also, PostgreSQL has a limitation that ORDER BY expressions must appear in select list for SELECT DISTINCT. Therefore, we should not replace custom SELECT list when using `count(:all)` with `distinct`. Closes #31277.
* try using regexesBen Toews2017-11-091-9/+3
|
* allow table name and direction in string order argBen Toews2017-11-091-23/+8
|
* allow Arel.sql() for pluckBen Toews2017-11-091-6/+14
|
* add config to check arguments to unsafe AR methodsBen Toews2017-11-091-14/+28
|
* Ensure `apply_join_dependency` for `collection_cache_key` if eager-loading ↵Ryuta Kamizono2017-11-061-2/+2
| | | | | | is needed Fixes #30315.
* Fix `COUNT(DISTINCT ...)` for `GROUP BY` with `ORDER BY` and `LIMIT`Ryuta Kamizono2017-10-141-1/+1
| | | | | | | | | | This is the fix for the regression of #29848. In #29848, I've kept existing select list in the subquery for the count if ORDER BY is given. But it had accidentally affect to GROUP BY queries also. It should keep the previous behavior in that case. Fixes #30886.
* Remove meaningless named `construct_relation_for_association_calculations`Ryuta Kamizono2017-10-091-2/+3
| | | | | I don't think this is a good abstraction because the internal method is used only if the relation need to be applied join dependency.
* Ensure `1 AS one` for SQL Server with calculations.Ken Collins2017-09-221-1/+1
|
* Ensure sum honors distinct on has_many throughAaron Wortham2017-08-141-0/+3
| | | | | | When using a has_many through relation and then summing an attribute the distinct was not being used. This will ensure that when summing an attribute, the number is only used once when distinct has been used.
* Merge pull request #29848 from kamipo/fix_distinct_count_with_order_and_limitRafael França2017-07-241-10/+19
|\ | | | | Fix `COUNT(DISTINCT ...)` with `ORDER BY` and `LIMIT`
| * Should keep the table name qualified `*` for distinct subqueryRyuta Kamizono2017-07-221-1/+1
| |
| * Fix `COUNT(DISTINCT ...)` with `ORDER BY` and `LIMIT`Ryuta Kamizono2017-07-221-10/+19
| | | | | | | | | | | | | | Since #26972, `ORDER BY` is kept if `LIMIT` is presented for performance. But in most SQL servers (e.g. PostgreSQL, SQL Server, etc), `ORDER BY` expressions must appear in select list for `SELECT DISTINCT`. We should not replace existing select list in that case.
* | Refactor Active Record to let Arel manage bind paramsSean Griffin2017-07-241-3/+3
|/ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | A common source of bugs and code bloat within Active Record has been the need for us to maintain the list of bind values separately from the AST they're associated with. This makes any sort of AST manipulation incredibly difficult, as any time we want to potentially insert or remove an AST node, we need to traverse the entire tree to find where the associated bind parameters are. With this change, the bind parameters now live on the AST directly. Active Record does not need to know or care about them until the final AST traversal for SQL construction. Rather than returning just the SQL, the Arel collector will now return both the SQL and the bind parameters. At this point the connection adapter will have all the values that it had before. A bit of this code is janky and something I'd like to refactor later. In particular, I don't like how we're handling associations in the predicate builder, the special casing of `StatementCache::Substitute` in `QueryAttribute`, or generally how we're handling bind value replacement in the statement cache when prepared statements are disabled. This also mostly reverts #26378, as it moved all the code into a location that I wanted to delete. /cc @metaskills @yahonda, this change will affect the adapters Fixes #29766. Fixes #29804. Fixes #26541. Close #28539. Close #24769. Close #26468. Close #26202. There are probably other issues/PRs that can be closed because of this commit, but that's all I could find on the first few pages.
* Use frozen-string-literal in ActiveRecordKir Shatrov2017-07-191-0/+2
|
* Skip query cache for in_batches and friendsEugene Kenny2017-07-061-3/+3
| | | | | | | | | | | The `find_each`, `find_in_batches` and `in_batches` APIs usually operate on large numbers of records, where it's preferable not to load them all into memory at once. If the query cache is enabled, it will hold onto the query results until the end of the execution context (request/job), which means the memory used is still proportional to the total number of records. These queries are typically not repeated, so the query cache isn't desirable here.
* Revert "Merge pull request #29540 from kirs/rubocop-frozen-string"Matthew Draper2017-07-021-1/+0
| | | | | This reverts commit 3420a14590c0e6915d8b6c242887f74adb4120f9, reversing changes made to afb66a5a598ce4ac74ad84b125a5abf046dcf5aa.
* Enforce frozen string in RubocopKir Shatrov2017-07-011-0/+1
|
* Fix the next version of Rails from 5.3 to 6.0Ryuta Kamizono2017-06-291-2/+2
|
* Remove delegating to arel in a relationRyuta Kamizono2017-06-291-1/+1
| | | | | The delegation was needed since passing `relation` with `relation.bound_attributes`. It should use `relation.arel` in that case.
* Deprecate passing arguments and block at the same time to `count` and `sum` ↵Ryuta Kamizono2017-05-291-2/+20
| | | | | | | | in `ActiveRecord::Calculations` `select`, `count`, and `sum` in `Relation` are also `Enumerable` method that can be passed block. `select` with block already doesn't take arguments since 4fc3366. This is follow up of that.
* Don't pass `arel.engine` to `Arel::SelectManager.new`Ryuta Kamizono2017-05-051-2/+1
| | | | | | The argument of `Arel::SelectManager.new` is `table`, not `engine`. https://github.com/rails/arel/blob/v8.0.0/lib/arel/select_manager.rb#L10
* Simply forward `Calculations#count` to `Enumerable#count`Ryuta Kamizono2017-03-101-7/+4
| | | | | | | Follow up of #24203. Since b644964b `ActiveRecord::Relation` includes `Enumerable` so it is enough to call `super` simply.
* Include selects in group query with having clauseEugene Kenny2017-02-261-0/+1
| | | | | | | | | | | | | | When a grouped calculation contains a having clause that references a selected value, we need to include that selected value in the query. Postgres doesn't support referencing a selected value in a having clause, but other databases do; we can skip the test on the pg adapter but run it for the others. This was fixed before in 9a298a162c16e019fe6971e563e7f4916e86ced6, but the test coverage was lost in 5a05207d99b7e2678f9b42db2d9ffc21ec2c8c3b. The fix regressed in 6311975fb3c02f50730fd1e11b8dba8dd9c05306 and was removed in 97d46c17ea9113b0ce970167f5208c8d9170915c.
* Remove useless `select_values += select_values`Ryuta Kamizono2017-02-261-1/+0
| | | | | `select_values` is a local variable defined at previous line. `select_values += select_values` is totally useless.
* Suppress `DISTINCT` clause outside aggregate functionRyuta Kamizono2017-02-251-2/+2
| | | | | | | | | | | | | | | | | | `DISTINCT` clause is applied inside aggregate function by `operation_over_aggregate_column` if needed. Unneeded outside aggregate function. ```ruby # Before author.unique_categorized_posts.count # => SELECT DISTINCT COUNT(DISTINCT "posts"."id") FROM "posts" INNER JOIN "categorizations" ON "posts"."id" = "categorizations"."post_id" WHERE "categorizations"."author_id" = ? [["author_id", 2]] # After author.unique_categorized_posts.count # => SELECT COUNT(DISTINCT "posts"."id") FROM "posts" INNER JOIN "categorizations" ON "posts"."id" = "categorizations"."post_id" WHERE "categorizations"."author_id" = ? [["author_id", 2]] ``` Closes #27615
* Revert "Remove useless `column_alias` in `subquery_for_count`"Ryuta Kamizono2017-02-071-2/+3
| | | | This reverts commit 28977f1fa3d7b15c1608174a165e60b71ddf3995.
* Remove useless `column_alias` in `subquery_for_count`Ryuta Kamizono2017-02-061-3/+2
| | | | | | | | | If select clause is specified and last column has a column alias, additional column alias causes a statement invalid. Add test coverage for counting a single column with NULL values. Fixes #27676, #27682, and #27705.
* `self.` is not needed when calling its own instance methodAkira Matsuda2017-01-051-1/+1
| | | | Actually, private methods cannot be called with `self.`, so it's not just redundant, it's a bad habit in Ruby