forked from dvarrazzo/pgmp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpgmp.pysql
421 lines (319 loc) · 8.59 KB
/
pgmp.pysql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/* pgmp -- Module installation SQL script
*
* Copyright (C) 2011 Daniele Varrazzo
*
* This file is part of the PostgreSQL GMP Module
*
* The PostgreSQL GMP Module is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 3 of the License,
* or (at your option) any later version.
*
* The PostgreSQL GMP Module is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the PostgreSQL GMP Module. If not, see
* http://www.gnu.org/licenses/.
*/
!# This file is made of a mix of Python code and SQL statements.
!# Use the script ``tools/unmix.py`` to convert it into plain SQL.
-- Adjust this setting to control where the objects get created.
SET search_path = public;
!! PYON
def func(sqlname, argin, argout=None, cname=None):
"""Create a SQL function from a C function"""
if not argout: argout = base_type
print "CREATE OR REPLACE FUNCTION %s(%s)" \
% (sqlname, ", ".join(argin.split()))
print "RETURNS", argout
if not cname: cname = "p" + sqlname
print "AS 'MODULE_PATHNAME', '%s'" % cname
print "LANGUAGE C IMMUTABLE STRICT;"
print
!! PYOFF
--
-- mpz user-defined type
--
!! PYON
base_type = 'mpz'
func('mpz_in', 'cstring', 'mpz')
func('mpz_out', 'mpz', 'cstring')
!! PYOFF
CREATE TYPE mpz (
INPUT = mpz_in
, OUTPUT = mpz_out
, INTERNALLENGTH = VARIABLE
, STORAGE = EXTENDED
, CATEGORY = 'N'
);
-- Other I/O functions
!! PYON
func('mpz', 'text int4', 'mpz', cname='pmpz_in_base')
func('text', 'mpz int4', 'cstring', cname='pmpz_out_base')
!! PYOFF
--
-- mpz cast
--
!! PYON
import re
def castfrom(typname, implicit=False):
"""Create a cast from a different type to `base_type`"""
fname = "%s_from_%s" % (base_type, typname)
func(fname, typname)
print "CREATE CAST (%s AS %s)" % (typname, base_type)
print "WITH FUNCTION %s(%s)" % (fname, typname)
print {'I': "AS IMPLICIT;", 'A': "AS ASSIGNMENT;", False: ';'}[implicit]
print
print
castfrom('int2', implicit='I')
castfrom('int4', implicit='I')
castfrom('int8', implicit='I')
castfrom('numeric', implicit='A')
def castto(typname, implicit=False):
fname = "%s_to_%s" % (base_type, typname)
"""Create a cast from `base_type` to a different type"""
func(fname, base_type, typname)
print "CREATE CAST (%s AS %s)" % (base_type, typname)
print "WITH FUNCTION %s(%s)" % (fname, base_type)
print {'I': "AS IMPLICIT;", 'A': "AS ASSIGNMENT;", False: ';'}[implicit]
print
print
castto('int8', implicit='A')
castto('int4', implicit='A')
castto('int2', implicit='A')
!! PYOFF
CREATE CAST (mpz AS numeric)
WITH INOUT
AS ASSIGNMENT;
--
-- mpz operators
--
!! PYON
func('mpz_uplus', 'mpz')
func('mpz_neg', 'mpz')
func('abs', 'mpz', cname='pmpz_abs')
!! PYOFF
CREATE OPERATOR - (
RIGHTARG = mpz,
PROCEDURE = mpz_neg
);
CREATE OPERATOR + (
RIGHTARG = mpz,
PROCEDURE = mpz_uplus
);
!! PYON
def op(sym, fname, rarg=None, comm=None):
"""Create an operator on `base_type`"""
if rarg == None: rarg = base_type
func('%s_%s' % (base_type, fname), base_type + " " + rarg)
print "CREATE OPERATOR %s (" % sym
print " LEFTARG = %s," % base_type
print " RIGHTARG = %s," % rarg
if comm: print " COMMUTATOR = %s," % comm
print " PROCEDURE = %s_%s" % (base_type, fname)
print ");"
print
print
op('+', 'add', comm='+')
op('-', 'sub')
op('*', 'mul', comm='*')
op('/', 'tdiv_q')
op('%', 'tdiv_r')
op('+/', 'cdiv_q')
op('+%', 'cdiv_r')
op('-/', 'fdiv_q')
op('-%', 'fdiv_r')
op('/!', 'divexact')
op('<<', 'mul_2exp', rarg='int4')
op('>>', 'tdiv_q_2exp', rarg='int4')
op('%>', 'tdiv_r_2exp', rarg='int4')
op('+>>', 'cdiv_q_2exp', rarg='int4')
op('+%>', 'cdiv_r_2exp', rarg='int4')
op('->>', 'fdiv_q_2exp', rarg='int4')
op('-%>', 'fdiv_r_2exp', rarg='int4')
func('pow', 'mpz int4', cname='pmpz_pow_ui')
!! PYOFF
CREATE OPERATOR ^ (
LEFTARG = mpz,
RIGHTARG = int4,
PROCEDURE = pow
);
--
-- mpz comparisons
--
CREATE OR REPLACE FUNCTION mpz_eq(mpz, mpz)
RETURNS boolean
AS 'MODULE_PATHNAME', 'pmpz_eq'
LANGUAGE C IMMUTABLE STRICT;
CREATE OPERATOR = (
LEFTARG = mpz
, RIGHTARG = mpz
, PROCEDURE = mpz_eq
, COMMUTATOR = =
, NEGATOR = <>
, RESTRICT = eqsel
, JOIN = eqjoinsel
, HASHES
, MERGES
);
CREATE OR REPLACE FUNCTION mpz_ne(mpz, mpz)
RETURNS boolean
AS 'MODULE_PATHNAME', 'pmpz_ne'
LANGUAGE C IMMUTABLE STRICT;
CREATE OPERATOR <> (
LEFTARG = mpz
, RIGHTARG = mpz
, PROCEDURE = mpz_ne
, COMMUTATOR = <>
, NEGATOR = =
, RESTRICT = neqsel
, JOIN = neqjoinsel
);
!! PYON
def bop(sym, fname, comm, neg):
"""Create an operator on `base_type` returning a bool"""
func('%s_%s' % (base_type, fname),
base_type + " " + base_type, argout='boolean')
fname1 = fname[0] + 't'
print "CREATE OPERATOR %s (" % sym
print " LEFTARG =", base_type
print " , RIGHTARG =", base_type
print " , PROCEDURE = %s_%s" % (base_type, fname)
print " , COMMUTATOR = %s" % comm
print " , NEGATOR = %s" % neg
print " , RESTRICT = scalar%ssel" % fname1
print " , JOIN = scalar%sjoinsel" % fname1
print ");"
print
print
bop('>', 'gt', comm='<', neg='<=')
bop('>=', 'ge', comm='<=', neg='<')
bop('<', 'lt', comm='>', neg='>=')
bop('<=', 'le', comm='>=', neg='>')
!! PYOFF
--
-- mpz indexes
--
CREATE OR REPLACE FUNCTION mpz_cmp(mpz, mpz)
RETURNS integer
AS 'MODULE_PATHNAME', 'pmpz_cmp'
LANGUAGE C IMMUTABLE STRICT;
CREATE OPERATOR CLASS mpz_btree_ops
DEFAULT FOR TYPE mpz USING btree AS
OPERATOR 1 < ,
OPERATOR 2 <= ,
OPERATOR 3 = ,
OPERATOR 4 >= ,
OPERATOR 5 > ,
FUNCTION 1 mpz_cmp(mpz, mpz)
;
-- TODO: OPERATOR FAMILY?
--
-- Aggregation functions
--
CREATE OR REPLACE FUNCTION mpza_in(cstring)
RETURNS _mpza
AS 'MODULE_PATHNAME', 'pmpza_in'
LANGUAGE C IMMUTABLE STRICT;
CREATE OR REPLACE FUNCTION mpza_out(_mpza)
RETURNS cstring
AS 'MODULE_PATHNAME', 'pmpza_out'
LANGUAGE C IMMUTABLE STRICT;
-- TODO: this type should be marked as "non-storable"
CREATE TYPE _mpza (
INPUT = mpza_in
, OUTPUT = mpza_out
-- 12 is ok on 32 bit, but probably ininfluent anyway
-- as we never store this type
, INTERNALLENGTH = 16
);
CREATE OR REPLACE FUNCTION _mpz_from_mpza(_mpza)
RETURNS mpz
AS 'MODULE_PATHNAME', '_pmpz_from_pmpza'
LANGUAGE C IMMUTABLE STRICT;
CREATE OR REPLACE FUNCTION _mpz_agg_add(_mpza, mpz)
RETURNS _mpza
AS 'MODULE_PATHNAME', '_pmpz_agg_add'
LANGUAGE C IMMUTABLE STRICT;
CREATE AGGREGATE sum (mpz)
(
SFUNC = _mpz_agg_add
, STYPE = _mpza
, FINALFUNC = _mpz_from_mpza
, INITCOND = ''
);
CREATE OR REPLACE FUNCTION _mpz_agg_mul(_mpza, mpz)
RETURNS _mpza
AS 'MODULE_PATHNAME', '_pmpz_agg_mul'
LANGUAGE C IMMUTABLE STRICT;
CREATE AGGREGATE prod (mpz)
(
SFUNC = _mpz_agg_mul
, STYPE = _mpza
, FINALFUNC = _mpz_from_mpza
, INITCOND = ''
);
--
-- Test functions
--
CREATE OR REPLACE FUNCTION mpz_test_dataset(int4, int4)
RETURNS SETOF mpz
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE OR REPLACE FUNCTION numeric_test_dataset(int4, numeric)
RETURNS SETOF numeric
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
--
-- mpq user-defined type
--
!! PYON
base_type = 'mpq'
func('mpq_in', 'cstring', 'mpq')
func('mpq_out', 'mpq', 'cstring')
!! PYOFF
CREATE TYPE mpq (
INPUT = mpq_in
, OUTPUT = mpq_out
, INTERNALLENGTH = VARIABLE
, STORAGE = EXTENDED
, CATEGORY = 'N'
);
!! PYON
func('mpq', 'mpz mpz', 'mpq', cname='pmpq_mpz_mpz')
func('mpq', 'int4 int4', 'mpq', cname='pmpq_int4_int4')
func('mpq', 'numeric numeric', 'mpq', cname='pmpq_numeric_numeric')
func('num', 'mpq', 'mpz', cname='pmpq_num')
func('den', 'mpq', 'mpz', cname='pmpq_den')
castfrom('int2', implicit='I')
castfrom('int4', implicit='I')
castfrom('int8', implicit='I')
castfrom('numeric', implicit='I')
castfrom('mpz', implicit='I')
!! PYOFF
--
-- mpq operators
--
!! PYON
func('mpq_uminus', 'mpq')
func('mpq_uplus', 'mpq')
!! PYOFF
CREATE OPERATOR - (
RIGHTARG = mpq,
PROCEDURE = mpq_uminus
);
CREATE OPERATOR + (
RIGHTARG = mpq,
PROCEDURE = mpq_uplus
);
!! PYON
op('+', 'add', comm='+')
op('-', 'sub')
op('*', 'mul', comm='*')
op('/', 'div')
op('<<', 'mul_2exp', rarg='int4')
op('>>', 'div_2exp', rarg='int4')
!! PYOFF