Skip to content

Commit

Permalink
Better indentation of subqueries inside a "WITH" clause. Thanks to Cy…
Browse files Browse the repository at this point in the history
…ril Chaboisseau for the report.
  • Loading branch information
darold committed Mar 28, 2020
1 parent 3197662 commit 0beffd1
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 131 deletions.
20 changes: 15 additions & 5 deletions lib/pgFormatter/Beautify.pm
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,7 @@ sub beautify
$self->{ '_has_limit' } = 0;
$self->{ '_not_a_type' } = 0;
$self->{ 'stmt_number' } = 1;
$self->{ '_is_subquery' } = 0;

my $last = '';
$self->tokenize_sql();
Expand Down Expand Up @@ -767,7 +768,7 @@ sub beautify
# Control case where we have to add a newline, go back and
# reset indentation after the last ) in the WITH statement
####
if ($token =~ /^WITH$/i && (!defined $last || $last ne ')')
if ($token =~ /^WITH$/i && (!defined $last or $last ne ')')
&& !$self->{ '_is_in_partition' } && !$self->{ '_is_in_publication' }
&& !$self->{ '_is_in_policy' } && uc($self->_next_token) ne 'TIME')
{
Expand Down Expand Up @@ -1292,6 +1293,7 @@ sub beautify
$self->{ '_is_in_create' }++ if ($self->{ '_is_in_create' });
$self->{ '_is_in_constraint' }++ if ($self->{ '_is_in_constraint' });
$self->_add_token( $token, $last );
$self->{ '_is_subquery' }++ if (defined $self->_next_token and uc($self->_next_token) eq 'SELECT');
if (defined $self->_next_token and $self->_next_token eq ')' and !$self->{ '_is_in_create' }) {
$last = $self->_set_last($token, $last);
next;
Expand Down Expand Up @@ -1388,7 +1390,6 @@ sub beautify
}
if (defined $self->_next_token && $self->_next_token !~ /FILTER/i)
{

my $add_nl = 0;
$add_nl = 1 if ($self->{ '_is_in_create' } > 1
and defined $last and $last ne '('
Expand Down Expand Up @@ -1438,9 +1439,17 @@ sub beautify
}

# When closing CTE statement go back again
if ($self->_next_token =~ /^(?:SELECT|INSERT|UPDATE|DELETE)$/i && !$self->{ '_is_in_policy' }) {
$self->_back($token, $last) if ($self->{ '_current_sql_stmt' } ne 'INSERT');
}
if ( ($self->_next_token =~ /^(?:SELECT|INSERT|UPDATE|DELETE)$/i and !$self->{ '_is_in_policy' })
or ($self->{ '_is_in_with' } and $self->{ '_is_subquery' }
and $self->{ '_is_subquery' } % 2 == 0) ) {
$self->_back($token, $last) if ($self->{ '_current_sql_stmt' } ne 'INSERT'
and (!$self->{ '_parenthesis_level' } or !defined $self->_next_token
or uc($self->_next_token) eq 'AS'
or ($#{$self->{ '_tokens' }} >= 1 and $self->{ '_tokens' }->[ 1 ] eq ',')));
}
$self->{ '_is_subquery' }-- if ($self->{ '_is_subquery' }
and defined $self->_next_token and $#{$self->{ '_tokens' }} >= 1
and (uc($self->_next_token) eq 'AS' or $self->{ '_tokens' }->[ 1 ] eq ','));
if ($self->{ '_is_in_create' } <= 1) {
my $next_tok = quotemeta($self->_next_token);
$self->_new_line($token,$last)
Expand Down Expand Up @@ -1585,6 +1594,7 @@ sub beautify
$self->{ '_is_in_returns_table' } = 0;
$self->{ '_has_limit' } = 0;
$self->{ '_not_a_type' } = 0;
$self->{ '_is_subquery' } = 0;

if ( $self->{ '_insert_values' } )
{
Expand Down
50 changes: 25 additions & 25 deletions t/pg-test-files/expected/subselect.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1821,13 +1821,13 @@ AS ((
FROM
x
)
SELECT
z.a || z1.a AS a
FROM
z
CROSS JOIN z AS z1
WHERE
length(z.a || z1.a) < 5))
SELECT
z.a || z1.a AS a
FROM
z
CROSS JOIN z AS z1
WHERE
length(z.a || z1.a) < 5))
SELECT
*
FROM
Expand Down Expand Up @@ -1872,12 +1872,12 @@ AS ((
FROM
x
)
SELECT
z.a || z.a AS a
FROM
z
WHERE
length(z.a || z.a) < 5))
SELECT
z.a || z.a AS a
FROM
z
WHERE
length(z.a || z.a) < 5))
SELECT
*
FROM
Expand Down Expand Up @@ -1926,10 +1926,10 @@ FROM ( WITH y AS (
FROM
x
)
SELECT
*
FROM
y) ss;
SELECT
*
FROM
y) ss;

EXPLAIN (
VERBOSE,
Expand All @@ -1948,10 +1948,10 @@ FROM ( WITH y AS (
FROM
x
)
SELECT
*
FROM
y) ss;
SELECT
*
FROM
y) ss;

-- Ensure that we inline the currect CTE when there are
-- multiple CTEs with the same name
Expand All @@ -1969,10 +1969,10 @@ FROM ( WITH x AS (
SELECT
2 AS y
)
SELECT
*
FROM
x) ss;
SELECT
*
FROM
x) ss;

-- Row marks are not pushed into CTEs
EXPLAIN (
Expand Down
Loading

0 comments on commit 0beffd1

Please sign in to comment.