1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
|
/*-------------------------------------------------------------------------
*
* parse_target.c
* handle target lists
*
* Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* src/backend/parser/parse_target.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "catalog/pg_type.h"
#include "commands/dbcommands.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "parser/parse_coerce.h"
#include "parser/parse_expr.h"
#include "parser/parse_func.h"
#include "parser/parse_relation.h"
#include "parser/parse_target.h"
#include "parser/parse_type.h"
#include "parser/parsetree.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "utils/typcache.h"
static void markTargetListOrigin(ParseState *pstate, TargetEntry *tle,
Var *var, int levelsup);
static Node *transformAssignmentSubscripts(ParseState *pstate,
Node *basenode,
const char *targetName,
Oid targetTypeId,
int32 targetTypMod,
Oid targetCollation,
List *subscripts,
bool isSlice,
List *indirection,
ListCell *next_indirection,
Node *rhs,
CoercionContext ccontext,
int location);
static List *ExpandColumnRefStar(ParseState *pstate, ColumnRef *cref,
bool make_target_entry);
static List *ExpandAllTables(ParseState *pstate, int location);
static List *ExpandIndirectionStar(ParseState *pstate, A_Indirection *ind,
bool make_target_entry, ParseExprKind exprKind);
static List *ExpandSingleTable(ParseState *pstate, ParseNamespaceItem *nsitem,
int sublevels_up, int location,
bool make_target_entry);
static List *ExpandRowReference(ParseState *pstate, Node *expr,
bool make_target_entry);
static int FigureColnameInternal(Node *node, char **name);
/*
* transformTargetEntry()
* Transform any ordinary "expression-type" node into a targetlist entry.
* This is exported so that parse_clause.c can generate targetlist entries
* for ORDER/GROUP BY items that are not already in the targetlist.
*
* node the (untransformed) parse tree for the value expression.
* expr the transformed expression, or NULL if caller didn't do it yet.
* exprKind expression kind (EXPR_KIND_SELECT_TARGET, etc)
* colname the column name to be assigned, or NULL if none yet set.
* resjunk true if the target should be marked resjunk, ie, it is not
* wanted in the final projected tuple.
*/
TargetEntry *
transformTargetEntry(ParseState *pstate,
Node *node,
Node *expr,
ParseExprKind exprKind,
char *colname,
bool resjunk)
{
/* Transform the node if caller didn't do it already */
if (expr == NULL)
{
/*
* If it's a SetToDefault node and we should allow that, pass it
* through unmodified. (transformExpr will throw the appropriate
* error if we're disallowing it.)
*/
if (exprKind == EXPR_KIND_UPDATE_SOURCE && IsA(node, SetToDefault))
expr = node;
else
expr = transformExpr(pstate, node, exprKind);
}
if (colname == NULL && !resjunk)
{
/*
* Generate a suitable column name for a column without any explicit
* 'AS ColumnName' clause.
*/
colname = FigureColname(node);
}
return makeTargetEntry((Expr *) expr,
(AttrNumber) pstate->p_next_resno++,
colname,
resjunk);
}
/*
* transformTargetList()
* Turns a list of ResTarget's into a list of TargetEntry's.
*
* This code acts mostly the same for SELECT, UPDATE, or RETURNING lists;
* the main thing is to transform the given expressions (the "val" fields).
* The exprKind parameter distinguishes these cases when necessary.
*/
List *
transformTargetList(ParseState *pstate, List *targetlist,
ParseExprKind exprKind)
{
List *p_target = NIL;
bool expand_star;
ListCell *o_target;
/* Shouldn't have any leftover multiassign items at start */
Assert(pstate->p_multiassign_exprs == NIL);
/* Expand "something.*" in SELECT and RETURNING, but not UPDATE */
expand_star = (exprKind != EXPR_KIND_UPDATE_SOURCE);
foreach(o_target, targetlist)
{
ResTarget *res = (ResTarget *) lfirst(o_target);
/*
* Check for "something.*". Depending on the complexity of the
* "something", the star could appear as the last field in ColumnRef,
* or as the last indirection item in A_Indirection.
*/
if (expand_star)
{
if (IsA(res->val, ColumnRef))
{
ColumnRef *cref = (ColumnRef *) res->val;
if (IsA(llast(cref->fields), A_Star))
{
/* It is something.*, expand into multiple items */
p_target = list_concat(p_target,
ExpandColumnRefStar(pstate,
cref,
true));
continue;
}
}
else if (IsA(res->val, A_Indirection))
{
A_Indirection *ind = (A_Indirection *) res->val;
if (IsA(llast(ind->indirection), A_Star))
{
/* It is something.*, expand into multiple items */
p_target = list_concat(p_target,
ExpandIndirectionStar(pstate,
ind,
true,
exprKind));
continue;
}
}
}
/*
* Not "something.*", or we want to treat that as a plain whole-row
* variable, so transform as a single expression
*/
p_target = lappend(p_target,
transformTargetEntry(pstate,
res->val,
NULL,
exprKind,
res->name,
false));
}
/*
* If any multiassign resjunk items were created, attach them to the end
* of the targetlist. This should only happen in an UPDATE tlist. We
* don't need to worry about numbering of these items; transformUpdateStmt
* will set their resnos.
*/
if (pstate->p_multiassign_exprs)
{
Assert(exprKind == EXPR_KIND_UPDATE_SOURCE);
p_target = list_concat(p_target, pstate->p_multiassign_exprs);
pstate->p_multiassign_exprs = NIL;
}
return p_target;
}
/*
* transformExpressionList()
*
* This is the identical transformation to transformTargetList, except that
* the input list elements are bare expressions without ResTarget decoration,
* and the output elements are likewise just expressions without TargetEntry
* decoration. Also, we don't expect any multiassign constructs within the
* list, so there's nothing to do for that. We use this for ROW() and
* VALUES() constructs.
*
* exprKind is not enough to tell us whether to allow SetToDefault, so
* an additional flag is needed for that.
*/
List *
transformExpressionList(ParseState *pstate, List *exprlist,
ParseExprKind exprKind, bool allowDefault)
{
List *result = NIL;
ListCell *lc;
foreach(lc, exprlist)
{
Node *e = (Node *) lfirst(lc);
/*
* Check for "something.*". Depending on the complexity of the
* "something", the star could appear as the last field in ColumnRef,
* or as the last indirection item in A_Indirection.
*/
if (IsA(e, ColumnRef))
{
ColumnRef *cref = (ColumnRef *) e;
if (IsA(llast(cref->fields), A_Star))
{
/* It is something.*, expand into multiple items */
result = list_concat(result,
ExpandColumnRefStar(pstate, cref,
false));
continue;
}
}
else if (IsA(e, A_Indirection))
{
A_Indirection *ind = (A_Indirection *) e;
if (IsA(llast(ind->indirection), A_Star))
{
/* It is something.*, expand into multiple items */
result = list_concat(result,
ExpandIndirectionStar(pstate, ind,
false, exprKind));
continue;
}
}
/*
* Not "something.*", so transform as a single expression. If it's a
* SetToDefault node and we should allow that, pass it through
* unmodified. (transformExpr will throw the appropriate error if
* we're disallowing it.)
*/
if (allowDefault && IsA(e, SetToDefault))
/* do nothing */ ;
else
e = transformExpr(pstate, e, exprKind);
result = lappend(result, e);
}
return result;
}
/*
* resolveTargetListUnknowns()
* Convert any unknown-type targetlist entries to type TEXT.
*
* We do this after we've exhausted all other ways of identifying the output
* column types of a query.
*/
void
resolveTargetListUnknowns(ParseState *pstate, List *targetlist)
{
ListCell *l;
foreach(l, targetlist)
{
TargetEntry *tle = (TargetEntry *) lfirst(l);
Oid restype = exprType((Node *) tle->expr);
if (restype == UNKNOWNOID)
{
tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
restype, TEXTOID, -1,
COERCION_IMPLICIT,
COERCE_IMPLICIT_CAST,
-1);
}
}
}
/*
* markTargetListOrigins()
* Mark targetlist columns that are simple Vars with the source
* table's OID and column number.
*
* Currently, this is done only for SELECT targetlists and RETURNING lists,
* since we only need the info if we are going to send it to the frontend.
*/
void
markTargetListOrigins(ParseState *pstate, List *targetlist)
{
ListCell *l;
foreach(l, targetlist)
{
TargetEntry *tle = (TargetEntry *) lfirst(l);
markTargetListOrigin(pstate, tle, (Var *) tle->expr, 0);
}
}
/*
* markTargetListOrigin()
* If 'var' is a Var of a plain relation, mark 'tle' with its origin
*
* levelsup is an extra offset to interpret the Var's varlevelsup correctly.
*
* Note that we do not drill down into views, but report the view as the
* column owner. There's also no need to drill down into joins: if we see
* a join alias Var, it must be a merged JOIN USING column (or possibly a
* whole-row Var); that is not a direct reference to any plain table column,
* so we don't report it.
*/
static void
markTargetListOrigin(ParseState *pstate, TargetEntry *tle,
Var *var, int levelsup)
{
int netlevelsup;
RangeTblEntry *rte;
AttrNumber attnum;
if (var == NULL || !IsA(var, Var))
return;
netlevelsup = var->varlevelsup + levelsup;
rte = GetRTEByRangeTablePosn(pstate, var->varno, netlevelsup);
attnum = var->varattno;
switch (rte->rtekind)
{
case RTE_RELATION:
/* It's a table or view, report it */
tle->resorigtbl = rte->relid;
tle->resorigcol = attnum;
break;
case RTE_SUBQUERY:
/* Subselect-in-FROM: copy up from the subselect */
if (attnum != InvalidAttrNumber)
{
TargetEntry *ste = get_tle_by_resno(rte->subquery->targetList,
attnum);
if (ste == NULL || ste->resjunk)
elog(ERROR, "subquery %s does not have attribute %d",
rte->eref->aliasname, attnum);
tle->resorigtbl = ste->resorigtbl;
tle->resorigcol = ste->resorigcol;
}
break;
case RTE_JOIN:
case RTE_FUNCTION:
case RTE_VALUES:
case RTE_TABLEFUNC:
case RTE_NAMEDTUPLESTORE:
case RTE_RESULT:
/* not a simple relation, leave it unmarked */
break;
case RTE_CTE:
/*
* CTE reference: copy up from the subquery, if possible. If the
* RTE is a recursive self-reference then we can't do anything
* because we haven't finished analyzing it yet. However, it's no
* big loss because we must be down inside the recursive term of a
* recursive CTE, and so any markings on the current targetlist
* are not going to affect the results anyway.
*/
if (attnum != InvalidAttrNumber && !rte->self_reference)
{
CommonTableExpr *cte = GetCTEForRTE(pstate, rte, netlevelsup);
TargetEntry *ste;
List *tl = GetCTETargetList(cte);
int extra_cols = 0;
/*
* RTE for CTE will already have the search and cycle columns
* added, but the subquery won't, so skip looking those up.
*/
if (cte->search_clause)
extra_cols += 1;
if (cte->cycle_clause)
extra_cols += 2;
if (extra_cols &&
attnum > list_length(tl) &&
attnum <= list_length(tl) + extra_cols)
break;
ste = get_tle_by_resno(tl, attnum);
if (ste == NULL || ste->resjunk)
elog(ERROR, "CTE %s does not have attribute %d",
rte->eref->aliasname, attnum);
tle->resorigtbl = ste->resorigtbl;
tle->resorigcol = ste->resorigcol;
}
break;
}
}
/*
* transformAssignedExpr()
* This is used in INSERT and UPDATE statements only. It prepares an
* expression for assignment to a column of the target table.
* This includes coercing the given value to the target column's type
* (if necessary), and dealing with any subfield names or subscripts
* attached to the target column itself. The input expression has
* already been through transformExpr().
*
* pstate parse state
* expr expression to be modified
* exprKind indicates which type of statement we're dealing with
* colname target column name (ie, name of attribute to be assigned to)
* attrno target attribute number
* indirection subscripts/field names for target column, if any
* location error cursor position for the target column, or -1
*
* Returns the modified expression.
*
* Note: location points at the target column name (SET target or INSERT
* column name list entry), and must therefore be -1 in an INSERT that
* omits the column name list. So we should usually prefer to use
* exprLocation(expr) for errors that can happen in a default INSERT.
*/
Expr *
transformAssignedExpr(ParseState *pstate,
Expr *expr,
ParseExprKind exprKind,
const char *colname,
int attrno,
List *indirection,
int location)
{
Relation rd = pstate->p_target_relation;
Oid type_id; /* type of value provided */
Oid attrtype; /* type of target column */
int32 attrtypmod;
Oid attrcollation; /* collation of target column */
ParseExprKind sv_expr_kind;
/*
* Save and restore identity of expression type we're parsing. We must
* set p_expr_kind here because we can parse subscripts without going
* through transformExpr().
*/
Assert(exprKind != EXPR_KIND_NONE);
sv_expr_kind = pstate->p_expr_kind;
pstate->p_expr_kind = exprKind;
Assert(rd != NULL);
if (attrno <= 0)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("cannot assign to system column \"%s\"",
colname),
parser_errposition(pstate, location)));
attrtype = attnumTypeId(rd, attrno);
attrtypmod = TupleDescAttr(rd->rd_att, attrno - 1)->atttypmod;
attrcollation = TupleDescAttr(rd->rd_att, attrno - 1)->attcollation;
/*
* If the expression is a DEFAULT placeholder, insert the attribute's
* type/typmod/collation into it so that exprType etc will report the
* right things. (We expect that the eventually substituted default
* expression will in fact have this type and typmod. The collation
* likely doesn't matter, but let's set it correctly anyway.) Also,
* reject trying to update a subfield or array element with DEFAULT, since
* there can't be any default for portions of a column.
*/
if (expr && IsA(expr, SetToDefault))
{
SetToDefault *def = (SetToDefault *) expr;
def->typeId = attrtype;
def->typeMod = attrtypmod;
def->collation = attrcollation;
if (indirection)
{
if (IsA(linitial(indirection), A_Indices))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("cannot set an array element to DEFAULT"),
parser_errposition(pstate, location)));
else
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("cannot set a subfield to DEFAULT"),
parser_errposition(pstate, location)));
}
}
/* Now we can use exprType() safely. */
type_id = exprType((Node *) expr);
/*
* If there is indirection on the target column, prepare an array or
* subfield assignment expression. This will generate a new column value
* that the source value has been inserted into, which can then be placed
* in the new tuple constructed by INSERT or UPDATE.
*/
if (indirection)
{
Node *colVar;
if (pstate->p_is_insert)
{
/*
* The command is INSERT INTO table (col.something) ... so there
* is not really a source value to work with. Insert a NULL
* constant as the source value.
*/
colVar = (Node *) makeNullConst(attrtype, attrtypmod,
attrcollation);
}
else
{
/*
* Build a Var for the column to be updated.
*/
Var *var;
var = makeVar(pstate->p_target_nsitem->p_rtindex, attrno,
attrtype, attrtypmod, attrcollation, 0);
var->location = location;
colVar = (Node *) var;
}
expr = (Expr *)
transformAssignmentIndirection(pstate,
colVar,
colname,
false,
attrtype,
attrtypmod,
attrcollation,
indirection,
list_head(indirection),
(Node *) expr,
COERCION_ASSIGNMENT,
location);
}
else
{
/*
* For normal non-qualified target column, do type checking and
* coercion.
*/
Node *orig_expr = (Node *) expr;
expr = (Expr *)
coerce_to_target_type(pstate,
orig_expr, type_id,
attrtype, attrtypmod,
COERCION_ASSIGNMENT,
COERCE_IMPLICIT_CAST,
-1);
if (expr == NULL)
ereport(ERROR,
(errcode(ERRCODE_DATATYPE_MISMATCH),
errmsg("column \"%s\" is of type %s"
" but expression is of type %s",
colname,
format_type_be(attrtype),
format_type_be(type_id)),
errhint("You will need to rewrite or cast the expression."),
parser_errposition(pstate, exprLocation(orig_expr))));
}
pstate->p_expr_kind = sv_expr_kind;
return expr;
}
/*
* updateTargetListEntry()
* This is used in UPDATE statements (and ON CONFLICT DO UPDATE)
* only. It prepares an UPDATE TargetEntry for assignment to a
* column of the target table. This includes coercing the given
* value to the target column's type (if necessary), and dealing with
* any subfield names or subscripts attached to the target column
* itself.
*
* pstate parse state
* tle target list entry to be modified
* colname target column name (ie, name of attribute to be assigned to)
* attrno target attribute number
* indirection subscripts/field names for target column, if any
* location error cursor position (should point at column name), or -1
*/
void
updateTargetListEntry(ParseState *pstate,
TargetEntry *tle,
char *colname,
int attrno,
List *indirection,
int location)
{
/* Fix up expression as needed */
tle->expr = transformAssignedExpr(pstate,
tle->expr,
EXPR_KIND_UPDATE_TARGET,
colname,
attrno,
indirection,
location);
/*
* Set the resno to identify the target column --- the rewriter and
* planner depend on this. We also set the resname to identify the target
* column, but this is only for debugging purposes; it should not be
* relied on. (In particular, it might be out of date in a stored rule.)
*/
tle->resno = (AttrNumber) attrno;
tle->resname = colname;
}
/*
* Process indirection (field selection or subscripting) of the target
* column in INSERT/UPDATE/assignment. This routine recurses for multiple
* levels of indirection --- but note that several adjacent A_Indices nodes
* in the indirection list are treated as a single multidimensional subscript
* operation.
*
* In the initial call, basenode is a Var for the target column in UPDATE,
* or a null Const of the target's type in INSERT, or a Param for the target
* variable in PL/pgSQL assignment. In recursive calls, basenode is NULL,
* indicating that a substitute node should be consed up if needed.
*
* targetName is the name of the field or subfield we're assigning to, and
* targetIsSubscripting is true if we're subscripting it. These are just for
* error reporting.
*
* targetTypeId, targetTypMod, targetCollation indicate the datatype and
* collation of the object to be assigned to (initially the target column,
* later some subobject).
*
* indirection is the list of indirection nodes, and indirection_cell is the
* start of the sublist remaining to process. When it's NULL, we're done
* recursing and can just coerce and return the RHS.
*
* rhs is the already-transformed value to be assigned; note it has not been
* coerced to any particular type.
*
* ccontext is the coercion level to use while coercing the rhs. For
* normal statements it'll be COERCION_ASSIGNMENT, but PL/pgSQL uses
* a special value.
*
* location is the cursor error position for any errors. (Note: this points
* to the head of the target clause, eg "foo" in "foo.bar[baz]". Later we
* might want to decorate indirection cells with their own location info,
* in which case the location argument could probably be dropped.)
*/
Node *
transformAssignmentIndirection(ParseState *pstate,
Node *basenode,
const char *targetName,
bool targetIsSubscripting,
Oid targetTypeId,
int32 targetTypMod,
Oid targetCollation,
List *indirection,
ListCell *indirection_cell,
Node *rhs,
CoercionContext ccontext,
int location)
{
Node *result;
List *subscripts = NIL;
bool isSlice = false;
ListCell *i;
if (indirection_cell && !basenode)
{
/*
* Set up a substitution. We abuse CaseTestExpr for this. It's safe
* to do so because the only nodes that will be above the CaseTestExpr
* in the finished expression will be FieldStore and SubscriptingRef
* nodes. (There could be other stuff in the tree, but it will be
* within other child fields of those node types.)
*/
CaseTestExpr *ctest = makeNode(CaseTestExpr);
ctest->typeId = targetTypeId;
ctest->typeMod = targetTypMod;
ctest->collation = targetCollation;
basenode = (Node *) ctest;
}
/*
* We have to split any field-selection operations apart from
* subscripting. Adjacent A_Indices nodes have to be treated as a single
* multidimensional subscript operation.
*/
for_each_cell(i, indirection, indirection_cell)
{
Node *n = lfirst(i);
if (IsA(n, A_Indices))
{
subscripts = lappend(subscripts, n);
if (((A_Indices *) n)->is_slice)
isSlice = true;
}
else if (IsA(n, A_Star))
{
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("row expansion via \"*\" is not supported here"),
parser_errposition(pstate, location)));
}
else
{
FieldStore *fstore;
Oid baseTypeId;
int32 baseTypeMod;
Oid typrelid;
AttrNumber attnum;
Oid fieldTypeId;
int32 fieldTypMod;
Oid fieldCollation;
Assert(IsA(n, String));
/* process subscripts before this field selection */
if (subscripts)
{
/* recurse, and then return because we're done */
return transformAssignmentSubscripts(pstate,
basenode,
targetName,
targetTypeId,
targetTypMod,
targetCollation,
subscripts,
isSlice,
indirection,
i,
rhs,
ccontext,
location);
}
/* No subscripts, so can process field selection here */
/*
* Look up the composite type, accounting for possibility that
* what we are given is a domain over composite.
*/
baseTypeMod = targetTypMod;
baseTypeId = getBaseTypeAndTypmod(targetTypeId, &baseTypeMod);
typrelid = typeidTypeRelid(baseTypeId);
if (!typrelid)
ereport(ERROR,
(errcode(ERRCODE_DATATYPE_MISMATCH),
errmsg("cannot assign to field \"%s\" of column \"%s\" because its type %s is not a composite type",
strVal(n), targetName,
format_type_be(targetTypeId)),
parser_errposition(pstate, location)));
attnum = get_attnum(typrelid, strVal(n));
if (attnum == InvalidAttrNumber)
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_COLUMN),
errmsg("cannot assign to field \"%s\" of column \"%s\" because there is no such column in data type %s",
strVal(n), targetName,
format_type_be(targetTypeId)),
parser_errposition(pstate, location)));
if (attnum < 0)
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_COLUMN),
errmsg("cannot assign to system column \"%s\"",
strVal(n)),
parser_errposition(pstate, location)));
get_atttypetypmodcoll(typrelid, attnum,
&fieldTypeId, &fieldTypMod, &fieldCollation);
/* recurse to create appropriate RHS for field assign */
rhs = transformAssignmentIndirection(pstate,
NULL,
strVal(n),
false,
fieldTypeId,
fieldTypMod,
fieldCollation,
indirection,
lnext(indirection, i),
rhs,
ccontext,
location);
/* and build a FieldStore node */
fstore = makeNode(FieldStore);
fstore->arg = (Expr *) basenode;
fstore->newvals = list_make1(rhs);
fstore->fieldnums = list_make1_int(attnum);
fstore->resulttype = baseTypeId;
/* If target is a domain, apply constraints */
if (baseTypeId != targetTypeId)
return coerce_to_domain((Node *) fstore,
baseTypeId, baseTypeMod,
targetTypeId,
COERCION_IMPLICIT,
COERCE_IMPLICIT_CAST,
location,
false);
return (Node *) fstore;
}
}
/* process trailing subscripts, if any */
if (subscripts)
{
/* recurse, and then return because we're done */
return transformAssignmentSubscripts(pstate,
basenode,
targetName,
targetTypeId,
targetTypMod,
targetCollation,
subscripts,
isSlice,
indirection,
NULL,
rhs,
ccontext,
location);
}
/* base case: just coerce RHS to match target type ID */
result = coerce_to_target_type(pstate,
rhs, exprType(rhs),
targetTypeId, targetTypMod,
ccontext,
COERCE_IMPLICIT_CAST,
-1);
if (result == NULL)
{
if (targetIsSubscripting)
ereport(ERROR,
(errcode(ERRCODE_DATATYPE_MISMATCH),
errmsg("subscripted assignment to \"%s\" requires type %s"
" but expression is of type %s",
targetName,
format_type_be(targetTypeId),
format_type_be(exprType(rhs))),
errhint("You will need to rewrite or cast the expression."),
parser_errposition(pstate, location)));
else
ereport(ERROR,
(errcode(ERRCODE_DATATYPE_MISMATCH),
errmsg("subfield \"%s\" is of type %s"
" but expression is of type %s",
targetName,
format_type_be(targetTypeId),
format_type_be(exprType(rhs))),
errhint("You will need to rewrite or cast the expression."),
parser_errposition(pstate, location)));
}
return result;
}
/*
* helper for transformAssignmentIndirection: process container assignment
*/
static Node *
transformAssignmentSubscripts(ParseState *pstate,
Node *basenode,
const char *targetName,
Oid targetTypeId,
int32 targetTypMod,
Oid targetCollation,
List *subscripts,
bool isSlice,
List *indirection,
ListCell *next_indirection,
Node *rhs,
CoercionContext ccontext,
int location)
{
Node *result;
SubscriptingRef *sbsref;
Oid containerType;
int32 containerTypMod;
Oid typeNeeded;
int32 typmodNeeded;
Oid collationNeeded;
Assert(subscripts != NIL);
/* Identify the actual container type involved */
containerType = targetTypeId;
containerTypMod = targetTypMod;
transformContainerType(&containerType, &containerTypMod);
/* Process subscripts and identify required type for RHS */
sbsref = transformContainerSubscripts(pstate,
basenode,
containerType,
containerTypMod,
subscripts,
true);
typeNeeded = sbsref->refrestype;
typmodNeeded = sbsref->reftypmod;
/*
* Container normally has same collation as its elements, but there's an
* exception: we might be subscripting a domain over a container type. In
* that case use collation of the base type. (This is shaky for arbitrary
* subscripting semantics, but it doesn't matter all that much since we
* only use this to label the collation of a possible CaseTestExpr.)
*/
if (containerType == targetTypeId)
collationNeeded = targetCollation;
else
collationNeeded = get_typcollation(containerType);
/* recurse to create appropriate RHS for container assign */
rhs = transformAssignmentIndirection(pstate,
NULL,
targetName,
true,
typeNeeded,
typmodNeeded,
collationNeeded,
indirection,
next_indirection,
rhs,
ccontext,
location);
/*
* Insert the already-properly-coerced RHS into the SubscriptingRef. Then
* set refrestype and reftypmod back to the container type's values.
*/
sbsref->refassgnexpr = (Expr *) rhs;
sbsref->refrestype = containerType;
sbsref->reftypmod = containerTypMod;
result = (Node *) sbsref;
/* If target was a domain over container, need to coerce up to the domain */
if (containerType != targetTypeId)
{
Oid resulttype = exprType(result);
result = coerce_to_target_type(pstate,
result, resulttype,
targetTypeId, targetTypMod,
ccontext,
COERCE_IMPLICIT_CAST,
-1);
/* can fail if we had int2vector/oidvector, but not for true domains */
if (result == NULL)
ereport(ERROR,
(errcode(ERRCODE_CANNOT_COERCE),
errmsg("cannot cast type %s to %s",
format_type_be(resulttype),
format_type_be(targetTypeId)),
parser_errposition(pstate, location)));
}
return result;
}
/*
* checkInsertTargets -
* generate a list of INSERT column targets if not supplied, or
* test supplied column names to make sure they are in target table.
* Also return an integer list of the columns' attribute numbers.
*/
List *
checkInsertTargets(ParseState *pstate, List *cols, List **attrnos)
{
*attrnos = NIL;
if (cols == NIL)
{
/*
* Generate default column list for INSERT.
*/
int numcol = RelationGetNumberOfAttributes(pstate->p_target_relation);
int i;
for (i = 0; i < numcol; i++)
{
ResTarget *col;
Form_pg_attribute attr;
attr = TupleDescAttr(pstate->p_target_relation->rd_att, i);
if (attr->attisdropped)
continue;
col = makeNode(ResTarget);
col->name = pstrdup(NameStr(attr->attname));
col->indirection = NIL;
col->val = NULL;
col->location = -1;
cols = lappend(cols, col);
*attrnos = lappend_int(*attrnos, i + 1);
}
}
else
{
/*
* Do initial validation of user-supplied INSERT column list.
*/
Bitmapset *wholecols = NULL;
Bitmapset *partialcols = NULL;
ListCell *tl;
foreach(tl, cols)
{
ResTarget *col = (ResTarget *) lfirst(tl);
char *name = col->name;
int attrno;
/* Lookup column name, ereport on failure */
attrno = attnameAttNum(pstate->p_target_relation, name, false);
if (attrno == InvalidAttrNumber)
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_COLUMN),
errmsg("column \"%s\" of relation \"%s\" does not exist",
name,
RelationGetRelationName(pstate->p_target_relation)),
parser_errposition(pstate, col->location)));
/*
* Check for duplicates, but only of whole columns --- we allow
* INSERT INTO foo (col.subcol1, col.subcol2)
*/
if (col->indirection == NIL)
{
/* whole column; must not have any other assignment */
if (bms_is_member(attrno, wholecols) ||
bms_is_member(attrno, partialcols))
ereport(ERROR,
(errcode(ERRCODE_DUPLICATE_COLUMN),
errmsg("column \"%s\" specified more than once",
name),
parser_errposition(pstate, col->location)));
wholecols = bms_add_member(wholecols, attrno);
}
else
{
/* partial column; must not have any whole assignment */
if (bms_is_member(attrno, wholecols))
ereport(ERROR,
(errcode(ERRCODE_DUPLICATE_COLUMN),
errmsg("column \"%s\" specified more than once",
name),
parser_errposition(pstate, col->location)));
partialcols = bms_add_member(partialcols, attrno);
}
*attrnos = lappend_int(*attrnos, attrno);
}
}
return cols;
}
/*
* ExpandColumnRefStar()
* Transforms foo.* into a list of expressions or targetlist entries.
*
* This handles the case where '*' appears as the last or only item in a
* ColumnRef. The code is shared between the case of foo.* at the top level
* in a SELECT target list (where we want TargetEntry nodes in the result)
* and foo.* in a ROW() or VALUES() construct (where we want just bare
* expressions).
*
* The referenced columns are marked as requiring SELECT access.
*/
static List *
ExpandColumnRefStar(ParseState *pstate, ColumnRef *cref,
bool make_target_entry)
{
List *fields = cref->fields;
int numnames = list_length(fields);
if (numnames == 1)
{
/*
* Target item is a bare '*', expand all tables
*
* (e.g., SELECT * FROM emp, dept)
*
* Since the grammar only accepts bare '*' at top level of SELECT, we
* need not handle the make_target_entry==false case here.
*/
Assert(make_target_entry);
return ExpandAllTables(pstate, cref->location);
}
else
{
/*
* Target item is relation.*, expand that table
*
* (e.g., SELECT emp.*, dname FROM emp, dept)
*
* Note: this code is a lot like transformColumnRef; it's tempting to
* call that instead and then replace the resulting whole-row Var with
* a list of Vars. However, that would leave us with the RTE's
* selectedCols bitmap showing the whole row as needing select
* permission, as well as the individual columns. That would be
* incorrect (since columns added later shouldn't need select
* permissions). We could try to remove the whole-row permission bit
* after the fact, but duplicating code is less messy.
*/
char *nspname = NULL;
char *relname = NULL;
ParseNamespaceItem *nsitem = NULL;
int levels_up;
enum
{
CRSERR_NO_RTE,
CRSERR_WRONG_DB,
CRSERR_TOO_MANY
} crserr = CRSERR_NO_RTE;
/*
* Give the PreParseColumnRefHook, if any, first shot. If it returns
* non-null then we should use that expression.
*/
if (pstate->p_pre_columnref_hook != NULL)
{
Node *node;
node = pstate->p_pre_columnref_hook(pstate, cref);
if (node != NULL)
return ExpandRowReference(pstate, node, make_target_entry);
}
switch (numnames)
{
case 2:
relname = strVal(linitial(fields));
nsitem = refnameNamespaceItem(pstate, nspname, relname,
cref->location,
&levels_up);
break;
case 3:
nspname = strVal(linitial(fields));
relname = strVal(lsecond(fields));
nsitem = refnameNamespaceItem(pstate, nspname, relname,
cref->location,
&levels_up);
break;
case 4:
{
char *catname = strVal(linitial(fields));
/*
* We check the catalog name and then ignore it.
*/
if (strcmp(catname, get_database_name(MyDatabaseId)) != 0)
{
crserr = CRSERR_WRONG_DB;
break;
}
nspname = strVal(lsecond(fields));
relname = strVal(lthird(fields));
nsitem = refnameNamespaceItem(pstate, nspname, relname,
cref->location,
&levels_up);
break;
}
default:
crserr = CRSERR_TOO_MANY;
break;
}
/*
* Now give the PostParseColumnRefHook, if any, a chance. We cheat a
* bit by passing the RangeTblEntry, not a Var, as the planned
* translation. (A single Var wouldn't be strictly correct anyway.
* This convention allows hooks that really care to know what is
* happening. It might be better to pass the nsitem, but we'd have to
* promote that struct to a full-fledged Node type so that callees
* could identify its type.)
*/
if (pstate->p_post_columnref_hook != NULL)
{
Node *node;
node = pstate->p_post_columnref_hook(pstate, cref,
(Node *) (nsitem ? nsitem->p_rte : NULL));
if (node != NULL)
{
if (nsitem != NULL)
ereport(ERROR,
(errcode(ERRCODE_AMBIGUOUS_COLUMN),
errmsg("column reference \"%s\" is ambiguous",
NameListToString(cref->fields)),
parser_errposition(pstate, cref->location)));
return ExpandRowReference(pstate, node, make_target_entry);
}
}
/*
* Throw error if no translation found.
*/
if (nsitem == NULL)
{
switch (crserr)
{
case CRSERR_NO_RTE:
errorMissingRTE(pstate, makeRangeVar(nspname, relname,
cref->location));
break;
case CRSERR_WRONG_DB:
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("cross-database references are not implemented: %s",
NameListToString(cref->fields)),
parser_errposition(pstate, cref->location)));
break;
case CRSERR_TOO_MANY:
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("improper qualified name (too many dotted names): %s",
NameListToString(cref->fields)),
parser_errposition(pstate, cref->location)));
break;
}
}
/*
* OK, expand the nsitem into fields.
*/
return ExpandSingleTable(pstate, nsitem, levels_up, cref->location,
make_target_entry);
}
}
/*
* ExpandAllTables()
* Transforms '*' (in the target list) into a list of targetlist entries.
*
* tlist entries are generated for each relation visible for unqualified
* column name access. We do not consider qualified-name-only entries because
* that would include input tables of aliasless JOINs, NEW/OLD pseudo-entries,
* etc.
*
* The referenced relations/columns are marked as requiring SELECT access.
*/
static List *
ExpandAllTables(ParseState *pstate, int location)
{
List *target = NIL;
bool found_table = false;
ListCell *l;
foreach(l, pstate->p_namespace)
{
ParseNamespaceItem *nsitem = (ParseNamespaceItem *) lfirst(l);
/* Ignore table-only items */
if (!nsitem->p_cols_visible)
continue;
/* Should not have any lateral-only items when parsing targetlist */
Assert(!nsitem->p_lateral_only);
/* Remember we found a p_cols_visible item */
found_table = true;
target = list_concat(target,
expandNSItemAttrs(pstate,
nsitem,
0,
location));
}
/*
* Check for "SELECT *;". We do it this way, rather than checking for
* target == NIL, because we want to allow SELECT * FROM a zero_column
* table.
*/
if (!found_table)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("SELECT * with no tables specified is not valid"),
parser_errposition(pstate, location)));
return target;
}
/*
* ExpandIndirectionStar()
* Transforms foo.* into a list of expressions or targetlist entries.
*
* This handles the case where '*' appears as the last item in A_Indirection.
* The code is shared between the case of foo.* at the top level in a SELECT
* target list (where we want TargetEntry nodes in the result) and foo.* in
* a ROW() or VALUES() construct (where we want just bare expressions).
* For robustness, we use a separate "make_target_entry" flag to control
* this rather than relying on exprKind.
*/
static List *
ExpandIndirectionStar(ParseState *pstate, A_Indirection *ind,
bool make_target_entry, ParseExprKind exprKind)
{
Node *expr;
/* Strip off the '*' to create a reference to the rowtype object */
ind = copyObject(ind);
ind->indirection = list_truncate(ind->indirection,
list_length(ind->indirection) - 1);
/* And transform that */
expr = transformExpr(pstate, (Node *) ind, exprKind);
/* Expand the rowtype expression into individual fields */
return ExpandRowReference(pstate, expr, make_target_entry);
}
/*
* ExpandSingleTable()
* Transforms foo.* into a list of expressions or targetlist entries.
*
* This handles the case where foo has been determined to be a simple
* reference to an RTE, so we can just generate Vars for the expressions.
*
* The referenced columns are marked as requiring SELECT access.
*/
static List *
ExpandSingleTable(ParseState *pstate, ParseNamespaceItem *nsitem,
int sublevels_up, int location, bool make_target_entry)
{
if (make_target_entry)
{
/* expandNSItemAttrs handles permissions marking */
return expandNSItemAttrs(pstate, nsitem, sublevels_up, location);
}
else
{
RangeTblEntry *rte = nsitem->p_rte;
List *vars;
ListCell *l;
vars = expandNSItemVars(nsitem, sublevels_up, location, NULL);
/*
* Require read access to the table. This is normally redundant with
* the markVarForSelectPriv calls below, but not if the table has zero
* columns. We need not do anything if the nsitem is for a join: its
* component tables will have been marked ACL_SELECT when they were
* added to the rangetable. (This step changes things only for the
* target relation of UPDATE/DELETE, which cannot be under a join.)
*/
if (rte->rtekind == RTE_RELATION)
rte->requiredPerms |= ACL_SELECT;
/* Require read access to each column */
foreach(l, vars)
{
Var *var = (Var *) lfirst(l);
markVarForSelectPriv(pstate, var);
}
return vars;
}
}
/*
* ExpandRowReference()
* Transforms foo.* into a list of expressions or targetlist entries.
*
* This handles the case where foo is an arbitrary expression of composite
* type.
*/
static List *
ExpandRowReference(ParseState *pstate, Node *expr,
bool make_target_entry)
{
List *result = NIL;
TupleDesc tupleDesc;
int numAttrs;
int i;
/*
* If the rowtype expression is a whole-row Var, we can expand the fields
* as simple Vars. Note: if the RTE is a relation, this case leaves us
* with the RTE's selectedCols bitmap showing the whole row as needing
* select permission, as well as the individual columns. However, we can
* only get here for weird notations like (table.*).*, so it's not worth
* trying to clean up --- arguably, the permissions marking is correct
* anyway for such cases.
*/
if (IsA(expr, Var) &&
((Var *) expr)->varattno == InvalidAttrNumber)
{
Var *var = (Var *) expr;
ParseNamespaceItem *nsitem;
nsitem = GetNSItemByRangeTablePosn(pstate, var->varno, var->varlevelsup);
return ExpandSingleTable(pstate, nsitem, var->varlevelsup, var->location, make_target_entry);
}
/*
* Otherwise we have to do it the hard way. Our current implementation is
* to generate multiple copies of the expression and do FieldSelects.
* (This can be pretty inefficient if the expression involves nontrivial
* computation :-(.)
*
* Verify it's a composite type, and get the tupdesc.
* get_expr_result_tupdesc() handles this conveniently.
*
* If it's a Var of type RECORD, we have to work even harder: we have to
* find what the Var refers to, and pass that to get_expr_result_tupdesc.
* That task is handled by expandRecordVariable().
*/
if (IsA(expr, Var) &&
((Var *) expr)->vartype == RECORDOID)
tupleDesc = expandRecordVariable(pstate, (Var *) expr, 0);
else
tupleDesc = get_expr_result_tupdesc(expr, false);
Assert(tupleDesc);
/* Generate a list of references to the individual fields */
numAttrs = tupleDesc->natts;
for (i = 0; i < numAttrs; i++)
{
Form_pg_attribute att = TupleDescAttr(tupleDesc, i);
FieldSelect *fselect;
if (att->attisdropped)
continue;
fselect = makeNode(FieldSelect);
fselect->arg = (Expr *) copyObject(expr);
fselect->fieldnum = i + 1;
fselect->resulttype = att->atttypid;
fselect->resulttypmod = att->atttypmod;
/* save attribute's collation for parse_collate.c */
fselect->resultcollid = att->attcollation;
if (make_target_entry)
{
/* add TargetEntry decoration */
TargetEntry *te;
te = makeTargetEntry((Expr *) fselect,
(AttrNumber) pstate->p_next_resno++,
pstrdup(NameStr(att->attname)),
false);
result = lappend(result, te);
}
else
result = lappend(result, fselect);
}
return result;
}
/*
* expandRecordVariable
* Get the tuple descriptor for a Var of type RECORD, if possible.
*
* Since no actual table or view column is allowed to have type RECORD, such
* a Var must refer to a JOIN or FUNCTION RTE or to a subquery output. We
* drill down to find the ultimate defining expression and attempt to infer
* the tupdesc from it. We ereport if we can't determine the tupdesc.
*
* levelsup is an extra offset to interpret the Var's varlevelsup correctly.
*/
TupleDesc
expandRecordVariable(ParseState *pstate, Var *var, int levelsup)
{
TupleDesc tupleDesc;
int netlevelsup;
RangeTblEntry *rte;
AttrNumber attnum;
Node *expr;
/* Check my caller didn't mess up */
Assert(IsA(var, Var));
Assert(var->vartype == RECORDOID);
/*
* Note: it's tempting to use GetNSItemByRangeTablePosn here so that we
* can use expandNSItemVars instead of expandRTE; but that does not work
* for some of the recursion cases below, where we have consed up a
* ParseState that lacks p_namespace data.
*/
netlevelsup = var->varlevelsup + levelsup;
rte = GetRTEByRangeTablePosn(pstate, var->varno, netlevelsup);
attnum = var->varattno;
if (attnum == InvalidAttrNumber)
{
/* Whole-row reference to an RTE, so expand the known fields */
List *names,
*vars;
ListCell *lname,
*lvar;
int i;
expandRTE(rte, var->varno, 0, var->location, false,
&names, &vars);
tupleDesc = CreateTemplateTupleDesc(list_length(vars));
i = 1;
forboth(lname, names, lvar, vars)
{
char *label = strVal(lfirst(lname));
Node *varnode = (Node *) lfirst(lvar);
TupleDescInitEntry(tupleDesc, i,
label,
exprType(varnode),
exprTypmod(varnode),
0);
TupleDescInitEntryCollation(tupleDesc, i,
exprCollation(varnode));
i++;
}
Assert(lname == NULL && lvar == NULL); /* lists same length? */
return tupleDesc;
}
expr = (Node *) var; /* default if we can't drill down */
switch (rte->rtekind)
{
case RTE_RELATION:
case RTE_VALUES:
case RTE_NAMEDTUPLESTORE:
case RTE_RESULT:
/*
* This case should not occur: a column of a table, values list,
* or ENR shouldn't have type RECORD. Fall through and fail (most
* likely) at the bottom.
*/
break;
case RTE_SUBQUERY:
{
/* Subselect-in-FROM: examine sub-select's output expr */
TargetEntry *ste = get_tle_by_resno(rte->subquery->targetList,
attnum);
if (ste == NULL || ste->resjunk)
elog(ERROR, "subquery %s does not have attribute %d",
rte->eref->aliasname, attnum);
expr = (Node *) ste->expr;
if (IsA(expr, Var))
{
/*
* Recurse into the sub-select to see what its Var refers
* to. We have to build an additional level of ParseState
* to keep in step with varlevelsup in the subselect.
*/
ParseState mypstate;
MemSet(&mypstate, 0, sizeof(mypstate));
mypstate.parentParseState = pstate;
mypstate.p_rtable = rte->subquery->rtable;
/* don't bother filling the rest of the fake pstate */
return expandRecordVariable(&mypstate, (Var *) expr, 0);
}
/* else fall through to inspect the expression */
}
break;
case RTE_JOIN:
/* Join RTE --- recursively inspect the alias variable */
Assert(attnum > 0 && attnum <= list_length(rte->joinaliasvars));
expr = (Node *) list_nth(rte->joinaliasvars, attnum - 1);
Assert(expr != NULL);
/* We intentionally don't strip implicit coercions here */
if (IsA(expr, Var))
return expandRecordVariable(pstate, (Var *) expr, netlevelsup);
/* else fall through to inspect the expression */
break;
case RTE_FUNCTION:
/*
* We couldn't get here unless a function is declared with one of
* its result columns as RECORD, which is not allowed.
*/
break;
case RTE_TABLEFUNC:
/*
* Table function cannot have columns with RECORD type.
*/
break;
case RTE_CTE:
/* CTE reference: examine subquery's output expr */
if (!rte->self_reference)
{
CommonTableExpr *cte = GetCTEForRTE(pstate, rte, netlevelsup);
TargetEntry *ste;
ste = get_tle_by_resno(GetCTETargetList(cte), attnum);
if (ste == NULL || ste->resjunk)
elog(ERROR, "CTE %s does not have attribute %d",
rte->eref->aliasname, attnum);
expr = (Node *) ste->expr;
if (IsA(expr, Var))
{
/*
* Recurse into the CTE to see what its Var refers to. We
* have to build an additional level of ParseState to keep
* in step with varlevelsup in the CTE; furthermore it
* could be an outer CTE.
*/
ParseState mypstate;
Index levelsup;
MemSet(&mypstate, 0, sizeof(mypstate));
/* this loop must work, since GetCTEForRTE did */
for (levelsup = 0;
levelsup < rte->ctelevelsup + netlevelsup;
levelsup++)
pstate = pstate->parentParseState;
mypstate.parentParseState = pstate;
mypstate.p_rtable = ((Query *) cte->ctequery)->rtable;
/* don't bother filling the rest of the fake pstate */
return expandRecordVariable(&mypstate, (Var *) expr, 0);
}
/* else fall through to inspect the expression */
}
break;
}
/*
* We now have an expression we can't expand any more, so see if
* get_expr_result_tupdesc() can do anything with it.
*/
return get_expr_result_tupdesc(expr, false);
}
/*
* FigureColname -
* if the name of the resulting column is not specified in the target
* list, we have to guess a suitable name. The SQL spec provides some
* guidance, but not much...
*
* Note that the argument is the *untransformed* parse tree for the target
* item. This is a shade easier to work with than the transformed tree.
*/
char *
FigureColname(Node *node)
{
char *name = NULL;
(void) FigureColnameInternal(node, &name);
if (name != NULL)
return name;
/* default result if we can't guess anything */
return "?column?";
}
/*
* FigureIndexColname -
* choose the name for an expression column in an index
*
* This is actually just like FigureColname, except we return NULL if
* we can't pick a good name.
*/
char *
FigureIndexColname(Node *node)
{
char *name = NULL;
(void) FigureColnameInternal(node, &name);
return name;
}
/*
* FigureColnameInternal -
* internal workhorse for FigureColname
*
* Return value indicates strength of confidence in result:
* 0 - no information
* 1 - second-best name choice
* 2 - good name choice
* The return value is actually only used internally.
* If the result isn't zero, *name is set to the chosen name.
*/
static int
FigureColnameInternal(Node *node, char **name)
{
int strength = 0;
if (node == NULL)
return strength;
switch (nodeTag(node))
{
case T_ColumnRef:
{
char *fname = NULL;
ListCell *l;
/* find last field name, if any, ignoring "*" */
foreach(l, ((ColumnRef *) node)->fields)
{
Node *i = lfirst(l);
if (IsA(i, String))
fname = strVal(i);
}
if (fname)
{
*name = fname;
return 2;
}
}
break;
case T_A_Indirection:
{
A_Indirection *ind = (A_Indirection *) node;
char *fname = NULL;
ListCell *l;
/* find last field name, if any, ignoring "*" and subscripts */
foreach(l, ind->indirection)
{
Node *i = lfirst(l);
if (IsA(i, String))
fname = strVal(i);
}
if (fname)
{
*name = fname;
return 2;
}
return FigureColnameInternal(ind->arg, name);
}
break;
case T_FuncCall:
*name = strVal(llast(((FuncCall *) node)->funcname));
return 2;
case T_A_Expr:
if (((A_Expr *) node)->kind == AEXPR_NULLIF)
{
/* make nullif() act like a regular function */
*name = "nullif";
return 2;
}
break;
case T_TypeCast:
strength = FigureColnameInternal(((TypeCast *) node)->arg,
name);
if (strength <= 1)
{
if (((TypeCast *) node)->typeName != NULL)
{
*name = strVal(llast(((TypeCast *) node)->typeName->names));
return 1;
}
}
break;
case T_CollateClause:
return FigureColnameInternal(((CollateClause *) node)->arg, name);
case T_GroupingFunc:
/* make GROUPING() act like a regular function */
*name = "grouping";
return 2;
case T_SubLink:
switch (((SubLink *) node)->subLinkType)
{
case EXISTS_SUBLINK:
*name = "exists";
return 2;
case ARRAY_SUBLINK:
*name = "array";
return 2;
case EXPR_SUBLINK:
{
/* Get column name of the subquery's single target */
SubLink *sublink = (SubLink *) node;
Query *query = (Query *) sublink->subselect;
/*
* The subquery has probably already been transformed,
* but let's be careful and check that. (The reason
* we can see a transformed subquery here is that
* transformSubLink is lazy and modifies the SubLink
* node in-place.)
*/
if (IsA(query, Query))
{
TargetEntry *te = (TargetEntry *) linitial(query->targetList);
if (te->resname)
{
*name = te->resname;
return 2;
}
}
}
break;
/* As with other operator-like nodes, these have no names */
case MULTIEXPR_SUBLINK:
case ALL_SUBLINK:
case ANY_SUBLINK:
case ROWCOMPARE_SUBLINK:
case CTE_SUBLINK:
break;
}
break;
case T_CaseExpr:
strength = FigureColnameInternal((Node *) ((CaseExpr *) node)->defresult,
name);
if (strength <= 1)
{
*name = "case";
return 1;
}
break;
case T_A_ArrayExpr:
/* make ARRAY[] act like a function */
*name = "array";
return 2;
case T_RowExpr:
/* make ROW() act like a function */
*name = "row";
return 2;
case T_CoalesceExpr:
/* make coalesce() act like a regular function */
*name = "coalesce";
return 2;
case T_MinMaxExpr:
/* make greatest/least act like a regular function */
switch (((MinMaxExpr *) node)->op)
{
case IS_GREATEST:
*name = "greatest";
return 2;
case IS_LEAST:
*name = "least";
return 2;
}
break;
case T_SQLValueFunction:
/* make these act like a function or variable */
switch (((SQLValueFunction *) node)->op)
{
case SVFOP_CURRENT_DATE:
*name = "current_date";
return 2;
case SVFOP_CURRENT_TIME:
case SVFOP_CURRENT_TIME_N:
*name = "current_time";
return 2;
case SVFOP_CURRENT_TIMESTAMP:
case SVFOP_CURRENT_TIMESTAMP_N:
*name = "current_timestamp";
return 2;
case SVFOP_LOCALTIME:
case SVFOP_LOCALTIME_N:
*name = "localtime";
return 2;
case SVFOP_LOCALTIMESTAMP:
case SVFOP_LOCALTIMESTAMP_N:
*name = "localtimestamp";
return 2;
case SVFOP_CURRENT_ROLE:
*name = "current_role";
return 2;
case SVFOP_CURRENT_USER:
*name = "current_user";
return 2;
case SVFOP_USER:
*name = "user";
return 2;
case SVFOP_SESSION_USER:
*name = "session_user";
return 2;
case SVFOP_CURRENT_CATALOG:
*name = "current_catalog";
return 2;
case SVFOP_CURRENT_SCHEMA:
*name = "current_schema";
return 2;
}
break;
case T_XmlExpr:
/* make SQL/XML functions act like a regular function */
switch (((XmlExpr *) node)->op)
{
case IS_XMLCONCAT:
*name = "xmlconcat";
return 2;
case IS_XMLELEMENT:
*name = "xmlelement";
return 2;
case IS_XMLFOREST:
*name = "xmlforest";
return 2;
case IS_XMLPARSE:
*name = "xmlparse";
return 2;
case IS_XMLPI:
*name = "xmlpi";
return 2;
case IS_XMLROOT:
*name = "xmlroot";
return 2;
case IS_XMLSERIALIZE:
*name = "xmlserialize";
return 2;
case IS_DOCUMENT:
/* nothing */
break;
}
break;
case T_XmlSerialize:
*name = "xmlserialize";
return 2;
default:
break;
}
return strength;
}
|