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

Index Result rows rather than to convert them into hashes #51744

Merged
merged 1 commit into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,8 @@ def table_structure_with_collation(table_name, basic_structure)
end

basic_structure.map do |column|
column = column.to_h

column_name = column["name"]

if collation_hash.has_key? column_name
Expand Down
75 changes: 68 additions & 7 deletions activerecord/lib/active_record/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,59 @@ module ActiveRecord
class Result
include Enumerable

class IndexedRow
def initialize(column_indexes, row)
@column_indexes = column_indexes
@row = row
end

def size
@column_indexes.size
end
alias_method :length, :size

def each_key(&block)
@column_indexes.each_key(&block)
end

def keys
@column_indexes.keys
end

def ==(other)
if other.is_a?(Hash)
to_hash == other
else
super
end
end

def key?(column)
@column_indexes.key?(column)
end

def fetch(column)
if index = @column_indexes[column]
@row[index]
elsif block_given?
yield
else
raise KeyError, "key not found: #{column.inspect}"
end
end

def [](column)
if index = @column_indexes[column]
@row[index]
end
end

def to_h
@column_indexes.transform_values { |index| @row[index] }
end
alias_method :to_hash, :to_h
end

attr_reader :columns, :rows, :column_types

def self.empty(async: false) # :nodoc:
Expand Down Expand Up @@ -67,14 +120,16 @@ def length
end

# Calls the given block once for each element in row collection, passing
# row as parameter.
# row as parameter. Each row is a Hash-like, read only object.
#
# To get real hashes, use +.to_a.each+.
#
# Returns an +Enumerator+ if no block is given.
def each(&block)
if block_given?
hash_rows.each(&block)
indexed_rows.each(&block)
else
hash_rows.to_enum { @rows.size }
indexed_rows.to_enum { @rows.size }
end
end

Expand Down Expand Up @@ -134,14 +189,13 @@ def cast_values(type_overrides = {}) # :nodoc:
end

def initialize_copy(other)
@columns = columns
@rows = rows.dup
@rows = rows.dup
@column_types = column_types.dup
@hash_rows = nil
end

def freeze # :nodoc:
hash_rows.freeze
indexed_rows.freeze
super
end

Expand All @@ -154,7 +208,7 @@ def column_indexes # :nodoc:
hash[columns[index]] = index
index += 1
end
hash
hash.freeze
end
end

Expand All @@ -167,6 +221,13 @@ def column_type(name, index, type_overrides)
end
end

def indexed_rows
@indexed_rows ||= begin
columns = column_indexes
@rows.map { |row| IndexedRow.new(columns, row) }.freeze
end
end

def hash_rows
# We use transform_values to rows.
# This is faster because we avoid any reallocs and avoid hashing entirely.
Expand Down