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

Commit bcbd609

Browse files
tglsfdcCommitfest Bot
authored and
Commitfest Bot
committed
Add cross-type comparisons for float types.
1 parent ec9b362 commit bcbd609

File tree

6 files changed

+381
-6
lines changed

6 files changed

+381
-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
@@ -61,3 +61,23 @@ ADD
6161
OPERATOR 20 >= (int8, int4),
6262
OPERATOR 21 > (int8, int4)
6363
;
64+
65+
ALTER OPERATOR FAMILY float4_ops USING gin
66+
ADD
67+
-- Code 1: RHS is float8
68+
OPERATOR 9 < (float4, float8),
69+
OPERATOR 10 <= (float4, float8),
70+
OPERATOR 11 = (float4, float8),
71+
OPERATOR 12 >= (float4, float8),
72+
OPERATOR 13 > (float4, float8)
73+
;
74+
75+
ALTER OPERATOR FAMILY float8_ops USING gin
76+
ADD
77+
-- Code 1: RHS is float4
78+
OPERATOR 9 < (float8, float4),
79+
OPERATOR 10 <= (float8, float4),
80+
OPERATOR 11 = (float8, float4),
81+
OPERATOR 12 >= (float8, float4),
82+
OPERATOR 13 > (float8, float4)
83+
;

contrib/btree_gin/btree_gin.c

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -393,27 +393,59 @@ leftmostvalue_float4(void)
393393
return Float4GetDatum(-get_float4_infinity());
394394
}
395395

396+
static Datum
397+
cvt_float8_float4(Datum input)
398+
{
399+
float8 val = DatumGetFloat8(input);
400+
float4 result;
401+
402+
/*
403+
* Assume that ordinary C conversion will produce a usable result.
404+
* (Compare dtof(), which raises error conditions that we don't need.)
405+
* Note that for inputs that aren't exactly representable as float4, it
406+
* doesn't matter whether the conversion rounds up or down. That might
407+
* cause us to scan a few index entries that we'll reject as not matching,
408+
* but we won't miss any that should match.
409+
*/
410+
result = (float4) val;
411+
return Float4GetDatum(result);
412+
}
413+
396414
static const bool float4_rhs_is_varlena[] =
397-
{false};
415+
{false, false};
416+
417+
static const btree_gin_convert_function float4_cvt_fns[] =
418+
{NULL, cvt_float8_float4};
398419

399420
static const PGFunction float4_cmp_fns[] =
400-
{btfloat4cmp};
421+
{btfloat4cmp, btfloat84cmp};
401422

402-
GIN_SUPPORT(float4, leftmostvalue_float4, float4_rhs_is_varlena, NULL, float4_cmp_fns)
423+
GIN_SUPPORT(float4, leftmostvalue_float4, float4_rhs_is_varlena, float4_cvt_fns, float4_cmp_fns)
403424

404425
static Datum
405426
leftmostvalue_float8(void)
406427
{
407428
return Float8GetDatum(-get_float8_infinity());
408429
}
409430

431+
static Datum
432+
cvt_float4_float8(Datum input)
433+
{
434+
float4 val = DatumGetFloat4(input);
435+
436+
return Float8GetDatum((float8) val);
437+
}
438+
410439
static const bool float8_rhs_is_varlena[] =
411-
{false};
440+
{false, false};
441+
442+
static const btree_gin_convert_function float8_cvt_fns[] =
443+
{NULL, cvt_float4_float8};
412444

413445
static const PGFunction float8_cmp_fns[] =
414-
{btfloat8cmp};
446+
{btfloat8cmp, btfloat48cmp};
415447

416-
GIN_SUPPORT(float8, leftmostvalue_float8, float8_rhs_is_varlena, NULL, float8_cmp_fns)
448+
GIN_SUPPORT(float8, leftmostvalue_float8, float8_rhs_is_varlena, float8_cvt_fns, float8_cmp_fns)
417449

418450
static Datum
419451
leftmostvalue_money(void)

contrib/btree_gin/expected/float4.out

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,230 @@ SELECT * FROM test_float4 WHERE i>1::float4 ORDER BY i;
4242
3
4343
(2 rows)
4444

