Skip to content

Commit 8494064

Browse files
committed
New C function: bms_add_range
This will be used by pending patches to improve partition pruning. Amit Langote and Kyotaro Horiguchi, per a suggestion from David Rowley. Review and testing of the larger patch set of which this is a part by Ashutosh Bapat, David Rowley, Dilip Kumar, Jesper Pedersen, Rajkumar Raghuwanshi, Beena Emerson, Amul Sul, and Kyotaro Horiguchi. Discussion: http://postgr.es/m/098b9c71-1915-1a2a-8d52-1a7a50ce79e8@lab.ntt.co.jp
1 parent 8d4e70a commit 8494064

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

src/backend/nodes/bitmapset.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,78 @@ bms_add_members(Bitmapset *a, const Bitmapset *b)
784784
return result;
785785
}
786786

787+
/*
788+
* bms_add_range
789+
* Add members in the range of 'lower' to 'upper' to the set.
790+
*
791+
* Note this could also be done by calling bms_add_member in a loop, however,
792+
* using this function will be faster when the range is large as we work with
793+
* at the bitmapword level rather than at bit level.
794+
*/
795+
Bitmapset *
796+
bms_add_range(Bitmapset *a, int lower, int upper)
797+
{
798+
int lwordnum,
799+
lbitnum,
800+
uwordnum,
801+
ushiftbits,
802+
wordnum;
803+
804+
if (lower < 0 || upper < 0)
805+
elog(ERROR, "negative bitmapset member not allowed");
806+
if (lower > upper)
807+
elog(ERROR, "lower range must not be above upper range");
808+
uwordnum = WORDNUM(upper);
809+
810+
if (a == NULL)
811+
{
812+
a = (Bitmapset *) palloc0(BITMAPSET_SIZE(uwordnum + 1));
813+
a->nwords = uwordnum + 1;
814+
}
815+
816+
/* ensure we have enough words to store the upper bit */
817+
else if (uwordnum >= a->nwords)
818+
{
819+
int oldnwords = a->nwords;
820+
int i;
821+
822+
a = (Bitmapset *) repalloc(a, BITMAPSET_SIZE(uwordnum + 1));
823+
a->nwords = uwordnum + 1;
824+
/* zero out the enlarged portion */
825+
for (i = oldnwords; i < a->nwords; i++)
826+
a->words[i] = 0;
827+
}
828+
829+
wordnum = lwordnum = WORDNUM(lower);
830+
831+
lbitnum = BITNUM(lower);
832+
ushiftbits = BITS_PER_BITMAPWORD - (BITNUM(upper) + 1);
833+
834+
/*
835+
* Special case when lwordnum is the same as uwordnum we must perform the
836+
* upper and lower masking on the word.
837+
*/
838+
if (lwordnum == uwordnum)
839+
{
840+
a->words[lwordnum] |= ~(bitmapword) (((bitmapword) 1 << lbitnum) - 1)
841+
& (~(bitmapword) 0) >> ushiftbits;
842+
}
843+
else
844+
{
845+
/* turn on lbitnum and all bits left of it */
846+
a->words[wordnum++] |= ~(bitmapword) (((bitmapword) 1 << lbitnum) - 1);
847+
848+
/* turn on all bits for any intermediate words */
849+
while (wordnum < uwordnum)
850+
a->words[wordnum++] = ~(bitmapword) 0;
851+
852+
/* turn on upper's bit and all bits right of it. */
853+
a->words[uwordnum] |= (~(bitmapword) 0) >> ushiftbits;
854+
}
855+
856+
return a;
857+
}
858+
787859
/*
788860
* bms_int_members - like bms_intersect, but left input is recycled
789861
*/

src/include/nodes/bitmapset.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ extern bool bms_is_empty(const Bitmapset *a);
9090
extern Bitmapset *bms_add_member(Bitmapset *a, int x);
9191
extern Bitmapset *bms_del_member(Bitmapset *a, int x);
9292
extern Bitmapset *bms_add_members(Bitmapset *a, const Bitmapset *b);
93+
extern Bitmapset *bms_add_range(Bitmapset *a, int lower, int upper);
9394
extern Bitmapset *bms_int_members(Bitmapset *a, const Bitmapset *b);
9495
extern Bitmapset *bms_del_members(Bitmapset *a, const Bitmapset *b);
9596
extern Bitmapset *bms_join(Bitmapset *a, Bitmapset *b);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy