1. 13 5月, 2020 3 次提交
    • R
      Fix `pluck` to correctly type cast same column names and association columns · 71f0df94
      Ryuta Kamizono 提交于
      That issues are caused by using only the model's cast types on the
      relation.
      To fix that issues, use the attribute's type caster takes precedence
      over the model's cast types on the relation.
      
      Fixes #35232.
      Fixes #36042.
      Fixes #37484.
      71f0df94
    • 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. 07 5月, 2020 1 次提交
  3. 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
  4. 01 5月, 2020 1 次提交
    • R
      Avoid quite useless loop in `find_by_sql` · 71e45582
      Ryuta Kamizono 提交于
      `column_types` is empty except PostgreSQL adapter, and
      `attribute_types.each_key { |k| column_types.delete k }` is also empty
      even if PostgreSQL adapter almost all case, so that code is quite
      useless. This improves performance for `find_by_sql` to avoid that
      useless loop as much as possible.
      
      ```ruby
      ActiveRecord::Schema.define do
        create_table :active_storage_blobs do |t|
          t.string   :key,          null: false
          t.string   :filename,     null: false
          t.string   :content_type
          t.text     :metadata
          t.string   :service_name, null: false
          t.bigint   :byte_size,    null: false
          t.string   :checksum,     null: false
          t.datetime :created_at,   null: false
      
          t.index [ :key ], unique: true
        end
      end
      
      class ActiveStorageBlob < ActiveRecord::Base
      end
      
      Benchmark.ips do |x|
        x.report("find_by") { ActiveStorageBlob.find_by(id: 1) }
      end
      ```
      
      Before:
      
      ```
      Warming up --------------------------------------
                   find_by     1.256k i/100ms
      Calculating -------------------------------------
                   find_by     12.595k (± 3.4%) i/s -     64.056k in   5.091599s
      ```
      
      After:
      
      ```
      Warming up --------------------------------------
                   find_by     1.341k i/100ms
      Calculating -------------------------------------
                   find_by     13.170k (± 3.5%) i/s -     67.050k in   5.097439s
      ```
      
      To avoid column types loop for PostgreSQL adapter, this skips returning
      additional column types if a column has already been type casted by pg
      decoders. Fortunately this fixes #36186 partly for common types.
      71e45582
  5. 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
  6. 24 4月, 2020 1 次提交
  7. 12 4月, 2020 1 次提交
  8. 19 3月, 2020 1 次提交
  9. 27 1月, 2020 2 次提交
  10. 14 1月, 2020 1 次提交
  11. 11 10月, 2019 1 次提交
  12. 08 8月, 2019 1 次提交
  13. 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
  14. 02 8月, 2019 1 次提交
    • G
      Add tests for selecting aggregates · 05300be6
      Godfrey Chan 提交于
      These tests verifies that aggregates like `AVG` can be selected as
      "virtual attributes" on Active Record models and have the correct
      column type.
      05300be6
  15. 08 7月, 2019 1 次提交
  16. 17 6月, 2019 1 次提交
    • R
      PostgreSQL: Fix GROUP BY with ORDER BY virtual count attribute · cb0299c9
      Ryuta Kamizono 提交于
      GROUP BY with virtual count attribute is invalid for almost all
      databases, but it is valid for PostgreSQL, and it had worked until Rails
      5.2.2, so it is a regression for Rails 5.2.3 (caused by 311f0011).
      
      I can't find perfectly solution for fixing this for now, but I would not
      like to break existing apps, so I decided to allow referencing virtual
      count attribute in ORDER BY clause when GROUP BY aggrigation (it partly
      revert the effect of 311f0011) to fix the regression #36022.
      
      Fixes #36022.
      cb0299c9
  17. 11 6月, 2019 1 次提交
  18. 02 6月, 2019 1 次提交
    • Y
      Address test_pluck_columns_with_same_name failure due to nondeterministic sort order · 86384ea8
      Yasuo Honda 提交于
      ```ruby
      $ bundle exec rake test_postgresql
      ... snip ...
      Failure:
      CalculationsTest#test_pluck_columns_with_same_name [/home/yahonda/git/rails/activerecord/test/cases/calculations_test.rb:842]:
      --- expected
      +++ actual
      @@ -1 +1 @@
      -[["The First Topic", "The Second Topic of the day"], ["The Third Topic of the day", "The Fourth Topic of the day"]]
      +[["The Third Topic of the day", "The Fourth Topic of the day"], ["The First Topic", "The Second Topic of the day"]]
      ```
      86384ea8
  19. 22 5月, 2019 2 次提交
  20. 08 4月, 2019 1 次提交
  21. 04 4月, 2019 1 次提交
    • 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
  22. 26 2月, 2019 1 次提交
  23. 24 2月, 2019 2 次提交
  24. 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
  25. 18 1月, 2019 3 次提交
  26. 04 1月, 2019 1 次提交
  27. 02 1月, 2019 1 次提交
  28. 06 12月, 2018 1 次提交
  29. 03 12月, 2018 1 次提交
  30. 23 11月, 2018 1 次提交
    • G
      Redact SQL in errors · 192b7bcf
      Gannon McGibbon 提交于
      Move `ActiveRecord::StatementInvalid` SQL to error property.
      Also add bindings as an error property.
      192b7bcf
  31. 19 6月, 2018 1 次提交
  32. 07 6月, 2018 1 次提交
    • R
      Fix GROUP BY queries to apply LIMIT/OFFSET after aggregations · 63e35a13
      Ryuta Kamizono 提交于
      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.
      63e35a13
  33. 09 3月, 2018 1 次提交