Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 10 additions & 16 deletions roaring64/bsi64.go
Original file line number Diff line number Diff line change
Expand Up @@ -1131,22 +1131,16 @@ func (b *BSI) SumBigValues(foundSet *Bitmap) (sum *big.Int, count uint64) {
}
sum = new(big.Int)
count = foundSet.GetCardinality()
resultsChan := make(chan int64, b.BitCount())
var wg sync.WaitGroup
for i := 0; i < b.BitCount(); i++ {
wg.Add(1)
go func(j int) {
defer wg.Done()
resultsChan <- int64(foundSet.AndCardinality(&b.bA[j]) << uint(j))
}(i)
}
wg.Wait()
close(resultsChan)

for val := range resultsChan {
sum.Add(sum, big.NewInt(val))
}
sum.Sub(sum, big.NewInt(int64(foundSet.AndCardinality(&b.bA[b.BitCount()])<<uint(b.BitCount()))))
bitCount := b.BitCount()
var temp big.Int
for i := 0; i < bitCount; i++ {
temp.SetUint64(foundSet.AndCardinality(&b.bA[i]))
temp.Lsh(&temp, uint(i))
sum.Add(sum, &temp)
}
temp.SetUint64(foundSet.AndCardinality(&b.bA[bitCount]))
temp.Lsh(&temp, uint(bitCount))
sum.Sub(sum, &temp)

return sum, count
}
Expand Down
66 changes: 66 additions & 0 deletions roaring64/bsi64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,72 @@ func TestSumWithNil(t *testing.T) {
assert.Equal(t, int64(0), sum)
}

func TestSumBigValues(t *testing.T) {
positive := new(big.Int).Lsh(big.NewInt(1), 149)
positive.Add(positive, big.NewInt(7))
negative := new(big.Int).Lsh(big.NewInt(1), 120)
negative.Neg(negative)
negative.Sub(negative, big.NewInt(11))
values := []struct {
columnID uint64
value *big.Int
}{
{1, big.NewInt(-9)},
{2, positive},
{3, negative},
{4, big.NewInt(7)},
}

bsi := NewDefaultBSI()
want := new(big.Int)
for _, value := range values {
bsi.SetBigValue(value.columnID, value.value)
want.Add(want, value.value)
}

filteredWant := new(big.Int).Add(values[0].value, values[2].value)
for _, test := range []struct {
name string
foundSet *Bitmap
want *big.Int
count uint64
}{
{"all", nil, want, uint64(len(values))},
{"filtered", BitmapOf(1, 3), filteredWant, 2},
} {
t.Run(test.name, func(t *testing.T) {
sum, count := bsi.SumBigValues(test.foundSet)
assert.Equal(t, test.count, count)
assert.Zero(t, sum.Cmp(test.want))
})
}
}

func BenchmarkBSI64SumBigValues(b *testing.B) {
const valueCount = 128

bsi := NewBSI(Max64BitSigned, 0)
foundSet := bsi.GetExistenceBitmap()
want := new(big.Int)
for columnID := uint64(0); columnID < valueCount; columnID++ {
value := int64(columnID)
bsi.SetValue(columnID, value)
want.Add(want, big.NewInt(value))
}
if bsi.BitCount() != 63 {
b.Fatalf("BitCount() = %d, want 63", bsi.BitCount())
}

var sum *big.Int
var count uint64
for b.Loop() {
sum, count = bsi.SumBigValues(foundSet)
}
if count != valueCount || sum.Cmp(want) != 0 {
b.Fatalf("SumBigValues() = (%v, %d), want (%v, %d)", sum, count, want, valueCount)
}
}

func TestTransposeWithCountsNil(t *testing.T) {
bsi := setup()
bsi.SetValue(101, 50)
Expand Down
Loading