Skip to content

Commit

Permalink
Fix bad formatting of materialized view ddl with parameters. Thanks t…
Browse files Browse the repository at this point in the history
…o Wilson Lin for the report.
  • Loading branch information
darold committed Dec 3, 2021
1 parent 8fcbe18 commit dffa2b5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
21 changes: 18 additions & 3 deletions lib/pgFormatter/Beautify.pm
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,7 @@ sub beautify
$self->{ '_mysql_delimiter' } = '';
$self->{ '_is_in_generated' } = 0;
$self->{ '_is_in_between' } = 0;
$self->{ '_is_in_materialized' } = 0;

@{ $self->{ '_begin_level' } } = ();

Expand Down Expand Up @@ -889,6 +890,11 @@ sub beautify
next;
}

# mark when we are processing a materialized view to avoid formating issue with parameters
if (uc($token) eq 'MATERIALIZED' and uc($self->_next_token) eq 'VIEW') {
$self->{ '_is_in_materialized' } = 1;
}

####
# Find if the current keyword is a known function name
####
Expand Down Expand Up @@ -979,21 +985,24 @@ sub beautify
{
if (!$self->{ '_is_in_partition' } and !$self->{ '_is_in_publication' } and !$self->{ '_is_in_policy' })
{
$self->{ '_is_in_with' } = 1 if (!$self->{ '_is_in_using' }
$self->{ '_is_in_with' } = 1 if (!$self->{ '_is_in_using' } and !$self->{ '_is_in_materialized' }
and uc($self->_next_token) ne 'ORDINALITY' and uc($last) ne 'START');
$self->{ 'no_break' } = 1 if (uc($self->_next_token) eq 'ORDINALITY');
}
$self->{ '_is_in_materialized' } = 0;
}
elsif ($token =~ /^WITH$/i && uc($self->_next_token) eq 'ORDINALITY')
{
$self->{ 'no_break' } = 1;
}
elsif ($token =~ /^(AS|IS)$/i && defined $self->_next_token && $self->_next_token =~ /^(NOT|\()$/)
{
$self->{ '_is_in_materialized' } = 0;
$self->{ '_is_in_with' }++ if ($self->{ '_is_in_with' } == 1);
}
elsif ($self->{ '_is_in_create' } && $token =~ /^AS$/i && defined $self->_next_token && uc($self->_next_token) eq 'SELECT')
{
$self->{ '_is_in_materialized' } = 0;
$self->{ '_is_in_create' } = 0;
}
elsif ( $token eq '[' )
Expand Down Expand Up @@ -1026,6 +1035,7 @@ sub beautify
if ($self->{ '_is_in_create' } > 1 and defined $self->_next_token
and uc($self->_next_token) eq 'AS' and !$self->{ '_is_in_with'})
{
$self->{ '_is_in_materialized' } = 0;
$self->_new_line($token,$last) if ($last ne '(' and !$self->{ '_is_in_create' });
if ($self->{ '_is_in_returns_table' } and !$self->{ '_parenthesis_level' })
{
Expand Down Expand Up @@ -1227,12 +1237,16 @@ sub beautify
}
}
# Desactivate index like formatting when RETURN(S) keyword is found
elsif ($token =~ /^(RETURN|RETURNS)$/i) {
elsif ($token =~ /^(RETURN|RETURNS)$/i)
{
$self->{ '_is_in_index' } = 0;
if (uc($token) eq 'RETURNS' and uc ($self->_next_token()) eq 'TABLE') {
$self->{ '_is_in_returns_table' } = 1;
}
} elsif ($token =~ /^AS$/i) {
}
elsif ($token =~ /^AS$/i)
{
$self->{ '_is_in_materialized' } = 0;
if ( !$self->{ '_is_in_index' } and $self->{ '_is_in_from' } and $last eq ')' and uc($token) eq 'AS' and $self->_next_token() eq '(') {
$self->{ '_is_in_index' } = 1;
} else {
Expand Down Expand Up @@ -1879,6 +1893,7 @@ sub beautify
$self->{ '_not_a_type' } = 0;
$self->{ '_is_subquery' } = 0;
$self->{ '_is_in_order_by' } = 0;
$self->{ '_is_in_materialized' } = 0;

if ( $self->{ '_insert_values' } )
{
Expand Down
16 changes: 16 additions & 0 deletions t/test-files/ex49.sql
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,19 @@ CREATE aggregate my_sum_init (

CREATE AGGREGATE myaggn08a (BASETYPE = anyelement, SFUNC = tf2p, STYPE = int[], FINALFUNC = ffnp, INITCOND = '{}');


create materialized view v as
select
sum(x) as x,
sum(y) as y,
sum(z) as z
from
t;

create materialized view v with (storage_param=1) as
select
sum(x) as x,
sum(y) as y,
sum(z) as z
from
t;
16 changes: 16 additions & 0 deletions t/test-files/expected/ex49.sql
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,19 @@ CREATE AGGREGATE myaggn08a (
INITCOND = '{}'
);

CREATE MATERIALIZED VIEW v AS
SELECT
sum(x) AS x,
sum(y) AS y,
sum(z) AS z
FROM
t;

CREATE MATERIALIZED VIEW v WITH (storage_param = 1) AS
SELECT
sum(x) AS x,
sum(y) AS y,
sum(z) AS z
FROM
t;

0 comments on commit dffa2b5

Please sign in to comment.