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 pathifnull.go
74 lines (64 loc) · 1.58 KB
/
ifnull.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
package function
import (
"fmt"
"github.com/src-d/go-mysql-server/sql"
"github.com/src-d/go-mysql-server/sql/expression"
)
// IfNull function returns the specified value IF the expression is NULL, otherwise return the expression.
type IfNull struct {
expression.BinaryExpression
}
// NewIfNull returns a new IFNULL UDF
func NewIfNull(ex, value sql.Expression) sql.Expression {
return &IfNull{
expression.BinaryExpression{
Left: ex,
Right: value,
},
}
}
// Eval implements the Expression interface.
func (f *IfNull) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
left, err := f.Left.Eval(ctx, row)
if err != nil {
return nil, err
}
if left != nil {
return left, nil
}
right, err := f.Right.Eval(ctx, row)
if err != nil {
return nil, err
}
return right, nil
}
// Type implements the Expression interface.
func (f *IfNull) Type() sql.Type {
if sql.IsNull(f.Left) {
if sql.IsNull(f.Right) {
return sql.Null
}
return f.Right.Type()
}
return f.Left.Type()
}
// IsNullable implements the Expression interface.
func (f *IfNull) IsNullable() bool {
if sql.IsNull(f.Left) {
if sql.IsNull(f.Right) {
return true
}
return f.Right.IsNullable()
}
return f.Left.IsNullable()
}
func (f *IfNull) String() string {
return fmt.Sprintf("ifnull(%s, %s)", f.Left, f.Right)
}
// WithChildren implements the Expression interface.
func (f *IfNull) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 2 {
return nil, sql.ErrInvalidChildrenNumber.New(f, len(children), 2)
}
return NewIfNull(children[0], children[1]), nil
}