Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

overlay deparse problem fixed #161

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ GEM
powerpack (0.1.1)
rainbow (2.2.2)
rake
rake (12.0.0)
rake (13.0.1)
rake-compiler (0.9.9)
rake
rspec (3.6.0)
Expand Down
2 changes: 1 addition & 1 deletion ext/pg_query/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'mkmf'
require 'open-uri'

LIB_PG_QUERY_TAG = '10-1.0.1'.freeze
LIB_PG_QUERY_TAG = '10-1.0.3'.freeze

workdir = Dir.pwd
libdir = File.join(workdir, 'libpg_query-' + LIB_PG_QUERY_TAG)
Expand Down
10 changes: 7 additions & 3 deletions lib/pg_query/deparse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -410,9 +410,13 @@ def deparse_funccall(node)
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('OVER %s', deparse_item(node['over'])) if node['over']
if name == 'overlay'
output << format('%s(%s placing %s from %s for %s)', name, 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('OVER %s', deparse_item(node['over'])) if node['over']
end

output.join(' ')
end
Expand Down
3 changes: 3 additions & 0 deletions lib/pg_query/parse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ def load_tables_and_aliases! # rubocop:disable Metrics/CyclomaticComplexity
if statement[CREATE_TABLE_AS_STMT]['into'] && statement[CREATE_TABLE_AS_STMT]['into'][INTO_CLAUSE]['rel']
from_clause_items << { item: statement[CREATE_TABLE_AS_STMT]['into'][INTO_CLAUSE]['rel'], type: :ddl }
end
if statement[CREATE_TABLE_AS_STMT]['query']
statements << statement[CREATE_TABLE_AS_STMT]['query']
end
when TRUNCATE_STMT
from_clause_items += statement.values[0]['relations'].map { |r| { item: r, type: :ddl } }
when VIEW_STMT
Expand Down
6 changes: 3 additions & 3 deletions spec/lib/normalized_parsing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def parse_expr(expr)
# support that due to keyword/function duality (e.g. JOIN)
expect { described_class.parse("SELECT ? 10") }.to raise_error do |error|
expect(error).to be_a(described_class::ParseError)
expect(error.message).to eq "syntax error at or near \"10\" (scan.l:1121)"
expect(error.message).to eq "syntax error at or near \"10\" (scan.l:1162)"
end
end

Expand Down Expand Up @@ -172,7 +172,7 @@ def parse_expr(expr)

it "parses ?!=?" do
e = parse_expr("?!=?")
expect(e["name"]).to eq [{"String"=>{"str"=>"!="}}]
expect(e["name"]).to eq [{"String"=>{"str"=>"<>"}}]
expect(e["lexpr"][described_class::PARAM_REF]).not_to be_nil
expect(e["rexpr"][described_class::PARAM_REF]).not_to be_nil
end
Expand All @@ -186,7 +186,7 @@ def parse_expr(expr)

it "parses x!=?" do
e = parse_expr("x!=?")
expect(e["name"]).to eq [{"String"=>{"str"=>"!="}}]
expect(e["name"]).to eq [{"String"=>{"str"=>"<>"}}]
expect(e["lexpr"][described_class::COLUMN_REF]).not_to be_nil
expect(e["rexpr"][described_class::PARAM_REF]).not_to be_nil
end
Expand Down
25 changes: 24 additions & 1 deletion spec/lib/parse_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
it "handles errors" do
expect { described_class.parse("SELECT 'ERR") }.to(raise_error do |error|
expect(error).to be_a(described_class::ParseError)
expect(error.message).to eq "unterminated quoted string at or near \"'ERR\" (scan.l:1121)"
expect(error.message).to eq "unterminated quoted string at or near \"'ERR\" (scan.l:1162)"
expect(error.location).to eq 8 # 8th character in query string
end)
end
Expand Down Expand Up @@ -931,4 +931,27 @@
'defaction' => 0,
'location' => 44 } }] } }}}]
end

describe 'parsing CREATE TABLE AS' do
it 'finds tables in the subquery' do
query = described_class.parse(<<-SQL)
CREATE TABLE foo AS
SELECT * FROM bar;
SQL
expect(query.tables).to eq(['foo', 'bar'])
expect(query.ddl_tables).to eq(['foo'])
expect(query.select_tables).to eq(['bar'])
end

it 'finds tables in the subquery with UNION' do
query = described_class.parse(<<-SQL)
CREATE TABLE foo AS
SELECT id FROM bar UNION SELECT id from baz;
SQL

expect(query.tables).to eq(['foo', 'bar', 'baz'])
expect(query.ddl_tables).to eq(['foo'])
expect(query.select_tables).to eq(['bar', 'baz'])
end
end
end
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 @@ -163,6 +163,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