Skip to content

Commit

Permalink
Improve deparsing of OVERLAY keyword function (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
emin100 authored and lfittl committed Dec 27, 2020
1 parent 5d49e21 commit 36ad3cc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
16 changes: 11 additions & 5 deletions lib/pg_query/deparse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -413,11 +413,17 @@ def deparse_funccall(node)
# COUNT(*)
args << '*' if node['agg_star']

name = (node['funcname'].map { |n| deparse_item(n, FUNC_CALL) } - ['pg_catalog']).join('.')
distinct = node['agg_distinct'] ? 'DISTINCT ' : ''
output << format('%s(%s%s)', name, distinct, args.join(', '))
output << format('FILTER (WHERE %s)', deparse_item(node['agg_filter'])) if node['agg_filter']
output << format('OVER %s', deparse_item(node['over'])) if node['over']
name = (node['funcname'].map { |n| deparse_item(n, FUNC_CALL) }).join('.')
if name == 'pg_catalog.overlay'
# Note that this is a bit odd, but "OVERLAY" is a keyword on its own merit, and only accepts the
# keyword parameter style when its called as a keyword, not as a regular function (i.e. pg_catalog.overlay)
output << format('OVERLAY(%s PLACING %s FROM %s FOR %s)', args[0], args[1], args[2], args[3])
else
distinct = node['agg_distinct'] ? 'DISTINCT ' : ''
output << format('%s(%s%s)', name, distinct, args.join(', '))
output << format('FILTER (WHERE %s)', deparse_item(node['agg_filter'])) if node['agg_filter']
output << format('OVER %s', deparse_item(node['over'])) if node['over']
end

output.join(' ')
end
Expand Down
6 changes: 6 additions & 0 deletions spec/lib/pg_query/deparse_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@
it { is_expected.to eq oneline_query }
end

context 'OVERLAY' do
let(:query) { 'SELECT OVERLAY("m"."name" PLACING \'******\' FROM 3 FOR 6) AS tc_kimlik FROM "tb_test" m' }

it { is_expected.to eq query }
end

context 'SUM' do
let(:query) { 'SELECT sum("price_cents") FROM "products"' }

Expand Down

0 comments on commit 36ad3cc

Please sign in to comment.