-
Notifications
You must be signed in to change notification settings - Fork 0
/
str_to_date.go
executable file
·100 lines (86 loc) · 2.6 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
package function
import (
"fmt"
"github.com/Rock-liyi/p2pdb-store/sql"
"github.com/Rock-liyi/p2pdb-store/sql/parse/dateparse"
)
// 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 &StringToDatetime{
Date: args[0],
Format: args[1],
}, nil
}
// StringToDatetime defines the built-in function STR_TO_DATE(str, format)
type StringToDatetime struct {
Date sql.Expression
Format sql.Expression
}
var _ sql.FunctionExpression = (*StringToDatetime)(nil)
// Description implements sql.FunctionExpression
func (s StringToDatetime) Description() string {
return "parses the date/datetime/timestamp expression according to the format specifier."
}
// Resolved returns whether the node is resolved.
func (s StringToDatetime) Resolved() bool {
dateResolved := s.Date == nil || s.Date.Resolved()
formatResolved := s.Format == nil || s.Format.Resolved()
return dateResolved && formatResolved
}
func (s StringToDatetime) String() string {
return fmt.Sprintf("STR_TO_DATE(%s, %s)", s.Date, s.Format)
}
// Type returns the expression type.
func (s StringToDatetime) Type() sql.Type {
return sql.Datetime
}
// IsNullable returns whether the expression can be null.
func (s StringToDatetime) IsNullable() bool {
return true
}
// Eval evaluates the given row and returns a result.
func (s StringToDatetime) 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 {
return sql.Null, nil
}
return goTime, nil
}
// Children returns the children expressions of this expression.
func (s StringToDatetime) 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 StringToDatetime) WithChildren(children ...sql.Expression) (sql.Expression, error) {
return NewStrToDate(children...)
}
func (s StringToDatetime) FunctionName() string {
return "str_to_date"
}