rails
rails copied to clipboard
[AR] Fix result with anonymous PG columns of different type from json
Fix #45758
Updates ActiveRecord PostgreSQL adapter and ActiveRecord::Result to handle DB result with no-name columns (?column?
) of different type.
cc @fatkodima @skipkayhil
Test failures look real
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 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.
@skipkayhil @fatkodima The tests are now fixed with having types
stored for both column names and column indexes. Thank you.
@fatkodima @skipkayhil Hi. Merged main
branch, resolved the conflicts.
What do you think about the PR?
Thank you.
Looks good to me. Make sure you squashed the commits.
@fatkodima Squashed. Thank you!
:thinking: CI seems sad about the change, is that failure anyhow related? By quick look it doesn't seem to.
@simi I think failed CI tests are unrelated to the changes. Thank you.