[DEPARSE] A COLLATE clause in the URI of XMLNAMESPACES does not read back
[DEPARSE] A COLLATE clause in the URI of XMLNAMESPACES does not read back
Hi hackers,
The namespace URI in XMLNAMESPACES is a b_expr in the grammar, so an
expression with a COLLATE clause has to be in parentheses there.
Pretty-printed output leaves them out, so the definition of a view
using one cannot be read back. The two attached patches add a test
and fix it.
The problem
-----------
create view v as
select * from xmltable(
xmlnamespaces(('http://x.y' collate "C") as p),
'/p:a' passing (null::xml) columns v text path 'p:b');
before: XMLNAMESPACES ('http://x.y'::text COLLATE "C" AS p)
ERROR: syntax error at or near "COLLATE"
after: XMLNAMESPACES (('http://x.y'::text COLLATE "C") AS p)
Only pretty-printed output is affected, such as pg_get_viewdef(...,
true) and psql's \d+. Outside pretty mode the COLLATE expression
prints its own parentheses, so pg_dump is fine.
The fix
-------
get_xmltable() printed the URI with get_rule_expr() as it was. 0002
puts it in parentheses, after XMLNAMESPACES( as well as after DEFAULT,
unless it is a plain Const. A constant is the usual case and needs
none, so its output does not change. Any other expression gets them
even where it would not need them, such as an operator expression;
that output read back before and still does. This is what ruleutils.c
already does for b_expr and c_expr arguments elsewhere, such as those
of POSITION and XMLEXISTS.
Outside pretty mode such a URI now gets two pairs of parentheses,
because the COLLATE expression prints its own there. That is how
other operands are printed in that mode, and it reads back.
Testing
-------
0001 adds a view to xml.sql, prints it with pg_get_viewdef(..., true)
and recreates the view from that output. 0001 has the output before
the fix, where the recreation fails, and 0002 updates it.
Back branches
-------------
XMLNAMESPACES has been printed this way since XMLTABLE came in 10, so
I think this should be fixed back to 14. The code change applies to
14-19 as is. The test applies to 17 and later; in 14-16 it needs
another place in xml.sql, and its expected output may differ.
This came out of the same AI-assisted audit of ruleutils.c as my
earlier [DEPARSE] reports.
Best regards,
Henson
The namespace URI in XMLNAMESPACES is a b_expr in the grammar, so an
expression with a COLLATE clause has to be in parentheses there.
Pretty-printed output leaves them out, so the definition of a view
using one cannot be read back. The two attached patches add a test
and fix it.
The problem
-----------
create view v as
select * from xmltable(
xmlnamespaces(('http://x.y' collate "C") as p),
'/p:a' passing (null::xml) columns v text path 'p:b');
before: XMLNAMESPACES ('http://x.y'::text COLLATE "C" AS p)
ERROR: syntax error at or near "COLLATE"
after: XMLNAMESPACES (('http://x.y'::text COLLATE "C") AS p)
Only pretty-printed output is affected, such as pg_get_viewdef(...,
true) and psql's \d+. Outside pretty mode the COLLATE expression
prints its own parentheses, so pg_dump is fine.
The fix
-------
get_xmltable() printed the URI with get_rule_expr() as it was. 0002
puts it in parentheses, after XMLNAMESPACES( as well as after DEFAULT,
unless it is a plain Const. A constant is the usual case and needs
none, so its output does not change. Any other expression gets them
even where it would not need them, such as an operator expression;
that output read back before and still does. This is what ruleutils.c
already does for b_expr and c_expr arguments elsewhere, such as those
of POSITION and XMLEXISTS.
Outside pretty mode such a URI now gets two pairs of parentheses,
because the COLLATE expression prints its own there. That is how
other operands are printed in that mode, and it reads back.
Testing
-------
0001 adds a view to xml.sql, prints it with pg_get_viewdef(..., true)
and recreates the view from that output. 0001 has the output before
the fix, where the recreation fails, and 0002 updates it.
Back branches
-------------
XMLNAMESPACES has been printed this way since XMLTABLE came in 10, so
I think this should be fixed back to 14. The code change applies to
14-19 as is. The test applies to 17 and later; in 14-16 it needs
another place in xml.sql, and its expected output may differ.
This came out of the same AI-assisted audit of ruleutils.c as my
earlier [DEPARSE] reports.
Best regards,
Henson
Re: [DEPARSE] A COLLATE clause in the URI of XMLNAMESPACES does not read back
От:
Prashant Chinnam <crprashant@hotmail.com>
Дата:
Hi Henson, Thanks for the patch. I read the report and the patch, and tested v1 hands-on (the hands-on testing below was done with AI assistance; the code reading and this review are mine). Code reading: the URI of XMLNAMESPACES is a b_expr, so it cannot carry a COLLATE, and get_xmltable() was the one place in that function printing an expression without parentheses -- the ROWS expression, the PASSING document and the column expressions are all wrapped already. In pretty mode a CollateClause drops its own parentheses, so the printed definition is not valid input. Parenthesizing the URI there, in both the AS-name and the DEFAULT branch, fixes it at the right layer, and the plain-Const exception keeps the common case free of output churn. Parenthesizing in non-pretty mode as well gives two pairs of parentheses there, which reads back fine and is consistent with how other operands in this function are treated. Reproduction on current master: with views whose namespace URI carries a COLLATE (named namespace, DEFAULT namespace, one element of a mixed list, and a function-call URI), pg_get_viewdef(..., true) prints the COLLATE bare, and recreating the views from those definitions fails with 'syntax error at or near "COLLATE"' - 6 of the 12 objects in my test battery. The non-pretty definitions round-trip, and a full pg_dump/restore is clean on master, matching your report that this is a pretty-printing problem only. I also confirmed that functions are not affected, since pg_get_functiondef prints the body non-pretty. With v1 applied, every object round-trips from both the pretty and the non-pretty definition, with the definitions byte-identical before and after recreation, and a pg_dump/restore is clean. Objects created before the patch are fixed by the patched deparser alone, as expected for a ruleutils fix. Plain-constant URIs print exactly as before (a literal with a cast folds to a Const and is also unchanged); operator-expression URIs gain parentheses, as your commit message notes, and still read back. One extra form I tried: a LATERAL view whose namespace URI is a column reference with a COLLATE also reads back and executes correctly with the patch. One small coverage note: the new test covers the pretty, named-AS form. The DEFAULT form and the non-pretty output are exercised by my testing above but are not in the patch's tests; a DEFAULT line in the new test would be a nice addition, but I do not consider it a blocker. I ran installcheck-world against the patched build: core regression 239/239, core isolation 134/134, contrib 206/206 + 20/20 isolation, test modules 71/71 + 24/24 isolation - all pass. This looks complete and correct to me. ~ Prashant
Re: [DEPARSE] A COLLATE clause in the URI of XMLNAMESPACES does not read back
От:
Henson Choi <assam258@gmail.com>
Дата:
Hi Prashant,
Thank you for the review and the testing.
> One small coverage note: the new test covers the pretty, named-AS
> form. The DEFAULT form and the non-pretty output are exercised by my
> testing above but are not in the patch's tests; a DEFAULT line in the
> new test would be a nice addition, but I do not consider it a blocker.
Good point. I will extend the test and post a v2.
Best regards,
Henson
Thank you for the review and the testing.
> One small coverage note: the new test covers the pretty, named-AS
> form. The DEFAULT form and the non-pretty output are exercised by my
> testing above but are not in the patch's tests; a DEFAULT line in the
> new test would be a nice addition, but I do not consider it a blocker.
Good point. I will extend the test and post a v2.
Best regards,
Henson
Re: [DEPARSE] A COLLATE clause in the URI of XMLNAMESPACES does not read back
Hi Prashant,
> One small coverage note: the new test covers the pretty, named-AS
> form. The DEFAULT form and the non-pretty output are exercised by
> my testing above but are not in the patch's tests; a DEFAULT line
> in the new test would be a nice addition, but I do not consider it
> a blocker.
v2, attached, adds a DEFAULT namespace to the view in the test:
XMLNAMESPACES(('http://x.y' COLLATE "C") AS p,
DEFAULT ('http://x.z' COLLATE "C"))
so the expected output now shows both URIs printed bare before the
fix and in parentheses after it:
before:
XMLNAMESPACES ('http://x.y'::text COLLATE "C" AS p,
DEFAULT 'http://x.z'::text COLLATE "C")
after:
XMLNAMESPACES (('http://x.y'::text COLLATE "C") AS p,
DEFAULT ('http://x.z'::text COLLATE "C"))
The code in 0002 is unchanged from v1. The DEFAULT branch already
got the parentheses there; the test now checks it.
I did not add the non-pretty output. It reads back even before the
fix, so it would not catch this problem coming back.
Regards,
Henson
> One small coverage note: the new test covers the pretty, named-AS
> form. The DEFAULT form and the non-pretty output are exercised by
> my testing above but are not in the patch's tests; a DEFAULT line
> in the new test would be a nice addition, but I do not consider it
> a blocker.
v2, attached, adds a DEFAULT namespace to the view in the test:
XMLNAMESPACES(('http://x.y' COLLATE "C") AS p,
DEFAULT ('http://x.z' COLLATE "C"))
so the expected output now shows both URIs printed bare before the
fix and in parentheses after it:
before:
XMLNAMESPACES ('http://x.y'::text COLLATE "C" AS p,
DEFAULT 'http://x.z'::text COLLATE "C")
after:
XMLNAMESPACES (('http://x.y'::text COLLATE "C") AS p,
DEFAULT ('http://x.z'::text COLLATE "C"))
The code in 0002 is unchanged from v1. The DEFAULT branch already
got the parentheses there; the test now checks it.
I did not add the non-pretty output. It reads back even before the
fix, so it would not catch this problem coming back.
Regards,
Henson
Re: [DEPARSE] A COLLATE clause in the URI of XMLNAMESPACES does not read back
От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Henson Choi writes: > The namespace URI in XMLNAMESPACES is a b_expr in the grammar, so an > expression with a COLLATE clause has to be in parentheses there. > Pretty-printed output leaves them out, so the definition of a view > using one cannot be read back. The two attached patches add a test > and fix it. I'm inclined to reject this on the grounds that it depends on a false expectation, namely that the output of pretty-print mode is guaranteed to parse back in every case. If that were guaranteed, we would not need the non-pretty-print mode. AFAICS the troublesome cases do work fine in non-pretty-print mode, so this doesn't break pg_dump. If these cases were likely to appear in the field, maybe we'd fix it anyway, but I can't get excited about that. When would a namespace argument be anything but a literal? The SQL spec doesn't appear to allow anything but there, so we're already going above-and-beyond by allowing b_expr. If anything, it's a spec-compatibility bug that we tend to render the argument as 'foo'::text and not just 'foo' (not that I'm excited about fixing that either). regards, tom lane