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

types: fix bug which Float type is not effective in AddDate & SubDate function #7840

Merged
merged 6 commits into from
Oct 12, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
170 changes: 167 additions & 3 deletions expression/builtin_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,19 @@ var (
_ builtinFunc = &builtinAddDateStringDecimalSig{}
_ builtinFunc = &builtinAddDateIntStringSig{}
_ builtinFunc = &builtinAddDateIntIntSig{}
_ builtinFunc = &builtinAddDateIntDecimalSig{}
_ builtinFunc = &builtinAddDateDatetimeStringSig{}
_ builtinFunc = &builtinAddDateDatetimeIntSig{}
_ builtinFunc = &builtinAddDateDatetimeDecimalSig{}
_ builtinFunc = &builtinSubDateStringStringSig{}
_ builtinFunc = &builtinSubDateStringIntSig{}
_ builtinFunc = &builtinSubDateStringDecimalSig{}
_ builtinFunc = &builtinSubDateIntStringSig{}
_ builtinFunc = &builtinSubDateIntIntSig{}
_ builtinFunc = &builtinSubDateIntDecimalSig{}
_ builtinFunc = &builtinSubDateDatetimeStringSig{}
_ builtinFunc = &builtinSubDateDatetimeIntSig{}
_ builtinFunc = &builtinSubDateDatetimeDecimalSig{}
)

