Skip to content

Commit d673eef

Browse files
committed
Fix XMLTABLE() deparsing to quote namespace names if necessary.
When deparsing an XMLTABLE() expression, XML namespace names were not quoted. However, since they are parsed as ColLabel tokens, some names require double quotes to ensure that they are properly interpreted. Fix by using quote_identifier() in the deparsing code. Back-patch to all supported versions. Dean Rasheed, reviewed by Tom Lane. Discussion: https://postgr.es/m/CAEZATCXTpAS%3DncfLNTZ7YS6O5puHeLg_SUYAit%2Bcs7wsrd9Msg%40mail.gmail.com
1 parent 29dfffa commit d673eef

File tree

5 files changed

+26
-13
lines changed

5 files changed

+26
-13
lines changed

src/backend/utils/adt/ruleutils.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -11814,7 +11814,8 @@ get_xmltable(TableFunc *tf, deparse_context *context, bool showimplicit)
1181411814
if (ns_node != NULL)
1181511815
{
1181611816
get_rule_expr(expr, context, showimplicit);
11817-
appendStringInfo(buf, " AS %s", strVal(ns_node));
11817+
appendStringInfo(buf, " AS %s",
11818+
quote_identifier(strVal(ns_node)));
1181811819
}
1181911820
else
1182011821
{

src/test/regress/expected/xml.out

+7-3
Original file line numberDiff line numberDiff line change
@@ -1379,16 +1379,20 @@ SELECT * FROM XMLTABLE(XMLNAMESPACES('http://x.y' AS zz),
13791379
10
13801380
(1 row)
13811381

1382-
CREATE VIEW xmltableview2 AS SELECT * FROM XMLTABLE(XMLNAMESPACES('http://x.y' AS zz),
1383-
'/zz:rows/zz:row'
1382+
CREATE VIEW xmltableview2 AS SELECT * FROM XMLTABLE(XMLNAMESPACES('http://x.y' AS "Zz"),
1383+
'/Zz:rows/Zz:row'
13841384
PASSING '<rows xmlns="http://x.y"><row><a>10</a></row></rows>'
1385-
COLUMNS a int PATH 'zz:a');
1385+
COLUMNS a int PATH 'Zz:a');
13861386
SELECT * FROM xmltableview2;
13871387
a
13881388
----
13891389
10
13901390
(1 row)
13911391

1392+
\sv xmltableview2
1393+
CREATE OR REPLACE VIEW public.xmltableview2 AS
1394+
SELECT a
1395+
FROM XMLTABLE(XMLNAMESPACES ('http://x.y'::text AS "Zz"), ('/Zz:rows/Zz:row'::text) PASSING ('<rows xmlns="http://x.y"><row><a>10</a></row></rows>'::xml) COLUMNS a integer PATH ('Zz:a'::text))
13921396
SELECT * FROM XMLTABLE(XMLNAMESPACES(DEFAULT 'http://x.y'),
13931397
'/rows/row'
13941398
PASSING '<rows xmlns="http://x.y"><row><a>10</a></row></rows>'

src/test/regress/expected/xml_1.out

+5-3
Original file line numberDiff line numberDiff line change
@@ -1046,10 +1046,10 @@ ERROR: unsupported XML feature
10461046
LINE 3: PASSING '<rows xmlns="http://x.y"><row...
10471047
^
10481048
DETAIL: This functionality requires the server to be built with libxml support.
1049-
CREATE VIEW xmltableview2 AS SELECT * FROM XMLTABLE(XMLNAMESPACES('http://x.y' AS zz),
1050-
'/zz:rows/zz:row'
1049+
CREATE VIEW xmltableview2 AS SELECT * FROM XMLTABLE(XMLNAMESPACES('http://x.y' AS "Zz"),
1050+
'/Zz:rows/Zz:row'
10511051
PASSING '<rows xmlns="http://x.y"><row><a>10</a></row></rows>'
1052-
COLUMNS a int PATH 'zz:a');
1052+
COLUMNS a int PATH 'Zz:a');
10531053
ERROR: unsupported XML feature
10541054
LINE 3: PASSING '<rows xmlns="http://x.y"><row...
10551055
^
@@ -1058,6 +1058,8 @@ SELECT * FROM xmltableview2;
10581058
ERROR: relation "xmltableview2" does not exist
10591059
LINE 1: SELECT * FROM xmltableview2;
10601060
^
1061+
\sv xmltableview2
1062+
ERROR: relation "xmltableview2" does not exist
10611063
SELECT * FROM XMLTABLE(XMLNAMESPACES(DEFAULT 'http://x.y'),
10621064
'/rows/row'
10631065
PASSING '<rows xmlns="http://x.y"><row><a>10</a></row></rows>'

src/test/regress/expected/xml_2.out

+7-3
Original file line numberDiff line numberDiff line change
@@ -1365,16 +1365,20 @@ SELECT * FROM XMLTABLE(XMLNAMESPACES('http://x.y' AS zz),
13651365
10
13661366
(1 row)
13671367

1368-
CREATE VIEW xmltableview2 AS SELECT * FROM XMLTABLE(XMLNAMESPACES('http://x.y' AS zz),
1369-
'/zz:rows/zz:row'
1368+
CREATE VIEW xmltableview2 AS SELECT * FROM XMLTABLE(XMLNAMESPACES('http://x.y' AS "Zz"),
1369+
'/Zz:rows/Zz:row'
13701370
PASSING '<rows xmlns="http://x.y"><row><a>10</a></row></rows>'
1371-
COLUMNS a int PATH 'zz:a');
1371+
COLUMNS a int PATH 'Zz:a');
13721372
SELECT * FROM xmltableview2;
13731373
a
13741374
----
13751375
10
13761376
(1 row)
13771377

1378+
\sv xmltableview2
1379+
CREATE OR REPLACE VIEW public.xmltableview2 AS
1380+
SELECT a
1381+
FROM XMLTABLE(XMLNAMESPACES ('http://x.y'::text AS "Zz"), ('/Zz:rows/Zz:row'::text) PASSING ('<rows xmlns="http://x.y"><row><a>10</a></row></rows>'::xml) COLUMNS a integer PATH ('Zz:a'::text))
13781382
SELECT * FROM XMLTABLE(XMLNAMESPACES(DEFAULT 'http://x.y'),
13791383
'/rows/row'
13801384
PASSING '<rows xmlns="http://x.y"><row><a>10</a></row></rows>'

src/test/regress/sql/xml.sql

+5-3
Original file line numberDiff line numberDiff line change
@@ -439,13 +439,15 @@ SELECT * FROM XMLTABLE(XMLNAMESPACES('http://x.y' AS zz),
439439
PASSING '<rows xmlns="http://x.y"><row><a>10</a></row></rows>'
440440
COLUMNS a int PATH 'zz:a');
441441

442-
CREATE VIEW xmltableview2 AS SELECT * FROM XMLTABLE(XMLNAMESPACES('http://x.y' AS zz),
443-
'/zz:rows/zz:row'
442+
CREATE VIEW xmltableview2 AS SELECT * FROM XMLTABLE(XMLNAMESPACES('http://x.y' AS "Zz"),
443+
'/Zz:rows/Zz:row'
444444
PASSING '<rows xmlns="http://x.y"><row><a>10</a></row></rows>'
445-
COLUMNS a int PATH 'zz:a');
445+
COLUMNS a int PATH 'Zz:a');
446446

447447
SELECT * FROM xmltableview2;
448448

449+
\sv xmltableview2
450+
449451
SELECT * FROM XMLTABLE(XMLNAMESPACES(DEFAULT 'http://x.y'),
450452
'/rows/row'
451453
PASSING '<rows xmlns="http://x.y"><row><a>10</a></row></rows>'

0 commit comments

Comments
 (0)