readfuncs.c 53.2 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
10
 *	  $Header: /cvsroot/pgsql/src/backend/nodes/readfuncs.c,v 1.60 1999/03/01 00:10:31 tgl 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
 *-------------------------------------------------------------------------
 */
#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"
40
#include "catalog/pg_index.h"
41 42 43 44 45 46 47 48 49 50
#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"

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

55
static Datum readDatum(Oid type);
56

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

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

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

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

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

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

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

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

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

106 107 108 109 110 111 112
	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);
113
		StrNCpy(local_node->into, token, length + 1);
114 115 116 117 118
	}

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

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

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

128 129 130
	token = lsptok(NULL, &length);		/* skip :unionall */
	token = lsptok(NULL, &length);		/* get unionall */
	local_node->unionall = (token[0] == 't') ? true : false;
131

132 133 134
	token = lsptok(NULL, &length);		/* skip :uniqueFlag */
	token = lsptok(NULL, &length);		/* get uniqueFlag */
	if (length == 0)
135 136 137 138
		local_node->uniqueFlag = NULL;
	else
	{
		local_node->uniqueFlag = palloc(length + 1);
139
		StrNCpy(local_node->uniqueFlag, token, length + 1);
140 141
	}

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

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

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

	token = lsptok(NULL, &length);		/* skip :qual */
	local_node->qual = nodeRead(true);
153 154 155 156 157 158 159

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

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

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

164 165 166 167
	token = lsptok(NULL, &length);		/* skip the :hasSubLinks */
	token = lsptok(NULL, &length);		/* get hasSubLinks */
	local_node->hasSubLinks = (token[0] == 't') ? true : false;

168 169 170
	token = lsptok(NULL, &length);		/* skip :unionClause */
	local_node->unionClause = nodeRead(true);

B
Hi!  
Bruce Momjian 已提交
171 172 173 174 175
	/***S*I***/
 	token = lsptok(NULL, &length);		/* skip :intersectClause */
 	local_node->intersectClause = nodeRead(true);
 

B
Bruce Momjian 已提交
176 177 178 179 180 181
	token = lsptok(NULL, &length);		/* skip :limitOffset */
	local_node->limitOffset = nodeRead(true);

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

182 183 184
	token = lsptok(NULL, &length);		/* skip :rowMark */
	local_node->rowMark = nodeRead(true);

185
	return local_node;
186 187 188
}

/* ----------------
189
 *		_readSortClause
190 191
 * ----------------
 */
192 193
static SortClause *
_readSortClause()
194
{
195
	SortClause *local_node;
196 197 198
	char	   *token;
	int			length;

199
	local_node = makeNode(SortClause);
200

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

204 205
	token = lsptok(NULL, &length);		/* skip :opoid */
	token = lsptok(NULL, &length);		/* get opoid */
206
	local_node->opoid = strtoul(token, NULL, 10);
207

208
	return local_node;
209
}
210

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
/* ----------------
 *		_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 */
229
	local_node->grpOpoid = strtoul(token, NULL, 10);
230

231
	return local_node;
232 233 234
}

/* ----------------
235
 *		_getPlan
236 237 238
 * ----------------
 */
static void
239
_getPlan(Plan *node)
240
{
241 242
	char	   *token;
	int			length;
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258

	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 */

259
	if (length == 0)
260 261 262 263
		node->state = (EState *) NULL;
	else
	{							/* Disgusting hack until I figure out what
								 * to do here */
264
		node->state = (EState *) !NULL;
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
	}

	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;
280 281 282
}

/*
283
 *	Stuff from plannodes.h
284 285 286
 */

/* ----------------
287
 *		_readPlan
288 289
 * ----------------
 */
290
static Plan *
291 292
_readPlan()
{
293
	Plan	   *local_node;
294 295 296 297 298

	local_node = makeNode(Plan);

	_getPlan(local_node);

299
	return local_node;
300 301 302
}

/* ----------------
303
 *		_readResult
304
 *
305 306
 *		Does some obscene, possibly unportable, magic with
 *		sizes of things.
307 308
 * ----------------
 */
309
static Result *
310 311
_readResult()
{
312 313 314
	Result	   *local_node;
	char	   *token;
	int			length;
315 316 317 318 319 320 321 322

	local_node = makeNode(Result);

	_getPlan((Plan *) local_node);

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

323
	return local_node;
324 325 326
}

/* ----------------
327
 *		_readAppend
328
 *
329
 *	Append is a subclass of Plan.
330 331 332
 * ----------------
 */

333
static Append *
334 335
_readAppend()
{
336 337 338
	Append	   *local_node;
	char	   *token;
	int			length;
339 340 341 342 343

	local_node = makeNode(Append);

	_getPlan((Plan *) local_node);

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

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

350 351 352
	token = lsptok(NULL, &length);		/* eat :inheritrelid */
	token = lsptok(NULL, &length);		/* get inheritrelid */
	local_node->inheritrelid = strtoul(token, NULL, 10);
353

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

357
	return local_node;
358 359 360
}