func convertTimeToMysqlTime(t time.Time, fsp int) (types.Time, error) {
Expand Down Expand Up @@ -2589,8 +2593,14 @@ func (du *baseDateArithmitical) getIntervalFromDecimal(ctx sessionctx.Context, a
interval = "00:" + interval
case "SECOND_MICROSECOND":
/* keep interval as original decimal */
case "SECOND", "MICROSECOND":
args[1] = WrapWithCastAsReal(ctx, args[1])
interval, isNull, err = args[1].EvalString(ctx, row)
if isNull || err != nil {
return "", true, errors.Trace(err)
}
default:
// YEAR, QUARTER, MONTH, WEEK, DAY, HOUR, MINUTE, SECOND, MICROSECOND
// YEAR, QUARTER, MONTH, WEEK, DAY, HOUR, MINUTE
args[1] = WrapWithCastAsInt(ctx, args[1])
interval, isNull, err = args[1].EvalString(ctx, row)
if isNull || err != nil {
Expand Down Expand Up @@ -2620,7 +2630,8 @@ func (du *baseDateArithmitical) add(ctx sessionctx.Context, date types.Time, int
return types.Time{}, true, errors.Trace(err)
}

goTime = goTime.Add(dur)
duration := time.Duration(dur)
goTime = goTime.Add(duration)
goTime = goTime.AddDate(int(year), int(month), int(day))

if goTime.Nanosecond() == 0 {
Expand All @@ -2645,7 +2656,8 @@ func (du *baseDateArithmitical) sub(ctx sessionctx.Context, date types.Time, int
return types.Time{}, true, errors.Trace(err)
}

goTime = goTime.Add(dur)
duration := time.Duration(dur)
goTime = goTime.Add(duration)
goTime = goTime.AddDate(int(year), int(month), int(day))

if goTime.Nanosecond() == 0 {
Expand Down Expand Up @@ -2707,6 +2719,11 @@ func (c *addDateFunctionClass) getFunction(ctx sessionctx.Context, args []Expres
baseBuiltinFunc: bf,
baseDateArithmitical: newDateArighmeticalUtil(),
}
case dateEvalTp == types.ETInt && intervalEvalTp == types.ETDecimal:
Copy link
Member

Choose a reason for hiding this comment

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

Could you extract a function to check if we should use Decimal math, there are some redundant code, and it's easy to make mistake to write compose equal condition.

sig = &builtinAddDateIntDecimalSig{
baseBuiltinFunc: bf,
baseDateArithmitical: newDateArighmeticalUtil(),
}
case dateEvalTp == types.ETDatetime && intervalEvalTp == types.ETString:
sig = &builtinAddDateDatetimeStringSig{
baseBuiltinFunc: bf,
Expand All @@ -2717,6 +2734,11 @@ func (c *addDateFunctionClass) getFunction(ctx sessionctx.Context, args []Expres
baseBuiltinFunc: bf,
baseDateArithmitical: newDateArighmeticalUtil(),
}
case dateEvalTp == types.ETDatetime && intervalEvalTp == types.ETDecimal:
sig = &builtinAddDateDatetimeDecimalSig{
baseBuiltinFunc: bf,
baseDateArithmitical: newDateArighmeticalUtil(),
}
}
return sig, nil
}
Expand Down Expand Up @@ -2886,6 +2908,39 @@ func (b *builtinAddDateIntIntSig) evalTime(row chunk.Row) (types.Time, bool, err
return result, isNull || err != nil, errors.Trace(err)
}

type builtinAddDateIntDecimalSig struct {
baseBuiltinFunc
baseDateArithmitical
}

func (b *builtinAddDateIntDecimalSig) Clone() builtinFunc {
newSig := &builtinAddDateIntDecimalSig{baseDateArithmitical: b.baseDateArithmitical}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}

// evalTime evals ADDDATE(date,INTERVAL expr unit).
// See https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_adddate
func (b *builtinAddDateIntDecimalSig) evalTime(row chunk.Row) (types.Time, bool, error) {
unit, isNull, err := b.args[2].EvalString(b.ctx, row)
if isNull || err != nil {
return types.Time{}, true, errors.Trace(err)
}

date, isNull, err := b.getDateFromInt(b.ctx, b.args, row, unit)
if isNull || err != nil {
return types.Time{}, true, errors.Trace(err)
}

interval, isNull, err := b.getIntervalFromDecimal(b.ctx, b.args, row, unit)
if isNull || err != nil {
return types.Time{}, true, errors.Trace(err)
}

result, isNull, err := b.add(b.ctx, date, interval, unit)
return result, isNull || err != nil, errors.Trace(err)
}

type builtinAddDateDatetimeStringSig struct {
baseBuiltinFunc
baseDateArithmitical
Expand Down Expand Up @@ -2952,6 +3007,39 @@ func (b *builtinAddDateDatetimeIntSig) evalTime(row chunk.Row) (types.Time, bool
return result, isNull || err != nil, errors.Trace(err)
}

type builtinAddDateDatetimeDecimalSig struct {
baseBuiltinFunc
baseDateArithmitical
}

func (b *builtinAddDateDatetimeDecimalSig) Clone() builtinFunc {
newSig := &builtinAddDateDatetimeDecimalSig{baseDateArithmitical: b.baseDateArithmitical}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}

// evalTime evals ADDDATE(date,INTERVAL expr unit).
// See https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_adddate
func (b *builtinAddDateDatetimeDecimalSig) evalTime(row chunk.Row) (types.Time, bool, error) {
unit, isNull, err := b.args[2].EvalString(b.ctx, row)
if isNull || err != nil {
return types.Time{}, true, errors.Trace(err)
}

date, isNull, err := b.getDateFromDatetime(b.ctx, b.args, row, unit)
if isNull || err != nil {
return types.Time{}, true, errors.Trace(err)
}

interval, isNull, err := b.getIntervalFromDecimal(b.ctx, b.args, row, unit)
if isNull || err != nil {
return types.Time{}, true, errors.Trace(err)
}

result, isNull, err := b.add(b.ctx, date, interval, unit)
return result, isNull || err != nil, errors.Trace(err)
}

type subDateFunctionClass struct {
baseFunctionClass
}
Expand Down Expand Up @@ -3001,6 +3089,11 @@ func (c *subDateFunctionClass) getFunction(ctx sessionctx.Context, args []Expres
baseBuiltinFunc: bf,
baseDateArithmitical: newDateArighmeticalUtil(),
}
case dateEvalTp == types.ETInt && intervalEvalTp == types.ETDecimal:
sig = &builtinSubDateIntDecimalSig{
baseBuiltinFunc: bf,
baseDateArithmitical: newDateArighmeticalUtil(),
}
case dateEvalTp == types.ETDatetime && intervalEvalTp == types.ETString:
sig = &builtinSubDateDatetimeStringSig{
baseBuiltinFunc: bf,
Expand All @@ -3011,6 +3104,11 @@ func (c *subDateFunctionClass) getFunction(ctx sessionctx.Context, args []Expres
baseBuiltinFunc: bf,
baseDateArithmitical: newDateArighmeticalUtil(),
}
case dateEvalTp == types.ETDatetime && intervalEvalTp == types.ETDecimal:
sig = &builtinSubDateDatetimeDecimalSig{
baseBuiltinFunc: bf,
baseDateArithmitical: newDateArighmeticalUtil(),
}
}
return sig, nil
}
Expand Down Expand Up @@ -3183,6 +3281,39 @@ type builtinSubDateDatetimeStringSig struct {
baseDateArithmitical
}

type builtinSubDateIntDecimalSig struct {
baseBuiltinFunc
baseDateArithmitical
}

func (b *builtinSubDateIntDecimalSig) Clone() builtinFunc {
newSig := &builtinSubDateIntDecimalSig{baseDateArithmitical: b.baseDateArithmitical}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}

// evalTime evals SUBDATE(date,INTERVAL expr unit).
// See https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_subdate
func (b *builtinSubDateIntDecimalSig) evalTime(row chunk.Row) (types.Time, bool, error) {
unit, isNull, err := b.args[2].EvalString(b.ctx, row)
if isNull || err != nil {
return types.Time{}, true, errors.Trace(err)
}

date, isNull, err := b.getDateFromInt(b.ctx, b.args, row, unit)
if isNull || err != nil {
return types.Time{}, true, errors.Trace(err)
}

interval, isNull, err := b.getIntervalFromDecimal(b.ctx, b.args, row, unit)
if isNull || err != nil {
return types.Time{}, true, errors.Trace(err)
}

result, isNull, err := b.sub(b.ctx, date, interval, unit)
return result, isNull || err != nil, errors.Trace(err)
}

func (b *builtinSubDateDatetimeStringSig) Clone() builtinFunc {
newSig := &builtinSubDateDatetimeStringSig{baseDateArithmitical: b.baseDateArithmitical}
newSig.cloneFrom(&b.baseBuiltinFunc)
Expand Down Expand Up @@ -3244,6 +3375,39 @@ func (b *builtinSubDateDatetimeIntSig) evalTime(row chunk.Row) (types.Time, bool
return result, isNull || err != nil, errors.Trace(err)
}

type builtinSubDateDatetimeDecimalSig struct {
baseBuiltinFunc
baseDateArithmitical
}

func (b *builtinSubDateDatetimeDecimalSig) Clone() builtinFunc {
newSig := &builtinSubDateDatetimeDecimalSig{baseDateArithmitical: b.baseDateArithmitical}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}

// evalTime evals SUBDATE(date,INTERVAL expr unit).
// See https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_subdate
func (b *builtinSubDateDatetimeDecimalSig) evalTime(row chunk.Row) (types.Time, bool, error) {
unit, isNull, err := b.args[2].EvalString(b.ctx, row)
if isNull || err != nil {
return types.Time{}, true, errors.Trace(err)
}

date, isNull, err := b.getDateFromDatetime(b.ctx, b.args, row, unit)
if isNull || err != nil {
return types.Time{}, true, errors.Trace(err)
}

interval, isNull, err := b.getIntervalFromDecimal(b.ctx, b.args, row, unit)
if isNull || err != nil {
return types.Time{}, true, errors.Trace(err)
}

result, isNull, err := b.sub(b.ctx, date, interval, unit)
return result, isNull || err != nil, errors.Trace(err)
}

type timestampDiffFunctionClass struct {
baseFunctionClass
}
Expand Down
3 changes: 2 additions & 1 deletion expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1798,8 +1798,9 @@ func (s *testIntegrationSuite) TestTimeBuiltin(c *C) {
{"\"2011-01-01 00:00:00\"", "10.10", "DAY", "2011-01-11 00:00:00", "2010-12-22 00:00:00"},
{"\"2011-01-01 00:00:00\"", "10.10", "HOUR", "2011-01-01 10:00:00", "2010-12-31 14:00:00"},
{"\"2011-01-01 00:00:00\"", "10.10", "MINUTE", "2011-01-01 00:10:00", "2010-12-31 23:50:00"},
{"\"2011-01-01 00:00:00\"", "10.10", "SECOND", "2011-01-01 00:00:10", "2010-12-31 23:59:50"},
{"\"2011-01-01 00:00:00\"", "10.10", "SECOND", "2011-01-01 00:00:10.100000", "2010-12-31 23:59:49.900000"},
{"\"2011-01-01 00:00:00\"", "10.10", "MICROSECOND", "2011-01-01 00:00:00.000010", "2010-12-31 23:59:59.999990"},
{"\"2011-01-01 00:00:00\"", "10.90", "MICROSECOND", "2011-01-01 00:00:00.000011", "2010-12-31 23:59:59.999989"},

{"\"2009-01-01\"", "6/4", "HOUR_MINUTE", "2009-01-04 12:20:00", "2008-12-28 11:40:00"},
{"\"2009-01-01\"", "6/0", "HOUR_MINUTE", "<nil>", "<nil>"},
Expand Down
2 changes: 2 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,8 @@ func (s *testParserSuite) TestBuiltin(c *C) {
{"select now(6)", true},
{"select sysdate(), sysdate(6)", true},
{"SELECT time('01:02:03');", true},
{"SELECT time('01:02:03.1')", true},
{"SELECT time('20.1')", true},
{"SELECT TIMEDIFF('2000:01:01 00:00:00', '2000:01:01 00:00:00.000001');", true},
{"SELECT TIMESTAMPDIFF(MONTH,'2003-02-01','2003-05-01');", true},
{"SELECT TIMESTAMPDIFF(YEAR,'2002-05-01','2001-01-01');", true},
Expand Down
Loading