aboutsummaryrefslogtreecommitdiff
path: root/src/backend/optimizer/prep/preptlist.c
blob: fe1c2c92ba26ac64d32b6a0ae8ad17f34d942498 (plain)
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
/*-------------------------------------------------------------------------
 *
 * preptlist.c--
 *    Routines to preprocess the parse tree target list
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *    $Header: /cvsroot/pgsql/src/backend/optimizer/prep/preptlist.c,v 1.1.1.1 1996/07/09 06:21:38 scrappy Exp $
 *
 *-------------------------------------------------------------------------
 */
#include <string.h>
#include "postgres.h"

#include "nodes/pg_list.h"
#include "nodes/relation.h"
#include "nodes/primnodes.h"
#include "nodes/parsenodes.h"

#include "nodes/makefuncs.h"

#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/palloc.h"

#include "parser/parsetree.h"		/* for getrelid() */
#include "parser/catalog_utils.h"

#include "optimizer/internal.h"
#include "optimizer/prep.h"
#include "optimizer/clauses.h"
#include "optimizer/tlist.h"

static List *expand_targetlist(List *tlist, Oid relid, int command_type,
			       Index result_relation);
static List *replace_matching_resname(List *new_tlist,
				      List *old_tlist);
static List *new_relation_targetlist(Oid relid, Index rt_index,
				     NodeTag node_type);
					 

/*    
 * preprocess-targetlist--
 *    Driver for preprocessing the parse tree targetlist.
 *    
 *    1. Deal with appends and replaces by filling missing attributes
 *       in the target list.
 *    2. Reset operator OIDs to the appropriate regproc ids.
 *    
 *    Returns the new targetlist.
 */
List *
preprocess_targetlist(List *tlist,
		      int command_type,
		      Index result_relation,
		      List *range_table)
{
    List *expanded_tlist = NIL;
    Oid relid = InvalidOid;
    List *t_list = NIL;
    List *temp = NIL;

    if (result_relation>=1 && command_type != CMD_SELECT) {
	relid = getrelid(result_relation, range_table);
    }
     
    /*
     * for heap_formtuple to work, the targetlist must match the exact
     * order of the attributes. We also need to fill in the missing
     * attributes here. 			-ay 10/94
     */
    expanded_tlist =
	expand_targetlist(tlist, relid, command_type, result_relation);

    /*    XXX should the fix-opids be this early?? */
    /*    was mapCAR  */
    foreach (temp,expanded_tlist) {
	TargetEntry *tle = lfirst(temp);
	if (tle->expr)
	    fix_opid(tle->expr);
    }
    t_list = copyObject(expanded_tlist);

    /* ------------------
     *  for "replace" or "delete" queries, add ctid of the result
     *  relation into the target list so that the ctid can get
     *  propogate through the execution and in the end ExecReplace()
     *  will find the right tuple to replace or delete.  This
     *  extra field will be removed in ExecReplace().
     *  For convinient, we append this extra field to the end of
     *  the target list.
     * ------------------
     */
    if (command_type == CMD_UPDATE || command_type == CMD_DELETE) {
	TargetEntry *ctid;
	Resdom *resdom;
	Var *var;

	resdom = makeResdom(length(t_list) + 1,
			    27,
			    6,
			    "ctid",
			    0,
			    0,
			    1);

	var = makeVar(result_relation, -1, 27, result_relation, -1);

	ctid = makeNode(TargetEntry);
	ctid->resdom = resdom;
	ctid->expr = (Node *)var;
	t_list = lappend(t_list, ctid);
    }

    return(t_list);
}

/*****************************************************************************
 *
 *     	TARGETLIST EXPANSION
 *
 *****************************************************************************/

/*    
 * expand-targetlist--
 *    Given a target list as generated by the parser and a result relation,
 *    add targetlist entries for the attributes which have not been used.
 *    
 *    XXX This code is only supposed to work with unnested relations.
 *    
 *    'tlist' is the original target list
 *    'relid' is the relid of the result relation
 *    'command' is the update command
 *    
 * Returns the expanded target list, sorted in resno order.
 */
static List *
expand_targetlist(List *tlist,
		  Oid relid,
		  int command_type,
		  Index result_relation)
{
    NodeTag node_type = T_Invalid;
    
    switch (command_type) {
    case CMD_INSERT:
	node_type = (NodeTag)T_Const;
	break;
    case CMD_UPDATE:
	node_type = (NodeTag)T_Var;
	break;
    } 
    
    if(node_type != T_Invalid) {
	List *ntlist = new_relation_targetlist(relid,
					       result_relation,
					       node_type);

	return (replace_matching_resname(ntlist, tlist));
    } else {
	return (tlist);
    }
    
}