/* ----------------
361
 *		_getJoin
362 363 364 365 366
 *
 * In case Join is not the same structure as Plan someday.
 * ----------------
 */
static void
367
_getJoin(Join *node)
368
{
369
	_getPlan((Plan *) node);
370 371 372 373
}


/* ----------------
374
 *		_readJoin
375
 *
376
 *	Join is a subclass of Plan
377 378
 * ----------------
 */
379
static Join *
380 381
_readJoin()
{
382
	Join	   *local_node;
383 384 385 386 387

	local_node = makeNode(Join);

	_getJoin(local_node);

388
	return local_node;
389 390 391
}

/* ----------------
392 393 394
 *		_readNestLoop
 *
 *	NestLoop is a subclass of Join
395 396 397 398 399 400
 * ----------------
 */

static NestLoop *
_readNestLoop()
{
401
	NestLoop   *local_node;
402 403 404 405 406

	local_node = makeNode(NestLoop);

	_getJoin((Join *) local_node);

407
	return local_node;
408 409 410
}

/* ----------------
411 412 413
 *		_readMergeJoin
 *
 *	MergeJoin is a subclass of Join
414 415 416 417 418
 * ----------------
 */
static MergeJoin *
_readMergeJoin()
{
419 420 421
	MergeJoin  *local_node;
	char	   *token;
	int			length;
422 423 424 425 426 427 428

	local_node = makeNode(MergeJoin);

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

429
	return local_node;
430 431 432
}

/* ----------------
433 434 435
 *		_readHashJoin
 *
 *	HashJoin is a subclass of Join.
436 437 438 439 440
 * ----------------
 */
static HashJoin *
_readHashJoin()
{
441 442 443
	HashJoin   *local_node;
	char	   *token;
	int			length;
444 445 446 447 448 449 450 451 452 453

	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 */
454
	local_node->hashjoinop = strtoul(token, NULL, 10);
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471

	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;

472
	return local_node;
473 474 475
}

/* ----------------
476
 *		_getScan
477
 *
478 479 480
 *	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.)
481
 *
482
 *	Scan gets its own get function since stuff inherits it.
483 484
 * ----------------
 */
485
static void
486
_getScan(Scan *node)
487
{
488 489
	char	   *token;
	int			length;
490 491 492 493 494

	_getPlan((Plan *) node);

	token = lsptok(NULL, &length);		/* eat :scanrelid */
	token = lsptok(NULL, &length);		/* get scanrelid */
495
	node->scanrelid = strtoul(token, NULL, 10);
496 497 498
}

/* ----------------
499 500
 *		_readScan
 *
501 502 503
 * Scan is a subclass of Plan (Not Node, see above).
 * ----------------
 */
504
static Scan *
505 506
_readScan()
{
507
	Scan	   *local_node;
508 509 510 511 512

	local_node = makeNode(Scan);

	_getScan(local_node);

513
	return local_node;
514 515 516
}

/* ----------------
517 518 519
 *		_readSeqScan
 *
 *	SeqScan is a subclass of Scan
520 521 522 523 524
 * ----------------
 */
static SeqScan *
_readSeqScan()
{
525
	SeqScan    *local_node;
526 527 528 529 530

	local_node = makeNode(SeqScan);

	_getScan((Scan *) local_node);

531
	return local_node;
532 533 534
}

/* ----------------
535 536 537
 *		_readIndexScan
 *
 *	IndexScan is a subclass of Scan
538 539 540 541 542
 * ----------------
 */
static IndexScan *
_readIndexScan()
{
543 544 545
	IndexScan  *local_node;
	char	   *token;
	int			length;
546 547 548 549 550 551

	local_node = makeNode(IndexScan);

	_getScan((Scan *) local_node);

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

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

V
Vadim B. Mikheev 已提交
557 558 559
	token = lsptok(NULL, &length);		/* eat :indxqualorig */
	local_node->indxqualorig = nodeRead(true);		/* now read it */

560
	return local_node;
561 562 563
}

/* ----------------
564
 *		_readNoname
565
 *
566
 *	Noname is a subclass of Plan
567 568
 * ----------------
 */
569 570
static Noname *
_readNoname()
571
{
572
	Noname	   *local_node;
573 574
	char	   *token;
	int			length;
575

576
	local_node = makeNode(Noname);
577 578 579

	_getPlan((Plan *) local_node);

580 581 582
	token = lsptok(NULL, &length);		/* eat :nonameid */
	token = lsptok(NULL, &length);		/* get nonameid */
	local_node->nonameid = atol(token);
583 584 585 586 587

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

588
	return local_node;
589 590 591
}

/* ----------------
592 593
 *		_readSort
 *
594
 *	Sort is a subclass of Noname
595 596
 * ----------------
 */
