File tree 7 files changed +77
-0
lines changed
7 files changed +77
-0
lines changed Original file line number Diff line number Diff line change 11
11
* Support Infinity and NaN.
12
12
* Add strict comparison operators that error out when dimensions of arguments
13
13
do not match: << <<= == <<>> >>= >>.
14
+ * Add range type over units: unitrange.
14
15
15
16
6.0: Mar 7, 2018
16
17
----------------
Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ postgresql-unit (7.0-1) UNRELEASED; urgency=medium
11
11
* Support Infinity and NaN.
12
12
* Add strict comparison operators that error out when dimensions of
13
13
arguments do not match: << <<= == <<>> >>= >>.
14
+ * Add range type over units: unitrange.
14
15
15
16
-- Christoph Berg <
[email protected] > Sun, 08 Jul 2018 21:46:17 +0200
16
17
Original file line number Diff line number Diff line change @@ -232,3 +232,28 @@ SELECT '1 m'::unit >> '1 m'::unit;
232
232
233
233
SELECT '1 m'::unit >> '1 A'::unit;
234
234
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"
Original file line number Diff line number Diff line change @@ -51,3 +51,10 @@ SELECT '1 m'::unit >>= '1 m'::unit;
51
51
SELECT ' 1 m' ::unit >>= ' 1 A' ::unit;
52
52
SELECT ' 1 m' ::unit >> ' 1 m' ::unit;
53
53
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' );
Original file line number Diff line number Diff line change @@ -95,5 +95,20 @@ CREATE OPERATOR CLASS unit_strict_ops
95
95
OPERATOR 5 >> ,
96
96
FUNCTION 1 unit_strict_cmp(unit, unit);
97
97
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
+
98
113
-- load prefixes and units tables
99
114
SELECT unit_load();
Original file line number Diff line number Diff line change @@ -576,6 +576,21 @@ CREATE OPERATOR CLASS unit_strict_ops
576
576
OPERATOR 5 >> ,
577
577
FUNCTION 1 unit_strict_cmp(unit, unit);
578
578
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
+
579
594
-- aggregates
580
595
581
596
CREATE AGGREGATE sum(unit)
Original file line number Diff line number Diff line change @@ -728,6 +728,19 @@ unit_round(PG_FUNCTION_ARGS)
728
728
PG_RETURN_POINTER (result );
729
729
}
730
730
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
+
731
744
/* operators */
732
745
733
746
PG_FUNCTION_INFO_V1 (unit_add );
You can’t perform that action at this time.
0 commit comments