Skip to content

Commit e0f0cf6

Browse files
committed
Support send/receive via the binary protocol.
Close #21
1 parent 5edaccc commit e0f0cf6

File tree

11 files changed

+276
-56
lines changed

11 files changed

+276
-56
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ DATA_built = unit--2--3.sql unit--3.sql \
99
unit--4--5.sql unit--5.sql \
1010
unit--5--6.sql unit--6.sql \
1111
unit--6--7.sql unit--7.sql
12-
REGRESS = extension tables unit unicode prefix units time temperature functions round derived compare aggregate iec custom
12+
REGRESS = extension tables unit binary unicode prefix units time temperature functions round derived compare aggregate iec custom
1313
# Jessie's and trusty's bison (3.0.2) is buggy, ship pregenerated .tab files
1414
EXTRA_CLEAN = unitparse.yy.* powers powers.o unit-*.dump # unitparse.tab.*
1515

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Add π to known units.
77
* Remove bison 2 compat mode, but keep pregenerated files for jessie and
88
trusty.
9+
* Support send/receive via the binary protocol.
910

1011
6.0: Mar 7, 2018
1112
----------------

README.md

Lines changed: 124 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ All operators and functions operate with that base-units-only format.
143143
Conversion from and to other unit representations is done on input, output, and
144144
using the `@` operator only.
145145

146+
This is also the format used in binary mode in the PostgreSQL wire protocol.
147+
146148
Prefixes and Units Lookup Tables
147149
--------------------------------
148150

@@ -324,54 +326,129 @@ PostgreSQL Operators and Functions
324326
The `unit` extension provides the following objects:
325327

