mirror of
https://github.com/postgres/postgres.git
synced 2025-12-12 02:37:31 +03:00
Implement the width_bucket() function, per SQL2003. This commit only adds
a variant of the function for the 'numeric' datatype; it would be possible to add additional variants for other datatypes, but I haven't done so yet. This commit includes regression tests and minimal documentation; if we want developers to actually use this function in applications, we'll probably need to document what it does more fully.
This commit is contained in:
@@ -730,6 +730,57 @@ SELECT a, ceil(a), ceiling(a), floor(a), round(a) FROM ceil_floor_round;
|
||||
(7 rows)
|
||||
|
||||
DROP TABLE ceil_floor_round;
|
||||
-- Testing for width_bucket()
|
||||
-- NULL result
|
||||
SELECT width_bucket(NULL, NULL, NULL, NULL);
|
||||
width_bucket
|
||||
--------------
|
||||
|
||||
(1 row)
|
||||
|
||||
-- errors
|
||||
SELECT width_bucket(5.0, 3.0, 4.0, 0);
|
||||
ERROR: count must be greater than zero
|
||||
SELECT width_bucket(5.0, 3.0, 4.0, -5);
|
||||
ERROR: count must be greater than zero
|
||||
SELECT width_bucket(3.0, 3.0, 3.0, 888);
|
||||
ERROR: lower bound cannot equal upper bound
|
||||
-- normal operation
|
||||
CREATE TABLE width_bucket_test (operand numeric);
|
||||
COPY width_bucket_test FROM stdin;
|
||||
SELECT
|
||||
operand,
|
||||
width_bucket(operand, 0, 10, 5) AS wb_1,
|
||||
width_bucket(operand, 10, 0, 5) AS wb_2,
|
||||
width_bucket(operand, 2, 8, 4) AS wb_3,
|
||||
width_bucket(operand, 5.0, 5.5, 20) AS wb_4,
|
||||
width_bucket(operand, -25, 25, 10) AS wb_5
|
||||
FROM width_bucket_test;
|
||||
operand | wb_1 | wb_2 | wb_3 | wb_4 | wb_5
|
||||
------------------+------+------+------+------+------
|
||||
-5.2 | 0 | 6 | 0 | 0 | 4
|
||||
-0.0000000000001 | 0 | 6 | 0 | 0 | 5
|
||||
0.0000000000001 | 1 | 5 | 0 | 0 | 6
|
||||
1 | 1 | 5 | 0 | 0 | 6
|
||||
1.99999999999999 | 1 | 5 | 0 | 0 | 6
|
||||
2 | 2 | 5 | 1 | 0 | 6
|
||||
2.00000000000001 | 2 | 4 | 1 | 0 | 6
|
||||
3 | 2 | 4 | 1 | 0 | 6
|
||||
4 | 3 | 4 | 2 | 0 | 6
|
||||
4.5 | 3 | 3 | 2 | 0 | 6
|
||||
5 | 3 | 3 | 3 | 1 | 7
|
||||
5.5 | 3 | 3 | 3 | 21 | 7
|
||||
6 | 4 | 3 | 3 | 21 | 7
|
||||
7 | 4 | 2 | 4 | 21 | 7
|
||||
8 | 5 | 2 | 5 | 21 | 7
|
||||
9 | 5 | 1 | 5 | 21 | 7
|
||||
9.99999999999999 | 5 | 1 | 5 | 21 | 7
|
||||
10 | 6 | 1 | 5 | 21 | 8
|
||||
10.0000000000001 | 6 | 0 | 5 | 21 | 8
|
||||
NaN | 6 | 0 | 5 | 21 | 11
|
||||
(20 rows)
|
||||
|
||||
DROP TABLE width_bucket_test;
|
||||
-- TO_CHAR()
|
||||
--
|
||||
SELECT '' AS to_char_1, to_char(val, '9G999G999G999G999G999')
|
||||
|
||||
@@ -667,6 +667,52 @@ INSERT INTO ceil_floor_round VALUES ('-0.000001');
|
||||
SELECT a, ceil(a), ceiling(a), floor(a), round(a) FROM ceil_floor_round;
|
||||
DROP TABLE ceil_floor_round;
|
||||
|
||||
-- Testing for width_bucket()
|
||||
-- NULL result
|
||||
SELECT width_bucket(NULL, NULL, NULL, NULL);
|
||||
|
||||
-- errors
|
||||
SELECT width_bucket(5.0, 3.0, 4.0, 0);
|
||||
SELECT width_bucket(5.0, 3.0, 4.0, -5);
|
||||
SELECT width_bucket(3.0, 3.0, 3.0, 888);
|
||||
|
||||
-- normal operation
|
||||
CREATE TABLE width_bucket_test (operand numeric);
|
||||
|
||||
COPY width_bucket_test FROM stdin;
|
||||
-5.2
|
||||
-0.0000000000001
|
||||
0.0000000000001
|
||||
1
|
||||
1.99999999999999
|
||||
2
|
||||
2.00000000000001
|
||||
3
|
||||
4
|
||||
4.5
|
||||
5
|
||||
5.5
|
||||
6
|
||||
7
|
||||
8
|
||||
9
|
||||
9.99999999999999
|
||||
10
|
||||
10.0000000000001
|
||||
NaN
|
||||
\.
|
||||
|
||||
SELECT
|
||||
operand,
|
||||
width_bucket(operand, 0, 10, 5) AS wb_1,
|
||||
width_bucket(operand, 10, 0, 5) AS wb_2,
|
||||
width_bucket(operand, 2, 8, 4) AS wb_3,
|
||||
width_bucket(operand, 5.0, 5.5, 20) AS wb_4,
|
||||
width_bucket(operand, -25, 25, 10) AS wb_5
|
||||
FROM width_bucket_test;
|
||||
|
||||
DROP TABLE width_bucket_test;
|
||||
|
||||
-- TO_CHAR()
|
||||
--
|
||||
SELECT '' AS to_char_1, to_char(val, '9G999G999G999G999G999')
|
||||
|
||||
Reference in New Issue
Block a user