equalfuncs.c 14.5 KB
Newer Older
1 2 3
/*-------------------------------------------------------------------------
 *
 * equalfuncs.c--
4
 *	  equal functions to compare the nodes
5 6 7 8 9
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
10
 *	  $Header: /cvsroot/pgsql/src/backend/nodes/equalfuncs.c,v 1.8 1997/09/08 02:23:36 momjian Exp $
11 12 13
 *
 *-------------------------------------------------------------------------
 */
14 15
#include <string.h>

16 17 18 19 20 21 22 23 24 25 26 27 28
#include "postgres.h"

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

#include "utils/builtins.h"		/* for namestrcmp() */
#include "utils/datum.h"
#include "utils/elog.h"
#include "storage/itemptr.h"

29
static bool equali(List * a, List * b);
30

31
/*
32
 *	Stuff from primnodes.h
33 34 35
 */

/*
36
 *	Resdom is a subclass of Node.
37
 */
38
static bool
39
_equalResdom(Resdom * a, Resdom * b)
40
{
41 42 43 44 45 46 47 48 49 50 51 52 53 54
	if (a->resno != b->resno)
		return (false);
	if (a->restype != b->restype)
		return (false);
	if (a->reslen != b->reslen)
		return (false);
	if (strcmp(a->resname, b->resname) != 0)
		return (false);
	if (a->reskey != b->reskey)
		return (false);
	if (a->reskeyop != b->reskeyop)
		return (false);

	return (true);
55 56
}

57
static bool
58
_equalFjoin(Fjoin * a, Fjoin * b)
59
{
60
	int			nNodes;
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

	if (a->fj_initialized != b->fj_initialized)
		return (false);
	if (a->fj_nNodes != b->fj_nNodes)
		return (false);
	if (!equal(a->fj_innerNode, b->fj_innerNode))
		return (false);

	nNodes = a->fj_nNodes;
	if (memcmp(a->fj_results, b->fj_results, nNodes * sizeof(Datum)) != 0)
		return (false);
	if (memcmp(a->fj_alwaysDone, b->fj_alwaysDone, nNodes * sizeof(bool)) != 0)
		return (false);

	return (true);
76 77 78
}

/*
79
 *	Expr is a subclass of Node.
80
 */
81
static bool
82
_equalExpr(Expr * a, Expr * b)
83
{
84 85 86 87 88 89 90 91
	if (a->opType != b->opType)
		return (false);
	if (!equal(a->oper, b->oper))
		return (false);
	if (!equal(a->args, b->args))
		return (false);

	return (true);
92 93
}

94
static bool
95
_equalIter(Iter * a, Iter * b)
96
{
97
	return (equal(a->iterexpr, b->iterexpr));
98 99
}

100
static bool
101
_equalStream(Stream * a, Stream * b)
102
{
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
	if (a->clausetype != b->clausetype)
		return (false);
	if (a->groupup != b->groupup)
		return (false);
	if (a->groupcost != b->groupcost)
		return (false);
	if (a->groupsel != b->groupsel)
		return (false);
	if (!equal(a->pathptr, b->pathptr))
		return (false);
	if (!equal(a->cinfo, b->cinfo))
		return (false);
	if (!equal(a->upstream, b->upstream))
		return (false);
	return (equal(a->downstream, b->downstream));
118 119 120
}

/*
121
 *	Var is a subclass of Expr.
122
 */
123
static bool
124
_equalVar(Var * a, Var * b)
125
{
126 127 128 129 130 131 132 133 134 135 136 137
	if (a->varno != b->varno)
		return (false);
	if (a->varattno != b->varattno)
		return (false);
	if (a->vartype != b->vartype)
		return (false);
	if (a->varnoold != b->varnoold)
		return (false);
	if (a->varoattno != b->varoattno)
		return (false);

	return (true);
138 139
}

140
static bool
141
_equalArray(Array * a, Array * b)
142
{
143 144 145 146 147 148 149 150 151 152 153
	if (a->arrayelemtype != b->arrayelemtype)
		return (false);
	if (a->arrayndim != b->arrayndim)
		return (false);
	if (a->arraylow.indx[0] != b->arraylow.indx[0])
		return (false);
	if (a->arrayhigh.indx[0] != b->arrayhigh.indx[0])
		return (false);
	if (a->arraylen != b->arraylen)
		return (false);
	return (TRUE);
154 155
}

