Fix ruleutils pretty-printing to not generate trailing whitespace.

The pretty-printing logic in ruleutils.c operates by inserting a newline
and some indentation whitespace into strings that are already valid SQL.
This naturally results in leaving some trailing whitespace before the
newline in many cases; which can be annoying when processing the output
with other tools, as complained of by Joe Abbate.  We can fix that in
a pretty localized fashion by deleting any trailing whitespace before
we append a pretty-printing newline.  In addition, we have to modify the
code inserted by commit 2f582f76b1 so that
we also delete trailing whitespace when transposing items from temporary
buffers into the main result string, when a temporary item starts with a
newline.

This results in rather voluminous changes to the regression test results,
but it's easily verified that they are only removal of trailing whitespace.

Back-patch to 9.3, because the aforementioned commit resulted in many
more cases of trailing whitespace than had occurred in earlier branches.
This commit is contained in:
Tom Lane 2013-11-11 13:36:38 -05:00
parent 648bd05b13
commit 0b7e660d6c
9 changed files with 725 additions and 694 deletions

View File

@ -365,6 +365,7 @@ static const char *get_simple_binary_op_name(OpExpr *expr);
static bool isSimpleNode(Node *node, Node *parentNode, int prettyFlags);
static void appendContextKeyword(deparse_context *context, const char *str,
int indentBefore, int indentAfter, int indentPlus);
static void removeStringInfoSpaces(StringInfo str);
static void get_rule_expr(Node *node, deparse_context *context,
bool showimplicit);
static void get_oper_expr(OpExpr *expr, deparse_context *context);
@ -4479,42 +4480,42 @@ get_target_list(List *targetList, deparse_context *context,
/* Consider line-wrapping if enabled */
if (PRETTY_INDENT(context) && context->wrapColumn >= 0)
{
int leading_nl_pos = -1;
char *trailing_nl;
int pos;
int leading_nl_pos;
/* Does the new field start with whitespace plus a new line? */
for (pos = 0; pos < targetbuf.len; pos++)
{
if (targetbuf.data[pos] == '\n')
{
leading_nl_pos = pos;
break;
}
if (targetbuf.data[pos] != ' ')
break;
}
/* Locate the start of the current line in the output buffer */
trailing_nl = strrchr(buf->data, '\n');
if (trailing_nl == NULL)
trailing_nl = buf->data;
/* Does the new field start with a new line? */
if (targetbuf.len > 0 && targetbuf.data[0] == '\n')
leading_nl_pos = 0;
else
trailing_nl++;
leading_nl_pos = -1;
/*
* If the field we're adding is the first in the list, or it
* already has a leading newline, don't add anything. Otherwise,
* add a newline, plus some indentation, if either the new field
* would cause an overflow or the last field used more than one
* line.
*/
if (colno > 1 &&
leading_nl_pos == -1 &&
((strlen(trailing_nl) + strlen(targetbuf.data) > context->wrapColumn) ||
last_was_multiline))
appendContextKeyword(context, "", -PRETTYINDENT_STD,
PRETTYINDENT_STD, PRETTYINDENT_VAR);
/* If so, we shouldn't add anything */
if (leading_nl_pos >= 0)
{
/* instead, remove any trailing spaces currently in buf */
removeStringInfoSpaces(buf);
}
else
{
char *trailing_nl;
/* Locate the start of the current line in the output buffer */
trailing_nl = strrchr(buf->data, '\n');
if (trailing_nl == NULL)
trailing_nl = buf->data;
else
trailing_nl++;
/*
* Add a newline, plus some indentation, if the new field is
* not the first and either the new field would cause an
* overflow or the last field used more than one line.
*/
if (colno > 1 &&
((strlen(trailing_nl) + targetbuf.len > context->wrapColumn) ||
last_was_multiline))
appendContextKeyword(context, "", -PRETTYINDENT_STD,
PRETTYINDENT_STD, PRETTYINDENT_VAR);
}
/* Remember this field's multiline status for next iteration */
last_was_multiline =
@ -6236,23 +6237,42 @@ static void
appendContextKeyword(deparse_context *context, const char *str,
int indentBefore, int indentAfter, int indentPlus)
{
StringInfo buf = context->buf;
if (PRETTY_INDENT(context))
{
context->indentLevel += indentBefore;
appendStringInfoChar(context->buf, '\n');
appendStringInfoSpaces(context->buf,
/* remove any trailing spaces currently in the buffer ... */
removeStringInfoSpaces(buf);
/* ... then add a newline and some spaces */
appendStringInfoChar(buf, '\n');
appendStringInfoSpaces(buf,
Max(context->indentLevel, 0) + indentPlus);
appendStringInfoString(context->buf, str);
appendStringInfoString(buf, str);
context->indentLevel += indentAfter;
if (context->indentLevel < 0)
context->indentLevel = 0;
}
else
appendStringInfoString(context->buf, str);
appendStringInfoString(buf, str);
}
/*
* removeStringInfoSpaces - delete trailing spaces from a buffer.
*
* Possibly this should move to stringinfo.c at some point.
*/
static void
removeStringInfoSpaces(StringInfo str)
{
while (str->len > 0 && str->data[str->len - 1] == ' ')
str->data[--(str->len)] = '\0';
}
/*
* get_rule_expr_paren - deparse expr using get_rule_expr,
* embracing the string with parentheses if necessary for prettyPrint.
@ -7942,22 +7962,33 @@ get_from_clause(Query *query, const char *prefix, deparse_context *context)
/* Consider line-wrapping if enabled */
if (PRETTY_INDENT(context) && context->wrapColumn >= 0)
{
char *trailing_nl;
/* Locate the start of the current line in the buffer */
trailing_nl = strrchr(buf->data, '\n');
if (trailing_nl == NULL)
trailing_nl = buf->data;
/* Does the new item start with a new line? */
if (itembuf.len > 0 && itembuf.data[0] == '\n')
{
/* If so, we shouldn't add anything */
/* instead, remove any trailing spaces currently in buf */
removeStringInfoSpaces(buf);
}
else
trailing_nl++;
{
char *trailing_nl;
/*
* Add a newline, plus some indentation, if the new item would
* cause an overflow.
*/
if (strlen(trailing_nl) + strlen(itembuf.data) > context->wrapColumn)
appendContextKeyword(context, "", -PRETTYINDENT_STD,
PRETTYINDENT_STD, PRETTYINDENT_VAR);
/* Locate the start of the current line in the buffer */
trailing_nl = strrchr(buf->data, '\n');
if (trailing_nl == NULL)
trailing_nl = buf->data;
else
trailing_nl++;
/*
* Add a newline, plus some indentation, if the new item
* would cause an overflow.
*/
if (strlen(trailing_nl) + itembuf.len > context->wrapColumn)
appendContextKeyword(context, "", -PRETTYINDENT_STD,
PRETTYINDENT_STD,
PRETTYINDENT_VAR);
}
}
/* Add the new item */

View File

@ -960,10 +960,10 @@ select * from agg_view1;
(1 row)
select pg_get_viewdef('agg_view1'::regclass);
pg_get_viewdef
----------------------------------------------------------------------------------------------------------------------
SELECT aggfns(DISTINCT v.a, v.b, v.c) AS aggfns +
FROM ( VALUES (1,3,'foo'::text), (0,NULL::integer,NULL::text), (2,2,'bar'::text), (3,1,'baz'::text)) v(a, b, c), +
pg_get_viewdef
---------------------------------------------------------------------------------------------------------------------
SELECT aggfns(DISTINCT v.a, v.b, v.c) AS aggfns +
FROM ( VALUES (1,3,'foo'::text), (0,NULL::integer,NULL::text), (2,2,'bar'::text), (3,1,'baz'::text)) v(a, b, c),+
generate_series(1, 3) i(i);
(1 row)
@ -978,10 +978,10 @@ select * from agg_view1;
(1 row)
select pg_get_viewdef('agg_view1'::regclass);
pg_get_viewdef
----------------------------------------------------------------------------------------------------------------------
SELECT aggfns(DISTINCT v.a, v.b, v.c ORDER BY v.b) AS aggfns +
FROM ( VALUES (1,3,'foo'::text), (0,NULL::integer,NULL::text), (2,2,'bar'::text), (3,1,'baz'::text)) v(a, b, c), +
pg_get_viewdef
---------------------------------------------------------------------------------------------------------------------
SELECT aggfns(DISTINCT v.a, v.b, v.c ORDER BY v.b) AS aggfns +
FROM ( VALUES (1,3,'foo'::text), (0,NULL::integer,NULL::text), (2,2,'bar'::text), (3,1,'baz'::text)) v(a, b, c),+
generate_series(1, 3) i(i);
(1 row)
@ -1044,10 +1044,10 @@ select * from agg_view1;
(1 row)
select pg_get_viewdef('agg_view1'::regclass);
pg_get_viewdef
----------------------------------------------------------------------------------------------------------------------
SELECT aggfns(DISTINCT v.a, v.b, v.c ORDER BY v.a, v.c USING ~<~ NULLS LAST, v.b) AS aggfns +
FROM ( VALUES (1,3,'foo'::text), (0,NULL::integer,NULL::text), (2,2,'bar'::text), (3,1,'baz'::text)) v(a, b, c), +
pg_get_viewdef
---------------------------------------------------------------------------------------------------------------------
SELECT aggfns(DISTINCT v.a, v.b, v.c ORDER BY v.a, v.c USING ~<~ NULLS LAST, v.b) AS aggfns +
FROM ( VALUES (1,3,'foo'::text), (0,NULL::integer,NULL::text), (2,2,'bar'::text), (3,1,'baz'::text)) v(a, b, c),+
generate_series(1, 2) i(i);
(1 row)

View File

@ -312,8 +312,8 @@ CREATE VIEW aliased_view_4 AS
f2 | integer | | plain |
f3 | text | | extended |
View definition:
SELECT tt1.f1,
tt1.f2,
SELECT tt1.f1,
tt1.f2,
tt1.f3
FROM tt1
WHERE (EXISTS ( SELECT 1
@ -328,8 +328,8 @@ View definition:
f2 | integer | | plain |
f3 | text | | extended |
View definition:
SELECT a1.f1,
a1.f2,
SELECT a1.f1,
a1.f2,
a1.f3
FROM tt1 a1
WHERE (EXISTS ( SELECT 1
@ -344,8 +344,8 @@ View definition:
f2 | integer | | plain |
f3 | text | | extended |
View definition:
SELECT tt1.f1,
tt1.f2,
SELECT tt1.f1,
tt1.f2,
tt1.f3
FROM tt1
WHERE (EXISTS ( SELECT 1
@ -360,8 +360,8 @@ View definition:
f2 | integer | | plain |
f3 | text | | extended |
View definition:
SELECT tt1.y1,
tt1.f2,
SELECT tt1.y1,
tt1.f2,
tt1.f3
FROM temp_view_test.tt1
WHERE (EXISTS ( SELECT 1
@ -377,8 +377,8 @@ ALTER TABLE tx1 RENAME TO a1;
f2 | integer | | plain |
f3 | text | | extended |
View definition:
SELECT tt1.f1,
tt1.f2,
SELECT tt1.f1,
tt1.f2,
tt1.f3
FROM tt1
WHERE (EXISTS ( SELECT 1
@ -393,8 +393,8 @@ View definition:
f2 | integer | | plain |
f3 | text | | extended |
View definition:
SELECT a1.f1,
a1.f2,
SELECT a1.f1,
a1.f2,
a1.f3
FROM tt1 a1
WHERE (EXISTS ( SELECT 1
@ -409,8 +409,8 @@ View definition:
f2 | integer | | plain |
f3 | text | | extended |
View definition:
SELECT tt1.f1,
tt1.f2,
SELECT tt1.f1,
tt1.f2,
tt1.f3
FROM tt1
WHERE (EXISTS ( SELECT 1
@ -425,8 +425,8 @@ View definition:
f2 | integer | | plain |
f3 | text | | extended |
View definition:
SELECT tt1.y1,
tt1.f2,
SELECT tt1.y1,
tt1.f2,
tt1.f3
FROM temp_view_test.tt1
WHERE (EXISTS ( SELECT 1
@ -442,8 +442,8 @@ ALTER TABLE tt1 RENAME TO a2;
f2 | integer | | plain |
f3 | text | | extended |
View definition:
SELECT a2.f1,
a2.f2,
SELECT a2.f1,
a2.f2,
a2.f3
FROM a2
WHERE (EXISTS ( SELECT 1
@ -458,8 +458,8 @@ View definition:
f2 | integer | | plain |
f3 | text | | extended |
View definition:
SELECT a1.f1,
a1.f2,
SELECT a1.f1,
a1.f2,
a1.f3
FROM a2 a1
WHERE (EXISTS ( SELECT 1
@ -474,8 +474,8 @@ View definition:
f2 | integer | | plain |
f3 | text | | extended |
View definition:
SELECT a2.f1,
a2.f2,
SELECT a2.f1,
a2.f2,
a2.f3
FROM a2
WHERE (EXISTS ( SELECT 1
@ -490,8 +490,8 @@ View definition:
f2 | integer | | plain |
f3 | text | | extended |
View definition:
SELECT tt1.y1,
tt1.f2,
SELECT tt1.y1,
tt1.f2,
tt1.f3
FROM temp_view_test.tt1
WHERE (EXISTS ( SELECT 1
@ -507,8 +507,8 @@ ALTER TABLE a1 RENAME TO tt1;
f2 | integer | | plain |
f3 | text | | extended |
View definition:
SELECT a2.f1,
a2.f2,
SELECT a2.f1,
a2.f2,
a2.f3
FROM a2
WHERE (EXISTS ( SELECT 1
@ -523,8 +523,8 @@ View definition:
f2 | integer | | plain |
f3 | text | | extended |
View definition:
SELECT a1.f1,
a1.f2,
SELECT a1.f1,
a1.f2,
a1.f3
FROM a2 a1
WHERE (EXISTS ( SELECT 1
@ -539,8 +539,8 @@ View definition:
f2 | integer | | plain |
f3 | text | | extended |
View definition:
SELECT a2.f1,
a2.f2,
SELECT a2.f1,
a2.f2,
a2.f3
FROM a2
WHERE (EXISTS ( SELECT 1
@ -555,8 +555,8 @@ View definition:
f2 | integer | | plain |
f3 | text | | extended |
View definition:
SELECT tt1.y1,
tt1.f2,
SELECT tt1.y1,
tt1.f2,
tt1.f3
FROM temp_view_test.tt1
WHERE (EXISTS ( SELECT 1
@ -573,8 +573,8 @@ ALTER TABLE tx1 SET SCHEMA temp_view_test;
f2 | integer | | plain |
f3 | text | | extended |
View definition:
SELECT tx1.f1,
tx1.f2,
SELECT tx1.f1,
tx1.f2,
tx1.f3
FROM temp_view_test.tx1
WHERE (EXISTS ( SELECT 1
@ -589,8 +589,8 @@ View definition:
f2 | integer | | plain |
f3 | text | | extended |
View definition:
SELECT a1.f1,
a1.f2,
SELECT a1.f1,
a1.f2,
a1.f3
FROM temp_view_test.tx1 a1
WHERE (EXISTS ( SELECT 1
@ -605,8 +605,8 @@ View definition:
f2 | integer | | plain |
f3 | text | | extended |
View definition:
SELECT tx1.f1,
tx1.f2,
SELECT tx1.f1,
tx1.f2,
tx1.f3
FROM temp_view_test.tx1
WHERE (EXISTS ( SELECT 1
@ -621,8 +621,8 @@ View definition:
f2 | integer | | plain |
f3 | text | | extended |
View definition:
SELECT tt1.y1,
tt1.f2,
SELECT tt1.y1,
tt1.f2,
tt1.f3
FROM temp_view_test.tt1
WHERE (EXISTS ( SELECT 1
@ -640,8 +640,8 @@ ALTER TABLE tmp1 RENAME TO tx1;
f2 | integer | | plain |
f3 | text | | extended |
View definition:
SELECT tx1.f1,
tx1.f2,
SELECT tx1.f1,
tx1.f2,
tx1.f3
FROM temp_view_test.tx1
WHERE (EXISTS ( SELECT 1
@ -656,8 +656,8 @@ View definition:
f2 | integer | | plain |
f3 | text | | extended |
View definition:
SELECT a1.f1,
a1.f2,
SELECT a1.f1,
a1.f2,
a1.f3
FROM temp_view_test.tx1 a1
WHERE (EXISTS ( SELECT 1
@ -672,8 +672,8 @@ View definition:
f2 | integer | | plain |
f3 | text | | extended |
View definition:
SELECT tx1.f1,
tx1.f2,
SELECT tx1.f1,
tx1.f2,
tx1.f3
FROM temp_view_test.tx1
WHERE (EXISTS ( SELECT 1
@ -688,8 +688,8 @@ View definition:
f2 | integer | | plain |
f3 | text | | extended |
View definition:
SELECT tx1.y1,
tx1.f2,
SELECT tx1.y1,
tx1.f2,
tx1.f3
FROM tx1
WHERE (EXISTS ( SELECT 1

View File

@ -97,7 +97,7 @@ CREATE INDEX aa ON bb (grandtot);
type | text | | extended | |
totamt | numeric | | main | |
View definition:
SELECT tv.type,
SELECT tv.type,
tv.totamt
FROM tv
ORDER BY tv.type;
@ -109,7 +109,7 @@ View definition:
type | text | | extended | |
totamt | numeric | | main | |
View definition:
SELECT tv.type,
SELECT tv.type,
tv.totamt
FROM tv
ORDER BY tv.type;
@ -158,7 +158,7 @@ SET search_path = mvschema, public;
type | text | | extended | |
totamt | numeric | | main | |
View definition:
SELECT tv.type,
SELECT tv.type,
tv.totamt
FROM tv
ORDER BY tv.type;
@ -343,11 +343,11 @@ CREATE VIEW v_test2 AS SELECT moo, 2*moo FROM v_test1 UNION ALL SELECT moo, 3*mo
moo | integer | | plain |
?column? | integer | | plain |
View definition:
SELECT v_test1.moo,
SELECT v_test1.moo,
2 * v_test1.moo
FROM v_test1
UNION ALL
SELECT v_test1.moo,
UNION ALL
SELECT v_test1.moo,
3 * v_test1.moo
FROM v_test1;
@ -359,11 +359,11 @@ CREATE MATERIALIZED VIEW mv_test2 AS SELECT moo, 2*moo FROM v_test2 UNION ALL SE
moo | integer | | plain | |
?column? | integer | | plain | |
View definition:
SELECT v_test2.moo,
SELECT v_test2.moo,
2 * v_test2.moo
FROM v_test2
UNION ALL
SELECT v_test2.moo,
UNION ALL
SELECT v_test2.moo,
3 * v_test2.moo
FROM v_test2;

View File

@ -1381,9 +1381,9 @@ select * from dfview;
c3 | bigint | | plain |
c4 | bigint | | plain |
View definition:
SELECT int8_tbl.q1,
int8_tbl.q2,
dfunc(int8_tbl.q1, int8_tbl.q2, flag := int8_tbl.q1 > int8_tbl.q2) AS c3,
SELECT int8_tbl.q1,
int8_tbl.q2,
dfunc(int8_tbl.q1, int8_tbl.q2, flag := int8_tbl.q1 > int8_tbl.q2) AS c3,
dfunc(int8_tbl.q1, flag := int8_tbl.q1 < int8_tbl.q2, b := int8_tbl.q2) AS c4
FROM int8_tbl;

File diff suppressed because it is too large Load Diff

View File

@ -1119,7 +1119,7 @@ DROP TRIGGER instead_of_delete_trig ON main_view;
a | integer | | plain |
b | integer | | plain |
View definition:
SELECT main_table.a,
SELECT main_table.a,
main_table.b
FROM main_table;
Triggers:

View File

@ -1389,7 +1389,7 @@ CREATE VIEW rw_view1 AS SELECT * FROM base_tbl WHERE a < b
a | integer | | plain |
b | integer | | plain |
View definition:
SELECT base_tbl.a,
SELECT base_tbl.a,
base_tbl.b
FROM base_tbl
WHERE base_tbl.a < base_tbl.b;

View File

@ -361,7 +361,7 @@ SELECT sum(n) FROM t;
View definition:
WITH RECURSIVE t(n) AS (
VALUES (1)
UNION ALL
UNION ALL
SELECT t_1.n + 1
FROM t t_1
WHERE t_1.n < 100