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
|
/*-------------------------------------------------------------------------
*
* parse_target.c
* handle target lists
*
* Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/parser/parse_target.c,v 1.9 1998/02/10 16:03:41 momjian Exp $
*
*-------------------------------------------------------------------------
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "postgres.h"
#include "catalog/pg_type.h"
#include "nodes/makefuncs.h"
#include "nodes/primnodes.h"
#include "parser/parse_expr.h"
#include "parser/parse_func.h"
#include "parser/parse_node.h"
#include "parser/parse_relation.h"
#include "parser/parse_target.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
static List *expandAllTables(ParseState *pstate);
static char *figureColname(Node *expr, Node *resval);
static TargetEntry *make_targetlist_expr(ParseState *pstate,
char *colname,
Node *expr,
List *arrayRef);
/*
* transformTargetList -
* turns a list of ResTarget's into a list of TargetEntry's
*/
List *
transformTargetList(ParseState *pstate, List *targetlist)
{
List *p_target = NIL;
List *tail_p_target = NIL;
while (targetlist != NIL)
{
ResTarget *res = (ResTarget *) lfirst(targetlist);
TargetEntry *tent = makeNode(TargetEntry);
switch (nodeTag(res->val))
{
case T_Ident:
{
Node *expr;
Oid type_id;
int16 type_mod;
char *identname;
char *resname;
identname = ((Ident *) res->val)->name;
handleTargetColname(pstate, &res->name, NULL, identname);
/*
* here we want to look for column names only, not relation
* names (even though they can be stored in Ident nodes, too)
*/
expr = transformIdent(pstate, (Node *) res->val, EXPR_COLUMN_FIRST);
type_id = exprType(expr);
if (nodeTag(expr) == T_Var)
type_mod = ((Var *)expr)->vartypmod;
else
type_mod = -1;
resname = (res->name) ? res->name : identname;
tent->resdom = makeResdom((AttrNumber) pstate->p_last_resno++,
(Oid) type_id,
type_mod,
resname,
(Index) 0,
(Oid) 0,
0);
tent->expr = expr;
break;
}
case T_ParamNo:
case T_FuncCall:
case T_A_Const:
case T_A_Expr:
{
Node *expr = transformExpr(pstate, (Node *) res->val, EXPR_COLUMN_FIRST);
handleTargetColname(pstate, &res->name, NULL, NULL);
/* note indirection has not been transformed */
if (pstate->p_is_insert && res->indirection != NIL)
{
/* this is an array assignment */
char *val;
char *str,
*save_str;
List *elt;
int i = 0,
ndims;
int lindx[MAXDIM],
uindx[MAXDIM];
int resdomno;
Relation rd;
Value *constval;
if (exprType(expr) != UNKNOWNOID ||
!IsA(expr, Const))
elog(ERROR, "yyparse: string constant expected");
val = (char *) textout((struct varlena *)
((Const *) expr)->constvalue);
str = save_str = (char *) palloc(strlen(val) + MAXDIM * 25 + 2);
foreach(elt, res->indirection)
{
A_Indices *aind = (A_Indices *) lfirst(elt);
aind->uidx = transformExpr(pstate, aind->uidx, EXPR_COLUMN_FIRST);
if (!IsA(aind->uidx, Const))
elog(ERROR,
"Array Index for Append should be a constant");
uindx[i] = ((Const *) aind->uidx)->constvalue;
if (aind->lidx != NULL)
{
aind->lidx = transformExpr(pstate, aind->lidx, EXPR_COLUMN_FIRST);
if (!IsA(aind->lidx, Const))
elog(ERROR,
"Array Index for Append should be a constant");
lindx[i] = ((Const *) aind->lidx)->constvalue;
}
else
{
lindx[i] = 1;
}
if (lindx[i] > uindx[i])
elog(ERROR, "yyparse: lower index cannot be greater than upper index");
sprintf(str, "[%d:%d]", lindx[i], uindx[i]);
str += strlen(str);
i++;
}
sprintf(str, "=%s", val);
rd = pstate->p_target_relation;
Assert(rd != NULL);
resdomno = attnameAttNum(rd, res->name);
ndims = attnumAttNelems(rd, resdomno);
if (i != ndims)
elog(ERROR, "yyparse: array dimensions do not match");
constval = makeNode(Value);
constval->type = T_String;
constval->val.str = save_str;
tent = make_targetlist_expr(pstate, res->name,
(Node *) make_const(constval),
NULL);
pfree(save_str);
}
else
{
char *colname = res->name;
/* this is not an array assignment */
if (colname == NULL)
{
/*
* if you're wondering why this is here, look
* at the yacc grammar for why a name can be
* missing. -ay
*/
colname = figureColname(expr, res->val);
}
if (res->indirection)
{
List *ilist = res->indirection;
while (ilist != NIL)
{
A_Indices *ind = lfirst(ilist);
ind->lidx = transformExpr(pstate, ind->lidx, EXPR_COLUMN_FIRST);
ind->uidx = transformExpr(pstate, ind->uidx, EXPR_COLUMN_FIRST);
ilist = lnext(ilist);
}
}
res->name = colname;
tent = make_targetlist_expr(pstate, res->name, expr,
res->indirection);
}
break;
}
case T_Attr:
{
Oid type_id;
int16 type_mod;
Attr *att = (Attr *) res->val;
Node *result;
char *attrname;
char *resname;
Resdom *resnode;
List *attrs = att->attrs;
/*
* Target item is a single '*', expand all tables (eg.
* SELECT * FROM emp)
*/
if (att->relname != NULL && !strcmp(att->relname, "*"))
{
if (tail_p_target == NIL)
p_target = tail_p_target = expandAllTables(pstate);
else
lnext(tail_p_target) = expandAllTables(pstate);
while (lnext(tail_p_target) != NIL)
/* make sure we point to the last target entry */
tail_p_target = lnext(tail_p_target);
/*
* skip rest of while loop
*/
targetlist = lnext(targetlist);
continue;
}
/*
* Target item is relation.*, expand the table (eg.
* SELECT emp.*, dname FROM emp, dept)
*/
attrname = strVal(lfirst(att->attrs));
if (att->attrs != NIL && !strcmp(attrname, "*"))
{
/*
* tail_p_target is the target list we're building
* in the while loop. Make sure we fix it after
* appending more nodes.
*/
if (tail_p_target == NIL)
p_target = tail_p_target = expandAll(pstate, att->relname,
att->relname, &pstate->p_last_resno);
else
lnext(tail_p_target) =
expandAll(pstate, att->relname, att->relname,
&pstate->p_last_resno);
while (lnext(tail_p_target) != NIL)
/* make sure we point to the last target entry */
tail_p_target = lnext(tail_p_target);
/*
* skip the rest of the while loop
*/
targetlist = lnext(targetlist);
continue;
}
/*
* Target item is fully specified: ie. relation.attribute
*/
result = ParseNestedFuncOrColumn(pstate, att, &pstate->p_last_resno,EXPR_COLUMN_FIRST);
handleTargetColname(pstate, &res->name, att->relname, attrname);
if (att->indirection != NIL)
{
List *ilist = att->indirection;
while (ilist != NIL)
{
A_Indices *ind = lfirst(ilist);
ind->lidx = transformExpr(pstate, ind->lidx, EXPR_COLUMN_FIRST);
ind->uidx = transformExpr(pstate, ind->uidx, EXPR_COLUMN_FIRST);
ilist = lnext(ilist);
}
result = (Node *) make_array_ref(result, att->indirection);
}
type_id = exprType(result);
if (nodeTag(result) == T_Var)
type_mod = ((Var *)result)->vartypmod;
else
type_mod = -1;
/* move to last entry */
while (lnext(attrs) != NIL)
attrs = lnext(attrs);
resname = (res->name) ? res->name : strVal(lfirst(attrs));
resnode = makeResdom((AttrNumber) pstate->p_last_resno++,
(Oid) type_id,
type_mod,
resname,
(Index) 0,
(Oid) 0,
0);
tent->resdom = resnode;
tent->expr = result;
break;
}
default:
/* internal error */
elog(ERROR,
"internal error: do not know how to transform targetlist");
break;
}
if (p_target == NIL)
{
p_target = tail_p_target = lcons(tent, NIL);
}
else
{
lnext(tail_p_target) = lcons(tent, NIL);
tail_p_target = lnext(tail_p_target);
}
targetlist = lnext(targetlist);
}
return p_target;
}
/*
* make_targetlist_expr -
* make a TargetEntry from an expression
*
* arrayRef is a list of transformed A_Indices
*/
static TargetEntry *
make_targetlist_expr(ParseState *pstate,
char *colname,
Node *expr,
List *arrayRef)
{
Oid type_id,
attrtype;
int16 type_mod,
attrtypmod;
int resdomno;
Relation rd;
bool attrisset;
TargetEntry *tent;
Resdom *resnode;
if (expr == NULL)
elog(ERROR, "make_targetlist_expr: invalid use of NULL expression");
type_id = exprType(expr);
if (nodeTag(expr) == T_Var)
type_mod = ((Var *)expr)->vartypmod;
else
type_mod = -1;
/* Processes target columns that will be receiving results */
if (pstate->p_is_insert || pstate->p_is_update)
{
/*
* insert or update query -- insert, update work only on one
* relation, so multiple occurence of same resdomno is bogus
*/
rd = pstate->p_target_relation;
Assert(rd != NULL);
resdomno = attnameAttNum(rd, colname);
attrisset = attnameIsSet(rd, colname);
attrtype = attnumTypeId(rd, resdomno);
if ((arrayRef != NIL) && (lfirst(arrayRef) == NIL))
attrtype = GetArrayElementType(attrtype);
attrtypmod = rd->rd_att->attrs[resdomno - 1]->atttypmod;
#if 0
if (Input_is_string && Typecast_ok)
{
Datum val;
if (type_id == typeTypeId(type("unknown")))
{
val = (Datum) textout((struct varlena *)
((Const) lnext(expr))->constvalue);
}
else
{
val = ((Const) lnext(expr))->constvalue;
}
if (attrisset)
{
lnext(expr) = makeConst(attrtype,
attrlen,
val,
false,
true,
true, /* is set */
false);
}
else
{
lnext(expr) =
makeConst(attrtype,
attrlen,
(Datum) fmgr(typeidRetinfunc(attrtype),
val, typeidTypElem(attrtype), -1),
false,
true /* Maybe correct-- 80% chance */ ,
false, /* is not a set */
false);
}
}
else if ((Typecast_ok) && (attrtype != type_id))
{
lnext(expr) =
parser_typecast2(expr, typeidType(attrtype));
}
else if (attrtype != type_id)
{
if ((attrtype == INT2OID) && (type_id == INT4OID))
lfirst(expr) = lispInteger(INT2OID); /* handle CASHOID too */
else if ((attrtype == FLOAT4OID) && (type_id == FLOAT8OID))
lfirst(expr) = lispInteger(FLOAT4OID);
else
elog(ERROR, "unequal type in tlist : %s \n", colname);
}
Input_is_string = false;
Input_is_integer = false;
Typecast_ok = true;
#endif
if (attrtype != type_id)
{
if (IsA(expr, Const))
{
/* try to cast the constant */
if (arrayRef && !(((A_Indices *) lfirst(arrayRef))->lidx))
{
/* updating a single item */
Oid typelem = typeidTypElem(attrtype);
expr = (Node *) parser_typecast2(expr,
type_id,
typeidType(typelem),
attrtypmod);
}
else
expr = (Node *) parser_typecast2(expr,
type_id,
typeidType(attrtype),
attrtypmod);
}
else
{
/* currently, we can't handle casting of expressions */
elog(ERROR, "parser: attribute '%s' is of type '%s' but expression is of type '%s'",
colname,
typeidTypeName(attrtype),
typeidTypeName(type_id));
}
}
if (arrayRef != NIL)
{
Expr *target_expr;
Attr *att = makeNode(Attr);
List *ar = arrayRef;
List *upperIndexpr = NIL;
List *lowerIndexpr = NIL;
att->relname = pstrdup(RelationGetRelationName(rd)->data);
att->attrs = lcons(makeString(colname), NIL);
target_expr = (Expr *) ParseNestedFuncOrColumn(pstate, att,
&pstate->p_last_resno,
EXPR_COLUMN_FIRST);
while (ar != NIL)
{
A_Indices *ind = lfirst(ar);
if (lowerIndexpr || (!upperIndexpr && ind->lidx))
{
/*
* XXX assume all lowerIndexpr is non-null in this
* case
*/
lowerIndexpr = lappend(lowerIndexpr, ind->lidx);
}
upperIndexpr = lappend(upperIndexpr, ind->uidx);
ar = lnext(ar);
}
expr = (Node *) make_array_set(target_expr,
upperIndexpr,
lowerIndexpr,
(Expr *) expr);
attrtype = attnumTypeId(rd, resdomno);
attrtypmod = get_atttypmod(rd->rd_id, resdomno);
}
}
else
{
resdomno = pstate->p_last_resno++;
attrtype = type_id;
attrtypmod = type_mod;
}
tent = makeNode(TargetEntry);
resnode = makeResdom((AttrNumber) resdomno,
(Oid) attrtype,
attrtypmod,
colname,
(Index) 0,
(Oid) 0,
0);
tent->resdom = resnode;
tent->expr = expr;
return tent;
}
/*
* makeTargetNames -
* generate a list of column names if not supplied or
* test supplied column names to make sure they are in target table
* (used exclusively for inserts)
*/
List *
makeTargetNames(ParseState *pstate, List *cols)
{
List *tl = NULL;
/* Generate ResTarget if not supplied */
if (cols == NIL)
{
int numcol;
int i;
AttributeTupleForm *attr = pstate->p_target_relation->rd_att->attrs;
numcol = pstate->p_target_relation->rd_rel->relnatts;
for (i = 0; i < numcol; i++)
{
Ident *id = makeNode(Ident);
id->name = palloc(NAMEDATALEN);
StrNCpy(id->name, attr[i]->attname.data, NAMEDATALEN);
id->indirection = NIL;
id->isRel = false;
if (tl == NIL)
cols = tl = lcons(id, NIL);
else
{
lnext(tl) = lcons(id, NIL);
tl = lnext(tl);
}
}
}
else
{
foreach(tl, cols)
{
List *nxt;
char *name = ((Ident *) lfirst(tl))->name;
/* elog on failure */
attnameAttNum(pstate->p_target_relation, name);
foreach(nxt, lnext(tl))
if (!strcmp(name, ((Ident *) lfirst(nxt))->name))
elog(ERROR, "Attribute '%s' should be specified only once", name);
}
}
return cols;
}
/*
* expandAllTables -
* turns '*' (in the target list) into a list of attributes
* (of all relations in the range table)
*/
static List *
expandAllTables(ParseState *pstate)
{
List *target = NIL;
List *legit_rtable = NIL;
List *rt,
*rtable;
rtable = pstate->p_rtable;
if (pstate->p_is_rule)
{
/*
* skip first two entries, "*new*" and "*current*"
*/
rtable = lnext(lnext(pstate->p_rtable));
}
/* this should not happen */
if (rtable == NULL)
elog(ERROR, "cannot expand: null p_rtable");
/*
* go through the range table and make a list of range table entries
* which we will expand.
*/
foreach(rt, rtable)
{
RangeTblEntry *rte = lfirst(rt);
/*
* we only expand those specify in the from clause. (This will
* also prevent us from using the wrong table in inserts: eg.
* tenk2 in "insert into tenk2 select * from tenk1;")
*/
if (!rte->inFromCl)
continue;
legit_rtable = lappend(legit_rtable, rte);
}
foreach(rt, legit_rtable)
{
RangeTblEntry *rte = lfirst(rt);
List *temp = target;
if (temp == NIL)
target = expandAll(pstate, rte->relname, rte->refname,
&pstate->p_last_resno);
else
{
while (temp != NIL && lnext(temp) != NIL)
temp = lnext(temp);
lnext(temp) = expandAll(pstate, rte->relname, rte->refname,
&pstate->p_last_resno);
}
}
return target;
}
/*
* figureColname -
* if the name of the resulting column is not specified in the target
* list, we have to guess.
*
*/
static char *
figureColname(Node *expr, Node *resval)
{
switch (nodeTag(expr))
{
case T_Aggreg:
return (char *) ((Aggreg *) expr)->aggname;
case T_Expr:
if (((Expr *) expr)->opType == FUNC_EXPR)
{
if (nodeTag(resval) == T_FuncCall)
return ((FuncCall *) resval)->funcname;
}
break;
default:
break;
}
return "?column?";
}
|