readfuncs.c 50.6 KB
Newer Older
1 2 3
/*-------------------------------------------------------------------------
 *
 * readfuncs.c--
4
 *	  Reader functions for Postgres tree nodes.
5 6 7 8 9
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
B
Bruce Momjian 已提交
10
 *	  $Header: /cvsroot/pgsql/src/backend/nodes/readfuncs.c,v 1.38 1998/10/22 13:52:22 momjian Exp $
11 12
 *
 * NOTES
13 14 15 16 17 18 19 20 21 22
 *	  Most of the read functions for plan nodes are tested. (In fact, they
 *	  pass the regression test as of 11/8/94.) The rest (for path selection)
 *	  are probably never used. No effort has been made to get them to work.
 *	  The simplest way to test these functions is by doing the following in
 *	  ProcessQuery (before executing the plan):
 *				plan = stringToNode(nodeToString(plan));
 *	  Then, run the regression test. Let's just say you'll notice if either
 *	  of the above function are not properly done.
 *														- ay 11/94
 *
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
 *-------------------------------------------------------------------------
 */
#include <stdio.h>
#include <math.h>
#include <string.h>

#include "postgres.h"

#include "access/heapam.h"
#include "access/htup.h"
#include "fmgr.h"
#include "utils/builtins.h"
#include "utils/elog.h"
#include "utils/palloc.h"

#include "utils/lsyscache.h"
#include "utils/syscache.h"
#include "catalog/pg_type.h"

#include "nodes/primnodes.h"
#include "nodes/plannodes.h"
#include "nodes/parsenodes.h"
#include "nodes/execnodes.h"
#include "nodes/relation.h"
#include "nodes/readfuncs.h"

/* ----------------
50
 *		node creator declarations
51 52 53
 * ----------------
 */

54
static Datum readDatum(Oid type);
55

56
static List *
57
toIntList(List *list)
58
{
59
	List	   *l;
60 61 62 63 64 65 66

	foreach(l, list)
	{
		/* ugly manipulation, should probably free the Value node too */
		lfirst(l) = (void *) intVal(lfirst(l));
	}
	return list;
67 68 69
}

/* ----------------
70
 *		_readQuery
71 72
 * ----------------
 */
73
static Query *
74 75
_readQuery()
{
76 77 78
	Query	   *local_node;
	char	   *token;
	int			length;
79

80 81 82 83 84 85
	local_node = makeNode(Query);

	token = lsptok(NULL, &length);		/* skip the :command */
	token = lsptok(NULL, &length);		/* get the commandType */
	local_node->commandType = atoi(token);

86
	token = lsptok(NULL, &length);		/* skip :utility */
87
	/* we can't get create or index here, can we? */
88

89
	token = lsptok(NULL, &length);		/* get the notify name if any */
90
	if (length == 0)
91 92 93
		local_node->utilityStmt = NULL;
	else
	{
94
		NotifyStmt *n = makeNode(NotifyStmt);
95 96

		n->relname = palloc(length + 1);
97
		StrNCpy(n->relname, token, length + 1);
98 99 100
		local_node->utilityStmt = (Node *) n;
	}

101
	token = lsptok(NULL, &length);		/* skip the :resultRelation */
102 103 104
	token = lsptok(NULL, &length);		/* get the resultRelation */
	local_node->resultRelation = atoi(token);

105 106 107 108 109 110 111
	token = lsptok(NULL, &length);		/* skip :into */
	token = lsptok(NULL, &length);		/* get into */
	if (length == 0)
		local_node->into = NULL;
	else
	{
		local_node->into = palloc(length + 1);
112
		StrNCpy(local_node->into, token, length + 1);
113 114 115 116 117
	}

	token = lsptok(NULL, &length);		/* skip :isPortal */
	token = lsptok(NULL, &length);		/* get isPortal */
	local_node->isPortal = (token[0] == 't') ? true : false;
118

119 120 121
	token = lsptok(NULL, &length);		/* skip :isBinary */
	token = lsptok(NULL, &length);		/* get isBinary */
	local_node->isBinary = (token[0] == 't') ? true : false;
122

123 124 125
	token = lsptok(NULL, &length);		/* skip :unionall */
	token = lsptok(NULL, &length);		/* get unionall */
	local_node->unionall = (token[0] == 't') ? true : false;
126

127 128 129
	token = lsptok(NULL, &length);		/* skip :uniqueFlag */
	token = lsptok(NULL, &length);		/* get uniqueFlag */
	if (length == 0)
130 131 132 133
		local_node->uniqueFlag = NULL;
	else
	{
		local_node->uniqueFlag = palloc(length + 1);
134
		StrNCpy(local_node->uniqueFlag, token, length + 1);
135 136
	}

137 138 139 140 141 142
	token = lsptok(NULL, &length);		/* skip :sortClause */
	local_node->sortClause = nodeRead(true);

	token = lsptok(NULL, &length);		/* skip :rtable */
	local_node->rtable = nodeRead(true);

143 144 145 146 147
	token = lsptok(NULL, &length);		/* skip :targetlist */
	local_node->targetList = nodeRead(true);

	token = lsptok(NULL, &length);		/* skip :qual */
	local_node->qual = nodeRead(true);
148 149 150 151 152 153 154

	token = lsptok(NULL, &length);		/* skip :groupClause */
	local_node->groupClause = nodeRead(true);

	token = lsptok(NULL, &length);		/* skip :havingQual */
	local_node->havingQual = nodeRead(true);

155 156 157
	token = lsptok(NULL, &length);		/* skip the :hasAggs */
	token = lsptok(NULL, &length);		/* get hasAggs */
	local_node->hasAggs = (token[0] == 't') ? true : false;
158

159 160 161 162
	token = lsptok(NULL, &length);		/* skip the :hasSubLinks */
	token = lsptok(NULL, &length);		/* get hasSubLinks */
	local_node->hasSubLinks = (token[0] == 't') ? true : false;

163 164 165
	token = lsptok(NULL, &length);		/* skip :unionClause */
	local_node->unionClause = nodeRead(true);

B
Bruce Momjian 已提交
166 167 168 169 170 171
	token = lsptok(NULL, &length);		/* skip :limitOffset */
	local_node->limitOffset = nodeRead(true);

	token = lsptok(NULL, &length);		/* skip :limitCount */
	local_node->limitCount = nodeRead(true);

172
	return local_node;
173 174 175
}

/* ----------------
176
 *		_readSortClause
177 178
 * ----------------
 */
179 180
static SortClause *
_readSortClause()
181
{
182
	SortClause *local_node;
183 184 185
	char	   *token;
	int			length;

186
	local_node = makeNode(SortClause);
187

188 189
	token = lsptok(NULL, &length);		/* skip the :resdom */
	local_node->resdom = nodeRead(true);
190

191 192
	token = lsptok(NULL, &length);		/* skip :opoid */
	token = lsptok(NULL, &length);		/* get opoid */
193
	local_node->opoid = strtoul(token, NULL, 10);
194

195
	return local_node;
196
}
197

198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
/* ----------------
 *		_readGroupClause
 * ----------------
 */
static GroupClause *
_readGroupClause()
{
	GroupClause *local_node;
	char	   *token;
	int			length;

	local_node = makeNode(GroupClause);

	token = lsptok(NULL, &length);		/* skip the :entry */
	local_node->entry = nodeRead(true);

	token = lsptok(NULL, &length);		/* skip :grpOpoid */
	token = lsptok(NULL, &length);		/* get grpOpoid */
216
	local_node->grpOpoid = strtoul(token, NULL, 10);
217

218
	return local_node;
219 220 221
}

/* ----------------
222
 *		_getPlan
223 224 225
 * ----------------
 */
static void
226
_getPlan(Plan *node)
227
{
228 229
	char	   *token;
	int			length;
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245

	token = lsptok(NULL, &length);		/* first token is :cost */
	token = lsptok(NULL, &length);		/* next is the actual cost */
	node->cost = (Cost) atof(token);

	token = lsptok(NULL, &length);		/* skip the :size */
	token = lsptok(NULL, &length);		/* get the plan_size */
	node->plan_size = atoi(token);

	token = lsptok(NULL, &length);		/* skip the :width */
	token = lsptok(NULL, &length);		/* get the plan_width */
	node->plan_width = atoi(token);

	token = lsptok(NULL, &length);		/* eat the :state stuff */
	token = lsptok(NULL, &length);		/* now get the state */

246
	if (length == 0)
247 248 249 250
		node->state = (EState *) NULL;
	else
	{							/* Disgusting hack until I figure out what
								 * to do here */
251
		node->state = (EState *) !NULL;
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
	}

	token = lsptok(NULL, &length);		/* eat :qptargetlist */
	node->targetlist = nodeRead(true);

	token = lsptok(NULL, &length);		/* eat :qpqual */
	node->qual = nodeRead(true);

	token = lsptok(NULL, &length);		/* eat :lefttree */
	node->lefttree = (Plan *) nodeRead(true);

	token = lsptok(NULL, &length);		/* eat :righttree */
	node->righttree = (Plan *) nodeRead(true);

	return;
267 268 269
}

