diff --git a/lib/pg_query/deparse.rb b/lib/pg_query/deparse.rb index 2dbfe59d..fc4ea626 100644 --- a/lib/pg_query/deparse.rb +++ b/lib/pg_query/deparse.rb @@ -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 diff --git a/spec/lib/pg_query/deparse_spec.rb b/spec/lib/pg_query/deparse_spec.rb index b99e9d65..7ac9f75d 100644 --- a/spec/lib/pg_query/deparse_spec.rb +++ b/spec/lib/pg_query/deparse_spec.rb @@ -334,6 +334,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" } @@ -346,6 +352,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"' }