-
-
Notifications
You must be signed in to change notification settings - Fork 195
/
str_to_date.go
115 lines (96 loc) · 3.09 KB
/
str_to_date.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
package function
import (
"fmt"
"github.com/dolthub/go-mysql-server/sql/planbuilder/dateparse"
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/types"
)
// NewStrToDate constructs a new function expression from the given child expressions.
func NewStrToDate(args ...sql.Expression) (sql.Expression, error) {
if len(args) != 2 {
return nil, sql.ErrInvalidArgumentNumber.New("STR_TO_DATE", 2, len(args))
}
return &StrToDate{
Date: args[0],
Format: args[1],
}, nil
}
// StrToDate defines the built-in function STR_TO_DATE(str, format)
type StrToDate struct {
Date sql.Expression
Format sql.Expression
}
var _ sql.FunctionExpression = (*StrToDate)(nil)
var _ sql.CollationCoercible = (*StrToDate)(nil)
// Description implements sql.FunctionExpression
func (s StrToDate) Description() string {
return "parses the date/datetime/timestamp expression according to the format specifier."
}
// Resolved returns whether the node is resolved.
func (s StrToDate) Resolved() bool {
dateResolved := s.Date == nil || s.Date.Resolved()
formatResolved := s.Format == nil || s.Format.Resolved()
return dateResolved && formatResolved
}
func (s StrToDate) String() string {
return fmt.Sprintf("%s(%s,%s)", s.FunctionName(), s.Date, s.Format)
}
// Type returns the expression type.
func (s StrToDate) Type() sql.Type {
// TODO: precision
return types.Datetime
}
// CollationCoercibility implements the interface sql.CollationCoercible.
func (StrToDate) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
return sql.Collation_binary, 5
}
// IsNullable returns whether the expression can be null.
func (s StrToDate) IsNullable() bool {
return true
}
// Eval evaluates the given row and returns a result.
func (s StrToDate) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
date, err := s.Date.Eval(ctx, row)
if err != nil {
return nil, err
}
format, err := s.Format.Eval(ctx, row)
if err != nil {
return nil, err
}
dateStr, ok := date.(string)
if !ok {
// TODO: improve this error
return nil, sql.ErrInvalidType.New(fmt.Sprintf("%T", date))
}
formatStr, ok := format.(string)
if !ok {
// TODO: improve this error
return nil, sql.ErrInvalidType.New(fmt.Sprintf("%T", formatStr))
}
goTime, err := dateparse.ParseDateWithFormat(dateStr, formatStr)
if err != nil {
ctx.Warn(1411, fmt.Sprintf("Incorrect value: '%s' for function %s", dateStr, s.FunctionName()))
return nil, nil
}
// zero dates '0000-00-00' and '2010-00-13' are allowed,
// but depends on strict sql_mode with NO_ZERO_DATE or NO_ZERO_IN_DATE modes enabled.
return goTime, nil
}
// Children returns the children expressions of this expression.
func (s StrToDate) Children() []sql.Expression {
children := make([]sql.Expression, 0, 2)
if s.Date != nil {
children = append(children, s.Date)
}
if s.Format != nil {
children = append(children, s.Format)
}
return children
}
func (s StrToDate) WithChildren(children ...sql.Expression) (sql.Expression, error) {
return NewStrToDate(children...)
}
func (s StrToDate) FunctionName() string {
return "str_to_date"
}