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 1 commit
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
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 @@
}, "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
}

Check warning on line 3825 in go/vt/vtgate/evalengine/compiler_asm.go

View check run for this annotation

Codecov / codecov/patch

go/vt/vtgate/evalengine/compiler_asm.go#L3824-L3825

Added lines #L3824 - L3825 were not covered by tests
d := env.vm.stack[env.vm.sp-1].(*evalTemporal)

sec := d.dt.Time.Hour()*3600 + d.dt.Time.Minute()*60 + d.dt.Time.Second()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic is duplicated in the evaluation VM too. Could you please extract it into a ToSeconds() method in Time?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

env.vm.stack[env.vm.sp-1] = env.vm.arena.newEvalInt64(int64(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
}

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

View check run for this annotation

Codecov / codecov/patch

go/vt/vtgate/evalengine/fn_time.go#L1338-L1339

Added lines #L1338 - L1339 were not covered by tests
if err != nil {
return nil, err
}

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

View check run for this annotation

Codecov / codecov/patch

go/vt/vtgate/evalengine/fn_time.go#L1341-L1342

Added lines #L1341 - L1342 were not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

go/vt/vtgate/evalengine/fn_time.go#L1346-L1347

Added lines #L1346 - L1347 were not covered by tests

sec := d.dt.Time.Hour()*3600 + d.dt.Time.Minute()*60 + d.dt.Time.Second()
return newEvalInt64(int64(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:

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

View check run for this annotation

Codecov / codecov/patch

go/vt/vtgate/evalengine/fn_time.go#L1362

Added line #L1362 was not covered by tests
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
21 changes: 21 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,26 @@ func FnFromDays(yield Query) {
}
}

func FnTimeToSec(yield Query) {
for _, d := range inputConversions {
yield(fmt.Sprintf("FROM_DAYS(%s)", d), nil)
beingnoble03 marked this conversation as resolved.
Show resolved Hide resolved
}

time := []string{
`0`,
`'00:00:00'`,
`'22:23:00'`,
`'00:39:38'`,
`'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