156
static bool
157
_equalArrayRef(ArrayRef * a, ArrayRef * b)
158
{
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
	if (a->refelemtype != b->refelemtype)
		return (false);
	if (a->refattrlength != b->refattrlength)
		return (false);
	if (a->refelemlength != b->refelemlength)
		return (false);
	if (a->refelembyval != b->refelembyval)
		return (false);
	if (!equal(a->refupperindexpr, b->refupperindexpr))
		return (false);
	if (!equal(a->reflowerindexpr, b->reflowerindexpr))
		return (false);
	if (!equal(a->refexpr, b->refexpr))
		return (false);
	return (equal(a->refassgnexpr, b->refassgnexpr));
174 175 176
}

/*
177
 *	Oper is a subclass of Expr.
178
 */
179
static bool
180
_equalOper(Oper * a, Oper * b)
181
{
182 183 184 185 186 187
	if (a->opno != b->opno)
		return (false);
	if (a->opresulttype != b->opresulttype)
		return (false);

	return (true);
188 189 190
}

/*
191
 *	Const is a subclass of Expr.
192
 */
193
static bool
194
_equalConst(Const * a, Const * b)
195
{
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210

	/*
	 * * this function used to do a pointer compare on a and b.  That's *
	 * ridiculous.	-- JMH, 7/11/92
	 */
	if (a->consttype != b->consttype)
		return (false);
	if (a->constlen != b->constlen)
		return (false);
	if (a->constisnull != b->constisnull)
		return (false);
	if (a->constbyval != b->constbyval)
		return (false);
	return (datumIsEqual(a->constvalue, b->constvalue,
						 a->consttype, a->constbyval, a->constlen));
211 212 213
}

/*
214
 *	Param is a subclass of Expr.
215
 */
216
static bool
217
_equalParam(Param * a, Param * b)
218
{
219 220 221 222 223 224 225 226 227
	if (a->paramkind != b->paramkind)
		return (false);
	if (a->paramtype != b->paramtype)
		return (false);
	if (!equal(a->param_tlist, b->param_tlist))
		return (false);

	switch (a->paramkind)
	{
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
		case PARAM_NAMED:
		case PARAM_NEW:
		case PARAM_OLD:
			if (strcmp(a->paramname, b->paramname) != 0)
				return (false);
			break;
		case PARAM_NUM:
			if (a->paramid != b->paramid)
				return (false);
			break;
		case PARAM_INVALID:

			/*
			 * XXX: Hmmm... What are we supposed to return in this case ??
			 */
			return (true);
			break;
		default:
			elog(WARN, "_equalParam: Invalid paramkind value: %d",
				 a->paramkind);
248 249 250
	}

	return (true);
251 252 253
}

/*
254
 *	Func is a subclass of Expr.
255
 */
256
static bool
257
_equalFunc(Func * a, Func * b)
258
{
259 260 261 262 263 264 265 266 267 268 269 270 271 272
	if (a->funcid != b->funcid)
		return (false);
	if (a->functype != b->functype)
		return (false);
	if (a->funcisindex != b->funcisindex)
		return (false);
	if (a->funcsize != b->funcsize)
		return (false);
	if (!equal(a->func_tlist, b->func_tlist))
		return (false);
	if (!equal(a->func_planlist, b->func_planlist))
		return (false);

	return (true);
273 274 275 276 277
}

/*
 * CInfo is a subclass of Node.
 */
278
static bool
279
_equalCInfo(CInfo * a, CInfo * b)
280
{
281 282 283 284 285 286 287 288 289
	Assert(IsA(a, CInfo));
	Assert(IsA(b, CInfo));

	if (!equal(a->clause, b->clause))
		return (false);
	if (a->selectivity != b->selectivity)
		return (false);
	if (a->notclause != b->notclause)
		return (false);
290
#ifdef EqualMergeOrderExists
291 292
	if (!EqualMergeOrder(a->mergesortorder, b->mergesortorder))
		return (false);
293
#endif
294 295 296 297
	if (a->hashjoinoperator != b->hashjoinoperator)
		return (false);
	return (equal((a->indexids),
				  (b->indexids)));
298 299
}

