~ubuntu-branches/ubuntu/hardy/postgresql-8.4/hardy-backports

« back to all changes in this revision

Viewing changes to src/backend/parser/parse_node.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-03-20 12:00:13 UTC
  • Revision ID: james.westby@ubuntu.com-20090320120013-hogj7egc5mjncc5g
Tags: upstream-8.4~0cvs20090328
ImportĀ upstreamĀ versionĀ 8.4~0cvs20090328

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-------------------------------------------------------------------------
 
2
 *
 
3
 * parse_node.c
 
4
 *        various routines that make nodes for querytrees
 
5
 *
 
6
 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
 
7
 * Portions Copyright (c) 1994, Regents of the University of California
 
8
 *
 
9
 *
 
10
 * IDENTIFICATION
 
11
 *        $PostgreSQL$
 
12
 *
 
13
 *-------------------------------------------------------------------------
 
14
 */
 
15
#include "postgres.h"
 
16
 
 
17
#include "access/heapam.h"
 
18
#include "catalog/pg_type.h"
 
19
#include "mb/pg_wchar.h"
 
20
#include "nodes/makefuncs.h"
 
21
#include "nodes/nodeFuncs.h"
 
22
#include "parser/parsetree.h"
 
23
#include "parser/parse_coerce.h"
 
24
#include "parser/parse_expr.h"
 
25
#include "parser/parse_relation.h"
 
26
#include "utils/builtins.h"
 
27
#include "utils/int8.h"
 
28
#include "utils/syscache.h"
 
29
#include "utils/varbit.h"
 
30
 
 
31
 
 
32
static void pcb_error_callback(void *arg);
 
33
 
 
34
 
 
35
/*
 
36
 * make_parsestate
 
37
 *              Allocate and initialize a new ParseState.
 
38
 *
 
39
 * Caller should eventually release the ParseState via free_parsestate().
 
40
 */
 
41
ParseState *
 
42
make_parsestate(ParseState *parentParseState)
 
43
{
 
44
        ParseState *pstate;
 
45
 
 
46
        pstate = palloc0(sizeof(ParseState));
 
47
 
 
48
        pstate->parentParseState = parentParseState;
 
49
 
 
50
        /* Fill in fields that don't start at null/false/zero */
 
51
        pstate->p_next_resno = 1;
 
52
 
 
53
        if (parentParseState)
 
54
        {
 
55
                pstate->p_sourcetext = parentParseState->p_sourcetext;
 
56
                pstate->p_variableparams = parentParseState->p_variableparams;
 
57
        }
 
58
 
 
59
        return pstate;
 
60
}
 
61
 
 
62
/*
 
63
 * free_parsestate
 
64
 *              Release a ParseState and any subsidiary resources.
 
65
 */
 
66
void
 
67
free_parsestate(ParseState *pstate)
 
68
{
 
69
        /*
 
70
         * Check that we did not produce too many resnos; at the very least we
 
71
         * cannot allow more than 2^16, since that would exceed the range of a
 
72
         * AttrNumber. It seems safest to use MaxTupleAttributeNumber.
 
73
         */
 
74
        if (pstate->p_next_resno - 1 > MaxTupleAttributeNumber)
 
75
                ereport(ERROR,
 
76
                                (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
 
77
                                 errmsg("target lists can have at most %d entries",
 
78
                                                MaxTupleAttributeNumber)));
 
79
 
 
80
        if (pstate->p_target_relation != NULL)
 
81
                heap_close(pstate->p_target_relation, NoLock);
 
82
 
 
83
        pfree(pstate);
 
84
}
 
85
 
 
86
 
 
87
/*
 
88
 * parser_errposition
 
89
 *              Report a parse-analysis-time cursor position, if possible.
 
90
 *
 
91
 * This is expected to be used within an ereport() call.  The return value
 
92
 * is a dummy (always 0, in fact).
 
93
 *
 
94
 * The locations stored in raw parsetrees are byte offsets into the source
 
95
 * string.      We have to convert them to 1-based character indexes for reporting
 
96
 * to clients.  (We do things this way to avoid unnecessary overhead in the
 
97
 * normal non-error case: computing character indexes would be much more
 
98
 * expensive than storing token offsets.)
 
99
 */
 
100
int
 
101
parser_errposition(ParseState *pstate, int location)
 
