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 TO_DAYS #15065

Merged
merged 5 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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: 2 additions & 2 deletions go/mysql/datetime/datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@
dt.Time.minute = uint8((dur % time.Hour) / time.Minute)
dt.Time.hour = uint16(dur / time.Hour)

daynum := mysqlDayNumber(dt.Date.Year(), dt.Date.Month(), 1) + int(days)
daynum := MysqlDayNumber(dt.Date.Year(), dt.Date.Month(), 1) + int(days)

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

View check run for this annotation

Codecov / codecov/patch

go/mysql/datetime/datetime.go#L613

Added line #L613 was not covered by tests
if daynum < 0 || daynum > maxDay {
return false
}
Expand All @@ -619,7 +619,7 @@
return true

case itv.unit.HasDayParts():
daynum := mysqlDayNumber(dt.Date.Year(), dt.Date.Month(), dt.Date.Day())
daynum := MysqlDayNumber(dt.Date.Year(), dt.Date.Month(), dt.Date.Day())

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

View check run for this annotation

Codecov / codecov/patch

go/mysql/datetime/datetime.go#L622

Added line #L622 was not covered by tests
daynum += itv.day
dt.Date.year, dt.Date.month, dt.Date.day = mysqlDateFromDayNumber(daynum)
return true
Expand Down
8 changes: 4 additions & 4 deletions go/mysql/datetime/mydate.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ limitations under the License.

package datetime

// mysqlDayNumber converts a date into an absolute day number.
// MysqlDayNumber converts a date into an absolute day number.
// This is an algorithm that has been reverse engineered from MySQL;
// the tables used as a reference can be found in `testdata/year_to_daynr.json`.
// It is worth noting that this absolute day number does not match the
Expand All @@ -29,7 +29,7 @@ package datetime
// This API should only be used when performing datetime calculations (addition
// and subtraction), so that the results match MySQL's. All other date handling
// operations must use our helpers based on Go's standard library.
func mysqlDayNumber(year, month, day int) int {
func MysqlDayNumber(year, month, day int) int {
if year == 0 && month == 0 {
return 0
}
Expand All @@ -49,8 +49,8 @@ func mysqlDayNumber(year, month, day int) int {
// mysqlDateFromDayNumber converts an absolute day number into a date (a year, month, day triplet).
// This is an algorithm that has been reverse engineered from MySQL;
// the tables used as a reference can be found in `testdata/daynr_to_date.json`.
// See the warning from mysqlDayNumber: the day number used as an argument to
// this function must come from mysqlDayNumber or the results won't be correct.
// See the warning from MysqlDayNumber: the day number used as an argument to
// this function must come from MysqlDayNumber or the results won't be correct.
// This API should only be used when performing datetime calculations (addition
// and subtraction), so that the results match MySQL's. All other date handling
// operations must use our helpers based on Go's standard library.
Expand Down
4 changes: 2 additions & 2 deletions go/mysql/datetime/mydate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestDayNumber(t *testing.T) {
require.NoError(t, err)

for year, daynr := range expected {
assert.Equal(t, daynr, mysqlDayNumber(year, 1, 1))
assert.Equal(t, daynr, MysqlDayNumber(year, 1, 1))
}
}

Expand All @@ -54,6 +54,6 @@ func TestDayNumberFields(t *testing.T) {
assert.Equal(t, tc[2], int(m))
assert.Equal(t, tc[3], int(d))

assert.Equalf(t, tc[0], mysqlDayNumber(tc[1], tc[2], tc[3]), "date %d-%d-%d", tc[1], tc[2], tc[3])
assert.Equalf(t, tc[0], MysqlDayNumber(tc[1], tc[2], tc[3]), "date %d-%d-%d", tc[1], tc[2], tc[3])
}
}
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.

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 @@ -3795,6 +3795,22 @@ func (asm *assembler) Fn_LAST_DAY() {
}, "FN LAST_DAY DATETIME(SP-1)")
}