/*
270
 *	Stuff from plannodes.h
271 272 273
 */

/* ----------------
274
 *		_readPlan
275 276
 * ----------------
 */
277
static Plan *
278 279
_readPlan()
{
280
	Plan	   *local_node;
281 282 283 284 285

	local_node = makeNode(Plan);

	_getPlan(local_node);

286
	return local_node;
287 288 289
}

/* ----------------
290
 *		_readResult
291
 *
292 293
 *		Does some obscene, possibly unportable, magic with
 *		sizes of things.
294 295
 * ----------------
 */
296
static Result *
297 298
_readResult()
{
299 300 301
	Result	   *local_node;
	char	   *token;
	int			length;
302 303 304 305 306 307 308 309

	local_node = makeNode(Result);

	_getPlan((Plan *) local_node);

	token = lsptok(NULL, &length);		/* eat :resconstantqual */
	local_node->resconstantqual = nodeRead(true);		/* now read it */

310
	return local_node;
311 312 313
}

/* ----------------
314
 *		_readAppend
315
 *
316
 *	Append is a subclass of Plan.
317 318 319
 * ----------------
 */

320
static Append *
321 322
_readAppend()
{
323 324 325
	Append	   *local_node;
	char	   *token;
	int			length;
326 327 328 329 330

	local_node = makeNode(Append);

	_getPlan((Plan *) local_node);

331 332
	token = lsptok(NULL, &length);		/* eat :appendplans */
	local_node->appendplans = nodeRead(true);	/* now read it */
333

334
	token = lsptok(NULL, &length);		/* eat :unionrtables */
335
	local_node->unionrtables = nodeRead(true);	/* now read it */
336

337 338 339
	token = lsptok(NULL, &length);		/* eat :inheritrelid */
	token = lsptok(NULL, &length);		/* get inheritrelid */
	local_node->inheritrelid = strtoul(token, NULL, 10);
340

341
	token = lsptok(NULL, &length);		/* eat :inheritrtable */
342
	local_node->inheritrtable = nodeRead(true); /* now read it */
343

344
	return local_node;
345 346 347
}

/* ----------------
348
 *		_getJoin
349 350 351 352 353
 *
 * In case Join is not the same structure as Plan someday.
 * ----------------
 */
static void
354
_getJoin(Join *node)
355
{
356
	_getPlan((Plan *) node);
357 358 359 360
}


/* ----------------
361
 *		_readJoin
362
 *
363
 *	Join is a subclass of Plan
364 365
 * ----------------
 */
366
static Join *
367 368
_readJoin()
{
369
	Join	   *local_node;
370 371 372 373 374

	local_node = makeNode(Join);

	_getJoin(local_node);

375
	return local_node;
376 377 378
}

/* ----------------
379 380 381
 *		_readNestLoop
 *
 *	NestLoop is a subclass of Join
382 383 384 385 386 387
 * ----------------
 */

static NestLoop *
_readNestLoop()
{
388
	NestLoop   *local_node;
389 390 391 392 393

	local_node = makeNode(NestLoop);

	_getJoin((Join *) local_node);

394
	return local_node;
395 396 397
}

/* ----------------
398 399 400
 *		_readMergeJoin
 *
 *	MergeJoin is a subclass of Join
401 402 403 404 405
 * ----------------
 */
static MergeJoin *
_readMergeJoin()
{
406 407 408
	MergeJoin  *local_node;
	char	   *token;
	int			length;
409 410 411 412 413 414 415

	local_node = makeNode(MergeJoin);

	_getJoin((Join *) local_node);
	token = lsptok(NULL, &length);		/* eat :mergeclauses */
	local_node->mergeclauses = nodeRead(true);	/* now read it */

416 417 418
	token = lsptok(NULL, &length);		/* eat :mergejoinop */
	token = lsptok(NULL, &length);		/* get mergejoinop */
	local_node->mergejoinop = atol(token);
419

420
	return local_node;
421 422 423
}

/* ----------------
424 425 426
 *		_readHashJoin
 *
 *	HashJoin is a subclass of Join.
427 428 429 430 431
 * ----------------
 */
static HashJoin *
_readHashJoin()
{
432 433 434
	HashJoin   *local_node;
	char	   *token;
	int			length;
435 436 437 438 439 440 441 442 443 444

	local_node = makeNode(HashJoin);

	_getJoin((Join *) local_node);

	token = lsptok(NULL, &length);		/* eat :hashclauses */
	local_node->hashclauses = nodeRead(true);	/* now read it */

	token = lsptok(NULL, &length);		/* eat :hashjoinop */
	token = lsptok(NULL, &length);		/* get hashjoinop */
445
	local_node->hashjoinop = strtoul(token, NULL, 10);
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462

	token = lsptok(NULL, &length);		/* eat :hashjointable */
	token = lsptok(NULL, &length);		/* eat hashjointable */
	local_node->hashjointable = NULL;

	token = lsptok(NULL, &length);		/* eat :hashjointablekey */
	token = lsptok(NULL, &length);		/* eat hashjointablekey */
	local_node->hashjointablekey = 0;

	token = lsptok(NULL, &length);		/* eat :hashjointablesize */
	token = lsptok(NULL, &length);		/* eat hashjointablesize */
	local_node->hashjointablesize = 0;

	token = lsptok(NULL, &length);		/* eat :hashdone */
	token = lsptok(NULL, &length);		/* eat hashdone */
	local_node->hashdone = false;

463
	return local_node;
464 465 466
}

/* ----------------
467
 *		_getScan
468
 *
469 470 471
 *	Scan is a subclass of Node
 *	(Actually, according to the plannodes.h include file, it is a
 *	subclass of Plan.  This is why _getPlan is used here.)
472
 *
473
 *	Scan gets its own get function since stuff inherits it.
474 475
 * ----------------
 */
476
static void
477
_getScan(Scan *node)
478
{
479 480
	char	   *token;
	int			length;
481 482 483 484 485

	_getPlan((Plan *) node);

	token = lsptok(NULL, &length);		/* eat :scanrelid */
	token = lsptok(NULL, &length);		/* get scanrelid */
486
	node->scanrelid = strtoul(token, NULL, 10);
487 488 489
}

/* ----------------
490 491
 *		_readScan
 *
492 493 494
 * Scan is a subclass of Plan (Not Node, see above).
 * ----------------
 */
495
static Scan *
496 497
_readScan()
{
498
	Scan	   *local_node;
499 500 501 502 503

	local_node = makeNode(Scan);

	_getScan(local_node);

504
	return local_node;
505 506 507
}

/* ----------------
508 509 510
 *		_readSeqScan
 *
 *	SeqScan is a subclass of Scan
511 512 513 514 515
 * ----------------
 */
static SeqScan *
_readSeqScan()
{
516
	SeqScan    *local_node;
517 518 519 520 521

	local_node = makeNode(SeqScan);

	_getScan((Scan *) local_node);

522
	return local_node;
523 524 525
}

/* ----------------
526 527 528
 *		_readIndexScan
 *
 *	IndexScan is a subclass of Scan
529 530 531 532 533
 * ----------------
 */
static IndexScan *
_readIndexScan()
{
534 535 536
	IndexScan  *local_node;
	char	   *token;
	int			length;
537 538 539 540 541 542 543 544 545 546 547 548

	local_node = makeNode(IndexScan);

	_getScan((Scan *) local_node);

	token = lsptok(NULL, &length);		/* eat :indxid */
	local_node->indxid =
		toIntList(nodeRead(true));		/* now read it */

	token = lsptok(NULL, &length);		/* eat :indxqual */
	local_node->indxqual = nodeRead(true);		/* now read it */

549
	return local_node;
550 551 552
}

/* ----------------
553 554 555
 *		_readTemp
 *
 *	Temp is a subclass of Plan
556 557
 * ----------------
 */
558
static Temp *
559 560
_readTemp()
{
561 562 563
	Temp	   *local_node;
	char	   *token;
	int			length;
564 565 566 567 568 569 570 571 572 573 574 575 576

	local_node = makeNode(Temp);

	_getPlan((Plan *) local_node);

	token = lsptok(NULL, &length);		/* eat :tempid */
	token = lsptok(NULL, &length);		/* get tempid */
	local_node->tempid = atol(token);

	token = lsptok(NULL, &length);		/* eat :keycount */
	token = lsptok(NULL, &length);		/* get keycount */
	local_node->keycount = atoi(token);

577
	return local_node;
578 579 580
}

