-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaggregate.go
More file actions
185 lines (163 loc) · 3.91 KB
/
aggregate.go
File metadata and controls
185 lines (163 loc) · 3.91 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package duckql
import (
"reflect"
)
type AggregateFunctionColumn struct {
UnderlyingColumn string
ResultPosition int
Function AggregateFunction
}
func (a *AggregateFunctionColumn) Call(rows ResultRows) ResultRows {
return a.Function(a, rows)
}
type AggregateFunction func(*AggregateFunctionColumn, ResultRows) ResultRows
var functionMap = map[string]AggregateFunction{
"avg": averageOfColumn,
"count": countRows,
"max": maxOfColumn,
"min": minOfColumn,
"sum": sumOfColumn,
"total": sumOfColumn,
}
func averageOfColumn(c *AggregateFunctionColumn, rows ResultRows) ResultRows {
var sum float64
for _, row := range rows {
for _, column := range row {
if column.Name == c.UnderlyingColumn {
switch column.Value.Kind() {
case reflect.Float32, reflect.Float64:
sum += column.Value.Float()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
sum += float64(column.Value.Int())
}
goto nextRow
}
}
nextRow:
}
resultRow := rows[len(rows)-1]
resultRow[c.ResultPosition] = ResultValue{
Name: "average",
Value: reflect.ValueOf(sum / float64(len(rows))),
}
return ResultRows{
resultRow,
}
}
func countRows(c *AggregateFunctionColumn, rows ResultRows) ResultRows {
resultRow := rows[len(rows)-1]
resultRow[c.ResultPosition] = ResultValue{
Name: "count",
Value: reflect.ValueOf(len(rows)),
}
return ResultRows{
resultRow,
}
}
func maxOfColumn(c *AggregateFunctionColumn, rows ResultRows) ResultRows {
var max reflect.Value
var maxRow ResultRow
for _, row := range rows {
for _, column := range row {
if column.Name == c.UnderlyingColumn {
if !max.IsValid() {
max = column.Value
maxRow = row
goto nextRow
} else {
switch column.Value.Kind() {
case reflect.Float32, reflect.Float64:
if column.Value.Float() > max.Float() {
max = column.Value
maxRow = row
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if column.Value.Int() > max.Int() {
max = column.Value
maxRow = row
}
case reflect.String:
if column.Value.String() > max.String() {
max = column.Value
maxRow = row
}
}
goto nextRow
}
}
}
nextRow:
}
return ResultRows{
maxRow,
}
}
func minOfColumn(c *AggregateFunctionColumn, rows ResultRows) ResultRows {
var min reflect.Value
var minRow ResultRow
for _, row := range rows {
for _, column := range row {
if column.Name == c.UnderlyingColumn {
if !min.IsValid() {
min = column.Value
minRow = row
goto nextRow
} else {
switch column.Value.Kind() {
case reflect.Float32, reflect.Float64:
if column.Value.Float() < min.Float() {
min = column.Value
minRow = row
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if column.Value.Int() < min.Int() {
min = column.Value
minRow = row
}
case reflect.String:
if column.Value.String() < min.String() {
min = column.Value
minRow = row
}
}
goto nextRow
}
}
}
nextRow:
}
return ResultRows{
minRow,
}
}
func sumOfColumn(c *AggregateFunctionColumn, rows ResultRows) ResultRows {
var sum reflect.Value
var sumRow ResultRow
for _, row := range rows {
for _, column := range row {
if column.Name == c.UnderlyingColumn {
if !sum.IsValid() {
sum = column.Value
goto nextRow
} else {
switch column.Value.Kind() {
case reflect.Float32, reflect.Float64:
sum = reflect.ValueOf(sum.Float() + column.Value.Float())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
sum = reflect.ValueOf(sum.Int() + column.Value.Int())
}
goto nextRow
}
}
}
nextRow:
}
sumRow = rows[len(rows)-1]
sumRow[c.ResultPosition] = ResultValue{
Name: "sum",
Value: sum,
}
return ResultRows{
sumRow,
}
}