Skip to content

Commit

Permalink
Avoid casting of OIDs to fix compat with Redshift database
Browse files Browse the repository at this point in the history
Redshift doesn't support OID these casts, so we do OID lookup through the pg_proc table and add type 'name' as an alias for 'text'.

Also clean up the type map generation, so that only arrays are separated and all other types can be registered.

Fixes ged#369
  • Loading branch information
larskanis committed Feb 19, 2021
1 parent 8b46376 commit 365008e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 30 deletions.
36 changes: 9 additions & 27 deletions lib/pg/basic_type_mapping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,32 +40,17 @@ class CoderMap
def initialize(result, coders_by_name, format, arraycoder)
coder_map = {}

_ranges, nodes = result.partition { |row| row['typinput'] == 'range_in' }
leaves, nodes = nodes.partition { |row| row['typelem'].to_i == 0 }
arrays, nodes = nodes.partition { |row| row['typinput'] == 'array_in' }

# populate the enum types
_enums, leaves = leaves.partition { |row| row['typinput'] == 'enum_in' }
# enums.each do |row|
# coder_map[row['oid'].to_i] = OID::Enum.new
# end
arrays, nodes = result.partition { |row| row['typinput'] == 'array_in' }

# populate the base types
leaves.find_all { |row| coders_by_name.key?(row['typname']) }.each do |row|
nodes.find_all { |row| coders_by_name.key?(row['typname']) }.each do |row|
coder = coders_by_name[row['typname']].dup
coder.oid = row['oid'].to_i
coder.name = row['typname']
coder.format = format
coder_map[coder.oid] = coder
end

_records_by_oid = result.group_by { |row| row['oid'] }

# populate composite types
# nodes.each do |row|
# add_oid row, records_by_oid, coder_map
# end

if arraycoder
# populate array types
arrays.each do |row|
Expand All @@ -82,13 +67,6 @@ def initialize(result, coders_by_name, format, arraycoder)
end
end

# populate range types
# ranges.find_all { |row| coder_map.key? row['rngsubtype'].to_i }.each do |row|
# subcoder = coder_map[row['rngsubtype'].to_i]
# range = OID::Range.new subcoder
# coder_map[row['oid'].to_i] = range
# end

@coders = coder_map.values
@coders_by_name = @coders.inject({}){|h, t| h[t.name] = t; h }
@coders_by_oid = @coders.inject({}){|h, t| h[t.oid] = t; h }
Expand Down Expand Up @@ -118,14 +96,16 @@ def supports_ranges?(connection)
def build_coder_maps(connection)
if supports_ranges?(connection)
result = connection.exec <<-SQL
SELECT t.oid, t.typname::text, t.typelem, t.typdelim, t.typinput::text, r.rngsubtype
SELECT t.oid, t.typname, t.typelem, t.typdelim, ti.proname AS typinput, r.rngsubtype
FROM pg_type as t
LEFT JOIN pg_range as r ON oid = rngtypid
JOIN pg_proc as ti ON ti.oid = t.typinput
LEFT JOIN pg_range as r ON t.oid = r.rngtypid
SQL
else
result = connection.exec <<-SQL
SELECT t.oid, t.typname::text, t.typelem, t.typdelim, t.typinput::text
SELECT t.oid, t.typname, t.typelem, t.typdelim, ti.proname AS typinput
FROM pg_type as t
JOIN pg_proc as ti ON ti.oid = t.typinput
SQL
end

Expand Down Expand Up @@ -200,6 +180,7 @@ def self.alias_type(format, new, old)
alias_type 0, 'char', 'text'
alias_type 0, 'bpchar', 'text'
alias_type 0, 'xml', 'text'
alias_type 0, 'name', 'text'

# FIXME: why are we keeping these types as strings?
# alias_type 'tsvector', 'text'
Expand Down Expand Up @@ -248,6 +229,7 @@ def self.alias_type(format, new, old)
alias_type 1, 'char', 'text'
alias_type 1, 'bpchar', 'text'
alias_type 1, 'xml', 'text'
alias_type 1, 'name', 'text'

register_type 1, 'bytea', PG::BinaryEncoder::Bytea, PG::BinaryDecoder::Bytea
register_type 1, 'bool', PG::BinaryEncoder::Boolean, PG::BinaryDecoder::Boolean
Expand Down
7 changes: 4 additions & 3 deletions spec/pg/basic_type_mapping_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,10 @@ def to_s
it "should do string type conversions" do
@conn.internal_encoding = 'utf-8'
[1, 0].each do |format|
res = @conn.exec_params( "SELECT 'abcäöü'::TEXT", [], format )
expect( res.values ).to eq( [['abcäöü']] )
expect( res.values[0][0].encoding ).to eq( Encoding::UTF_8 )
res = @conn.exec_params( "SELECT 'abcäöü'::TEXT, 'colname'::name", [], format )
expect( res.values ).to eq( [['abcäöü', 'colname']] )
expect( [res.ftype(0), res.ftype(1)] ).to eq( [25, 19] )
expect( res.values[0].map(&:encoding) ).to eq( [Encoding::UTF_8] * 2 )
end
end

Expand Down

0 comments on commit 365008e

Please sign in to comment.