/* ----------------
581 582 583
 *		_readSort
 *
 *	Sort is a subclass of Temp
584 585
 * ----------------
 */
586
static Sort *
587 588
_readSort()
{
589 590 591
	Sort	   *local_node;
	char	   *token;
	int			length;
592 593 594 595 596 597 598 599 600 601 602 603 604

	local_node = makeNode(Sort);

	_getPlan((Plan *) local_node);

	token = lsptok(NULL, &length);		/* eat :tempid */
	token = lsptok(NULL, &length);		/* get tempid */
	local_node->tempid = atol(token);

	token = lsptok(NULL, &length);		/* eat :keycount */
	token = lsptok(NULL, &length);		/* get keycount */
	local_node->keycount = atoi(token);

605
	return local_node;
606 607
}

608
static Agg *
609 610
_readAgg()
{
611 612 613
	Agg		   *local_node;
	char	   *token;
	int			length;
614 615 616 617

	local_node = makeNode(Agg);
	_getPlan((Plan *) local_node);

618 619
	token = lsptok(NULL, &length);		/* eat :agg */
	local_node->aggs = nodeRead(true);	/* now read it */
620

621
	return local_node;
622 623 624
}

/* ----------------
625
 *		_readUnique
626 627 628
 *
 * For some reason, unique is a subclass of Temp.
 */
629
static Unique *
630 631
_readUnique()
{
632 633 634
	Unique	   *local_node;
	char	   *token;
	int			length;
635 636 637 638 639 640 641 642 643 644 645 646 647

	local_node = makeNode(Unique);

	_getPlan((Plan *) local_node);

	token = lsptok(NULL, &length);		/* eat :tempid */
	token = lsptok(NULL, &length);		/* get :tempid */
	local_node->tempid = atol(token);

	token = lsptok(NULL, &length);		/* eat :keycount */
	token = lsptok(NULL, &length);		/* get :keycount */
	local_node->keycount = atoi(token);

648
	return local_node;
649 650 651
}

/* ----------------
652 653 654
 *		_readHash
 *
 *	Hash is a subclass of Temp
655 656
 * ----------------
 */
657
static Hash *
658 659
_readHash()
{
660 661 662
	Hash	   *local_node;
	char	   *token;
	int			length;
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682

	local_node = makeNode(Hash);

	_getPlan((Plan *) local_node);

	token = lsptok(NULL, &length);		/* eat :hashkey */
	local_node->hashkey = (Var *) nodeRead(true);

	token = lsptok(NULL, &length);		/* eat :hashtable */
	token = lsptok(NULL, &length);		/* eat hashtable address */
	local_node->hashtable = NULL;

	token = lsptok(NULL, &length);		/* eat :hashtablekey */
	token = lsptok(NULL, &length);		/* get hashtablekey */
	local_node->hashtablekey = 0;

	token = lsptok(NULL, &length);		/* eat :hashtablesize */
	token = lsptok(NULL, &length);		/* get hashtablesize */
	local_node->hashtablesize = 0;

683
	return local_node;
684 685 686
}

/*
687
 *	Stuff from primnodes.h.
688 689 690
 */

/* ----------------
691 692 693
 *		_readResdom
 *
 *	Resdom is a subclass of Node
694 695
 * ----------------
 */
696
static Resdom *
697 698
_readResdom()
{
699 700 701
	Resdom	   *local_node;
	char	   *token;
	int			length;
702 703 704 705 706 707 708 709 710 711 712

	local_node = makeNode(Resdom);

	token = lsptok(NULL, &length);		/* eat :resno */
	token = lsptok(NULL, &length);		/* get resno */
	local_node->resno = atoi(token);

	token = lsptok(NULL, &length);		/* eat :restype */
	token = lsptok(NULL, &length);		/* get restype */
	local_node->restype = atol(token);

713 714 715
	token = lsptok(NULL, &length);		/* eat :restypmod */
	token = lsptok(NULL, &length);		/* get restypmod */
	local_node->restypmod = atoi(token);
716 717 718 719

	token = lsptok(NULL, &length);		/* eat :resname */
	token = lsptok(NULL, &length);		/* get the name */

720
	if (length == 0)
721 722 723
		local_node->resname = NULL;
	else
	{
724
		local_node->resname = (char *) palloc(length + 1);
725
		StrNCpy(local_node->resname, token + 1, length + 1 - 2);		/* strip quotes */
726 727 728 729
	}

	token = lsptok(NULL, &length);		/* eat :reskey */
	token = lsptok(NULL, &length);		/* get reskey */
730
	local_node->reskey = strtoul(token, NULL, 10);
731 732 733 734 735 736 737 738 739

	token = lsptok(NULL, &length);		/* eat :reskeyop */
	token = lsptok(NULL, &length);		/* get reskeyop */
	local_node->reskeyop = (Oid) atol(token);

	token = lsptok(NULL, &length);		/* eat :resjunk */
	token = lsptok(NULL, &length);		/* get resjunk */
	local_node->resjunk = atoi(token);

740
	return local_node;
741 742 743
}

/* ----------------
744 745 746
 *		_readExpr
 *
 *	Expr is a subclass of Node
747 748
 * ----------------
 */
749
static Expr *
750 751
_readExpr()
{
752 753 754
	Expr	   *local_node;
	char	   *token;
	int			length;
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773

	local_node = makeNode(Expr);

	token = lsptok(NULL, &length);		/* eat :typeOid */
	token = lsptok(NULL, &length);		/* get typeOid */
	local_node->typeOid = (Oid) atol(token);

	token = lsptok(NULL, &length);		/* eat :opType */
	token = lsptok(NULL, &length);		/* get opType */
	if (!strncmp(token, "op", 2))
		local_node->opType = OP_EXPR;
	else if (!strncmp(token, "func", 4))
		local_node->opType = FUNC_EXPR;
	else if (!strncmp(token, "or", 2))
		local_node->opType = OR_EXPR;
	else if (!strncmp(token, "and", 3))
		local_node->opType = AND_EXPR;
	else if (!strncmp(token, "not", 3))
		local_node->opType = NOT_EXPR;
V
Vadim B. Mikheev 已提交
774 775
	else if (!strncmp(token, "subp", 4))
		local_node->opType = SUBPLAN_EXPR;
776 777 778 779 780 781 782

	token = lsptok(NULL, &length);		/* eat :oper */
	local_node->oper = nodeRead(true);

	token = lsptok(NULL, &length);		/* eat :args */
	local_node->args = nodeRead(true);	/* now read it */

783
	return local_node;
784 785 786
}

/* ----------------
787 788 789
 *		_readVar
 *
 *	Var is a subclass of Expr
790 791
 * ----------------
 */
792
static Var *
793 794
_readVar()
{
795 796 797
	Var		   *local_node;
	char	   *token;
	int			length;
798 799 800 801 802

	local_node = makeNode(Var);

	token = lsptok(NULL, &length);		/* eat :varno */
	token = lsptok(NULL, &length);		/* get varno */
803
	local_node->varno = strtoul(token, NULL, 10);
804 805 806 807 808 809 810 811 812

	token = lsptok(NULL, &length);		/* eat :varattno */
	token = lsptok(NULL, &length);		/* get varattno */
	local_node->varattno = atoi(token);

	token = lsptok(NULL, &length);		/* eat :vartype */
	token = lsptok(NULL, &length);		/* get vartype */
	local_node->vartype = (Oid) atol(token);

813 814
	token = lsptok(NULL, &length);		/* eat :vartypmod */
	token = lsptok(NULL, &length);		/* get vartypmod */
B
Bruce Momjian 已提交
815
	local_node->vartypmod = atoi(token);
816

817 818 819 820
	token = lsptok(NULL, &length);		/* eat :varlevelsup */
	token = lsptok(NULL, &length);		/* get varlevelsup */
	local_node->varlevelsup = (Oid) atol(token);

821 822 823 824 825 826 827 828
	token = lsptok(NULL, &length);		/* eat :varnoold */
	token = lsptok(NULL, &length);		/* get varnoold */
	local_node->varnoold = (Oid) atol(token);

	token = lsptok(NULL, &length);		/* eat :varoattno */
	token = lsptok(NULL, &length);		/* eat :varoattno */
	local_node->varoattno = (int) atol(token);

829
	return local_node;
830 831 832 833 834 835 836 837
}

/* ----------------
 * _readArray
 *
 * Array is a subclass of Expr
 * ----------------
 */
