1. 13 5月, 2020 2 次提交
    • R
      Fix type casting aggregated values on association's attributes · 5f59eac2
      Ryuta Kamizono 提交于
      Follow up of #39255.
      
      Previously aggregation functions only use the model's attribute types on
      the relation for type cast, this will be looking up association's
      attribute and type caster if a column name is table name qualified.
      
      Fixes #39248.
      5f59eac2
    • R
      Fix `minimum` and `maximum` on time zone aware attributes · 11751b2e
      Ryuta Kamizono 提交于
      This is the opposite direction of #39039.
      
      #39111 fixes `minimum` and `maximum` on date columns with type casting
      by column type on the database. But column type has no information for
      time zone aware attributes, it means that attribute type should always
      be precedence over column type. I've realized that fact in the related
      issue report #39248.
      
      I've reverted the expectation of #39039, to make time zone aware
      attributes works.
      11751b2e
  2. 02 5月, 2020 1 次提交
    • R
      Fix `minimum` and `maximum` on non numeric column · 767edafc
      Ryuta Kamizono 提交于
      I supposed all aggregation functions will return numeric result in
      #39039, but that assumption was incorrect for `minimum` and `maximum`,
      if an aggregated column is non numeric type.
      
      I've restored type casting aggregated result for `minimum` and `maximum`.
      
      Fixes #39110.
      767edafc
  3. 29 4月, 2020 1 次提交
    • R
      Remove useless type cast on aggregated value · e10765cf
      Ryuta Kamizono 提交于
      Related #39039.
      
      Currently PostgreSQL adapter is the only adapter that rely on type
      casting by `result.column_types`, now the adapter can return numeric
      value without type casting by Active Record side.
      So we can remove that useless type cast on aggregated value.
      e10765cf
  4. 25 4月, 2020 1 次提交
    • R
      Fix aggregate functions to return numeric value consistently even on custom attribute type · 89e1dd61
      Ryuta Kamizono 提交于
      Currently, `count` and `average` always returns numeric value, but
      `sum`, `maximum`, and `minimum` not always return numeric value if
      aggregated on custom attribute type.
      
      I think that inconsistent behavior is surprising:
      
      ```ruby
      # All adapters except postgresql adapter are affected
      # by custom type casting.
      
      Book.group(:status).sum(:status)
      # => { "proposed" => "proposed", "published" => nil }
      ```
      
      That is caused by fallback looking up cast type to `type_for(column)`.
      Now all supported adapters can return numeric value without that
      fallback, so I think we can remove that, it will also fix aggregate
      functions to return numeric value consistently.
      89e1dd61
  5. 20 3月, 2020 1 次提交
    • S
      Update doc for #pluck to use non-deprecated syntax · d43a4bce
      stevenjackson 提交于
      The current implementation raises a warning in Rails 6.0.2.2
      ```
      DEPRECATION WARNING: Dangerous query method (method whose arguments are used as raw SQL) called with non-attribute argument(s): "DATEDIFF(updated_at, created_at)". Non-attribute arguments will be disallowed in Rails 6.1. This method should not be called with user-provided values, such as request parameters or model attributes. Known-safe values can be passed by wrapping them in Arel.sql().
      ```
      d43a4bce
  6. 19 3月, 2020 1 次提交
  7. 05 3月, 2020 1 次提交
  8. 27 1月, 2020 1 次提交
  9. 07 8月, 2019 1 次提交
    • R
      Fix GROUP BY aggregation alias to not duplicate "_" chars · e62195e2
      Ryuta Kamizono 提交于
      c9e4c848 has one performance optimization for `aggregate_alias` to early
      returning by `aggregate_alias.match?(/\A\w+\z/)`, but it is caused a
      regression that failing deduplication for non word chars #36867.
      
      I've quited the optimization and add a test to prevent a future
      regression.
      
      Fixes #36867.
      e62195e2
  10. 08 7月, 2019 1 次提交
  11. 22 4月, 2019 1 次提交
  12. 08 4月, 2019 1 次提交
  13. 06 4月, 2019 1 次提交
    • R
      Don't repeat same expression in SELECT and GROUP BY clauses · c9e4c848
      Ryuta Kamizono 提交于
      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.
      c9e4c848
  14. 04 4月, 2019 2 次提交
    • R
      Fix `count(:all)` with eager loading and explicit select and order · 060e09df
      Ryuta Kamizono 提交于
      This follows up ebc09ed9.
      
      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.
      060e09df
    • R
      Optimizer hints should be applied on Top level query as much as possible · 7fb2ba5e
      Ryuta Kamizono 提交于
      I've experienced this issue in our app, some hints only works on Top
      level query (e.g. `MAX_EXECUTION_TIME`).
      7fb2ba5e
  15. 26 2月, 2019 1 次提交
  16. 13 2月, 2019 1 次提交
    • R
      Fix `pluck` and `select` with custom attributes · 0ee96d13
      Ryuta Kamizono 提交于
      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.
      0ee96d13
  17. 18 1月, 2019 2 次提交
  18. 04 1月, 2019 1 次提交
    • A
      Make average compatible accross Ruby versions · d237c7c7
      Alberto Almagro 提交于
      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.
      d237c7c7
  19. 02 1月, 2019 1 次提交
  20. 27 8月, 2018 1 次提交
  21. 25 8月, 2018 1 次提交
  22. 29 7月, 2018 2 次提交
  23. 26 2月, 2018 1 次提交
  24. 10 2月, 2018 2 次提交
  25. 29 1月, 2018 1 次提交
    • D
      Avoid extra calls to to_s · 2e8c8d60
      Daniel Colson 提交于
      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.
      2e8c8d60
  26. 25 1月, 2018 1 次提交
    • R
      Fix `count(:all)` with eager loading and having an order other than the driving table · ebc09ed9
      Ryuta Kamizono 提交于
      This is a regression caused by 6beb4de7.
      
      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. 6beb4de7 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.
      ebc09ed9
  27. 20 12月, 2017 1 次提交
    • R
      Fix `count(:all)` to correctly work `distinct` with custom SELECT list · c6cd9a59
      Ryuta Kamizono 提交于
      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.
      c6cd9a59
  28. 09 11月, 2017 4 次提交
  29. 06 11月, 2017 1 次提交
  30. 14 10月, 2017 1 次提交
  31. 09 10月, 2017 1 次提交
  32. 23 9月, 2017 1 次提交