597
static Sort *
598 599
_readSort()
{
600 601 602
	Sort	   *local_node;
	char	   *token;
	int			length;
603 604 605 606 607

	local_node = makeNode(Sort);

	_getPlan((Plan *) local_node);

608 609 610
	token = lsptok(NULL, &length);		/* eat :nonameid */
	token = lsptok(NULL, &length);		/* get nonameid */
	local_node->nonameid = atol(token);
611 612 613 614 615

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

616
	return local_node;
617 618
}

619
static Agg *
620 621
_readAgg()
{
622 623 624
	Agg		   *local_node;
	char	   *token;
	int			length;
625 626 627 628

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

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

632
	return local_node;
633 634 635
}

/* ----------------
636
 *		_readUnique
637
 *
638
 * For some reason, unique is a subclass of Noname.
639
 */
640
static Unique *
641 642
_readUnique()
{
643 644 645
	Unique	   *local_node;
	char	   *token;
	int			length;
646 647 648 649 650

	local_node = makeNode(Unique);

	_getPlan((Plan *) local_node);

651 652 653
	token = lsptok(NULL, &length);		/* eat :nonameid */
	token = lsptok(NULL, &length);		/* get :nonameid */
	local_node->nonameid = atol(token);
654 655 656 657 658

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

659
	return local_node;
660 661 662
}

/* ----------------
663 664
 *		_readHash
 *
665
 *	Hash is a subclass of Noname
666 667
 * ----------------
 */
668
static Hash *
669 670
_readHash()
{
671 672 673
	Hash	   *local_node;
	char	   *token;
	int			length;
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693

	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;

694
	return local_node;
695 696 697
}

/*
698
 *	Stuff from primnodes.h.
699 700 701
 */

/* ----------------
702 703 704
 *		_readResdom
 *
 *	Resdom is a subclass of Node
705 706
 * ----------------
 */
707
static Resdom *
708 709
_readResdom()
{
710 711 712
	Resdom	   *local_node;
	char	   *token;
	int			length;
713 714 715 716 717 718 719 720 721 722 723

	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);

724 725 726
	token = lsptok(NULL, &length);		/* eat :restypmod */
	token = lsptok(NULL, &length);		/* get restypmod */
	local_node->restypmod = atoi(token);
727 728 729 730

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

731
	if (length == 0)
732 733 734
		local_node->resname = NULL;
	else
	{
735
		local_node->resname = (char *) palloc(length + 1);
736
		StrNCpy(local_node->resname, token + 1, length + 1 - 2);		/* strip quotes */
737 738 739 740
	}

	token = lsptok(NULL, &length);		/* eat :reskey */
	token = lsptok(NULL, &length);		/* get reskey */
741
	local_node->reskey = strtoul(token, NULL, 10);
742 743 744 745 746 747 748 749 750

	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);

751
	return local_node;
752 753 754
}

/* ----------------
755 756 757
 *		_readExpr
 *
 *	Expr is a subclass of Node
758 759
 * ----------------
 */
760
static Expr *
761 762
_readExpr()
{
763 764 765
	Expr	   *local_node;
	char	   *token;
	int			length;
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784

	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 已提交
785 786
	else if (!strncmp(token, "subp", 4))
		local_node->opType = SUBPLAN_EXPR;
787 788 789 790 791 792 793

	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 */

794
	return local_node;
795 796
}

797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
/* ----------------
 *		_readCaseExpr
 *
 *	CaseExpr is a subclass of Node
 * ----------------
 */
static CaseExpr *
_readCaseExpr()
{
	CaseExpr   *local_node;
	char	   *token;
	int			length;

	local_node = makeNode(CaseExpr);

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

	return local_node;
}

/* ----------------
 *		_readCaseWhen
 *
 *	CaseWhen is a subclass of Node
 * ----------------
 */
static CaseWhen *
_readCaseWhen()
{
	CaseWhen   *local_node;
	char	   *token;
	int			length;

	local_node = makeNode(CaseWhen);

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

	return local_node;
}

841
/* ----------------
842 843 844
 *		_readVar
 *
 *	Var is a subclass of Expr
845 846
 * ----------------
 */
847
static Var *
848 849
_readVar()
{
850 851 852
	Var		   *local_node;
	char	   *token;
	int			length;
853 854 855 856 857

	local_node = makeNode(Var);

	token = lsptok(NULL, &length);		/* eat :varno */
	token = lsptok(NULL, &length);		/* get varno */
858
	local_node->varno = strtoul(token, NULL, 10);
859 860 861 862 863 864 865 866 867

	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);

868 869
	token = lsptok(NULL, &length);		/* eat :vartypmod */
	token = lsptok(NULL, &length);		/* get vartypmod */
B
Bruce Momjian 已提交
870
	local_node->vartypmod = atoi(token);
871

872 873 874 875
	token = lsptok(NULL, &length);		/* eat :varlevelsup */
	token = lsptok(NULL, &length);		/* get varlevelsup */
	local_node->varlevelsup = (Oid) atol(token);

