rails icon indicating copy to clipboard operation
rails copied to clipboard

[AR] Fix result with anonymous PG columns of different type from json

Open shhavel opened this issue 1 year ago • 4 comments

Fix #45758

Updates ActiveRecord PostgreSQL adapter and ActiveRecord::Result to handle DB result with no-name columns (?column?) of different type.

cc @fatkodima @skipkayhil

shhavel avatar Aug 06 '22 19:08 shhavel

Test failures look real

skipkayhil avatar Aug 06 '22 21:08 skipkayhil

I investigated a bit. So Result#column_types are accessed directly in several places by name, like https://github.com/rails/rails/blob/aa00ef35eb0314c2b68103a7dfaf2d75a33cbc0b/activerecord/lib/active_record/querying.rb#L66-L70 or https://github.com/rails/rails/blob/aa00ef35eb0314c2b68103a7dfaf2d75a33cbc0b/activerecord/lib/active_record/relation/calculations.rb#L427 and more. But we want to be accessed by the index. So that's why CI is red.

We can make Result#column_type method public https://github.com/rails/rails/blob/aa00ef35eb0314c2b68103a7dfaf2d75a33cbc0b/activerecord/lib/active_record/result.rb#L143-L148 and implement it with something like

def column_type(name, type_overrides = {})
  type_overrides.fetch(name) do
    column_types.fetch(name) do
      index = columns.index(name)
      column_types[index] || Type.default_value
    end
end

and use it everywhere I mentioned before?

This time we will have a small overhead from index = columns.index(name). Probably needs benchmarking.

fatkodima avatar Aug 07 '22 13:08 fatkodima

@fatkodima Thank you so much for the guidance, review and investigation so far.

I think to workaround the issue with duplicated column names in PG result and also to maintain the current behaviour of AR::Result#column_types, it is possible to save type for both column's index and column's name. For column's index store explicitly a default type. And prioritise column's index type lookup over column's name type lookup.

shhavel avatar Aug 07 '22 16:08 shhavel

@skipkayhil @fatkodima The tests are now fixed with having types stored for both column names and column indexes. Thank you.

shhavel avatar Aug 08 '22 09:08 shhavel

@fatkodima @skipkayhil Hi. Merged main branch, resolved the conflicts. What do you think about the PR? Thank you.

shhavel avatar Nov 11 '22 08:11 shhavel

Looks good to me. Make sure you squashed the commits.

fatkodima avatar Nov 11 '22 10:11 fatkodima

@fatkodima Squashed. Thank you!

shhavel avatar Nov 11 '22 11:11 shhavel

:thinking: CI seems sad about the change, is that failure anyhow related? By quick look it doesn't seem to.

simi avatar Nov 11 '22 15:11 simi

@simi I think failed CI tests are unrelated to the changes. Thank you.

shhavel avatar Nov 12 '22 00:11 shhavel