From e250f09b5d34d6c936b18f3b7699df23a0555092 Mon Sep 17 00:00:00 2001 From: Ziqi Zhao Date: Fri, 10 Nov 2023 21:33:34 +0800 Subject: [PATCH] change origin schema in `ReduceResolution` method of histogram and float histogram (#13116) * change origin schema in ReduceResolution method of histogram and float histogram Signed-off-by: Ziqi Zhao --------- Signed-off-by: Ziqi Zhao --- model/histogram/float_histogram.go | 2 +- model/histogram/float_histogram_test.go | 43 +++++++++++++++++++++++++ model/histogram/histogram.go | 1 + model/histogram/histogram_test.go | 43 +++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 1 deletion(-) diff --git a/model/histogram/float_histogram.go b/model/histogram/float_histogram.go index 212b02880..e0f5d208e 100644 --- a/model/histogram/float_histogram.go +++ b/model/histogram/float_histogram.go @@ -1112,6 +1112,6 @@ func floatBucketsMatch(b1, b2 []float64) bool { func (h *FloatHistogram) ReduceResolution(targetSchema int32) *FloatHistogram { h.PositiveSpans, h.PositiveBuckets = reduceResolution(h.PositiveSpans, h.PositiveBuckets, h.Schema, targetSchema, false) h.NegativeSpans, h.NegativeBuckets = reduceResolution(h.NegativeSpans, h.NegativeBuckets, h.Schema, targetSchema, false) - + h.Schema = targetSchema return h } diff --git a/model/histogram/float_histogram_test.go b/model/histogram/float_histogram_test.go index 6f445c0cf..bfe3525fa 100644 --- a/model/histogram/float_histogram_test.go +++ b/model/histogram/float_histogram_test.go @@ -2442,3 +2442,46 @@ func createRandomSpans(rng *rand.Rand, spanNum int32) ([]Span, []float64) { } return Spans, Buckets } + +func TestFloatHistogramReduceResolution(t *testing.T) { + tcs := map[string]struct { + origin *FloatHistogram + target *FloatHistogram + }{ + "valid float histogram": { + origin: &FloatHistogram{ + Schema: 0, + PositiveSpans: []Span{ + {Offset: 0, Length: 4}, + {Offset: 0, Length: 0}, + {Offset: 3, Length: 2}, + }, + PositiveBuckets: []float64{1, 3, 1, 2, 1, 1}, + NegativeSpans: []Span{ + {Offset: 0, Length: 4}, + {Offset: 0, Length: 0}, + {Offset: 3, Length: 2}, + }, + NegativeBuckets: []float64{1, 3, 1, 2, 1, 1}, + }, + target: &FloatHistogram{ + Schema: -1, + PositiveSpans: []Span{ + {Offset: 0, Length: 3}, + {Offset: 1, Length: 1}, + }, + PositiveBuckets: []float64{1, 4, 2, 2}, + NegativeSpans: []Span{ + {Offset: 0, Length: 3}, + {Offset: 1, Length: 1}, + }, + NegativeBuckets: []float64{1, 4, 2, 2}, + }, + }, + } + + for _, tc := range tcs { + target := tc.origin.ReduceResolution(tc.target.Schema) + require.Equal(t, tc.target, target) + } +} diff --git a/model/histogram/histogram.go b/model/histogram/histogram.go index 4699bd3cb..3ebb27fbc 100644 --- a/model/histogram/histogram.go +++ b/model/histogram/histogram.go @@ -503,5 +503,6 @@ func (h *Histogram) ReduceResolution(targetSchema int32) *Histogram { h.NegativeSpans, h.NegativeBuckets = reduceResolution( h.NegativeSpans, h.NegativeBuckets, h.Schema, targetSchema, true, ) + h.Schema = targetSchema return h } diff --git a/model/histogram/histogram_test.go b/model/histogram/histogram_test.go index 6f12f53e8..5aa9ca6fe 100644 --- a/model/histogram/histogram_test.go +++ b/model/histogram/histogram_test.go @@ -967,3 +967,46 @@ func BenchmarkHistogramValidation(b *testing.B) { require.NoError(b, h.Validate()) } } + +func TestHistogramReduceResolution(t *testing.T) { + tcs := map[string]struct { + origin *Histogram + target *Histogram + }{ + "valid histogram": { + origin: &Histogram{ + Schema: 0, + PositiveSpans: []Span{ + {Offset: 0, Length: 4}, + {Offset: 0, Length: 0}, + {Offset: 3, Length: 2}, + }, + PositiveBuckets: []int64{1, 2, -2, 1, -1, 0}, + NegativeSpans: []Span{ + {Offset: 0, Length: 4}, + {Offset: 0, Length: 0}, + {Offset: 3, Length: 2}, + }, + NegativeBuckets: []int64{1, 2, -2, 1, -1, 0}, + }, + target: &Histogram{ + Schema: -1, + PositiveSpans: []Span{ + {Offset: 0, Length: 3}, + {Offset: 1, Length: 1}, + }, + PositiveBuckets: []int64{1, 3, -2, 0}, + NegativeSpans: []Span{ + {Offset: 0, Length: 3}, + {Offset: 1, Length: 1}, + }, + NegativeBuckets: []int64{1, 3, -2, 0}, + }, + }, + } + + for _, tc := range tcs { + target := tc.origin.ReduceResolution(tc.target.Schema) + require.Equal(t, tc.target, target) + } +}