876 877 878 879 880 881 882 883
	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);

884
	return local_node;
885 886 887 888 889 890 891 892
}

/* ----------------
 * _readArray
 *
 * Array is a subclass of Expr
 * ----------------
 */
893
static Array *
894 895
_readArray()
{
896 897 898
	Array	   *local_node;
	char	   *token;
	int			length;
899 900 901 902 903

	local_node = makeNode(Array);

	token = lsptok(NULL, &length);		/* eat :arrayelemtype */
	token = lsptok(NULL, &length);		/* get arrayelemtype */
904
	local_node->arrayelemtype = strtoul(token, NULL, 10);
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925

	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);

926
	return local_node;
927 928 929 930 931 932 933 934 935 936 937
}

/* ----------------
 * _readArrayRef
 *
 * ArrayRef is a subclass of Expr
 * ----------------
 */
static ArrayRef *
_readArrayRef()
{
938 939 940
	ArrayRef   *local_node;
	char	   *token;
	int			length;
941 942 943 944 945

	local_node = makeNode(ArrayRef);

	token = lsptok(NULL, &length);		/* eat :refelemtype */
	token = lsptok(NULL, &length);		/* get refelemtype */
946
	local_node->refelemtype = strtoul(token, NULL, 10);
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

	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);

972
	return local_node;
973 974 975
}

/* ----------------
976 977 978
 *		_readConst
 *
 *	Const is a subclass of Expr
979 980
 * ----------------
 */
981
static Const *
982 983
_readConst()
{
984 985 986
	Const	   *local_node;
	char	   *token;
	int			length;
987 988 989 990 991 992 993 994 995 996

	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 */
997
	local_node->constlen = strtol(token, NULL, 10);
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 1026 1027 1028 1029 1030

	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;

1031
	return local_node;
1032 1033 1034
}

/* ----------------
1035 1036 1037
 *		_readFunc
 *
 *	Func is a subclass of Expr
1038 1039
 * ----------------
 */
1040
static Func *
1041 1042
_readFunc()
{
1043 1044 1045
	Func	   *local_node;
	char	   *token;
	int			length;
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080

	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 */

1081
	return local_node;
1082 1083 1084
}

/* ----------------
1085 1086 1087
 *		_readOper
 *
 *	Oper is a subclass of Expr
1088 1089
 * ----------------
 */
1090
static Oper *
1091 1092
_readOper()
{
1093 1094 1095
	Oper	   *local_node;
	char	   *token;
	int			length;
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116

	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;

1117
	return local_node;
1118 1119 1120
}

/* ----------------
1121 1122 1123
 *		_readParam
 *
 *	Param is a subclass of Expr
1124 1125
 * ----------------
 */
1126
static Param *
1127 1128
_readParam()
{
1129 1130 1131
	Param	   *local_node;
	char	   *token;
	int			length;
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144

	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 */
1145 1146 1147 1148 1149
	if (length == 0)
		local_node->paramname = NULL;
	else
	{
		local_node->paramname = (char *) palloc(length + 1);
1150
		StrNCpy(local_node->paramname, token, length + 1);
1151
	}
1152 1153 1154 1155 1156 1157 1158 1159

	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 */

1160
	return local_node;
1161 1162
}

1163
/* ----------------
B
Bruce Momjian 已提交
1164
 *		_readAggref
1165
 *
B
Bruce Momjian 已提交
1166
 *	Aggref is a subclass of Node
1167 1168
 * ----------------
 */
B
Bruce Momjian 已提交
1169 1170
static Aggref *
_readAggref()
1171
{
B
Bruce Momjian 已提交
1172
	Aggref	   *local_node;
1173 1174
	char	   *token;
	int			length;
1175

B
Bruce Momjian 已提交
1176
	local_node = makeNode(Aggref);
1177 1178 1179 1180

	token = lsptok(NULL, &length);		/* eat :aggname */
	token = lsptok(NULL, &length);		/* get aggname */
	local_node->aggname = (char *) palloc(length + 1);
1181
	StrNCpy(local_node->aggname, token, length + 1);
1182 1183 1184 1185 1186 1187 1188 1189 1190

	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);

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

1194 1195 1196 1197
	token = lsptok(NULL, &length);		/* eat :aggno */
	token = lsptok(NULL, &length);		/* get aggno */
	local_node->aggno = atoi(token);

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

1202
	return local_node;
1203 1204
}

1205 1206 1207 1208 1209 1210 1211 1212 1213
/* ----------------
 *		_readSubLink
 *
 *	SubLink is a subclass of Node
 * ----------------
 */
static SubLink *
_readSubLink()
{
1214
	SubLink    *local_node;
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
	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 */
1232
	local_node->oper = nodeRead(true);	/* now read it */
1233 1234 1235 1236

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

1237
	return local_node;
1238 1239
}

