-
Notifications
You must be signed in to change notification settings - Fork 43
/
group_by_range.go
101 lines (80 loc) · 2.09 KB
/
group_by_range.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package search
import (
"github.com/aliyun/aliyun-tablestore-go-sdk/v5/tablestore/otsprotocol"
"github.com/golang/protobuf/proto"
)
type GroupByRange struct {
AggName string
Field string
RangeList []Range
SubAggList []Aggregation
SubGroupByList []GroupBy
}
func (g *GroupByRange) GetName() string {
return g.AggName
}
func (g *GroupByRange) GetType() GroupByType {
return GroupByRangeType
}
func (g *GroupByRange) Serialize() ([]byte, error) {
pbGroupBy := &otsprotocol.GroupByRange{}
//field_name
pbGroupBy.FieldName = &g.Field
//ranges
pbRanges, err := BuildPBForRanges(g.RangeList)
if err != nil {
return nil, err
}
pbGroupBy.Ranges = pbRanges
//sub aggs
pbAggregations, err := BuildPBForAggregations(g.SubAggList)
if err != nil {
return nil, err
}
if pbAggregations != nil {
pbGroupBy.SubAggs = pbAggregations
}
//sub group bys
pbGroupBys, err := BuildPBForGroupBys(g.SubGroupByList)
if err != nil {
return nil, err
}
if pbGroupBys != nil {
pbGroupBy.SubGroupBys = pbGroupBys
}
data, err := proto.Marshal(pbGroupBy)
return data, err
}
func (g *GroupByRange) ProtoBuffer() (*otsprotocol.GroupBy, error) {
return BuildPBForGroupBy(g)
}
func (g *GroupByRange) SubAggregations(subAggregations ...Aggregation) *GroupByRange {
g.SubAggList = subAggregations
return g
}
func (g *GroupByRange) SubAggregation(subAggregation Aggregation) *GroupByRange {
g.SubAggList = append(g.SubAggList, subAggregation)
return g
}
func (g *GroupByRange) SubGroupBys(subGroupBys ...GroupBy) *GroupByRange {
g.SubGroupByList = subGroupBys
return g
}
func (g *GroupByRange) SubGroupBy(subGroupBy GroupBy) *GroupByRange {
g.SubGroupByList = append(g.SubGroupByList, subGroupBy)
return g
}
func (g *GroupByRange) Name(name string) *GroupByRange {
g.AggName = name
return g
}
func (g *GroupByRange) FieldName(fieldName string) *GroupByRange {
g.Field = fieldName
return g
}
//append a Range[from, to)
func (g *GroupByRange) Range(fromInclusive float64, toExclusive float64) *GroupByRange {
g.RangeList = append(g.RangeList,
Range { from: fromInclusive, to: toExclusive})
return g
}