45+
explain (costs off)
46+
SELECT * FROM test_float4 WHERE i<1::float8 ORDER BY i;
47+
QUERY PLAN
48+
-------------------------------------------------------
49+
Sort
50+
Sort Key: i
51+
-> Bitmap Heap Scan on test_float4
52+
Recheck Cond: (i < '1'::double precision)
53+
-> Bitmap Index Scan on idx_float4
54+
Index Cond: (i < '1'::double precision)
55+
(6 rows)
56+
57+
SELECT * FROM test_float4 WHERE i<1::float8 ORDER BY i;
58+
i
59+
----
60+
-2
61+
-1
62+
0
63+
(3 rows)
64+
65+
SELECT * FROM test_float4 WHERE i<=1::float8 ORDER BY i;
66+
i
67+
----
68+
-2
69+
-1
70+
0
71+
1
72+
(4 rows)
73+
74+
SELECT * FROM test_float4 WHERE i=1::float8 ORDER BY i;
75+
i
76+
---
77+
1
78+
(1 row)
79+
80+
SELECT * FROM test_float4 WHERE i>=1::float8 ORDER BY i;
81+
i
82+
---
83+
1
84+
2
85+
3
86+
(3 rows)
87+
88+
SELECT * FROM test_float4 WHERE i>1::float8 ORDER BY i;
89+
i
90+
---
91+
2
92+
3
93+
(2 rows)
94+
95+
-- Check endpoint and out-of-range cases
96+
INSERT INTO test_float4 VALUES ('NaN'), ('Inf'), ('-Inf');
97+
SELECT * FROM test_float4 WHERE i<'-Inf'::float8 ORDER BY i;
98+
i
99+
---
100+
(0 rows)
101+
102+
SELECT * FROM test_float4 WHERE i<='-Inf'::float8 ORDER BY i;
103+
i
104+
-----------
105+
-Infinity
106+
(1 row)
107+
108+
SELECT * FROM test_float4 WHERE i='-Inf'::float8 ORDER BY i;
109+
i
110+
-----------
111+
-Infinity
112+
(1 row)
113+
114+
SELECT * FROM test_float4 WHERE i>='-Inf'::float8 ORDER BY i;
115+
i
116+
-----------
117+
-Infinity
118+
-2
119+
-1
120+
0
121+
1
122+
2
123+
3
124+
Infinity
125+
NaN
126+
(9 rows)
127+
128+
SELECT * FROM test_float4 WHERE i>'-Inf'::float8 ORDER BY i;
129+
i
130+
----------
131+
-2
132+
-1
133+
0
134+
1
135+
2
136+
3
137+
Infinity
138+
NaN
139+
(8 rows)
140+
141+
SELECT * FROM test_float4 WHERE i<'Inf'::float8 ORDER BY i;
142+
i
143+
-----------
144+
-Infinity
145+
-2
146+
-1
147+
0
148+
1
149+
2
150+
3
151+
(7 rows)
152+
153+
SELECT * FROM test_float4 WHERE i<='Inf'::float8 ORDER BY i;
154+
i
155+
-----------
156+
-Infinity
157+
-2
158+
-1
159+
0
160+
1
161+
2
162+
3
163+
Infinity
164+
(8 rows)
165+
166+
SELECT * FROM test_float4 WHERE i='Inf'::float8 ORDER BY i;
167+
i
168+
----------
169+
Infinity
170+
(1 row)
171+
172+
SELECT * FROM test_float4 WHERE i>='Inf'::float8 ORDER BY i;
173+
i
174+
----------
175+
Infinity
176+
NaN
177+
(2 rows)
178+
179+
SELECT * FROM test_float4 WHERE i>'Inf'::float8 ORDER BY i;
180+
i
181+
-----
182+
NaN
183+
(1 row)
184+
185+
SELECT * FROM test_float4 WHERE i<'1e300'::float8 ORDER BY i;
186+
i
187+
-----------
188+
-Infinity
189+
-2
190+
-1
191+
0
192+
1
193+
2
194+
3
195+
(7 rows)
196+
197+
SELECT * FROM test_float4 WHERE i<='1e300'::float8 ORDER BY i;
198+
i
199+
-----------
200+
-Infinity
201+
-2
202+
-1
203+
0
204+
1
205+
2
206+
3
207+
(7 rows)
208+
209+
SELECT * FROM test_float4 WHERE i='1e300'::float8 ORDER BY i;
210+
i
211+
---
212+
(0 rows)
213+
214+
SELECT * FROM test_float4 WHERE i>='1e300'::float8 ORDER BY i;
215+
i
216+
----------
217+
Infinity
218+
NaN
219+
(2 rows)
220+
221+
SELECT * FROM test_float4 WHERE i>'1e300'::float8 ORDER BY i;
222+
i
223+
----------
224+
Infinity
225+
NaN
226+
(2 rows)
227+
228+
SELECT * FROM test_float4 WHERE i<'NaN'::float8 ORDER BY i;
229+
i
230+
-----------
231+
-Infinity
232+
-2
233+
-1
234+
0
235+
1
236+
2
237+
3
238+
Infinity
239+
(8 rows)
240+
241+
SELECT * FROM test_float4 WHERE i<='NaN'::float8 ORDER BY i;
242+
i
243+
-----------
244+
-Infinity
245+
-2
246+
-1
247+
0
248+
1
249+
2
250+
3
251+
Infinity
252+
NaN
253+
(9 rows)
254+
255+
SELECT * FROM test_float4 WHERE i='NaN'::float8 ORDER BY i;
256+
i
257+
-----
258+
NaN
259+
(1 row)
260+
261+
SELECT * FROM test_float4 WHERE i>='NaN'::float8 ORDER BY i;
262+
i
263+
-----
264+
NaN
265+
(1 row)
266+
267+
SELECT * FROM test_float4 WHERE i>'NaN'::float8 ORDER BY i;
268+
i
269+
---
270+
(0 rows)
271+

contrib/btree_gin/expected/float8.out

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

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

0 commit comments

Comments
 (0)