838
static Array *
839 840
_readArray()
{
841 842 843
	Array	   *local_node;
	char	   *token;
	int			length;
844 845 846 847 848

	local_node = makeNode(Array);

	token = lsptok(NULL, &length);		/* eat :arrayelemtype */
	token = lsptok(NULL, &length);		/* get arrayelemtype */
849
	local_node->arrayelemtype = strtoul(token, NULL, 10);
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870

	token = lsptok(NULL, &length);		/* eat :arrayelemlength */
	token = lsptok(NULL, &length);		/* get arrayelemlength */
	local_node->arrayelemlength = atoi(token);

	token = lsptok(NULL, &length);		/* eat :arrayelembyval */
	token = lsptok(NULL, &length);		/* get arrayelembyval */
	local_node->arrayelembyval = (token[0] == 't') ? true : false;

	token = lsptok(NULL, &length);		/* eat :arraylow */
	token = lsptok(NULL, &length);		/* get arraylow */
	local_node->arraylow.indx[0] = atoi(token);

	token = lsptok(NULL, &length);		/* eat :arrayhigh */
	token = lsptok(NULL, &length);		/* get arrayhigh */
	local_node->arrayhigh.indx[0] = atoi(token);

	token = lsptok(NULL, &length);		/* eat :arraylen */
	token = lsptok(NULL, &length);		/* get arraylen */
	local_node->arraylen = atoi(token);

871
	return local_node;
872 873 874 875 876 877 878 879 880 881 882
}

/* ----------------
 * _readArrayRef
 *
 * ArrayRef is a subclass of Expr
 * ----------------
 */
static ArrayRef *
_readArrayRef()
{
883 884 885
	ArrayRef   *local_node;
	char	   *token;
	int			length;
886 887 888 889 890

	local_node = makeNode(ArrayRef);

	token = lsptok(NULL, &length);		/* eat :refelemtype */
	token = lsptok(NULL, &length);		/* get refelemtype */
891
	local_node->refelemtype = strtoul(token, NULL, 10);
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916

	token = lsptok(NULL, &length);		/* eat :refattrlength */
	token = lsptok(NULL, &length);		/* get refattrlength */
	local_node->refattrlength = atoi(token);

	token = lsptok(NULL, &length);		/* eat :refelemlength */
	token = lsptok(NULL, &length);		/* get refelemlength */
	local_node->refelemlength = atoi(token);

	token = lsptok(NULL, &length);		/* eat :refelembyval */
	token = lsptok(NULL, &length);		/* get refelembyval */
	local_node->refelembyval = (token[0] == 't') ? true : false;

	token = lsptok(NULL, &length);		/* eat :refupperindex */
	local_node->refupperindexpr = nodeRead(true);

	token = lsptok(NULL, &length);		/* eat :reflowerindex */
	local_node->reflowerindexpr = nodeRead(true);

	token = lsptok(NULL, &length);		/* eat :refexpr */
	local_node->refexpr = nodeRead(true);

	token = lsptok(NULL, &length);		/* eat :refassgnexpr */
	local_node->refassgnexpr = nodeRead(true);

917
	return local_node;
918 919 920
}

/* ----------------
921 922 923
 *		_readConst
 *
 *	Const is a subclass of Expr
924 925
 * ----------------
 */
926
static Const *
927 928
_readConst()
{
929 930 931
	Const	   *local_node;
	char	   *token;
	int			length;
932 933 934 935 936 937 938 939 940 941

	local_node = makeNode(Const);

	token = lsptok(NULL, &length);		/* get :consttype */
	token = lsptok(NULL, &length);		/* now read it */
	local_node->consttype = atol(token);


	token = lsptok(NULL, &length);		/* get :constlen */
	token = lsptok(NULL, &length);		/* now read it */
942
	local_node->constlen = strtol(token, NULL, 10);
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975

	token = lsptok(NULL, &length);		/* get :constisnull */
	token = lsptok(NULL, &length);		/* now read it */

	if (!strncmp(token, "true", 4))
		local_node->constisnull = true;
	else
		local_node->constisnull = false;


	token = lsptok(NULL, &length);		/* get :constvalue */

	if (local_node->constisnull)
	{
		token = lsptok(NULL, &length);	/* skip "NIL" */
	}
	else
	{

		/*
		 * read the value
		 */
		local_node->constvalue = readDatum(local_node->consttype);
	}

	token = lsptok(NULL, &length);		/* get :constbyval */
	token = lsptok(NULL, &length);		/* now read it */

	if (!strncmp(token, "true", 4))
		local_node->constbyval = true;
	else
		local_node->constbyval = false;

976
	return local_node;
977 978 979
}

/* ----------------
980 981 982
 *		_readFunc
 *
 *	Func is a subclass of Expr
983 984
 * ----------------
 */
985
static Func *
986 987
_readFunc()
{
988 989 990
	Func	   *local_node;
	char	   *token;
	int			length;
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025

	local_node = makeNode(Func);

	token = lsptok(NULL, &length);		/* get :funcid */
	token = lsptok(NULL, &length);		/* now read it */
	local_node->funcid = atol(token);

	token = lsptok(NULL, &length);		/* get :functype */
	token = lsptok(NULL, &length);		/* now read it */
	local_node->functype = atol(token);

	token = lsptok(NULL, &length);		/* get :funcisindex */
	token = lsptok(NULL, &length);		/* now read it */

	if (!strncmp(token, "true", 4))
		local_node->funcisindex = true;
	else
		local_node->funcisindex = false;

	token = lsptok(NULL, &length);		/* get :funcsize */
	token = lsptok(NULL, &length);		/* now read it */
	local_node->funcsize = atol(token);

	token = lsptok(NULL, &length);		/* get :func_fcache */
	token = lsptok(NULL, &length);		/* get @ */
	token = lsptok(NULL, &length);		/* now read it */

	local_node->func_fcache = (FunctionCache *) NULL;

	token = lsptok(NULL, &length);		/* get :func_tlist */
	local_node->func_tlist = nodeRead(true);	/* now read it */

	token = lsptok(NULL, &length);		/* get :func_planlist */
	local_node->func_planlist = nodeRead(true); /* now read it */

1026
	return local_node;
1027 1028 1029
}

/* ----------------
1030 1031 1032
 *		_readOper
 *
 *	Oper is a subclass of Expr
1033 1034
 * ----------------
 */
1035
static Oper *
1036 1037
_readOper()
{
1038 1039 1040
	Oper	   *local_node;
	char	   *token;
	int			length;
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061

	local_node = makeNode(Oper);

	token = lsptok(NULL, &length);		/* get :opno */
	token = lsptok(NULL, &length);		/* now read it */
	local_node->opno = atol(token);

	token = lsptok(NULL, &length);		/* get :opid */
	token = lsptok(NULL, &length);		/* now read it */
	local_node->opid = atol(token);

	token = lsptok(NULL, &length);		/* get :opresulttype */
	token = lsptok(NULL, &length);		/* now read it */
	local_node->opresulttype = atol(token);

	/*
	 * NOTE: Alternatively we can call 'replace_opid' which initializes
	 * both 'opid' and 'op_fcache'.
	 */
	local_node->op_fcache = (FunctionCache *) NULL;

1062
	return local_node;
1063 1064 1065
}

/* ----------------
1066 1067 1068
 *		_readParam
 *
 *	Param is a subclass of Expr
1069 1070
 * ----------------
 */
1071
static Param *
1072 1073
_readParam()
{
1074 1075 1076
	Param	   *local_node;
	char	   *token;
	int			length;
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089

	local_node = makeNode(Param);

	token = lsptok(NULL, &length);		/* get :paramkind */
	token = lsptok(NULL, &length);		/* now read it */
	local_node->paramkind = atoi(token);

	token = lsptok(NULL, &length);		/* get :paramid */
	token = lsptok(NULL, &length);		/* now read it */
	local_node->paramid = atol(token);

	token = lsptok(NULL, &length);		/* get :paramname */
	token = lsptok(NULL, &length);		/* now read it */
1090 1091 1092 1093 1094
	if (length == 0)
		local_node->paramname = NULL;
	else
	{
		local_node->paramname = (char *) palloc(length + 1);
1095
		StrNCpy(local_node->paramname, token, length + 1);
1096
	}
1097 1098 1099 1100 1101 1102 1103 1104

	token = lsptok(NULL, &length);		/* get :paramtype */
	token = lsptok(NULL, &length);		/* now read it */

	local_node->paramtype = atol(token);
	token = lsptok(NULL, &length);		/* get :param_tlist */
	local_node->param_tlist = nodeRead(true);	/* now read it */

1105
	return local_node;
1106 1107
}

