-
-
Notifications
You must be signed in to change notification settings - Fork 191
/
case.go
276 lines (233 loc) · 5.92 KB
/
case.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright 2020-2021 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package expression
import (
"bytes"
"github.com/dolthub/go-mysql-server/sql"
)
// CaseBranch is a single branch of a case expression.
type CaseBranch struct {
Cond sql.Expression
Value sql.Expression
}
// Case is an expression that returns the value of one of its branches when a
// condition is met.
type Case struct {
Expr sql.Expression
Branches []CaseBranch
Else sql.Expression
}
// NewCase returns an new Case expression.
func NewCase(expr sql.Expression, branches []CaseBranch, elseExpr sql.Expression) *Case {
return &Case{expr, branches, elseExpr}
}
// From the description of operator typing here:
// https://dev.mysql.com/doc/refman/8.0/en/flow-control-functions.html#operator_case
func combinedCaseBranchType(left, right sql.Type) sql.Type {
if left == sql.Null {
return right
}
if right == sql.Null {
return left
}
if sql.IsTextOnly(left) && sql.IsTextOnly(right) {
return sql.LongText
}
if sql.IsTextBlob(left) && sql.IsTextBlob(right) {
return sql.LongBlob
}
if sql.IsTime(left) && sql.IsTime(right) {
if left == right {
return left
}
return sql.Datetime
}
if sql.IsNumber(left) && sql.IsNumber(right) {
if left == sql.Float64 || right == sql.Float64 {
return sql.Float64
}
if left == sql.Float32 || right == sql.Float32 {
return sql.Float32
}
if sql.IsDecimal(left) || sql.IsDecimal(right) {
return sql.MustCreateDecimalType(65, 10)
}
if left == sql.Uint64 && sql.IsSigned(right) ||
right == sql.Uint64 && sql.IsSigned(left) {
return sql.MustCreateDecimalType(65, 10)
}
if !sql.IsSigned(left) && !sql.IsSigned(right) {
return sql.Uint64
} else {
return sql.Int64
}
}
return sql.LongText
}
// Type implements the sql.Expression interface.
func (c *Case) Type() sql.Type {
curr := sql.Null
for _, b := range c.Branches {
curr = combinedCaseBranchType(curr, b.Value.Type())
}
if c.Else != nil {
curr = combinedCaseBranchType(curr, c.Else.Type())
}
return curr
}
// IsNullable implements the sql.Expression interface.
func (c *Case) IsNullable() bool {
for _, b := range c.Branches {
if b.Value.IsNullable() {
return true
}
}
return c.Else == nil || c.Else.IsNullable()
}
// Resolved implements the sql.Expression interface.
func (c *Case) Resolved() bool {
if (c.Expr != nil && !c.Expr.Resolved()) ||
(c.Else != nil && !c.Else.Resolved()) {
return false
}
for _, b := range c.Branches {
if !b.Cond.Resolved() || !b.Value.Resolved() {
return false
}
}
return true
}
// Children implements the sql.Expression interface.
func (c *Case) Children() []sql.Expression {
var children []sql.Expression
if c.Expr != nil {
children = append(children, c.Expr)
}
for _, b := range c.Branches {
children = append(children, b.Cond, b.Value)
}
if c.Else != nil {
children = append(children, c.Else)
}
return children
}
// Eval implements the sql.Expression interface.
func (c *Case) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
span, ctx := ctx.Span("expression.Case")
defer span.Finish()
var expr interface{}
var err error
if c.Expr != nil {
expr, err = c.Expr.Eval(ctx, row)
if err != nil {
return nil, err
}
}
t := c.Type()
for _, b := range c.Branches {
var cond sql.Expression
if expr != nil {
cond = NewEquals(NewLiteral(expr, c.Expr.Type()), b.Cond)
} else {
cond = b.Cond
}
res, err := sql.EvaluateCondition(ctx, cond, row)
if err != nil {
return nil, err
}
if sql.IsTrue(res) {
bval, err := b.Value.Eval(ctx, row)
if err != nil {
return nil, err
}
return t.Convert(bval)
}
}
if c.Else != nil {
val, err := c.Else.Eval(ctx, row)
if err != nil {
return nil, err
}
return t.Convert(val)
}
return nil, nil
}
// WithChildren implements the Expression interface.
func (c *Case) WithChildren(children ...sql.Expression) (sql.Expression, error) {
var expected = len(c.Branches) * 2
if c.Expr != nil {
expected++
}
if c.Else != nil {
expected++
}
if len(children) != expected {
return nil, sql.ErrInvalidChildrenNumber.New(c, len(children), expected)
}
var expr, elseExpr sql.Expression
if c.Expr != nil {
expr = children[0]
children = children[1:]
}
if c.Else != nil {
elseExpr = children[len(children)-1]
children = children[:len(children)-1]
}
var branches []CaseBranch
for i := 0; i < len(children); i += 2 {
branches = append(branches, CaseBranch{
Cond: children[i],
Value: children[i+1],
})
}
return NewCase(expr, branches, elseExpr), nil
}
func (c *Case) String() string {
var buf bytes.Buffer
buf.WriteString("CASE ")
if c.Expr != nil {
buf.WriteString(c.Expr.String())
}
for _, b := range c.Branches {
buf.WriteString(" WHEN ")
buf.WriteString(b.Cond.String())
buf.WriteString(" THEN ")
buf.WriteString(b.Value.String())
}
if c.Else != nil {
buf.WriteString(" ELSE ")
buf.WriteString(c.Else.String())
}
buf.WriteString(" END")
return buf.String()
}
func (c *Case) DebugString() string {
var buf bytes.Buffer
buf.WriteString("CASE ")
if c.Expr != nil {
buf.WriteString(sql.DebugString(c.Expr))
}
for _, b := range c.Branches {
buf.WriteString(" WHEN ")
buf.WriteString(sql.DebugString(b.Cond))
buf.WriteString(" THEN ")
buf.WriteString(sql.DebugString(b.Value))
}
if c.Else != nil {
buf.WriteString(" ELSE ")
buf.WriteString(sql.DebugString(c.Else))
}
buf.WriteString(" END")
return buf.String()
}