PostgreSQL Source Code git master
test_bitmapset.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * test_bitmapset.c
4 * Test the Bitmapset data structure.
5 *
6 * This module tests the Bitmapset implementation in PostgreSQL, covering
7 * all public API functions.
8 *
9 * Copyright (c) 2025, PostgreSQL Global Development Group
10 *
11 * IDENTIFICATION
12 * src/test/modules/test_bitmapset/test_bitmapset.c
13 *
14 *-------------------------------------------------------------------------
15 */
16
17#include "postgres.h"
18
19#include <stddef.h>
20#include "catalog/pg_type.h"
21#include "common/pg_prng.h"
22#include "utils/array.h"
23#include "fmgr.h"
24#include "nodes/bitmapset.h"
25#include "nodes/nodes.h"
26#include "nodes/pg_list.h"
27#include "utils/builtins.h"
28#include "utils/timestamp.h"
29
31
32/* Bitmapset API functions in order of appearance in bitmapset.c */
65
66/* Test utility functions */
68
69/* Convenient macros to test results */
70#define EXPECT_TRUE(expr) \
71 do { \
72 if (!(expr)) \
73 elog(ERROR, \
74 "%s was unexpectedly false in file \"%s\" line %u", \
75 #expr, __FILE__, __LINE__); \
76 } while (0)
77
78#define EXPECT_NOT_NULL(expr) \
79 do { \
80 if ((expr) == NULL) \
81 elog(ERROR, \
82 "%s was unexpectedly true in file \"%s\" line %u", \
83 #expr, __FILE__, __LINE__); \
84 } while (0)
85
86/* Encode/Decode to/from TEXT and Bitmapset */
87#define BITMAPSET_TO_TEXT(bms) cstring_to_text(nodeToString(bms))
88#define TEXT_TO_BITMAPSET(str) ((Bitmapset *) stringToNode(text_to_cstring(str)))
89
90/*
91 * Helper macro to fetch text parameters as Bitmapsets. SQL-NULL means empty
92 * set.
93 */
94#define PG_ARG_GETBITMAPSET(n) \
95 (PG_ARGISNULL(n) ? NULL : TEXT_TO_BITMAPSET(PG_GETARG_TEXT_PP(n)))
96
97/*
98 * Helper macro to handle converting sets back to text, returning the
99 * resulting text representation of the set.
100 */
101#define PG_RETURN_BITMAPSET_AS_TEXT(bms) \
102 PG_RETURN_TEXT_P(BITMAPSET_TO_TEXT(bms))
103
104/*
105 * Individual test functions for each bitmapset API function
106 *
107 * Primarily, we aim to keep these as close to simple wrapper functions as
108 * possible in order to publish the functions of bitmapset.c to the SQL layer
109 * with as little interference as possible. We opt to return SQL NULL in
110 * cases where the input given to the SQL function isn't valid to pass to the
111 * underlying bitmapset.c function. For example we cannot do much useful
112 * testing if someone calls test_bms_make_singleton(NULL) since
113 * bms_make_singleton() expects an integer argument.
114 *
115 * For function arguments which are to be converted to Bitmapsets, we accept
116 * SQL NULL as a valid argument to mean an empty set. Optionally callers may
117 * pass '(b)'.
118 *
119 * For the test functions which return a Bitmapset, these are converted back
120 * to text with result generated by nodeToString().
121 */
122
123Datum
125{
126 Bitmapset *bms;
127 int member;
128
129 if (PG_ARGISNULL(1))
130 PG_RETURN_NULL(); /* invalid input */
131
132 bms = PG_ARG_GETBITMAPSET(0);
133 member = PG_GETARG_INT32(1);
134
135 bms = bms_add_member(bms, member);
136
138}
139
140Datum
142{
145
146 /* left input is recycled */
147 bms1 = bms_add_members(bms1, bms2);
148
150}
151
152Datum
154{
155 Bitmapset *bms;
156 int32 member;
157
158 if (PG_ARGISNULL(1))
159 PG_RETURN_NULL(); /* invalid input */
160
161 bms = PG_ARG_GETBITMAPSET(0);
162 member = PG_GETARG_INT32(1);
163
164 bms = bms_del_member(bms, member);
165
167}
168
169Datum
171{
172 Bitmapset *bms;
173 int32 member;
174 bool result;
175
176 if (PG_ARGISNULL(1))
177 PG_RETURN_NULL(); /* invalid input */
178
179 bms = PG_ARG_GETBITMAPSET(0);
180 member = PG_GETARG_INT32(1);
181
182 result = bms_is_member(member, bms);
183
184 PG_RETURN_BOOL(result);
185}
186
187Datum
189{
191 int result;
192
193 result = bms_num_members(bms);
194
195 PG_RETURN_INT32(result);
196}
197
198Datum
200{
201 Bitmapset *bms;
202 int32 member;
203
204 member = PG_GETARG_INT32(0);
205 bms = bms_make_singleton(member);
206
208}
209
210Datum
212{
214 Bitmapset *copy_bms;
215
216 copy_bms = bms_copy(bms);
217
219}
220
221Datum
223{
226 bool result;
227
228 result = bms_equal(bms1, bms2);
229
230 PG_RETURN_BOOL(result);
231}
232
233Datum
235{
238 Bitmapset *result_bms;
239
240 result_bms = bms_union(bms1, bms2);
241
242 PG_RETURN_BITMAPSET_AS_TEXT(result_bms);
243}
244
245Datum
247{
249 BMS_Membership result;
250
251 result = bms_membership(bms);
252
253 PG_RETURN_INT32((int32) result);
254}
255
256Datum
258{
259 Bitmapset *bms;
260 int32 prevmember;
261 int result;
262
263 if (PG_ARGISNULL(1))
264 PG_RETURN_NULL(); /* invalid input */
265
266 bms = PG_ARG_GETBITMAPSET(0);
267 prevmember = PG_GETARG_INT32(1);
268
269 result = bms_next_member(bms, prevmember);
270
271 PG_RETURN_INT32(result);
272}
273
274Datum
276{
279 Bitmapset *result_bms;
280
281 result_bms = bms_intersect(bms1, bms2);
282
283 PG_RETURN_BITMAPSET_AS_TEXT(result_bms);
284}
285
286Datum
288{
291 Bitmapset *result_bms;
292
293 result_bms = bms_difference(bms1, bms2);
294
295 PG_RETURN_BITMAPSET_AS_TEXT(result_bms);
296}
297
298Datum
300{
303 int result;
304
305 result = bms_compare(bms1, bms2);
306
307 PG_RETURN_INT32(result);
308}
309
310Datum
312{
314 bool result;
315
316 result = bms_is_empty(bms);
317
318 PG_RETURN_BOOL(result);
319}
320
321Datum
323{
326 bool result;
327
328 result = bms_is_subset(bms1, bms2);
329
330 PG_RETURN_BOOL(result);
331}
332
333Datum
335{
338 BMS_Comparison result;
339
340 result = bms_subset_compare(bms1, bms2);
341
342 PG_RETURN_INT32((int32) result);
343}
344
345Datum
347{
349 int result;
350
351 result = bms_singleton_member(bms);
352
353 PG_RETURN_INT32(result);
354}
355
356Datum
358{
360 int member;
361
362 /*
363 * Keep this simple. Return -1 when we detect the set is not a singleton
364 * set, otherwise return the singleton member.
365 */
366 if (!bms_get_singleton_member(bms, &member))
367 member = -1;
368
369 PG_RETURN_INT32(member);
370}
371
372Datum
374{
375 Bitmapset *bms;
376 int32 prevmember;
377 int result;
378
379 if (PG_ARGISNULL(1))
380 PG_RETURN_NULL(); /* invalid input */
381
382 bms = PG_ARG_GETBITMAPSET(0);
383 prevmember = PG_GETARG_INT32(1);
384
385 result = bms_prev_member(bms, prevmember);
386
387 PG_RETURN_INT32(result);
388}
389
390Datum
392{
395 bool result;
396
397 result = bms_overlap(bms1, bms2);
398
399 PG_RETURN_BOOL(result);
400}
401
402Datum
404{
405 Bitmapset *bms;
406 ArrayType *array;
407 List *int_list = NIL;
408 bool result;
409 Datum *elem_datums = NULL;
410 bool *elem_nulls = NULL;
411 int elem_count;
412 int i;
413
414 bms = PG_ARG_GETBITMAPSET(0);
415
416 if (!PG_ARGISNULL(1))
417 {
418 array = PG_GETARG_ARRAYTYPE_P(1);
419
420 deconstruct_array(array,
421 INT4OID, sizeof(int32), true, 'i',
422 &elem_datums, &elem_nulls, &elem_count);
423
424 for (i = 0; i < elem_count; i++)
425 {
426 if (!elem_nulls[i])
427 {
428 int32 member = DatumGetInt32(elem_datums[i]);
429
430 int_list = lappend_int(int_list, member);
431 }
432 }
433 }
434
435 result = bms_overlap_list(bms, int_list);
436
437 list_free(int_list);
438
439 if (elem_datums)
440 pfree(elem_datums);
441
442 if (elem_nulls)
443 pfree(elem_nulls);
444
445 PG_RETURN_BOOL(result);
446}
447
448Datum
450{
453 bool result;
454
455 result = bms_nonempty_difference(bms1, bms2);
456
457 PG_RETURN_BOOL(result);
458}
459
460Datum
462{
463 Bitmapset *bms;
464 int32 member;
465 int result;
466
467 if (PG_ARGISNULL(1))
468 PG_RETURN_NULL(); /* invalid input */
469
470 bms = PG_ARG_GETBITMAPSET(0);
471 member = PG_GETARG_INT32(1);
472
473 result = bms_member_index(bms, member);
474
475 PG_RETURN_INT32(result);
476}
477
478Datum
480{
481 Bitmapset *bms;
482 int32 lower,
483 upper;
484
485 if (PG_ARGISNULL(1) || PG_ARGISNULL(2))
486 PG_RETURN_NULL(); /* invalid input */
487
488 bms = PG_ARG_GETBITMAPSET(0);
491
492 bms = bms_add_range(bms, lower, upper);
493
495}
496
497Datum
499{
502
503 /* left input gets recycled */
504 bms1 = bms_int_members(bms1, bms2);
505
507}
508
509Datum
511{
514
515 /* left input gets recycled */
516 bms1 = bms_del_members(bms1, bms2);
517
519}
520
521Datum
523{
526
527 /* left input gets recycled */
528 bms1 = bms_replace_members(bms1, bms2);
529
531}
532
533Datum
535{
538 Bitmapset *result_bms;
539
540 /* either input can be recycled */
541 result_bms = bms_join(bms1, bms2);
542
543 PG_RETURN_BITMAPSET_AS_TEXT(result_bms);
544}
545
546Datum
548{
550 uint32 hash_result;
551
552 hash_result = bms_hash_value(bms);
553
554 PG_RETURN_INT32(hash_result);
555}
556
557Datum
559{
561 uint32 hash_result;
562
563 /* Call bitmap_hash */
564 hash_result = bitmap_hash(&bms, sizeof(Bitmapset *));
565
566 PG_RETURN_INT32(hash_result);
567}
568
569Datum
571{
574 int match_result;
575
576 /* Call bitmap_match with addresses of the Bitmapset pointers */
577 match_result = bitmap_match(&bms1, &bms2, sizeof(Bitmapset *));
578
579 PG_RETURN_INT32(match_result);
580}
581
582/*
583 * Contrary to all the other functions which are one-one mappings with the
584 * equivalent C functions, this stresses Bitmapsets in a random fashion for
585 * various operations.
586 *
587 * "min_value" is the minimal value used for the members, that will stand
588 * up to a range of "max_range". "num_ops" defines the number of time each
589 * operation is done. "seed" is a random seed used to calculate the member
590 * values. When "seed" is <= 0, a random seed will be chosen automatically.
591 *
592 * The return value is the number of times all operations have been executed.
593 */
594Datum
596{
597 Bitmapset *bms1 = NULL;
598 Bitmapset *bms2 = NULL;
599 Bitmapset *bms = NULL;
600 Bitmapset *result = NULL;
603 int num_ops;
604 int max_range;
605 int min_value;
606 int member;
607 int *members;
608 int num_members = 0;
609 int total_ops = 0;
610
611 if (PG_GETARG_INT32(0) > 0)
612 seed = PG_GETARG_INT32(0);
613
614 num_ops = PG_GETARG_INT32(1);
615 max_range = PG_GETARG_INT32(2);
616 min_value = PG_GETARG_INT32(3);
617
618 pg_prng_seed(&state, seed);
619
620 /*
621 * There can be up to "num_ops" members added. This is very unlikely,
622 * still possible if all the operations hit the "0" case during phase 4
623 * where multiple operation types are mixed together.
624 */
625 members = palloc(sizeof(int) * num_ops);
626
627 /* Phase 1: Random insertions in first set */
628 for (int i = 0; i < num_ops / 2; i++)
629 {
630 member = pg_prng_uint32(&state) % max_range + min_value;
631
632 if (!bms_is_member(member, bms1))
633 members[num_members++] = member;
634 bms1 = bms_add_member(bms1, member);
635 }
636
637 /* Phase 2: Random insertions in second set */
638 for (int i = 0; i < num_ops / 4; i++)
639 {
640 member = pg_prng_uint32(&state) % max_range + min_value;
641
642 if (!bms_is_member(member, bms2))
643 members[num_members++] = member;
644 bms2 = bms_add_member(bms2, member);
645 }
646
647 /* Test union */
648 result = bms_union(bms1, bms2);
649 EXPECT_NOT_NULL(result);
650
651 /* Verify union contains all members from first and second sets */
652 for (int i = 0; i < num_members; i++)
653 {
654 if (!bms_is_member(members[i], result))
655 elog(ERROR, "union missing member %d", members[i]);
656 }
657 bms_free(result);
658
659 /*
660 * Test intersection, checking that all the members in the result are from
661 * both the first and second sets.
662 */
663 result = bms_intersect(bms1, bms2);
664 if (result != NULL)
665 {
666 member = -1;
667
668 while ((member = bms_next_member(result, member)) >= 0)
669 {
670 if (!bms_is_member(member, bms1) || !bms_is_member(member, bms2))
671 elog(ERROR, "intersection contains invalid member %d", member);
672 }
673 bms_free(result);
674 }
675
676 /* Phase 3: Test range operations */
677 result = NULL;
678 for (int i = 0; i < num_ops; i++)
679 {
680 int lower = pg_prng_uint32(&state) % 100;
681 int upper = lower + (pg_prng_uint32(&state) % 20);
682
683 result = bms_add_range(result, lower, upper);
684 }
685 if (result != NULL)
686 {
687 EXPECT_TRUE(bms_num_members(result) > 0);
688 bms_free(result);
689 }
690
691 bms_free(bms1);
692 bms_free(bms2);
693
694 /*
695 * Phase 4: mix of operations on a single set, cross-checking a bitmap
696 * with a secondary state, "members".
697 */
698 num_members = 0;
699
700 for (int op = 0; op < num_ops; op++)
701 {
702 switch (pg_prng_uint32(&state) % 3)
703 {
704 case 0: /* add */
705 member = pg_prng_uint32(&state) % max_range + min_value;
706 if (!bms_is_member(member, bms))
707 members[num_members++] = member;
708 bms = bms_add_member(bms, member);
709 break;
710 case 1: /* delete */
711 if (num_members > 0)
712 {
713 int pos = pg_prng_uint32(&state) % num_members;
714
715 member = members[pos];
716 if (!bms_is_member(member, bms))
717 elog(ERROR, "expected %d to be a valid member", member);
718
719 bms = bms_del_member(bms, member);
720
721 /*
722 * Move the final array member at the position of the
723 * member just deleted, reducing the array size by one.
724 */
725 members[pos] = members[--num_members];
726 }
727 break;
728 case 2: /* test membership */
729 /* Verify that bitmap contains all members */
730 for (int i = 0; i < num_members; i++)
731 {
732 if (!bms_is_member(members[i], bms))
733 elog(ERROR, "missing member %d", members[i]);
734 }
735 break;
736 }
737 total_ops++;
738 }
739
740 bms_free(bms);
741 pfree(members);
742
743 PG_RETURN_INT32(total_ops);
744}
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:263
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3632
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1645
Bitmapset * bms_replace_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:972
Bitmapset * bms_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:346
int bms_prev_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1367
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:216
Bitmapset * bms_int_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:1109
Bitmapset * bms_intersect(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:292
uint32 bitmap_hash(const void *key, Size keysize)
Definition: bitmapset.c:1436
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:142
BMS_Comparison bms_subset_compare(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:445
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1306
uint32 bms_hash_value(const Bitmapset *a)
Definition: bitmapset.c:1420
Bitmapset * bms_del_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:1161
Bitmapset * bms_add_range(Bitmapset *a, int lower, int upper)
Definition: bitmapset.c:1019
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition: bitmapset.c:868
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:412
int bms_singleton_member(const Bitmapset *a)
Definition: bitmapset.c:672
void bms_free(Bitmapset *a)
Definition: bitmapset.c:239
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:751
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:510
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:815
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:917
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:251
int bitmap_match(const void *key1, const void *key2, Size keysize)
Definition: bitmapset.c:1446
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:781
int bms_member_index(Bitmapset *a, int x)
Definition: bitmapset.c:539
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:582
int bms_compare(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:183
bool bms_get_singleton_member(const Bitmapset *a, int *member)
Definition: bitmapset.c:715
Bitmapset * bms_join(Bitmapset *a, Bitmapset *b)
Definition: bitmapset.c:1230
bool bms_nonempty_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:641
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:122
bool bms_overlap_list(const Bitmapset *a, const List *b)
Definition: bitmapset.c:608
#define bms_is_empty(a)
Definition: bitmapset.h:118
BMS_Comparison
Definition: bitmapset.h:61
BMS_Membership
Definition: bitmapset.h:70
int32_t int32
Definition: c.h:534
uint64_t uint64
Definition: c.h:539
uint32_t uint32
Definition: c.h:538
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define PG_RETURN_NULL()
Definition: fmgr.h:345
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
int i
Definition: isn.c:77
List * lappend_int(List *list, int datum)
Definition: list.c:357
void list_free(List *list)
Definition: list.c:1546
void pfree(void *pointer)
Definition: mcxt.c:1594
void * palloc(Size size)
Definition: mcxt.c:1365
Datum lower(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:49
Datum upper(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:80
#define NIL
Definition: pg_list.h:68
uint32 pg_prng_uint32(pg_prng_state *state)
Definition: pg_prng.c:227
void pg_prng_seed(pg_prng_state *state, uint64 seed)
Definition: pg_prng.c:89
uint64_t Datum
Definition: postgres.h:70
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:212
Definition: pg_list.h:54
Definition: regguts.h:323
Datum test_bms_compare(PG_FUNCTION_ARGS)
Datum test_bms_difference(PG_FUNCTION_ARGS)
Datum test_random_operations(PG_FUNCTION_ARGS)
#define PG_RETURN_BITMAPSET_AS_TEXT(bms)
Datum test_bms_hash_value(PG_FUNCTION_ARGS)
#define EXPECT_TRUE(expr)
Datum test_bms_del_member(PG_FUNCTION_ARGS)
Datum test_bitmap_match(PG_FUNCTION_ARGS)
Datum test_bms_is_member(PG_FUNCTION_ARGS)
#define PG_ARG_GETBITMAPSET(n)
Datum test_bms_is_empty(PG_FUNCTION_ARGS)
Datum test_bms_add_member(PG_FUNCTION_ARGS)
Datum test_bms_int_members(PG_FUNCTION_ARGS)
Datum test_bms_add_members(PG_FUNCTION_ARGS)
PG_MODULE_MAGIC
Datum test_bitmap_hash(PG_FUNCTION_ARGS)
Datum test_bms_member_index(PG_FUNCTION_ARGS)
Datum test_bms_singleton_member(PG_FUNCTION_ARGS)
Datum test_bms_overlap_list(PG_FUNCTION_ARGS)
Datum test_bms_subset_compare(PG_FUNCTION_ARGS)
Datum test_bms_overlap(PG_FUNCTION_ARGS)
Datum test_bms_join(PG_FUNCTION_ARGS)
#define EXPECT_NOT_NULL(expr)
Datum test_bms_prev_member(PG_FUNCTION_ARGS)
PG_FUNCTION_INFO_V1(test_bms_make_singleton)
Datum test_bms_equal(PG_FUNCTION_ARGS)
Datum test_bms_add_range(PG_FUNCTION_ARGS)
Datum test_bms_copy(PG_FUNCTION_ARGS)
Datum test_bms_make_singleton(PG_FUNCTION_ARGS)
Datum test_bms_replace_members(PG_FUNCTION_ARGS)
Datum test_bms_get_singleton_member(PG_FUNCTION_ARGS)
Datum test_bms_num_members(PG_FUNCTION_ARGS)
Datum test_bms_next_member(PG_FUNCTION_ARGS)
Datum test_bms_membership(PG_FUNCTION_ARGS)
Datum test_bms_union(PG_FUNCTION_ARGS)
Datum test_bms_intersect(PG_FUNCTION_ARGS)
Datum test_bms_del_members(PG_FUNCTION_ARGS)
Datum test_bms_is_subset(PG_FUNCTION_ARGS)
Datum test_bms_nonempty_difference(PG_FUNCTION_ARGS)