-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add comparison and arithmetic operators - Add tests and Github workflow - Improve the README
- Loading branch information
Showing
17 changed files
with
1,002 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
name: CI | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
- main | ||
pull_request: | ||
|
||
jobs: | ||
test: | ||
strategy: | ||
matrix: | ||
pg: [16, 15, 14, 13, 12, 11, 10] | ||
name: 🐘 PostgreSQL ${{ matrix.pg }} | ||
runs-on: ubuntu-latest | ||
container: pgxn/pgxn-tools | ||
steps: | ||
- run: pg-start ${{ matrix.pg }} | ||
- uses: actions/checkout@v2 | ||
- run: pg-build-test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
*.o | ||
*.so | ||
|
||
.deps/ | ||
|
||
test/results | ||
test/regression.diffs | ||
test/regression.out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,15 @@ | ||
EXTENSION = saturated_int | ||
MODULE_big = saturated_int | ||
|
||
PG_CONFIG ?= PG_CONFIG | ||
PG_CONFIG ?= pg_config | ||
|
||
DATA = $(wildcard *--*.sql) | ||
OBJS = $(patsubst %.c,%.o,$(wildcard src/*.c)) | ||
|
||
PGXS := $(shell $(PG_CONFIG) --pgxs) | ||
|
||
TESTS = $(sort $(wildcard test/sql/*.sql)) | ||
REGRESS = $(patsubst test/sql/%.sql,%,$(TESTS)) | ||
REGRESS_OPTS = --inputdir=test --outputdir=test | ||
|
||
include $(PGXS) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,88 @@ | ||
# pg-saturated_int | ||
|
||
An integer type with [saturation arithmetic](https://en.wikipedia.org/wiki/Saturation_arithmetic). | ||
The difference from builtin integer type is that if an input value is out of allowed range | ||
then the error `integer out of range` won't be raised. | ||
|
||
## Types | ||
|
||
### `saturated_int` | ||
|
||
`saturated_int` is an integer type which implements [saturation arithmetic](https://en.wikipedia.org/wiki/Saturation_arithmetic). | ||
Allowed range is `-2147483648` to `+2147483647`. | ||
|
||
```sql | ||
-- Cast to saturated_int | ||
|
||
select 999999999999999::saturated_int; | ||
saturated_int | ||
--------------- | ||
2147483647 | ||
|
||
select 2147483648::saturated_int; | ||
saturated_int | ||
--------------- | ||
2147483647 | ||
|
||
select (-2147483649)::saturated_int; | ||
saturated_int | ||
--------------- | ||
-2147483648 | ||
``` | ||
|
||
## Supported operators | ||
|
||
`saturated_int` supports comparison (`<`, `<=`, `<>`, `=`, `>`, `>=`) and arithmetic operators. Here is some examples: | ||
|
||
```sql | ||
select 999999999999999::saturated_int > 2147483648::saturated_int; | ||
?column? | ||
---------- | ||
f | ||
|
||
select 999999999999999::saturated_int = 2147483648::saturated_int; | ||
?column? | ||
---------- | ||
t | ||
|
||
select 999999999999999::saturated_int * 2147483648::saturated_int; | ||
?column? | ||
------------ | ||
2147483647 | ||
|
||
select (-999999999999999)::saturated_int * 2147483648::saturated_int; | ||
?column? | ||
------------- | ||
-2147483648 | ||
|
||
select 2147483648::saturated_int / (-1)::saturated_int; | ||
?column? | ||
------------- | ||
-2147483647 | ||
``` | ||
|
||
Note that it is necessary to explicitly cast both of operands to `saturated_int`. Implicit cast isn't supported: | ||
|
||
```sql | ||
select 2147483647::int = 2147483648::saturated_int; | ||
ERROR: operator does not exist: integer = saturated_int at character 24 | ||
|
||
select 2147483647::int * 2147483648::saturated_int; | ||
ERROR: operator does not exist: integer * saturated_int at character 24 | ||
``` | ||
|
||
## Index support | ||
|
||
The extension supports `btree` and `hash` indexes. | ||
|
||
## Installation from source codes | ||
|
||
To install `saturated_int`, execute this in the extension's directory: | ||
|
||
```shell | ||
make install | ||
``` | ||
|
||
> **Notice:** Don't forget to set the `PG_CONFIG` variable (`make PG_CONFIG=...`) | ||
> in case you want to test `saturated_int` on a non-default or custom build of PostgreSQL. | ||
> Read more [here](https://wiki.postgresql.org/wiki/Building_and_Installing_PostgreSQL_Extension_Modules). |
Oops, something went wrong.