func (asm *assembler) Fn_TO_DAYS() {
asm.emit(func(env *ExpressionEnv) int {
if env.vm.stack[env.vm.sp-1] == nil {
return 1
}
vmg marked this conversation as resolved.
Show resolved Hide resolved
arg := env.vm.stack[env.vm.sp-1].(*evalTemporal)
if arg.dt.Date.IsZero() {
env.vm.stack[env.vm.sp-1] = nil
} else {
numDays := datetime.MysqlDayNumber(arg.dt.Date.Year(), arg.dt.Date.Month(), arg.dt.Date.Day())
env.vm.stack[env.vm.sp-1] = env.vm.arena.newEvalInt64(int64(numDays))
}
return 1
}, "FN TO_DAYS DATE(SP-1)")
}

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

builtinToDays struct {
CallExpr
}

builtinQuarter struct {
CallExpr
}
Expand Down Expand Up @@ -173,6 +177,7 @@
var _ IR = (*builtinMonth)(nil)
var _ IR = (*builtinMonthName)(nil)
var _ IR = (*builtinLastDay)(nil)
var _ IR = (*builtinToDays)(nil)
var _ IR = (*builtinQuarter)(nil)
var _ IR = (*builtinSecond)(nil)
var _ IR = (*builtinTime)(nil)
Expand Down Expand Up @@ -1249,6 +1254,41 @@
return ctype{Type: sqltypes.Date, Flag: arg.Flag | flagNullable}, nil
}

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

numDays := datetime.MysqlDayNumber(dt.dt.Date.Year(), dt.dt.Date.Month(), dt.dt.Date.Day())
return newEvalInt64(int64(numDays)), nil
}

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

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

View check run for this annotation

Codecov / codecov/patch

go/vt/vtgate/evalengine/fn_time.go#L1277-L1278

Added lines #L1277 - L1278 were not covered by tests

skip := c.compileNullCheck1(arg)

switch arg.Type {
case sqltypes.Date, sqltypes.Datetime:
default:
c.asm.Convert_xD(1, true)
}
c.asm.Fn_TO_DAYS()
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 @@ -131,6 +131,7 @@ var Cases = []TestCase{
{Run: FnMonth},
{Run: FnMonthName},
{Run: FnLastDay},
{Run: FnToDays},
{Run: FnQuarter},
{Run: FnSecond},
{Run: FnTime},
Expand Down Expand Up @@ -1766,6 +1767,30 @@ func FnLastDay(yield Query) {
}
}

func FnToDays(yield Query) {
for _, d := range inputConversions {
yield(fmt.Sprintf("TO_DAYS(%s)", d), nil)
}

dates := []string{
`DATE'0000-00-00'`,
`0`,
`'0000-00-00'`,
`DATE'2023-09-03 00:00:00'`,
`DATE'2023-09-03 07:00:00'`,
`DATE'0000-00-00 00:00:00'`,
`950501`,
`'2007-10-07'`,
Copy link
Contributor

Choose a reason for hiding this comment

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

https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_to-days has these cases:

TO_DAYS(950501);
TO_DAYS('2007-10-07');
TO_DAYS('2008-10-07'),
TO_DAYS('08-10-07');
TO_DAYS('0000-00-00');
TO_DAYS('0000-01-01');

Can you add the ones from there missing to make sure they all pass?

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.

`'2008-10-07'`,
`'08-10-07'`,
`'0000-01-01'`,
}

for _, d := range dates {
yield(fmt.Sprintf("TO_DAYS(%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 @@ -419,6 +419,11 @@
return nil, argError(method)
}
return &builtinLastDay{CallExpr: call}, nil
case "to_days":
if len(args) != 1 {
return nil, argError(method)
}

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

View check run for this annotation

Codecov / codecov/patch

go/vt/vtgate/evalengine/translate_builtin.go#L424-L425

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