diff --git a/lib/pg_query/deparse.rb b/lib/pg_query/deparse.rb index 2e160df8..8009af03 100644 --- a/lib/pg_query/deparse.rb +++ b/lib/pg_query/deparse.rb @@ -1159,7 +1159,8 @@ def deparse_typecast(node) if deparse_item(node['typeName']) == 'boolean' deparse_item(node['arg']) == "'t'" ? 'true' : 'false' else - deparse_item(node['arg']) + '::' + deparse_typename(node['typeName'][TYPE_NAME]) + context = true if node['arg']['A_Expr'] + deparse_item(node['arg'], context) + '::' + deparse_typename(node['typeName'][TYPE_NAME]) end end diff --git a/spec/lib/pg_query/deparse_spec.rb b/spec/lib/pg_query/deparse_spec.rb index ea605639..201b62c1 100644 --- a/spec/lib/pg_query/deparse_spec.rb +++ b/spec/lib/pg_query/deparse_spec.rb @@ -519,6 +519,12 @@ it { is_expected.to eq query } end + context 'with parentheses' do + let(:query) { "SELECT (1 + 3)::int8" } + + it { is_expected.to eq query } + end + context 'regclass' do let(:query) { "SELECT ?::regclass" }