-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_rig.c
695 lines (574 loc) · 13.2 KB
/
data_rig.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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
/******************************************************************************
contrib/data_rig/data_rig.c
This file contains routines that can be bound to a Postgres backend and
called by the backend in the process of processing queries. The calling
format for these routines is dictated by Postgres architecture.
******************************************************************************/
#include "postgres.h"
#include <float.h>
#include <math.h>
#include "access/gist.h"
#include "access/stratnum.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "data_rig.h"
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(fact_in);
PG_FUNCTION_INFO_V1(fact_out);
PG_FUNCTION_INFO_V1(fact_ia);
PG_FUNCTION_INFO_V1(fact_compress);
PG_FUNCTION_INFO_V1(fact_decompress);
PG_FUNCTION_INFO_V1(fact_union);
PG_FUNCTION_INFO_V1(fact_consistent);
PG_FUNCTION_INFO_V1(fact_penalty);
PG_FUNCTION_INFO_V1(fact_contains);
PG_FUNCTION_INFO_V1(fact_contained);
PG_FUNCTION_INFO_V1(fact_picksplit);
PG_FUNCTION_INFO_V1(fact_intersect);
PG_FUNCTION_INFO_V1(fact_same);
PG_FUNCTION_INFO_V1(to_fact_number);
Datum
to_fact_number(PG_FUNCTION_ARGS)
{
int32_t dimension = PG_GETARG_INT32(0);
int32_t value = PG_GETARG_INT32(1);
PG_RETURN_INT32((dimension<<24) + value);
}
Datum
fact_in(PG_FUNCTION_ARGS)
{
//char *str = PG_GETARG_CSTRING(0);
//FACT *result;
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("fact_in() not implemented")));
PG_RETURN_DATUM(0);
//PG_RETURN_NDBOX(result);
}
Datum
fact_out(PG_FUNCTION_ARGS)
{
FACT *cube = (FACT*)PG_GETARG_DATUM(0);
StringInfoData buf;
int dim = DIM(cube);
int i;
initStringInfo(&buf);
appendStringInfoChar(&buf, '(');
for (i = 0; i < dim; i++)
{
if (i > 0)
appendStringInfoString(&buf, ", ");
appendStringInfoString(&buf, float8out_internal((double)cube->x[i]));
}
appendStringInfoChar(&buf, ')');
PG_FREE_IF_COPY(cube, 0);
PG_RETURN_CSTRING(buf.data);
}
/*
* Taken from the intarray contrib header
*/
#define ARRPTR(x) ( ARR_DATA_PTR(x) )
#define ARRNELEMS(x) ArrayGetNItems( ARR_NDIM(x), ARR_DIMS(x))
static int
isort_cmp(const void *a, const void *b, void *arg)
{
int32_t aval = *((const int32_t *) a);
int32_t bval = *((const int32_t *) b);
if (aval < bval)
return -1;
if (aval > bval)
return 1;
/*
* Report if we have any duplicates. If there are equal keys, qsort must
* compare them at some point, else it wouldn't know whether one should go
* before or after the other.
*/
*((bool *) arg) = true;
return 0;
}
/* Sort the given data (len >= 2). Return true if any duplicates found */
static bool
isort(int32_t *a, int len)
{
bool r = false;
qsort_arg(a, len, sizeof(int32_t), isort_cmp, (void *) &r);
return r;
}
static void adjust_fact(FACT* result, int dim)
{
int32_t i,size;
if(isort(result->x,dim) && dim>1)
{
int current = 1;
int32_t element = result->x[0];
for (i = 1; i < dim; i++)
{
if(element != result->x[i])
{
element = result->x[current] = result->x[i];
current++;
}
}
size = FACT_SIZE(current);
SET_DIM(result, current);
SET_VARSIZE(result, size);
}
}
Datum
fact_ia(PG_FUNCTION_ARGS)
{
ArrayType *ur = PG_GETARG_ARRAYTYPE_P(0);
FACT *result;
int i;
int dim;
int size;
int32_t *dur;
if (array_contains_nulls(ur))
ereport(ERROR,
(errcode(ERRCODE_ARRAY_ELEMENT_ERROR),
errmsg("cannot work with arrays containing NULLs")));
dim = ARRNELEMS(ur);
dur = (int32_t *) ARRPTR(ur);
size = FACT_SIZE(dim);
result = (FACT *) palloc0(size);
SET_VARSIZE(result, size);
SET_DIM(result, dim);
for (i = 0; i < dim; i++)
result->x[i] = dur[i];
adjust_fact(result, dim);
PG_RETURN_POINTER(result);
}
Datum
fact_compress(PG_FUNCTION_ARGS)
{
PG_RETURN_DATUM(PG_GETARG_DATUM(0));
}
Datum
fact_decompress(PG_FUNCTION_ARGS)
{
PG_RETURN_DATUM(PG_GETARG_DATUM(0));
}
Datum
fact_union(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
int *sizep = (int *) PG_GETARG_POINTER(1);
FACT *out = (FACT *) NULL;
FACT *tmp;
int i,j,k,n;
int32_t *da,*db,na,nb;
int dim = DIM((FACT*)entryvec->vector[0].key);
out = (FACT*)palloc(FACT_SIZE(dim));
tmp = (FACT*)entryvec->vector[0].key;
for(i = 0; i < dim; i++)
{
out->x[i] = tmp->x[i];
}
for (n = 1; n < entryvec->n; n++)
{
tmp = (FACT*)entryvec->vector[n].key;
da = out->x;
na = dim;
db = tmp->x;
nb = DIM(tmp);
i = j = k = 0;
while (i < na && j < nb)
{
if (da[i] < db[j])
i++;
else if (da[i] == db[j])
{
if (k == 0 || da[k - 1] != db[j])
da[k++] = db[j];
i++;
j++;
}
else
j++;
}
dim = k;
}
*sizep = FACT_SIZE(dim);
SET_DIM(out, dim);
SET_VARSIZE(out, *sizep);
PG_RETURN_POINTER(out);
}
static bool
contains(int32_t *da, int na, int32_t *db, int nb)
{
int i,
j,
n;
i = j = n = 0;
while (i < na && j < nb)
{
if (da[i] < db[j])
i++;
else if (da[i] == db[j])
{
n++;
i++;
j++;
}
else
break; /* db[j] is not in da */
}
return (n == nb) ? TRUE : FALSE;
}
static bool
contains_internal(int32_t *da, int na, int32_t *db, int nb)
{
int i,
j;
bool afail = false;
bool bfail = false;
i = j = 0;
while (i < na && j < nb)
{
if (da[i] < db[j])
{
i++;
if(GET_CLS(da[i]) != GET_CLS(db[i]))
{
afail = false;
bfail = true;
}
else
{
if(bfail)
return FALSE;
afail = true;
}
}
else if (da[i] == db[j])
{
i++;
j++;
}
else
{
j++;
if(GET_CLS(da[i]) != GET_CLS(db[i]))
{
afail = false;
bfail = true;
}
else
{
if(afail)
return FALSE;
bfail = true;
}
}
}
return TRUE;
}
Datum
fact_consistent(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
FACT *key = (FACT*) entry->key;
FACT *query = (FACT*) PG_GETARG_POINTER(1);
//StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
/* Oid subtype = PG_GETARG_OID(3); */
bool *recheck = (bool *) PG_GETARG_POINTER(4);
bool res;
/* All cases served by this function are exact */
*recheck = false;
/*
* if entry is not leaf, use g_cube_internal_consistent, else use
* g_cube_leaf_consistent
*/
if (GIST_LEAF(entry))
res = contains(query->x,DIM(query),key->x,DIM(key));
else
res = contains_internal(key->x,DIM(key),query->x,DIM(query));
PG_FREE_IF_COPY(query, 1);
PG_RETURN_BOOL(res);
}
static int
countloss(int32_t *da, int na, int32_t *db, int nb)
{
int i,
j,
n;
if(na==0 || nb==0)
return na;
i = j = n = 0;
while (i < na && j < nb)
{
if (da[i] < db[j])
{
i++;
n++;
}
else if (da[i] == db[j])
{
i++;
j++;
}
else
{
j++;
}
}
return n;
}
static int
countdiff(int32_t *da, int na, int32_t *db, int nb)
{
int i,
j,
n;
if(na==0 || nb==0)
return na+nb;
i = j = n = 0;
while (i < na && j < nb)
{
if (da[i] < db[j])
{
i++;
n++;
}
else if (da[i] == db[j])
{
i++;
j++;
}
else
{
j++;
n++;
}
}
return n;
}
Datum
fact_penalty(PG_FUNCTION_ARGS)
{
GISTENTRY *origentry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *newentry = (GISTENTRY *) PG_GETARG_POINTER(1);
float *result = (float *) PG_GETARG_POINTER(2);
FACT *n = (FACT*)newentry->key;
FACT *o = (FACT*)origentry->key;
*result = countloss(o->x,DIM(o),n->x,DIM(n))+(1/(DIM(o)+1.0));
PG_RETURN_FLOAT8(*result);
}
Datum
fact_contains(PG_FUNCTION_ARGS)
{
FACT *a = (FACT*) PG_GETARG_POINTER(0),
*b = (FACT*) PG_GETARG_POINTER(1);
bool res;
res = contains(a->x, DIM(a), b->x, DIM(b));
PG_FREE_IF_COPY(a, 0);
PG_FREE_IF_COPY(b, 1);
PG_RETURN_BOOL(res);
}
Datum
fact_contained(PG_FUNCTION_ARGS)
{
FACT *a = (FACT*) PG_GETARG_POINTER(0),
*b = (FACT*) PG_GETARG_POINTER(1);
bool res;
res = contains(b->x, DIM(b), a->x, DIM(a));
PG_FREE_IF_COPY(a, 0);
PG_FREE_IF_COPY(b, 1);
PG_RETURN_BOOL(res);
}
static FACT *
fact_union_internal(FACT *a, FACT *b)
{
int i,j,k;
FACT *result;
int dim;
int size;
int32_t *da,*db,*dr,na,nb;
/* trivial case */
if (a == b)
return a;
size = FACT_SIZE(Min(DIM(a),DIM(b)));
result = palloc0(size);
dr = result->x;
da = a->x;
na = DIM(a);
db = b->x;
nb = DIM(b);
i = j = k = 0;
while (i < na && j < nb)
{
if (da[i] < db[j])
i++;
else if (da[i] == db[j])
{
if (k == 0 || dr[k - 1] != db[j])
dr[k++] = db[j];
i++;
j++;
}
else
j++;
}
dim = k;
size = FACT_SIZE(dim);
SET_VARSIZE(result, size);
SET_DIM(result, dim);
return (result);
}
Datum
fact_intersect(PG_FUNCTION_ARGS)
{
FACT *a = (FACT*) PG_GETARG_POINTER(0),
*b = (FACT*) PG_GETARG_POINTER(1);
FACT *res;
res = fact_union_internal(b, a);
PG_FREE_IF_COPY(a, 0);
PG_FREE_IF_COPY(b, 1);
PG_RETURN_POINTER(res);
}
Datum
fact_picksplit(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
OffsetNumber i,
j;
FACT *datum_alpha,
*datum_beta;
FACT *datum_l,
*datum_r;
FACT *union_dl,
*union_dr;
bool firsttime;
double size_alpha,
size_beta;
double size_waste,
waste;
double size_l,
size_r;
int nbytes;
OffsetNumber seed_1 = 1,
seed_2 = 2;
OffsetNumber *left,
*right;
OffsetNumber maxoff;
maxoff = entryvec->n - 2;
nbytes = (maxoff + 2) * sizeof(OffsetNumber);
v->spl_left = (OffsetNumber *) palloc(nbytes);
v->spl_right = (OffsetNumber *) palloc(nbytes);
firsttime = true;
waste = 0.0;
for (i = FirstOffsetNumber; i < maxoff; i = OffsetNumberNext(i))
{
datum_alpha = DatumGetFact(entryvec->vector[i].key);
for (j = OffsetNumberNext(i); j <= maxoff; j = OffsetNumberNext(j))
{
datum_beta = DatumGetFact(entryvec->vector[j].key);
size_waste = countdiff(datum_alpha->x,DIM(datum_alpha),datum_beta->x,DIM(datum_beta));
/*
* are these a more promising split than what we've already seen?
*/
if (size_waste > waste || firsttime)
{
waste = size_waste;
seed_1 = i;
seed_2 = j;
firsttime = false;
}
}
}
left = v->spl_left;
v->spl_nleft = 0;
right = v->spl_right;
v->spl_nright = 0;
datum_alpha = DatumGetFact(entryvec->vector[seed_1].key);
datum_l = datum_alpha;
size_l = DIM(datum_l);
datum_beta = DatumGetFact(entryvec->vector[seed_2].key);
datum_r = datum_beta;
size_r = DIM(datum_r);
/*
* Now split up the regions between the two seeds. An important property
* of this split algorithm is that the split vector v has the indices of
* items to be split in order in its left and right vectors. We exploit
* this property by doing a merge in the code that actually splits the
* page.
*
* For efficiency, we also place the new index tuple in this loop. This is
* handled at the very end, when we have placed all the existing tuples
* and i == maxoff + 1.
*/
maxoff = OffsetNumberNext(maxoff);
for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
{
/*
* If we've already decided where to place this item, just put it on
* the right list. Otherwise, we need to figure out which page needs
* the least enlargement in order to store the item.
*/
if (i == seed_1)
{
*left++ = i;
v->spl_nleft++;
continue;
}
else if (i == seed_2)
{
*right++ = i;
v->spl_nright++;
continue;
}
/* okay, which page needs least enlargement? */
datum_alpha = DatumGetFact(entryvec->vector[i].key);
union_dl = fact_union_internal(datum_l, datum_alpha);
union_dr = fact_union_internal(datum_r, datum_alpha);
size_alpha = DIM(union_dl);
size_beta = DIM(union_dr);
/* pick which page to add it to */
if (size_alpha - size_l < size_beta - size_r)
{
datum_l = union_dl;
size_l = size_alpha;
*left++ = i;
v->spl_nleft++;
}
else
{
datum_r = union_dr;
size_r = size_beta;
*right++ = i;
v->spl_nright++;
}
}
*left = *right = FirstOffsetNumber; /* sentinel value, see dosplit() */
v->spl_ldatum = PointerGetDatum(datum_l);
v->spl_rdatum = PointerGetDatum(datum_r);
PG_RETURN_POINTER(v);
}
static int32
fact_cmp(FACT *a, FACT *b)
{
int i;
int dim;
if(DIM(a) != DIM(b))
return DIM(a) - DIM(b);
dim = DIM(a);
for(i = 0; i < dim; i++)
{
if(a->x[i]!=b->x[i])
return a->x[i] - b->x[i];
}
return 0;
}
Datum
fact_same(PG_FUNCTION_ARGS)
{
FACT *b1 = (FACT*)PG_GETARG_POINTER(0);
FACT *b2 = (FACT*)PG_GETARG_POINTER(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
if (fact_cmp(b1, b2) == 0)
*result = TRUE;
else
*result = FALSE;
PG_RETURN_POINTER(result);
}