Skip to content

Commit 01838f3

Browse files
committed
Add range type over units: unitrange.
Suggested by Gavin Wahl, thanks! Close #22.
1 parent 754f2d2 commit 01838f3

File tree

7 files changed

+77
-0
lines changed

7 files changed

+77
-0
lines changed

Diff for: NEWS.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* Support Infinity and NaN.
1212
* Add strict comparison operators that error out when dimensions of arguments
1313
do not match: << <<= == <<>> >>= >>.
14+
* Add range type over units: unitrange.
1415

1516
6.0: Mar 7, 2018
1617
----------------

Diff for: debian/changelog

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ postgresql-unit (7.0-1) UNRELEASED; urgency=medium
1111
* Support Infinity and NaN.
1212
* Add strict comparison operators that error out when dimensions of
1313
arguments do not match: << <<= == <<>> >>= >>.
14+
* Add range type over units: unitrange.
1415

1516
-- Christoph Berg <[email protected]> Sun, 08 Jul 2018 21:46:17 +0200
1617

Diff for: expected/compare.out

+25
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,28 @@ SELECT '1 m'::unit >> '1 m'::unit;
232232

233233
SELECT '1 m'::unit >> '1 A'::unit;
234234
ERROR: dimension mismatch in "strict comparison" operation: "1 m", "1 A"
235+
-- test range type
236+
SELECT 'empty'::unitrange;
237+
unitrange
238+
-----------
239+
empty
240+
(1 row)
241+
242+
SELECT '(1 m, 2 m)'::unitrange;
243+
unitrange
244+
---------------
245+
("1 m","2 m")
246+
(1 row)
247+
248+
SELECT '(1 m, 2 A)'::unitrange;
249+
ERROR: dimension mismatch in "strict comparison" operation: "1 m", "2 A"
250+
LINE 1: SELECT '(1 m, 2 A)'::unitrange;
251+
^
252+
SELECT unit_diff('1 A', '2 A');
253+
unit_diff
254+
-----------
255+
-1
256+
(1 row)
257+
258+
SELECT unit_diff('1 A', '2 K');
259+
ERROR: dimension mismatch in "-" operation: "1 A", "2 K"

Diff for: sql/compare.sql

+7
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,10 @@ SELECT '1 m'::unit >>= '1 m'::unit;
5151
SELECT '1 m'::unit >>= '1 A'::unit;
5252
SELECT '1 m'::unit >> '1 m'::unit;
5353
SELECT '1 m'::unit >> '1 A'::unit;
54+
55+
-- test range type
56+
SELECT 'empty'::unitrange;
57+
SELECT '(1 m, 2 m)'::unitrange;
58+
SELECT '(1 m, 2 A)'::unitrange;
59+
SELECT unit_diff('1 A', '2 A');
60+
SELECT unit_diff('1 A', '2 K');

Diff for: unit--6--7.sql.in

+15
Original file line numberDiff line numberDiff line change
@@ -95,5 +95,20 @@ CREATE OPERATOR CLASS unit_strict_ops
9595
OPERATOR 5 >> ,
9696
FUNCTION 1 unit_strict_cmp(unit, unit);
9797

98+
-- range type
99+
100+
CREATE FUNCTION unit_diff(unit, unit)
101+
RETURNS float8
102+
AS '$libdir/unit'
103+
LANGUAGE C IMMUTABLE STRICT;
104+
105+
COMMENT ON FUNCTION unit_diff IS 'returns difference of two units as float8 for use in the unitrange type';
106+
107+
CREATE TYPE unitrange AS RANGE (
108+
SUBTYPE = unit,
109+
SUBTYPE_OPCLASS = unit_strict_ops,
110+
SUBTYPE_DIFF = unit_diff
111+
);
112+
98113
-- load prefixes and units tables
99114
SELECT unit_load();

Diff for: unit--7.sql.in

+15
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,21 @@ CREATE OPERATOR CLASS unit_strict_ops
576576
OPERATOR 5 >> ,
577577
FUNCTION 1 unit_strict_cmp(unit, unit);
578578

579+
-- range type
580+
581+
CREATE FUNCTION unit_diff(unit, unit)
582+
RETURNS float8
583+
AS '$libdir/unit'
584+
LANGUAGE C IMMUTABLE STRICT;
585+
586+
COMMENT ON FUNCTION unit_diff IS 'returns difference of two units as float8 for use in the unitrange type';
587+
588+
CREATE TYPE unitrange AS RANGE (
589+
SUBTYPE = unit,
590+
SUBTYPE_OPCLASS = unit_strict_ops,
591+
SUBTYPE_DIFF = unit_diff
592+
);
593+
579594
-- aggregates
580595

581596
CREATE AGGREGATE sum(unit)

Diff for: unit.c

+13
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,19 @@ unit_round(PG_FUNCTION_ARGS)
728728
PG_RETURN_POINTER(result);
729729
}
730730

731+
PG_FUNCTION_INFO_V1(unit_diff);
732+
733+
Datum
734+
unit_diff(PG_FUNCTION_ARGS)
735+
{
736+
Unit *a = (Unit *) PG_GETARG_POINTER(0);
737+
Unit *b = (Unit *) PG_GETARG_POINTER(1);
738+
Unit result;
739+
740+
unit_sub_internal(a, b, &result);
741+
PG_RETURN_FLOAT8(result.value);
742+
}
743+
731744
/* operators */
732745

733746
PG_FUNCTION_INFO_V1(unit_add);

0 commit comments

Comments
 (0)