Skip to content

Commit

Permalink
Make use of DateFromDayNumber in FROM_DAYS func
Browse files Browse the repository at this point in the history
Signed-off-by: Noble Mittal <[email protected]>
  • Loading branch information
beingnoble03 committed Jan 28, 2024
1 parent 9a73127 commit e44193a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 19 deletions.
12 changes: 5 additions & 7 deletions go/vt/vtgate/evalengine/compiler_asm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3798,15 +3798,13 @@ func (asm *assembler) Fn_LAST_DAY() {
func (asm *assembler) Fn_FROM_DAYS() {
asm.emit(func(env *ExpressionEnv) int {
arg := env.vm.stack[env.vm.sp-1].(*evalInt64)
y, m, d := datetime.MysqlDateFromDayNumber(int(arg.i))
if y == 0 && m == 0 && d == 0 {
env.vm.stack[env.vm.sp-1] = env.vm.arena.newEvalDate(datetime.Date{})
} else if y > 9999 {
d := datetime.DateFromDayNumber(int(arg.i))
if d.Year() > 9999 {
env.vm.stack[env.vm.sp-1] = nil
} else {
dt := datetime.NewDateFromStd(time.Date(int(y), time.Month(m), int(d), 0, 0, 0, 0, env.currentTimezone()))
env.vm.stack[env.vm.sp-1] = env.vm.arena.newEvalDate(dt)
return 1
}

env.vm.stack[env.vm.sp-1] = env.vm.arena.newEvalDate(d)
return 1
}, "FN FROM_DAYS INT64(SP-1)")
}
Expand Down
16 changes: 4 additions & 12 deletions go/vt/vtgate/evalengine/fn_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -1263,21 +1263,13 @@ func (b *builtinFromDays) eval(env *ExpressionEnv) (eval, error) {
return nil, err
}

days := evalToInt64(arg).i
y, m, d := datetime.MysqlDateFromDayNumber(int(days))
d := datetime.DateFromDayNumber(int(evalToInt64(arg).i))

// mysql returns 0000-00-00 for days below 366 and above 3652499
if y == 0 && m == 0 && d == 0 {
return newEvalDate(datetime.Date{}, true), nil
}

// mysql returns NULL if y is greater than 9999
if y > 9999 {
// mysql returns NULL if year is greater than 9999
if d.Year() > 9999 {
return nil, nil
}

dt := datetime.NewDateFromStd(time.Date(int(y), time.Month(m), int(d), 0, 0, 0, 0, env.currentTimezone()))
return newEvalDate(dt, true), nil
return newEvalDate(d, true), nil
}

func (call *builtinFromDays) compile(c *compiler) (ctype, error) {
Expand Down

0 comments on commit e44193a

Please sign in to comment.