• Stars
    star
    229
  • Rank 174,666 (Top 4 %)
  • Language PLpgSQL
  • License
    MIT License
  • Created almost 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Precise fractional arithmetic for PostgreSQL

Precise fractions for PostgreSQL

An efficient custom type. Perfect for exact arithmetic or user-specified table row ordering. Holds values as big as an integer, with matching precision in the denominator.

Features

  • Stores fractions in exactly 64 bits (same size as float)
  • Written in C for high performance
  • Detects and halts arithmetic overflow for correctness
  • Uses native CPU instructions for fast overflow detection
  • Defers GCD calculation until requested or absolutely required
  • Supports btree and hash indices
  • Implements Stern-Brocot trees for finding intermediate points
  • Coercion from integer/bigint/tuple
  • Custom aggregate

Motivation

See my blog post about User-Defined Order in SQL.

Usage

Basics

-- fractions are precise
-- this would not work with a float type
select 1::rational / 3 * 3 = 1;
-- => t

-- provides the usual operations, e.g.
select '1/3'::rational + '2/7';
-- => 13/21

-- helper "ratt' type to coerce from tuples
select 1 + (i,i+1)::ratt from generate_series(1,5) as i;
-- => 3/2, 5/3, 7/4, 9/5, 11/6

-- simplify if desired
select rational_simplify('36/12');
-- => 3/1

-- convert float to rational
select 0.263157894737::float::rational;
-- => 5/19

-- convert rational to float
select '-1/2'::rational::float;
-- => -0.5

Next, reordering items without renumbering surrounding items.

Note we use an integer sequence rather than the default of bigint, and explicitly cast nextval(). There is no conversion from bigint to rational because numerators in this extension can hold at most integer range anyway.

create sequence todos_seq as integer;

create table todos (
  prio rational unique
    default nextval('todos_seq')::integer,
  what text not null
);

insert into todos (what) values
  ('install extension'),
  ('read about it'),
  ('try it'),
  ('profit?');

select * from todos order by prio asc;
/*
β”Œβ”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ prio β”‚       what        β”‚
β”œβ”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ 1/1  β”‚ install extension β”‚
β”‚ 2/1  β”‚ read about it     β”‚
β”‚ 3/1  β”‚ try it            β”‚
β”‚ 4/1  β”‚ profit?           β”‚
β””β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
*/

-- put "try" between "install" and "read"
update todos
set prio = rational_intermediate(1,2)
where prio = 3;

select * from todos order by prio asc;
/*
β”Œβ”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ prio β”‚       what        β”‚
β”œβ”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ 1/1  β”‚ install extension β”‚
β”‚ 3/2  β”‚ try it            β”‚
β”‚ 2/1  β”‚ read about it     β”‚
β”‚ 4/1  β”‚ profit?           β”‚
β””β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
*/

-- put "read" back between "install" and "try"
update todos
set prio = rational_intermediate(1,'3/2')
where prio = 2;

select * from todos order by prio asc;
/*
β”Œβ”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ prio β”‚       what        β”‚
β”œβ”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ 1/1  β”‚ install extension β”‚
β”‚ 4/3  β”‚ read about it     β”‚
β”‚ 3/2  β”‚ try it            β”‚
β”‚ 4/1  β”‚ profit?           β”‚
β””β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
*/

This extension uses Stern-Brocot trees to find efficient intermediate points as fractions in lowest terms. It can continue to split deeper between fractions as much as any practical application requires.

Using floats, on the other hand, and picking the midpoints between adjacent values runs out of space rapidly (you only need 50-odd inserts at the wrong spot to start hitting problems).

Installation

Clone this repo, go inside and simply run:

make
sudo make install

Then, in your database:

create extension pg_rational;

Caveats

The rational_intermediate function is super fast on typical intervals, but the narrower the range between arguments the longer it takes. We may want to add a max search depth parameter to prevent malicious values from hogging the server.

Thanks

This is my first PostgreSQL extension, and these resources were helpful in learning to write it.

More Repositories

1

css-ratiocinator

because your CSS is garbage
JavaScript
1,027
star
2

haskell-vim-now

One-line Haskell Vim install
Shell
987
star
3

angular-paginate-anything

Γ€ la carte server-side pagination
JavaScript
517
star
4

lucre

Let people pay you for any or no reason.
Ruby
506
star
5

heroku-buildpack-ghc

Deploy Haskell apps to Heroku
Shell
272
star
6

objgrep

Find strings inside complicated javascript objects
JavaScript
160
star
7

immutube

Youtube + functors, a most unlikely combo
JavaScript
141
star
8

pg_listen

Trigger shell command from NOTIFY
C
83
star
9

gitftp

Browse git over anonymous FTP
C
82
star
10

mimedown

markdown to multipart mime
C
79
star
11

c-mix

Demo of C project with Haskell functions
C
77
star
12

microservice-template

Basic architecture for running and monitoring workers
Shell
70
star
13

postgrest-example

Migrations for an example conference API
PLpgSQL
70
star
14

styleguide_rails

generate a living styleguide with one command
JavaScript
69
star
15

libderp

C collections. Easy to build, boring algorithms. Dumb is good.
C
45
star
16

haskell-pair

Haskell pair programming server via Vagrant
Shell
41
star
17

react-showpiece

Eminently styleable markup
CSS
37
star
18

obsd

Redacted config files
Vim Script
36
star
19

clean_pagination

API pagination the way RFC7233 intended it
Ruby
33
star
20

vimrc

An old plugin-heavy vim config (I do things differently nowadays)
Vim Script
31
star
21

