Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid coercing all SELECTs with the same column name to the same value #36186

Closed

Commits on Apr 22, 2020

  1. Avoid coercing all SELECTs with the same column name to the same value

    Fixes rails#36042
    
    Rails had been mapping types to columns in a Result Set by indexing types on columns' names. When two values in a Result Set have the same column name, they are forced to share a type.
    
    There are several scenarios where multiple values may share the same column name.
    
    1. One (where two tables happen to have columns with the same name but the column's values differ) is outlined in rails#36042.
    
    2. Another is when using functions in a `.select` or `.pluck`. In the absence of an Alias clause, Postgres names values with the last function called, so if two values in the same query happen to use `COALESCE`, they'll both be named `"coalesce"`.
    
       ```ruby
       ActiveRecord::Base.pluck(
         Arel.sql("COALESCE(NULL, 'four')"),
         Arel.sql("COALESCE(NULL, 4)")
       )
       ```
    
    This commit matches `types` with `columns` and `values` (in a row) by array position (which is an assumption already made in `PostgreSQL::DatabaseStatements#exec_query`). It adds `column_types` as a public method on `ActiveRecord::Result` to preserve the class's public API.
    
    Does Rails expect client code to be constructing `ActiveRecord::Result` objects? Do we need to check whether its third argument is a `Hash`, give — maybe — a deprecation warning and convert it to an `Array`?
    boblail committed Apr 22, 2020
    Configuration menu
    Copy the full SHA
    20f3f0e View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    cb0dc11 View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    6b57bf9 View commit details
    Browse the repository at this point in the history
  4. Configuration menu
    Copy the full SHA
    f4630d8 View commit details
    Browse the repository at this point in the history