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
148
|
package mapr
import (
"context"
"fmt"
"sort"
"strconv"
)
// GroupSet represents a map of aggregate sets. The group sets
// are requierd by the "group by" mapr clause, whereas the
// group set map keys are the values of the "group by" arguments.
// E.g. "group by $cid" would create one aggregate set and one map
// entry per customer id.
type GroupSet struct {
sets map[string]*AggregateSet
}
// Internal helper type
type result struct {
groupKey string
values []string
columnWidths []int
orderBy float64
}
// NewGroupSet returns a new empty group set.
func NewGroupSet() *GroupSet {
g := GroupSet{}
g.InitSet()
return &g
}
// String representation of the group set.
func (g *GroupSet) String() string {
return fmt.Sprintf("GroupSet(%v)", g.sets)
}
// InitSet makes the group set empty (initialize).
func (g *GroupSet) InitSet() {
g.sets = make(map[string]*AggregateSet)
}
// GetSet gets a specific aggregate set from the group set.
func (g *GroupSet) GetSet(groupKey string) *AggregateSet {
set, ok := g.sets[groupKey]
if !ok {
set = NewAggregateSet()
g.sets[groupKey] = set
}
return set
}
// Serialize the group set (e.g. to send it over the wire).
func (g *GroupSet) Serialize(ctx context.Context, ch chan<- string) {
for groupKey, set := range g.sets {
set.Serialize(ctx, groupKey, ch)
}
}
// Return a sorted result slice of the query from the group set.
func (g *GroupSet) result(query *Query, gathercolumnWidths bool) ([]result, []int, error) {
var err error
var rows []result
// Helpers for calculating the ASCII table output (output is the terminal and
// not a CSV file).
columnWidths := make([]int, len(query.Select))
var valueStrLen int
for groupKey, set := range g.sets {
result := result{groupKey: groupKey}
for i, sc := range query.Select {
if valueStrLen, err = g.resultSelect(query, &sc, set, &result); err != nil {
return rows, columnWidths, err
}
// Do we want to gather the table withs? This is required to print out a decent
// ASCII formated table (table output is the terminal and not a CSV file).
if !gathercolumnWidths {
continue
}
if columnWidths[i] < len(sc.FieldStorage) {
columnWidths[i] = len(sc.FieldStorage)
}
if columnWidths[i] < valueStrLen {
columnWidths[i] = valueStrLen
}
}
rows = append(rows, result)
}
g.resultOrderBy(query, rows)
return rows, columnWidths, nil
}
func (*GroupSet) resultSelect(query *Query, sc *selectCondition, set *AggregateSet,
result *result) (int, error) {
var valueStr string
var value float64
switch sc.Operation {
case Count:
value = set.FValues[sc.FieldStorage]
valueStr = fmt.Sprintf("%d", int(value))
case Len:
fallthrough
case Sum:
fallthrough
case Min:
fallthrough
case Max:
value = set.FValues[sc.FieldStorage]
valueStr = fmt.Sprintf("%f", value)
case Last:
valueStr = set.SValues[sc.FieldStorage]
value, _ = strconv.ParseFloat(valueStr, 64)
case Avg:
value = set.FValues[sc.FieldStorage] / float64(set.Samples)
valueStr = fmt.Sprintf("%f", value)
default:
return 0, fmt.Errorf("Unknown aggregation method '%v'", sc.Operation)
}
if sc.FieldStorage == query.OrderBy {
result.orderBy = value
}
result.values = append(result.values, valueStr)
return len(valueStr), nil
}
func (*GroupSet) resultOrderBy(query *Query, rows []result) {
if query.OrderBy == "" {
return
}
if query.ReverseOrder {
sort.SliceStable(rows, func(i, j int) bool {
return rows[i].orderBy < rows[j].orderBy
})
} else {
sort.SliceStable(rows, func(i, j int) bool {
return rows[i].orderBy > rows[j].orderBy
})
}
}
|