algorithm-freezer

Know your algorithms cold!
Haskell
29
star
22

utofu

Unicode Trust on First Use (TOFU)
C
28
star
23

haskell-circle-example

Exemplary Stack-based template for Circle CI
Haskell
16
star
24

groupthink

obey the masses with realtime voting
JavaScript
15
star
25

autolytics

An embarrassment of analytics riches
12
star
26

wc

Beating haskell with C
C
11
star
27

haskell-postgres-examples

A cookbook for Postgres in Haskell
Haskell
10
star
28

picobounce

Experiment to build an IRC bouncer out of independent programs connected by named pipes. Only half done.
C
8
star
29

mother-structures

Programming via abstract math
CoffeeScript
8
star
30

randln

Fast, flexible, portable way to get random lines out of files
C
6
star
31

lexyacc

Learning to parse
Lex
6
star
32

dumbus

🚌 Hyperefficient bus planning for dumbphones
Haskell
6
star
33

aeson-t

Transform JSON
Haskell
6
star
34

libc

Implementing the C standard library
C
5
star
35

posix-man

Man pages for POSIX (sections 0,1,3; issues 6 and 7)
Roff
5
star
36

clache

Run Lazy K programs in the cloud
Ruby
5
star
37

aws_pipes

AWS queues Γ  la Unix
Ruby
5
star
38

libscorm

Create SCORM 2004 courses in Flash or HTML
JavaScript
5
star
39

findrss

Search (un)common paths to find the Atom or RSS feed of a site
Shell
5
star
40

lancelot

Your knight in shining ASCII armor
Haskell
4
star
41

sinatra-sql

Easy PostgreSQL access with migrations in Sinatra
Ruby
4
star
42

generator-omni-module

Write JS modules that run everywhere
JavaScript
4
star
43

micro-scraper

Microservice to download pages
Haskell
4
star
44

wchar-conformance

Test ISO 10646 conformance of wchar_t
C
4
star
45

ordinary

Transfinite arithmetic in Ruby
Ruby
4
star
46

kr

Quick 'n dirty K&R exercises
C
3
star
47

incl

Preprocessor to include files into output stream
C
3
star
48

philosoraptor

Logic programming in JavaScript
3
star
49

qwertywords

Words that are fun to type like figment ajangle
C
3
star
50

jsobject

Nicer JavaScript objects through ExternalInterface in ActionScript 3
ActionScript
3
star
51

itertools

Python itertools for Ruby
Ruby
3
star
52

turing

There are many like it, but this one is mine
C
2
star
53

float

Experiments in number representations, a collaboration with Eric Bavier
C
2
star
54

endoxa-graph

everything's connected, man
JavaScript
2
star
55

doublekill

Weird experiments with signals
C
2
star
56

twittective

Some screen scraping in Haskell
Haskell
2
star
57

quickcheck-simple

Project template for doing Haskell exercises
Haskell
2
star
58

angular-http-batch

Leap multiple $http requests in a single bound
JavaScript
2
star
59

retags

Keep ctags up to date with minimal overhead
Shell
2
star
60

pthread-test

Exercises from Butenhof's book
C
2
star
61

semln

Filter to add semantic line breaks in many languages
C
2
star
62

hasql-stress

Trying to reproduce deadlock
Haskell
2
star
63

git-zophrenic

Finds the secret government messages in your Git hashes
Shell
1
star
64

stalk27

When will route 27 actually arrive? No more lies.
Shell
1
star
65

jvscrpt

Drop the function arguments and write less code
JavaScript
1
star
66

multiparse

Drafts for my parsing article
Yacc
1
star
67

ringfifo

Nonblocking multi-use named pipes
C
1
star
68

begriffs.com

my site
CSS
1
star
69

CopernicusJS

A revolution in CSS positioning
JavaScript
1
star
70

angular-patience

Efficiently wait for long server processing
JavaScript
1
star
71

flexicode

Tools scanning Unicode in Flex
C
1
star
72

stm32f411

STM32F4 (STM32F411CEU6) "black pill" development with non-proprietary tools
C
1
star
73

scheme48hrs

Write yourself a Scheme in 48 hours exercises
Haskell
1
star
74

getclojure-ui

A front-end for devn/getclojure
Ruby
1
star
75

apue

Advanced Programming in the UNIX Environment
C
1
star
76

1up

Project page for my one-year challenge
Ruby
1
star
77

warp-async-test

Can warp handle multiple requests at once?
Haskell
1
star
78

rustybank

Concurrency project created by "mob programming" at a meetup
Rust
1
star
79

crapyesod

Haskell
1
star
80

semiquandle

Experiments in classification
Python
1
star
81

mathnotes

Graduate mathematics notes in TeX
1
star
82

cmsis_nucleo

Experiments with CMSIS (+device family pack) and RTOS with static allocation
C
1
star
83

enigma

Totally random dynamic CSS
JavaScript
1
star
84

preview-ics

Preview iCal invitations from the command line (or mutt)
1
star
85

heroku-buildpack-ghc-test-yesod

Test yesod server deployment
CSS
1
star
86

motif

Trying examples from "Motif Programming" by Marshall Brain
C
1
star
87

heroku-buildpack-ghc-test-snap

Test snap server deployment
Haskell
1
star
88

slow-warp

Why isn't warp responding concurrently?
Haskell
1
star
89

decaying-accumulator

A number that tends toward zero but can be nudged
JavaScript
1
star
90

githhub-care-package

Daily list of repos that need your help
1
star