Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 1e6bb5a

Browse files
tglsfdcCommitfest Bot
authored and
Commitfest Bot
committed
Add cross-type comparisons for string types.
(Only these two cases appear in the catalogs.)
1 parent bcbd609 commit 1e6bb5a

File tree

6 files changed

+192
-6
lines changed

6 files changed

+192
-6
lines changed

contrib/btree_gin/btree_gin--1.3--1.4.sql

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,23 @@ ADD
8181
OPERATOR 12 >= (float8, float4),
8282
OPERATOR 13 > (float8, float4)
8383
;
84+
85+
ALTER OPERATOR FAMILY text_ops USING gin
86+
ADD
87+
-- Code 1: RHS is name
88+
OPERATOR 9 < (text, name),
89+
OPERATOR 10 <= (text, name),
90+
OPERATOR 11 = (text, name),
91+
OPERATOR 12 >= (text, name),
92+
OPERATOR 13 > (text, name)
93+
;
94+
95+
ALTER OPERATOR FAMILY name_ops USING gin
96+
ADD
97+
-- Code 1: RHS is text
98+
OPERATOR 9 < (name, text),
99+
OPERATOR 10 <= (name, text),
100+
OPERATOR 11 = (name, text),
101+
OPERATOR 12 >= (name, text),
102+
OPERATOR 13 > (name, text)
103+
;

contrib/btree_gin/btree_gin.c

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
#include <limits.h>
77

88
#include "access/stratnum.h"
9+
#include "mb/pg_wchar.h"
910
#include "utils/builtins.h"
1011
#include "utils/date.h"
1112
#include "utils/float.h"
1213
#include "utils/inet.h"
1314
#include "utils/numeric.h"
1415
#include "utils/timestamp.h"
1516
#include "utils/uuid.h"
17+
#include "varatt.h"
1618

1719
PG_MODULE_MAGIC_EXT(
1820
.name = "btree_gin",
@@ -622,13 +624,24 @@ leftmostvalue_text(void)
622624
return PointerGetDatum(cstring_to_text_with_len("", 0));
623625
}
624626

627+
static Datum
628+
cvt_name_text(Datum input)
629+
{
630+
Name val = DatumGetName(input);
631+
632+
return PointerGetDatum(cstring_to_text(NameStr(*val)));
633+
}
634+
625635
static const bool text_rhs_is_varlena[] =
626-
{true};
636+
{true, false};
637+
638+
static const btree_gin_convert_function text_cvt_fns[] =
639+
{NULL, cvt_name_text};
627640

628641
static const PGFunction text_cmp_fns[] =
629-
{bttextcmp};
642+
{bttextcmp, btnametextcmp};
630643

631-
GIN_SUPPORT(text, leftmostvalue_text, text_rhs_is_varlena, NULL, text_cmp_fns)
644+
GIN_SUPPORT(text, leftmostvalue_text, text_rhs_is_varlena, text_cvt_fns, text_cmp_fns)
632645

633646
static const bool bpchar_rhs_is_varlena[] =
634647
{true};
@@ -828,13 +841,37 @@ leftmostvalue_name(void)
828841
return NameGetDatum(result);
829842
}
830843

844+
static Datum
845+
cvt_text_name(Datum input)
846+
{
847+
text *val = DatumGetTextPP(input);
848+
NameData *result = (NameData *) palloc0(NAMEDATALEN);
849+
int len = VARSIZE_ANY_EXHDR(val);
850+
851+
/*
852+
* Truncate oversize input. We're assuming this will produce a result
853+
* considered less than the original. That could be a bad assumption in
854+
* some collations, but fortunately an index on "name" is generally going
855+
* to use C collation.
856+
*/
857+
if (len >= NAMEDATALEN)
858+
len = pg_mbcliplen(VARDATA_ANY(val), len, NAMEDATALEN - 1);
859+
860+
memcpy(NameStr(*result), VARDATA_ANY(val), len);
861+
862+
return NameGetDatum(result);
863+
}
864+
831865
static const bool name_rhs_is_varlena[] =
832-
{false};
866+
{false, true};
867+
868+
static const btree_gin_convert_function name_cvt_fns[] =
869+
{NULL, cvt_text_name};
833870

834871
static const PGFunction name_cmp_fns[] =
835-
{btnamecmp};
872+
{btnamecmp, bttextnamecmp};
836873

837-
GIN_SUPPORT(name, leftmostvalue_name, name_rhs_is_varlena, NULL, name_cmp_fns)
874+
GIN_SUPPORT(name, leftmostvalue_name, name_rhs_is_varlena, name_cvt_fns, name_cmp_fns)
838875

839876
static Datum
840877
leftmostvalue_bool(void)

contrib/btree_gin/expected/name.out

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,62 @@ EXPLAIN (COSTS OFF) SELECT * FROM test_name WHERE i>'abc' ORDER BY i;
9595
Index Cond: (i > 'abc'::name)
9696
(6 rows)
9797

