From c6dc1090553b71092603c34ac6e8e84c2bd6192b Mon Sep 17 00:00:00 2001 From: Charles Masson Date: Tue, 18 Apr 2023 19:00:13 +0200 Subject: [PATCH] Fix merge after clear --- ddsketch/store/buffered_paginated.go | 3 +++ ddsketch/store/store_test.go | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/ddsketch/store/buffered_paginated.go b/ddsketch/store/buffered_paginated.go index 5fad7fb..11f4310 100644 --- a/ddsketch/store/buffered_paginated.go +++ b/ddsketch/store/buffered_paginated.go @@ -379,6 +379,9 @@ func (s *BufferedPaginatedStore) MergeWith(other Store) { if ok && s.pageLenLog2 == o.pageLenLog2 { // Merge pages. for oPageOffset, oPage := range o.pages { + if len(oPage) == 0 { + continue + } oPageIndex := o.minPageIndex + oPageOffset page := s.page(oPageIndex, true) for i, oCount := range oPage { diff --git a/ddsketch/store/store_test.go b/ddsketch/store/store_test.go index 7278100..6a6e1c8 100644 --- a/ddsketch/store/store_test.go +++ b/ddsketch/store/store_test.go @@ -300,6 +300,23 @@ func TestMergeFuzzy(t *testing.T) { } } +func TestMergeAfterClear(t *testing.T) { + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { + s1 := testCase.newStore() + s2 := testCase.newStore() + + for i := 0; i < 1000; i++ { + s2.Add(i) + } + s2.Clear() + + s1.MergeWith(s2) + s2.MergeWith(s1) + }) + } +} + func testStore(t *testing.T, store Store, normalizedBins []Bin) { assertEncodeBins(t, store, normalizedBins) testCopy(t, store, normalizedBins)