1240
/*
1241
 *	Stuff from execnodes.h
1242 1243 1244
 */

/* ----------------
1245 1246 1247
 *		_readEState
 *
 *	EState is a subclass of Node.
1248 1249
 * ----------------
 */
1250
static EState *
1251 1252
_readEState()
{
1253 1254 1255
	EState	   *local_node;
	char	   *token;
	int			length;
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273

	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);

1274
	return local_node;
1275 1276 1277
}

/*
1278
 *	Stuff from relation.h
1279 1280 1281
 */

/* ----------------
B
Bruce Momjian 已提交
1282
 *		_readRelOptInfo
1283 1284
 * ----------------
 */
B
Bruce Momjian 已提交
1285
static RelOptInfo *
B
Bruce Momjian 已提交
1286
_readRelOptInfo()
1287
{
1288
	RelOptInfo *local_node;
1289 1290
	char	   *token;
	int			length;
1291

B
Bruce Momjian 已提交
1292
	local_node = makeNode(RelOptInfo);
1293 1294

	token = lsptok(NULL, &length);		/* get :relids */
1295
	local_node->relids = toIntList(nodeRead(true));		/* now read it */
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339

	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 :cheapestpath */
	token = lsptok(NULL, &length);		/* get @ */
	token = lsptok(NULL, &length);		/* now read it */

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


1340 1341
	token = lsptok(NULL, &length);		/* get :restrictinfo */
	local_node->restrictinfo = nodeRead(true);	/* now read it */
1342 1343 1344 1345 1346 1347 1348

	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 */

1349
	return local_node;
1350 1351 1352
}

/* ----------------
1353
 *		_readTargetEntry
1354 1355 1356 1357 1358
 * ----------------
 */
static TargetEntry *
_readTargetEntry()
{
1359 1360 1361
	TargetEntry *local_node;
	char	   *token;
	int			length;
1362

1363
	local_node = makeNode(TargetEntry);
1364

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

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

1371
	return local_node;
1372 1373 1374
}

/* ----------------
1375
 *		_readRangeTblEntry
1376 1377 1378 1379 1380
 * ----------------
 */
static RangeTblEntry *
_readRangeTblEntry()
{
1381 1382 1383
	RangeTblEntry *local_node;
	char	   *token;
	int			length;
1384

1385
	local_node = makeNode(RangeTblEntry);
1386

1387 1388
	token = lsptok(NULL, &length);		/* eat :relname */
	token = lsptok(NULL, &length);		/* get :relname */
1389
	if (length == 0)
1390 1391 1392
		local_node->relname = NULL;
	else
	{
1393
		local_node->relname = (char *) palloc(length + 1);
1394
		StrNCpy(local_node->relname, token, length + 1);
1395 1396 1397 1398
	}

	token = lsptok(NULL, &length);		/* eat :refname */
	token = lsptok(NULL, &length);		/* get :refname */
1399
	if (length == 0)
1400 1401 1402
		local_node->refname = NULL;
	else
	{
1403
		local_node->refname = (char *) palloc(length + 1);
1404
		StrNCpy(local_node->refname, token, length + 1);
1405 1406 1407 1408
	}

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

1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422
	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;

1423
	return local_node;
1424 1425
}

1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443
static RowMark *
_readRowMark()
{
	RowMark	   *local_node = makeNode(RowMark);
	char	   *token;
	int			length;

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

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

	return local_node;
}

1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491
/* ----------------
 *		_readPathOrder
 *
 *	PathOrder is part of Path and it's subclasses.
 * ----------------
 */
static PathOrder *
_readPathOrder()
{
	PathOrder   *local_node;
	char	   *token;
	int			length;

	local_node = makeNode(PathOrder);

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

	if (local_node->ordtype == SORTOP_ORDER)
	{
		token = lsptok(NULL, &length);		/* get :sortop */

		if (length == 0)
			local_node->ord.sortop = NULL;
		else
		{
			int i = -1;
	
			local_node->ord.sortop = palloc(sizeof(Oid) * (INDEX_MAX_KEYS+1));
	
			do {
				i++;
				Assert(i <= INDEX_MAX_KEYS);
				token = lsptok(NULL, &length);	/* now read it */
				local_node->ord.sortop[i] = strtoul(token, NULL, 10);
			} while (local_node->ord.sortop[i] != 0);
		}
	}
	else
	{
		token = lsptok(NULL, &length);		/* get :merge */
		local_node->ord.merge = nodeRead(true); /* now read it */
	}

	return local_node;
}

1492
/* ----------------
1493 1494 1495
 *		_readPath
 *
 *	Path is a subclass of Node.
1496 1497
 * ----------------
 */
1498
static Path *
1499 1500
_readPath()
{
1501 1502 1503
	Path	   *local_node;
	char	   *token;
	int			length;
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514

	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);

