Skip to content

Commit

Permalink
evalEngine: Implement TIME_TO_SEC (#15094)
Browse files Browse the repository at this point in the history
Signed-off-by: Noble Mittal <[email protected]>
  • Loading branch information
beingnoble03 authored Feb 5, 2024
1 parent e9c513e commit 3c60baa
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 0 deletions.
4 changes: 4 additions & 0 deletions go/mysql/datetime/datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,10 @@ func (t Time) toDuration() time.Duration {
return dur
}

func (t Time) ToSeconds() int64 {
return int64(t.ToDuration().Seconds())
}

func (d Date) ToStdTime(loc *time.Location) (out time.Time) {
return time.Date(d.Year(), time.Month(d.Month()), d.Day(), 0, 0, 0, 0, loc)
}
Expand Down
12 changes: 12 additions & 0 deletions go/vt/vtgate/evalengine/cached_size.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions go/vt/vtgate/evalengine/compiler_asm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3818,6 +3818,19 @@ func (asm *assembler) Fn_FROM_DAYS() {
}, "FN FROM_DAYS INT64(SP-1)")
}

func (asm *assembler) Fn_TIME_TO_SEC() {
asm.emit(func(env *ExpressionEnv) int {
if env.vm.stack[env.vm.sp-1] == nil {
return 1
}
d := env.vm.stack[env.vm.sp-1].(*evalTemporal)

sec := d.dt.Time.ToSeconds()
env.vm.stack[env.vm.sp-1] = env.vm.arena.newEvalInt64(sec)
return 1
}, "FN TIME_TO_SEC TIME(SP-1)")
}

func (asm *assembler) Fn_QUARTER() {
asm.emit(func(env *ExpressionEnv) int {
if env.vm.stack[env.vm.sp-1] == nil {
Expand Down
42 changes: 42 additions & 0 deletions go/vt/vtgate/evalengine/fn_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ type (
CallExpr
}

builtinTimeToSec struct {
CallExpr
}

builtinQuarter struct {
CallExpr
}
Expand Down Expand Up @@ -183,6 +187,7 @@ var _ IR = (*builtinMonthName)(nil)
var _ IR = (*builtinLastDay)(nil)
var _ IR = (*builtinToDays)(nil)
var _ IR = (*builtinFromDays)(nil)
var _ IR = (*builtinTimeToSec)(nil)
var _ IR = (*builtinQuarter)(nil)
var _ IR = (*builtinSecond)(nil)
var _ IR = (*builtinTime)(nil)
Expand Down Expand Up @@ -1327,6 +1332,43 @@ func (call *builtinFromDays) compile(c *compiler) (ctype, error) {
return ctype{Type: sqltypes.Date, Flag: arg.Flag | flagNullable}, nil
}

func (b *builtinTimeToSec) eval(env *ExpressionEnv) (eval, error) {
arg, err := b.arg1(env)
if arg == nil {
return nil, nil
}
if err != nil {
return nil, err
}

d := evalToTime(arg, -1)
if d == nil {
return nil, nil
}

sec := d.dt.Time.ToSeconds()
return newEvalInt64(sec), nil
}

func (call *builtinTimeToSec) compile(c *compiler) (ctype, error) {
arg, err := call.Arguments[0].compile(c)
if err != nil {
return ctype{}, err
}

skip := c.compileNullCheck1(arg)

switch arg.Type {
case sqltypes.Date, sqltypes.Datetime, sqltypes.Time:
default:
c.asm.Convert_xT(1, -1)
}

c.asm.Fn_TIME_TO_SEC()
c.asm.jumpDestination(skip)
return ctype{Type: sqltypes.Int64, Col: collationNumeric, Flag: arg.Flag | flagNullable}, nil
}

func (b *builtinQuarter) eval(env *ExpressionEnv) (eval, error) {
date, err := b.arg1(env)
if err != nil {
Expand Down
25 changes: 25 additions & 0 deletions go/vt/vtgate/evalengine/testcases/cases.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ var Cases = []TestCase{
{Run: FnLastDay},
{Run: FnToDays},
{Run: FnFromDays},
{Run: FnTimeToSec},
{Run: FnQuarter},
{Run: FnSecond},
{Run: FnTime},
Expand Down Expand Up @@ -1816,6 +1817,30 @@ func FnFromDays(yield Query) {
}
}

func FnTimeToSec(yield Query) {
for _, d := range inputConversions {
yield(fmt.Sprintf("TIME_TO_SEC(%s)", d), nil)
}

time := []string{
`0`,
`'00:00:00'`,
`'22:23:00'`,
`'00:39:38'`,
`TIME'00:39:38'`,
`TIME'102:39:38'`,
`TIME'838:59:59'`,
`TIME'-838:59:59'`,
`'000220`,
`'2003-09-03 00:39:38'`,
`'2003-09-03'`,
}

for _, t := range time {
yield(fmt.Sprintf("TIME_TO_SEC(%s)", t), nil)
}
}

func FnQuarter(yield Query) {
for _, d := range inputConversions {
yield(fmt.Sprintf("QUARTER(%s)", d), nil)
Expand Down
5 changes: 5 additions & 0 deletions go/vt/vtgate/evalengine/translate_builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,11 @@ func (ast *astCompiler) translateFuncExpr(fn *sqlparser.FuncExpr) (IR, error) {
return nil, argError(method)
}
return &builtinFromDays{CallExpr: call}, nil
case "time_to_sec":
if len(args) != 1 {
return nil, argError(method)
}
return &builtinTimeToSec{CallExpr: call}, nil
case "quarter":
if len(args) != 1 {
return nil, argError(method)
Expand Down

0 comments on commit 3c60baa

Please sign in to comment.