Skip to content

Commit

Permalink
evalengine: Implement LAST_DAY
Browse files Browse the repository at this point in the history
Signed-off-by: Noble Mittal <[email protected]>
  • Loading branch information
beingnoble03 committed Jan 25, 2024
1 parent 1bf8dda commit 32a2831
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
16 changes: 16 additions & 0 deletions go/vt/vtgate/evalengine/compiler_asm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3778,6 +3778,22 @@ func (asm *assembler) Fn_MONTHNAME(col collations.TypedCollation) {
}, "FN MONTHNAME DATE(SP-1)")
}

func (asm *assembler) Fn_LAST_DAY() {
asm.emit(func(env *ExpressionEnv) int {
if env.vm.stack[env.vm.sp-1] == nil {
return 1
}
arg := env.vm.stack[env.vm.sp-1].(*evalTemporal)
d, ok := lastDay(arg.dt)
if !ok {
env.vm.stack[env.vm.sp-1] = nil
return 1
}

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

View check run for this annotation

Codecov / codecov/patch

go/vt/vtgate/evalengine/compiler_asm.go#L3789-L3791

Added lines #L3789 - L3791 were not covered by tests
env.vm.stack[env.vm.sp-1] = env.vm.arena.newEvalDate(d)
return 1
}, "FN LAST_DAY DATETIME(SP-1)")
}

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

builtinLastDay struct {
CallExpr
}

builtinQuarter struct {
CallExpr
}
Expand Down Expand Up @@ -168,6 +172,7 @@ var _ IR = (*builtinMicrosecond)(nil)
var _ IR = (*builtinMinute)(nil)
var _ IR = (*builtinMonth)(nil)
var _ IR = (*builtinMonthName)(nil)
var _ IR = (*builtinLastDay)(nil)
var _ IR = (*builtinQuarter)(nil)
var _ IR = (*builtinSecond)(nil)
var _ IR = (*builtinTime)(nil)
Expand Down Expand Up @@ -1200,6 +1205,54 @@ func (call *builtinMonthName) compile(c *compiler) (ctype, error) {
return ctype{Type: sqltypes.VarChar, Col: col, Flag: arg.Flag | flagNullable}, nil
}

func lastDay(dt datetime.DateTime) (datetime.Date, bool) {
ts := dt.Date.ToStdTime(time.Local)
firstDayOfNextMonth := time.Date(ts.Year(), ts.Month()+1, 1, 0, 0, 0, 0, time.Local)
lastDayOfMonth := firstDayOfNextMonth.AddDate(0, 0, -1)

date := datetime.NewDateFromStd(lastDayOfMonth)
return date, true
}

func (b *builtinLastDay) eval(env *ExpressionEnv) (eval, error) {
date, err := b.arg1(env)
if err != nil {
return nil, err
}
if date == nil {
return nil, nil
}
dt := evalToDateTime(date, -1, env.now, env.sqlmode.AllowZeroDate())
if dt == nil || dt.isZero() {
return nil, nil
}

d, ok := lastDay(dt.dt)
if !ok {
return nil, nil
}

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

View check run for this annotation

Codecov / codecov/patch

go/vt/vtgate/evalengine/fn_time.go#L1232-L1233

Added lines #L1232 - L1233 were not covered by tests

return newEvalDate(d, env.sqlmode.AllowZeroDate()), nil
}

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

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

View check run for this annotation

Codecov / codecov/patch

go/vt/vtgate/evalengine/fn_time.go#L1241-L1242

Added lines #L1241 - L1242 were not covered by tests

skip := c.compileNullCheck1(arg)

switch arg.Type {
case sqltypes.Date, sqltypes.Datetime:
default:
c.asm.Convert_xD(1, c.sqlmode.AllowZeroDate())
}
c.asm.Fn_LAST_DAY()
c.asm.jumpDestination(skip)
return ctype{Type: sqltypes.Date, Flag: arg.Flag | flagNullable}, nil
}

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

func FnLastDay(yield Query) {
for _, d := range inputConversions {
yield(fmt.Sprintf("LAST_DAY(%s)", d), 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 @@ -414,6 +414,11 @@ func (ast *astCompiler) translateFuncExpr(fn *sqlparser.FuncExpr) (IR, error) {
return nil, argError(method)
}
return &builtinMonthName{CallExpr: call, collate: ast.cfg.Collation}, nil
case "last_day":
if len(args) != 1 {
return nil, argError(method)
}

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

View check run for this annotation

Codecov / codecov/patch

go/vt/vtgate/evalengine/translate_builtin.go#L419-L420

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

0 comments on commit 32a2831

Please sign in to comment.