102
{
 
103
        int                     pos;
 
104
 
 
105
        /* No-op if location was not provided */
 
106
        if (location < 0)
 
107
                return 0;
 
108
        /* Can't do anything if source text is not available */
 
109
        if (pstate == NULL || pstate->p_sourcetext == NULL)
 
110
                return 0;
 
111
        /* Convert offset to character number */
 
112
        pos = pg_mbstrlen_with_len(pstate->p_sourcetext, location) + 1;
 
113
        /* And pass it to the ereport mechanism */
 
114
        return errposition(pos);
 
115
}
 
116
 
 
117
 
 
118
/*
 
119
 * setup_parser_errposition_callback
 
120
 *              Arrange for non-parser errors to report an error position
 
121
 *
 
122
 * Sometimes the parser calls functions that aren't part of the parser
 
123
 * subsystem and can't reasonably be passed a ParseState; yet we would
 
124
 * like any errors thrown in those functions to be tagged with a parse
 
125
 * error location.  Use this function to set up an error context stack
 
126
 * entry that will accomplish that.  Usage pattern:
 
127
 *
 
128
 *              declare a local variable "ParseCallbackState pcbstate"
 
129
 *              ...
 
130
 *              setup_parser_errposition_callback(&pcbstate, pstate, location);
 
131
 *              call function that might throw error;
 
132
 *              cancel_parser_errposition_callback(&pcbstate);
 
133
 */
 
134
void
 
135
setup_parser_errposition_callback(ParseCallbackState *pcbstate,
 
136
                                                                  ParseState *pstate, int location)
 
137
{
 
138
        /* Setup error traceback support for ereport() */
 
139
        pcbstate->pstate = pstate;
 
140
        pcbstate->location = location;
 
141
        pcbstate->errcontext.callback = pcb_error_callback;
 
142
        pcbstate->errcontext.arg = (void *) pcbstate;
 
143
        pcbstate->errcontext.previous = error_context_stack;
 
144
        error_context_stack = &pcbstate->errcontext;
 
145
}
 
146
 
 
147
/*
 
148
 * Cancel a previously-set-up errposition callback.
 
149
 */
 
150
void
 
151
cancel_parser_errposition_callback(ParseCallbackState *pcbstate)
 
152
{
 
153
        /* Pop the error context stack */
 
154
        error_context_stack = pcbstate->errcontext.previous;
 
155
}
 
156
 
 
157
/*
 
158
 * Error context callback for inserting parser error location.
 
159
 *
 
160
 * Note that this will be called for *any* error occurring while the
 
161
 * callback is installed.  We avoid inserting an irrelevant error location
 
162
 * if the error is a query cancel --- are there any other important cases?
 
163
 */
 
164
static void
 
165
pcb_error_callback(void *arg)
 
166
{
 
167
        ParseCallbackState *pcbstate = (ParseCallbackState *) arg;
 
168
 
 
169
        if (geterrcode() != ERRCODE_QUERY_CANCELED)
 
170
                (void) parser_errposition(pcbstate->pstate, pcbstate->location);
 
171
}
 
172
 
 
173
 
 
174
/*
 
175
 * make_var
 
176
 *              Build a Var node for an attribute identified by RTE and attrno
 
177
 */
 
178
Var *
 
179
make_var(ParseState *pstate, RangeTblEntry *rte, int attrno, int location)
 
180
{
 
181
        Var                *result;
 
182
        int                     vnum,
 
183
                                sublevels_up;
 
184
        Oid                     vartypeid;
 
185
        int32           type_mod;
 
186
 
 
187
        vnum = RTERangeTablePosn(pstate, rte, &sublevels_up);
 
188
        get_rte_attribute_type(rte, attrno, &vartypeid, &type_mod);
 
189
        result = makeVar(vnum, attrno, vartypeid, type_mod, sublevels_up);
 
190
        result->location = location;
 
191
        return result;
 
192
}
 
193
 
 
194
/*
 
195
 * transformArrayType()
 
196
 *              Get the element type of an array type in preparation for subscripting
 
197
 */
 
198
Oid
 
199
transformArrayType(Oid arrayType)
 