B
Bruce Momjian 已提交
1515 1516
	token = lsptok(NULL, &length);		/* get :pathorder */
	local_node->pathorder = nodeRead(true); /* now read it */
1517

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

1521
	return local_node;
1522 1523 1524
}

/* ----------------
1525 1526 1527
 *		_readIndexPath
 *
 *	IndexPath is a subclass of Path.
1528 1529 1530 1531 1532
 * ----------------
 */
static IndexPath *
_readIndexPath()
{
1533 1534 1535
	IndexPath  *local_node;
	char	   *token;
	int			length;
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546

	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);

B
Bruce Momjian 已提交
1547 1548
	token = lsptok(NULL, &length);		/* get :pathorder */
 	local_node->path.pathorder = nodeRead(true);		/* now read it */
1549

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

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

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

1559
	return local_node;
1560 1561 1562
}

/* ----------------
1563
 *		_readNestPath
1564
 *
1565
 *	NestPath is a subclass of Path
1566 1567
 * ----------------
 */
1568 1569
static NestPath *
_readNestPath()
1570
{
1571
	NestPath   *local_node;
1572 1573
	char	   *token;
	int			length;
1574 1575


1576
	local_node = makeNode(NestPath);
1577 1578 1579 1580 1581 1582 1583 1584 1585

	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);

B
Bruce Momjian 已提交
1586 1587
	token = lsptok(NULL, &length);		/* get :pathorder */
	local_node->path.pathorder = nodeRead(true);		/* now read it */
1588

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

1592 1593
	token = lsptok(NULL, &length);		/* get :pathinfo */
	local_node->pathinfo = nodeRead(true);		/* now read it */
1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620

	/*
	 * 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 */
1621
	local_node->path.joinid = toIntList(nodeRead(true));		/* now read it */
1622

1623
	return local_node;
1624 1625 1626
}

/* ----------------
1627 1628
 *		_readMergePath
 *
1629
 *	MergePath is a subclass of NestPath.
1630 1631 1632 1633 1634
 * ----------------
 */
static MergePath *
_readMergePath()
{
1635 1636 1637
	MergePath  *local_node;
	char	   *token;
	int			length;
1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650

	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);

B
Bruce Momjian 已提交
1651 1652
	token = lsptok(NULL, &length);		/* get :pathorder */
	local_node->jpath.path.pathorder = nodeRead(true); /* now read it */
1653

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

1657 1658
	token = lsptok(NULL, &length);		/* get :pathinfo */
	local_node->jpath.pathinfo = nodeRead(true);	/* now read it */
1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685

	/*
	 * 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 */
1686
	local_node->jpath.path.joinid = toIntList(nodeRead(true));		/* now read it */
1687 1688 1689 1690 1691 1692 1693 1694 1695 1696

	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 */

1697
	return local_node;
1698 1699 1700
}

/* ----------------
1701 1702
 *		_readHashPath
 *
1703
 *	HashPath is a subclass of NestPath.
1704 1705 1706 1707 1708
 * ----------------
 */
static HashPath *
_readHashPath()
{
1709 1710 1711
	HashPath   *local_node;
	char	   *token;
	int			length;
1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724

	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);

B
Bruce Momjian 已提交
1725 1726
	token = lsptok(NULL, &length);		/* get :pathorder */
	local_node->jpath.path.pathorder = nodeRead(true); /* now read it */
1727

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

1731 1732
	token = lsptok(NULL, &length);		/* get :pathinfo */
	local_node->jpath.pathinfo = nodeRead(true);	/* now read it */
1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759

	/*
	 * 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 */
1760
	local_node->jpath.path.joinid = toIntList(nodeRead(true));		/* now read it */
1761 1762 1763 1764 1765 1766 1767 1768 1769 1770

	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 */

1771
	return local_node;
1772 1773 1774
}

/* ----------------
1775 1776 1777
 *		_readOrderKey
 *
 *	OrderKey is a subclass of Node.
1778 1779 1780 1781 1782
 * ----------------
 */
static OrderKey *
_readOrderKey()
{
1783 1784 1785
	OrderKey   *local_node;
	char	   *token;
	int			length;
1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796

	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 */

1797
	local_node->array_index = strtoul(token, NULL, 10);
1798

1799
	return local_node;
1800 1801 1802
}

/* ----------------
1803 1804 1805
 *		_readJoinKey
 *
 *	JoinKey is a subclass of Node.
1806 1807 1808 1809 1810
 * ----------------
 */
static JoinKey *
_readJoinKey()
{
1811 1812 1813
	JoinKey    *local_node;
	char	   *token;
	int			length;
1814 1815 1816 1817 1818 1819 1820 1821 1822

	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 */

1823
	return local_node;
1824 1825 1826
}

/* ----------------
1827 1828 1829
 *		_readMergeOrder
 *
 *	MergeOrder is a subclass of Node.
1830 1831 1832 1833 1834
 * ----------------
 */