300
static bool
301
_equalJoinMethod(JoinMethod * a, JoinMethod * b)
302
{
303 304 305 306 307 308 309 310 311 312
	Assert(IsA(a, JoinMethod));
	Assert(IsA(b, JoinMethod));

	if (!equal((a->jmkeys),
			   (b->jmkeys)))
		return (false);
	if (!equal((a->clauses),
			   (b->clauses)))
		return (false);
	return (true);
313 314
}

315
static bool
316
_equalPath(Path * a, Path * b)
317
{
318 319 320 321 322 323 324 325 326 327
	if (a->pathtype != b->pathtype)
		return (false);
	if (a->parent != b->parent)
		return (false);

	/*
	 * if (a->path_cost != b->path_cost) return(false);
	 */
	if (a->p_ordering.ordtype == SORTOP_ORDER)
	{
328
		int			i = 0;
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355

		if (a->p_ordering.ord.sortop == NULL ||
			b->p_ordering.ord.sortop == NULL)
		{

			if (a->p_ordering.ord.sortop != b->p_ordering.ord.sortop)
				return false;
		}
		else
		{
			while (a->p_ordering.ord.sortop[i] != 0 &&
				   b->p_ordering.ord.sortop[i] != 0)
			{
				if (a->p_ordering.ord.sortop[i] != b->p_ordering.ord.sortop[i])
					return false;
				i++;
			}
			if (a->p_ordering.ord.sortop[i] != 0 ||
				b->p_ordering.ord.sortop[i] != 0)
				return false;
		}
	}
	else
	{
		if (!equal((a->p_ordering.ord.merge),
				   (b->p_ordering.ord.merge)))
			return (false);
356
	}
357 358 359 360 361 362 363 364 365 366 367
	if (!equal((a->keys),
			   (b->keys)))
		return (false);

	/*
	 * if (a->outerjoincost != b->outerjoincost) return(false);
	 */
	if (!equali((a->joinid),
				(b->joinid)))
		return (false);
	return (true);
368 369
}

370
static bool
371
_equalIndexPath(IndexPath * a, IndexPath * b)
372
{
373 374 375 376 377 378 379
	if (!_equalPath((Path *) a, (Path *) b))
		return (false);
	if (!equali((a->indexid), (b->indexid)))
		return (false);
	if (!equal((a->indexqual), (b->indexqual)))
		return (false);
	return (true);
380 381
}

382
static bool
383
_equalJoinPath(JoinPath * a, JoinPath * b)
384
{
385 386 387 388 389 390 391 392 393 394 395 396
	Assert(IsA_JoinPath(a));
	Assert(IsA_JoinPath(b));

	if (!_equalPath((Path *) a, (Path *) b))
		return (false);
	if (!equal((a->pathclauseinfo), (b->pathclauseinfo)))
		return (false);
	if (!equal((a->outerjoinpath), (b->outerjoinpath)))
		return (false);
	if (!equal((a->innerjoinpath), (b->innerjoinpath)))
		return (false);
	return (true);
397 398
}

399
static bool
400
_equalMergePath(MergePath * a, MergePath * b)
401
{
402 403 404 405 406 407 408 409 410 411 412 413
	Assert(IsA(a, MergePath));
	Assert(IsA(b, MergePath));

	if (!_equalJoinPath((JoinPath *) a, (JoinPath *) b))
		return (false);
	if (!equal((a->path_mergeclauses), (b->path_mergeclauses)))
		return (false);
	if (!equal((a->outersortkeys), (b->outersortkeys)))
		return (false);
	if (!equal((a->innersortkeys), (b->innersortkeys)))
		return (false);
	return (true);
414 415
}

416
static bool
417
_equalHashPath(HashPath * a, HashPath * b)
418
{
419 420 421 422 423 424 425 426 427 428 429 430
	Assert(IsA(a, HashPath));
	Assert(IsA(b, HashPath));

	if (!_equalJoinPath((JoinPath *) a, (JoinPath *) b))
		return (false);
	if (!equal((a->path_hashclauses), (b->path_hashclauses)))
		return (false);
	if (!equal((a->outerhashkeys), (b->outerhashkeys)))
		return (false);
	if (!equal((a->innerhashkeys), (b->innerhashkeys)))
		return (false);
	return (true);
431 432
}

