Skip to content

Commit 75f54f8

Browse files
committed
fix all nil pointer error in bsi functions
1 parent 32dbf71 commit 75f54f8

File tree

4 files changed

+112
-6
lines changed

4 files changed

+112
-6
lines changed

BitSliceIndexing/bsi.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,10 @@ func (b *BSI) MinMax(parallelism int, op Operation, foundSet *roaring.Bitmap) in
435435

436436
resultsChan := make(chan int64, n)
437437

438+
if foundSet == nil {
439+
foundSet = b.eBM
440+
}
441+
438442
card := foundSet.GetCardinality()
439443
x := card / uint64(n)
440444

@@ -545,7 +549,9 @@ func (b *BSI) minOrMax(op Operation, batch []uint32, resultsChan chan int64, wg
545549
// Sum all values contained within the foundSet. As a convenience, the cardinality of the foundSet
546550
// is also returned (for calculating the average).
547551
func (b *BSI) Sum(foundSet *roaring.Bitmap) (sum int64, count uint64) {
548-
552+
if foundSet == nil {
553+
foundSet = b.eBM
554+
}
549555
count = foundSet.GetCardinality()
550556
var wg sync.WaitGroup
551557
for i := 0; i < b.BitCount(); i++ {
@@ -569,7 +575,9 @@ func (b *BSI) Transpose() *roaring.Bitmap {
569575
// the column IDs in the source (foundSet) as compared with this BSI. This can be useful for
570576
// vectoring one set of integers to another.
571577
func (b *BSI) IntersectAndTranspose(parallelism int, foundSet *roaring.Bitmap) *roaring.Bitmap {
572-
578+
if foundSet == nil {
579+
foundSet = b.eBM
580+
}
573581
trans := &task{bsi: b}
574582
return parallelExecutor(parallelism, trans, transpose, foundSet)
575583
}
@@ -820,7 +828,9 @@ func (b *BSI) addDigit(foundSet *roaring.Bitmap, i int) {
820828
// is useful for situations where there is a one-to-many relationship between the vectored integer sets. The resulting BSI
821829
// contains the number of times a particular value appeared in the input BSI as an integer count.
822830
func (b *BSI) TransposeWithCounts(parallelism int, foundSet *roaring.Bitmap) *BSI {
823-
831+
if foundSet == nil {
832+
foundSet = b.eBM
833+
}
824834
return parallelExecutorBSIResults(parallelism, b, transposeWithCounts, foundSet, true)
825835
}
826836

@@ -847,6 +857,9 @@ func transposeWithCounts(input *BSI, batch []uint32, resultsChan chan *BSI, wg *
847857

848858
// Increment - In-place increment of values in a BSI. Found set select columns for incrementing.
849859
func (b *BSI) Increment(foundSet *roaring.Bitmap) {
860+
if foundSet == nil {
861+
foundSet = b.eBM
862+
}
850863
b.addDigit(foundSet, 0)
851864
b.eBM.Or(foundSet)
852865
}

BitSliceIndexing/bsi_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,3 +475,28 @@ func TestIssue426(t *testing.T) {
475475
fmt.Println(bitmap.ToArray())
476476
assert.Equal(t, uint64(0), bitmap.GetCardinality())
477477
}
478+
479+
func TestMinMaxWithNil(t *testing.T) {
480+
bsi := setupRandom()
481+
assert.Equal(t, bsi.MinValue, bsi.MinMax(0, MIN, nil))
482+
assert.Equal(t, bsi.MaxValue, bsi.MinMax(0, MAX, nil))
483+
}
484+
485+
func TestSumWithNil(t *testing.T) {
486+
487+
bsi := setup()
488+
489+
sum, count := bsi.Sum(bsi.GetExistenceBitmap())
490+
sumNil, countNil := bsi.Sum(nil)
491+
assert.Equal(t, countNil, count)
492+
assert.Equal(t, sumNil, sum)
493+
}
494+
495+
func TestTransposeWithCountsNil(t *testing.T) {
496+
bsi := setup()
497+
bsi.SetValue(101, 50)
498+
transposed := bsi.TransposeWithCounts(0, nil)
499+
a, ok := transposed.GetValue(uint64(50))
500+
assert.True(t, ok)
501+
assert.Equal(t, int64(2), a)
502+
}

roaring64/bsi64.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,13 @@ func (b *BSI) CompareValue(parallelism int, op Operation, valueOrStart, end int6
312312
func (b *BSI) CompareBigValue(parallelism int, op Operation, valueOrStart, end *big.Int,
313313
foundSet *Bitmap) *Bitmap {
314314

315+
if valueOrStart == nil {
316+
valueOrStart = b.MinMaxBig(parallelism, MIN, &b.eBM)
317+
}
318+
if end == nil {
319+
end = b.MinMaxBig(parallelism, MAX, &b.eBM)
320+
}
321+
315322
comp := &task{bsi: b, op: op, valueOrStart: valueOrStart, end: end}
316323
if foundSet == nil {
317324
return parallelExecutor(parallelism, comp, compareValue, &b.eBM)
@@ -656,7 +663,9 @@ func (b *BSI) Sum(foundSet *Bitmap) (int64, uint64) {
656663
// SumBigValues - Sum all values contained within the foundSet. As a convenience, the cardinality of the foundSet
657664
// is also returned (for calculating the average). This method will sum arbitrarily large values.
658665
func (b *BSI) SumBigValues(foundSet *Bitmap) (sum *big.Int, count uint64) {
659-
666+
if foundSet == nil {
667+
foundSet = &b.eBM
668+
}
660669
sum = new(big.Int)
661670
count = foundSet.GetCardinality()
662671
resultsChan := make(chan int64, b.BitCount())
@@ -691,7 +700,9 @@ func (b *BSI) Transpose() *Bitmap {
691700
//
692701
// TODO: This implementation is functional but not performant, needs to be re-written perhaps using SIMD SSE2 instructions.
693702
func (b *BSI) IntersectAndTranspose(parallelism int, foundSet *Bitmap) *Bitmap {
694-
703+
if foundSet == nil {
704+
foundSet = &b.eBM
705+
}
695706
trans := &task{bsi: b}
696707
return parallelExecutor(parallelism, trans, transpose, foundSet)
697708
}
@@ -1009,7 +1020,12 @@ func (b *BSI) addDigit(foundSet *Bitmap, i int) {
10091020
// is useful for situations where there is a one-to-many relationship between the vectored integer sets. The resulting BSI
10101021
// contains the number of times a particular value appeared in the input BSI.
10111022
func (b *BSI) TransposeWithCounts(parallelism int, foundSet, filterSet *Bitmap) *BSI {
1012-
1023+
if foundSet == nil {
1024+
foundSet = &b.eBM
1025+
}
1026+
if filterSet == nil {
1027+
filterSet = &b.eBM
1028+
}
10131029
return parallelExecutorBSIResults(parallelism, b, transposeWithCounts, foundSet, filterSet, true)
10141030
}
10151031

@@ -1039,6 +1055,9 @@ func transposeWithCounts(input *BSI, filterSet *Bitmap, batch []uint64, resultsC
10391055

10401056
// Increment - In-place increment of values in a BSI. Found set select columns for incrementing.
10411057
func (b *BSI) Increment(foundSet *Bitmap) {
1058+
if foundSet == nil {
1059+
foundSet = &b.eBM
1060+
}
10421061
b.addDigit(foundSet, 0)
10431062
b.eBM.Or(foundSet)
10441063
}

roaring64/bsi64_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,3 +770,52 @@ func TestMutatedBsiEquality(t *testing.T) {
770770
fresh.SetValue(0, 1)
771771
assert.True(t, fresh.Equals(mutated))
772772
}
773+
774+
func TestSumWithNil(t *testing.T) {
775+
bsi := setupNegativeBoundary()
776+
assert.Equal(t, uint64(11), bsi.GetCardinality())
777+
sum, cnt := bsi.Sum(nil)
778+
assert.Equal(t, uint64(11), cnt)
779+
assert.Equal(t, int64(0), sum)
780+
}
781+
782+
func TestTransposeWithCountsNil(t *testing.T) {
783+
bsi := setup()
784+
bsi.SetValue(101, 50)
785+
transposed := bsi.TransposeWithCounts(0, nil, nil)
786+
a, ok := transposed.GetValue(uint64(50))
787+
assert.True(t, ok)
788+
assert.Equal(t, int64(2), a)
789+
a, ok = transposed.GetValue(uint64(49))
790+
assert.True(t, ok)
791+
assert.Equal(t, int64(1), a)
792+
}
793+
794+
func TestRangeNilBig(t *testing.T) {
795+
796+
bsi := NewDefaultBSI()
797+
798+
// Populate large timestamp values
799+
for i := 0; i <= 100; i++ {
800+
t := time.Now()
801+
newTime := t.AddDate(1000, 0, 0) // Add 1000 years
802+
secs := int64(newTime.UnixMilli() / 1000)
803+
nano := int32(newTime.Nanosecond())
804+
bigTime := secondsAndNanosToBigInt(secs, nano)
805+
bsi.SetBigValue(uint64(i), bigTime)
806+
}
807+
808+
start, _ := bsi.GetBigValue(uint64(45)) // starting value at columnID 45
809+
end, _ := bsi.GetBigValue(uint64(55)) // ending value at columnID 55
810+
setStart := bsi.CompareBigValue(0, RANGE, nil, end, nil)
811+
tmpStart := bsi.CompareBigValue(0, RANGE, bsi.MinMaxBig(0, MIN, nil), end, nil)
812+
assert.Equal(t, tmpStart.GetCardinality(), setStart.GetCardinality())
813+
814+
setEnd := bsi.CompareBigValue(0, RANGE, start, nil, nil)
815+
tmpEnd := bsi.CompareBigValue(0, RANGE, start, bsi.MinMaxBig(0, MAX, nil), nil)
816+
assert.Equal(t, tmpEnd.GetCardinality(), setEnd.GetCardinality())
817+
818+
setAll := bsi.CompareBigValue(0, RANGE, nil, nil, nil)
819+
tmpAll := bsi.CompareBigValue(0, RANGE, bsi.MinMaxBig(0, MIN, nil), bsi.MinMaxBig(0, MAX, nil), nil)
820+
assert.Equal(t, tmpAll.GetCardinality(), setAll.GetCardinality())
821+
}

0 commit comments

Comments
 (0)