static MergeOrder *
_readMergeOrder()
{
1835 1836 1837
	MergeOrder *local_node;
	char	   *token;
	int			length;
1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864

	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);

1865
	return local_node;
1866 1867 1868
}

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

1881
	local_node = makeNode(RestrictInfo);
1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901

	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 */

1902 1903
	token = lsptok(NULL, &length);		/* get :mergejoinorder */
	local_node->mergejoinorder = (MergeOrder *) nodeRead(true);
1904 1905 1906 1907 1908 1909

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

	local_node->hashjoinoperator = atol(token);

1910
	return local_node;
1911 1912 1913
}

/* ----------------
1914 1915 1916
 *		_readJoinMethod
 *
 *	JoinMethod is a subclass of Node.
1917 1918 1919 1920 1921
 * ----------------
 */
static JoinMethod *
_readJoinMethod()
{
1922 1923 1924
	JoinMethod *local_node;
	char	   *token;
	int			length;
1925 1926 1927 1928 1929 1930 1931 1932 1933

	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 */

1934
	return local_node;
1935 1936 1937
}

/* ----------------
1938
 *		_readHashInfo
1939
 *
1940
 * HashInfo is a subclass of JoinMethod.
1941 1942
 * ----------------
 */
1943 1944
static HashInfo *
_readHashInfo()
1945
{
B
Bruce Momjian 已提交
1946
	HashInfo	*local_node;
1947 1948
	char	   *token;
	int			length;
1949

1950
	local_node = makeNode(HashInfo);
1951 1952 1953 1954

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

1955
	local_node->hashop = strtoul(token, NULL, 10);
1956 1957 1958 1959 1960 1961 1962

	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 */

1963
	return local_node;
1964 1965 1966
}

/* ----------------
1967
 *		_readJoinInfo()
1968
 *
1969
 *	JoinInfo is a subclass of Node.
1970 1971
 * ----------------
 */
1972 1973
static JoinInfo *
_readJoinInfo()
1974
{
1975
	JoinInfo   *local_node;
1976 1977
	char	   *token;
	int			length;
1978

1979
	local_node = makeNode(JoinInfo);
1980

B
Bruce Momjian 已提交
1981 1982
	token = lsptok(NULL, &length);		/* get :unjoined_relids */
	local_node->unjoined_relids = toIntList(nodeRead(true));		/* now read it */
1983

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

1987
	token = lsptok(NULL, &length);		/* get :mergejoinable */
1988 1989

	if (!strncmp(token, "true", 4))
1990
		local_node->mergejoinable = true;
1991
	else
1992
		local_node->mergejoinable = false;
1993 1994 1995 1996 1997 1998 1999 2000

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

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

2001
	return local_node;
2002 2003 2004
}

/* ----------------
2005
 *		_readIter()
2006 2007 2008
 *
 * ----------------
 */
2009
static Iter *
2010 2011
_readIter()
{
2012 2013 2014
	Iter	   *local_node;
	char	   *token;
	int			length;
2015 2016 2017 2018 2019 2020

	local_node = makeNode(Iter);

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

2021
	return local_node;
2022 2023 2024 2025
}


/* ----------------
2026
 *		parsePlanString
2027 2028 2029 2030 2031 2032 2033
 *
 * 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.
 * ----------------
 */
