|
21 | 21 | #include "access/nbtree.h"
|
22 | 22 | #include "fmgr.h"
|
23 | 23 | #include "utils/lsyscache.h"
|
| 24 | +#include "utils/rel.h" |
24 | 25 | #include "utils/sortsupport.h"
|
25 | 26 |
|
26 | 27 |
|
@@ -86,28 +87,14 @@ PrepareSortSupportComparisonShim(Oid cmpFunc, SortSupport ssup)
|
86 | 87 | }
|
87 | 88 |
|
88 | 89 | /*
|
89 |
| - * Fill in SortSupport given an ordering operator (btree "<" or ">" operator). |
90 |
| - * |
91 |
| - * Caller must previously have zeroed the SortSupportData structure and then |
92 |
| - * filled in ssup_cxt, ssup_collation, and ssup_nulls_first. This will fill |
93 |
| - * in ssup_reverse as well as the comparator function pointer. |
| 90 | + * Look up and call sortsupport function to setup SortSupport comparator; |
| 91 | + * or if no such function exists or it declines to set up the appropriate |
| 92 | + * state, prepare a suitable shim. |
94 | 93 | */
|
95 |
| -void |
96 |
| -PrepareSortSupportFromOrderingOp(Oid orderingOp, SortSupport ssup) |
| 94 | +static void |
| 95 | +FinishSortSupportFunction(Oid opfamily, Oid opcintype, SortSupport ssup) |
97 | 96 | {
|
98 | 97 | Oid sortSupportFunction;
|
99 |
| - Oid opfamily; |
100 |
| - Oid opcintype; |
101 |
| - int16 strategy; |
102 |
| - |
103 |
| - Assert(ssup->comparator == NULL); |
104 |
| - |
105 |
| - /* Find the operator in pg_amop */ |
106 |
| - if (!get_ordering_op_properties(orderingOp, &opfamily, &opcintype, |
107 |
| - &strategy)) |
108 |
| - elog(ERROR, "operator %u is not a valid ordering operator", |
109 |
| - orderingOp); |
110 |
| - ssup->ssup_reverse = (strategy == BTGreaterStrategyNumber); |
111 | 98 |
|
112 | 99 | /* Look for a sort support function */
|
113 | 100 | sortSupportFunction = get_opfamily_proc(opfamily, opcintype, opcintype,
|
@@ -136,3 +123,57 @@ PrepareSortSupportFromOrderingOp(Oid orderingOp, SortSupport ssup)
|
136 | 123 | PrepareSortSupportComparisonShim(sortFunction, ssup);
|
137 | 124 | }
|
138 | 125 | }
|
| 126 | + |
| 127 | +/* |
| 128 | + * Fill in SortSupport given an ordering operator (btree "<" or ">" operator). |
| 129 | + * |
| 130 | + * Caller must previously have zeroed the SortSupportData structure and then |
| 131 | + * filled in ssup_cxt, ssup_collation, and ssup_nulls_first. This will fill |
| 132 | + * in ssup_reverse as well as the comparator function pointer. |
| 133 | + */ |
| 134 | +void |
| 135 | +PrepareSortSupportFromOrderingOp(Oid orderingOp, SortSupport ssup) |
| 136 | +{ |
| 137 | + Oid opfamily; |
| 138 | + Oid opcintype; |
| 139 | + int16 strategy; |
| 140 | + |
| 141 | + Assert(ssup->comparator == NULL); |
| 142 | + |
| 143 | + /* Find the operator in pg_amop */ |
| 144 | + if (!get_ordering_op_properties(orderingOp, &opfamily, &opcintype, |
| 145 | + &strategy)) |
| 146 | + elog(ERROR, "operator %u is not a valid ordering operator", |
| 147 | + orderingOp); |
| 148 | + ssup->ssup_reverse = (strategy == BTGreaterStrategyNumber); |
| 149 | + |
| 150 | + FinishSortSupportFunction(opfamily, opcintype, ssup); |
| 151 | +} |
| 152 | + |
| 153 | +/* |
| 154 | + * Fill in SortSupport given an index relation, attribute, and strategy. |
| 155 | + * |
| 156 | + * Caller must previously have zeroed the SortSupportData structure and then |
| 157 | + * filled in ssup_cxt, ssup_attno, ssup_collation, and ssup_nulls_first. This |
| 158 | + * will fill in ssup_reverse (based on the supplied strategy), as well as the |
| 159 | + * comparator function pointer. |
| 160 | + */ |
| 161 | +void |
| 162 | +PrepareSortSupportFromIndexRel(Relation indexRel, int16 strategy, |
| 163 | + SortSupport ssup) |
| 164 | +{ |
| 165 | + Oid opfamily = indexRel->rd_opfamily[ssup->ssup_attno - 1]; |
| 166 | + Oid opcintype = indexRel->rd_opcintype[ssup->ssup_attno - 1]; |
| 167 | + |
| 168 | + Assert(ssup->comparator == NULL); |
| 169 | + |
| 170 | + /* Find the operator in pg_amop */ |
| 171 | + if (indexRel->rd_rel->relam != BTREE_AM_OID) |
| 172 | + elog(ERROR, "unexpected non-btree AM: %u", indexRel->rd_rel->relam); |
| 173 | + if (strategy != BTGreaterStrategyNumber && |
| 174 | + strategy != BTLessStrategyNumber) |
| 175 | + elog(ERROR, "unexpected sort support strategy: %d", strategy); |
| 176 | + ssup->ssup_reverse = (strategy == BTGreaterStrategyNumber); |
| 177 | + |
| 178 | + FinishSortSupportFunction(opfamily, opcintype, ssup); |
| 179 | +} |
0 commit comments