-
Notifications
You must be signed in to change notification settings - Fork 291
/
builtin_compare.go
287 lines (248 loc) · 6.95 KB
/
builtin_compare.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
277
278
279
280
281
282
283
284
285
286
287
// Copyright 2017 PingCAP, 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package expression
import (
"sort"
"github.com/juju/errors"
"github.com/chrislusf/gleam/sql/context"
"github.com/chrislusf/gleam/sql/parser/opcode"
"github.com/chrislusf/gleam/sql/util/types"
)
var (
_ functionClass = &coalesceFunctionClass{}
_ functionClass = &greatestFunctionClass{}
_ functionClass = &leastFunctionClass{}
_ functionClass = &intervalFunctionClass{}
_ functionClass = &compareFunctionClass{}
)
var (
_ builtinFunc = &builtinCoalesceSig{}
_ builtinFunc = &builtinGreatestSig{}
_ builtinFunc = &builtinLeastSig{}
_ builtinFunc = &builtinIntervalSig{}
_ builtinFunc = &builtinCompareSig{}
)
type coalesceFunctionClass struct {
baseFunctionClass
}
func (c *coalesceFunctionClass) getFunction(args []Expression, ctx context.Context) (builtinFunc, error) {
return &builtinCoalesceSig{newBaseBuiltinFunc(args, ctx)}, errors.Trace(c.verifyArgs(args))
}
type builtinCoalesceSig struct {
baseBuiltinFunc
}
func (b *builtinCoalesceSig) eval(row []types.Datum) (types.Datum, error) {
args, err := b.evalArgs(row)
if err != nil {
return types.Datum{}, errors.Trace(err)
}
return builtinCoalesce(args, b.ctx)
}
// See http://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#function_coalesce
func builtinCoalesce(args []types.Datum, ctx context.Context) (d types.Datum, err error) {
for _, d = range args {
if !d.IsNull() {
return d, nil
}
}
return d, nil
}
type greatestFunctionClass struct {
baseFunctionClass
}
func (c *greatestFunctionClass) getFunction(args []Expression, ctx context.Context) (builtinFunc, error) {
return &builtinGreatestSig{newBaseBuiltinFunc(args, ctx)}, errors.Trace(c.verifyArgs(args))
}
type builtinGreatestSig struct {
baseBuiltinFunc
}
func (b *builtinGreatestSig) eval(row []types.Datum) (types.Datum, error) {
args, err := b.evalArgs(row)
if err != nil {
return types.Datum{}, errors.Trace(err)
}
return builtinGreatest(args, b.ctx)
}
// See http://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#function_greatest
func builtinGreatest(args []types.Datum, ctx context.Context) (d types.Datum, err error) {
if args[0].IsNull() {
return
}
max := 0
sc := ctx.GetSessionVars().StmtCtx
for i := 1; i < len(args); i++ {
if args[i].IsNull() {
return
}
var cmp int
if cmp, err = args[i].CompareDatum(sc, args[max]); err != nil {
return
}
if cmp > 0 {
max = i
}
}
d = args[max]
return
}
type leastFunctionClass struct {
baseFunctionClass
}
func (c *leastFunctionClass) getFunction(args []Expression, ctx context.Context) (builtinFunc, error) {
return &builtinLeastSig{newBaseBuiltinFunc(args, ctx)}, errors.Trace(c.verifyArgs(args))
}
type builtinLeastSig struct {
baseBuiltinFunc
}
func (b *builtinLeastSig) eval(row []types.Datum) (types.Datum, error) {
args, err := b.evalArgs(row)
if err != nil {
return types.Datum{}, errors.Trace(err)
}
return builtinLeast(args, b.ctx)
}
// See http://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#function_least
func builtinLeast(args []types.Datum, ctx context.Context) (d types.Datum, err error) {
if args[0].IsNull() {
return
}
min := 0
sc := ctx.GetSessionVars().StmtCtx
for i := 1; i < len(args); i++ {
if args[i].IsNull() {
return
}
var cmp int
if cmp, err = args[i].CompareDatum(sc, args[min]); err != nil {
return
}
if cmp < 0 {
min = i
}
}
d = args[min]
return
}
type intervalFunctionClass struct {
baseFunctionClass
}
func (c *intervalFunctionClass) getFunction(args []Expression, ctx context.Context) (builtinFunc, error) {
return &builtinIntervalSig{newBaseBuiltinFunc(args, ctx)}, errors.Trace(c.verifyArgs(args))
}
type builtinIntervalSig struct {
baseBuiltinFunc
}
func (b *builtinIntervalSig) eval(row []types.Datum) (types.Datum, error) {
args, err := b.evalArgs(row)
if err != nil {
return types.Datum{}, errors.Trace(err)
}
return builtinInterval(args, b.ctx)
}
// See http://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#function_interval
func builtinInterval(args []types.Datum, ctx context.Context) (d types.Datum, err error) {
if args[0].IsNull() {
d.SetInt64(int64(-1))
return
}
sc := ctx.GetSessionVars().StmtCtx
idx := sort.Search(len(args)-1, func(i int) bool {
d1, d2 := args[0], args[i+1]
if d1.Kind() == types.KindInt64 && d1.Kind() == d2.Kind() {
return d1.GetInt64() < d2.GetInt64()
}
if d1.Kind() == types.KindUint64 && d1.Kind() == d2.Kind() {
return d1.GetUint64() < d2.GetUint64()
}
if d1.Kind() == types.KindInt64 && d2.Kind() == types.KindUint64 {
return d1.GetInt64() < 0 || d1.GetUint64() < d2.GetUint64()
}
if d1.Kind() == types.KindUint64 && d2.Kind() == types.KindInt64 {
return d2.GetInt64() > 0 && d1.GetUint64() < d2.GetUint64()
}
v1, _ := d1.ToFloat64(sc)
v2, _ := d2.ToFloat64(sc)
return v1 < v2
})
d.SetInt64(int64(idx))
return
}
type compareFunctionClass struct {
baseFunctionClass
op opcode.Op
}
func (c *compareFunctionClass) getFunction(args []Expression, ctx context.Context) (builtinFunc, error) {
return &builtinCompareSig{newBaseBuiltinFunc(args, ctx), c.op}, errors.Trace(c.verifyArgs(args))
}
type builtinCompareSig struct {
baseBuiltinFunc
op opcode.Op
}
func (b *builtinCompareSig) eval(row []types.Datum) (types.Datum, error) {
args, err := b.evalArgs(row)
if err != nil {
return types.Datum{}, errors.Trace(err)
}
return compareFuncFactory(b.op)(args, b.ctx)
}
func compareFuncFactory(op opcode.Op) BuiltinFunc {
return func(args []types.Datum, ctx context.Context) (d types.Datum, err error) {
sc := ctx.GetSessionVars().StmtCtx
var a, b = args[0], args[1]
if op != opcode.NullEQ {
a, b, err = types.CoerceDatum(sc, a, b)
if err != nil {
return d, errors.Trace(err)
}
}
if a.IsNull() || b.IsNull() {
// for <=>, if a and b are both nil, return true.
// if a or b is nil, return false.
if op == opcode.NullEQ {
if a.IsNull() && b.IsNull() {
d.SetInt64(oneI64)
} else {
d.SetInt64(zeroI64)
}
}
return
}
n, err := a.CompareDatum(sc, b)
if err != nil {
return d, errors.Trace(err)
}
var result bool
switch op {
case opcode.LT:
result = n < 0
case opcode.LE:
result = n <= 0
case opcode.EQ, opcode.NullEQ:
result = n == 0
case opcode.GT:
result = n > 0
case opcode.GE:
result = n >= 0
case opcode.NE:
result = n != 0
default:
return d, errInvalidOperation.Gen("invalid op %v in comparison operation", op)
}
if result {
d.SetInt64(oneI64)
} else {
d.SetInt64(zeroI64)
}
return
}
}