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 pathmax.go
89 lines (72 loc) · 1.91 KB
/
max.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
package aggregation
import (
"fmt"
"reflect"
"github.com/src-d/go-mysql-server/sql"
"github.com/src-d/go-mysql-server/sql/expression"
)
// Max agregation returns the greatest value of the selected column.
// It implements the Aggregation interface
type Max struct {
expression.UnaryExpression
}
// NewMax returns a new Max node.
func NewMax(e sql.Expression) *Max {
return &Max{expression.UnaryExpression{Child: e}}
}
// Resolved implements the Resolvable interface.
func (m *Max) Resolved() bool {
return m.Child.Resolved()
}
// Type returns the resultant type of the aggregation.
func (m *Max) Type() sql.Type {
return m.Child.Type()
}
func (m *Max) String() string {
return fmt.Sprintf("MAX(%s)", m.Child)
}
// IsNullable returns whether the return value can be null.
func (m *Max) IsNullable() bool {
return false
}
// WithChildren implements the Expression interface.
func (m *Max) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(m, len(children), 1)
}
return NewMax(children[0]), nil
}
// NewBuffer creates a new buffer to compute the result.
func (m *Max) NewBuffer() sql.Row {
return sql.NewRow(nil)
}
// Update implements the Aggregation interface.
func (m *Max) Update(ctx *sql.Context, buffer, row sql.Row) error {
v, err := m.Child.Eval(ctx, row)
if err != nil {
return err
}
if reflect.TypeOf(v) == nil {
return nil
}
if buffer[0] == nil {
buffer[0] = v
}
cmp, err := m.Child.Type().Compare(v, buffer[0])
if err != nil {
return err
}
if cmp == 1 {
buffer[0] = v
}
return nil
}
// Merge implements the Aggregation interface.
func (m *Max) Merge(ctx *sql.Context, buffer, partial sql.Row) error {
return m.Update(ctx, buffer, partial)
}
// Eval implements the Aggregation interface.
func (m *Max) Eval(ctx *sql.Context, buffer sql.Row) (interface{}, error) {
max := buffer[0]
return max, nil
}