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
|
/*-------------------------------------------------------------------------
*
* ylib.c--
*
* Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/parser/parser.c,v 1.1.1.1 1996/07/09 06:21:40 scrappy Exp $
*
*-------------------------------------------------------------------------
*/
#include <string.h>
#include <stdio.h>
#ifndef WIN32
#include <pwd.h>
#endif /*WIN32 */
#include <sys/param.h> /* for MAXPATHLEN */
#include "utils/elog.h"
#include "parser/catalog_utils.h"
#include "nodes/pg_list.h"
#include "utils/exc.h"
#include "utils/excid.h"
#include "utils/palloc.h"
#include "catalog/pg_aggregate.h"
#include "catalog/pg_type.h"
#include "nodes/primnodes.h"
#include "nodes/plannodes.h"
#include "nodes/execnodes.h"
#include "nodes/relation.h"
#include "parser/parse_query.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "access/heapam.h"
#include "nodes/makefuncs.h"
#include "optimizer/clauses.h"
char *parseString; /* the char* which holds the string to be parsed */
char *parseCh; /* a pointer used during parsing to walk down ParseString*/
List *parsetree = NIL;
static void fixupsets();
static void define_sets();
/*
* parser-- returns a list of parse trees
*
* CALLER is responsible for free'ing the list returned
*/
QueryTreeList *
parser(char *str, Oid *typev, int nargs)
{
QueryTreeList* queryList;
int yyresult;
#if defined(FLEX_SCANNER)
extern void DeleteBuffer(void);
#endif /* FLEX_SCANNER */
init_io();
/* Set things up to read from the string, if there is one */
if (strlen(str) != 0) {
parseString = (char *) palloc(strlen(str) + 1);
memmove(parseString,str,strlen(str)+1);
}
parser_init(typev, nargs);
yyresult = yyparse();
#if defined(FLEX_SCANNER)
DeleteBuffer();
#endif /* FLEX_SCANNER */
clearerr(stdin);
if (yyresult) { /* error */
return((QueryTreeList*)NULL);
}
queryList = parse_analyze(parsetree);
#ifdef SETS_FIXED
/* Fixing up sets calls the parser, so it reassigns the global
* variable parsetree. So save the real parsetree.
*/
savetree = parsetree;
foreach (parse, savetree) { /* savetree is really a list of parses */
/* find set definitions embedded in query */
fixupsets((Query *)lfirst(parse));
}
return savetree;
#endif
return queryList;
}
static void
fixupsets(Query *parse)
{
if (parse == NULL)
return;
if (parse->commandType==CMD_UTILITY) /* utility */
return;
if (parse->commandType!=CMD_INSERT)
return;
define_sets(parse);
}
/* Recursively find all of the Consts in the parsetree. Some of
* these may represent a set. The value of the Const will be the
* query (a string) which defines the set. Call SetDefine to define
* the set, and store the OID of the new set in the Const instead.
*/
static void
define_sets(Node *clause)
{
#ifdef SETS_FIXED
Oid setoid;
Type t = type("oid");
Oid typeoid = typeid(t);
Size oidsize = tlen(t);
bool oidbyval = tbyval(t);
if (clause==NULL) {
return;
} else if (IsA(clause,LispList)) {
define_sets(lfirst(clause));
define_sets(lnext(clause));
} else if (IsA(clause,Const)) {
if (get_constisnull((Const)clause) ||
!get_constisset((Const)clause)) {
return;
}
setoid = SetDefine(((Const*)clause)->constvalue,
get_id_typname(((Const*)clause)->consttype));
set_constvalue((Const)clause, setoid);
set_consttype((Const)clause,typeoid);
set_constlen((Const)clause,oidsize);
set_constbyval((Const)clause,oidbyval);
} else if ( IsA(clause,Iter) ) {
define_sets(((Iter*)clause)->iterexpr);
} else if (single_node (clause)) {
return;
} else if (or_clause(clause)) {
List *temp;
/* mapcan */
foreach (temp, ((Expr*)clause)->args) {
define_sets(lfirst(temp));
}
} else if (is_funcclause (clause)) {
List *temp;
/* mapcan */
foreach(temp, ((Expr*)clause)->args) {
define_sets(lfirst(temp));
}
} else if (IsA(clause,ArrayRef)) {
define_sets(((ArrayRef*)clause)->refassgnexpr);
} else if (not_clause (clause)) {
define_sets (get_notclausearg (clause));
} else if (is_opclause (clause)) {
define_sets(get_leftop (clause));
define_sets(get_rightop (clause));
}
#endif
}
#define PSIZE(PTR) (*((int32 *)(PTR) - 1))
Node *
parser_typecast(Value *expr, TypeName *typename, int typlen)
{
/* check for passing non-ints */
Const *adt;
Datum lcp;
Type tp;
char type_string[16];
int32 len;
char *cp = NULL;
char *const_string;
bool string_palloced = false;
switch(nodeTag(expr)) {
case T_String:
const_string = DatumGetPointer(expr->val.str);
break;
case T_Integer:
const_string = (char *) palloc(256);
string_palloced = true;
sprintf(const_string, "%ld", expr->val.ival);
break;
default:
elog(WARN,
"parser_typecast: cannot cast this expression to type \"%s\"",
typename->name);
}
if (typename->arrayBounds != NIL) {
sprintf(type_string,"_%s", typename->name);
tp = (Type) type(type_string);
} else {
tp = (Type) type(typename->name);
}
len = tlen(tp);
#if 0 /* fix me */
switch ( CInteger(lfirst(expr)) ) {
case 23: /* int4 */
const_string = (char *) palloc(256);
string_palloced = true;
sprintf(const_string,"%d", ((Const*)lnext(expr))->constvalue);
break;
case 19: /* char16 */
const_string = (char *) palloc(256);
string_palloced = true;
sprintf(const_string,"%s", ((Const*)lnext(expr))->constvalue);
break;
case 18: /* char */
const_string = (char *) palloc(256);
string_palloced = true;
sprintf(const_string,"%c", ((Const)lnext(expr))->constvalue);
break;
case 701:/* float8 */
const_string = (char *) palloc(256);
string_palloced = true;
sprintf(const_string,"%f", ((Const)lnext(expr))->constvalue);
break;
case 25: /* text */
const_string = DatumGetPointer(((Const)lnext(expr))->constvalue);
const_string = (char *) textout((struct varlena *)const_string);
break;
case 705: /* unknown */
const_string = DatumGetPointer(((Const)lnext(expr))->constvalue);
const_string = (char *) textout((struct varlena *)const_string);
break;
default:
elog(WARN,"unknown type %d", CInteger(lfirst(expr)));
}
#endif
cp = instr2 (tp, const_string, typlen);
if (!tbyvalue(tp)) {
if (len >= 0 && len != PSIZE(cp)) {
char *pp;
pp = (char *) palloc(len);
memmove(pp, cp, len);
cp = pp;
}
lcp = PointerGetDatum(cp);
} else {
switch(len) {
case 1:
lcp = Int8GetDatum(cp);
break;
case 2:
lcp = Int16GetDatum(cp);
break;
case 4:
lcp = Int32GetDatum(cp);
break;
default:
lcp = PointerGetDatum(cp);
break;
}
}
adt = makeConst(typeid(tp),
len,
(Datum)lcp ,
0,
tbyvalue(tp),
0 /* not a set */);
if (string_palloced)
pfree(const_string);
return (Node*)adt;
}
Node *
parser_typecast2(Node *expr, int exprType, Type tp, int typlen)
{
/* check for passing non-ints */
Const *adt;
Datum lcp;
int32 len = tlen(tp);
char *cp = NULL;
char *const_string;
bool string_palloced = false;
Assert(IsA(expr,Const));
switch (exprType) {
case 23: /* int4 */
const_string = (char *) palloc(256);
string_palloced = true;
sprintf(const_string,"%d",
(int) ((Const*)expr)->constvalue);
break;
case 19: /* char16 */
const_string = (char *) palloc(256);
string_palloced = true;
sprintf(const_string,"%s",
(char*) ((Const*)expr)->constvalue);
break;
case 18: /* char */
const_string = (char *) palloc(256);
string_palloced = true;
sprintf(const_string,"%c",
(char) ((Const*)expr)->constvalue);
break;
case 700: /* float4 */
{
float32 floatVal =
DatumGetFloat32(((Const*)expr)->constvalue);
const_string = (char *) palloc(256);
string_palloced = true;
sprintf(const_string,"%f", *floatVal);
break;
}
case 701:/* float8 */
{
float64 floatVal =
DatumGetFloat64(((Const*)expr)->constvalue);
const_string = (char *) palloc(256);
string_palloced = true;
sprintf(const_string,"%f", *floatVal);
break;
}
case 25: /* text */
const_string =
DatumGetPointer(((Const*)expr)->constvalue );
const_string = (char *) textout((struct varlena *)const_string);
break;
case 705: /* unknown */
const_string =
DatumGetPointer(((Const*)expr)->constvalue );
const_string = (char *) textout((struct varlena *)const_string);
break;
default:
elog(WARN,"unknown type%d ",exprType);
}
cp = instr2 (tp, const_string, typlen);
if (!tbyvalue(tp)) {
if (len >= 0 && len != PSIZE(cp)) {
char *pp;
pp = (char *) palloc(len);
memmove(pp, cp, len);
cp = pp;
}
lcp = PointerGetDatum(cp);
} else {
switch(len) {
case 1:
lcp = Int8GetDatum(cp);
break;
case 2:
lcp = Int16GetDatum(cp);
break;
case 4:
lcp = Int32GetDatum(cp);
break;
default:
lcp = PointerGetDatum(cp);
break;
}
}
adt = makeConst((Oid)typeid(tp),
(Size)len,
(Datum)lcp,
0,
0 /*was omitted*/,
0 /* not a set */);
/*
printf("adt %s : %d %d %d\n",CString(expr),typeid(tp) ,
len,cp);
*/
if (string_palloced) pfree(const_string);
return ((Node*) adt);
}
Aggreg *
ParseAgg(char *aggname, Oid basetype, Node *target)
{
Oid fintype;
Oid vartype;
Oid xfn1;
Form_pg_aggregate aggform;
Aggreg *aggreg;
HeapTuple theAggTuple;
theAggTuple = SearchSysCacheTuple(AGGNAME, PointerGetDatum(aggname),
ObjectIdGetDatum(basetype),
0, 0);
if (!HeapTupleIsValid(theAggTuple)) {
elog(WARN, "aggregate %s does not exist", aggname);
}
aggform = (Form_pg_aggregate) GETSTRUCT(theAggTuple);
fintype = aggform->aggfinaltype;
xfn1 = aggform->aggtransfn1;
if (nodeTag(target) != T_Var)
elog(WARN, "parser: aggregate can only be applied on an attribute");
/* only aggregates with transfn1 need a base type */
if (OidIsValid(xfn1)) {
basetype = aggform->aggbasetype;
vartype = ((Var*)target)->vartype;
if (basetype != vartype) {
Type tp1, tp2, get_id_type();
tp1 = get_id_type(basetype);
tp2 = get_id_type(vartype);
elog(NOTICE, "Aggregate type mismatch:");
elog(WARN, "%s works on %s, not %s", aggname,
tname(tp1), tname(tp2));
}
}
aggreg = makeNode(Aggreg);
aggreg->aggname = pstrdup(aggname);
aggreg->basetype = aggform->aggbasetype;
aggreg->aggtype = fintype;
aggreg->target = target;
return aggreg;
}
|