2034
Node *
2035
parsePlanString(void)
2036
{
2037 2038 2039
	char	   *token;
	int			length;
	void	   *return_value = NULL;
2040 2041 2042

	token = lsptok(NULL, &length);

2043
	if (!strncmp(token, "PLAN", length))
2044
		return_value = _readPlan();
2045
	else if (!strncmp(token, "RESULT", length))
2046
		return_value = _readResult();
2047
	else if (!strncmp(token, "APPEND", length))
2048
		return_value = _readAppend();
2049
	else if (!strncmp(token, "JOIN", length))
2050
		return_value = _readJoin();
2051
	else if (!strncmp(token, "NESTLOOP", length))
2052
		return_value = _readNestLoop();
2053
	else if (!strncmp(token, "MERGEJOIN", length))
2054
		return_value = _readMergeJoin();
2055
	else if (!strncmp(token, "HASHJOIN", length))
2056
		return_value = _readHashJoin();
2057
	else if (!strncmp(token, "SCAN", length))
2058
		return_value = _readScan();
2059
	else if (!strncmp(token, "SEQSCAN", length))
2060
		return_value = _readSeqScan();
2061
	else if (!strncmp(token, "INDEXSCAN", length))
2062
		return_value = _readIndexScan();
2063 2064
	else if (!strncmp(token, "NONAME", length))
		return_value = _readNoname();
2065
	else if (!strncmp(token, "SORT", length))
2066
		return_value = _readSort();
2067
	else if (!strncmp(token, "AGGREG", length))
B
Bruce Momjian 已提交
2068
		return_value = _readAggref();
2069
	else if (!strncmp(token, "SUBLINK", length))
2070
		return_value = _readSubLink();
2071
	else if (!strncmp(token, "AGG", length))
2072
		return_value = _readAgg();
2073
	else if (!strncmp(token, "UNIQUE", length))
2074
		return_value = _readUnique();
2075
	else if (!strncmp(token, "HASH", length))
2076
		return_value = _readHash();
2077
	else if (!strncmp(token, "RESDOM", length))
2078
		return_value = _readResdom();
2079
	else if (!strncmp(token, "EXPR", length))
2080
		return_value = _readExpr();
2081
	else if (!strncmp(token, "ARRAYREF", length))
2082
		return_value = _readArrayRef();
2083
	else if (!strncmp(token, "ARRAY", length))
2084
		return_value = _readArray();
2085
	else if (!strncmp(token, "VAR", length))
2086
		return_value = _readVar();
2087
	else if (!strncmp(token, "CONST", length))
2088
		return_value = _readConst();
2089
	else if (!strncmp(token, "FUNC", length))
2090
		return_value = _readFunc();
2091
	else if (!strncmp(token, "OPER", length))
2092
		return_value = _readOper();
2093
	else if (!strncmp(token, "PARAM", length))
2094
		return_value = _readParam();
2095
	else if (!strncmp(token, "ESTATE", length))
2096
		return_value = _readEState();
B
Bruce Momjian 已提交
2097
	else if (!strncmp(token, "RELOPTINFO", length))
B
Bruce Momjian 已提交
2098
		return_value = _readRelOptInfo();
B
Bruce Momjian 已提交
2099
	else if (!strncmp(token, "TARGETENTRY", length))
2100
		return_value = _readTargetEntry();
2101
	else if (!strncmp(token, "RTE", length))
2102
		return_value = _readRangeTblEntry();
2103 2104
	else if (!strncmp(token, "PATHORDER", length))
		return_value = _readPathOrder();
2105
	else if (!strncmp(token, "PATH", length))
2106
		return_value = _readPath();
2107
	else if (!strncmp(token, "INDEXPATH", length))
2108
		return_value = _readIndexPath();
2109 2110
	else if (!strncmp(token, "NESTPATH", length))
		return_value = _readNestPath();
2111
	else if (!strncmp(token, "MERGEPATH", length))
2112
		return_value = _readMergePath();
2113
	else if (!strncmp(token, "HASHPATH", length))
2114
		return_value = _readHashPath();
2115
	else if (!strncmp(token, "ORDERKEY", length))
2116
		return_value = _readOrderKey();
2117
	else if (!strncmp(token, "JOINKEY", length))
2118
		return_value = _readJoinKey();
2119
	else if (!strncmp(token, "MERGEORDER", length))
2120
		return_value = _readMergeOrder();
B
Bruce Momjian 已提交
2121
	else if (!strncmp(token, "RETRICTINFO", length))
2122
		return_value = _readRestrictInfo();
2123
	else if (!strncmp(token, "JOINMETHOD", length))
2124
		return_value = _readJoinMethod();
2125 2126
	else if (!strncmp(token, "JOININFO", length))
		return_value = _readJoinInfo();
B
Bruce Momjian 已提交
2127
	else if (!strncmp(token, "HASHINFO", length))
2128
		return_value = _readHashInfo();
2129
	else if (!strncmp(token, "ITER", length))
2130
		return_value = _readIter();
2131
	else if (!strncmp(token, "QUERY", length))
2132
		return_value = _readQuery();
2133
	else if (!strncmp(token, "SORTCLAUSE", length))
2134
		return_value = _readSortClause();
2135
	else if (!strncmp(token, "GROUPCLAUSE", length))
2136
		return_value = _readGroupClause();
2137 2138 2139 2140
	else if (!strncmp(token, "CASE", length))
		return_value = _readCaseExpr();
	else if (!strncmp(token, "WHEN", length))
		return_value = _readCaseWhen();
2141 2142
	else if (!strncmp(token, "ROWMARK", length))
		return_value = _readRowMark();
2143
	else
2144
		elog(ERROR, "badly formatted planstring \"%.10s\"...\n", token);
2145

2146
	return (Node *) return_value;
2147
}
2148

2149 2150 2151
/*------------------------------------------------------------*/

/* ----------------
2152
 *		readDatum
2153 2154 2155 2156 2157
 *
 * given a string representation of the value of the given type,
 * create the appropriate Datum
 * ----------------
 */
2158
static Datum
2159 2160
readDatum(Oid type)
{
2161 2162 2163 2164 2165 2166 2167
	int			length;
	int			tokenLength;
	char	   *token;
	bool		byValue;
	Datum		res;
	char	   *s;
	int			i;
2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180

	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))
2181
			elog(ERROR, "readValue: byval & length = %d", length);
2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204
		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] != ']')
2205
		elog(ERROR, "readValue: ']' expected, length =%d", length);
2206

2207
	return res;
2208
}