326328
```
327-
function avg(unit)
328-
function cbrt(unit)
329-
function dimension(unit)
330-
function max(unit)
331-
function min(unit)
332-
function round(unit)
333-
function sqrt(unit)
334-
function stddev_pop(unit)
335-
function stddev_samp(unit)
336-
function stddev(unit)
337-
function sum(unit)
338-
function unit_is_hashed(cstring)
339-
function unit_load()
340-
function unit_reset()
341-
function value(unit)
342-
function variance(unit)
343-
function var_pop(unit)
344-
function var_samp(unit)
345-
operator class unit_ops for access method btree
346-
operator /(double precision,unit)
347-
operator *(double precision,unit)
348-
operator family unit_ops for access method btree
349-
operator ||/(NONE,unit)
350-
operator |/(NONE,unit)
351-
operator -(NONE,unit)
352-
operator @(unit,text)
353-
operator @@(unit,text)
354-
operator /(unit,double precision)
355-
operator *(unit,double precision)
356-
operator ^(unit,integer)
357-
operator <=(unit,unit)
358-
operator <>(unit,unit)
359-
operator <(unit,unit)
360-
operator =(unit,unit)
361-
operator >=(unit,unit)
362-
operator >(unit,unit)
363-
operator -(unit,unit)
364-
operator /(unit,unit)
365-
operator *(unit,unit)
366-
operator +(unit,unit)
367-
table unit_prefixes
368-
table unit_units
369-
type unit
370-
type unit_accum_t
371-
```
329+
Objects in extension "unit"
330+
Object description
331+
--------------------------------------------------
332+
function ampere(double precision)
333+
function au(double precision)
334+
function avg(unit)
335+
function becquerel(double precision)
336+
function byte(double precision)
337+
function candela(double precision)
338+
function cbrt(unit)
339+
function celsius(double precision)
340+
function coulomb(double precision)
341+
function day(double precision)
342+
function dbl_unit_div(double precision,unit)
343+
function dbl_unit_mul(double precision,unit)
344+
function decibel(double precision)
345+
function degree_arc(double precision)
346+
function dimension(unit)
347+
function farad(double precision)
348+
function gray(double precision)
349+
function hectare(double precision)
350+
function henry(double precision)
351+
function hertz(double precision)
352+
function hour(double precision)
353+
function joule(double precision)
354+
function katal(double precision)
355+
function kelvin(double precision)
356+
function kilogram(double precision)
357+
function liter(double precision)
358+
function lumen(double precision)
359+
function lux(double precision)
360+
function max(unit)
361+
function meter(double precision)
362+
function min(unit)
363+
function minute_arc(double precision)
364+
function minute(double precision)
365+
function mole(double precision)
366+
function newton(double precision)
367+
function ohm(double precision)
368+
function pascal(double precision)
369+
function radian(double precision)
370+
function round(unit)
371+
function second_arc(double precision)
372+
function second(double precision)
373+
function siemens(double precision)
374+
function sievert(double precision)
375+
function sqrt(unit)
376+
function stddev_pop(unit)
377+
function stddev_samp(unit)
378+
function stddev(unit)
379+
function steradian(double precision)
380+
function sum(unit)
381+
function tesla(double precision)
382+
function tonne(double precision)
383+
function unit_accum(unit_accum_t,unit)
384+
function unit_add(unit,unit)
385+
function unit_at_double(unit,text)
386+
function unit_at(unit,text)
387+
function unit_avg(unit_accum_t)
388+
function unit_cmp(unit,unit)
389+
function unit_dbl_div(unit,double precision)
390+
function unit_dbl_mul(unit,double precision)
391+
function unit_div(unit,unit)
392+
function unit(double precision)
393+
function unit_eq(unit,unit)
394+
function unit_ge(unit,unit)
395+
function unit_greatest(unit,unit)
396+
function unit_gt(unit,unit)
397+
function unit_in(cstring)
398+
function unit_is_hashed(cstring)
399+
function unit_least(unit,unit)
400+
function unit_le(unit,unit)
401+
function unit_load()
402+
function unit_lt(unit,unit)
403+
function unit_mul(unit,unit)
404+
function unit_neg(unit)
405+
function unit_ne(unit,unit)
406+
function unit_out(unit)
407+
function unit_pow(unit,integer)
408+
function unit_recv(internal)
409+
function unit_reset()
410+
function unit_send(unit)
411+
function unit_stddev_pop(unit_accum_t)
412+
function unit_stddev_samp(unit_accum_t)
413+
function unit_sub(unit,unit)
414+
function unit_var_pop(unit_accum_t)
415+
function unit_var_samp(unit_accum_t)
416+
function value(unit)
417+
function variance(unit)
418+
function var_pop(unit)
419+
function var_samp(unit)
420+
function volt(double precision)
421+
function watt(double precision)
422+
function weber(double precision)
423+
operator class unit_ops for access method btree
424+
operator /(double precision,unit)
425+
operator *(double precision,unit)
426+
operator family unit_ops for access method btree
427+
operator ||/(NONE,unit)
428+
operator |/(NONE,unit)
429+
operator -(NONE,unit)
430+
operator /(unit,double precision)
431+
operator *(unit,double precision)
432+
operator ^(unit,integer)
433+
operator @(unit,text)
434+
operator @@(unit,text)
435+
operator <=(unit,unit)
436+
operator <>(unit,unit)
437+
operator <(unit,unit)
438+
operator =(unit,unit)
439+
operator >=(unit,unit)
440+
operator >(unit,unit)
441+
operator -(unit,unit)
442+
operator /(unit,unit)
443+
operator *(unit,unit)
444+
operator +(unit,unit)
445+
table unit_prefixes
446+
table unit_units
447+
type unit
448+
type unit_accum_t
449+
(117 rows)
372450
373-
Internal function names and constructor-like functions (`meter(5)`) are omitted
374-
in this list.
451+
```
375452

376453
Details:
377454

debian/changelog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ postgresql-unit (7.0-1) UNRELEASED; urgency=medium
66
* Add π to known units.
77
* Remove bison 2 compat mode, but keep pregenerated files for jessie and
88
trusty.
9+
* Support send/receive via the binary protocol.
910

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

expected/binary.out

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
SELECT unit_send('0');
2+
unit_send
3+
------------------------------------
4+
\x00000000000000000000000000000000
5+
(1 row)
6+
7+
SELECT unit_send(-'0'::unit);
8+
unit_send
9+
------------------------------------
10+
\x80000000000000000000000000000000
11+
(1 row)
12+
13+
SELECT unit_send('1 m'::unit);
14+
unit_send
15+
------------------------------------
16+
\x3ff00000000000000100000000000000
17+
(1 row)
18+
19+
SELECT unit_send('1 kg^-1'::unit);
20+
unit_send
21+
------------------------------------
22+
\x3ff000000000000000ff000000000000
23+
(1 row)
24+
25+
SELECT unit_send('1 s'::unit);
26+
unit_send
27+
------------------------------------
28+
\x3ff00000000000000000010000000000
29+
(1 row)
30+
31+
SELECT unit_send('1 A^-1'::unit);
32+
unit_send
33+
------------------------------------
34+
\x3ff0000000000000000000ff00000000
35+
(1 row)
36+
37+
SELECT unit_send('1 K'::unit);
38+
unit_send
39+
------------------------------------
40+
\x3ff00000000000000000000001000000
41+
(1 row)
42+
43+
SELECT unit_send('1 mol^-1'::unit);
44+
unit_send
45+
------------------------------------
46+
\x3ff00000000000000000000000ff0000
47+
(1 row)
48+
49+
SELECT unit_send('1 cd'::unit);
50+
unit_send
51+
------------------------------------
52+
\x3ff00000000000000000000000000100
53+
(1 row)
54+
55+
SELECT unit_send('1 B^-1'::unit);
56+
unit_send
57+
------------------------------------
58+
\x3ff000000000000000000000000000ff
59+
(1 row)
60+

expected/upgrade.out

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,25 @@ ALTER EXTENSION unit UPDATE TO "6";
3434
:pg_depend_save
3535
\! pg_dump -f unit-5-6.dump -T pg_depend_save
3636
:pg_depend_restore
37+
-- upgrade to version 7
38+
ALTER EXTENSION unit UPDATE TO "7";
39+
:pg_depend_save
40+
\! pg_dump -f unit-6-7.dump -T pg_depend_save
41+
:pg_depend_restore
3742
-- reinstall latest version
3843
DROP EXTENSION unit CASCADE;
3944
CREATE EXTENSION unit;
4045
:pg_depend_save
41-
\! pg_dump -f unit-6.dump -T pg_depend_save
46+
\! pg_dump -f unit-create.dump -T pg_depend_save
4247
:pg_depend_restore
4348
-- different ordering in unit_accum_t expected:
44-
\! diff -u --label unit-5-6.dump unit-5-6.dump --label unit-6.dump unit-6.dump | sed -e 's/@@.*@@/@@/'
45-
--- unit-5-6.dump
46-
+++ unit-6.dump
49+
\! diff -u --label unit-update.dump unit-6-7.dump --label unit-create.dump unit-create.dump | sed -e 's/@@.*@@/@@/'
50+
--- unit-update.dump
51+
+++ unit-create.dump
4752
@@
4853

49-
CREATE TYPE unit_accum_t AS (
50-
s unit,
54+
CREATE TYPE public.unit_accum_t AS (
55+
s public.unit,
5156
- n bigint,
5257
- squares double precision
5358
+ squares double precision,

sql/binary.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
SELECT unit_send('0');
2+
SELECT unit_send(-'0'::unit);
3+
SELECT unit_send('1 m'::unit);
4+
SELECT unit_send('1 kg^-1'::unit);
5+
SELECT unit_send('1 s'::unit);
6+
SELECT unit_send('1 A^-1'::unit);
7+
SELECT unit_send('1 K'::unit);
8+
SELECT unit_send('1 mol^-1'::unit);
9+
SELECT unit_send('1 cd'::unit);
10+
SELECT unit_send('1 B^-1'::unit);

sql/upgrade.sql

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,18 @@ ALTER EXTENSION unit UPDATE TO "6";
4242
\! pg_dump -f unit-5-6.dump -T pg_depend_save
4343
:pg_depend_restore
4444

45+
-- upgrade to version 7
46+
ALTER EXTENSION unit UPDATE TO "7";
47+
:pg_depend_save
48+
\! pg_dump -f unit-6-7.dump -T pg_depend_save
49+
:pg_depend_restore
50+
4551
-- reinstall latest version
4652
DROP EXTENSION unit CASCADE;
4753
CREATE EXTENSION unit;
4854
:pg_depend_save
49-
\! pg_dump -f unit-6.dump -T pg_depend_save
55+
\! pg_dump -f unit-create.dump -T pg_depend_save
5056
:pg_depend_restore
5157

5258
-- different ordering in unit_accum_t expected:
53-
\! diff -u --label unit-5-6.dump unit-5-6.dump --label unit-6.dump unit-6.dump | sed -e 's/@@.*@@/@@/'
59+
\! diff -u --label unit-update.dump unit-6-7.dump --label unit-create.dump unit-create.dump | sed -e 's/@@.*@@/@@/'

unit--6--7.sql.in

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
-- add recv/send functions
2+
3+
CREATE FUNCTION unit_recv(internal)
4+
RETURNS unit
5+
AS '$libdir/unit'
6+
LANGUAGE C IMMUTABLE STRICT;
7+
8+
CREATE FUNCTION unit_send(unit)
9+
RETURNS bytea
10+
AS '$libdir/unit'
11+
LANGUAGE C IMMUTABLE STRICT;
12+
13+
UPDATE pg_type SET typreceive = 'unit_recv', typsend = 'unit_send'
14+
WHERE typname = 'unit' AND typnamespace = '@extschema@'::regnamespace;
15+
16+
INSERT INTO pg_depend (classid, objid, objsubid, refclassid, refobjid, refobjsubid, deptype)
17+
VALUES
18+
('pg_type'::regclass, 'unit'::regtype, 0, 'pg_proc'::regclass, 'unit_recv'::regproc, 0, 'n'),
19+
('pg_type'::regclass, 'unit'::regtype, 0, 'pg_proc'::regclass, 'unit_send'::regproc, 0, 'n');
20+
121
-- convert @ from (unit, text)->cstring to (unit, text)->text
222

323
DROP OPERATOR @ (unit, text);

unit--7.sql.in

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,22 @@ CREATE FUNCTION unit_out(unit)
2727
AS '$libdir/unit'
2828
LANGUAGE C IMMUTABLE STRICT;
2929

30+
CREATE FUNCTION unit_recv(internal)
31+
RETURNS unit
32+
AS '$libdir/unit'
33+
LANGUAGE C IMMUTABLE STRICT;
34+
35+
CREATE FUNCTION unit_send(unit)
36+
RETURNS bytea
37+
AS '$libdir/unit'
38+
LANGUAGE C IMMUTABLE STRICT;
39+
3040
CREATE TYPE unit (
3141
internallength = 16,
3242
input = unit_in,
3343
output = unit_out,
44+
receive = unit_recv,
45+
send = unit_send,
3446
alignment = double
3547
);
3648

0 commit comments

Comments
 (0)