forked from citusdata/postgresql-hll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hll.c
3358 lines (2779 loc) · 88.2 KB
/
hll.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
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright 2013 Aggregate Knowledge, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <postgres.h> // Needs to be first.
#if defined(__APPLE__)
#include <libkern/OSByteOrder.h>
#define bswap_64 OSSwapInt64
#else
#include <byteswap.h>
#endif
#include <funcapi.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include "utils/array.h"
#include "utils/bytea.h"
#include "utils/int8.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "catalog/pg_type.h"
#include "lib/stringinfo.h"
#include "libpq/pqformat.h"
#include "MurmurHash3.h"
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
// ----------------------------------------------------------------
// Output Version Control
// ----------------------------------------------------------------
// Set the default output schema.
static uint8_t g_output_version = 1;
// ----------------------------------------------------------------
// Type Modifiers
// ----------------------------------------------------------------
// The type modifiers need to be packed in the lower 31 bits
// of an int32. We currently use the lowest 15 bits.
//
#define LOG2M_BITS 5
#define REGWIDTH_BITS 3
#define EXPTHRESH_BITS 6
#define SPARSEON_BITS 1
#define TYPMOD_BITS 15
#define MAX_BITVAL(nbits) ((1 << nbits) - 1)
// Defaults if type modifier values are not specified.
//
#define DEFAULT_LOG2M 11
#define DEFAULT_REGWIDTH 5
#define DEFAULT_EXPTHRESH -1
#define DEFAULT_SPARSEON 1
static int32 g_default_log2m = DEFAULT_LOG2M;
static int32 g_default_regwidth = DEFAULT_REGWIDTH;
static int64 g_default_expthresh = DEFAULT_EXPTHRESH;
static int32 g_default_sparseon = DEFAULT_SPARSEON;
enum {
MST_UNDEFINED = 0x0, // Invalid/undefined set.
MST_EMPTY = 0x1, // Empty set.
MST_EXPLICIT = 0x2, // List of explicit ids.
MST_SPARSE = 0x3, // Sparse set of compression registers.
MST_COMPRESSED = 0x4, // Array of compression registers.
MST_UNINIT = 0xffff, // Internal uninitialized.
};
static int32 typmod_log2m(int32 typmod)
{
return (typmod >> (TYPMOD_BITS - LOG2M_BITS))
& MAX_BITVAL(LOG2M_BITS);
}
static int32 typmod_regwidth(int32 typmod)
{
return (typmod >> (TYPMOD_BITS - LOG2M_BITS - REGWIDTH_BITS))
& MAX_BITVAL(REGWIDTH_BITS);
}
static int32 typmod_expthresh(int32 typmod)
{
return (typmod >> (TYPMOD_BITS - LOG2M_BITS -
REGWIDTH_BITS - EXPTHRESH_BITS))
& MAX_BITVAL(EXPTHRESH_BITS);
}
static int32 typmod_sparseon(int32 typmod)
{
return (typmod >> (TYPMOD_BITS - LOG2M_BITS -
REGWIDTH_BITS - EXPTHRESH_BITS - SPARSEON_BITS))
& MAX_BITVAL(SPARSEON_BITS);
}
// The expthresh is represented in a encoded format in the
// type modifier to save metadata bits. This routine is used
// when the expthresh comes from a typmod value or hll header.
//
static int64 decode_expthresh(int32 encoded_expthresh)
{
// This routine presumes the encoded value is correct and
// doesn't range check.
//
if (encoded_expthresh == 63)
return -1LL;
else if (encoded_expthresh == 0)
return 0;
else
return 1LL << (encoded_expthresh - 1);
}
static int32 integer_log2(int64 val)
{
// Take the log2 of the expthresh.
int32 count = 0;
int64 value = val;
Assert(val >= 0);
while (value)
{
++count;
value >>= 1;
}
return count - 1;
}
// This routine is used to encode an expthresh value to be stored
// in the typmod metadata or a hll header.
//
static int32 encode_expthresh(int64 expthresh)
{
// This routine presumes the uncompressed value is correct and
// doesn't range check.
//
if (expthresh == -1)
return 63;
else if (expthresh == 0)
return 0;
else
return integer_log2(expthresh) + 1;
}
// If expthresh == -1 (auto select expthresh) determine
// the expthresh to use from nbits and nregs.
//
static size_t
expthresh_value(int64 expthresh, size_t nbits, size_t nregs)
{
if (expthresh != -1)
{
return (size_t) expthresh;
}
else
{
// Auto is selected, choose the maximum number of explicit
// registers that fits in the same space as the compressed
// encoding.
size_t cmpsz = ((nbits * nregs) + 7) / 8;
return cmpsz / sizeof(uint64_t);
}
}
// ----------------------------------------------------------------
// Maximum Sparse Control
// ----------------------------------------------------------------
// By default we set the sparse to full compressed threshold
// automatically to the point where the sparse representation would
// start to be larger. This can be overridden with the
// hll_set_max_sparse directive ...
//
static int g_max_sparse = -1;
// ----------------------------------------------------------------
// Aggregating Data Structure
// ----------------------------------------------------------------
typedef struct
{
size_t mse_nelem;
uint64_t mse_elems[0];
} ms_explicit_t;
// Defines the *unpacked* register.
typedef uint8_t compreg_t;
typedef struct
{
compreg_t msc_regs[0];
} ms_compressed_t;
// Size of the compressed or explicit data.
#define MS_MAXDATA (128 * 1024)
typedef struct
{
size_t ms_nbits;
size_t ms_nregs;
size_t ms_log2nregs;
int64 ms_expthresh;
bool ms_sparseon;
uint64_t ms_type; // size is only for alignment.
union
{
// MST_EMPTY and MST_UNDEFINED don't need data.
// MST_SPARSE is only used in the packed format.
//
ms_explicit_t as_expl; // MST_EXPLICIT
ms_compressed_t as_comp; // MST_COMPRESSED
uint8_t as_size[MS_MAXDATA]; // sizes the union.
} ms_data;
} multiset_t;
typedef struct
{
size_t brc_nbits; // Read size.
uint32_t brc_mask; // Read mask.
uint8_t const * brc_curp; // Current byte.
size_t brc_used; // Used bits.
} bitstream_read_cursor_t;
static uint32_t
bitstream_unpack(bitstream_read_cursor_t * brcp)
{
uint32_t retval;
// Fetch the quadword containing our data.
uint64_t qw = * (uint64_t const *) brcp->brc_curp;
// Swap the bytes.
qw = bswap_64(qw);
// Shift the bits we want into place.
qw >>= 64 - brcp->brc_nbits - brcp->brc_used;
// Mask the bits we want.
retval = (uint32_t) (qw & 0xffffffff) & brcp->brc_mask;
// We've used some more bits now.
brcp->brc_used += brcp->brc_nbits;
// Normalize the cursor.
while (brcp->brc_used >= 8)
{
brcp->brc_used -= 8;
brcp->brc_curp += 1;
}
return retval;
}
static void
compressed_unpack(compreg_t * i_regp,
size_t i_width,
size_t i_nregs,
uint8_t const * i_bitp,
size_t i_size,
uint8_t i_vers)
{
size_t bitsz;
size_t padsz;
bitstream_read_cursor_t brc;
bitsz = i_width * i_nregs;
// Fail fast if the compressed array isn't big enough.
if (i_size * 8 < bitsz)
ereport(ERROR,
(errcode(ERRCODE_DATA_EXCEPTION),
errmsg("compressed hll argument not large enough")));
padsz = i_size * 8 - bitsz;
// Fail fast if the pad size doesn't make sense.
if (padsz >= 8)
ereport(ERROR,
(errcode(ERRCODE_DATA_EXCEPTION),
errmsg("inconsistent padding in compressed hll argument")));
brc.brc_nbits = i_width;
brc.brc_mask = (1 << i_width) - 1;
brc.brc_curp = i_bitp;
brc.brc_used = 0;
for (ssize_t ndx = 0; ndx < i_nregs; ++ndx)
{
uint32_t val = bitstream_unpack(&brc);
i_regp[ndx] = val;
}
}
static void
sparse_unpack(compreg_t * i_regp,
size_t i_width,
size_t i_log2nregs,
size_t i_nfilled,
uint8_t const * i_bitp,
size_t i_size)
{
size_t bitsz;
size_t padsz;
size_t chunksz;
uint32_t regmask;
bitstream_read_cursor_t brc;
chunksz = i_log2nregs + i_width;
bitsz = chunksz * i_nfilled;
padsz = i_size * 8 - bitsz;
// Fail fast if the pad size doesn't make sense.
if (padsz >= 8)
ereport(ERROR,
(errcode(ERRCODE_DATA_EXCEPTION),
errmsg("inconsistent padding in sparse hll argument")));
regmask = (1 << i_width) - 1;
brc.brc_nbits = chunksz;
brc.brc_mask = (1 << chunksz) - 1;
brc.brc_curp = i_bitp;
brc.brc_used = 0;
for (ssize_t ii = 0; ii < i_nfilled; ++ii)
{
uint32_t buffer = bitstream_unpack(&brc);
uint32_t val = buffer & regmask;
uint32_t ndx = buffer >> i_width;
i_regp[ndx] = val;
}
}
typedef struct
{
size_t bwc_nbits; // Write size.
uint8_t * bwc_curp; // Current byte.
size_t bwc_used; // Used bits.
} bitstream_write_cursor_t;
static void
bitstream_pack(bitstream_write_cursor_t * bwcp, uint32_t val)
{
size_t bits_left_in_first_byte = 8 - bwcp->bwc_used;
size_t bits_to_write_after_first_byte = bwcp->bwc_nbits - bits_left_in_first_byte;
size_t full_bytes_to_write = bits_to_write_after_first_byte / 8;
size_t remainder_bits_after_full_bytes = bits_to_write_after_first_byte % 8;
// write is small enough that it fits in current byte's remaining space with
// room to spare, so pad the bottom of the byte by left-shifting
if (bwcp->bwc_nbits < bits_left_in_first_byte)
{
* bwcp->bwc_curp = (* bwcp->bwc_curp) | ((uint8_t)(val << (bits_left_in_first_byte - bwcp->bwc_nbits)));
// consume part of the byte and exit
bwcp->bwc_used += bwcp->bwc_nbits;
return;
}
// write fits exactly in current byte's remaining space, so just OR it in to
// the bottom bits of the byte
if (bwcp->bwc_nbits == bits_left_in_first_byte)
{
* bwcp->bwc_curp = (* bwcp->bwc_curp) | (uint8_t)(val);
// consume remainder of byte and exit
bwcp->bwc_used = 0;
bwcp->bwc_curp += 1;
return;
}
// write DOES NOT fit into current byte, so shift off all but the topmost
// bits from the value and OR those into the bottom bits of the byte
/* bwcp->bwc_nbits > bits_left_in_first_byte */
* bwcp->bwc_curp = (* bwcp->bwc_curp) | ((uint8_t)(val >> (bwcp->bwc_nbits - bits_left_in_first_byte)));
// consume remainder of byte
bwcp->bwc_used = 0;
bwcp->bwc_curp += 1;
// if there are 8 or more bits of the value left to write, write them in one
// byte chunks, higher chunks first
if (full_bytes_to_write > 0)
{
for (size_t i = 0; i < full_bytes_to_write; ++i)
{
size_t bits_to_keep = bits_left_in_first_byte + (8 * (i + 1));
size_t right_shift = (bwcp->bwc_nbits - bits_to_keep);
// no OR here because byte is guaranteed to be completely unused
// see above, before conditional
* bwcp->bwc_curp = (uint8_t)(val >> right_shift);
// consume entire byte
bwcp->bwc_used = 0;
bwcp->bwc_curp += 1;
}
}
if (remainder_bits_after_full_bytes > 0)
{
uint8_t mask = (1 << remainder_bits_after_full_bytes) - 1;
// no OR here because byte is guaranteed to be completely unused
* bwcp->bwc_curp = ((uint8_t)val & mask) << (8 - remainder_bits_after_full_bytes);
// consume part of the byte
bwcp->bwc_used = remainder_bits_after_full_bytes;
}
}
static void
compressed_pack(compreg_t const * i_regp,
size_t i_width,
size_t i_nregs,
uint8_t * o_bitp,
size_t i_size,
uint8_t i_vers)
{
size_t bitsz;
size_t padsz;
bitstream_write_cursor_t bwc;
// We need to zero the output array because we use
// an bitwise-or-accumulator below.
memset(o_bitp, '\0', i_size);
bitsz = i_width * i_nregs;
// Fail fast if the compressed array isn't big enough.
if (i_size * 8 < bitsz)
ereport(ERROR,
(errcode(ERRCODE_DATA_EXCEPTION),
errmsg("compressed output buffer not large enough")));
padsz = i_size * 8 - bitsz;
// Fail fast if the pad size doesn't make sense.
if (padsz >= 8)
ereport(ERROR,
(errcode(ERRCODE_DATA_EXCEPTION),
errmsg("inconsistent compressed output pad size")));
bwc.bwc_nbits = i_width;
bwc.bwc_curp = o_bitp;
bwc.bwc_used = 0;
for (ssize_t ndx = 0; ndx < i_nregs; ++ndx)
bitstream_pack(&bwc, i_regp[ndx]);
}
static void
sparse_pack(compreg_t const * i_regp,
size_t i_width,
size_t i_nregs,
size_t i_log2nregs,
size_t i_nfilled,
uint8_t * o_bitp,
size_t i_size)
{
size_t bitsz;
size_t padsz;
bitstream_write_cursor_t bwc;
// We need to zero the output array because we use
// an bitwise-or-accumulator below.
memset(o_bitp, '\0', i_size);
bitsz = i_nfilled * (i_log2nregs + i_width);
// Fail fast if the compressed array isn't big enough.
if (i_size * 8 < bitsz)
ereport(ERROR,
(errcode(ERRCODE_DATA_EXCEPTION),
errmsg("sparse output buffer not large enough")));
padsz = i_size * 8 - bitsz;
// Fail fast if the pad size doesn't make sense.
if (padsz >= 8)
ereport(ERROR,
(errcode(ERRCODE_DATA_EXCEPTION),
errmsg("inconsistent sparse output pad size")));
bwc.bwc_nbits = i_log2nregs + i_width;
bwc.bwc_curp = o_bitp;
bwc.bwc_used = 0;
for (ssize_t ndx = 0; ndx < i_nregs; ++ndx)
{
if (i_regp[ndx] != 0)
{
uint32_t buffer = (ndx << i_width) | i_regp[ndx];
bitstream_pack(&bwc, buffer);
}
}
}
static void
check_metadata(multiset_t const * i_omp, multiset_t const * i_imp)
{
if (i_omp->ms_nbits != i_imp->ms_nbits)
{
ereport(ERROR,
(errcode(ERRCODE_DATA_EXCEPTION),
errmsg("register width does not match: "
"source uses %ld and dest uses %ld",
i_imp->ms_nbits, i_omp->ms_nbits)));
}
if (i_omp->ms_nregs != i_imp->ms_nregs)
{
ereport(ERROR,
(errcode(ERRCODE_DATA_EXCEPTION),
errmsg("register count does not match: "
"source uses %ld and dest uses %ld",
i_imp->ms_nregs, i_omp->ms_nregs)));
}
// Don't need to compare log2nregs because we compared nregs ...
if (i_omp->ms_expthresh != i_imp->ms_expthresh)
{
ereport(ERROR,
(errcode(ERRCODE_DATA_EXCEPTION),
errmsg("explicit threshold does not match: "
"source uses %ld and dest uses %ld",
i_imp->ms_expthresh, i_omp->ms_expthresh)));
}
if (i_omp->ms_sparseon != i_imp->ms_sparseon)
{
ereport(ERROR,
(errcode(ERRCODE_DATA_EXCEPTION),
errmsg("sparse enable does not match: "
"source uses %d and dest uses %d",
i_imp->ms_sparseon, i_omp->ms_sparseon)));
}
}
static void
copy_metadata(multiset_t * o_msp, multiset_t const * i_msp)
{
o_msp->ms_nbits = i_msp->ms_nbits;
o_msp->ms_nregs = i_msp->ms_nregs;
o_msp->ms_log2nregs = i_msp->ms_log2nregs;
o_msp->ms_expthresh = i_msp->ms_expthresh;
o_msp->ms_sparseon = i_msp->ms_sparseon;
}
static void
compressed_add(multiset_t * o_msp, uint64_t elem)
{
size_t nbits = o_msp->ms_nbits;
size_t nregs = o_msp->ms_nregs;
size_t log2nregs = o_msp->ms_log2nregs;
ms_compressed_t * mscp = &o_msp->ms_data.as_comp;
uint64_t mask = nregs - 1;
size_t maxregval = (1 << nbits) - 1;
size_t ndx = elem & mask;
uint64_t ss_val = elem >> log2nregs;
size_t p_w = ss_val == 0 ? 0 : __builtin_ctzll(ss_val) + 1;
if (p_w > maxregval)
p_w = maxregval;
if (mscp->msc_regs[ndx] < p_w)
mscp->msc_regs[ndx] = p_w;
}
static void
compressed_explicit_union(multiset_t * o_msp, multiset_t const * i_msp)
{
ms_explicit_t const * msep = &i_msp->ms_data.as_expl;
for (size_t ii = 0; ii < msep->mse_nelem; ++ii)
compressed_add(o_msp, msep->mse_elems[ii]);
}
static void
explicit_to_compressed(multiset_t * msp)
{
// Make a copy of the explicit multiset.
multiset_t ms;
memcpy(&ms, msp, sizeof(ms));
// Clear the multiset.
memset(msp, '\0', sizeof(*msp));
// Restore the metadata.
copy_metadata(msp, &ms);
// Make it MST_COMPRESSED.
msp->ms_type = MST_COMPRESSED;
// Add all the elements back into the compressed multiset.
compressed_explicit_union(msp, &ms);
}
static int
element_compare(void const * ptr1, void const * ptr2)
{
// We used signed integer comparison to be compatible
// with the java code.
int64_t v1 = * (int64_t const *) ptr1;
int64_t v2 = * (int64_t const *) ptr2;
return (v1 < v2) ? -1 : (v1 > v2) ? 1 : 0;
}
static size_t numfilled(multiset_t const * i_msp)
{
ms_compressed_t const * mscp = &i_msp->ms_data.as_comp;
size_t nfilled = 0;
size_t nregs = i_msp->ms_nregs;
for (size_t ii = 0; ii < nregs; ++ii)
if (mscp->msc_regs[ii] > 0)
++nfilled;
return nfilled;
}
static char *
multiset_tostring(multiset_t const * i_msp)
{
char expbuf[256];
char * retstr;
size_t len;
size_t used;
size_t nbits = i_msp->ms_nbits;
size_t nregs = i_msp->ms_nregs;
int64 expthresh = i_msp->ms_expthresh;
size_t sparseon = i_msp->ms_sparseon;
size_t expval = expthresh_value(expthresh, nbits, nregs);
// If the expthresh is set to -1 (auto) augment the value
// with the automatically determined value.
//
if (expthresh == -1)
snprintf(expbuf, sizeof(expbuf), "%ld(%ld)", expthresh, expval);
else
snprintf(expbuf, sizeof(expbuf), "%ld", expthresh);
// Allocate an initial return buffer.
len = 1024;
retstr = (char *) palloc(len);
memset(retstr, '\0', len);
// We haven't used any return buffer yet.
used = 0;
// Print in a type-dependent way.
switch (i_msp->ms_type)
{
case MST_EMPTY:
used += snprintf(retstr, len, "EMPTY, "
"nregs=%ld, nbits=%ld, expthresh=%s, sparseon=%ld",
nregs, nbits, expbuf, sparseon);
break;
case MST_EXPLICIT:
{
ms_explicit_t const * msep = &i_msp->ms_data.as_expl;
size_t size = msep->mse_nelem;
char linebuf[1024];
ssize_t rv;
used += snprintf(retstr, len, "EXPLICIT, %ld elements, "
"nregs=%ld, nbits=%ld, "
"expthresh=%s, sparseon=%ld:",
size, nregs, nbits, expbuf, sparseon);
for (size_t ii = 0; ii < size; ++ii)
{
int64_t val = * (int64_t const *) & msep->mse_elems[ii];
rv = snprintf(linebuf, sizeof(linebuf),
"\n%ld: %20" PRIi64 " ",
ii, val);
// Do we need to reallocate the return buffer?
if (rv + used > len - 1)
{
len += 1024;
retstr = (char *) repalloc(retstr, len);
}
strncpy(&retstr[used], linebuf, len - used);
used += rv;
}
}
break;
case MST_COMPRESSED:
{
ms_compressed_t const * mscp = &i_msp->ms_data.as_comp;
char linebuf[1024];
size_t rowsz = 32;
size_t nrows = nregs / rowsz;
size_t ndx = 0;
used += snprintf(retstr, len,
"COMPRESSED, %ld filled "
"nregs=%ld, nbits=%ld, expthresh=%s, "
"sparseon=%ld:",
numfilled(i_msp),
nregs, nbits, expbuf, sparseon);
for (size_t rr = 0; rr < nrows; ++rr)
{
size_t pos = 0;
pos = snprintf(linebuf, sizeof(linebuf), "\n%4ld: ", ndx);
for (size_t cc = 0; cc < rowsz; ++cc)
{
pos += snprintf(&linebuf[pos], sizeof(linebuf) - pos,
"%2d ", mscp->msc_regs[ndx]);
++ndx;
}
// Do we need to reallocate the return buffer?
if (pos + used > len - 1)
{
len += 1024;
retstr = (char *) repalloc(retstr, len);
}
strncpy(&retstr[used], linebuf, len - used);
used += pos;
}
}
break;
case MST_UNDEFINED:
used += snprintf(retstr, len, "UNDEFINED "
"nregs=%ld, nbits=%ld, expthresh=%s, sparseon=%ld",
nregs, nbits, expbuf, sparseon);
break;
default:
ereport(ERROR,
(errcode(ERRCODE_DATA_EXCEPTION),
errmsg("unexpected multiset type value")));
break;
}
return retstr;
}
static void
explicit_validate(multiset_t const * i_msp, ms_explicit_t const * i_msep)
{
// Allow explicit multisets with no elements.
if (i_msep->mse_nelem == 0)
return;
// Confirm that all elements are ascending with no duplicates.
for (int ii = 0; ii < i_msep->mse_nelem - 1; ++ii)
{
if (element_compare(&i_msep->mse_elems[ii],
&i_msep->mse_elems[ii + 1]) != -1)
{
char * buf = multiset_tostring(i_msp);
ereport(ERROR,
(errcode(ERRCODE_DATA_EXCEPTION),
errmsg("duplicate or descending explicit elements: %s",
buf)));
pfree(buf);
}
}
}
static void
multiset_add(multiset_t * o_msp, uint64_t element)
{
// WARNING! This routine can change the type of the multiset!
size_t expval = expthresh_value(o_msp->ms_expthresh,
o_msp->ms_nbits,
o_msp->ms_nregs);
switch (o_msp->ms_type)
{
case MST_EMPTY:
// Are we forcing compressed?
if (expval == 0)
{
// Now we're explicit with no elements.
o_msp->ms_type = MST_EXPLICIT;
o_msp->ms_data.as_expl.mse_nelem = 0;
// Convert it to compressed.
explicit_to_compressed(o_msp);
// Add the element in compressed format.
compressed_add(o_msp, element);
}
else
{
// Now we're explicit with one element.
o_msp->ms_type = MST_EXPLICIT;
o_msp->ms_data.as_expl.mse_nelem = 1;
o_msp->ms_data.as_expl.mse_elems[0] = element;
}
break;
case MST_EXPLICIT:
{
ms_explicit_t * msep = &o_msp->ms_data.as_expl;
// If the element is already in the set we're done.
if (bsearch(&element,
msep->mse_elems,
msep->mse_nelem,
sizeof(uint64_t),
element_compare))
return;
// Is the explicit multiset full?
if (msep->mse_nelem == expval)
{
// Convert it to compressed.
explicit_to_compressed(o_msp);
// Add the element in compressed format.
compressed_add(o_msp, element);
}
else
{
// Add the element at the end.
msep->mse_elems[msep->mse_nelem++] = element;
// Resort the elements.
qsort(msep->mse_elems,
msep->mse_nelem,
sizeof(uint64_t),
element_compare);
}
}
break;
case MST_COMPRESSED:
compressed_add(o_msp, element);
break;
case MST_UNDEFINED:
// Result is unchanged.
break;
default:
ereport(ERROR,
(errcode(ERRCODE_DATA_EXCEPTION),
errmsg("undefined multiset type value #1")));
break;
}
}
static void
explicit_union(multiset_t * o_msp, ms_explicit_t const * i_msep)
{
// NOTE - This routine is optimized to add a batch of elements;
// it doesn't resort until they are all added ...
//
// WARNING! This routine can change the type of the target multiset!
size_t expval = expthresh_value(o_msp->ms_expthresh,
o_msp->ms_nbits,
o_msp->ms_nregs);
ms_explicit_t * msep = &o_msp->ms_data.as_expl;
// Note the starting size of the target set.
size_t orig_nelem = msep->mse_nelem;
for (size_t ii = 0; ii < i_msep->mse_nelem; ++ii)
{
uint64_t element = i_msep->mse_elems[ii];
switch (o_msp->ms_type)
{
case MST_EXPLICIT:
if (bsearch(&element,
msep->mse_elems,
orig_nelem,
sizeof(uint64_t),
element_compare))
continue;
if (msep->mse_nelem < expval)
{
// Add the element at the end.
msep->mse_elems[msep->mse_nelem++] = element;
}
else
{
// Convert it to compressed.
explicit_to_compressed(o_msp);
// Add the element in compressed format.
compressed_add(o_msp, element);
}
break;
case MST_COMPRESSED:
compressed_add(o_msp, element);
break;
}
}
// If the target multiset is still explicit it needs to be
// resorted.
if (o_msp->ms_type == MST_EXPLICIT)
{
// Resort the elements.
qsort(msep->mse_elems,
msep->mse_nelem,
sizeof(uint64_t),
element_compare);
}
}
static void unpack_header(multiset_t * o_msp,
uint8_t const * i_bitp,
uint8_t vers,
uint8_t type)
{
o_msp->ms_nbits = (i_bitp[1] >> 5) + 1;
o_msp->ms_log2nregs = i_bitp[1] & 0x1f;
o_msp->ms_nregs = 1 << o_msp->ms_log2nregs;
o_msp->ms_expthresh = decode_expthresh(i_bitp[2] & 0x3f);
o_msp->ms_sparseon = (i_bitp[2] >> 6) & 0x1;
}
static uint8_t
multiset_unpack(multiset_t * o_msp,
uint8_t const * i_bitp,
size_t i_size,
uint8_t * o_encoded_type)
{
// First byte is the version and type header.
uint8_t vers = (i_bitp[0] >> 4) & 0xf;
uint8_t type = i_bitp[0] & 0xf;
if (vers != 1)
ereport(ERROR,
(errcode(ERRCODE_DATA_EXCEPTION),
errmsg("unknown schema version %d", (int) vers)));
if (o_encoded_type != NULL)
*o_encoded_type = type;
// Set the type. NOTE - MST_SPARSE are converted to MST_COMPRESSED.
o_msp->ms_type = (type == MST_SPARSE) ? MST_COMPRESSED : type;
switch (type)
{
case MST_EMPTY:
if (vers == 1)
{
size_t hdrsz = 3;
// Make sure the size is consistent.
if (i_size != hdrsz)
{
ereport(ERROR,
(errcode(ERRCODE_DATA_EXCEPTION),
errmsg("inconsistently sized empty multiset")));
}