Skip to content

Commit d79af2a

Browse files
committed
Remove zero count, fix some bugs and add index for quantile interface
1 parent acffd9b commit d79af2a

File tree

3 files changed

+31
-36
lines changed

3 files changed

+31
-36
lines changed

dataset/generator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func (s *Constant) Generate() float64 { return s.constant }
2424
// Linearly increasing stream
2525
type Linear struct{ currentVal float64 }
2626

27-
func NewLinear() *Linear { return &Linear{0} }
27+
func NewLinear() *Linear { return &Linear{1} }
2828

2929
func (g *Linear) Generate() float64 {
3030
value := g.currentVal

ddsketch/ddsketch.go

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ import (
1515

1616
type DDSketch struct {
1717
mapping.IndexMapping
18-
store store.Store
19-
zeroCount int32
18+
store store.Store
2019
}
2120

2221
func NewDDSketch(indexMapping mapping.IndexMapping, store store.Store) *DDSketch {
@@ -71,18 +70,14 @@ func (s *DDSketch) Add(value float64) error {
7170

7271
// Adds a value to the sketch with a int32 count.
7372
func (s *DDSketch) AddWithCount(value float64, count int32) error {
74-
if value < 0 || value > s.MaxIndexableValue() {
73+
if value < s.MinIndexableValue() || value > s.MaxIndexableValue() {
7574
return errors.New("input value is outside the range that is tracked by the sketch")
7675
}
7776
if count < 0 {
78-
return errors.New("The count cannot be negative.")
77+
return errors.New("count cannot be negative")
7978
}
8079

81-
if value > s.MinIndexableValue() {
82-
s.store.AddWithCount(s.Index(value), count)
83-
} else {
84-
s.zeroCount += count
85-
}
80+
s.store.AddWithCount(s.Index(value), count)
8681
return nil
8782
}
8883

@@ -97,21 +92,27 @@ func (s *DDSketch) Copy() *DDSketch {
9792
// Return the value at the specified quantile. Return a non-nil error if the quantile is invalid
9893
// or if the sketch is empty.
9994
func (s *DDSketch) GetValueAtQuantile(quantile float64) (float64, error) {
95+
key, err := s.GetIndexAtQuantile(quantile)
96+
if err != nil {
97+
return math.NaN(), err
98+
}
99+
return s.Value(key), nil
100+
}
101+
102+
// Return the index at the specified quantile. Return a non-nil error if the quantile is invalid
103+
// or if the sketch is empty.
104+
func (s *DDSketch) GetIndexAtQuantile(quantile float64) (int, error) {
100105
if quantile < 0 || quantile > 1 {
101-
return math.NaN(), errors.New("The quantile must be between 0 and 1.")
106+
return 0, errors.New("quantile must be between 0 and 1")
102107
}
103108

104109
count := s.GetCount()
105110
if count == 0 {
106-
return math.NaN(), errors.New("no such element exists")
111+
return 0, errors.New("no such element exists")
107112
}
108113

109114
rank := quantile * float64(count-1)
110-
if rank < float64(s.zeroCount) {
111-
return 0, nil
112-
} else {
113-
return s.Value(s.store.KeyAtRank(rank - float64(s.zeroCount))), nil
114-
}
115+
return s.store.KeyAtRank(rank), nil
115116
}
116117

117118
// Return the values at the respective specified quantiles. Return a non-nil error if any of the quantiles
@@ -130,47 +131,41 @@ func (s *DDSketch) GetValuesAtQuantiles(quantiles []float64) ([]float64, error)
130131

131132
// Return the total number of values that have been added to this sketch.
132133
func (s *DDSketch) GetCount() int32 {
133-
return s.zeroCount + s.store.TotalCount()
134+
return s.store.TotalCount()
134135
}
135136

136137
// Return true iff no value has been added to this sketch.
137138
func (s *DDSketch) IsEmpty() bool {
138-
return s.zeroCount == 0 && s.store.IsEmpty()
139+
return s.store.IsEmpty()
139140
}
140141

141142
// Return the maximum value that has been added to this sketch. Return a non-nil error if the sketch
142143
// is empty.
143144
func (s *DDSketch) GetMaxValue() (float64, error) {
144-
if !s.store.IsEmpty() {
145-
maxIndex, _ := s.store.MaxIndex()
146-
return s.Value(maxIndex), nil
147-
} else {
148-
return 0, nil
145+
maxIndex, err := s.store.MaxIndex()
146+
if err != nil {
147+
return math.NaN(), err
149148
}
149+
return s.Value(maxIndex), nil
150150
}
151151

152152
// Return the minimum value that has been added to this sketch. Returns a non-nil error if the sketch
153153
// is empty.
154154
func (s *DDSketch) GetMinValue() (float64, error) {
155-
if s.zeroCount > 0 {
156-
return 0, nil
157-
} else {
158-
minIndex, err := s.store.MinIndex()
159-
if err != nil {
160-
return math.NaN(), err
161-
}
162-
return s.Value(minIndex), nil
155+
minIndex, err := s.store.MinIndex()
156+
if err != nil {
157+
return math.NaN(), err
163158
}
159+
return s.Value(minIndex), nil
164160
}
165161

166162
// Merges the other sketch into this one. After this operation, this sketch encodes the values that
167163
// were added to both this and the other sketches.
168164
func (s *DDSketch) MergeWith(other *DDSketch) error {
169165
if !s.IndexMapping.Equals(other.IndexMapping) {
170-
return errors.New("Cannot merge sketches with different index mappings.")
166+
return errors.New("cannot merge sketches with different index mappings")
171167
}
172168
s.store.MergeWith(other.store)
173-
s.zeroCount += other.zeroCount
174169
return nil
175170
}
176171

ddsketch/store/dense_store.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,14 @@ func (s *DenseStore) TotalCount() int32 {
138138

139139
func (s *DenseStore) MinIndex() (int, error) {
140140
if s.IsEmpty() {
141-
return 0, errors.New("MinIndex of empty store is undefined.")
141+
return 0, errors.New("MinIndex of empty store is undefined")
142142
}
143143
return s.minIndex, nil
144144
}
145145

146146
func (s *DenseStore) MaxIndex() (int, error) {
147147
if s.IsEmpty() {
148-
return 0, errors.New("MaxIndex of empty store is undefined.")
148+
return 0, errors.New("MaxIndex of empty store is undefined")
149149
}
150150
return s.maxIndex, nil
151151
}

0 commit comments

Comments
 (0)