static List *
replace_matching_resname(List *new_tlist, List *old_tlist)
{
    List *temp, *i;
    List *t_list = NIL;
     
    foreach (i,new_tlist) {
	TargetEntry *new_tle = (TargetEntry *)lfirst(i);
	TargetEntry *matching_old_tl = NULL;

	foreach (temp, old_tlist) {
	    TargetEntry *old_tle = (TargetEntry *)lfirst(temp);
	    
	    old_tle = lfirst(temp);
	    if (!strcmp(old_tle->resdom->resname,
			new_tle->resdom->resname)) {
		matching_old_tl = old_tle;
		break;
	    }
	}
	  
	if(matching_old_tl) {
	    matching_old_tl->resdom->resno =
		new_tle->resdom->resno;
	    t_list = lappend(t_list, matching_old_tl);
	} 
	else {
	    t_list = lappend(t_list, new_tle);
	} 
    }

    /*
     * It is possible that 'old_tlist' has some negative
     * attributes (i.e. negative resnos). This only happens
     * if this is a replace/append command and we explicitly
     * specify a system attribute. Of course this is not a very good
     * idea if this is a user query, but on the other hand the rule
     * manager uses this mechanism to replace rule locks.
     *
     * So, copy all these entries to the end of the target list
     * and set their 'resjunk' value to 1 to show that these are
     * special attributes and have to be treated specially by the
     * executor!
     */
    foreach (temp, old_tlist) {
	TargetEntry *old_tle, *new_tl;
	Resdom *newresno;

	old_tle = lfirst(temp);
	if (old_tle->resdom->resno < 0) {
	    newresno = (Resdom*) copyObject((Node*)old_tle->resdom);
	    newresno->resno = length(t_list) +1;
	    newresno->resjunk = 1;
	    new_tl = MakeTLE(newresno, old_tle->expr);
	    t_list = lappend(t_list, new_tl);
	}
    }

    return (t_list);
}

/*    
 * new-relation-targetlist--
 *    Generate a targetlist for the relation with relation OID 'relid'
 *    and rangetable index 'rt-index'.
 *    
 *    Returns the new targetlist.
 */
static List *
new_relation_targetlist(Oid relid, Index rt_index, NodeTag node_type)
{
    AttrNumber attno;
    List *t_list = NIL;
    char *attname;
    Oid atttype = 0;
    int16 typlen = 0;
    bool attisset = false;
/*    Oid type_id; */
/*    type_id = RelationIdGetTypeId(relid); */

    for(attno=1; attno <= get_relnatts(relid); attno++) {
	attname = get_attname(/*type_id,*/ relid, attno);
	atttype = get_atttype(/*type_id,*/ relid, attno);
	/*
	 * Since this is an append or replace, the size of any set
	 * attribute is the size of the OID used to represent it.
	 */
	attisset = get_attisset(/* type_id,*/ relid, attname);
	if (attisset) {
	    typlen = tlen(type("oid"));
	} else {
	    typlen = get_typlen(atttype);
	}
	
	switch (node_type) {
	case T_Const:
	    { 
		struct varlena *typedefault = get_typdefault(atttype);
		int temp = 0;
		Const *temp2 = (Const*)NULL;
		TargetEntry *temp3 = (TargetEntry *)NULL;

		if (typedefault==NULL) 
		    temp = 0;
		else 
		    temp = typlen;
		 
		temp2 = makeConst (atttype,
				   temp,
				   (Datum)typedefault,
				   (typedefault == (struct varlena *)NULL),
				   /* XXX this is bullshit */
				   false,
				   false /* not a set */);
		 
		temp3 = MakeTLE (makeResdom(attno,
					    atttype,
					    typlen,
					    attname,
					    0,
					    (Oid)0,
					    0),
				 (Node*)temp2);
		t_list = lappend(t_list,temp3);
		break;
	    } 
	case T_Var:
	    { 
		Var *temp_var = (Var*)NULL;
		TargetEntry *temp_list = NULL;

		temp_var =
		    makeVar(rt_index, attno, atttype, rt_index, attno);

		temp_list = MakeTLE(makeResdom(attno,
					       atttype,
					       typlen,
					       attname,
					       0,
					       (Oid)0,
					       0),
				    (Node*)temp_var);
		t_list = lappend(t_list,temp_list);
		break;
	    }
	default: 		/* do nothing */
	    break;
	}
    }

    return(t_list);
}