|
7 | 7 | *
|
8 | 8 | *
|
9 | 9 | * IDENTIFICATION
|
10 |
| - * $Header: /cvsroot/pgsql/src/backend/optimizer/util/Attic/keys.c,v 1.18 1999/02/19 02:05:16 momjian Exp $ |
| 10 | + * $Header: /cvsroot/pgsql/src/backend/optimizer/util/Attic/keys.c,v 1.19 1999/02/20 18:01:02 momjian Exp $ |
11 | 11 | *
|
12 | 12 | *-------------------------------------------------------------------------
|
13 | 13 | */
|
@@ -110,62 +110,70 @@ extract_join_key(JoinKey *jk, int outer_or_inner)
|
110 | 110 | * Returns t iff two sets of path keys are equivalent. They are
|
111 | 111 | * equivalent if the first Var nodes match the second Var nodes.
|
112 | 112 | *
|
113 |
| - * XXX It isn't necessary to check that each sublist exactly contain |
114 |
| - * the same elements because if the routine that built these |
115 |
| - * sublists together is correct, having one element in common |
116 |
| - * implies having all elements in common. |
117 |
| - * Huh? bjm |
| 113 | + * See the top of optimizer/path/pathkeys.c for a description of pathkeys. |
| 114 | + * Each pathkey is ordered by its join order, so they not pre-ordered to |
| 115 | + * match. We must search them ourselves. |
118 | 116 | *
|
| 117 | + * This gets called a lot, so it is optimized. |
119 | 118 | */
|
120 | 119 | bool
|
121 | 120 | pathkeys_match(List *keys1, List *keys2, int *better_key)
|
122 | 121 | {
|
123 | 122 | List *key1,
|
124 |
| - *key2, |
125 |
| - *key1a, |
126 |
| - *key2a; |
| 123 | + *key2; |
| 124 | + bool key1_subsetof_key2 = true, |
| 125 | + key2_subsetof_key1 = true; |
127 | 126 |
|
128 | 127 | for (key1 = keys1, key2 = keys2;
|
129 | 128 | key1 != NIL && key2 != NIL;
|
130 | 129 | key1 = lnext(key1), key2 = lnext(key2))
|
131 | 130 | {
|
132 |
| - for (key1a = lfirst(key1), key2a = lfirst(key2); |
133 |
| - key1a != NIL && key2a != NIL; |
134 |
| - key1a = lnext(key1a), key2a = lnext(key2a)) |
135 |
| - if (!equal(lfirst(key1a), lfirst(key2a))) |
| 131 | + List *i; |
| 132 | + |
| 133 | + if (key1_subsetof_key2) |
| 134 | + foreach(i, lfirst(key1)) |
136 | 135 | {
|
137 |
| - *better_key = 0; |
138 |
| - return false; |
| 136 | + Var *subkey = lfirst(i); |
| 137 | + if (!member(subkey, lfirst(key2))) |
| 138 | + { |
| 139 | + key1_subsetof_key2 = false; |
| 140 | + break; |
| 141 | + } |
139 | 142 | }
|
140 |
| - if (key1a != NIL && key2a == NIL) |
141 |
| - { |
142 |
| - *better_key = 1; |
143 |
| - return true; |
144 |
| - } |
145 |
| - if (key1a == NIL && key2a != NIL) |
146 |
| - { |
147 |
| - *better_key = 2; |
148 |
| - return true; |
149 |
| - } |
| 143 | + |
| 144 | + if (key2_subsetof_key1) |
| 145 | + foreach(i, lfirst(key2)) |
| 146 | + { |
| 147 | + Var *subkey = lfirst(i); |
| 148 | + if (!member(subkey, lfirst(key1))) |
| 149 | + { |
| 150 | + key2_subsetof_key1 = false; |
| 151 | + break; |
| 152 | + } |
| 153 | + } |
| 154 | + if (!key1_subsetof_key2 && !key2_subsetof_key1) |
| 155 | + break; /* no need to continue comparisons. */ |
150 | 156 | }
|
151 | 157 |
|
152 |
| - /* Now the result should be true if list keys2 has at least as many |
153 |
| - * entries as keys1, ie, we did not fall off the end of keys2 first. |
154 |
| - * If key1 is now NIL then we hit the end of keys1 before or at the |
155 |
| - * same time as the end of keys2. |
156 |
| - */ |
157 |
| - if (key1 != NIL && key2 == NIL) |
| 158 | + if (!key1_subsetof_key2 && !key2_subsetof_key1) |
158 | 159 | {
|
159 |
| - *better_key = 1; |
160 |
| - return true; |
| 160 | + *better_key = 0; |
| 161 | + return false; |
161 | 162 | }
|
162 |
| - if (key1 == NIL && key2 != NIL) |
| 163 | + if (key1_subsetof_key2 && !key2_subsetof_key1) |
163 | 164 | {
|
164 | 165 | *better_key = 2;
|
165 | 166 | return true;
|
166 | 167 | }
|
| 168 | + if (!key1_subsetof_key2 && key2_subsetof_key1) |
| 169 | + { |
| 170 | + *better_key = 1; |
| 171 | + return true; |
| 172 | + } |
| 173 | + |
167 | 174 | *better_key = 0;
|
168 | 175 | return true;
|
| 176 | + |
169 | 177 | }
|
170 | 178 |
|
171 | 179 | /*
|
|
0 commit comments