mirror of
https://github.com/postgres/postgres.git
synced 2025-07-27 12:41:57 +03:00
Add window RANGE support for float4, float8, numeric.
Commit 0a459cec9
left this for later, but since time's running out,
I went ahead and took care of it. There are more data types that
somebody might someday want RANGE support for, but this is enough
to satisfy all expectations of the SQL standard, which just says that
"numeric, datetime, and interval" types should have RANGE support.
This commit is contained in:
@ -1864,6 +1864,191 @@ from generate_series(-9223372036854775806, -9223372036854775804) x;
|
||||
-9223372036854775806 | -9223372036854775806
|
||||
(3 rows)
|
||||
|
||||
-- Test in_range for other numeric datatypes
|
||||
create temp table numerics(
|
||||
id int,
|
||||
f_float4 float4,
|
||||
f_float8 float8,
|
||||
f_numeric numeric
|
||||
);
|
||||
insert into numerics values
|
||||
(0, '-infinity', '-infinity', '-1000'), -- numeric type lacks infinities
|
||||
(1, -3, -3, -3),
|
||||
(2, -1, -1, -1),
|
||||
(3, 0, 0, 0),
|
||||
(4, 1.1, 1.1, 1.1),
|
||||
(5, 1.12, 1.12, 1.12),
|
||||
(6, 2, 2, 2),
|
||||
(7, 100, 100, 100),
|
||||
(8, 'infinity', 'infinity', '1000'),
|
||||
(9, 'NaN', 'NaN', 'NaN');
|
||||
select id, f_float4, first_value(id) over w, last_value(id) over w
|
||||
from numerics
|
||||
window w as (order by f_float4 range between
|
||||
1 preceding and 1 following);
|
||||
id | f_float4 | first_value | last_value
|
||||
----+-----------+-------------+------------
|
||||
0 | -Infinity | 0 | 0
|
||||
1 | -3 | 1 | 1
|
||||
2 | -1 | 2 | 3
|
||||
3 | 0 | 2 | 3
|
||||
4 | 1.1 | 4 | 6
|
||||
5 | 1.12 | 4 | 6
|
||||
6 | 2 | 4 | 6
|
||||
7 | 100 | 7 | 7
|
||||
8 | Infinity | 8 | 8
|
||||
9 | NaN | 9 | 9
|
||||
(10 rows)
|
||||
|
||||
select id, f_float4, first_value(id) over w, last_value(id) over w
|
||||
from numerics
|
||||
window w as (order by f_float4 range between
|
||||
1 preceding and 1.1::float4 following);
|
||||
id | f_float4 | first_value | last_value
|
||||
----+-----------+-------------+------------
|
||||
0 | -Infinity | 0 | 0
|
||||
1 | -3 | 1 | 1
|
||||
2 | -1 | 2 | 3
|
||||
3 | 0 | 2 | 4
|
||||
4 | 1.1 | 4 | 6
|
||||
5 | 1.12 | 4 | 6
|
||||
6 | 2 | 4 | 6
|
||||
7 | 100 | 7 | 7
|
||||
8 | Infinity | 8 | 8
|
||||
9 | NaN | 9 | 9
|
||||
(10 rows)
|
||||
|
||||
select id, f_float4, first_value(id) over w, last_value(id) over w
|
||||
from numerics
|
||||
window w as (order by f_float4 range between
|
||||
'inf' preceding and 'inf' following);
|
||||
id | f_float4 | first_value | last_value
|
||||
----+-----------+-------------+------------
|
||||
0 | -Infinity | 0 | 8
|
||||
1 | -3 | 0 | 8
|
||||
2 | -1 | 0 | 8
|
||||
3 | 0 | 0 | 8
|
||||
4 | 1.1 | 0 | 8
|
||||
5 | 1.12 | 0 | 8
|
||||
6 | 2 | 0 | 8
|
||||
7 | 100 | 0 | 8
|
||||
8 | Infinity | 0 | 8
|
||||
9 | NaN | 9 | 9
|
||||
(10 rows)
|
||||
|
||||
select id, f_float4, first_value(id) over w, last_value(id) over w
|
||||
from numerics
|
||||
window w as (order by f_float4 range between
|
||||
1.1 preceding and 'NaN' following); -- error, NaN disallowed
|
||||
ERROR: invalid preceding or following size in window function
|
||||
select id, f_float8, first_value(id) over w, last_value(id) over w
|
||||
from numerics
|
||||
window w as (order by f_float8 range between
|
||||
1 preceding and 1 following);
|
||||
id | f_float8 | first_value | last_value
|
||||
----+-----------+-------------+------------
|
||||
0 | -Infinity | 0 | 0
|
||||
1 | -3 | 1 | 1
|
||||
2 | -1 | 2 | 3
|
||||
3 | 0 | 2 | 3
|
||||
4 | 1.1 | 4 | 6
|
||||
5 | 1.12 | 4 | 6
|
||||
6 | 2 | 4 | 6
|
||||
7 | 100 | 7 | 7
|
||||
8 | Infinity | 8 | 8
|
||||
9 | NaN | 9 | 9
|
||||
(10 rows)
|
||||
|
||||
select id, f_float8, first_value(id) over w, last_value(id) over w
|
||||
from numerics
|
||||
window w as (order by f_float8 range between
|
||||
1 preceding and 1.1::float8 following);
|
||||
id | f_float8 | first_value | last_value
|
||||
----+-----------+-------------+------------
|
||||
0 | -Infinity | 0 | 0
|
||||
1 | -3 | 1 | 1
|
||||
2 | -1 | 2 | 3
|
||||
3 | 0 | 2 | 4
|
||||
4 | 1.1 | 4 | 6
|
||||
5 | 1.12 | 4 | 6
|
||||
6 | 2 | 4 | 6
|
||||
7 | 100 | 7 | 7
|
||||
8 | Infinity | 8 | 8
|
||||
9 | NaN | 9 | 9
|
||||
(10 rows)
|
||||
|
||||
select id, f_float8, first_value(id) over w, last_value(id) over w
|
||||
from numerics
|
||||
window w as (order by f_float8 range between
|
||||
'inf' preceding and 'inf' following);
|
||||
id | f_float8 | first_value | last_value
|
||||
----+-----------+-------------+------------
|
||||
0 | -Infinity | 0 | 8
|
||||
1 | -3 | 0 | 8
|
||||
2 | -1 | 0 | 8
|
||||
3 | 0 | 0 | 8
|
||||
4 | 1.1 | 0 | 8
|
||||
5 | 1.12 | 0 | 8
|
||||
6 | 2 | 0 | 8
|
||||
7 | 100 | 0 | 8
|
||||
8 | Infinity | 0 | 8
|
||||
9 | NaN | 9 | 9
|
||||
(10 rows)
|
||||
|
||||
select id, f_float8, first_value(id) over w, last_value(id) over w
|
||||
from numerics
|
||||
window w as (order by f_float8 range between
|
||||
1.1 preceding and 'NaN' following); -- error, NaN disallowed
|
||||
ERROR: invalid preceding or following size in window function
|
||||
select id, f_numeric, first_value(id) over w, last_value(id) over w
|
||||
from numerics
|
||||
window w as (order by f_numeric range between
|
||||
1 preceding and 1 following);
|
||||
id | f_numeric | first_value | last_value
|
||||
----+-----------+-------------+------------
|
||||
0 | -1000 | 0 | 0
|
||||
1 | -3 | 1 | 1
|
||||
2 | -1 | 2 | 3
|
||||
3 | 0 | 2 | 3
|
||||
4 | 1.1 | 4 | 6
|
||||
5 | 1.12 | 4 | 6
|
||||
6 | 2 | 4 | 6
|
||||
7 | 100 | 7 | 7
|
||||
8 | 1000 | 8 | 8
|
||||
9 | NaN | 9 | 9
|
||||
(10 rows)
|
||||
|
||||
select id, f_numeric, first_value(id) over w, last_value(id) over w
|
||||
from numerics
|
||||
window w as (order by f_numeric range between
|
||||
1 preceding and 1.1::numeric following);
|
||||
id | f_numeric | first_value | last_value
|
||||
----+-----------+-------------+------------
|
||||
0 | -1000 | 0 | 0
|
||||
1 | -3 | 1 | 1
|
||||
2 | -1 | 2 | 3
|
||||
3 | 0 | 2 | 4
|
||||
4 | 1.1 | 4 | 6
|
||||
5 | 1.12 | 4 | 6
|
||||
6 | 2 | 4 | 6
|
||||
7 | 100 | 7 | 7
|
||||
8 | 1000 | 8 | 8
|
||||
9 | NaN | 9 | 9
|
||||
(10 rows)
|
||||
|
||||
select id, f_numeric, first_value(id) over w, last_value(id) over w
|
||||
from numerics
|
||||
window w as (order by f_numeric range between
|
||||
1 preceding and 1.1::float8 following); -- currently unsupported
|
||||
ERROR: RANGE with offset PRECEDING/FOLLOWING is not supported for column type numeric and offset type double precision
|
||||
LINE 4: 1 preceding and 1.1::float8 following);
|
||||
^
|
||||
HINT: Cast the offset value to an appropriate type.
|
||||
select id, f_numeric, first_value(id) over w, last_value(id) over w
|
||||
from numerics
|
||||
window w as (order by f_numeric range between
|
||||
1.1 preceding and 'NaN' following); -- error, NaN disallowed
|
||||
ERROR: invalid preceding or following size in window function
|
||||
-- Test in_range for other datetime datatypes
|
||||
create temp table datetimes(
|
||||
id int,
|
||||
|
@ -489,6 +489,78 @@ from generate_series(9223372036854775804, 9223372036854775806) x;
|
||||
select x, last_value(x) over (order by x desc range between current row and 5 following)
|
||||
from generate_series(-9223372036854775806, -9223372036854775804) x;
|
||||
|
||||
-- Test in_range for other numeric datatypes
|
||||
|
||||
create temp table numerics(
|
||||
id int,
|
||||
f_float4 float4,
|
||||
f_float8 float8,
|
||||
f_numeric numeric
|
||||
);
|
||||
|
||||
insert into numerics values
|
||||
(0, '-infinity', '-infinity', '-1000'), -- numeric type lacks infinities
|
||||
(1, -3, -3, -3),
|
||||
(2, -1, -1, -1),
|
||||
(3, 0, 0, 0),
|
||||
(4, 1.1, 1.1, 1.1),
|
||||
(5, 1.12, 1.12, 1.12),
|
||||
(6, 2, 2, 2),
|
||||
(7, 100, 100, 100),
|
||||
(8, 'infinity', 'infinity', '1000'),
|
||||
(9, 'NaN', 'NaN', 'NaN');
|
||||
|
||||
select id, f_float4, first_value(id) over w, last_value(id) over w
|
||||
from numerics
|
||||
window w as (order by f_float4 range between
|
||||
1 preceding and 1 following);
|
||||
select id, f_float4, first_value(id) over w, last_value(id) over w
|
||||
from numerics
|
||||
window w as (order by f_float4 range between
|
||||
1 preceding and 1.1::float4 following);
|
||||
select id, f_float4, first_value(id) over w, last_value(id) over w
|
||||
from numerics
|
||||
window w as (order by f_float4 range between
|
||||
'inf' preceding and 'inf' following);
|
||||
select id, f_float4, first_value(id) over w, last_value(id) over w
|
||||
from numerics
|
||||
window w as (order by f_float4 range between
|
||||
1.1 preceding and 'NaN' following); -- error, NaN disallowed
|
||||
|
||||
select id, f_float8, first_value(id) over w, last_value(id) over w
|
||||
from numerics
|
||||
window w as (order by f_float8 range between
|
||||
1 preceding and 1 following);
|
||||
select id, f_float8, first_value(id) over w, last_value(id) over w
|
||||
from numerics
|
||||
window w as (order by f_float8 range between
|
||||
1 preceding and 1.1::float8 following);
|
||||
select id, f_float8, first_value(id) over w, last_value(id) over w
|
||||
from numerics
|
||||
window w as (order by f_float8 range between
|
||||
'inf' preceding and 'inf' following);
|
||||
select id, f_float8, first_value(id) over w, last_value(id) over w
|
||||
from numerics
|
||||
window w as (order by f_float8 range between
|
||||
1.1 preceding and 'NaN' following); -- error, NaN disallowed
|
||||
|
||||
select id, f_numeric, first_value(id) over w, last_value(id) over w
|
||||
from numerics
|
||||
window w as (order by f_numeric range between
|
||||
1 preceding and 1 following);
|
||||
select id, f_numeric, first_value(id) over w, last_value(id) over w
|
||||
from numerics
|
||||
window w as (order by f_numeric range between
|
||||
1 preceding and 1.1::numeric following);
|
||||
select id, f_numeric, first_value(id) over w, last_value(id) over w
|
||||
from numerics
|
||||
window w as (order by f_numeric range between
|
||||
1 preceding and 1.1::float8 following); -- currently unsupported
|
||||
select id, f_numeric, first_value(id) over w, last_value(id) over w
|
||||
from numerics
|
||||
window w as (order by f_numeric range between
|
||||
1.1 preceding and 'NaN' following); -- error, NaN disallowed
|
||||
|
||||
-- Test in_range for other datetime datatypes
|
||||
|
||||
create temp table datetimes(
|
||||
|
Reference in New Issue
Block a user