-
Notifications
You must be signed in to change notification settings - Fork 25
/
topn.c
1216 lines (1010 loc) · 30.6 KB
/
topn.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
/*-------------------------------------------------------------------------
*
* topn.c
*
* This file contains the function definitions to perform aggregate top-n and
* union queries by using the SpaceSaving Algorithm.
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "fmgr.h"
#include "miscadmin.h"
#include "utils/guc.h"
#include "utils/guc_tables.h"
#include "access/hash.h"
#include "access/htup_details.h"
#include "catalog/pg_type.h"
#if PG_VERSION_NUM >= 130000
#include "common/jsonapi.h"
#endif
#include "funcapi.h"
#include "lib/stringinfo.h"
#include "mb/pg_wchar.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/datum.h"
#include "utils/hsearch.h"
#include "utils/palloc.h"
#include "utils/json.h"
#include "utils/jsonb.h"
#if PG_VERSION_NUM < 130000
#include "utils/jsonapi.h"
#endif
#include "utils/memutils.h"
/* declarations for dynamic loading */
PG_MODULE_MAGIC;
/*
* If the frequency type is changed to allow higher number of frequencies
* or decreased MAX_FREQUENCY should change accordingly.
* Additionally, in the topnGetDatum function, the get function of
* respective data type should replace Int64GetDatum.
*/
typedef int64 Frequency;
static int32 NumberOfCounters = 1000;
static int32 UnionFactor = 3;
#define MAX_KEYSIZE 256
#define MAX_FREQUENCY INT64_MAX
#if PG_VERSION_NUM >= 110000
#define PG_GETARG_JSONB(int) PG_GETARG_JSONB_P(int)
#define PG_RETURN_JSONB(jsonb) PG_RETURN_JSONB_P(jsonb)
#endif
#if PG_VERSION_NUM >= 170000
#define makeJsonLexContextCstringLenCompat(lex, json, len, encoding, need_escapes) \
makeJsonLexContextCstringLen(lex, json, len, encoding, need_escapes);
#elif PG_VERSION_NUM >= 130000
#define makeJsonLexContextCstringLenCompat(lex, json, len, encoding, need_escapes) \
makeJsonLexContextCstringLen(json, len, encoding, need_escapes);
#else
#define makeJsonLexContextCstringLenCompat(lex, json, len, encoding, need_escapes) \
makeJsonLexContextCstringLen(json, len, need_escapes);
#endif
/* Taken from jsonb.c */
#define JSONB_MAX_PAIRS (Min(MaxAllocSize / sizeof(JsonbPair), JB_CMASK))
/* Taken from jsonb.h for PG version less than 10 */
#define JsonContainerSize(jc) ((jc)->header & JB_CMASK)
/* Taken from jsonb.h for PG version less than 10 */
#define JsonContainerSize(jc) ((jc)->header & JB_CMASK)
#if PG_VERSION_NUM < 160000
#define JsonParseErrorType_compat void
#define JSON_SUCCESS_COMPAT
#else
#define JsonParseErrorType_compat JsonParseErrorType
#define JSON_SUCCESS_COMPAT JSON_SUCCESS
#endif
/* SQL Function definitions */
PG_FUNCTION_INFO_V1(topn);
PG_FUNCTION_INFO_V1(topn_add);
PG_FUNCTION_INFO_V1(topn_union);
PG_FUNCTION_INFO_V1(topn_add_trans);
PG_FUNCTION_INFO_V1(topn_union_trans);
PG_FUNCTION_INFO_V1(topn_union_internal);
PG_FUNCTION_INFO_V1(topn_serialize);
PG_FUNCTION_INFO_V1(topn_deserialize);
PG_FUNCTION_INFO_V1(topn_pack);
/*
* TopnAggState is the main struct to handle aggregate functions.
* It is used as an internal type and it is actually a lone HTAB.
* The data is being aggregated in HTAB since theoretically its enter/delete
* operations are in constant time and it has a dynamic size.
*/
typedef struct TopnAggState TopnAggState;
/*
* FrequentTopnItem is the struct to keep frequent items and their frequencies
* together. It is useful to sort the top-n items before returning in topn() function
* and in the prune stage.
*/
typedef struct FrequentTopnItem
{
char key[MAX_KEYSIZE];
Frequency frequency;
} FrequentTopnItem;
/*
* This struct is used by internal Postgres function which are directly
* COPY/PASTEd from the source code.
*/
typedef struct JsonbInState
{
JsonbParseState *parseState;
JsonbValue *res;
} JsonbInState;
Datum topn(PG_FUNCTION_ARGS);
Datum topn_add(PG_FUNCTION_ARGS);
Datum topn_union(PG_FUNCTION_ARGS);
Datum topn_add_trans(PG_FUNCTION_ARGS);
Datum topn_union_trans(PG_FUNCTION_ARGS);
Datum topn_pack(PG_FUNCTION_ARGS);
/* local functions forward declarations */
void _PG_init(void);
static void RegisterTopNConfigVariables(void);
static FrequentTopnItem * FrequencyArrayFromJsonb(JsonbContainer *container);
static TopnAggState * CreateTopnAggState(void);
static void MergeJsonbIntoTopnAggState(Jsonb *jsonb, TopnAggState *topn);
static int compareFrequentTopnItem(const void *item1, const void *item2);
static Datum topnGetDatum(FrequentTopnItem *topnItem, TupleDesc tupleDescriptor);
static void PruneHashTable(HTAB *hashTable, int itemLimit, int numberOfRemainingElements);
static Jsonb * MaterializeAggStateToJsonb(TopnAggState *topn);
static HTAB * topnHashtable(TopnAggState *topn);
static void MergeTopn(TopnAggState *left, TopnAggState *right);
static void IncreaseItemFrequency(FrequentTopnItem *item, Frequency amount);
static void InsertPairs(FrequentTopnItem *item, StringInfo jsonbStr);
static Jsonb * jsonb_from_cstring(char *json, int len);
static size_t checkStringLen(size_t len);
/*
* shared library initialization function which is used to define the
* topn.number_of_counters GUC
*/
void
_PG_init(void)
{
RegisterTopNConfigVariables();
}
/* Register topn configuration variable. */
static void
RegisterTopNConfigVariables(void)
{
DefineCustomIntVariable(
"topn.number_of_counters",
gettext_noop("Sets the number of counters to keep the track of."),
NULL,
&NumberOfCounters,
1000, 1, JSONB_MAX_PAIRS,
PGC_USERSET,
0,
NULL, NULL, NULL);
}
/*
* topn is a user-facing UDF which returns the top items and their frequencies.
* It first gets the jsonb and converts it into an ordered array of
* FrequentTopnItem which keeps Datums and the frequencies in the first call.
* Then, it returns an item and its frequency according to call counter which are
* all accumulated in a jsonb object.
*/
Datum
topn(PG_FUNCTION_ARGS)
{
FuncCallContext *functionCallContext = NULL;
Jsonb *jsonb = NULL;
int jsonbElementCount = 0;
int callCounter = 0;
int maxCallCounter = 0;
int itemCountToPrint = 0;
int desiredNToPrint = 0;
TupleDesc tupleDescriptor = NULL;
if (SRF_IS_FIRSTCALL())
{
MemoryContext oldcontext = NULL;
FrequentTopnItem *sortedTopnArray = NULL;
TupleDesc completeTupleDescriptor = NULL;
JsonbContainer *container;
functionCallContext = SRF_FIRSTCALL_INIT();
if (PG_ARGISNULL(0))
{
SRF_RETURN_DONE(functionCallContext);
}
oldcontext = MemoryContextSwitchTo(functionCallContext->multi_call_memory_ctx);
jsonb = PG_GETARG_JSONB(0);
container = &jsonb->root;
jsonbElementCount = JsonContainerSize(container);
/* if there is not any element in the array just return */
if (jsonbElementCount <= 0)
{
MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(functionCallContext);
}
desiredNToPrint = PG_GETARG_INT32(1);
if (desiredNToPrint > NumberOfCounters)
{
ereport(ERROR, (errmsg("desired number of counters is higher than the "
"topn.number_of_counters variable")));
}
itemCountToPrint = Min(desiredNToPrint, jsonbElementCount);
functionCallContext->max_calls = itemCountToPrint;
/* create an array to copy top-n items and sort them later */
sortedTopnArray = FrequencyArrayFromJsonb(container);
/* pass the sorted entries to the multi call context */
qsort(sortedTopnArray, jsonbElementCount, sizeof(FrequentTopnItem),
compareFrequentTopnItem);
functionCallContext->user_fctx = sortedTopnArray;
/* pass the tuple descriptor to be returned to the multi call context*/
tupleDescriptor =
#if PG_VERSION_NUM < 120000
CreateTemplateTupleDesc(2, false);
#else
CreateTemplateTupleDesc(2);
#endif
TupleDescInitEntry(tupleDescriptor, (AttrNumber) 1, "item",
TEXTOID, -1, 0);
TupleDescInitEntry(tupleDescriptor, (AttrNumber) 2, "frequency",
INT8OID, -1, 0);
completeTupleDescriptor = BlessTupleDesc(tupleDescriptor);
functionCallContext->tuple_desc = completeTupleDescriptor;
MemoryContextSwitchTo(oldcontext);
}
functionCallContext = SRF_PERCALL_SETUP();
maxCallCounter = functionCallContext->max_calls;
callCounter = functionCallContext->call_cntr;
if (callCounter < maxCallCounter)
{
FrequentTopnItem *sortedTopnArray = NULL;
Datum topnDatum = 0;
sortedTopnArray =
&((FrequentTopnItem *) functionCallContext->user_fctx)[callCounter];
topnDatum = topnGetDatum(sortedTopnArray, functionCallContext->tuple_desc);
SRF_RETURN_NEXT(functionCallContext, topnDatum);
}
else
{
SRF_RETURN_DONE(functionCallContext);
}
}
/*
* topn_add is the function used to update a jsonb object with the given element.
* Here the jsonb object is assumed that it is in valid topn format ("key":value).
*/
Datum
topn_add(PG_FUNCTION_ARGS)
{
FrequentTopnItem *item = NULL;
Jsonb *jsonb = NULL;
TopnAggState *stateTopn = NULL;
text *itemText = NULL;
bool found = false;
char itemString[MAX_KEYSIZE];
/*
* Create stateTopn when the first non-null item arrive by using the item's type.
* After stateTopn is created once, use it during aggregation and to pass the related
* information to the final function.
*/
if (PG_ARGISNULL(0) && PG_ARGISNULL(1))
{
jsonb = jsonb_from_cstring("{}", 2);
PG_RETURN_JSONB(jsonb);
}
else if (PG_ARGISNULL(0))
{
stateTopn = CreateTopnAggState();
jsonb = jsonb_from_cstring("{}", 2);
}
else if (PG_ARGISNULL(1))
{
jsonb = PG_GETARG_JSONB(0);
PG_RETURN_JSONB(jsonb);
}
else
{
stateTopn = CreateTopnAggState();
jsonb = PG_GETARG_JSONB(0);
}
/*
* Update fields of the stateTopn with the first item if
* the stateTopn is not updated yet.
*/
MergeJsonbIntoTopnAggState(jsonb, stateTopn);
itemText = PG_GETARG_TEXT_P(1);
text_to_cstring_buffer(itemText, itemString, MAX_KEYSIZE);
item = hash_search(topnHashtable(stateTopn), (void *) itemString,
HASH_ENTER, &found);
if (found)
{
IncreaseItemFrequency(item, 1);
}
else
{
item->frequency = 1;
PruneHashTable(topnHashtable(stateTopn), NumberOfCounters, NumberOfCounters);
}
jsonb = MaterializeAggStateToJsonb(stateTopn);
PG_RETURN_JSONB(jsonb);
}
/*
* topn_union is the function used to take the union of two jsonbs which are assumed
* to be in valid topn format as ("key":value).
*/
Datum
topn_union(PG_FUNCTION_ARGS)
{
Jsonb *jsonbLeft = NULL;
Jsonb *jsonbRight = NULL;
Jsonb *result = NULL;
TopnAggState *topn = NULL;
jsonbLeft = PG_GETARG_JSONB(0);
jsonbRight = PG_GETARG_JSONB(1);
/*allocate topn */
topn = CreateTopnAggState();
MergeJsonbIntoTopnAggState(jsonbLeft, topn);
MergeJsonbIntoTopnAggState(jsonbRight, topn);
PruneHashTable(topnHashtable(topn), NumberOfCounters, NumberOfCounters);
result = MaterializeAggStateToJsonb(topn);
PG_RETURN_JSONB(result);
}
/*
* topn_add_trans function is the transient function for topn_add_agg.
* In the first call, it initializes a Topn object and aggregates the
* data in it by using the HASH_ENTER of hash table.
*/
Datum
topn_add_trans(PG_FUNCTION_ARGS)
{
MemoryContext aggctx;
MemoryContext oldContext;
TopnAggState *topnTrans;
FrequentTopnItem *item = NULL;
text *textInput = NULL;
bool found = false;
char charInput[MAX_KEYSIZE];
/* We must be called as a transition routine or we fail. */
if (!AggCheckCallContext(fcinfo, &aggctx))
{
ereport(ERROR,
(errcode(ERRCODE_DATA_EXCEPTION),
errmsg("topn_add_trans outside transition context")));
}
/* If the first argument is a NULL on first call, init an empty topn */
if (PG_ARGISNULL(0))
{
oldContext = MemoryContextSwitchTo(aggctx);
topnTrans = CreateTopnAggState();
MemoryContextSwitchTo(oldContext);
}
else
{
topnTrans = (TopnAggState *) (PG_GETARG_POINTER(0));
}
if (PG_ARGISNULL(1))
{
PG_RETURN_POINTER(topnTrans);
}
/* Is the second argument non-null? */
textInput = PG_GETARG_TEXT_P(1);
text_to_cstring_buffer(textInput, charInput, MAX_KEYSIZE);
item = hash_search(topnHashtable(topnTrans), (void *) charInput, HASH_ENTER, &found);
if (found)
{
IncreaseItemFrequency(item, 1);
}
else
{
int itemLimit = NumberOfCounters * UnionFactor;
int remainingElements = hash_get_num_entries(topnHashtable(topnTrans)) / 2;
item->frequency = 1;
PruneHashTable(topnHashtable(topnTrans), itemLimit, remainingElements);
}
PG_RETURN_POINTER(topnTrans);
}
/*
* topn_union_trans function is the transient function for topn_union_agg.
* In the first call, it initializes a Topn and aggregates the jsonb
* objects in it by using the HASH_ENTER of hash table.
*/
Datum
topn_union_trans(PG_FUNCTION_ARGS)
{
MemoryContext aggctx;
MemoryContext oldContext;
TopnAggState *topnTrans;
TopnAggState *topnNewItem;
Jsonb *jsonbToBeAdded = NULL;
/* it must be called as a transition routine or it fails */
if (!AggCheckCallContext(fcinfo, &aggctx))
{
ereport(ERROR,
(errcode(ERRCODE_DATA_EXCEPTION),
errmsg("topn_union_trans outside transition context")));
}
/*
* If the first argument is a NULL on first call, init an empty topn.
* Otherwise, take the given argument and continue the process on it.
*/
if (PG_ARGISNULL(0))
{
oldContext = MemoryContextSwitchTo(aggctx);
topnTrans = CreateTopnAggState();
MemoryContextSwitchTo(oldContext);
}
else
{
topnTrans = (TopnAggState *) (PG_GETARG_POINTER(0));
}
if (!PG_ARGISNULL(1))
{
jsonbToBeAdded = PG_GETARG_JSONB(1);
topnNewItem = CreateTopnAggState();
MergeJsonbIntoTopnAggState(jsonbToBeAdded, topnNewItem);
/* always merges the right one into the left */
MergeTopn(topnTrans, topnNewItem);
/* No need to check the size again since it is already checked in MergeTopn */
}
PG_RETURN_POINTER(topnTrans);
}
/*
* topn_serialize function converts TopnAggState to bytea
*/
Datum
topn_serialize(PG_FUNCTION_ARGS)
{
Size topnArraySize;
TopnAggState *topnTrans = (TopnAggState *) PG_GETARG_POINTER(0);
HASH_SEQ_STATUS status;
FrequentTopnItem *currentTask = NULL;
bytea *ret;
char *bpPtr; /* Cursor for writing into ret */
long hashTableSize = hash_get_num_entries(topnHashtable(topnTrans));
/* it must be called as a transition routine or it fails */
if (!AggCheckCallContext(fcinfo, NULL))
{
ereport(ERROR,
(errcode(ERRCODE_DATA_EXCEPTION),
errmsg("topn_serialize outside transition context")));
}
topnArraySize = sizeof(FrequentTopnItem) * hashTableSize;
ret = palloc(VARHDRSZ + topnArraySize);
SET_VARSIZE(ret, VARHDRSZ + topnArraySize);
bpPtr = (void *) VARDATA(ret);
hash_seq_init(&status, topnHashtable(topnTrans));
while ((currentTask = (FrequentTopnItem *) hash_seq_search(&status)) != NULL)
{
memcpy(bpPtr, currentTask, sizeof(FrequentTopnItem));
bpPtr += sizeof(FrequentTopnItem);
}
PG_RETURN_BYTEA_P(ret);
}
/*
* topn_deserialize function converts bytea to TopnAggState
*/
Datum
topn_deserialize(PG_FUNCTION_ARGS)
{
MemoryContext aggctx;
MemoryContext oldContext;
TopnAggState *topnTrans;
bytea *bp = PG_GETARG_BYTEA_P(0);
char *bpPtr;
char *bpPtrEnd;
FrequentTopnItem *item;
size_t bpsz;
/* it must be called as a transition routine or it fails */
if (!AggCheckCallContext(fcinfo, &aggctx))
{
ereport(ERROR,
(errcode(ERRCODE_DATA_EXCEPTION),
errmsg("topn_deserialize outside transition context")));
}
oldContext = MemoryContextSwitchTo(aggctx);
topnTrans = CreateTopnAggState();
MemoryContextSwitchTo(oldContext);
bpsz = VARSIZE(bp) - VARHDRSZ;
for (bpPtr = VARDATA(bp), bpPtrEnd = bpPtr + bpsz;
bpPtr < bpPtrEnd;
bpPtr += sizeof(FrequentTopnItem))
{
item = hash_search(topnHashtable(topnTrans), bpPtr, HASH_ENTER, NULL);
memcpy(item, bpPtr, sizeof(FrequentTopnItem));
}
PG_RETURN_POINTER(topnTrans);
}
/*
* topn_union_internal function is the combinefunc for aggregates.
*/
Datum
topn_union_internal(PG_FUNCTION_ARGS)
{
MemoryContext aggctx;
MemoryContext oldContext;
TopnAggState *topnTrans;
/* it must be called as a transition routine or it fails */
if (!AggCheckCallContext(fcinfo, &aggctx))
{
ereport(ERROR,
(errcode(ERRCODE_DATA_EXCEPTION),
errmsg("topn_union_internal outside transition context")));
}
/*
* If the first argument is a NULL on first call, init an empty topn.
* Otherwise, take the given argument and continue the process on it.
*/
if (PG_ARGISNULL(0))
{
oldContext = MemoryContextSwitchTo(aggctx);
topnTrans = CreateTopnAggState();
MemoryContextSwitchTo(oldContext);
}
else
{
topnTrans = (TopnAggState *) (PG_GETARG_POINTER(0));
}
if (!PG_ARGISNULL(1))
{
MergeTopn(topnTrans, (TopnAggState *) (PG_GETARG_POINTER(1)));
}
PG_RETURN_POINTER(topnTrans);
}
/*
* the packer function for aggregate functions it basically transforms the HTAB object
* to a JSONB object and returns it to the user.
*/
Datum
topn_pack(PG_FUNCTION_ARGS)
{
MemoryContext aggctx;
Jsonb *jsonb = NULL;
TopnAggState *topnTrans;
StringInfo emptyJsonb = NULL;
/* We must be called as a transition routine or we fail. */
if (!AggCheckCallContext(fcinfo, &aggctx))
{
ereport(ERROR,
(errcode(ERRCODE_DATA_EXCEPTION),
errmsg("topn_pack outside aggregate context")));
}
/* Is the first argument a NULL? */
if (!PG_ARGISNULL(0))
{
topnTrans = (TopnAggState *) PG_GETARG_POINTER(0);
PruneHashTable(topnHashtable(topnTrans), NumberOfCounters, NumberOfCounters);
jsonb = MaterializeAggStateToJsonb(topnTrans);
}
else
{
emptyJsonb = makeStringInfo();
appendStringInfo(emptyJsonb, "{}");
jsonb = jsonb_from_cstring(emptyJsonb->data, emptyJsonb->len);
}
PG_RETURN_JSONB(jsonb);
}
/*
* FrequencyArrayFromJsonb function creates and returns a FrequencyItem array
* from a given JSONB container.
*/
static FrequentTopnItem *
FrequencyArrayFromJsonb(JsonbContainer *container)
{
Size topnArraySize = 0;
FrequentTopnItem *topnItemArray = NULL;
StringInfo key = NULL;
int jsonbElementCount = 0;
int topnIndex = 0;
JsonbIteratorToken jsonbIteratorToken;
JsonbValue itemJsonbValue;
JsonbIterator *iterator = NULL;
char *valueNumAsString = NULL;
Frequency frequencyValue = 0;
jsonbElementCount = JsonContainerSize(container);
topnArraySize = sizeof(FrequentTopnItem) * jsonbElementCount;
topnItemArray = (FrequentTopnItem *) palloc0(topnArraySize);
iterator = JsonbIteratorInit(container);
while ((jsonbIteratorToken = JsonbIteratorNext(&iterator, &itemJsonbValue, false)) !=
WJB_DONE)
{
if (jsonbIteratorToken == WJB_KEY && itemJsonbValue.type == jbvString)
{
/* json rules guarantee this is a string */
key = makeStringInfo();
appendBinaryStringInfo(key, itemJsonbValue.val.string.val,
itemJsonbValue.val.string.len);
if (key->len > MAX_KEYSIZE)
{
ereport(ERROR,
(errcode(ERRCODE_DATA_EXCEPTION),
errmsg("this jsonb object includes a key which is longer than "
"allowed topn key size (256 bytes)")));
}
jsonbIteratorToken = JsonbIteratorNext(&iterator, &itemJsonbValue, false);
if (jsonbIteratorToken == WJB_VALUE && itemJsonbValue.type == jbvNumeric)
{
valueNumAsString = numeric_normalize(itemJsonbValue.val.numeric);
frequencyValue = atol(valueNumAsString);
memcpy(topnItemArray[topnIndex].key, key->data, key->len);
topnItemArray[topnIndex].frequency = frequencyValue;
topnIndex++;
}
}
}
return topnItemArray;
}
/*
* Creates an empty TopnAggState struct.
*/
static TopnAggState *
CreateTopnAggState(void)
{
int32 hashTableSize = 0;
HASHCTL hashInfo;
int flags = HASH_ELEM | HASH_CONTEXT;
hashTableSize = (NumberOfCounters / 0.75) + 1;
memset(&hashInfo, 0, sizeof(hashInfo));
hashInfo.keysize = MAX_KEYSIZE;
hashInfo.entrysize = sizeof(FrequentTopnItem);
hashInfo.hcxt = CurrentMemoryContext;
#if PG_VERSION_NUM >= 140000
flags |= HASH_STRINGS;
#endif
return (TopnAggState *) hash_create("Item Frequency Map", hashTableSize, &hashInfo,
flags);
}
/*
* MergeJsonbIntoTopnAggState extracts the topn object from a given Jsonb.
*/
static void
MergeJsonbIntoTopnAggState(Jsonb *jsonb, TopnAggState *topn)
{
JsonbContainer *container = &jsonb->root;
JsonbIterator *iterator = JsonbIteratorInit(container);
JsonbIteratorToken jsonbIteratorToken;
JsonbValue itemJsonbValue;
char *valueNumAsString = NULL;
StringInfo key = makeStringInfo();
Frequency frequencyValue = 0;
bool found = false;
FrequentTopnItem *item = NULL;
while ((jsonbIteratorToken = JsonbIteratorNext(&iterator, &itemJsonbValue, false)) !=
WJB_DONE)
{
if (jsonbIteratorToken == WJB_KEY && itemJsonbValue.type == jbvString)
{
/* json rules guarantee this is a string */
key = makeStringInfo();
appendBinaryStringInfo(key, itemJsonbValue.val.string.val,
itemJsonbValue.val.string.len);
if (key->len > MAX_KEYSIZE)
{
ereport(ERROR,
(errcode(ERRCODE_DATA_EXCEPTION),
errmsg("this jsonb object includes a key which is longer than "
"allowed topn key size (256 bytes)")));
}
jsonbIteratorToken = JsonbIteratorNext(&iterator, &itemJsonbValue, false);
if (jsonbIteratorToken == WJB_VALUE && itemJsonbValue.type == jbvNumeric)
{
int sizeOfHashTable = 0;
int remainingElements = 0;
int itemLimit = 0;
valueNumAsString = numeric_normalize(itemJsonbValue.val.numeric);
frequencyValue = atol(valueNumAsString);
item = hash_search(topnHashtable(topn), (void *) key->data,
HASH_ENTER, &found);
if (found)
{
IncreaseItemFrequency(item, frequencyValue);
}
else
{
item->frequency = frequencyValue;
}
sizeOfHashTable = hash_get_num_entries(topnHashtable(topn));
remainingElements = sizeOfHashTable / 2;
itemLimit = NumberOfCounters * UnionFactor;
PruneHashTable(topnHashtable(topn), itemLimit, remainingElements);
}
}
}
}
/*
* Comparator function for FrequentTopnItem struct to be able to sort
* the array of it with qsort of stdlib.
*/
static int
compareFrequentTopnItem(const void *item1, const void *item2)
{
Frequency freq1 = ((FrequentTopnItem *) item1)->frequency;
Frequency freq2 = ((FrequentTopnItem *) item2)->frequency;
if (freq1 == freq2)
{
return 0;
}
else if (freq1 > freq2)
{
return -1;
}
else
{
return 1;
}
}
/*
* topnGetDatum converts the FrequentTopnItem passed to it into its datum
* representation. To do this, the function first creates the heap tuple from
* the topn key and value. Then, the function converts the heap tuple
* into a datum and returns it.
*/
static Datum
topnGetDatum(FrequentTopnItem *topnItem, TupleDesc tupleDescriptor)
{
Datum values[2];
bool isNulls[2];
HeapTuple topnTuple = NULL;
Datum topnDatum = 0;
memset(values, 0, sizeof(values));
memset(isNulls, false, sizeof(isNulls));
values[0] = CStringGetTextDatum(topnItem->key);
values[1] = Int64GetDatum((Frequency) topnItem->frequency);
topnTuple = heap_form_tuple(tupleDescriptor, values, isNulls);
topnDatum = HeapTupleGetDatum(topnTuple);
return topnDatum;
}
/*
* PruneHashTable removes some items from the HashTable to decrease its size. It finds
* minimum and maximum frequencies first and removes the items which have lower frequency
* than the average of them.
*/
static void
PruneHashTable(HTAB *hashTable, int itemLimit, int numberOfRemainingElements)
{
Size topnArraySize = 0;
int topnIndex = 0;
FrequentTopnItem *sortedTopnArray = NULL;
bool itemAlreadyHashed = false;
HASH_SEQ_STATUS status;
FrequentTopnItem *currentTask = NULL;
int index = 0;
int hashTableSize = hash_get_num_entries(hashTable);
if (hashTableSize <= itemLimit)
{
return;
}
/* create an array to copy top-n items and sort them later */
topnArraySize = sizeof(FrequentTopnItem) * hashTableSize;
sortedTopnArray = (FrequentTopnItem *) palloc(topnArraySize);
hash_seq_init(&status, hashTable);
while ((currentTask = (FrequentTopnItem *) hash_seq_search(&status)) != NULL)
{
memcpy(&sortedTopnArray[topnIndex], currentTask, sizeof(*currentTask));
topnIndex++;
}
qsort(sortedTopnArray, hashTableSize, sizeof(FrequentTopnItem),
compareFrequentTopnItem);
for (index = numberOfRemainingElements; index < hashTableSize; index++)
{
FrequentTopnItem *topnItem = &(sortedTopnArray[index]);
hash_search(hashTable, (void *) topnItem->key, HASH_REMOVE,
&itemAlreadyHashed);
}
}
/*
* MaterializeAggStateToJsonb extracts the jsonb object from a given HTAB
*/
static Jsonb *
MaterializeAggStateToJsonb(TopnAggState *topn)
{
StringInfo jsonbStr = makeStringInfo();
HASH_SEQ_STATUS status;
FrequentTopnItem *currentTask = NULL;
Jsonb *result = NULL;
appendStringInfo(jsonbStr, "{");
hash_seq_init(&status, topnHashtable(topn));
if ((currentTask = (FrequentTopnItem *) hash_seq_search(&status)) != NULL)
{
InsertPairs(currentTask, jsonbStr);
while ((currentTask = (FrequentTopnItem *) hash_seq_search(&status)) != NULL)
{
appendStringInfo(jsonbStr, ", ");
InsertPairs(currentTask, jsonbStr);
}
}
appendStringInfo(jsonbStr, "}");
result = jsonb_from_cstring(jsonbStr->data, jsonbStr->len);
return result;
}
/* Return TopnAggState's HTAB reference. */
static HTAB *
topnHashtable(TopnAggState *topn)
{
return (HTAB *) topn;
}
/*
* Takes the TopnAggState in source and merges them into the destination
* TopnAggState. If there are the same key values, their frequencies are
* summed up and the unique ones are just taken as they are.
*/
static void
MergeTopn(TopnAggState *destination, TopnAggState *source)
{
bool found = false;
HASH_SEQ_STATUS status;
FrequentTopnItem *currentTask = NULL;
char *key = NULL;
FrequentTopnItem *item = NULL;
hash_seq_init(&status, topnHashtable(source));
while ((currentTask = (FrequentTopnItem *) hash_seq_search(&status)) != NULL)
{
int sizeOfHashTable = 0;
int remainingElements = 0;
int itemLimit = 0;
key = currentTask->key;
item = hash_search(topnHashtable(destination), (void *) key, HASH_ENTER, &found);
if (found)
{
IncreaseItemFrequency(item, currentTask->frequency);
}
else
{
item->frequency = currentTask->frequency;
}
sizeOfHashTable = hash_get_num_entries(topnHashtable(destination));
itemLimit = NumberOfCounters * UnionFactor;
remainingElements = sizeOfHashTable / 2;
PruneHashTable(topnHashtable(destination), itemLimit, remainingElements);
}
}
/*
* IncreaseItemFrequency is used to increase the frequency in a controlled manner
* to avoid overflow issues.
*/
static void
IncreaseItemFrequency(FrequentTopnItem *item, Frequency amount)
{
Frequency freq = item->frequency;