Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0b7e660

Browse files
committed
Fix ruleutils pretty-printing to not generate trailing whitespace.
The pretty-printing logic in ruleutils.c operates by inserting a newlineand some indentation whitespace into strings that are already valid SQL.This naturally results in leaving some trailing whitespace before thenewline in many cases; which can be annoying when processing the outputwith other tools, as complained of by Joe Abbate. We can fix that ina pretty localized fashion by deleting any trailing whitespace beforewe append a pretty-printing newline. In addition, we have to modify thecode inserted by commit2f582f7 so thatwe also delete trailing whitespace when transposing items from temporarybuffers into the main result string, when a temporary item starts with anewline.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 manymore cases of trailing whitespace than had occurred in earlier branches.
1 parent648bd05 commit0b7e660

File tree

9 files changed

+723
-692
lines changed

9 files changed

+723
-692
lines changed

‎src/backend/utils/adt/ruleutils.c

Lines changed: 80 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ static const char *get_simple_binary_op_name(OpExpr *expr);
365365
staticboolisSimpleNode(Node*node,Node*parentNode,intprettyFlags);
366366
staticvoidappendContextKeyword(deparse_context*context,constchar*str,
367367
intindentBefore,intindentAfter,intindentPlus);
368+
staticvoidremoveStringInfoSpaces(StringInfostr);
368369
staticvoidget_rule_expr(Node*node,deparse_context*context,
369370
boolshowimplicit);
370371
staticvoidget_oper_expr(OpExpr*expr,deparse_context*context);
@@ -4479,42 +4480,42 @@ get_target_list(List *targetList, deparse_context *context,
44794480
/* Consider line-wrapping if enabled */
44804481
if (PRETTY_INDENT(context)&&context->wrapColumn >=0)
44814482
{
4482-
intleading_nl_pos=-1;
4483-
char*trailing_nl;
4484-
intpos;
4483+
intleading_nl_pos;
44854484

4486-
/* Does the new field start with whitespace plus a new line? */
4487-
for (pos=0;pos<targetbuf.len;pos++)
4485+
/* Does the new field start with a new line? */
4486+
if (targetbuf.len>0&&targetbuf.data[0]=='\n')
4487+
leading_nl_pos=0;
4488+
else
4489+
leading_nl_pos=-1;
4490+
4491+
/* If so, we shouldn't add anything */
4492+
if (leading_nl_pos >=0)
44884493
{
4489-
if (targetbuf.data[pos]=='\n')
4490-
{
4491-
leading_nl_pos=pos;
4492-
break;
4493-
}
4494-
if (targetbuf.data[pos]!=' ')
4495-
break;
4494+
/* instead, remove any trailing spaces currently in buf */
4495+
removeStringInfoSpaces(buf);
44964496
}
4497-
4498-
/* Locate the start of the current line in the output buffer */
4499-
trailing_nl=strrchr(buf->data,'\n');
4500-
if (trailing_nl==NULL)
4501-
trailing_nl=buf->data;
45024497
else
4503-
trailing_nl++;
4498+
{
4499+
char*trailing_nl;
45044500

4505-
/*
4506-
* If the field we're adding is the first in the list, or it
4507-
* already has a leading newline, don't add anything. Otherwise,
4508-
* add a newline, plus some indentation, if either the new field
4509-
* would cause an overflow or the last field used more than one
4510-
* line.
4511-
*/
4512-
if (colno>1&&
4513-
leading_nl_pos==-1&&
4514-
((strlen(trailing_nl)+strlen(targetbuf.data)>context->wrapColumn)||
4515-
last_was_multiline))
4516-
appendContextKeyword(context,"",-PRETTYINDENT_STD,
4517-
PRETTYINDENT_STD,PRETTYINDENT_VAR);
4501+
/* Locate the start of the current line in the output buffer */
4502+
trailing_nl=strrchr(buf->data,'\n');
4503+
if (trailing_nl==NULL)
4504+
trailing_nl=buf->data;
4505+
else
4506+
trailing_nl++;
4507+
4508+
/*
4509+
* Add a newline, plus some indentation, if the new field is
4510+
* not the first and either the new field would cause an
4511+
* overflow or the last field used more than one line.
4512+
*/
4513+
if (colno>1&&
4514+
((strlen(trailing_nl)+targetbuf.len>context->wrapColumn)||
4515+
last_was_multiline))
4516+
appendContextKeyword(context,"",-PRETTYINDENT_STD,
4517+
PRETTYINDENT_STD,PRETTYINDENT_VAR);
4518+
}
45184519

45194520
/* Remember this field's multiline status for next iteration */
45204521
last_was_multiline=
@@ -6236,23 +6237,42 @@ static void
62366237
appendContextKeyword(deparse_context*context,constchar*str,
62376238
intindentBefore,intindentAfter,intindentPlus)
62386239
{
6240+
StringInfobuf=context->buf;
6241+
62396242
if (PRETTY_INDENT(context))
62406243
{
62416244
context->indentLevel+=indentBefore;
62426245

6243-
appendStringInfoChar(context->buf,'\n');
6244-
appendStringInfoSpaces(context->buf,
6246+
/* remove any trailing spaces currently in the buffer ... */
6247+
removeStringInfoSpaces(buf);
6248+
/* ... then add a newline and some spaces */
6249+
appendStringInfoChar(buf,'\n');
6250+
appendStringInfoSpaces(buf,
62456251
Max(context->indentLevel,0)+indentPlus);
6246-
appendStringInfoString(context->buf,str);
6252+
6253+
appendStringInfoString(buf,str);
62476254

62486255
context->indentLevel+=indentAfter;
62496256
if (context->indentLevel<0)
62506257
context->indentLevel=0;
62516258
}
62526259
else
6253-
appendStringInfoString(context->buf,str);
6260+
appendStringInfoString(buf,str);
62546261
}
62556262

6263+
/*
6264+
* removeStringInfoSpaces - delete trailing spaces from a buffer.
6265+
*
6266+
* Possibly this should move to stringinfo.c at some point.
6267+
*/
6268+
staticvoid
6269+
removeStringInfoSpaces(StringInfostr)
6270+
{
6271+
while (str->len>0&&str->data[str->len-1]==' ')
6272+
str->data[--(str->len)]='\0';
6273+
}
6274+
6275+
62566276
/*
62576277
* get_rule_expr_paren- deparse expr using get_rule_expr,
62586278
* embracing the string with parentheses if necessary for prettyPrint.
@@ -7942,22 +7962,33 @@ get_from_clause(Query *query, const char *prefix, deparse_context *context)
79427962
/* Consider line-wrapping if enabled */
79437963
if (PRETTY_INDENT(context)&&context->wrapColumn >=0)
79447964
{
7945-
char*trailing_nl;
7946-
7947-
/* Locate the start of the current line in the buffer */
7948-
trailing_nl=strrchr(buf->data,'\n');
7949-
if (trailing_nl==NULL)
7950-
trailing_nl=buf->data;
7965+
/* Does the new item start with a new line? */
7966+
if (itembuf.len>0&&itembuf.data[0]=='\n')
7967+
{
7968+
/* If so, we shouldn't add anything */
7969+
/* instead, remove any trailing spaces currently in buf */
7970+
removeStringInfoSpaces(buf);
7971+
}
79517972
else
7952-
trailing_nl++;
7973+
{
7974+
char*trailing_nl;
79537975

7954-
/*
7955-
* Add a newline, plus some indentation, if the new item would
7956-
* cause an overflow.
7957-
*/
7958-
if (strlen(trailing_nl)+strlen(itembuf.data)>context->wrapColumn)
7959-
appendContextKeyword(context,"",-PRETTYINDENT_STD,
7960-
PRETTYINDENT_STD,PRETTYINDENT_VAR);
7976+
/* Locate the start of the current line in the buffer */
7977+
trailing_nl=strrchr(buf->data,'\n');
7978+
if (trailing_nl==NULL)
7979+
trailing_nl=buf->data;
7980+
else
7981+
trailing_nl++;
7982+
7983+
/*
7984+
* Add a newline, plus some indentation, if the new item
7985+
* would cause an overflow.
7986+
*/
7987+
if (strlen(trailing_nl)+itembuf.len>context->wrapColumn)
7988+
appendContextKeyword(context,"",-PRETTYINDENT_STD,
7989+
PRETTYINDENT_STD,
7990+
PRETTYINDENT_VAR);
7991+
}
79617992
}
79627993

79637994
/* Add the new item */

‎src/test/regress/expected/aggregates.out

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -960,10 +960,10 @@ select * from agg_view1;
960960
(1 row)
961961

962962
select pg_get_viewdef('agg_view1'::regclass);
963-
pg_get_viewdef
964-
----------------------------------------------------------------------------------------------------------------------
965-
SELECT aggfns(DISTINCT v.a, v.b, v.c) AS aggfns+
966-
FROM ( VALUES (1,3,'foo'::text), (0,NULL::integer,NULL::text), (2,2,'bar'::text), (3,1,'baz'::text)) v(a, b, c),+
963+
pg_get_viewdef
964+
---------------------------------------------------------------------------------------------------------------------
965+
SELECT aggfns(DISTINCT v.a, v.b, v.c) AS aggfns +
966+
FROM ( VALUES (1,3,'foo'::text), (0,NULL::integer,NULL::text), (2,2,'bar'::text), (3,1,'baz'::text)) v(a, b, c),+
967967
generate_series(1, 3) i(i);
968968
(1 row)
969969

@@ -978,10 +978,10 @@ select * from agg_view1;
978978
(1 row)
979979

980980
select pg_get_viewdef('agg_view1'::regclass);
981-
pg_get_viewdef
982-
----------------------------------------------------------------------------------------------------------------------
983-
SELECT aggfns(DISTINCT v.a, v.b, v.c ORDER BY v.b) AS aggfns+
984-
FROM ( VALUES (1,3,'foo'::text), (0,NULL::integer,NULL::text), (2,2,'bar'::text), (3,1,'baz'::text)) v(a, b, c),+
981+
pg_get_viewdef
982+
---------------------------------------------------------------------------------------------------------------------
983+
SELECT aggfns(DISTINCT v.a, v.b, v.c ORDER BY v.b) AS aggfns +
984+
FROM ( VALUES (1,3,'foo'::text), (0,NULL::integer,NULL::text), (2,2,'bar'::text), (3,1,'baz'::text)) v(a, b, c),+
985985
generate_series(1, 3) i(i);
986986
(1 row)
987987

@@ -1044,10 +1044,10 @@ select * from agg_view1;
10441044
(1 row)
10451045

10461046
select pg_get_viewdef('agg_view1'::regclass);
1047-
pg_get_viewdef
1048-
----------------------------------------------------------------------------------------------------------------------
1049-
SELECT aggfns(DISTINCT v.a, v.b, v.c ORDER BY v.a, v.c USING ~<~ NULLS LAST, v.b) AS aggfns+
1050-
FROM ( VALUES (1,3,'foo'::text), (0,NULL::integer,NULL::text), (2,2,'bar'::text), (3,1,'baz'::text)) v(a, b, c),+
1047+
pg_get_viewdef
1048+
---------------------------------------------------------------------------------------------------------------------
1049+
SELECT aggfns(DISTINCT v.a, v.b, v.c ORDER BY v.a, v.c USING ~<~ NULLS LAST, v.b) AS aggfns +
1050+
FROM ( VALUES (1,3,'foo'::text), (0,NULL::integer,NULL::text), (2,2,'bar'::text), (3,1,'baz'::text)) v(a, b, c),+
10511051
generate_series(1, 2) i(i);
10521052
(1 row)
10531053

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp