Skip to content

Commit bb85030

Browse files
committed
Fix contrib/btree_gist to handle collations properly.
Make use of the collation attached to the index column, instead of hard-wiring DEFAULT_COLLATION_OID. (Note: in theory this could require reindexing btree_gist indexes on textual columns, but I rather doubt anyone has one with a non-default declared collation as yet.)
1 parent ae20bf1 commit bb85030

File tree

7 files changed

+219
-187
lines changed

7 files changed

+219
-187
lines changed

contrib/btree_gist/btree_bit.c

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,40 +29,51 @@ Datum gbt_bit_same(PG_FUNCTION_ARGS);
2929
/* define for comparison */
3030

3131
static bool
32-
gbt_bitgt(const void *a, const void *b)
32+
gbt_bitgt(const void *a, const void *b, Oid collation)
3333
{
34-
return (DatumGetBool(DirectFunctionCall2(bitgt, PointerGetDatum(a), PointerGetDatum(b))));
34+
return DatumGetBool(DirectFunctionCall2(bitgt,
35+
PointerGetDatum(a),
36+
PointerGetDatum(b)));
3537
}
3638

3739
static bool
38-
gbt_bitge(const void *a, const void *b)
40+
gbt_bitge(const void *a, const void *b, Oid collation)
3941
{
40-
return (DatumGetBool(DirectFunctionCall2(bitge, PointerGetDatum(a), PointerGetDatum(b))));
42+
return DatumGetBool(DirectFunctionCall2(bitge,
43+
PointerGetDatum(a),
44+
PointerGetDatum(b)));
4145
}
4246

4347
static bool
44-
gbt_biteq(const void *a, const void *b)
48+
gbt_biteq(const void *a, const void *b, Oid collation)
4549
{
46-
return (DatumGetBool(DirectFunctionCall2(biteq, PointerGetDatum(a), PointerGetDatum(b))));
50+
return DatumGetBool(DirectFunctionCall2(biteq,
51+
PointerGetDatum(a),
52+
PointerGetDatum(b)));
4753
}
4854

4955
static bool
50-
gbt_bitle(const void *a, const void *b)
56+
gbt_bitle(const void *a, const void *b, Oid collation)
5157
{
52-
return (DatumGetBool(DirectFunctionCall2(bitle, PointerGetDatum(a), PointerGetDatum(b))));
58+
return DatumGetBool(DirectFunctionCall2(bitle,
59+
PointerGetDatum(a),
60+
PointerGetDatum(b)));
5361
}
5462

5563
static bool
56-
gbt_bitlt(const void *a, const void *b)
64+
gbt_bitlt(const void *a, const void *b, Oid collation)
5765
{
58-
return (DatumGetBool(DirectFunctionCall2(bitlt, PointerGetDatum(a), PointerGetDatum(b))));
66+
return DatumGetBool(DirectFunctionCall2(bitlt,
67+
PointerGetDatum(a),
68+
PointerGetDatum(b)));
5969
}
6070

6171
static int32
62-
gbt_bitcmp(const bytea *a, const bytea *b)
72+
gbt_bitcmp(const void *a, const void *b, Oid collation)
6373
{
64-
return
65-
(DatumGetInt32(DirectFunctionCall2(byteacmp, PointerGetDatum(a), PointerGetDatum(b))));
74+
return DatumGetInt32(DirectFunctionCall2(byteacmp,
75+
PointerGetDatum(a),
76+
PointerGetDatum(b)));
6677
}
6778

6879

@@ -134,20 +145,22 @@ gbt_bit_consistent(PG_FUNCTION_ARGS)
134145

135146
/* Oid subtype = PG_GETARG_OID(3); */
136147
bool *recheck = (bool *) PG_GETARG_POINTER(4);
137-
bool retval = FALSE;
148+
bool retval;
138149
GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(entry->key);
139150
GBT_VARKEY_R r = gbt_var_key_readable(key);
140151

141152
/* All cases served by this function are exact */
142153
*recheck = false;
143154

