-
Notifications
You must be signed in to change notification settings - Fork 14
/
pg_rational.c
676 lines (564 loc) · 14.1 KB
/
pg_rational.c
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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
#include "postgres.h"
#include "fmgr.h"
#include "access/hash.h"
#include "libpq/pqformat.h" /* send/recv functions */
#if PG_VERSION_NUM >= 110000
#include "common/int.h" /* portable overflow detection */
#endif
#include <limits.h>
#include <math.h>
PG_MODULE_MAGIC;
typedef struct
{
int32 numer;
int32 denom;
} Rational;
static int32 gcd(int32, int32);
static bool simplify(Rational *);
static int32 cmp(Rational *, Rational *);
static void neg(Rational *);
static Rational * add(Rational *, Rational *);
static Rational * mul(Rational *, Rational *);
static void mediant(Rational *, Rational *, Rational *);
/*
***************** IO ******************
*/
PG_FUNCTION_INFO_V1(rational_in);
PG_FUNCTION_INFO_V1(rational_in_float);
PG_FUNCTION_INFO_V1(rational_out);
PG_FUNCTION_INFO_V1(rational_out_float);
PG_FUNCTION_INFO_V1(rational_recv);
PG_FUNCTION_INFO_V1(rational_create);
PG_FUNCTION_INFO_V1(rational_embed);
PG_FUNCTION_INFO_V1(rational_send);
Datum
rational_in(PG_FUNCTION_ARGS)
{
char *s = PG_GETARG_CSTRING(0),
*after;
long long n,
d;
Rational *result = palloc(sizeof(Rational));
if (!isdigit(*s) && *s != '-')
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("Missing or invalid numerator")));
n = strtoll(s, &after, 10);
if (*after == '\0')
{
/* if just a number and no slash, interpret as an int */
d = 1;
}
else
{
/* otherwise look for denominator */
if (*after != '/')
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("Expecting '/' after number but found '%c'", *after)));
if (*(++after) == '\0')
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("Expecting value after '/' but got '\\0'")));
d = strtoll(after, &after, 10);
if (*after != '\0')
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("Expecting '\\0' but found '%c'", *after)));
if (d == 0)
ereport(ERROR,
(errcode(ERRCODE_DIVISION_BY_ZERO),
errmsg("fraction cannot have zero denominator")));
}
if (n < INT32_MIN || n > INT32_MAX || d < INT32_MIN || d > INT32_MAX)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("numerator or denominator outside valid int32 value")));
/*
* prevent negative denominator, but do not negate the smallest value --
* that would produce overflow
*/
if (d >= 0 || n == INT32_MIN || d == INT32_MIN)
{
result->numer = (int32) n;
result->denom = (int32) d;
}
else
{
result->numer = (int32) -n;
result->denom = (int32) -d;
}
PG_RETURN_POINTER(result);
}
/*
This function taken from John Kennedy's paper, "Algorithm To Convert a
Decimal to a Fraction." Translated from Pascal.
*/
Datum
rational_in_float(PG_FUNCTION_ARGS)
{
float8 target = PG_GETARG_FLOAT8(0),
z,
fnumer,
fdenom,
error;
int32 prev_denom,
sign;
Rational *result = palloc(sizeof(Rational));
if (target == (int32) target)
{
result->numer = (int32) target;
result->denom = 1;
PG_RETURN_POINTER(result);
}
sign = target < 0.0 ? -1 : 1;
target = fabs(target);
if (!(target <= INT32_MAX)) /* also excludes NaNs */
{
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("value too large for rational")));
}
z = target;
prev_denom = 0;
result->numer = (int32) round(target);
result->denom = 1;
do
{
z = 1.0 / (z - floor(z));
fdenom = result->denom * floor(z) + prev_denom;
fnumer = round(target * fdenom);
if (fnumer > INT32_MAX || fdenom > INT32_MAX)
break;
prev_denom = result->denom;
result->numer = (int32) fnumer;
result->denom = (int32) fdenom;
error = fabs(target - ((float8) result->numer / (float8) result->denom));
} while (z != floor(z) && error >= 1e-12);
result->numer *= sign;
PG_RETURN_POINTER(result);
}
Datum
rational_out(PG_FUNCTION_ARGS)
{
Rational *r = (Rational *) PG_GETARG_POINTER(0);
PG_RETURN_CSTRING(psprintf("%d/%d", r->numer, r->denom));
}
Datum
rational_out_float(PG_FUNCTION_ARGS)
{
Rational *r = (Rational *) PG_GETARG_POINTER(0);
PG_RETURN_FLOAT8((float8) r->numer / (float8) r->denom);
}
Datum
rational_recv(PG_FUNCTION_ARGS)
{
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
Rational *result = palloc(sizeof(Rational));
result->numer = pq_getmsgint(buf, sizeof(int32));
result->denom = pq_getmsgint(buf, sizeof(int32));
if (result->denom == 0)
ereport(ERROR,
(errcode(ERRCODE_DIVISION_BY_ZERO),
errmsg("fraction cannot have zero denominator: \"%d/%d\"",
result->numer, result->denom)));
PG_RETURN_POINTER(result);
}
Datum
rational_create(PG_FUNCTION_ARGS)
{
int32 n = PG_GETARG_INT32(0),
d = PG_GETARG_INT32(1);
Rational *result = palloc(sizeof(Rational));
if (d == 0)
ereport(ERROR,
(errcode(ERRCODE_DIVISION_BY_ZERO),
errmsg("fraction cannot have zero denominator: \"%d/%d\"", n, d)));
result->numer = n;
result->denom = d;
PG_RETURN_POINTER(result);
}
Datum
rational_embed(PG_FUNCTION_ARGS)
{
int32 n = PG_GETARG_INT32(0);
Rational *result = palloc(sizeof(Rational));
result->numer = n;
result->denom = 1;
PG_RETURN_POINTER(result);
}
Datum
rational_send(PG_FUNCTION_ARGS)
{
Rational *r = (Rational *) PG_GETARG_POINTER(0);
StringInfoData buf;
pq_begintypsend(&buf);
pq_sendint(&buf, r->numer, sizeof(int32));
pq_sendint(&buf, r->denom, sizeof(int32));
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}
/*
************* ARITHMETIC **************
*/
PG_FUNCTION_INFO_V1(rational_simplify);
PG_FUNCTION_INFO_V1(rational_add);
PG_FUNCTION_INFO_V1(rational_sub);
PG_FUNCTION_INFO_V1(rational_mul);
PG_FUNCTION_INFO_V1(rational_div);
PG_FUNCTION_INFO_V1(rational_neg);
Datum
rational_simplify(PG_FUNCTION_ARGS)
{
Rational *in = (Rational *) PG_GETARG_POINTER(0);
Rational *out = palloc(sizeof(Rational));
memcpy(out, in, sizeof(Rational));
simplify(out);
PG_RETURN_POINTER(out);
}
Datum
rational_add(PG_FUNCTION_ARGS)
{
Rational x,
y;
memcpy(&x, PG_GETARG_POINTER(0), sizeof(Rational));
memcpy(&y, PG_GETARG_POINTER(1), sizeof(Rational));
PG_RETURN_POINTER(add(&x, &y));
}
Datum
rational_sub(PG_FUNCTION_ARGS)
{
Rational x,
y;
memcpy(&x, PG_GETARG_POINTER(0), sizeof(Rational));
memcpy(&y, PG_GETARG_POINTER(1), sizeof(Rational));
neg(&y);
PG_RETURN_POINTER(add(&x, &y));
}
Datum
rational_mul(PG_FUNCTION_ARGS)
{
Rational x,
y;
memcpy(&x, PG_GETARG_POINTER(0), sizeof(Rational));
memcpy(&y, PG_GETARG_POINTER(1), sizeof(Rational));
PG_RETURN_POINTER(mul(&x, &y));
}
Datum
rational_div(PG_FUNCTION_ARGS)
{
Rational x,
y;
int32 tmp;
memcpy(&x, PG_GETARG_POINTER(0), sizeof(Rational));
memcpy(&y, PG_GETARG_POINTER(1), sizeof(Rational));
tmp = y.numer;
y.numer = y.denom;
y.denom = tmp;
PG_RETURN_POINTER(mul(&x, &y));
}
Datum
rational_neg(PG_FUNCTION_ARGS)
{
Rational *out = palloc(sizeof(Rational));
memcpy(out, PG_GETARG_POINTER(0), sizeof(Rational));
neg(out);
PG_RETURN_POINTER(out);
}
/*
*************** UTILITY ***************
*/
PG_FUNCTION_INFO_V1(rational_hash);
PG_FUNCTION_INFO_V1(rational_intermediate);
PG_FUNCTION_INFO_V1(rational_intermediate_float);
Datum
rational_hash(PG_FUNCTION_ARGS)
{
Rational x;
memcpy(&x, PG_GETARG_POINTER(0), sizeof(Rational));
/*
* hash_any works at binary level, so we must simplify fraction
*/
simplify(&x);
return hash_any((const unsigned char *) &x, sizeof(Rational));
}
Datum
rational_intermediate(PG_FUNCTION_ARGS)
{
Rational x,
y, /* arguments */
lo = {0, 1},
hi = {1, 0}, /* yes, an internal use of 1/0 */
*med = palloc(sizeof(Rational));
/*
* x = coalesce(lo, arg[0]) y = coalesce(hi, arg[1])
*/
memcpy(&x,
PG_ARGISNULL(0) ? &lo : (Rational *) PG_GETARG_POINTER(0),
sizeof(Rational));
memcpy(&y,
PG_ARGISNULL(1) ? &hi : (Rational *) PG_GETARG_POINTER(1),
sizeof(Rational));
if (cmp(&x, &lo) < 0 || cmp(&y, &lo) < 0)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("arguments must be non-negative")));
if (cmp(&x, &y) >= 0)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("first argument must be strictly smaller than second")));
while (true)
{
mediant(&lo, &hi, med);
if (cmp(med, &x) < 1)
memcpy(&lo, med, sizeof(Rational));
else if (cmp(med, &y) > -1)
memcpy(&hi, med, sizeof(Rational));
else
break;
}
PG_RETURN_POINTER(med);
}
/*
************* COMPARISON **************
*/
PG_FUNCTION_INFO_V1(rational_cmp);
PG_FUNCTION_INFO_V1(rational_eq);
PG_FUNCTION_INFO_V1(rational_ne);
PG_FUNCTION_INFO_V1(rational_lt);
PG_FUNCTION_INFO_V1(rational_le);
PG_FUNCTION_INFO_V1(rational_gt);
PG_FUNCTION_INFO_V1(rational_ge);
PG_FUNCTION_INFO_V1(rational_smaller);
PG_FUNCTION_INFO_V1(rational_larger);
Datum
rational_cmp(PG_FUNCTION_ARGS)
{
PG_RETURN_INT32(
cmp((Rational *) PG_GETARG_POINTER(0), (Rational *) PG_GETARG_POINTER(1)));
}
Datum
rational_eq(PG_FUNCTION_ARGS)
{
PG_RETURN_BOOL(
cmp((Rational *) PG_GETARG_POINTER(0), (Rational *) PG_GETARG_POINTER(1)) == 0);
}
Datum
rational_ne(PG_FUNCTION_ARGS)
{
PG_RETURN_BOOL(
cmp((Rational *) PG_GETARG_POINTER(0), (Rational *) PG_GETARG_POINTER(1)) != 0);
}
Datum
rational_lt(PG_FUNCTION_ARGS)
{
PG_RETURN_BOOL(
cmp((Rational *) PG_GETARG_POINTER(0), (Rational *) PG_GETARG_POINTER(1)) < 0);
}
Datum
rational_le(PG_FUNCTION_ARGS)
{
PG_RETURN_BOOL(
cmp((Rational *) PG_GETARG_POINTER(0), (Rational *) PG_GETARG_POINTER(1)) <= 0);
}
Datum
rational_gt(PG_FUNCTION_ARGS)
{
PG_RETURN_BOOL(
cmp((Rational *) PG_GETARG_POINTER(0), (Rational *) PG_GETARG_POINTER(1)) > 0);
}
Datum
rational_ge(PG_FUNCTION_ARGS)
{
PG_RETURN_BOOL(
cmp((Rational *) PG_GETARG_POINTER(0), (Rational *) PG_GETARG_POINTER(1)) >= 0);
}
Datum
rational_smaller(PG_FUNCTION_ARGS)
{
Rational *a = (Rational *) PG_GETARG_POINTER(0),
*b = (Rational *) PG_GETARG_POINTER(1);
PG_RETURN_POINTER(cmp(a, b) < 0 ? a : b);
}
Datum
rational_larger(PG_FUNCTION_ARGS)
{
Rational *a = (Rational *) PG_GETARG_POINTER(0),
*b = (Rational *) PG_GETARG_POINTER(1);
PG_RETURN_POINTER(cmp(a, b) > 0 ? a : b);
}
/*
************** INTERNAL ***************
*/
#if PG_VERSION_NUM < 110000
/* Shims for the lack of src/include/common/int.h in old Postgres.
* Less efficient because we don't leverage compiler builtins for
* detecting overflow like the Postgres source does.
*/
static inline bool
pg_mul_s32_overflow(int32 a, int32 b, int32 *result)
{
int64 res = (int64) a * (int64) b;
if (res > PG_INT32_MAX || res < PG_INT32_MIN)
{
*result = 0x5EED; /* to avoid spurious warnings */
return true;
}
*result = (int32) res;
return false;
}
static inline bool
pg_add_s32_overflow(int32 a, int32 b, int32 *result)
{
int64 res = (int64) a + (int64) b;
if (res > PG_INT32_MAX || res < PG_INT32_MIN)
{
*result = 0x5EED; /* to avoid spurious warnings */
return true;
}
*result = (int32) res;
return false;
}
#endif /* PG_VERSION_NUM */
int32
gcd(int32 a, int32 b)
{
int32 temp;
while (b != 0)
{
temp = a % b;
a = b;
b = temp;
}
return a;
}
bool
simplify(Rational * r)
{
int32 common = gcd(r->numer, r->denom);
/*
* tricky: avoid overflow from (INT32_MIN / -1)
*/
if (common != -1 || (r->numer != INT32_MIN && r->denom != INT32_MIN))
{
r->numer /= common;
r->denom /= common;
}
/*
* prevent negative denominator, but do not negate the smallest value --
* that would produce overflow
*/
if (r->denom < 0 && r->numer != INT32_MIN && r->denom != INT32_MIN)
{
r->numer *= -1;
r->denom *= -1;
}
return (common != 1) && (common != -1);
}
int32
cmp(Rational * a, Rational * b)
{
/*
* Overflow is not an option, we need a total order so that btree indices
* do not die. Hence do the arithmetic in 64 bits.
*/
int64 cross1 = (int64) a->numer * (int64) b->denom,
cross2 = (int64) a->denom * (int64) b->numer;
return (cross1 > cross2) - (cross1 < cross2);
}
void
neg(Rational * r)
{
if (r->numer == INT32_MIN)
{
simplify(r);
/*
* check again
*/
if (r->numer == INT32_MIN)
{
/*
* denom can't be MIN too or fraction would have previously
* simplified to 1/1
*/
r->denom *= -1;
return;
}
}
r->numer *= -1;
}
Rational *
add(Rational * x, Rational * y)
{
int32 xnyd,
ynxd,
numer,
denom;
bool nxyd_bad,
ynxd_bad,
numer_bad,
denom_bad;
Rational *result;
retry_add:
nxyd_bad = pg_mul_s32_overflow(x->numer, y->denom, &xnyd);
ynxd_bad = pg_mul_s32_overflow(y->numer, x->denom, &ynxd);
numer_bad = pg_add_s32_overflow(xnyd, ynxd, &numer);
denom_bad = pg_mul_s32_overflow(x->denom, y->denom, &denom);
if (nxyd_bad || ynxd_bad || numer_bad || denom_bad)
{
/* overflow in intermediate value */
if (!simplify(x) && !simplify(y))
{
/* neither fraction could reduce, cannot proceed */
ereport(ERROR, (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("intermediate value overflow in rational addition")
));
}
/* the fraction(s) reduced, good for one more retry */
goto retry_add;
}
result = palloc(sizeof(Rational));
result->numer = numer;
result->denom = denom;
return result;
}
Rational *
mul(Rational * x, Rational * y)
{
int32 numer,
denom;
bool numer_bad,
denom_bad;
Rational *result;
retry_mul:
numer_bad = pg_mul_s32_overflow(x->numer, y->numer, &numer);
denom_bad = pg_mul_s32_overflow(x->denom, y->denom, &denom);
if (numer_bad || denom_bad)
{
/* overflow in intermediate value */
if (!simplify(x) && !simplify(y))
{
/* neither fraction could reduce, cannot proceed */
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("intermediate value overflow in rational multiplication")));
}
/* the fraction(s) reduced, good for one more retry */
goto retry_mul;
}
result = palloc(sizeof(Rational));
result->numer = numer;
result->denom = denom;
return result;
}
void
mediant(Rational * x, Rational * y, Rational * m)
{
/*
* Rational_intermediate sends fractions with small numers and denoms, and
* slowly builds up. The search will take forever before we ever get close
* to arithmetic overflow in this function, so I don't guard it here.
*/
m->numer = x->numer + y->numer;
m->denom = x->denom + y->denom;
}