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 pathsubquery.go
125 lines (101 loc) · 2.53 KB
/
subquery.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
package expression
import (
"fmt"
"github.com/src-d/go-mysql-server/sql"
errors "gopkg.in/src-d/go-errors.v1"
)
var errExpectedSingleRow = errors.NewKind("the subquery returned more than 1 row")
// Subquery that is executed as an expression.
type Subquery struct {
Query sql.Node
value interface{}
}
// NewSubquery returns a new subquery node.
func NewSubquery(node sql.Node) *Subquery {
return &Subquery{node, nil}
}
// Eval implements the Expression interface.
func (s *Subquery) Eval(ctx *sql.Context, _ sql.Row) (interface{}, error) {
if s.value != nil {
if elems, ok := s.value.([]interface{}); ok {
if len(elems) > 1 {
return nil, errExpectedSingleRow.New()
}
return elems[0], nil
}
return s.value, nil
}
iter, err := s.Query.RowIter(ctx)
if err != nil {
return nil, err
}
rows, err := sql.RowIterToRows(iter)
if err != nil {
return nil, err
}
if len(rows) == 0 {
s.value = nil
return nil, nil
}
if len(rows) > 1 {
return nil, errExpectedSingleRow.New()
}
s.value = rows[0][0]
return s.value, nil
}
// EvalMultiple returns all rows returned by a subquery.
func (s *Subquery) EvalMultiple(ctx *sql.Context) ([]interface{}, error) {
if s.value != nil {
return s.value.([]interface{}), nil
}
iter, err := s.Query.RowIter(ctx)
if err != nil {
return nil, err
}
rows, err := sql.RowIterToRows(iter)
if err != nil {
return nil, err
}
if len(rows) == 0 {
s.value = []interface{}{}
return nil, nil
}
var result = make([]interface{}, len(rows))
for i, row := range rows {
result[i] = row[0]
}
s.value = result
return result, nil
}
// IsNullable implements the Expression interface.
func (s *Subquery) IsNullable() bool {
return s.Query.Schema()[0].Nullable
}
func (s *Subquery) String() string {
return fmt.Sprintf("(%s)", s.Query)
}
// Resolved implements the Expression interface.
func (s *Subquery) Resolved() bool {
return s.Query.Resolved()
}
// Type implements the Expression interface.
func (s *Subquery) Type() sql.Type {
return s.Query.Schema()[0].Type
}
// WithChildren implements the Expression interface.
func (s *Subquery) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 0 {
return nil, sql.ErrInvalidChildrenNumber.New(s, len(children), 0)
}
return s, nil
}
// Children implements the Expression interface.
func (s *Subquery) Children() []sql.Expression {
return nil
}
// WithQuery returns the subquery with the query node changed.
func (s *Subquery) WithQuery(node sql.Node) *Subquery {
ns := *s
ns.Query = node
return &ns
}