Skip to content

Commit

Permalink
Adding missing post aggregate for Quantiles double sketch. (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
saketbairoliya2 authored Oct 10, 2022
1 parent c9db3cc commit b2d2228
Show file tree
Hide file tree
Showing 8 changed files with 249 additions and 1 deletion.
6 changes: 6 additions & 0 deletions builder/postaggregation/post_aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ func Load(data []byte) (builder.PostAggregator, error) {
p = NewQuantilesDoublesSketchToQuantiles()
case "quantilesDoublesSketchToHistogram":
p = NewQuantilesDoublesSketchToHistogram()
case "quantilesDoublesSketchToRank":
p = NewQuantilesDoublesSketchToRank()
case "quantilesDoublesSketchToCDF":
p = NewQuantilesDoublesSketchToCDF()
case "quantilesDoublesSketchToString":
p = NewQuantilesDoublesSketchToString()
default:
return nil, errors.New("unsupported postaggregation type")
}
Expand Down
34 changes: 34 additions & 0 deletions builder/postaggregation/quantiles_doubles_sketch_to_cdf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package postaggregation

// QuantilesDoublesSketchToCDF struct based on
// PostAggregator section in https://druid.apache.org/docs/latest/development/extensions-core/datasketches-quantiles.html#cdf
type QuantilesDoublesSketchToCDF struct {
Base
Field *QuantilesDoublesSketchField `json:"field,omitempty"`
SplitPoints []float64 `json:"splitPoints,omitempty"`
}

// NewQuantilesDoublesSketchToCDF new instance of QuantilesDoublesSketchToCDF
func NewQuantilesDoublesSketchToCDF() *QuantilesDoublesSketchToCDF {
q := &QuantilesDoublesSketchToCDF{}
q.SetType("quantilesDoublesSketchToCDF")
return q
}

// SetName set name
func (q *QuantilesDoublesSketchToCDF) SetName(name string) *QuantilesDoublesSketchToCDF {
q.Base.SetName(name)
return q
}

// SetSplitPoints set splitPoints
func (q *QuantilesDoublesSketchToCDF) SetSplitPoints(splitPoints []float64) *QuantilesDoublesSketchToCDF {
q.SplitPoints = splitPoints
return q
}

// SetField set QuantilesDoublesSketchField
func (q *QuantilesDoublesSketchToCDF) SetField(field *QuantilesDoublesSketchField) *QuantilesDoublesSketchToCDF {
q.Field = field
return q
}
49 changes: 49 additions & 0 deletions builder/postaggregation/quantiles_doubles_sketch_to_cdf_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package postaggregation

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
)