1108
/* ----------------
1109 1110 1111
 *		_readAggreg
 *
 *	Aggreg is a subclass of Node
1112 1113
 * ----------------
 */
1114
static Aggreg *
1115 1116
_readAggreg()
{
1117 1118 1119
	Aggreg	   *local_node;
	char	   *token;
	int			length;
1120 1121 1122 1123 1124 1125

	local_node = makeNode(Aggreg);

	token = lsptok(NULL, &length);		/* eat :aggname */
	token = lsptok(NULL, &length);		/* get aggname */
	local_node->aggname = (char *) palloc(length + 1);
1126
	StrNCpy(local_node->aggname, token, length + 1);
1127 1128 1129 1130 1131 1132 1133 1134 1135

	token = lsptok(NULL, &length);		/* eat :basetype */
	token = lsptok(NULL, &length);		/* get basetype */
	local_node->basetype = (Oid) atol(token);

	token = lsptok(NULL, &length);		/* eat :aggtype */
	token = lsptok(NULL, &length);		/* get aggtype */
	local_node->aggtype = (Oid) atol(token);

1136 1137 1138
	token = lsptok(NULL, &length);		/* eat :target */
	local_node->target = nodeRead(true);		/* now read it */

1139 1140 1141 1142
	token = lsptok(NULL, &length);		/* eat :aggno */
	token = lsptok(NULL, &length);		/* get aggno */
	local_node->aggno = atoi(token);

1143 1144 1145
	token = lsptok(NULL, &length);		/* eat :usenulls */
	token = lsptok(NULL, &length);		/* get usenulls */
	local_node->usenulls = (token[0] == 't') ? true : false;
1146

1147
	return local_node;
1148 1149
}

1150 1151 1152 1153 1154 1155 1156 1157 1158
/* ----------------
 *		_readSubLink
 *
 *	SubLink is a subclass of Node
 * ----------------
 */
static SubLink *
_readSubLink()
{
1159
	SubLink    *local_node;
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
	char	   *token;
	int			length;

	local_node = makeNode(SubLink);

	token = lsptok(NULL, &length);		/* eat :subLinkType */
	token = lsptok(NULL, &length);		/* get subLinkType */
	local_node->subLinkType = atoi(token);

	token = lsptok(NULL, &length);		/* eat :useor */
	token = lsptok(NULL, &length);		/* get useor */
	local_node->useor = (token[0] == 't') ? true : false;

	token = lsptok(NULL, &length);		/* eat :lefthand */
	local_node->lefthand = nodeRead(true);		/* now read it */

	token = lsptok(NULL, &length);		/* eat :oper */
1177
	local_node->oper = nodeRead(true);	/* now read it */
1178 1179 1180 1181

	token = lsptok(NULL, &length);		/* eat :subselect */
	local_node->subselect = nodeRead(true);		/* now read it */

1182
	return local_node;
1183 1184
}

1185
/*
1186
 *	Stuff from execnodes.h
1187 1188 1189
 */

/* ----------------
1190 1191 1192
 *		_readEState
 *
 *	EState is a subclass of Node.
1193 1194
 * ----------------
 */
1195
static EState *
1196 1197
_readEState()
{
1198 1199 1200
	EState	   *local_node;
	char	   *token;
	int			length;
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218

	local_node = makeNode(EState);

	token = lsptok(NULL, &length);		/* get :direction */
	token = lsptok(NULL, &length);		/* now read it */

	local_node->es_direction = atoi(token);

	token = lsptok(NULL, &length);		/* get :range_table */

	local_node->es_range_table = nodeRead(true);		/* now read it */

	token = lsptok(NULL, &length);		/* get :result_relation_info */
	token = lsptok(NULL, &length);		/* get @ */
	token = lsptok(NULL, &length);		/* now read it */

	sscanf(token, "%x", (unsigned int *) &local_node->es_result_relation_info);

1219
	return local_node;
1220 1221 1222
}

/*
1223
 *	Stuff from relation.h
1224 1225 1226
 */

/* ----------------
B
Bruce Momjian 已提交
1227
 *		_readRelOptInfo
1228 1229
 * ----------------
 */
B
Bruce Momjian 已提交
1230
static RelOptInfo *
B
Bruce Momjian 已提交
1231
_readRelOptInfo()
1232
{
1233
	RelOptInfo *local_node;
1234 1235
	char	   *token;
	int			length;
1236

B
Bruce Momjian 已提交
1237
	local_node = makeNode(RelOptInfo);
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300

	token = lsptok(NULL, &length);		/* get :relids */
	local_node->relids =
		toIntList(nodeRead(true));		/* now read it */

	token = lsptok(NULL, &length);		/* get :indexed */
	token = lsptok(NULL, &length);		/* now read it */

	if (!strncmp(token, "true", 4))
		local_node->indexed = true;
	else
		local_node->indexed = false;

	token = lsptok(NULL, &length);		/* get :pages */
	token = lsptok(NULL, &length);		/* now read it */
	local_node->pages = (unsigned int) atoi(token);

	token = lsptok(NULL, &length);		/* get :tuples */
	token = lsptok(NULL, &length);		/* now read it */
	local_node->tuples = (unsigned int) atoi(token);

	token = lsptok(NULL, &length);		/* get :size */
	token = lsptok(NULL, &length);		/* now read it */
	local_node->size = (unsigned int) atoi(token);

	token = lsptok(NULL, &length);		/* get :width */
	token = lsptok(NULL, &length);		/* now read it */
	local_node->width = (unsigned int) atoi(token);

	token = lsptok(NULL, &length);		/* get :targetlist */
	local_node->targetlist = nodeRead(true);	/* now read it */

	token = lsptok(NULL, &length);		/* get :pathlist */
	local_node->pathlist = nodeRead(true);		/* now read it */

	/*
	 * Not sure if these are nodes or not.	They're declared as struct
	 * Path *.	Since i don't know, i'll just print the addresses for now.
	 * This can be changed later, if necessary.
	 */

	token = lsptok(NULL, &length);		/* get :unorderpath */
	token = lsptok(NULL, &length);		/* get @ */
	token = lsptok(NULL, &length);		/* now read it */

	sscanf(token, "%x", (unsigned int *) &local_node->unorderedpath);

	token = lsptok(NULL, &length);		/* get :cheapestpath */
	token = lsptok(NULL, &length);		/* get @ */
	token = lsptok(NULL, &length);		/* now read it */

	sscanf(token, "%x", (unsigned int *) &local_node->cheapestpath);


	token = lsptok(NULL, &length);		/* get :clauseinfo */
	local_node->clauseinfo = nodeRead(true);	/* now read it */

	token = lsptok(NULL, &length);		/* get :joininfo */
	local_node->joininfo = nodeRead(true);		/* now read it */

	token = lsptok(NULL, &length);		/* get :innerjoin */
	local_node->innerjoin = nodeRead(true);		/* now read it */

1301
	return local_node;
1302 1303 1304
}

/* ----------------
1305
 *		_readTargetEntry
1306 1307 1308 1309 1310
 * ----------------
 */
static TargetEntry *
_readTargetEntry()
{
1311 1312 1313
	TargetEntry *local_node;
	char	   *token;
	int			length;
1314

1315
	local_node = makeNode(TargetEntry);
1316

1317 1318
	token = lsptok(NULL, &length);		/* get :resdom */
	local_node->resdom = nodeRead(true);		/* now read it */
1319

1320 1321
	token = lsptok(NULL, &length);		/* get :expr */
	local_node->expr = nodeRead(true);	/* now read it */
1322

1323
	return local_node;
1324 1325 1326
}

/* ----------------
1327
 *		_readRangeTblEntry
1328 1329 1330 1331 1332
 * ----------------
 */
static RangeTblEntry *
_readRangeTblEntry()
{
1333 1334 1335
	RangeTblEntry *local_node;
	char	   *token;
	int			length;
1336

1337
	local_node = makeNode(RangeTblEntry);
1338

1339 1340
	token = lsptok(NULL, &length);		/* eat :relname */
	token = lsptok(NULL, &length);		/* get :relname */
1341
	if (length == 0)
1342 1343 1344
		local_node->relname = NULL;
	else
	{
1345
		local_node->relname = (char *) palloc(length + 1);
1346
		StrNCpy(local_node->relname, token, length + 1);
1347 1348 1349 1350
	}

	token = lsptok(NULL, &length);		/* eat :refname */
	token = lsptok(NULL, &length);		/* get :refname */
1351
	if (length == 0)
1352 1353 1354
		local_node->refname = NULL;
	else
	{
1355
		local_node->refname = (char *) palloc(length + 1);
1356
		StrNCpy(local_node->refname, token, length + 1);
1357 1358 1359 1360
	}

	token = lsptok(NULL, &length);		/* eat :relid */
	token = lsptok(NULL, &length);		/* get :relid */
1361
	local_node->relid = strtoul(token, NULL, 10);
1362

1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374
	token = lsptok(NULL, &length);		/* eat :inh */
	token = lsptok(NULL, &length);		/* get :inh */
	local_node->inh = (token[0] == 't') ? true : false;

	token = lsptok(NULL, &length);		/* eat :inFromCl */
	token = lsptok(NULL, &length);		/* get :inFromCl */
	local_node->inFromCl = (token[0] == 't') ? true : false;

	token = lsptok(NULL, &length);		/* eat :skipAcl */
	token = lsptok(NULL, &length);		/* get :skipAcl */
	local_node->skipAcl = (token[0] == 't') ? true : false;

1375
	return local_node;
1376 1377 1378
}