144155
if (GIST_LEAF(entry))
145-
retval = gbt_var_consistent(&r, query, &strategy, TRUE, &tinfo);
156+
retval = gbt_var_consistent(&r, query, strategy, PG_GET_COLLATION(),
157+
TRUE, &tinfo);
146158
else
147159
{
148160
bytea *q = gbt_bit_xfrm((bytea *) query);
149161

150-
retval = gbt_var_consistent(&r, (void *) q, &strategy, FALSE, &tinfo);
162+
retval = gbt_var_consistent(&r, q, strategy, PG_GET_COLLATION(),
163+
FALSE, &tinfo);
151164
}
152165
PG_RETURN_BOOL(retval);
153166
}
@@ -160,7 +173,8 @@ gbt_bit_union(PG_FUNCTION_ARGS)
160173
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
161174
int32 *size = (int *) PG_GETARG_POINTER(1);
162175

163-
PG_RETURN_POINTER(gbt_var_union(entryvec, size, &tinfo));
176+
PG_RETURN_POINTER(gbt_var_union(entryvec, size, PG_GET_COLLATION(),
177+
&tinfo));
164178
}
165179

166180

@@ -170,7 +184,8 @@ gbt_bit_picksplit(PG_FUNCTION_ARGS)
170184
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
171185
GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
172186

173-
gbt_var_picksplit(entryvec, v, &tinfo);
187+
gbt_var_picksplit(entryvec, v, PG_GET_COLLATION(),
188+
&tinfo);
174189
PG_RETURN_POINTER(v);
175190
}
176191

@@ -181,7 +196,8 @@ gbt_bit_same(PG_FUNCTION_ARGS)
181196
Datum d2 = PG_GETARG_DATUM(1);
182197
bool *result = (bool *) PG_GETARG_POINTER(2);
183198

184-
PG_RETURN_POINTER(gbt_var_same(result, d1, d2, &tinfo));
199+
*result = gbt_var_same(d1, d2, PG_GET_COLLATION(), &tinfo);
200+
PG_RETURN_POINTER(result);
185201
}
186202

187203

@@ -192,5 +208,6 @@ gbt_bit_penalty(PG_FUNCTION_ARGS)
192208
GISTENTRY *n = (GISTENTRY *) PG_GETARG_POINTER(1);
193209
float *result = (float *) PG_GETARG_POINTER(2);
194210

195-
PG_RETURN_POINTER(gbt_var_penalty(result, o, n, &tinfo));
211+
PG_RETURN_POINTER(gbt_var_penalty(result, o, n, PG_GET_COLLATION(),
212+
&tinfo));
196213
}

contrib/btree_gist/btree_bytea.c

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,41 +27,51 @@ Datum gbt_bytea_same(PG_FUNCTION_ARGS);
2727
/* define for comparison */
2828

2929
static bool
30-
gbt_byteagt(const void *a, const void *b)
30+
gbt_byteagt(const void *a, const void *b, Oid collation)
3131
{
32-
return (DatumGetBool(DirectFunctionCall2(byteagt, PointerGetDatum(a), PointerGetDatum(b))));
32+
return DatumGetBool(DirectFunctionCall2(byteagt,
33+
PointerGetDatum(a),
34+
PointerGetDatum(b)));
3335
}
3436

3537
static bool
36-
gbt_byteage(const void *a, const void *b)
38+
gbt_byteage(const void *a, const void *b, Oid collation)
3739
{
38-
return (DatumGetBool(DirectFunctionCall2(byteage, PointerGetDatum(a), PointerGetDatum(b))));
40+
return DatumGetBool(DirectFunctionCall2(byteage,
41+
PointerGetDatum(a),
42+
PointerGetDatum(b)));
3943
}
4044

4145
static bool
42-
gbt_byteaeq(const void *a, const void *b)
46+
gbt_byteaeq(const void *a, const void *b, Oid collation)
4347
{
44-
return (DatumGetBool(DirectFunctionCall2(byteaeq, PointerGetDatum(a), PointerGetDatum(b))));
48+
return DatumGetBool(DirectFunctionCall2(byteaeq,
49+
PointerGetDatum(a),
50+
PointerGetDatum(b)));
4551
}
4652

4753
static bool
48-
gbt_byteale(const void *a, const void *b)
54+
gbt_byteale(const void *a, const void *b, Oid collation)
4955
{
50-
return (DatumGetBool(DirectFunctionCall2(byteale, PointerGetDatum(a), PointerGetDatum(b))));
56+
return DatumGetBool(DirectFunctionCall2(byteale,
57+
PointerGetDatum(a),
58+
PointerGetDatum(b)));
5159
}
5260

