From 333dc5bc0eda3a57a2300b769c392ae504373318 Mon Sep 17 00:00:00 2001 From: yanghao <1994728136@qq.com> Date: Fri, 4 Dec 2026 18:48:07 -0500 Subject: [PATCH] Exclude foundSet columns without a value from BSI CompareValue A column of foundSet that holds no value was compared as if it held zero, so GE 0, LT 15 and RANGE [0, 100] returned it. Intersect foundSet with the existence bitmap in BitSliceIndexing CompareValue and in roaring64 CompareBigValue. The int64 path of roaring64 already did. Add TestBSICompareValueIssue583 and TestBSICompareValueMatchesNaive, which check all six operations against a plain comparison of the stored values, over mixed-sign values including MinInt64 and MaxInt64, with and without a foundSet that has columns holding no value. This is the part of #598 that #606 did not already fix. Fixes #583 --- BitSliceIndexing/bsi.go | 4 +- BitSliceIndexing/bsi_edgecases_test.go | 79 ++++++++++++++++++++++++++ roaring64/bsi64.go | 4 +- roaring64/bsi64_test.go | 58 +++++++++++++++++++ 4 files changed, 143 insertions(+), 2 deletions(-) diff --git a/BitSliceIndexing/bsi.go b/BitSliceIndexing/bsi.go index 9d56fc73..001eda4c 100644 --- a/BitSliceIndexing/bsi.go +++ b/BitSliceIndexing/bsi.go @@ -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) { diff --git a/BitSliceIndexing/bsi_edgecases_test.go b/BitSliceIndexing/bsi_edgecases_test.go index 5f920cff..d0f09943 100644 --- a/BitSliceIndexing/bsi_edgecases_test.go +++ b/BitSliceIndexing/bsi_edgecases_test.go @@ -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()) + } + } + } + } + } +} diff --git a/roaring64/bsi64.go b/roaring64/bsi64.go index a8eb0f73..be1fc5e7 100644 --- a/roaring64/bsi64.go +++ b/roaring64/bsi64.go @@ -766,7 +766,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) { diff --git a/roaring64/bsi64_test.go b/roaring64/bsi64_test.go index 35852c05..06dd6fed 100644 --- a/roaring64/bsi64_test.go +++ b/roaring64/bsi64_test.go @@ -6,6 +6,7 @@ import ( "encoding/binary" "fmt" "io" + "math" "math/big" "math/rand" "os" @@ -1106,3 +1107,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()) + } + } + } + } + } +}