Skip to content

Commit

Permalink
Added more tests. Added documentation. Set initial release version.
Browse files Browse the repository at this point in the history
  • Loading branch information
beargiles committed Jul 19, 2015
1 parent b46dfdb commit 50beda0
Show file tree
Hide file tree
Showing 14 changed files with 523 additions and 74 deletions.
16 changes: 16 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
License

PostgreSQL is released under the PostgreSQL License, a liberal Open Source license, similar to the BSD or MIT licenses.

PostgreSQL Database Management System
(formerly known as Postgres, then as Postgres95)

Portions Copyright (c) 1996-2015, The PostgreSQL Global Development Group

Portions Copyright (c) 1994, The Regents of the University of California

Permission to use, copy, modify, and distribute this software and its documentation for any purpose, without fee, and without a written agreement is hereby granted, provided that the above copyright notice and this paragraph and the following two paragraphs appear in all copies.

IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
4 changes: 2 additions & 2 deletions META.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "bignum",
"abstract": "Unlimited precision integers",
"description": "Unlimited precision integers provided by the OpenSSL library",
"version": "0.0.1",
"version": "0.8.0",
"maintainer": "Bear Giles <bgiles@coyotesong.com>"
"license": "postgresql",
"provides": {
Expand All @@ -13,7 +13,7 @@
"version": "0.0.1"
}
},
"release_status": "unstable",
"release_status": "stable",
"resources": {
"repository": {
"url": "https://github.com/beargiles/pg-bignum.git",
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
bignum
======

This is a user-defined type for unlimited precision integers. It is backed by the OpenSSL library so
it should be available on most if not all PostgreSQL installations without the need for another library.
This is a user-defined type for unlimited precision integers. It is
backed by the OpenSSL library so it should be available on most if
not all PostgreSQL installations without the need for another library.

To build it, just do this:

Expand Down Expand Up @@ -72,6 +73,7 @@ so:

Dependencies
------------

The `bignum` data type depends on the OpenSSL library.

Copyright and License
Expand Down
2 changes: 1 addition & 1 deletion bignum.control
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# bignum extension
comment = 'Unlimited precision integers'
default_version = '0.0.1'
default_version = '0.8.0'
module_pathname = '$libdir/bignum'
relocatable = true
87 changes: 77 additions & 10 deletions doc/bignum.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,99 @@ bignum
Synopsis
--------

'bignum' is a user-defined type that implements unlimited precision integers
'bignum' is a user-defined type that implements unlimited precision integers.

Description
-----------

'bignum' is a user-defined type that implements unlimited-precision integers using the
OpenSSL library.
OpenSSL library. All of the standard arithmetic, equality, and comparison operations are
defined as expected.

Usage
-----
The greatest common demoninator (GCD) function is supported.

You can create a bignum by casting an int4 or int8 value to this type. It is not
currently possible to cast from a bignum to an int4 or int8 value due to the risk of
overflow.
The aggregate functions (min, max, sum, avg) are not supported at this time.

All of the standard arithmetic, equality, and comparison operations are defined as
expected.
Casts to/from DECIMAL, NUMERIC, and existing multiple-precision integer extensions
are not supported at this time.

(TBD: gcd, etc.)
Usage
-----

Install the extension in the usual manner.

```sql
CREATE EXTENSION bignum;
```

The bignum can now be used as a standard type.

```sql
CREATE TABLE test (
a INT,
b BIGNUM
);

INSERT INTO table(a, b) VALUES (1, '1');
INSERT INTO table(a, b) VALUES (2, 2);
INSERT INTO table(a, b) VALUES (3, 3::int8);
```

We can now do standard math on the type. Note that int4 and int8 values are
automatically cast to bignum values so we don't have to do it manually.


```sql
bgiles=# create table t (a int, b bignum, c bignum);
CREATE TABLE
bgiles=# insert into t values (1, 1, 2), (2, 3, 4), (3, -2, -3);
INSERT 0 3

-- comparisons
bgiles=# select a, b, c, b < c as d, b <= c as d, b = c as f, b >= c as g, b > c as h, b <> c as i from t;
a | b | c | d | d | f | g | h | i
---+----+----+---+---+---+---+---+---
1 | 1 | 2 | t | t | f | f | f | t
2 | 3 | 4 | t | t | f | f | f | t
3 | -2 | -3 | f | f | f | t | t | t
(3 rows)

-- negation and absolute values.
bgiles=# select a, b, c, -b as d, abs(c) as e from t;
a | b | c | d | e
---+----+----+----+---
1 | 1 | 2 | -1 | 2
2 | 3 | 4 | -3 | 4
3 | -2 | -3 | 2 | 3
(3 rows)

-- basic math
bgiles=# select a, b, c, b+c as d, b-c as e, b*c as f, b/c as g, b%c as h from t;
a | b | c | d | e | f | g | h
---+----+----+----+----+----+---+----
1 | 1 | 2 | 3 | -1 | 2 | 0 | 1
2 | 3 | 4 | 7 | -1 | 12 | 0 | 3
3 | -2 | -3 | -5 | 1 | 6 | 0 | -2
(3 rows)

-- Greatest common denominator.
bgiles=# select gcd(3::bignum, 12::bignum) as a, gcd(5::bignum, 12::bignum) as b;
a | b
---+---
3 | 1
(1 row)

```

Support
-------

Source code is available at https://github.com/beargiles/pg-bignum.

Write me if you need additional functionality. There's a good chance that I've already
started work on it but have deferred publication until I identify a need and resolve
a few open questions.

Author
------

Expand Down
83 changes: 40 additions & 43 deletions sql/bignum.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
/*
* Author: Bear Giles <bgiles@coyotesong.com>
* Created at: 2015-07-18 18:03:42 -0600
*/

--
-- This file is released under the PostgreSQL license by its author,
-- Bear Giles <bgiles@coyotesong.com>
--
-- -------------------------------------------------------------------------------
--
-- Definition of BIGNUM user-defined type for unlimited precision extensions.
--
-- This extension is one of several interacting extensions built upon the OpenSSL
-- library but does not include any cryptographic functionality. The OpenSSL library
-- will usually be installed by default with PostgreSQL, local laws permitting.
--
-- This extension is not compatible the existing DECIMAL, NUMERIC, or other
-- unlimited precision integer extensions at this time.
--
-- If you're interested in Galois fields please write me. I've done some preliminary
-- work based upon this extension but it is not ready for publication.
--
-- Author: Bear Giles <bgiles@coyotesong.com>
-- Created at: 2015-07-18 18:03:42 -0600
--
-- -------------------------------------------------------------------------------
--
-- create type
--
Expand Down Expand Up @@ -285,6 +302,10 @@ CREATE OPERATOR - (
PROCEDURE = bn_negate
);

CREATE OR REPLACE FUNCTION abs(bignum) RETURNS bignum
AS 'bignum', 'pgx_bignum_abs'
LANGUAGE C IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION bn_add(bignum, bignum) RETURNS bignum
AS 'bignum', 'pgx_bignum_add'
LANGUAGE C IMMUTABLE STRICT;
Expand Down Expand Up @@ -345,21 +366,6 @@ CREATE OR REPLACE FUNCTION bn_modulus_i8(int8, bignum) RETURNS bignum
AS 'bignum', 'pgx_bignum_modulus_i8b'
LANGUAGE C IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION bn_gcd(bignum, bignum) RETURNS bignum
AS 'bignum', 'pgx_bignum_gcd'
LANGUAGE C IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION bn_gcd_i8(bignum, int8) RETURNS bignum
AS 'bignum', 'pgx_bignum_gcd_i8'
LANGUAGE C IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION bn_gcd_i8(int8, bignum) RETURNS bignum AS $$
SELECT bn_gcd_i8($2, $1);
$$ LANGUAGE SQL IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION bn_gcd_ii(int8, int8) RETURNS bignum
AS 'bignum', 'pgx_bignum_gcd_ii'
LANGUAGE C IMMUTABLE STRICT;

CREATE OPERATOR + (
LEFTARG = bignum,
Expand Down Expand Up @@ -457,32 +463,23 @@ CREATE OPERATOR % (
PROCEDURE = bn_modulus_i8
);

CREATE OPERATOR | (
LEFTARG = bignum,
RIGHTARG = bignum,
PROCEDURE = bn_gcd
);

CREATE OPERATOR | (
LEFTARG = bignum,
RIGHTARG = int8,
PROCEDURE = bn_gcd_i8
);
--
-- Greatest common denominator.
--
CREATE OR REPLACE FUNCTION gcd(bignum, bignum) RETURNS bignum
AS 'bignum', 'pgx_bignum_gcd'
LANGUAGE C IMMUTABLE STRICT;

CREATE OPERATOR | (
LEFTARG = int8,
RIGHTARG = bignum,
PROCEDURE = bn_gcd_i8
);
CREATE OR REPLACE FUNCTION gcd(bignum, int8) RETURNS bignum
AS 'bignum', 'pgx_bignum_gcd_i8'
LANGUAGE C IMMUTABLE STRICT;

CREATE OPERATOR | (
LEFTARG = int8,
RIGHTARG = int8,
PROCEDURE = bn_gcd_ii
);
CREATE OR REPLACE FUNCTION gcd(int8, bignum) RETURNS bignum AS $$
SELECT gcd($2, $1);
$$ LANGUAGE SQL IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION bn_abs(bignum) RETURNS bool
AS 'bignum', 'pgx_bignum_abs'
CREATE OR REPLACE FUNCTION gcd(int8, int8) RETURNS bignum
AS 'bignum', 'pgx_bignum_gcd_ii'
LANGUAGE C IMMUTABLE STRICT;

--
Expand Down
10 changes: 10 additions & 0 deletions sql/uninstall_bignum.sql
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
--
-- This file is released under the PostgreSQL license by its author,
-- Bear Giles <bgiles@coyotesong.com>
--
-- -------------------------------------------------------------------------------

--
-- Drop the user-defined types. Note: this will also nuke any user tables that include
-- this type.
--
DROP TYPE bignum CASCADE;

0 comments on commit 50beda0

Please sign in to comment.