Skip to content

Commit

Permalink
evalEngine: Implement TIME_TO_SEC
Browse files Browse the repository at this point in the history
Signed-off-by: Noble Mittal <[email protected]>
  • Loading branch information
beingnoble03 committed Jan 30, 2024
1 parent 77dc0c9 commit 70d761b
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 0 deletions.
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
}

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()
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 @@ 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
}

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)
}

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

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

0 comments on commit 70d761b

Please sign in to comment.