forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtin_control.go
76 lines (61 loc) · 1.93 KB
/
builtin_control.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
// Copyright 2015 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 expressions
import "github.com/pingcap/tidb/util/types"
// See https://dev.mysql.com/doc/refman/5.7/en/control-flow-functions.html#function_if
func builtinIf(args []interface{}, m map[interface{}]interface{}) (interface{}, error) {
// if(expr1, expr2, expr3)
// if expr1 is true, return expr2, otherwise, return expr3
v1 := args[0]
v2 := args[1]
v3 := args[2]
if v1 == nil {
return v3, nil
}
b, err := types.ToBool(v1)
if err != nil {
return nil, err
}
// TODO: check return type, must be numeric or string
if b == 1 {
return v2, nil
}
return v3, nil
}
// See https://dev.mysql.com/doc/refman/5.7/en/control-flow-functions.html#function_ifnull
func builtinIfNull(args []interface{}, m map[interface{}]interface{}) (interface{}, error) {
// ifnull(expr1, expr2)
// if expr1 is not null, return expr1, otherwise, return expr2
v1 := args[0]
v2 := args[1]
if v1 != nil {
return v1, nil
}
return v2, nil
}
// See https://dev.mysql.com/doc/refman/5.7/en/control-flow-functions.html#function_nullif
func builtinNullIf(args []interface{}, m map[interface{}]interface{}) (interface{}, error) {
// nullif(expr1, expr2)
// returns null if expr1 = expr2 is true, otherwise returns expr1
v1 := args[0]
v2 := args[1]
if v1 == nil || v2 == nil {
return v1, nil
}
// coerce for later eval compare
x, y := types.Coerce(v1, v2)
if n, err := evalCompare(x, y); err != nil || n == 0 {
return nil, err
}
return v1, nil
}