200
{
 
201
        Oid                     elementType;
 
202
        HeapTuple       type_tuple_array;
 
203
        Form_pg_type type_struct_array;
 
204
 
 
205
        /* Get the type tuple for the array */
 
206
        type_tuple_array = SearchSysCache(TYPEOID,
 
207
                                                                          ObjectIdGetDatum(arrayType),
 
208
                                                                          0, 0, 0);
 
209
        if (!HeapTupleIsValid(type_tuple_array))
 
210
                elog(ERROR, "cache lookup failed for type %u", arrayType);
 
211
        type_struct_array = (Form_pg_type) GETSTRUCT(type_tuple_array);
 
212
 
 
213
        /* needn't check typisdefined since this will fail anyway */
 
214
 
 
215
        elementType = type_struct_array->typelem;
 
216
        if (elementType == InvalidOid)
 
217
                ereport(ERROR,
 
218
                                (errcode(ERRCODE_DATATYPE_MISMATCH),
 
219
                                 errmsg("cannot subscript type %s because it is not an array",
 
220
                                                format_type_be(arrayType))));
 
221
 
 
222
        ReleaseSysCache(type_tuple_array);
 
223
 
 
224
        return elementType;
 
225
}
 
226
 
 
227
/*
 
228
 * transformArraySubscripts()
 
229
 *              Transform array subscripting.  This is used for both
 
230
 *              array fetch and array assignment.
 
231
 *
 
232
 * In an array fetch, we are given a source array value and we produce an
 
233
 * expression that represents the result of extracting a single array element
 
234
 * or an array slice.
 
235
 *
 
236
 * In an array assignment, we are given a destination array value plus a
 
237
 * source value that is to be assigned to a single element or a slice of
 
238
 * that array.  We produce an expression that represents the new array value
 
239
 * with the source data inserted into the right part of the array.
 
240
 *
 
241
 * pstate               Parse state
 
242
 * arrayBase    Already-transformed expression for the array as a whole
 
243
 * arrayType    OID of array's datatype (should match type of arrayBase)
 
244
 * elementType  OID of array's element type (fetch with transformArrayType,
 
245
 *                              or pass InvalidOid to do it here)
 
246
 * elementTypMod typmod to be applied to array elements (if storing) or of
 
247
 *                              the source array (if fetching)
 
248
 * indirection  Untransformed list of subscripts (must not be NIL)
 
249
 * assignFrom   NULL for array fetch, else transformed expression for source.
 
250
 */
 
251
ArrayRef *
 
252
transformArraySubscripts(ParseState *pstate,
 
253
                                                 Node *arrayBase,
 
254
                                                 Oid arrayType,
 
255
                                                 Oid elementType,
 
256
                                                 int32 elementTypMod,
 
257
                                                 List *indirection,
 
258
                                                 Node *assignFrom)
 
