[DEPARSE] IS DOCUMENT as an operand loses needed parentheses

Started by Henson Choiabout 1 month ago4 messageshackers
Jump to latest
#1Henson Choi
assam258@gmail.com

Hi hackers,

ruleutils.c treats x IS DOCUMENT as a self-delimiting,
function-like expression, like xmlelement(...). It is really a
postfix test like IS NULL, so printed as the operand of another
expression it loses the parentheses it needs. The text then reads
back as another expression, or is rejected. This happens outside
pretty mode too, so pg_dump output cannot be restored when such an
object exists. The two attached patches add tests and fix it.

The problem
-----------

In a view or rule, with IS DOCUMENT as the right operand of an
operator:

create table t (f bool, x xml);
create view v as select f = (x is document) as r1 from t;

non-pretty, before: SELECT (f = x IS DOCUMENT) AS r1
pretty, before: SELECT f = (x) IS DOCUMENT AS r1
both read back as (f = x) IS DOCUMENT:
ERROR: operator does not exist: boolean = xml
after: SELECT (f = (x IS DOCUMENT)) AS r1
SELECT f = (x IS DOCUMENT) AS r1

Index expressions, partition keys and extended statistics
expressions can be written without parentheses only if they are
function-like, and looks_like_function() says IS DOCUMENT is:

create index i on t ((x is document));
before: CREATE INDEX i ON public.t USING btree (x IS DOCUMENT)
ERROR: syntax error at or near "IS"
after: ... USING btree (((x IS DOCUMENT)))

partition by list ((x is document))
before: LIST (x IS DOCUMENT) -- same syntax error
after: LIST (((x IS DOCUMENT)))

create statistics s on (x is document), f from t;
before: ... ON f, (x) IS DOCUMENT FROM t -- syntax error
after: ... ON f, (x IS DOCUMENT) FROM t

CHECK constraints, column defaults and generated columns, trigger
WHEN conditions, policies, SQL-standard function bodies and
publication row filters go through the same code, and fail to
restore in the same way.

The fix
-------

0002 treats IS DOCUMENT like the other postfix tests.

- isSimpleNode(): takes IS DOCUMENT, and only it among the
XmlExprs, out of the function-like list, so that it is
judged like IS NULL.
- looks_like_function(): returns false for IS DOCUMENT. The other
XmlExprs (XMLCONCAT, XMLELEMENT, ...) stay function-like.
- get_rule_expr(): prints parentheses of its own outside pretty
mode, as IS NULL does. It also passes the single argument to
get_rule_expr_paren(), not the argument list, which is what gave
the needless parentheses of (x) IS DOCUMENT in pretty mode.

Outside pretty mode, IS DOCUMENT as the argument of a cast now
gets a second pair of parentheses, ((x IS DOCUMENT))::text. The
other postfix tests and operator expressions are already printed
that way, as in ((a IS NULL))::text, so this only makes the output
consistent, and it reads back the same.

Testing
-------

0001 adds the tests with the output before the fix, and 0002
updates it, so that the diff shows what changes. The view is
created again from its non-pretty text, and the statistics object
and the index from their printed definitions.

Some output still has more parentheses than it needs, such as
(((x IS DOCUMENT))) in a non-pretty index definition. That is not
specific to IS DOCUMENT: other postfix tests are printed the same
way there. I will approach it in a separate patch.

Back branches
-------------

I think this should be back-patched to 15. I applied both patches
to REL_15_STABLE through REL_19_STABLE.

The patches apply cleanly to REL_19 and pass make check. The
older branches need small adjustments: /* FALLTHROUGH */ in
place of pg_fallthrough (15-18), some hunks moved by hand
(15, 16) and, on 15, regenerated expected test output. With
those, all branches pass.

This came out of an AI-assisted audit of ruleutils.c. I will
post the other problems it found as separate, independent reports
as I work through them.

Best regards,
Henson

Attachments:

t254186_1
v1-0001-Add-tests-for-IS-DOCUMENT.patchapplication/x-patch; name=v1-0001-Add-tests-for-IS-DOCUMENT.patchDownload+87-2
v1-0002-Parenthesize-IS-DOCUMENT.patchapplication/x-patch; name=v1-0002-Parenthesize-IS-DOCUMENT.patchDownload+41-26
#2Prashant Chinnam
crprashant@hotmail.com
In reply to: Henson Choi (#1)
Re: [DEPARSE] IS DOCUMENT as an operand loses needed parentheses

The following review has been posted through the commitfest application:
make installcheck-world: not tested
Implements feature: tested, passed
Spec compliant: tested, passed
Documentation: not tested

Hi Henson,

I read your report and the patch, and I agree with the analysis.
IS DOCUMENT is a postfix test like IS NULL, and treating it as
self-delimiting in isSimpleNode() and looks_like_function() is
what loses the parentheses. The three changes look right to me,
including passing the single argument to get_rule_expr_paren()
instead of the argument list.

For the hands-on part I used AI assistance. The bug was
reproduced on master (the view reads back as (f = x IS DOCUMENT)
and fails with "operator does not exist: boolean = xml"; the
index definition prints without parentheses and fails with a
syntax error), and with v1 applied the view, index and
statistics definitions all read back and recreate successfully.

make installcheck-world was also run against an installed
server with the patch applied: the main regression suite passes
(239/239), as does the isolation suite (134/134), along with 47
of 51 contrib suites and 35 of 40 test-module suites. The
suites that did not run need features that were not built in
that environment (plperl, uuid-ossp, and the injection-points
option), so they are unrelated to this change.

One thing that was checked with that assistance: the other
IS_DOCUMENT special cases in the tree (parser, executor,
nodeFuncs, clauses) are not deparse paths, so the scope of the
patch looks complete to me. The double parentheses in the cast
case also match how IS NULL already prints, as you noted. No
documentation changes are needed for this fix.

Thanks for the clear report and the round-trip tests.

Regards,
Prashant

The new status of this patch is: Ready for Committer

#3Prashant Chinnam
crprashant@hotmail.com
In reply to: Prashant Chinnam (#2)
Re: [DEPARSE] IS DOCUMENT as an operand loses needed parentheses

A short follow-up to my review above: the installcheck-world
suites that could not run in my environment before have now
been run as well, on a build of the same patched tree with
plperl, uuid-ossp (OSSP library) and injection points enabled.

All of them pass: bool_plperl, hstore_plperl, jsonb_plperl and
uuid-ossp, and the gin, hash, nbtree, typcache and
injection_points module suites, plus the injection_points
isolation specs (18/18). So installcheck-world is now fully
green with the patch: main regression 239/239, isolation
134/134, contrib 51/51 suites, test modules 40/40 suites.

(As with the testing in my review, this was done with AI
assistance.)

#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: Henson Choi (#1)
Re: [DEPARSE] IS DOCUMENT as an operand loses needed parentheses

Prashant Chinnam <crprashant@hotmail.com> writes:

The new status of this patch is: Ready for Committer

Pushed, thanks for the patch and the review!

I simplified the test cases a good deal. IMO we only need to
demonstrate that the behavior is now correct, we do not need to
expend test cycles forevermore on what the bad consequences
would be if it were not correct.

regards, tom lane