This repository was archived by the owner on Jan 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathcount.go
180 lines (146 loc) · 4.18 KB
/
count.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
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
package aggregation
import (
"fmt"
"github.com/mitchellh/hashstructure"
"github.com/src-d/go-mysql-server/sql"
"github.com/src-d/go-mysql-server/sql/expression"
)
// Count node to count how many rows are in the result set.
type Count struct {
expression.UnaryExpression
}
// NewCount creates a new Count node.
func NewCount(e sql.Expression) *Count {
return &Count{expression.UnaryExpression{Child: e}}
}
// NewBuffer creates a new buffer for the aggregation.
func (c *Count) NewBuffer() sql.Row {
return sql.NewRow(int64(0))
}
// Type returns the type of the result.
func (c *Count) Type() sql.Type {
return sql.Int64
}
// IsNullable returns whether the return value can be null.
func (c *Count) IsNullable() bool {
return false
}
// Resolved implements the Expression interface.
func (c *Count) Resolved() bool {
if _, ok := c.Child.(*expression.Star); ok {
return true
}
return c.Child.Resolved()
}
func (c *Count) String() string {
return fmt.Sprintf("COUNT(%s)", c.Child)
}
// WithChildren implements the Expression interface.
func (c *Count) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(c, len(children), 1)
}
return NewCount(children[0]), nil
}
// Update implements the Aggregation interface.
func (c *Count) Update(ctx *sql.Context, buffer, row sql.Row) error {
var inc bool
if _, ok := c.Child.(*expression.Star); ok {
inc = true
} else {
v, err := c.Child.Eval(ctx, row)
if v != nil {
inc = true
}
if err != nil {
return err
}
}
if inc {
buffer[0] = buffer[0].(int64) + int64(1)
}
return nil
}
// Merge implements the Aggregation interface.
func (c *Count) Merge(ctx *sql.Context, buffer, partial sql.Row) error {
buffer[0] = buffer[0].(int64) + partial[0].(int64)
return nil
}
// Eval implements the Aggregation interface.
func (c *Count) Eval(ctx *sql.Context, buffer sql.Row) (interface{}, error) {
count := buffer[0]
return count, nil
}
// CountDistinct node to count how many rows are in the result set.
type CountDistinct struct {
expression.UnaryExpression
}
// NewCountDistinct creates a new CountDistinct node.
func NewCountDistinct(e sql.Expression) *CountDistinct {
return &CountDistinct{expression.UnaryExpression{Child: e}}
}
// NewBuffer creates a new buffer for the aggregation.
func (c *CountDistinct) NewBuffer() sql.Row {
return sql.NewRow(make(map[uint64]struct{}))
}
// Type returns the type of the result.
func (c *CountDistinct) Type() sql.Type {
return sql.Int64
}
// IsNullable returns whether the return value can be null.
func (c *CountDistinct) IsNullable() bool {
return false
}
// Resolved implements the Expression interface.
func (c *CountDistinct) Resolved() bool {
if _, ok := c.Child.(*expression.Star); ok {
return true
}
return c.Child.Resolved()
}
func (c *CountDistinct) String() string {
return fmt.Sprintf("COUNT(DISTINCT %s)", c.Child)
}
// WithChildren implements the Expression interface.
func (c *CountDistinct) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(c, len(children), 1)
}
return NewCountDistinct(children[0]), nil
}
// Update implements the Aggregation interface.
func (c *CountDistinct) Update(ctx *sql.Context, buffer, row sql.Row) error {
seen := buffer[0].(map[uint64]struct{})
var value interface{}
if _, ok := c.Child.(*expression.Star); ok {
value = row
} else {
v, err := c.Child.Eval(ctx, row)
if v == nil {
return nil
}
if err != nil {
return err
}
value = v
}
hash, err := hashstructure.Hash(value, nil)
if err != nil {
return fmt.Errorf("count distinct unable to hash value: %s", err)
}
seen[hash] = struct{}{}
return nil
}
// Merge implements the Aggregation interface.
func (c *CountDistinct) Merge(ctx *sql.Context, buffer, partial sql.Row) error {
seen := buffer[0].(map[uint64]struct{})
for k := range partial[0].(map[uint64]struct{}) {
seen[k] = struct{}{}
}
return nil
}
// Eval implements the Aggregation interface.
func (c *CountDistinct) Eval(ctx *sql.Context, buffer sql.Row) (interface{}, error) {
seen := buffer[0].(map[uint64]struct{})
return int64(len(seen)), nil
}