98+
explain (costs off)
99+
SELECT * FROM test_name WHERE i<'abc'::text ORDER BY i;
100+
QUERY PLAN
101+
---------------------------------------------
102+
Sort
103+
Sort Key: i
104+
-> Bitmap Heap Scan on test_name
105+
Recheck Cond: (i < 'abc'::text)
106+
-> Bitmap Index Scan on idx_name
107+
Index Cond: (i < 'abc'::text)
108+
(6 rows)
109+
110+
SELECT * FROM test_name WHERE i<'abc'::text ORDER BY i;
111+
i
112+
-----
113+
a
114+
ab
115+
abb
116+
(3 rows)
117+
118+
SELECT * FROM test_name WHERE i<='abc'::text ORDER BY i;
119+
i
120+
-----
121+
a
122+
ab
123+
abb
124+
abc
125+
(4 rows)
126+
127+
SELECT * FROM test_name WHERE i='abc'::text ORDER BY i;
128+
i
129+
-----
130+
abc
131+
(1 row)
132+
133+
SELECT * FROM test_name WHERE i>='abc'::text ORDER BY i;
134+
i
135+
-----
136+
abc
137+
axy
138+
xyz
139+
(3 rows)
140+
141+
SELECT * FROM test_name WHERE i>'abc'::text ORDER BY i;
142+
i
143+
-----
144+
axy
145+
xyz
146+
(2 rows)
147+
148+
SELECT * FROM test_name WHERE i<=repeat('abc', 100) ORDER BY i;
149+
i
150+
-----
151+
a
152+
ab
153+
abb
154+
abc
155+
(4 rows)
156+

contrib/btree_gin/expected/text.out

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,53 @@ SELECT * FROM test_text WHERE i>'abc' ORDER BY i;
4242
xyz
4343
(2 rows)
4444

45+
explain (costs off)
46+
SELECT * FROM test_text WHERE i<'abc'::name COLLATE "default" ORDER BY i;
47+
QUERY PLAN
48+
---------------------------------------------------------------
49+
Sort
50+
Sort Key: i
51+
-> Bitmap Heap Scan on test_text
52+
Recheck Cond: (i < 'abc'::name COLLATE "default")
53+
-> Bitmap Index Scan on idx_text
54+
Index Cond: (i < 'abc'::name COLLATE "default")
55+
(6 rows)
56+
57+
SELECT * FROM test_text WHERE i<'abc'::name COLLATE "default" ORDER BY i;
58+
i
59+
-----
60+
a
61+
ab
62+
abb
63+
(3 rows)
64+
65+
SELECT * FROM test_text WHERE i<='abc'::name COLLATE "default" ORDER BY i;
66+
i
67+
-----
68+
a
69+
ab
70+
abb
71+
abc
72+
(4 rows)
73+
74+
SELECT * FROM test_text WHERE i='abc'::name COLLATE "default" ORDER BY i;
75+
i
76+
-----
77+
abc
78+
(1 row)
79+
80+
SELECT * FROM test_text WHERE i>='abc'::name COLLATE "default" ORDER BY i;
81+
i
82+
-----
83+
abc
84+
axy
85+
xyz
86+
(3 rows)
87+
88+
SELECT * FROM test_text WHERE i>'abc'::name COLLATE "default" ORDER BY i;
89+
i
90+
-----
91+
axy
92+
xyz
93+
(2 rows)
94+

contrib/btree_gin/sql/name.sql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,14 @@ EXPLAIN (COSTS OFF) SELECT * FROM test_name WHERE i<='abc' ORDER BY i;
1919
EXPLAIN (COSTS OFF) SELECT * FROM test_name WHERE i='abc' ORDER BY i;
2020
EXPLAIN (COSTS OFF) SELECT * FROM test_name WHERE i>='abc' ORDER BY i;
2121
EXPLAIN (COSTS OFF) SELECT * FROM test_name WHERE i>'abc' ORDER BY i;
22+
23+
explain (costs off)
24+
SELECT * FROM test_name WHERE i<'abc'::text ORDER BY i;
25+
26+
SELECT * FROM test_name WHERE i<'abc'::text ORDER BY i;
27+
SELECT * FROM test_name WHERE i<='abc'::text ORDER BY i;
28+
SELECT * FROM test_name WHERE i='abc'::text ORDER BY i;
29+
SELECT * FROM test_name WHERE i>='abc'::text ORDER BY i;
30+
SELECT * FROM test_name WHERE i>'abc'::text ORDER BY i;
31+
32+
SELECT * FROM test_name WHERE i<=repeat('abc', 100) ORDER BY i;

contrib/btree_gin/sql/text.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,12 @@ SELECT * FROM test_text WHERE i<='abc' ORDER BY i;
1313
SELECT * FROM test_text WHERE i='abc' ORDER BY i;
1414
SELECT * FROM test_text WHERE i>='abc' ORDER BY i;
1515
SELECT * FROM test_text WHERE i>'abc' ORDER BY i;
16+
17+
explain (costs off)
18+
SELECT * FROM test_text WHERE i<'abc'::name COLLATE "default" ORDER BY i;
19+
20+
SELECT * FROM test_text WHERE i<'abc'::name COLLATE "default" ORDER BY i;
21+
SELECT * FROM test_text WHERE i<='abc'::name COLLATE "default" ORDER BY i;
22+
SELECT * FROM test_text WHERE i='abc'::name COLLATE "default" ORDER BY i;
23+
SELECT * FROM test_text WHERE i>='abc'::name COLLATE "default" ORDER BY i;
24+
SELECT * FROM test_text WHERE i>'abc'::name COLLATE "default" ORDER BY i;

0 commit comments

Comments
 (0)