aboutsummaryrefslogtreecommitdiff
path: root/src/backend/parser/parse_query.c
blob: 37c955017efa4ee0db9ac5326887eef365c75e21 (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
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
/*-------------------------------------------------------------------------
 *
 * parse_query.c--
 *    take an "optimizable" stmt and make the query tree that
 *     the planner requires.
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *    $Header: /cvsroot/pgsql/src/backend/parser/Attic/parse_query.c,v 1.1.1.1 1996/07/09 06:21:40 scrappy Exp $
 *
 *-------------------------------------------------------------------------
 */
#include <ctype.h>
#include <string.h>
#include "postgres.h"

#include "access/heapam.h"
#include "utils/tqual.h"
#include "access/tupmacs.h"
#include "utils/builtins.h"
#include "utils/elog.h"
#include "utils/palloc.h"
#include "utils/acl.h"          /* for ACL_NO_PRIV_WARNING */
#include "utils/rel.h" 		/* Relation stuff */

#include "utils/syscache.h"
#include "catalog/pg_type.h"
#include "catalog_utils.h"
#include "parser/parse_query.h"
/* #include "parser/io.h" */
#include "utils/lsyscache.h"

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

Oid *param_type_info;
int pfunc_num_args;

extern int Quiet;


/* given range variable, return id of variable; position starts with 1 */
int
RangeTablePosn(List *rtable, char *rangevar)
{
    int index;
    List *temp;
    
    index = 1;
/*    temp = pstate->p_rtable; */
    temp = rtable;
    while (temp != NIL) {
	RangeTblEntry *rt_entry = lfirst(temp);

	if (!strcmp(rt_entry->refname, rangevar))
	    return index;

	temp = lnext(temp);
	index++;
    }
    return(0);
}

char*
VarnoGetRelname(ParseState *pstate, int vnum)
{
    int i;
    List *temp = pstate->p_rtable;
    for( i = 1; i < vnum ; i++) 
	temp = lnext(temp);
    return(((RangeTblEntry*)lfirst(temp))->relname);
}


RangeTblEntry *
makeRangeTableEntry(char *relname,
		    bool inh,
		    TimeRange *timeRange,
		    char *refname)
{
    Relation relation;
    RangeTblEntry *ent = makeNode(RangeTblEntry);

    ent->relname = pstrdup(relname);
    ent->refname = refname;

    relation = heap_openr(ent->relname);
    if (relation == NULL) {
	elog(WARN,"%s: %s",
	     relname, ACL_NO_PRIV_WARNING);
    }

    /*
     * Flags - zero or more from archive,inheritance,union,version
     *  or recursive (transitive closure)
     * [we don't support them all -- ay 9/94 ]
     */
    ent->inh = inh;

    ent->timeRange = timeRange;
    
    /* RelOID */
    ent->relid = RelationGetRelationId(relation);

    /*
     * close the relation we're done with it for now.
     */
    heap_close(relation);
    return ent;
}

/*
 * expandAll -
 *    makes a list of attributes
 *    assumes reldesc caching works
 */
List *
expandAll(ParseState* pstate, char *relname, int *this_resno)
{
    Relation rdesc;
    List *tall = NIL;
    Var *varnode;
    int i, maxattrs, first_resno;
    int type_id, type_len, vnum;
    char *physical_relname;
    
    first_resno = *this_resno;
    
    /* printf("\nExpanding %.*s.all\n", NAMEDATALEN, relname); */
    vnum = RangeTablePosn(pstate->p_rtable, relname);
    if ( vnum == 0 ) {
	pstate->p_rtable = lappend(pstate->p_rtable,
				   makeRangeTableEntry(relname, FALSE, NULL,
						       relname));
	vnum = RangeTablePosn(pstate->p_rtable, relname);
    }
    
    physical_relname = VarnoGetRelname(pstate, vnum);
    
    rdesc = heap_openr(physical_relname);
    
    if (rdesc == NULL ) {
	elog(WARN,"Unable to expand all -- heap_openr failed on %s",
	     physical_relname);
	return NIL;
    }
    maxattrs = RelationGetNumberOfAttributes(rdesc);
    
    for ( i = maxattrs-1 ; i > -1 ; --i ) {
	char *attrname;
	TargetEntry *rte = makeNode(TargetEntry);
	
	attrname = pstrdup ((rdesc->rd_att->attrs[i]->attname).data);
	varnode = (Var*)make_var(pstate, relname, attrname, &type_id);
	type_len = (int)tlen(get_id_type(type_id));

	/* Even if the elements making up a set are complex, the
	 * set itself is not. */
	
	rte->resdom = makeResdom((AttrNumber) i + first_resno, 
				 (Oid)type_id,
				 (Size)type_len,
				 attrname,
				 (Index)0,
				 (Oid)0,
				 0);
	rte->expr = (Node *)varnode;
	tall = lcons(rte, tall);
    }
    
    /*
     * Close the reldesc - we're done with it now
     */
    heap_close(rdesc);
    *this_resno = first_resno + maxattrs;
    return(tall);
}

TimeQual
makeTimeRange(char *datestring1,
	      char *datestring2,
	      int timecode)	/* 0 = snapshot , 1 = timerange */
{
    TimeQual	qual;
    AbsoluteTime t1,t2;
    
    switch (timecode) {
    case 0:
	if (datestring1 == NULL) {
	    elog(WARN, "MakeTimeRange: bad snapshot arg");
	}
	t1 = nabstimein(datestring1);
	if (!AbsoluteTimeIsValid(t1)) {
	    elog(WARN, "bad snapshot time: \"%s\"",
		 datestring1);
	}
	qual = TimeFormSnapshotTimeQual(t1);
	break;
    case 1:
	if (datestring1 == NULL) {
	    t1 = NOSTART_ABSTIME;
	} else {
	    t1 = nabstimein(datestring1);
	    if (!AbsoluteTimeIsValid(t1)) {
		elog(WARN,
		     "bad range start time: \"%s\"",
		     datestring1);
	    }
	}
	if (datestring2 == NULL) {
	    t2 = NOEND_ABSTIME;
	} else {
	    t2 = nabstimein(datestring2);
	    if (!AbsoluteTimeIsValid(t2)) {
		elog(WARN,
		     "bad range end time: \"%s\"",
		     datestring2);
	    }
	}
	qual = TimeFormRangedTimeQual(t1,t2);
	break;
    default:
	elog(WARN, "MakeTimeRange: internal parser error");
    }
    return qual;
}

static void
disallow_setop(char *op, Type optype, Node *operand)
{
    if (operand==NULL)
	return;
    
    if (nodeTag(operand) == T_Iter) {
	elog(NOTICE, "An operand to the '%s' operator returns a set of %s,",
	     op, tname(optype));
	elog(WARN, "but '%s' takes single values, not sets.",
	     op);
    }
}

static Node *
make_operand(char *opname,
	     Node *tree,
	     int orig_typeId,
	     int true_typeId)
{
    Node *result;
    Type true_type;
    Datum val;
    Oid infunc;
    
    if (tree != NULL) {
	result = tree;
	true_type = get_id_type(true_typeId);
	disallow_setop(opname, true_type, result);
	if (true_typeId != orig_typeId) {	/* must coerce */
	    Const *con= (Const *)result;

	    Assert(nodeTag(result)==T_Const);
	    val = (Datum)textout((struct varlena *)
				 con->constvalue);
	    infunc = typeid_get_retinfunc(true_typeId);
	    con = makeNode(Const);
	    con->consttype = true_typeId;
	    con->constlen = tlen(true_type);
	    con->constvalue = (Datum)fmgr(infunc,
					  val,
					  get_typelem(true_typeId),
					  -1 /* for varchar() type */);
	    con->constisnull = false;
	    con->constbyval = true;
	    con->constisset = false;
	    result = (Node *)con;
	}
    }else {
	Const *con= makeNode(Const);

	con->consttype = true_typeId;
	con->constlen = 0;
	con->constvalue = (Datum)(struct varlena *)NULL;
	con->constisnull = true;
	con->constbyval = true;
	con->constisset = false;
	result = (Node *)con;
    }
    
    return result;
}


Expr *
make_op(char *opname, Node *ltree, Node *rtree)
{
    int ltypeId, rtypeId;
    Operator temp;
    OperatorTupleForm opform;
    Oper *newop;
    Node *left, *right;
    Expr *result;
    
    if (rtree == NULL) {

	/* right operator */
	ltypeId = (ltree==NULL) ? UNKNOWNOID : exprType(ltree);
	temp = right_oper(opname, ltypeId);
	opform = (OperatorTupleForm) GETSTRUCT(temp);
	left = make_operand(opname, ltree, ltypeId, opform->oprleft);
	right = NULL;

    }else if (ltree == NULL) {

	/* left operator */
	rtypeId = (rtree==NULL) ? UNKNOWNOID : exprType(rtree);
	temp = left_oper(opname, rtypeId);
	opform = (OperatorTupleForm) GETSTRUCT(temp);
	right = make_operand(opname, rtree, rtypeId, opform->oprright);
	left = NULL;

    }else {

	/* binary operator */
	ltypeId = (ltree==NULL) ? UNKNOWNOID : exprType(ltree);
	rtypeId = (rtree==NULL) ? UNKNOWNOID : exprType(rtree);
	temp = oper(opname, ltypeId, rtypeId);
	opform = (OperatorTupleForm) GETSTRUCT(temp);
	left = make_operand(opname, ltree, ltypeId, opform->oprleft);
	right = make_operand(opname, rtree, rtypeId, opform->oprright);
    }

    newop = makeOper(oprid(temp),   	/* opno */
		     InvalidOid,	/* opid */
		     opform->oprresult, /* operator result type */
		     0,
		     NULL);

    result = makeNode(Expr);
    result->typeOid = opform->oprresult;
    result->opType = OP_EXPR;
    result->oper = (Node *)newop;

    if (!left) {
	result->args = lcons(right, NIL);
    } else if (!right) {
	result->args = lcons(left, NIL);
    } else {
	result->args = lcons(left, lcons(right, NIL));
    }

    return result;
}

int
find_atttype(Oid relid, char *attrname)
{
    int attid, vartype;
    Relation rd;
    
    rd = heap_open(relid);
    if (!RelationIsValid(rd)) {
	rd = heap_openr(tname(get_id_type(relid)));
	if (!RelationIsValid(rd))
	    elog(WARN, "cannot compute type of att %s for relid %d",
		 attrname, relid);
    }
    
    attid =  nf_varattno(rd, attrname);
    
    if (attid == InvalidAttrNumber) 
        elog(WARN, "Invalid attribute %s\n", attrname);
    
    vartype = att_typeid(rd , attid);
    
    /*
     * close relation we're done with it now
     */
    heap_close(rd);
    
    return (vartype);
}


Var *
make_var(ParseState *pstate, char *relname, char *attrname, int *type_id)
{
    Var *varnode;
    int vnum, attid, vartypeid;
    Relation rd;
    
    vnum = RangeTablePosn(pstate->p_rtable, relname);
    
    if (vnum == 0) {
	pstate->p_rtable =
	    lappend(pstate->p_rtable,
		     makeRangeTableEntry(relname, FALSE,
					 NULL, relname));
	vnum = RangeTablePosn (pstate->p_rtable, relname);
	relname = VarnoGetRelname(pstate, vnum);
    } else {
	relname = VarnoGetRelname(pstate, vnum);
    }
    
    rd = heap_openr(relname);
/*    relid = RelationGetRelationId(rd); */
    attid =  nf_varattno(rd, (char *) attrname);
    if (attid == InvalidAttrNumber) 
	elog(WARN, "Invalid attribute %s\n", attrname);
    vartypeid = att_typeid(rd, attid);

    varnode = makeVar(vnum, attid, vartypeid, vnum, attid);

    /*
     * close relation we're done with it now
     */
    heap_close(rd);

    *type_id = vartypeid;
    return varnode;
}

/*
 *  make_array_ref() -- Make an array reference node.
 *
 *	Array references can hang off of arbitrary nested dot (or
 *	function invocation) expressions.  This routine takes a
 *	tree generated by ParseFunc() and an array index and
 *	generates a new array reference tree.  We do some simple
 *	typechecking to be sure the dereference is valid in the
 *	type system, but we don't do any bounds checking here.
 *
 *  indirection is a list of A_Indices
 */
ArrayRef *
make_array_ref(Node *expr,
	       List *indirection)
{
    Oid typearray;
    HeapTuple type_tuple;
    TypeTupleForm type_struct_array, type_struct_element;
    ArrayRef *aref;
    int reftype;
    List *upperIndexpr=NIL;
    List *lowerIndexpr=NIL;
    
    typearray = (Oid) exprType(expr);
    
    type_tuple = SearchSysCacheTuple(TYPOID, 
				     ObjectIdGetDatum(typearray), 
				     0,0,0);
    
    if (!HeapTupleIsValid(type_tuple))
	elog(WARN, "make_array_ref: Cache lookup failed for type %d\n",
	     typearray);
    
    /* get the array type struct from the type tuple */
    type_struct_array = (TypeTupleForm) GETSTRUCT(type_tuple);
    
    if (type_struct_array->typelem == InvalidOid) {
	elog(WARN, "make_array_ref: type %s is not an array",
	     (Name)&(type_struct_array->typname.data[0]));
    }
    
    /* get the type tuple for the element type */
    type_tuple = SearchSysCacheTuple(TYPOID, 
			     ObjectIdGetDatum(type_struct_array->typelem),
				     0,0,0);
    if (!HeapTupleIsValid(type_tuple))
	elog(WARN, "make_array_ref: Cache lookup failed for type %d\n",
	     typearray);
    
    type_struct_element = (TypeTupleForm) GETSTRUCT(type_tuple);

    while(indirection!=NIL) {
	A_Indices *ind = lfirst(indirection);
	if (ind->lidx) {
	    /* XXX assumes all lower indices non null in this case
	     */
	    lowerIndexpr = lappend(lowerIndexpr, ind->lidx);
	}
	upperIndexpr = lappend(upperIndexpr, ind->uidx);
	indirection = lnext(indirection);
    }
    aref = makeNode(ArrayRef);
    aref->refattrlength = type_struct_array->typlen;
    aref->refelemlength = type_struct_element->typlen;
    aref->refelemtype = type_struct_array->typelem;
    aref->refelembyval = type_struct_element->typbyval;
    aref->refupperindexpr = upperIndexpr;
    aref->reflowerindexpr = lowerIndexpr;
    aref->refexpr = expr;
    aref->refassgnexpr = NULL;

    if (lowerIndexpr == NIL) /* accessing a single array element */
	reftype = aref->refelemtype;
    else /* request to clip a part of the array, the result is another array */
	reftype = typearray;

    /* we change it to reflect the true type; since the original refelemtype
     * doesn't seem to get used anywhere. - ay 10/94
     */
    aref->refelemtype = reftype;	

    return aref;
}

ArrayRef *
make_array_set(Expr *target_expr,
	       List *upperIndexpr,
	       List *lowerIndexpr,
	       Expr *expr)
{
    Oid typearray;
    HeapTuple type_tuple;
    TypeTupleForm type_struct_array;
    TypeTupleForm type_struct_element;
    ArrayRef *aref;
    int reftype;
    
    typearray = exprType((Node*)target_expr);
    
    type_tuple = SearchSysCacheTuple(TYPOID, 
				     ObjectIdGetDatum(typearray), 
				     0,0,0);
    
    if (!HeapTupleIsValid(type_tuple))
	elog(WARN, "make_array_ref: Cache lookup failed for type %d\n",
	     typearray);
    
    /* get the array type struct from the type tuple */
    type_struct_array = (TypeTupleForm) GETSTRUCT(type_tuple);
    
    if (type_struct_array->typelem == InvalidOid) {
	elog(WARN, "make_array_ref: type %s is not an array",
	     (Name)&(type_struct_array->typname.data[0]));
    }
    /* get the type tuple for the element type */
    type_tuple = SearchSysCacheTuple(TYPOID, 
				     ObjectIdGetDatum(type_struct_array->typelem),
				     0,0,0);
    
    if (!HeapTupleIsValid(type_tuple))
	elog(WARN, "make_array_ref: Cache lookup failed for type %d\n",
	     typearray);
    
    type_struct_element = (TypeTupleForm) GETSTRUCT(type_tuple);
    
    aref = makeNode(ArrayRef);
    aref->refattrlength = type_struct_array->typlen;
    aref->refelemlength = type_struct_element->typlen;
    aref->refelemtype = type_struct_array->typelem;
    aref->refelembyval = type_struct_element->typbyval;
    aref->refupperindexpr = upperIndexpr;
    aref->reflowerindexpr = lowerIndexpr;
    aref->refexpr = (Node*)target_expr;
    aref->refassgnexpr = (Node*)expr;

    if (lowerIndexpr == NIL) /* accessing a single array element */
	reftype = aref->refelemtype;
    else /* request to set a part of the array, by another array */
	reftype = typearray;

    aref->refelemtype = reftype;
    
    return aref;
}

/*
 * 
 * make_const -
 * 
 * - takes a lispvalue, (as returned to the yacc routine by the lexer)
 *   extracts the type, and makes the appropriate type constant
 *   by invoking the (c-callable) lisp routine c-make-const
 *   via the lisp_call() mechanism
 *
 * eventually, produces a "const" lisp-struct as per nodedefs.cl
 */ 
Const *
make_const(Value *value)
{
    Type tp;
    Datum val;
    Const *con;
    
    switch(nodeTag(value)) {
    case T_Integer:
	tp = type("int4");
	val = Int32GetDatum(intVal(value));
	break;
	
    case T_Float:
	{
	    float32 dummy;
	    tp = type("float4");
	    
	    dummy = (float32)palloc(sizeof(float32data));
	    *dummy = floatVal(value);
	    
	    val = Float32GetDatum(dummy);
	}
	break;
	
    case T_String:
	tp = type("unknown"); /* unknown for now, will be type coerced */
	val = PointerGetDatum(textin(strVal(value)));
	break;

    case T_Null:
    default:
	{
	    if (nodeTag(value)!=T_Null)
		elog(NOTICE,"unknown type : %d\n", nodeTag(value));

	    /* null const */
	    con = makeConst(0, 0, (Datum)NULL, TRUE, 0, FALSE);
	    return NULL /*con*/;
	}
    }

    con = makeConst(typeid(tp),
		    tlen(tp),
		    val,
		    FALSE,
		    tbyval(tp),
		    FALSE); /* not a set */

    return (con);
}

/*
 * param_type_init()
 *
 * keep enough information around fill out the type of param nodes
 * used in postquel functions
 */
void
param_type_init(Oid* typev, int nargs)
{
    pfunc_num_args = nargs;
    param_type_info = typev;
}

Oid
param_type(int t)
{
    if ((t >pfunc_num_args) ||(t ==0)) return InvalidOid;
    return param_type_info[t-1];
}