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 pathlast.go
68 lines (54 loc) · 1.53 KB
/
last.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
package aggregation
import (
"fmt"
"github.com/src-d/go-mysql-server/sql"
"github.com/src-d/go-mysql-server/sql/expression"
)
// Last agregation returns the last of all values in the selected column.
// It implements the Aggregation interface.
type Last struct {
expression.UnaryExpression
}
// NewLast returns a new Last node.
func NewLast(e sql.Expression) *Last {
return &Last{expression.UnaryExpression{Child: e}}
}
// Type returns the resultant type of the aggregation.
func (l *Last) Type() sql.Type {
return l.Child.Type()
}
func (l *Last) String() string {
return fmt.Sprintf("LAST(%s)", l.Child)
}
// WithChildren implements the sql.Expression interface.
func (l *Last) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(l, len(children), 1)
}
return NewLast(children[0]), nil
}
// NewBuffer creates a new buffer to compute the result.
func (l *Last) NewBuffer() sql.Row {
return sql.NewRow(nil)
}
// Update implements the Aggregation interface.
func (l *Last) Update(ctx *sql.Context, buffer, row sql.Row) error {
v, err := l.Child.Eval(ctx, row)
if err != nil {
return err
}
if v == nil {
return nil
}
buffer[0] = v
return nil
}
// Merge implements the Aggregation interface.
func (l *Last) Merge(ctx *sql.Context, buffer, partial sql.Row) error {
buffer[0] = partial[0]
return nil
}
// Eval implements the Aggregation interface.
func (l *Last) Eval(ctx *sql.Context, buffer sql.Row) (interface{}, error) {
return buffer[0], nil
}