433
static bool
434
_equalJoinKey(JoinKey * a, JoinKey * b)
435
{
436 437 438 439 440 441 442 443
	Assert(IsA(a, JoinKey));
	Assert(IsA(b, JoinKey));

	if (!equal((a->outer), (b->outer)))
		return (false);
	if (!equal((a->inner), (b->inner)))
		return (false);
	return (true);
444 445
}

446
static bool
447
_equalMergeOrder(MergeOrder * a, MergeOrder * b)
448
{
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
	if (a == (MergeOrder *) NULL && b == (MergeOrder *) NULL)
		return (true);
	Assert(IsA(a, MergeOrder));
	Assert(IsA(b, MergeOrder));

	if (a->join_operator != b->join_operator)
		return (false);
	if (a->left_operator != b->left_operator)
		return (false);
	if (a->right_operator != b->right_operator)
		return (false);
	if (a->left_type != b->left_type)
		return (false);
	if (a->right_type != b->right_type)
		return (false);
	return (true);
465 466
}

467
static bool
468
_equalHInfo(HInfo * a, HInfo * b)
469
{
470 471 472 473 474 475
	Assert(IsA(a, HInfo));
	Assert(IsA(b, HInfo));

	if (a->hashop != b->hashop)
		return (false);
	return (true);
476 477
}

478 479
/* XXX	This equality function is a quick hack, should be
 *		fixed to compare all fields.
480
 */
481
static bool
482
_equalIndexScan(IndexScan * a, IndexScan * b)
483
{
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
	Assert(IsA(a, IndexScan));
	Assert(IsA(b, IndexScan));

	/*
	 * if(a->scan.plan.cost != b->scan.plan.cost) return(false);
	 */

	if (!equal((a->indxqual), (b->indxqual)))
		return (false);

	if (a->scan.scanrelid != b->scan.scanrelid)
		return (false);

	if (!equali((a->indxid), (b->indxid)))
		return (false);
	return (true);
500 501
}

502
static bool
503
_equalJInfo(JInfo * a, JInfo * b)
504
{
505 506 507 508 509 510 511 512 513 514 515
	Assert(IsA(a, JInfo));
	Assert(IsA(b, JInfo));
	if (!equal((a->otherrels), (b->otherrels)))
		return (false);
	if (!equal((a->jinfoclauseinfo), (b->jinfoclauseinfo)))
		return (false);
	if (a->mergesortable != b->mergesortable)
		return (false);
	if (a->hashjoinable != b->hashjoinable)
		return (false);
	return (true);
516 517 518
}

/*
519
 *	Stuff from execnodes.h
520 521 522
 */

/*
523
 *	EState is a subclass of Node.
524
 */
525
static bool
526
_equalEState(EState * a, EState * b)
527
{
528 529 530 531 532 533 534 535 536 537
	if (a->es_direction != b->es_direction)
		return (false);

	if (!equal(a->es_range_table, b->es_range_table))
		return (false);

	if (a->es_result_relation_info != b->es_result_relation_info)
		return (false);

	return (true);
538 539
}

540
static bool
541
_equalTargetEntry(TargetEntry * a, TargetEntry * b)
542
{
543 544 545 546 547 548 549 550
	if (!equal(a->resdom, b->resdom))
		return (false);
	if (!equal(a->fjoin, b->fjoin))
		return (false);
	if (!equal(a->expr, b->expr))
		return (false);

	return (true);
551 552
}

553 554

/*
555
 *	equal -- are two lists equal?
556
 *
557 558
 *		This is a comparison by value.	It would be simpler to write it
 *		to be recursive, but it should run faster if we iterate.
559
 */
560
static bool
561
_equalValue(Value * a, Value * b)
562
{
563 564 565 566 567
	if (a->type != b->type)
		return (false);

	switch (a->type)
	{
568 569 570 571 572 573 574 575
		case T_String:
			return strcmp(a->val.str, b->val.str);
		case T_Integer:
			return (a->val.ival == b->val.ival);
		case T_Float:
			return (a->val.dval == b->val.dval);
		default:
			break;
576 577 578
	}

	return (true);
579 580 581 582
}