5361
static bool
54-
gbt_bytealt(const void *a, const void *b)
62+
gbt_bytealt(const void *a, const void *b, Oid collation)
5563
{
56-
return (DatumGetBool(DirectFunctionCall2(bytealt, PointerGetDatum(a), PointerGetDatum(b))));
64+
return DatumGetBool(DirectFunctionCall2(bytealt,
65+
PointerGetDatum(a),
66+
PointerGetDatum(b)));
5767
}
5868

59-
6069
static int32
61-
gbt_byteacmp(const bytea *a, const bytea *b)
70+
gbt_byteacmp(const void *a, const void *b, Oid collation)
6271
{
63-
return
64-
(DatumGetInt32(DirectFunctionCall2(byteacmp, PointerGetDatum(a), PointerGetDatum(b))));
72+
return DatumGetInt32(DirectFunctionCall2(byteacmp,
73+
PointerGetDatum(a),
74+
PointerGetDatum(b)));
6575
}
6676

6777

@@ -111,7 +121,8 @@ gbt_bytea_consistent(PG_FUNCTION_ARGS)
111121
/* All cases served by this function are exact */
112122
*recheck = false;
113123

114-
retval = gbt_var_consistent(&r, query, &strategy, GIST_LEAF(entry), &tinfo);
124+
retval = gbt_var_consistent(&r, query, strategy, PG_GET_COLLATION(),
125+
GIST_LEAF(entry), &tinfo);
115126
PG_RETURN_BOOL(retval);
116127
}
117128

@@ -123,7 +134,8 @@ gbt_bytea_union(PG_FUNCTION_ARGS)
123134
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
124135
int32 *size = (int *) PG_GETARG_POINTER(1);
125136

126-
PG_RETURN_POINTER(gbt_var_union(entryvec, size, &tinfo));
137+
PG_RETURN_POINTER(gbt_var_union(entryvec, size, PG_GET_COLLATION(),
138+
&tinfo));
127139
}
128140

129141

@@ -133,7 +145,8 @@ gbt_bytea_picksplit(PG_FUNCTION_ARGS)
133145
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
134146
GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
135147

136-
gbt_var_picksplit(entryvec, v, &tinfo);
148+
gbt_var_picksplit(entryvec, v, PG_GET_COLLATION(),
149+
&tinfo);
137150
PG_RETURN_POINTER(v);
138151
}
139152

@@ -144,7 +157,8 @@ gbt_bytea_same(PG_FUNCTION_ARGS)
144157
Datum d2 = PG_GETARG_DATUM(1);
145158
bool *result = (bool *) PG_GETARG_POINTER(2);
146159

147-
PG_RETURN_POINTER(gbt_var_same(result, d1, d2, &tinfo));
160+
*result = gbt_var_same(d1, d2, PG_GET_COLLATION(), &tinfo);
161+
PG_RETURN_POINTER(result);
148162
}
149163

150164

@@ -155,5 +169,6 @@ gbt_bytea_penalty(PG_FUNCTION_ARGS)
155169
GISTENTRY *n = (GISTENTRY *) PG_GETARG_POINTER(1);
156170
float *result = (float *) PG_GETARG_POINTER(2);
157171

158-
PG_RETURN_POINTER(gbt_var_penalty(result, o, n, &tinfo));
172+
PG_RETURN_POINTER(gbt_var_penalty(result, o, n, PG_GET_COLLATION(),
173+
&tinfo));
159174
}

contrib/btree_gist/btree_numeric.c

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,41 +32,51 @@ Datum gbt_numeric_same(PG_FUNCTION_ARGS);
3232
/* define for comparison */
3333

3434
static bool
35-
gbt_numeric_gt(const void *a, const void *b)
35+
gbt_numeric_gt(const void *a, const void *b, Oid collation)
3636
{
37-
return (DatumGetBool(DirectFunctionCall2(numeric_gt, PointerGetDatum(a), PointerGetDatum(b))));
37+
return DatumGetBool(DirectFunctionCall2(numeric_gt,
38+
PointerGetDatum(a),
39+
PointerGetDatum(b)));
3840
}
3941

4042
static bool
41-
gbt_numeric_ge(const void *a, const void *b)
43+
gbt_numeric_ge(const void *a, const void *b, Oid collation)
4244
{
43-
return (DatumGetBool(DirectFunctionCall2(numeric_ge, PointerGetDatum(a), PointerGetDatum(b))));
45+
return DatumGetBool(DirectFunctionCall2(numeric_ge,
46+
PointerGetDatum(a),
47+
PointerGetDatum(b)));
4448
}
4549

