Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

evalEngine: Implement TIME_TO_SEC #15094

Merged
merged 3 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
return dur
}

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

Check warning on line 452 in go/mysql/datetime/datetime.go

View check run for this annotation

Codecov / codecov/patch

go/mysql/datetime/datetime.go#L451-L452

Added lines #L451 - L452 were not covered by tests
}

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 @@
CallExpr
}

builtinTimeToSec struct {
CallExpr
}

builtinQuarter struct {
CallExpr
}
Expand Down Expand Up @@ -183,6 +187,7 @@
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 @@
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
}

Check warning on line 1357 in go/vt/vtgate/evalengine/fn_time.go

View check run for this annotation

Codecov / codecov/patch

go/vt/vtgate/evalengine/fn_time.go#L1356-L1357

Added lines #L1356 - L1357 were not covered by tests

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'`,
dbussink marked this conversation as resolved.
Show resolved Hide resolved
}

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 @@
return nil, argError(method)
}
return &builtinFromDays{CallExpr: call}, nil
case "time_to_sec":
if len(args) != 1 {
return nil, argError(method)
}

Check warning on line 435 in go/vt/vtgate/evalengine/translate_builtin.go

View check run for this annotation

Codecov / codecov/patch

go/vt/vtgate/evalengine/translate_builtin.go#L434-L435

Added lines #L434 - L435 were not covered by tests
return &builtinTimeToSec{CallExpr: call}, nil
case "quarter":
if len(args) != 1 {
return nil, argError(method)
Expand Down
Loading