/*
 * equal--
583
 *	  returns whether two nodes are equal
584 585 586 587
 */
bool
equal(void *a, void *b)
{
588
	bool		retval = false;
589

590
	if (a == b)
591
		return (true);
592 593 594 595 596

	/*
	 * note that a!=b, so only one of them can be NULL
	 */
	if (a == NULL || b == NULL)
597
		return (false);
598 599 600 601 602 603 604 605 606

	/*
	 * are they the same type of nodes?
	 */
	if (nodeTag(a) != nodeTag(b))
		return (false);

	switch (nodeTag(a))
	{
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
		case T_Resdom:
			retval = _equalResdom(a, b);
			break;
		case T_Fjoin:
			retval = _equalFjoin(a, b);
			break;
		case T_Expr:
			retval = _equalExpr(a, b);
			break;
		case T_TargetEntry:
			retval = _equalTargetEntry(a, b);
			break;
		case T_Iter:
			retval = _equalIter(a, b);
			break;
		case T_Stream:
			retval = _equalStream(a, b);
			break;
		case T_Var:
			retval = _equalVar(a, b);
			break;
		case T_Array:
			retval = _equalArray(a, b);
			break;
		case T_ArrayRef:
			retval = _equalArrayRef(a, b);
			break;
		case T_Oper:
			retval = _equalOper(a, b);
			break;
		case T_Const:
			retval = _equalConst(a, b);
			break;
		case T_Param:
			retval = _equalParam(a, b);
			break;
		case T_Func:
			retval = _equalFunc(a, b);
			break;
		case T_CInfo:
			retval = _equalCInfo(a, b);
			break;
		case T_JoinMethod:
			retval = _equalJoinMethod(a, b);
			break;
		case T_Path:
			retval = _equalPath(a, b);
			break;
		case T_IndexPath:
			retval = _equalIndexPath(a, b);
			break;
		case T_JoinPath:
			retval = _equalJoinPath(a, b);
			break;
		case T_MergePath:
			retval = _equalMergePath(a, b);
			break;
		case T_HashPath:
			retval = _equalHashPath(a, b);
			break;
		case T_JoinKey:
			retval = _equalJoinKey(a, b);
			break;
		case T_MergeOrder:
			retval = _equalMergeOrder(a, b);
			break;
		case T_HInfo:
			retval = _equalHInfo(a, b);
			break;
		case T_IndexScan:
			retval = _equalIndexScan(a, b);
			break;
		case T_JInfo:
			retval = _equalJInfo(a, b);
			break;
		case T_EState:
			retval = _equalEState(a, b);
			break;
		case T_Integer:
		case T_String:
		case T_Float:
			retval = _equalValue(a, b);
			break;
		case T_List:
691
			{
692 693 694 695 696 697 698
				List	   *la = (List *) a;
				List	   *lb = (List *) b;
				List	   *l;

				if (a == NULL && b == NULL)
					return (true);
				if (length(a) != length(b))
699
					return (false);
700 701 702 703 704 705 706
				foreach(l, la)
				{
					if (!equal(lfirst(l), lfirst(lb)))
						return (false);
					lb = lnext(lb);
				}
				retval = true;
707
			}
708 709 710 711 712
			break;
		default:
			elog(NOTICE, "equal: don't know whether nodes of type %d are equal",
				 nodeTag(a));
			break;
713
	}
714 715

	return retval;
716 717 718 719
}

/*
 * equali--
720
 *	  compares two lists of integers
721 722 723
 *
 * XXX temp hack. needs something like T_IntList
 */
724
static bool
725 726
equali(List * a, List * b)
{
727 728 729
	List	   *la = (List *) a;
	List	   *lb = (List *) b;
	List	   *l;
730 731 732 733 734 735 736 737 738 739 740 741

	if (a == NULL && b == NULL)
		return (true);
	if (length(a) != length(b))
		return (false);
	foreach(l, la)
	{
		if (lfirsti(l) != lfirsti(lb))
			return (false);
		lb = lnext(lb);
	}
	return true;
742
}