259
{
 
260
        bool            isSlice = false;
 
261
        List       *upperIndexpr = NIL;
 
262
        List       *lowerIndexpr = NIL;
 
263
        ListCell   *idx;
 
264
        ArrayRef   *aref;
 
265
 
 
266
        /* Caller may or may not have bothered to determine elementType */
 
267
        if (!OidIsValid(elementType))
 
268
                elementType = transformArrayType(arrayType);
 
269
 
 
270
        /*
 
271
         * A list containing only single subscripts refers to a single array
 
272
         * element.  If any of the items are double subscripts (lower:upper), then
 
273
         * the subscript expression means an array slice operation. In this case,
 
274
         * we supply a default lower bound of 1 for any items that contain only a
 
275
         * single subscript.  We have to prescan the indirection list to see if
 
276
         * there are any double subscripts.
 
277
         */
 
278
        foreach(idx, indirection)
 
279
        {
 
280
                A_Indices  *ai = (A_Indices *) lfirst(idx);
 
281
 
 
282
                if (ai->lidx != NULL)
 
283
                {
 
284
                        isSlice = true;
 
285
                        break;
 
286
                }
 
287
        }
 
288
 
 
289
        /*
 
290
         * Transform the subscript expressions.
 
291
         */
 
292
        foreach(idx, indirection)
 
293
        {
 
294
                A_Indices  *ai = (A_Indices *) lfirst(idx);
 
295
                Node       *subexpr;
 
296
 
 
297
                Assert(IsA(ai, A_Indices));
 
298
                if (isSlice)
 
299
                {
 
300
                        if (ai->lidx)
 
301
                        {
 
302
                                subexpr = transformExpr(pstate, ai->lidx);
 
303
                                /* If it's not int4 already, try to coerce */
 
304
                                subexpr = coerce_to_target_type(pstate,
 
305
                                                                                                subexpr, exprType(subexpr),
 
306
                                                                                                INT4OID, -1,
 
307
                                                                                                COERCION_ASSIGNMENT,
 
308
                                                                                                COERCE_IMPLICIT_CAST,
 
309
                                                                                                -1);
 
310
                                if (subexpr == NULL)
 
311
                                        ereport(ERROR,
 
312
                                                        (errcode(ERRCODE_DATATYPE_MISMATCH),
 
313
                                                         errmsg("array subscript must have type integer"),
 
314
                                                         parser_errposition(pstate, exprLocation(ai->lidx))));
 
315
                        }
 
316
                        else
 
317
                        {
 
318
                                /* Make a constant 1 */
 
319
                                subexpr = (Node *) makeConst(INT4OID,
 
320
                                                                                         -1,
 
321
                                                                                         sizeof(int32),
 
322
                                                                                         Int32GetDatum(1),
 
323
                                                                                         false,
 
324
                                                                                         true);         /* pass by value */
 
325
                        }
 
326
                        lowerIndexpr = lappend(lowerIndexpr, subexpr);
 
327
                }
 
328
                subexpr = transformExpr(pstate, ai->uidx);
 
329
                /* If it's not int4 already, try to coerce */
 
330
                subexpr = coerce_to_target_type(pstate,
 
331
                                                                                subexpr, exprType(subexpr),
 
332
                                                                                INT4OID, -1,
 
333
                                                                                COERCION_ASSIGNMENT,
 
334
                                                                                COERCE_IMPLICIT_CAST,
 
335
                                                                                -1);
 
336
                if (subexpr == NULL)
 
337
                        ereport(ERROR,
 
338
                                        (errcode(ERRCODE_DATATYPE_MISMATCH),
 
339
                                         errmsg("array subscript must have type integer"),
 
340
                                         parser_errposition(pstate, exprLocation(ai->uidx))));
 
341
                upperIndexpr = lappend(upperIndexpr, subexpr);
 
342
        }
 
343
 
 
344
        /*
 
345
         * If doing an array store, coerce the source value to the right type.
 
346
         * (This should agree with the coercion done by transformAssignedExpr.)
 
347
         */
 
348
        if (assignFrom != NULL)
 
349
        {
 
350
                Oid                     typesource = exprType(assignFrom);
 
351
                Oid                     typeneeded = isSlice ? arrayType : elementType;
 
352
                Node       *newFrom;
 
353
 
 
354
                newFrom = coerce_to_target_type(pstate,
 
355
                                                                                assignFrom, typesource,
 
356
                                                                                typeneeded, elementTypMod,
 
357
                                                                                COERCION_ASSIGNMENT,
 
358
                                                                                COERCE_IMPLICIT_CAST,
 
359
                                                                                -1);
 
360
                if (newFrom == NULL)
 
361
                        ereport(ERROR,
 
362
                                        (errcode(ERRCODE_DATATYPE_MISMATCH),
 
363
                                         errmsg("array assignment requires type %s"
 
364
                                                        " but expression is of type %s",
 
365
                                                        format_type_be(typeneeded),
 
366
                                                        format_type_be(typesource)),
 
367
                           errhint("You will need to rewrite or cast the expression."),
 
368
                                         parser_errposition(pstate, exprLocation(assignFrom))));
 
369
                assignFrom = newFrom;
 
370
        }
 
371
 
 
372
        /*
 
373
         * Ready to build the ArrayRef node.
 
374
         */
 
375
        aref = makeNode(ArrayRef);
 
376
        aref->refarraytype = arrayType;
 
377
        aref->refelemtype = elementType;
 
378
        aref->reftypmod = elementTypMod;
 
379
        aref->refupperindexpr = upperIndexpr;
 
380
        aref->reflowerindexpr = lowerIndexpr;
 
381
        aref->refexpr = (Expr *) arrayBase;
 
382
        aref->refassgnexpr = (Expr *) assignFrom;
 
383
 
 
384
        return aref;
 
385
}
 
386
 
 
387
/*
 
388
 * make_const
 
389
 *
 
390
 *      Convert a Value node (as returned by the grammar) to a Const node
 
391
 *      of the "natural" type for the constant.  Note that this routine is
 
392
 *      only used when there is no explicit cast for the constant, so we
 
393
 *      have to guess what type is wanted.
 
394
 *
 
395
 *      For string literals we produce a constant of type UNKNOWN ---- whose
 
396
 *      representation is the same as cstring, but it indicates to later type
 
397
 *      resolution that we're not sure yet what type it should be considered.
 
398
 *      Explicit "NULL" constants are also typed as UNKNOWN.
 
399
 *
 
400
 *      For integers and floats we produce int4, int8, or numeric depending
 
401
 *      on the value of the number.  XXX We should produce int2 as well,
 
402
 *      but additional cleanup is needed before we can do that; there are
 
403
 *      too many examples that fail if we try.
 
404
 */
 