/* ----------------
1379 1380 1381
 *		_readPath
 *
 *	Path is a subclass of Node.
1382 1383
 * ----------------
 */
1384
static Path *
1385 1386
_readPath()
{
1387 1388 1389
	Path	   *local_node;
	char	   *token;
	int			length;
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400

	local_node = makeNode(Path);

	token = lsptok(NULL, &length);		/* get :pathtype */
	token = lsptok(NULL, &length);		/* now read it */
	local_node->pathtype = atol(token);

	token = lsptok(NULL, &length);		/* get :cost */
	token = lsptok(NULL, &length);		/* now read it */
	local_node->path_cost = (Cost) atof(token);

1401
#if 0
1402 1403 1404
	token = lsptok(NULL, &length);		/* get :p_ordering */
	local_node->p_ordering =
		nodeRead(true);			/* now read it */
1405
#endif
1406 1407 1408 1409

	token = lsptok(NULL, &length);		/* get :keys */
	local_node->keys = nodeRead(true);	/* now read it */

1410
	return local_node;
1411 1412 1413
}

/* ----------------
1414 1415 1416
 *		_readIndexPath
 *
 *	IndexPath is a subclass of Path.
1417 1418 1419 1420 1421
 * ----------------
 */
static IndexPath *
_readIndexPath()
{
1422 1423 1424
	IndexPath  *local_node;
	char	   *token;
	int			length;
1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435

	local_node = makeNode(IndexPath);

	token = lsptok(NULL, &length);		/* get :pathtype */
	token = lsptok(NULL, &length);		/* now read it */
	local_node->path.pathtype = atol(token);

	token = lsptok(NULL, &length);		/* get :cost */
	token = lsptok(NULL, &length);		/* now read it */
	local_node->path.path_cost = (Cost) atof(token);

1436
#if 0
1437 1438
	token = lsptok(NULL, &length);		/* get :p_ordering */
	local_node->path.p_ordering = nodeRead(true);		/* now read it */
1439
#endif
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450

	token = lsptok(NULL, &length);		/* get :keys */
	local_node->path.keys = nodeRead(true);		/* now read it */

	token = lsptok(NULL, &length);		/* get :indexid */
	local_node->indexid =
		toIntList(nodeRead(true));

	token = lsptok(NULL, &length);		/* get :indexqual */
	local_node->indexqual = nodeRead(true);		/* now read it */

1451
	return local_node;
1452 1453 1454
}

/* ----------------
1455 1456 1457
 *		_readJoinPath
 *
 *	JoinPath is a subclass of Path
1458 1459 1460 1461 1462
 * ----------------
 */
static JoinPath *
_readJoinPath()
{
1463 1464 1465
	JoinPath   *local_node;
	char	   *token;
	int			length;
1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477


	local_node = makeNode(JoinPath);

	token = lsptok(NULL, &length);		/* get :pathtype */
	token = lsptok(NULL, &length);		/* now read it */
	local_node->path.pathtype = atol(token);

	token = lsptok(NULL, &length);		/* get :cost */
	token = lsptok(NULL, &length);		/* now read it */
	local_node->path.path_cost = (Cost) atof(token);

1478
#if 0
1479 1480
	token = lsptok(NULL, &length);		/* get :p_ordering */
	local_node->path.p_ordering = nodeRead(true);		/* now read it */
1481
#endif
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517

	token = lsptok(NULL, &length);		/* get :keys */
	local_node->path.keys = nodeRead(true);		/* now read it */

	token = lsptok(NULL, &length);		/* get :pathclauseinfo */
	local_node->pathclauseinfo = nodeRead(true);		/* now read it */

	/*
	 * Not sure if these are nodes; they're declared as "struct path *".
	 * For now, i'll just print the addresses.
	 *
	 * GJK:  Since I am parsing this stuff, I'll just ignore the addresses,
	 * and initialize these pointers to NULL.
	 */

	token = lsptok(NULL, &length);		/* get :outerjoinpath */
	token = lsptok(NULL, &length);		/* get @ */
	token = lsptok(NULL, &length);		/* now read it */

	local_node->outerjoinpath = NULL;

	token = lsptok(NULL, &length);		/* get :innerjoinpath */
	token = lsptok(NULL, &length);		/* get @ */
	token = lsptok(NULL, &length);		/* now read it */

	local_node->innerjoinpath = NULL;

	token = lsptok(NULL, &length);		/* get :outerjoincost */
	token = lsptok(NULL, &length);		/* now read it */

	local_node->path.outerjoincost = (Cost) atof(token);

	token = lsptok(NULL, &length);		/* get :joinid */
	local_node->path.joinid =
		toIntList(nodeRead(true));		/* now read it */

1518
	return local_node;
1519 1520 1521
}

/* ----------------
1522 1523 1524
 *		_readMergePath
 *
 *	MergePath is a subclass of JoinPath.
1525 1526 1527 1528 1529
 * ----------------
 */
static MergePath *
_readMergePath()
{
1530 1531 1532
	MergePath  *local_node;
	char	   *token;
	int			length;
1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545

	local_node = makeNode(MergePath);

	token = lsptok(NULL, &length);		/* get :pathtype */
	token = lsptok(NULL, &length);		/* now read it */

	local_node->jpath.path.pathtype = atol(token);

	token = lsptok(NULL, &length);		/* get :cost */
	token = lsptok(NULL, &length);		/* now read it */

	local_node->jpath.path.path_cost = (Cost) atof(token);

1546
#if 0
1547 1548
	token = lsptok(NULL, &length);		/* get :p_ordering */
	local_node->jpath.path.p_ordering = nodeRead(true); /* now read it */
1549
#endif
1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594

	token = lsptok(NULL, &length);		/* get :keys */
	local_node->jpath.path.keys = nodeRead(true);		/* now read it */

	token = lsptok(NULL, &length);		/* get :pathclauseinfo */
	local_node->jpath.pathclauseinfo = nodeRead(true);	/* now read it */

	/*
	 * Not sure if these are nodes; they're declared as "struct path *".
	 * For now, i'll just print the addresses.
	 *
	 * GJK:  Since I am parsing this stuff, I'll just ignore the addresses,
	 * and initialize these pointers to NULL.
	 */

	token = lsptok(NULL, &length);		/* get :outerjoinpath */
	token = lsptok(NULL, &length);		/* get @ */
	token = lsptok(NULL, &length);		/* now read it */

	local_node->jpath.outerjoinpath = NULL;

	token = lsptok(NULL, &length);		/* get :innerjoinpath */
	token = lsptok(NULL, &length);		/* get @ */
	token = lsptok(NULL, &length);		/* now read it */

	local_node->jpath.innerjoinpath = NULL;

	token = lsptok(NULL, &length);		/* get :outerjoincost */
	token = lsptok(NULL, &length);		/* now read it */

	local_node->jpath.path.outerjoincost = (Cost) atof(token);

	token = lsptok(NULL, &length);		/* get :joinid */
	local_node->jpath.path.joinid =
		toIntList(nodeRead(true));		/* now read it */

	token = lsptok(NULL, &length);		/* get :path_mergeclauses */
	local_node->path_mergeclauses = nodeRead(true);		/* now read it */

	token = lsptok(NULL, &length);		/* get :outersortkeys */
	local_node->outersortkeys = nodeRead(true); /* now read it */

	token = lsptok(NULL, &length);		/* get :innersortkeys */
	local_node->innersortkeys = nodeRead(true); /* now read it */

1595
	return local_node;
1596 1597 1598
}

/* ----------------
1599 1600 1601
 *		_readHashPath
 *
 *	HashPath is a subclass of JoinPath.
1602 1603 1604 1605 1606
 * ----------------
 */