4650
static bool
47-
gbt_numeric_eq(const void *a, const void *b)
51+
gbt_numeric_eq(const void *a, const void *b, Oid collation)
4852
{
49-
return (DatumGetBool(DirectFunctionCall2(numeric_eq, PointerGetDatum(a), PointerGetDatum(b))));
53+
return DatumGetBool(DirectFunctionCall2(numeric_eq,
54+
PointerGetDatum(a),
55+
PointerGetDatum(b)));
5056
}
5157

5258
static bool
53-
gbt_numeric_le(const void *a, const void *b)
59+
gbt_numeric_le(const void *a, const void *b, Oid collation)
5460
{
55-
return (DatumGetBool(DirectFunctionCall2(numeric_le, PointerGetDatum(a), PointerGetDatum(b))));
61+
return DatumGetBool(DirectFunctionCall2(numeric_le,
62+
PointerGetDatum(a),
63+
PointerGetDatum(b)));
5664
}
5765

5866
static bool
59-
gbt_numeric_lt(const void *a, const void *b)
67+
gbt_numeric_lt(const void *a, const void *b, Oid collation)
6068
{
61-
return (DatumGetBool(DirectFunctionCall2(numeric_lt, PointerGetDatum(a), PointerGetDatum(b))));
69+
return DatumGetBool(DirectFunctionCall2(numeric_lt,
70+
PointerGetDatum(a),
71+
PointerGetDatum(b)));
6272
}
6373

64-
6574
static int32
66-
gbt_numeric_cmp(const bytea *a, const bytea *b)
75+
gbt_numeric_cmp(const void *a, const void *b, Oid collation)
6776
{
68-
return
69-
(DatumGetInt32(DirectFunctionCall2(numeric_cmp, PointerGetDatum(a), PointerGetDatum(b))));
77+
return DatumGetInt32(DirectFunctionCall2(numeric_cmp,
78+
PointerGetDatum(a),
79+
PointerGetDatum(b)));
7080
}
7181

7282

@@ -116,7 +126,8 @@ gbt_numeric_consistent(PG_FUNCTION_ARGS)
116126
/* All cases served by this function are exact */
117127
*recheck = false;
118128

119-
retval = gbt_var_consistent(&r, query, &strategy, GIST_LEAF(entry), &tinfo);
129+
retval = gbt_var_consistent(&r, query, strategy, PG_GET_COLLATION(),
130+
GIST_LEAF(entry), &tinfo);
120131
PG_RETURN_BOOL(retval);
121132
}
122133

@@ -128,7 +139,8 @@ gbt_numeric_union(PG_FUNCTION_ARGS)
128139
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
129140
int32 *size = (int *) PG_GETARG_POINTER(1);
130141

131-
PG_RETURN_POINTER(gbt_var_union(entryvec, size, &tinfo));
142+
PG_RETURN_POINTER(gbt_var_union(entryvec, size, PG_GET_COLLATION(),
143+
&tinfo));
132144
}
133145

134146

@@ -139,7 +151,8 @@ gbt_numeric_same(PG_FUNCTION_ARGS)
139151
Datum d2 = PG_GETARG_DATUM(1);
140152
bool *result = (bool *) PG_GETARG_POINTER(2);
141153

142-
PG_RETURN_POINTER(gbt_var_same(result, d1, d2, &tinfo));
154+
*result = gbt_var_same(d1, d2, PG_GET_COLLATION(), &tinfo);
155+
PG_RETURN_POINTER(result);
143156
}
144157

145158

@@ -163,7 +176,7 @@ gbt_numeric_penalty(PG_FUNCTION_ARGS)
163176

164177
rk = gbt_var_key_readable(org);
165178
uni = PointerGetDatum(gbt_var_key_copy(&rk, TRUE));
166-
gbt_var_bin_union(&uni, newe, &tinfo);
179+
gbt_var_bin_union(&uni, newe, PG_GET_COLLATION(), &tinfo);
167180
ok = gbt_var_key_readable(org);
168181
uk = gbt_var_key_readable((GBT_VARKEY *) DatumGetPointer(uni));
169182

@@ -224,6 +237,7 @@ gbt_numeric_picksplit(PG_FUNCTION_ARGS)
224237
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
225238
GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
226239

227-
gbt_var_picksplit(entryvec, v, &tinfo);
240+
gbt_var_picksplit(entryvec, v, PG_GET_COLLATION(),
241+
&tinfo);
228242
PG_RETURN_POINTER(v);
229243
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy