forked from Accedian/godruid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpost_aggregations.go
147 lines (133 loc) · 4.01 KB
/
post_aggregations.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package godruid
import (
"encoding/json"
)
type PostAggregation struct {
Type string `json:"type"`
Name string `json:"name,omitempty"`
Value interface{} `json:"value,omitempty"`
Fn string `json:"fn,omitempty"`
Fields []PostAggregation `json:"fields,omitempty"`
FieldName string `json:"fieldName,omitempty"`
FieldNames []string `json:"fieldNames,omitempty"`
Function string `json:"function,omitempty"`
Fraction float64 `json:"fraction,omitempty"` // druid-datasketches extension - quantiles
Fractions []float64 `json:"fractions,omitempty"` // druid-datasketches extension - quantiles
Field *PostAggregation `json:"field,omitempty"` // druid-datasketches extension
SplitPoints []float64 `json:"splitPoints,omitempty"` // druid-datasketches extension - histogram
}
// The agg reference.
type AggRefer struct {
Name string
Refer string // The refer of Name, empty means Name has no refer.
}
// Return the aggregations or post aggregations which this post aggregation used.
// It could be helpful while automatically filling the aggregations or post aggregations base on this.
func (pa PostAggregation) GetReferAggs(parentName ...string) (refers []AggRefer) {
switch pa.Type {
case "arithmetic":
if len(parentName) != 0 {
refers = append(refers, AggRefer{parentName[0], pa.Name})
} else {
refers = append(refers, AggRefer{pa.Name, ""})
}
for _, spa := range pa.Fields {
refers = append(refers, spa.GetReferAggs(pa.Name)...)
}
case "fieldAccess":
refers = append(refers, AggRefer{parentName[0], pa.FieldName})
case "constant":
// no need refers.
case "javascript":
for _, f := range pa.FieldNames {
refers = append(refers, AggRefer{pa.Name, f})
}
case "hyperUniqueCardinality":
refers = append(refers, AggRefer{parentName[0], pa.FieldName})
}
return
}
func PostAggRawJson(rawJson string) PostAggregation {
pa := &PostAggregation{}
json.Unmarshal([]byte(rawJson), pa)
return *pa
}
func PostAggArithmetic(name, fn string, fields []PostAggregation) PostAggregation {
return PostAggregation{
Type: "arithmetic",
Name: name,
Fn: fn,
Fields: fields,
}
}
func PostAggFieldAccessor(fieldName string) PostAggregation {
return PostAggregation{
Type: "fieldAccess",
FieldName: fieldName,
}
}
func PostAggConstant(name string, value interface{}) PostAggregation {
return PostAggregation{
Type: "constant",
Name: name,
Value: value,
}
}
func PostAggJavaScript(name, function string, fieldNames []string) PostAggregation {
return PostAggregation{
Type: "javascript",
Name: name,
FieldNames: fieldNames,
Function: function,
}
}
func PostAggFieldHyperUnique(fieldName string) PostAggregation {
return PostAggregation{
Type: "hyperUniqueCardinality",
FieldName: fieldName,
}
}
// druid-stats extension
func ExtPostAggStdDev(name, fieldName string) PostAggregation {
return PostAggregation{
Type: "stddev",
Name: name,
FieldName: fieldName,
}
}
// druid-datasketchess extension
func ExtPostAggQuantile(name, fieldName string, fraction float64) PostAggregation {
return PostAggregation{
Type: "quantilesDoublesSketchToQuantile",
Name: name,
Field: &PostAggregation{
Type: "fieldAccess",
FieldName: fieldName,
},
Fraction: fraction,
}
}
// druid-datasketchess extension
func ExtPostAggQuantiles(name, fieldName string, fractions []float64) PostAggregation {
return PostAggregation{
Type: "quantilesDoublesSketchToQuantiles",
Name: name,
Field: &PostAggregation{
Type: "fieldAccess",
FieldName: fieldName,
},
Fractions: fractions,
}
}
// druid-datasketchess extension
func ExtPostAggHistogram(name, fieldName string, splitPoints []float64) PostAggregation {
return PostAggregation{
Type: "quantilesDoublesSketchToHistogram",
Name: name,
Field: &PostAggregation{
Type: "fieldAccess",
FieldName: fieldName,
},
SplitPoints: splitPoints,
}
}