static HashPath *
_readHashPath()
{
1607 1608 1609
	HashPath   *local_node;
	char	   *token;
	int			length;
1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622

	local_node = makeNode(HashPath);

	token = lsptok(NULL, &length);		/* get :pathtype */
	token = lsptok(NULL, &length);		/* now read it */

	local_node->jpath.path.pathtype = atol(token);

	token = lsptok(NULL, &length);		/* get :cost */
	token = lsptok(NULL, &length);		/* now read it */

	local_node->jpath.path.path_cost = (Cost) atof(token);

1623
#if 0
1624 1625
	token = lsptok(NULL, &length);		/* get :p_ordering */
	local_node->jpath.path.p_ordering = nodeRead(true); /* now read it */
1626
#endif
1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671

	token = lsptok(NULL, &length);		/* get :keys */
	local_node->jpath.path.keys = nodeRead(true);		/* now read it */

	token = lsptok(NULL, &length);		/* get :pathclauseinfo */
	local_node->jpath.pathclauseinfo = nodeRead(true);	/* now read it */

	/*
	 * Not sure if these are nodes; they're declared as "struct path *".
	 * For now, i'll just print the addresses.
	 *
	 * GJK:  Since I am parsing this stuff, I'll just ignore the addresses,
	 * and initialize these pointers to NULL.
	 */

	token = lsptok(NULL, &length);		/* get :outerjoinpath */
	token = lsptok(NULL, &length);		/* get @ */
	token = lsptok(NULL, &length);		/* now read it */

	local_node->jpath.outerjoinpath = NULL;

	token = lsptok(NULL, &length);		/* get :innerjoinpath */
	token = lsptok(NULL, &length);		/* get @ */
	token = lsptok(NULL, &length);		/* now read it */

	local_node->jpath.innerjoinpath = NULL;

	token = lsptok(NULL, &length);		/* get :outerjoincost */
	token = lsptok(NULL, &length);		/* now read it */

	local_node->jpath.path.outerjoincost = (Cost) atof(token);

	token = lsptok(NULL, &length);		/* get :joinid */
	local_node->jpath.path.joinid =
		toIntList(nodeRead(true));		/* now read it */

	token = lsptok(NULL, &length);		/* get :path_hashclauses */
	local_node->path_hashclauses = nodeRead(true);		/* now read it */

	token = lsptok(NULL, &length);		/* get :outerhashkeys */
	local_node->outerhashkeys = nodeRead(true); /* now read it */

	token = lsptok(NULL, &length);		/* get :innerhashkeys */
	local_node->innerhashkeys = nodeRead(true); /* now read it */

1672
	return local_node;
1673 1674 1675
}

/* ----------------
1676 1677 1678
 *		_readOrderKey
 *
 *	OrderKey is a subclass of Node.
1679 1680 1681 1682 1683
 * ----------------
 */
static OrderKey *
_readOrderKey()
{
1684 1685 1686
	OrderKey   *local_node;
	char	   *token;
	int			length;
1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697

	local_node = makeNode(OrderKey);

	token = lsptok(NULL, &length);		/* get :attribute_number */
	token = lsptok(NULL, &length);		/* now read it */

	local_node->attribute_number = atoi(token);

	token = lsptok(NULL, &length);		/* get :array_index */
	token = lsptok(NULL, &length);		/* now read it */

1698
	local_node->array_index = strtoul(token, NULL, 10);
1699

1700
	return local_node;
1701 1702 1703
}

/* ----------------
1704 1705 1706
 *		_readJoinKey
 *
 *	JoinKey is a subclass of Node.
1707 1708 1709 1710 1711
 * ----------------
 */
static JoinKey *
_readJoinKey()
{
1712 1713 1714
	JoinKey    *local_node;
	char	   *token;
	int			length;
1715 1716 1717 1718 1719 1720 1721 1722 1723

	local_node = makeNode(JoinKey);

	token = lsptok(NULL, &length);		/* get :outer */
	local_node->outer = nodeRead(true); /* now read it */

	token = lsptok(NULL, &length);		/* get :inner */
	local_node->inner = nodeRead(true); /* now read it */

1724
	return local_node;
1725 1726 1727
}

/* ----------------
1728 1729 1730
 *		_readMergeOrder
 *
 *	MergeOrder is a subclass of Node.
1731 1732 1733 1734 1735
 * ----------------
 */
static MergeOrder *
_readMergeOrder()
{
1736 1737 1738
	MergeOrder *local_node;
	char	   *token;
	int			length;
1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765

	local_node = makeNode(MergeOrder);
	token = lsptok(NULL, &length);		/* get :join_operator */
	token = lsptok(NULL, &length);		/* now read it */

	local_node->join_operator = atol(token);

	token = lsptok(NULL, &length);		/* get :left_operator */
	token = lsptok(NULL, &length);		/* now read it */

	local_node->left_operator = atol(token);

	token = lsptok(NULL, &length);		/* get :right_operator */
	token = lsptok(NULL, &length);		/* now read it */

	local_node->right_operator = atol(token);

	token = lsptok(NULL, &length);		/* get :left_type */
	token = lsptok(NULL, &length);		/* now read it */

	local_node->left_type = atol(token);

	token = lsptok(NULL, &length);		/* get :right_type */
	token = lsptok(NULL, &length);		/* now read it */

	local_node->right_type = atol(token);

1766
	return local_node;
1767 1768 1769
}

/* ----------------
1770
 *		_readClauseInfo
1771
 *
1772
 *	ClauseInfo is a subclass of Node.
1773 1774
 * ----------------
 */
1775 1776
static ClauseInfo *
_readClauseInfo()
1777
{
1778
	ClauseInfo *local_node;
1779 1780
	char	   *token;
	int			length;
1781

1782
	local_node = makeNode(ClauseInfo);
1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802

	token = lsptok(NULL, &length);		/* get :clause */
	local_node->clause = nodeRead(true);		/* now read it */

	token = lsptok(NULL, &length);		/* get :selectivity */
	token = lsptok(NULL, &length);		/* now read it */

	local_node->selectivity = atof(token);

	token = lsptok(NULL, &length);		/* get :notclause */
	token = lsptok(NULL, &length);		/* now read it */

	if (!strncmp(token, "true", 4))
		local_node->notclause = true;
	else
		local_node->notclause = false;

	token = lsptok(NULL, &length);		/* get :indexids */
	local_node->indexids = nodeRead(true);		/* now read it */

1803 1804
	token = lsptok(NULL, &length);		/* get :mergejoinorder */
	local_node->mergejoinorder = (MergeOrder *) nodeRead(true);
1805 1806 1807 1808 1809 1810

	token = lsptok(NULL, &length);		/* get :hashjoinoperator */
	token = lsptok(NULL, &length);		/* now read it */

	local_node->hashjoinoperator = atol(token);

1811
	return local_node;
1812 1813 1814
}

/* ----------------
1815 1816 1817
 *		_readJoinMethod
 *
 *	JoinMethod is a subclass of Node.
1818 1819 1820 1821 1822
 * ----------------
 */
static JoinMethod *
_readJoinMethod()
{
1823 1824 1825
	JoinMethod *local_node;
	char	   *token;
	int			length;
1826 1827 1828 1829 1830 1831 1832 1833 1834

	local_node = makeNode(JoinMethod);

	token = lsptok(NULL, &length);		/* get :jmkeys */
	local_node->jmkeys = nodeRead(true);		/* now read it */

	token = lsptok(NULL, &length);		/* get :clauses */
	local_node->clauses = nodeRead(true);		/* now read it */

1835
	return local_node;
1836 1837 1838
}

/* ----------------
1839 1840
 *		_readHInfo
 *
1841 1842 1843
 * HInfo is a subclass of JoinMethod.
 * ----------------
 */
1844
static HInfo *
1845 1846
_readHInfo()
{
1847 1848 1849
	HInfo	   *local_node;
	char	   *token;
	int			length;
1850 1851 1852 1853 1854 1855

	local_node = makeNode(HInfo);

	token = lsptok(NULL, &length);		/* get :hashop */
	token = lsptok(NULL, &length);		/* now read it */

1856
	local_node->hashop = strtoul(token, NULL, 10);
1857 1858 1859 1860 1861 1862 1863

	token = lsptok(NULL, &length);		/* get :jmkeys */
	local_node->jmethod.jmkeys = nodeRead(true);		/* now read it */

	token = lsptok(NULL, &length);		/* get :clauses */
	local_node->jmethod.clauses = nodeRead(true);		/* now read it */

1864
	return local_node;
1865 1866 1867
}

/* ----------------
1868
 *		_readJoinInfo()
1869
 *
1870
 *	JoinInfo is a subclass of Node.
1871 1872
 * ----------------
 */
