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