PostgreSQL Source Code  git master
equalfuncs.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * equalfuncs.c
4  * Equality functions to compare node trees.
5  *
6  * NOTE: it is intentional that parse location fields (in nodes that have
7  * one) are not compared. This is because we want, for example, a variable
8  * "x" to be considered equal() to another reference to "x" in the query.
9  *
10  *
11  * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
12  * Portions Copyright (c) 1994, Regents of the University of California
13  *
14  * IDENTIFICATION
15  * src/backend/nodes/equalfuncs.c
16  *
17  *-------------------------------------------------------------------------
18  */
19 
20 #include "postgres.h"
21 
22 #include "miscadmin.h"
23 #include "utils/datum.h"
24 
25 
26 /*
27  * Macros to simplify comparison of different kinds of fields. Use these
28  * wherever possible to reduce the chance for silly typos. Note that these
29  * hard-wire the convention that the local variables in an Equal routine are
30  * named 'a' and 'b'.
31  */
32 
33 /* Compare a simple scalar field (int, float, bool, enum, etc) */
34 #define COMPARE_SCALAR_FIELD(fldname) \
35  do { \
36  if (a->fldname != b->fldname) \
37  return false; \
38  } while (0)
39 
40 /* Compare a field that is a pointer to some kind of Node or Node tree */
41 #define COMPARE_NODE_FIELD(fldname) \
42  do { \
43  if (!equal(a->fldname, b->fldname)) \
44  return false; \
45  } while (0)
46 
47 /* Compare a field that is a pointer to a Bitmapset */
48 #define COMPARE_BITMAPSET_FIELD(fldname) \
49  do { \
50  if (!bms_equal(a->fldname, b->fldname)) \
51  return false; \
52  } while (0)
53 
54 /* Compare a field that is a pointer to a C string, or perhaps NULL */
55 #define COMPARE_STRING_FIELD(fldname) \
56  do { \
57  if (!equalstr(a->fldname, b->fldname)) \
58  return false; \
59  } while (0)
60 
61 /* Macro for comparing string fields that might be NULL */
62 #define equalstr(a, b) \
63  (((a) != NULL && (b) != NULL) ? (strcmp(a, b) == 0) : (a) == (b))
64 
65 /* Compare a field that is an inline array */
66 #define COMPARE_ARRAY_FIELD(fldname) \
67  do { \
68  if (memcmp(a->fldname, b->fldname, sizeof(a->fldname)) != 0) \
69  return false; \
70  } while (0)
71 
72 /* Compare a field that is a pointer to a simple palloc'd object of size sz */
73 #define COMPARE_POINTER_FIELD(fldname, sz) \
74  do { \
75  if (memcmp(a->fldname, b->fldname, (sz)) != 0) \
76  return false; \
77  } while (0)
78 
79 /* Compare a parse location field (this is a no-op, per note above) */
80 #define COMPARE_LOCATION_FIELD(fldname) \
81  ((void) 0)
82 
83 /* Compare a CoercionForm field (also a no-op, per comment in primnodes.h) */
84 #define COMPARE_COERCIONFORM_FIELD(fldname) \
85  ((void) 0)
86 
87 
88 #include "equalfuncs.funcs.c"
89 
90 
91 /*
92  * Support functions for nodes with custom_copy_equal attribute
93  */
94 
95 static bool
96 _equalConst(const Const *a, const Const *b)
97 {
98  COMPARE_SCALAR_FIELD(consttype);
99  COMPARE_SCALAR_FIELD(consttypmod);
100  COMPARE_SCALAR_FIELD(constcollid);
101  COMPARE_SCALAR_FIELD(constlen);
102  COMPARE_SCALAR_FIELD(constisnull);
103  COMPARE_SCALAR_FIELD(constbyval);
104  COMPARE_LOCATION_FIELD(location);
105 
106  /*
107  * We treat all NULL constants of the same type as equal. Someday this
108  * might need to change? But datumIsEqual doesn't work on nulls, so...
109  */
110  if (a->constisnull)
111  return true;
112  return datumIsEqual(a->constvalue, b->constvalue,
113  a->constbyval, a->constlen);
114 }
115 
116 static bool
118 {
119  const ExtensibleNodeMethods *methods;
120 
121  COMPARE_STRING_FIELD(extnodename);
122 
123  /* At this point, we know extnodename is the same for both nodes. */
124  methods = GetExtensibleNodeMethods(a->extnodename, false);
125 
126  /* compare the private fields */
127  if (!methods->nodeEqual(a, b))
128  return false;
129 
130  return true;
131 }
132 
133 static bool
134 _equalA_Const(const A_Const *a, const A_Const *b)
135 {
136  /*
137  * Hack for in-line val field. Also val is not valid is isnull is true.
138  */
139  if (!a->isnull && !b->isnull &&
140  !equal(&a->val, &b->val))
141  return false;
142  COMPARE_SCALAR_FIELD(isnull);
143  COMPARE_LOCATION_FIELD(location);
144 
145  return true;
146 }
147 
148 /*
149  * Lists are handled specially
150  */
151 static bool
152 _equalList(const List *a, const List *b)
153 {
154  const ListCell *item_a;
155  const ListCell *item_b;
156 
157  /*
158  * Try to reject by simple scalar checks before grovelling through all the
159  * list elements...
160  */
162  COMPARE_SCALAR_FIELD(length);
163 
164  /*
165  * We place the switch outside the loop for the sake of efficiency; this
166  * may not be worth doing...
167  */
168  switch (a->type)
169  {
170  case T_List:
171  forboth(item_a, a, item_b, b)
172  {
173  if (!equal(lfirst(item_a), lfirst(item_b)))
174  return false;
175  }
176  break;
177  case T_IntList:
178  forboth(item_a, a, item_b, b)
179  {
180  if (lfirst_int(item_a) != lfirst_int(item_b))
181  return false;
182  }
183  break;
184  case T_OidList:
185  forboth(item_a, a, item_b, b)
186  {
187  if (lfirst_oid(item_a) != lfirst_oid(item_b))
188  return false;
189  }
190  break;
191  case T_XidList:
192  forboth(item_a, a, item_b, b)
193  {
194  if (lfirst_xid(item_a) != lfirst_xid(item_b))
195  return false;
196  }
197  break;
198  default:
199  elog(ERROR, "unrecognized list node type: %d",
200  (int) a->type);
201  return false; /* keep compiler quiet */
202  }
203 
204  /*
205  * If we got here, we should have run out of elements of both lists
206  */
207  Assert(item_a == NULL);
208  Assert(item_b == NULL);
209 
210  return true;
211 }
212 
213 
214 /*
215  * equal
216  * returns whether two nodes are equal
217  */
218 bool
219 equal(const void *a, const void *b)
220 {
221  bool retval;
222 
223  if (a == b)
224  return true;
225 
226  /*
227  * note that a!=b, so only one of them can be NULL
228  */
229  if (a == NULL || b == NULL)
230  return false;
231 
232  /*
233  * are they the same type of nodes?
234  */
235  if (nodeTag(a) != nodeTag(b))
236  return false;
237 
238  /* Guard against stack overflow due to overly complex expressions */
240 
241  switch (nodeTag(a))
242  {
243 #include "equalfuncs.switch.c"
244 
245  case T_List:
246  case T_IntList:
247  case T_OidList:
248  case T_XidList:
249  retval = _equalList(a, b);
250  break;
251 
252  default:
253  elog(ERROR, "unrecognized node type: %d",
254  (int) nodeTag(a));
255  retval = false; /* keep compiler quiet */
256  break;
257  }
258 
259  return retval;
260 }
bool datumIsEqual(Datum value1, Datum value2, bool typByVal, int typLen)
Definition: datum.c:223
#define ERROR
Definition: elog.h:35
static bool _equalExtensibleNode(const ExtensibleNode *a, const ExtensibleNode *b)
Definition: equalfuncs.c:117
#define COMPARE_LOCATION_FIELD(fldname)
Definition: equalfuncs.c:80
#define COMPARE_SCALAR_FIELD(fldname)
Definition: equalfuncs.c:34
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:219
static bool _equalConst(const Const *a, const Const *b)
Definition: equalfuncs.c:96
#define COMPARE_STRING_FIELD(fldname)
Definition: equalfuncs.c:55
static bool _equalList(const List *a, const List *b)
Definition: equalfuncs.c:152
static bool _equalA_Const(const A_Const *a, const A_Const *b)
Definition: equalfuncs.c:134
const ExtensibleNodeMethods * GetExtensibleNodeMethods(const char *extnodename, bool missing_ok)
Definition: extensible.c:125
int b
Definition: isn.c:70
int a
Definition: isn.c:69
Assert(fmt[strlen(fmt) - 1] !='\n')
#define nodeTag(nodeptr)
Definition: nodes.h:116
#define lfirst(lc)
Definition: pg_list.h:170
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:465
#define lfirst_int(lc)
Definition: pg_list.h:171
#define lfirst_oid(lc)
Definition: pg_list.h:172
#define lfirst_xid(lc)
Definition: pg_list.h:173
void check_stack_depth(void)
Definition: postgres.c:3440
bool(* nodeEqual)(const struct ExtensibleNode *a, const struct ExtensibleNode *b)
Definition: extensible.h:68
Definition: pg_list.h:52