405
Const *
 
406
make_const(ParseState *pstate, Value *value, int location)
 
407
{
 
408
        Const      *con;
 
409
        Datum           val;
 
410
        int64           val64;
 
411
        Oid                     typeid;
 
412
        int                     typelen;
 
413
        bool            typebyval;
 
414
        ParseCallbackState pcbstate;
 
415
 
 
416
        switch (nodeTag(value))
 
417
        {
 
418
                case T_Integer:
 
419
                        val = Int32GetDatum(intVal(value));
 
420
 
 
421
                        typeid = INT4OID;
 
422
                        typelen = sizeof(int32);
 
423
                        typebyval = true;
 
424
                        break;
 
425
 
 
426
                case T_Float:
 
427
                        /* could be an oversize integer as well as a float ... */
 
428
                        if (scanint8(strVal(value), true, &val64))
 
429
                        {
 
430
                                /*
 
431
                                 * It might actually fit in int32. Probably only INT_MIN can
 
432
                                 * occur, but we'll code the test generally just to be sure.
 
433
                                 */
 
434
                                int32           val32 = (int32) val64;
 
435
 
 
436
                                if (val64 == (int64) val32)
 
437
                                {
 
438
                                        val = Int32GetDatum(val32);
 
439
 
 
440
                                        typeid = INT4OID;
 
441
                                        typelen = sizeof(int32);
 
442
                                        typebyval = true;
 
443
                                }
 
444
                                else
 
445
                                {
 
446
                                        val = Int64GetDatum(val64);
 
447
 
 
448
                                        typeid = INT8OID;
 
449
                                        typelen = sizeof(int64);
 
450
                                        typebyval = FLOAT8PASSBYVAL;    /* int8 and float8 alike */
 
451
                                }
 
452
                        }
 
453
                        else
 
454
                        {
 
455
                                /* arrange to report location if numeric_in() fails */
 
456
                                setup_parser_errposition_callback(&pcbstate, pstate, location);
 
457
                                val = DirectFunctionCall3(numeric_in,
 
458
                                                                                  CStringGetDatum(strVal(value)),
 
459
                                                                                  ObjectIdGetDatum(InvalidOid),
 
460
                                                                                  Int32GetDatum(-1));
 
461
                                cancel_parser_errposition_callback(&pcbstate);
 
462
 
 
463
                                typeid = NUMERICOID;
 
464
                                typelen = -1;   /* variable len */
 
465
                                typebyval = false;
 
466
                        }
 
467
                        break;
 
468
 
 
469
                case T_String:
 
470
 
 
471
                        /*
 
472
                         * We assume here that UNKNOWN's internal representation is the
 
473
                         * same as CSTRING
 
474
                         */
 
475
                        val = CStringGetDatum(strVal(value));
 
476
 
 
477
                        typeid = UNKNOWNOID;    /* will be coerced later */
 
478
                        typelen = -2;           /* cstring-style varwidth type */
 
479
                        typebyval = false;
 
480
                        break;
 
481
 
 
482
                case T_BitString:
 
483
                        /* arrange to report location if bit_in() fails */
 
484
                        setup_parser_errposition_callback(&pcbstate, pstate, location);
 
485
                        val = DirectFunctionCall3(bit_in,
 
486
                                                                          CStringGetDatum(strVal(value)),
 
487
                                                                          ObjectIdGetDatum(InvalidOid),
 
488
                                                                          Int32GetDatum(-1));
 
489
                        cancel_parser_errposition_callback(&pcbstate);
 
490
                        typeid = BITOID;
 
491
                        typelen = -1;
 
492
                        typebyval = false;
 
493
                        break;
 
494
 
 
495
                case T_Null:
 
496
                        /* return a null const */
 
497
                        con = makeConst(UNKNOWNOID,
 
498
                                                        -1,
 
499
                                                        -2,
 
500
                                                        (Datum) 0,
 
501
                                                        true,
 
502
                                                        false);
 
503
                        con->location = location;
 
504
                        return con;
 
505
 
 
506
                default:
 
507
                        elog(ERROR, "unrecognized node type: %d", (int) nodeTag(value));
 
508
                        return NULL;            /* keep compiler quiet */
 
509
        }
 
510
 
 
511
        con = makeConst(typeid,
 
512
                                        -1,                     /* typmod -1 is OK for all cases */
 
513
                                        typelen,
 
514
                                        val,
 
515
                                        false,
 
516
                                        typebyval);
 
517
        con->location = location;
 
518
 
 
519
        return con;
 
520
}