1873 1874
static JoinInfo *
_readJoinInfo()
1875
{
1876
	JoinInfo   *local_node;
1877 1878
	char	   *token;
	int			length;
1879

1880
	local_node = makeNode(JoinInfo);
1881 1882 1883 1884 1885 1886 1887 1888

	token = lsptok(NULL, &length);		/* get :otherrels */
	local_node->otherrels =
		toIntList(nodeRead(true));		/* now read it */

	token = lsptok(NULL, &length);		/* get :jinfoclauseinfo */
	local_node->jinfoclauseinfo = nodeRead(true);		/* now read it */

1889
	token = lsptok(NULL, &length);		/* get :mergejoinable */
1890 1891

	if (!strncmp(token, "true", 4))
1892
		local_node->mergejoinable = true;
1893
	else
1894
		local_node->mergejoinable = false;
1895 1896 1897 1898 1899 1900 1901 1902

	token = lsptok(NULL, &length);		/* get :hashjoinable */

	if (!strncmp(token, "true", 4))
		local_node->hashjoinable = true;
	else
		local_node->hashjoinable = false;

1903
	return local_node;
1904 1905 1906
}

/* ----------------
1907
 *		_readIter()
1908 1909 1910
 *
 * ----------------
 */
1911
static Iter *
1912 1913
_readIter()
{
1914 1915 1916
	Iter	   *local_node;
	char	   *token;
	int			length;
1917 1918 1919 1920 1921 1922

	local_node = makeNode(Iter);

	token = lsptok(NULL, &length);		/* eat :iterexpr */
	local_node->iterexpr = nodeRead(true);		/* now read it */

1923
	return local_node;
1924 1925 1926 1927
}


/* ----------------
1928
 *		parsePlanString
1929 1930 1931 1932 1933 1934 1935
 *
 * Given a character string containing a plan, parsePlanString sets up the
 * plan structure representing that plan.
 *
 * The string passed to parsePlanString must be null-terminated.
 * ----------------
 */
1936
Node *
1937
parsePlanString(void)
1938
{
1939 1940 1941
	char	   *token;
	int			length;
	void	   *return_value = NULL;
1942 1943 1944

	token = lsptok(NULL, &length);

1945
	if (!strncmp(token, "PLAN", length))
1946
		return_value = _readPlan();
1947
	else if (!strncmp(token, "RESULT", length))
1948
		return_value = _readResult();
1949
	else if (!strncmp(token, "APPEND", length))
1950
		return_value = _readAppend();
1951
	else if (!strncmp(token, "JOIN", length))
1952
		return_value = _readJoin();
1953
	else if (!strncmp(token, "NESTLOOP", length))
1954
		return_value = _readNestLoop();
1955
	else if (!strncmp(token, "MERGEJOIN", length))
1956
		return_value = _readMergeJoin();
1957
	else if (!strncmp(token, "HASHJOIN", length))
1958
		return_value = _readHashJoin();
1959
	else if (!strncmp(token, "SCAN", length))
1960
		return_value = _readScan();
1961
	else if (!strncmp(token, "SEQSCAN", length))
1962
		return_value = _readSeqScan();
1963
	else if (!strncmp(token, "INDEXSCAN", length))
1964
		return_value = _readIndexScan();
1965
	else if (!strncmp(token, "TEMP", length))
1966
		return_value = _readTemp();
1967
	else if (!strncmp(token, "SORT", length))
1968
		return_value = _readSort();
1969
	else if (!strncmp(token, "AGGREG", length))
1970
		return_value = _readAggreg();
1971
	else if (!strncmp(token, "SUBLINK", length))
1972
		return_value = _readSubLink();
1973
	else if (!strncmp(token, "AGG", length))
1974
		return_value = _readAgg();
1975
	else if (!strncmp(token, "UNIQUE", length))
1976
		return_value = _readUnique();
1977
	else if (!strncmp(token, "HASH", length))
1978
		return_value = _readHash();
1979
	else if (!strncmp(token, "RESDOM", length))
1980
		return_value = _readResdom();
1981
	else if (!strncmp(token, "EXPR", length))
1982
		return_value = _readExpr();
1983
	else if (!strncmp(token, "ARRAYREF", length))
1984
		return_value = _readArrayRef();
1985
	else if (!strncmp(token, "ARRAY", length))
1986
		return_value = _readArray();
1987
	else if (!strncmp(token, "VAR", length))
1988
		return_value = _readVar();
1989
	else if (!strncmp(token, "CONST", length))
1990
		return_value = _readConst();
1991
	else if (!strncmp(token, "FUNC", length))
1992
		return_value = _readFunc();
1993
	else if (!strncmp(token, "OPER", length))
1994
		return_value = _readOper();
1995
	else if (!strncmp(token, "PARAM", length))
1996
		return_value = _readParam();
1997
	else if (!strncmp(token, "ESTATE", length))
1998
		return_value = _readEState();
B
Bruce Momjian 已提交
1999
	else if (!strncmp(token, "RELOPTINFO", length))
B
Bruce Momjian 已提交
2000
		return_value = _readRelOptInfo();
B
Bruce Momjian 已提交
2001
	else if (!strncmp(token, "TARGETENTRY", length))
2002
		return_value = _readTargetEntry();
2003
	else if (!strncmp(token, "RTE", length))
2004
		return_value = _readRangeTblEntry();
2005
	else if (!strncmp(token, "PATH", length))
2006
		return_value = _readPath();
2007
	else if (!strncmp(token, "INDEXPATH", length))
2008
		return_value = _readIndexPath();
2009
	else if (!strncmp(token, "JOINPATH", length))
2010
		return_value = _readJoinPath();
2011
	else if (!strncmp(token, "MERGEPATH", length))
2012
		return_value = _readMergePath();
2013
	else if (!strncmp(token, "HASHPATH", length))
2014
		return_value = _readHashPath();
2015
	else if (!strncmp(token, "ORDERKEY", length))
2016
		return_value = _readOrderKey();
2017
	else if (!strncmp(token, "JOINKEY", length))
2018
		return_value = _readJoinKey();
2019
	else if (!strncmp(token, "MERGEORDER", length))
2020
		return_value = _readMergeOrder();
2021 2022
	else if (!strncmp(token, "CLAUSEINFO", length))
		return_value = _readClauseInfo();
2023
	else if (!strncmp(token, "JOINMETHOD", length))
2024
		return_value = _readJoinMethod();
2025 2026
	else if (!strncmp(token, "JOININFO", length))
		return_value = _readJoinInfo();
2027
	else if (!strncmp(token, "HINFO", length))
2028
		return_value = _readHInfo();
2029
	else if (!strncmp(token, "ITER", length))
2030
		return_value = _readIter();
2031
	else if (!strncmp(token, "QUERY", length))
2032
		return_value = _readQuery();
2033
	else if (!strncmp(token, "SORTCLAUSE", length))
2034
		return_value = _readSortClause();
2035
	else if (!strncmp(token, "GROUPCLAUSE", length))
2036
		return_value = _readGroupClause();
2037
	else
2038
		elog(ERROR, "badly formatted planstring \"%.10s\"...\n", token);
2039

2040
	return (Node *) return_value;
2041
}
2042

2043 2044 2045
/*------------------------------------------------------------*/

/* ----------------
2046
 *		readDatum
2047 2048 2049 2050 2051
 *
 * given a string representation of the value of the given type,
 * create the appropriate Datum
 * ----------------
 */
2052
static Datum
2053 2054
readDatum(Oid type)
{
2055 2056 2057 2058 2059 2060 2061
	int			length;
	int			tokenLength;
	char	   *token;
	bool		byValue;
	Datum		res;
	char	   *s;
	int			i;
2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074

	byValue = get_typbyval(type);

	/*
	 * read the actual length of the value
	 */
	token = lsptok(NULL, &tokenLength);
	length = atoi(token);
	token = lsptok(NULL, &tokenLength); /* skip the '[' */

	if (byValue)
	{
		if (length > sizeof(Datum))
2075
			elog(ERROR, "readValue: byval & length = %d", length);
2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098
		s = (char *) (&res);
		for (i = 0; i < sizeof(Datum); i++)
		{
			token = lsptok(NULL, &tokenLength);
			s[i] = (char) atoi(token);
		}
	}
	else if (length <= 0)
		s = NULL;
	else if (length >= 1)
	{
		s = (char *) palloc(length);
		Assert(s != NULL);
		for (i = 0; i < length; i++)
		{
			token = lsptok(NULL, &tokenLength);
			s[i] = (char) atoi(token);
		}
		res = PointerGetDatum(s);
	}

	token = lsptok(NULL, &tokenLength); /* skip the ']' */
	if (token[0] != ']')
2099
		elog(ERROR, "readValue: ']' expected, length =%d", length);
2100

2101
	return res;
2102
}