Skip to content

Commit

Permalink
Fix deparsing for array indirection (#162)
Browse files Browse the repository at this point in the history
Examples:

SELECT  "proname", (SELECT regexp_split_to_array( "proargtypes"::text, ' ') )[ "idx"] AS argtype,  "proargnames "[ "idx"] AS argname FROM  "pg_proc"

SELECT ("k" #= hstore('{id}'::text[], ARRAY[1::text])).* FROM "test" k
  • Loading branch information
emin100 authored Dec 27, 2020
1 parent 6989cda commit 5d49e21
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/pg_query/deparse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,11 @@ def deparse_a_star(_node)
def deparse_a_indirection(node)
output = []
arg = deparse_item(node['arg'])
output << if node['arg'].key?(FUNC_CALL) || node['arg'].key?(SUB_LINK)
array_indirection = []
if node['indirection']
array_indirection = node['indirection'].select { |a| a.key?(A_INDICES) }
end
output << if node['arg'].key?(FUNC_CALL) || node['arg'].key?(A_EXPR) || (node['arg'].key?(SUB_LINK) && array_indirection.count.zero?)
"(#{arg})."
else
arg
Expand Down
12 changes: 12 additions & 0 deletions spec/lib/pg_query/deparse_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,12 @@
it { is_expected.to eq query }
end

context 'array indirection' do
let(:query) { "SELECT \"proname\", (SELECT regexp_split_to_array(\"proargtypes\"::text, ' ') )[\"idx\"] AS argtype, \"proargnames\"[\"idx\"] AS argname FROM \"pg_proc\"" }

it { is_expected.to eq oneline_query }
end

context 'sub query indirection' do
let(:query) { "SELECT COALESCE(((SELECT customer.sp_person(\"n\".\"id\") AS sp_person)).\"city_id\", NULL::int) AS city_id FROM \"customer\".\"tb_customer\" n" }

Expand All @@ -374,6 +380,12 @@
it { is_expected.to eq query }
end

context 'indirection with star' do
let(:query) { 'SELECT ("k" #= hstore(\'{id}\'::text[], ARRAY[1::text])).* FROM "test" k' }

it { is_expected.to eq query }
end

context 'NOT' do
let(:query) { 'SELECT * FROM "x" WHERE NOT "y"' }

Expand Down

0 comments on commit 5d49e21

Please sign in to comment.