func TestQuantilesDoublesSketchToCDF(t *testing.T) {
qf := NewQuantilesDoublesSketchField()
qf.SetType("fieldAccess").SetName("tp75tp90").SetFieldName("a1:agg")
quantilesDoublesSketchToCDF := NewQuantilesDoublesSketchToCDF()
quantilesDoublesSketchToCDF.SetName("tp75tp90").SetField(qf).SetSplitPoints([]float64{0.75, 0.90})

// "omitempty" will ignore boolean=false
quantilesDoublesSketchToCDFJSON := `
{
"type": "quantilesDoublesSketchToCDF",
"name": "tp75tp90",
"field": {
"type": "fieldAccess",
"name": "tp75tp90",
"fieldName": "a1:agg"
},
"splitPoints": [0.75, 0.9]
}
`

t.Run("build quantilesDoublesSketchToQuantiles",
func(t *testing.T) {
postAggJSON, err := json.Marshal(quantilesDoublesSketchToCDF)
assert.Nil(t,
err)
assert.JSONEq(t,
string(postAggJSON),
quantilesDoublesSketchToCDFJSON)
})

t.Run("load quantilesDoublesSketchToQuantiles",
func(t *testing.T) {
postAgg, err := Load([]byte(quantilesDoublesSketchToCDFJSON))
assert.Nil(t,
err)
assert.Equal(t,
quantilesDoublesSketchToCDF,
postAgg)
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type QuantilesDoublesSketchToQuantile struct {
Fraction *float64 `json:"fraction,omitempty"`
}

// NewQuantilesDoublesSketchToQuantile new instance of QuantilesDoublesSketchToHistogram
// NewQuantilesDoublesSketchToQuantile new instance of QuantilesDoublesSketchToQuantile
func NewQuantilesDoublesSketchToQuantile() *QuantilesDoublesSketchToQuantile {
q := &QuantilesDoublesSketchToQuantile{}
q.SetType("quantilesDoublesSketchToQuantile")
Expand Down
34 changes: 34 additions & 0 deletions builder/postaggregation/quantiles_doubles_sketch_to_rank.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package postaggregation

// QuantilesDoublesSketchToRank struct based on
// PostAggregator section in https://druid.apache.org/docs/latest/development/extensions-core/datasketches-quantiles.html#rank
type QuantilesDoublesSketchToRank struct {
Base
Field *QuantilesDoublesSketchField `json:"field,omitempty"`
Value *float64 `json:"value,omitempty"`
}

// NewQuantilesDoublesSketchToRank new instance of NewQuantilesDoublesSketchToRank
func NewQuantilesDoublesSketchToRank() *QuantilesDoublesSketchToRank {
q := &QuantilesDoublesSketchToRank{}
q.SetType("quantilesDoublesSketchToRank")
return q
}

// SetName set name
func (q *QuantilesDoublesSketchToRank) SetName(name string) *QuantilesDoublesSketchToRank {
q.Base.SetName(name)
return q
}

// SetValue set value
func (q *QuantilesDoublesSketchToRank) SetValue(value float64) *QuantilesDoublesSketchToRank {
q.Value = &value
return q
}

// SetField set QuantilesDoublesSketchField
func (q *QuantilesDoublesSketchToRank) SetField(field *QuantilesDoublesSketchField) *QuantilesDoublesSketchToRank {
q.Field = field
return q
}
50 changes: 50 additions & 0 deletions builder/postaggregation/quantiles_doubles_sketch_to_rank_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package postaggregation

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
)

func TestQuantilesDoublesSketchToRank(t *testing.T) {
qf := NewQuantilesDoublesSketchField()
qf.SetType("fieldAccess").SetName("tp90").SetFieldName("a1:agg")
quantilesDoublesSketchToRank := NewQuantilesDoublesSketchToRank()
quantilesDoublesSketchToRank.SetName("tp90").SetField(qf).SetValue(0.90)

// "omitempty" will ignore boolean=false
quantilesDoublesSketchToRankJSON := `
{
"type": "quantilesDoublesSketchToRank",
"name": "tp90",
"field": {
"type": "fieldAccess",
"name": "tp90",
"fieldName": "a1:agg"
},
"value": 0.9
}
`

t.Run("build quantilesDoublesSketchToRank",
func(t *testing.T) {
postAggJSON, err := json.Marshal(quantilesDoublesSketchToRank)
assert.Nil(t,
err)
assert.JSONEq(t,
string(postAggJSON),
quantilesDoublesSketchToRankJSON)
})

t.Run("load quantilesDoublesSketchToRank",
func(t *testing.T) {
postAgg, err := Load([]byte(quantilesDoublesSketchToRankJSON))
assert.Nil(t,
err)
assert.Equal(t,
quantilesDoublesSketchToRank,
postAgg)
})

}
27 changes: 27 additions & 0 deletions builder/postaggregation/quantiles_doubles_sketch_to_string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package postaggregation

// QuantilesDoublesSketchToString struct based on
// PostAggregator section in https://druid.apache.org/docs/latest/development/extensions-core/datasketches-quantiles.html#sketch-summary
type QuantilesDoublesSketchToString struct {
Base
Field *QuantilesDoublesSketchField `json:"field,omitempty"`
}

// NewQuantilesDoublesSketchToString new instance of QuantilesDoublesSketchToString
func NewQuantilesDoublesSketchToString() *QuantilesDoublesSketchToString {
q := &QuantilesDoublesSketchToString{}
q.SetType("quantilesDoublesSketchToString")
return q
}

// SetName set name
func (q *QuantilesDoublesSketchToString) SetName(name string) *QuantilesDoublesSketchToString {
q.Base.SetName(name)
return q
}

// SetField set QuantilesDoublesSketchField
func (q *QuantilesDoublesSketchToString) SetField(field *QuantilesDoublesSketchField) *QuantilesDoublesSketchToString {
q.Field = field
return q
}
48 changes: 48 additions & 0 deletions builder/postaggregation/quantiles_doubles_sketch_to_string_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package postaggregation

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
)

func TestQuantilesDoublesSketchToString(t *testing.T) {
qf := NewQuantilesDoublesSketchField()
qf.SetType("fieldAccess").SetName("tp75tp90").SetFieldName("a1:agg")
quantilesDoublesSketchToString := NewQuantilesDoublesSketchToString()
quantilesDoublesSketchToString.SetName("tp75tp90").SetField(qf)

// "omitempty" will ignore boolean=false
quantilesDoublesSketchToStringJSON := `
{
"type": "quantilesDoublesSketchToString",
"name": "tp75tp90",
"field": {
"type": "fieldAccess",
"name": "tp75tp90",
"fieldName": "a1:agg"
}
}
`

t.Run("build quantilesDoublesSketchToString",
func(t *testing.T) {
postAggJSON, err := json.Marshal(quantilesDoublesSketchToString)
assert.Nil(t,
err)
assert.JSONEq(t,
string(postAggJSON),
quantilesDoublesSketchToStringJSON)
})

t.Run("load quantilesDoublesSketchToString",
func(t *testing.T) {
postAgg, err := Load([]byte(quantilesDoublesSketchToStringJSON))
assert.Nil(t,
err)
assert.Equal(t,
quantilesDoublesSketchToString,
postAgg)
})
}

0 comments on commit b2d2228

Please sign in to comment.