Skip to content
Merged
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
4 changes: 3 additions & 1 deletion BitSliceIndexing/bsi.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,9 @@ func (b *BSI) CompareValue(parallelism int, op Operation, valueOrStart, end int6
if foundSet == nil {
return parallelExecutor(parallelism, comp, compareValue, b.eBM)
}
return parallelExecutor(parallelism, comp, compareValue, foundSet)
// A column of foundSet that holds no value cannot match: without the
// intersection it would be compared as if it held zero.
return parallelExecutor(parallelism, comp, compareValue, roaring.And(foundSet, b.eBM))
}

func compareValue(e *task, batch []uint32, resultsChan chan *roaring.Bitmap, wg *sync.WaitGroup) {
Expand Down
79 changes: 79 additions & 0 deletions BitSliceIndexing/bsi_edgecases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,82 @@ func TestBSISumSemantics(t *testing.T) {
assert.EqualValues(t, 7, sum)
assert.EqualValues(t, 2, count)
}

// Issue 583: RANGE with negative values, and foundSet columns that hold no value.
func TestBSICompareValueIssue583(t *testing.T) {
bsi := NewDefaultBSI()
bsi.SetValue(1, -5)
bsi.SetValue(2, 3)
bsi.SetValue(3, -100)
assert.Equal(t, []uint32{1}, bsi.CompareValue(0, RANGE, -10, 0, nil).ToArray())

bsi = NewDefaultBSI()
bsi.SetValue(1, 10)
bsi.SetValue(2, 20)
foundSet := roaring.BitmapOf(1, 2, 99)
assert.Equal(t, []uint32{1, 2}, bsi.CompareValue(0, GE, 0, 0, foundSet).ToArray())
assert.Equal(t, []uint32{1}, bsi.CompareValue(0, LT, 15, 0, foundSet).ToArray())
assert.Equal(t, []uint32{1, 2}, bsi.CompareValue(0, RANGE, 0, 100, foundSet).ToArray())
assert.Equal(t, []uint32{1}, bsi.CompareValue(0, EQ, 10, 0, foundSet).ToArray())

// A value and a bound of opposite signs and equal magnitude are not equal.
bsi = NewDefaultBSI()
bsi.SetValue(1, 100)
bsi.SetValue(2, -100)
assert.Equal(t, []uint32{2}, bsi.CompareValue(0, EQ, -100, 0, nil).ToArray())
assert.Equal(t, []uint32{1}, bsi.CompareValue(0, GT, -100, 0, nil).ToArray())
assert.Equal(t, []uint32{2}, bsi.CompareValue(0, LT, 100, 0, nil).ToArray())
}

// CompareValue must agree with a plain comparison of the stored values, for
// every operation, with and without a foundSet that has columns holding no value.
func TestBSICompareValueMatchesNaive(t *testing.T) {
ops := []struct {
name string
op Operation
naive func(v, start, end int64) bool
}{
{"LT", LT, func(v, start, _ int64) bool { return v < start }},
{"LE", LE, func(v, start, _ int64) bool { return v <= start }},
{"EQ", EQ, func(v, start, _ int64) bool { return v == start }},
{"GE", GE, func(v, start, _ int64) bool { return v >= start }},
{"GT", GT, func(v, start, _ int64) bool { return v > start }},
{"RANGE", RANGE, func(v, start, end int64) bool { return v >= start && v <= end }},
}
sets := [][]int64{
{-100, -10, -5, -3, -1, 0, 1, 3, 5, 10, 100, math.MinInt64, math.MaxInt64},
{0, 1, 3, 5, 10, 100},
{0, 1, 2, 3},
}
bounds := []int64{math.MinInt64, -200, -100, -11, -10, -5, -3, -1, 0, 1, 3, 5, 10, 11, 100, 200, math.MaxInt64}

for _, values := range sets {
bsi := NewDefaultBSI()
foundSet := roaring.BitmapOf(500, 501) // these two columns hold no value
for i, v := range values {
bsi.SetValue(uint64(i+1), v)
foundSet.Add(uint32(i + 1))
}
for _, o := range ops {
ends := bounds
if o.op != RANGE {
ends = bounds[:1] // the end bound is only read by RANGE
}
for _, start := range bounds {
for _, end := range ends {
want := roaring.New()
for i, v := range values {
if o.naive(v, start, end) {
want.Add(uint32(i + 1))
}
}
for _, fs := range []*roaring.Bitmap{nil, foundSet} {
got := bsi.CompareValue(0, o.op, start, end, fs)
require.Truef(t, want.Equals(got), "%s(%d, %d) on %v, foundSet=%v: got %v, want %v",
o.name, start, end, values, fs != nil, got.ToArray(), want.ToArray())
}
}
}
}
}
}
4 changes: 3 additions & 1 deletion roaring64/bsi64.go
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,9 @@ func (b *BSI) CompareBigValue(parallelism int, op Operation, valueOrStart, end *
if foundSet == nil {
return parallelExecutor(parallelism, comp, compareValue, &b.eBM)
}
return parallelExecutor(parallelism, comp, compareValue, foundSet)
// A column of foundSet that holds no value cannot match: without the
// intersection it would be compared as if it held zero.
return parallelExecutor(parallelism, comp, compareValue, And(foundSet, &b.eBM))
}

func (b *BSI) compareBigValueAsInt64(parallelism int, op Operation, valueOrStart, end *big.Int, foundSet *Bitmap) (*Bitmap, bool) {
Expand Down
57 changes: 57 additions & 0 deletions roaring64/bsi64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1407,3 +1407,60 @@ func TestSumBigValuesParallel(t *testing.T) {
assert.Equal(t, uint64(rows), count)
assert.Zero(t, sum.Cmp(want))
}

// CompareValue must agree with a plain comparison of the stored values, for
// every operation, with and without a foundSet that has columns holding no value.
func TestBSICompareValueMatchesNaive(t *testing.T) {
ops := []struct {
name string
op Operation
naive func(v, start, end int64) bool
}{
{"LT", LT, func(v, start, _ int64) bool { return v < start }},
{"LE", LE, func(v, start, _ int64) bool { return v <= start }},
{"EQ", EQ, func(v, start, _ int64) bool { return v == start }},
{"GE", GE, func(v, start, _ int64) bool { return v >= start }},
{"GT", GT, func(v, start, _ int64) bool { return v > start }},
{"RANGE", RANGE, func(v, start, end int64) bool { return v >= start && v <= end }},
}
sets := [][]int64{
{-100, -10, -5, -3, -1, 0, 1, 3, 5, 10, 100, math.MinInt64, math.MaxInt64},
{-100, -10, -5, -3, -1, 0, 1, 3, 5, 10, 100},
{0, 1, 3, 5, 10, 100},
}
bounds := []int64{math.MinInt64, -200, -100, -11, -10, -5, -3, -1, 0, 1, 3, 5, 10, 11, 100, 200, math.MaxInt64}

for _, values := range sets {
bsi := NewDefaultBSI()
foundSet := BitmapOf(500, 501) // these two columns hold no value
for i, v := range values {
bsi.SetValue(uint64(i+1), v)
foundSet.Add(uint64(i + 1))
}
// CompareValue documents that a bound outside of the range of the
// stored values may give a wrong result, so only bounds in range are used.
lo, hi := slices.Min(values), slices.Max(values)
inRange := slices.DeleteFunc(slices.Clone(bounds), func(b int64) bool { return b < lo || b > hi })
for _, o := range ops {
ends := inRange
if o.op != RANGE {
ends = inRange[:1] // the end bound is only read by RANGE
}
for _, start := range inRange {
for _, end := range ends {
want := NewBitmap()
for i, v := range values {
if o.naive(v, start, end) {
want.Add(uint64(i + 1))
}
}
for _, fs := range []*Bitmap{nil, foundSet} {
got := bsi.CompareValue(0, o.op, start, end, fs)
require.Truef(t, want.Equals(got), "%s(%d, %d) on %v, foundSet=%v: got %v, want %v",
o.name, start, end, values, fs != nil, got.ToArray(), want.ToArray())
}
}
}
}
}
}
Loading