-
Notifications
You must be signed in to change notification settings - Fork 3
/
decimal--2.0.sql
656 lines (528 loc) · 13.8 KB
/
decimal--2.0.sql
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
/* contrib/decimal/decimal--1.0.sql */
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
-- \echo Use "CREATE EXTENSION decimal" to load this file. \quit
--
-- PostgreSQL code for Decimal64/128.
--
--
-- Shell type to keep things a bit quieter.
--
CREATE TYPE decimal128;
--
-- Input and output functions.
--
CREATE FUNCTION decimal128_in(cstring)
RETURNS decimal128
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION decimal128_out(decimal128)
RETURNS cstring
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE TYPE decimal128 (
INPUT = decimal128_in,
OUTPUT = decimal128_out,
LIKE = pg_catalog.interval
);
--
-- decimal128 functions.
--
-- group 1: arithmatics,
CREATE FUNCTION decimal128_div(decimal128, decimal128)
RETURNS decimal128
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION decimal128_mul(decimal128, decimal128)
RETURNS decimal128
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION decimal128_add(decimal128, decimal128)
RETURNS decimal128
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION decimal128_sub(decimal128, decimal128)
RETURNS decimal128
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION decimal128_uminus(decimal128)
RETURNS decimal128
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION decimal128_uplus(decimal128)
RETURNS decimal128
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION decimal128_abs(decimal128)
RETURNS decimal128
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
-- group 2: compares.
CREATE FUNCTION decimal128_eq(decimal128, decimal128)
RETURNS boolean
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION decimal128_ne(decimal128, decimal128)
RETURNS boolean
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION decimal128_lt(decimal128, decimal128)
RETURNS boolean
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION decimal128_le(decimal128, decimal128)
RETURNS boolean
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION decimal128_gt(decimal128, decimal128)
RETURNS boolean
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION decimal128_ge(decimal128, decimal128)
RETURNS boolean
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
-- group 3: hash and cmp
CREATE FUNCTION decimal128_hash(decimal128)
RETURNS integer
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION decimal128_cmp(decimal128, decimal128)
RETURNS integer
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
-- group 4: misc
CREATE FUNCTION decimal128_inc(decimal128)
RETURNS decimal128
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION decimal128_smaller(decimal128, decimal128)
RETURNS decimal128
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION decimal128_larger(decimal128, decimal128)
RETURNS decimal128
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION round(decimal128, int)
RETURNS decimal128
AS 'MODULE_PATHNAME','decimal128_round'
LANGUAGE C IMMUTABLE STRICT;
-- NYI:
-- mod, sqrt, pow, log, ln, fac, sign, trunc, ceil, floor
-- exp, div_trunc,
-- decimal aggregates
-- NYI: higher momentum stats functions, stddev, var, etc.
CREATE AGGREGATE sum(decimal128)
(
sfunc = decimal128_add,
stype = decimal128
);
CREATE AGGREGATE min(decimal128)
(
sfunc = decimal128_smaller,
stype = decimal128
);
CREATE AGGREGATE max(decimal128)
(
sfunc = decimal128_larger,
stype = decimal128
);
--
-- operators
--
CREATE OPERATOR / (
PROCEDURE = decimal128_div,
LEFTARG = decimal128, RIGHTARG = decimal128
);
CREATE OPERATOR * (
PROCEDURE = decimal128_mul,
LEFTARG = decimal128, RIGHTARG = decimal128
);
CREATE OPERATOR + (
PROCEDURE = decimal128_add,
LEFTARG = decimal128, RIGHTARG = decimal128
);
CREATE OPERATOR - (
PROCEDURE = decimal128_sub,
LEFTARG = decimal128, RIGHTARG = decimal128
);
CREATE OPERATOR + (
PROCEDURE = decimal128_uplus,
RIGHTARG = decimal128
);
CREATE OPERATOR - (
PROCEDURE = decimal128_uminus,
RIGHTARG = decimal128
);
CREATE OPERATOR < (
PROCEDURE = decimal128_lt,
LEFTARG = decimal128, RIGHTARG = decimal128,
COMMUTATOR = >,
NEGATOR = >=
);
CREATE OPERATOR <= (
PROCEDURE = decimal128_le,
LEFTARG = decimal128, RIGHTARG = decimal128,
COMMUTATOR = >=,
NEGATOR = >
);
CREATE OPERATOR > (
PROCEDURE = decimal128_gt,
LEFTARG = decimal128, RIGHTARG = decimal128,
COMMUTATOR = <,
NEGATOR = <=
);
CREATE OPERATOR >= (
PROCEDURE = decimal128_ge,
LEFTARG = decimal128, RIGHTARG = decimal128,
COMMUTATOR = <=,
NEGATOR = <
);
CREATE OPERATOR = (
PROCEDURE = decimal128_eq,
LEFTARG = decimal128, RIGHTARG = decimal128,
COMMUTATOR = =,
NEGATOR = <>,
MERGES, HASHES
);
CREATE OPERATOR <> (
PROCEDURE = decimal128_ne,
LEFTARG = decimal128, RIGHTARG = decimal128,
COMMUTATOR = <>,
NEGATOR = =
);
CREATE OPERATOR FAMILY decimal128_ops USING btree;
CREATE OPERATOR FAMILY decimal128_ops USING hash;
CREATE OPERATOR CLASS decimal128_op_cls DEFAULT
FOR TYPE decimal128 USING btree FAMILY decimal128_ops AS
OPERATOR 1 <,
OPERATOR 2 <=,
OPERATOR 3 =,
OPERATOR 4 >=,
OPERATOR 5 >,
FUNCTION 1 decimal128_cmp(decimal128, decimal128);
CREATE OPERATOR CLASS decimal128_op_cls DEFAULT
FOR TYPE decimal128 USING hash FAMILY decimal128_ops AS
OPERATOR 1 =,
FUNCTION 1 decimal128_hash(decimal128);
--
-- Shell type to keep things a bit quieter.
--
CREATE TYPE decimal64;
--
-- Input and output functions.
--
CREATE FUNCTION decimal64_in(cstring)
RETURNS decimal64
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION decimal64_out(decimal64)
RETURNS cstring
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE TYPE decimal64 (
INPUT = decimal64_in,
OUTPUT = decimal64_out,
LIKE = pg_catalog.int8
);
--
-- decimal64 functions.
--
-- group 1: arithmatics,
CREATE FUNCTION decimal64_div(decimal64, decimal64)
RETURNS decimal64
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION decimal64_mul(decimal64, decimal64)
RETURNS decimal64
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION decimal64_add(decimal64, decimal64)
RETURNS decimal64
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION decimal64_sub(decimal64, decimal64)
RETURNS decimal64
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION decimal64_uminus(decimal64)
RETURNS decimal64
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION decimal64_uplus(decimal64)
RETURNS decimal64
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION decimal64_abs(decimal64)
RETURNS decimal64
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
-- group 2: compares.
CREATE FUNCTION decimal64_eq(decimal64, decimal64)
RETURNS boolean
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION decimal64_ne(decimal64, decimal64)
RETURNS boolean
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION decimal64_lt(decimal64, decimal64)
RETURNS boolean
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION decimal64_le(decimal64, decimal64)
RETURNS boolean
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION decimal64_gt(decimal64, decimal64)
RETURNS boolean
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION decimal64_ge(decimal64, decimal64)
RETURNS boolean
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
-- group 3: hash and cmp
CREATE FUNCTION decimal64_hash(decimal64)
RETURNS integer
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION decimal64_cmp(decimal64, decimal64)
RETURNS integer
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
-- group 4: misc
CREATE FUNCTION decimal64_inc(decimal64)
RETURNS decimal64
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION decimal64_smaller(decimal64, decimal64)
RETURNS decimal64
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION decimal64_larger(decimal64, decimal64)
RETURNS decimal64
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION round(decimal64, int)
RETURNS decimal64
AS 'MODULE_PATHNAME','decimal64_round'
LANGUAGE C IMMUTABLE STRICT;
-- NYI:
-- mod, sqrt, pow, log, ln, fac, sign, trunc, ceil, floor
-- exp, div_trunc,
-- decimal aggregates
-- NYI: higher momentum stats functions, stddev, var, etc.
CREATE AGGREGATE sum(decimal64)
(
sfunc = decimal64_add,
stype = decimal64
);
CREATE AGGREGATE min(decimal64)
(
sfunc = decimal64_smaller,
stype = decimal64
);
CREATE AGGREGATE max(decimal64)
(
sfunc = decimal64_larger,
stype = decimal64
);
--
-- operators
--
CREATE OPERATOR / (
PROCEDURE = decimal64_div,
LEFTARG = decimal64, RIGHTARG = decimal64
);
CREATE OPERATOR * (
PROCEDURE = decimal64_mul,
LEFTARG = decimal64, RIGHTARG = decimal64
);
CREATE OPERATOR + (
PROCEDURE = decimal64_add,
LEFTARG = decimal64, RIGHTARG = decimal64
);
CREATE OPERATOR - (
PROCEDURE = decimal64_sub,
LEFTARG = decimal64, RIGHTARG = decimal64
);
CREATE OPERATOR + (
PROCEDURE = decimal64_uplus,
RIGHTARG = decimal64
);
CREATE OPERATOR - (
PROCEDURE = decimal64_uminus,
RIGHTARG = decimal64
);
CREATE OPERATOR < (
PROCEDURE = decimal64_lt,
LEFTARG = decimal64, RIGHTARG = decimal64,
COMMUTATOR = >,
NEGATOR = >=
);
CREATE OPERATOR <= (
PROCEDURE = decimal64_le,
LEFTARG = decimal64, RIGHTARG = decimal64,
COMMUTATOR = >=,
NEGATOR = >
);
CREATE OPERATOR > (
PROCEDURE = decimal64_gt,
LEFTARG = decimal64, RIGHTARG = decimal64,
COMMUTATOR = <,
NEGATOR = <=
);
CREATE OPERATOR >= (
PROCEDURE = decimal64_ge,
LEFTARG = decimal64, RIGHTARG = decimal64,
COMMUTATOR = <=,
NEGATOR = <
);
CREATE OPERATOR = (
PROCEDURE = decimal64_eq,
LEFTARG = decimal64, RIGHTARG = decimal64,
COMMUTATOR = =,
NEGATOR = <>,
MERGES, HASHES
);
CREATE OPERATOR <> (
PROCEDURE = decimal64_ne,
LEFTARG = decimal64, RIGHTARG = decimal64,
COMMUTATOR = <>,
NEGATOR = =
);
CREATE OPERATOR FAMILY decimal64_ops USING btree;
CREATE OPERATOR FAMILY decimal64_ops USING hash;
CREATE OPERATOR CLASS decimal64_op_cls DEFAULT
FOR TYPE decimal64 USING btree FAMILY decimal64_ops AS
OPERATOR 1 <,
OPERATOR 2 <=,
OPERATOR 3 =,
OPERATOR 4 >=,
OPERATOR 5 >,
FUNCTION 1 decimal64_cmp(decimal64, decimal64);
CREATE OPERATOR CLASS decimal64_op_cls DEFAULT
FOR TYPE decimal64 USING hash FAMILY decimal64_ops AS
OPERATOR 1 =,
FUNCTION 1 decimal64_hash(decimal64);
/*
* Casts functions
*/
CREATE FUNCTION decimal128_decimal64(decimal128)
RETURNS decimal64
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION decimal64_decimal128(decimal64)
RETURNS decimal128
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION float4_decimal64(float4)
RETURNS decimal64
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION float4_decimal128(float4)
RETURNS decimal128
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION decimal64_float4(decimal64)
RETURNS float4
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION decimal128_float4(decimal128)
RETURNS float4
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION float8_decimal64(float8)
RETURNS decimal64
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION float8_decimal128(float8)
RETURNS decimal128
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION decimal64_float8(decimal64)
RETURNS float8
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION decimal128_float8(decimal128)
RETURNS float8
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION numeric_decimal64(numeric)
RETURNS decimal64
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION numeric_decimal128(numeric)
RETURNS decimal128
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION decimal64_numeric(decimal64)
RETURNS numeric
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION decimal128_numeric(decimal128)
RETURNS numeric
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION int4_decimal64(int4)
RETURNS decimal64
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION int4_decimal128(int4)
RETURNS decimal128
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION int8_decimal64(int8)
RETURNS decimal64
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION int8_decimal128(int8)
RETURNS decimal128
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE CAST(decimal128 AS decimal64)
WITH FUNCTION decimal128_decimal64(decimal128);
CREATE CAST(decimal64 AS decimal128)
WITH FUNCTION decimal64_decimal128(decimal64)
AS IMPLICIT;
CREATE CAST(float4 AS decimal64)
WITH FUNCTION float4_decimal64(float4)
AS IMPLICIT;
CREATE CAST(float4 AS decimal128)
WITH FUNCTION float4_decimal128(float4)
AS IMPLICIT;
CREATE CAST(decimal64 AS float4)
WITH FUNCTION decimal64_float4(decimal64);
CREATE CAST(decimal128 AS float4)
WITH FUNCTION decimal128_float4(decimal128);
CREATE CAST(float8 AS decimal64)
WITH FUNCTION float8_decimal64(float8);
CREATE CAST(float8 AS decimal128)
WITH FUNCTION float8_decimal128(float8)
AS IMPLICIT;
CREATE CAST(decimal64 AS float8)
WITH FUNCTION decimal64_float8(decimal64);
CREATE CAST(decimal128 AS float8)
WITH FUNCTION decimal128_float8(decimal128);
CREATE CAST(numeric AS decimal64)
WITH FUNCTION numeric_decimal64(numeric);
CREATE CAST(numeric AS decimal128)
WITH FUNCTION numeric_decimal128(numeric);
CREATE CAST(decimal64 AS numeric)
WITH FUNCTION decimal64_numeric(decimal64)
AS IMPLICIT;
CREATE CAST(decimal128 AS numeric)
WITH FUNCTION decimal128_numeric(decimal128)
AS IMPLICIT;
CREATE CAST(int4 AS decimal64)
WITH FUNCTION int4_decimal64(int4)
AS IMPLICIT;
CREATE CAST(int4 AS decimal128)
WITH FUNCTION int4_decimal128(int4)
AS IMPLICIT;
CREATE CAST(int8 AS decimal64)
WITH FUNCTION int8_decimal64(int8);
CREATE CAST(int8 AS decimal128)
WITH FUNCTION int8_decimal128(int8)
AS IMPLICIT;