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

*: add_date can return mysql.Time #9830

Merged
merged 13 commits into from
May 14, 2019
133 changes: 130 additions & 3 deletions expression/builtin_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -2748,6 +2748,18 @@ func (du *baseDateArithmitical) add(ctx sessionctx.Context, date types.Time, int
return date, false, nil
}

func (du *baseDateArithmitical) addDuration(ctx sessionctx.Context, d types.Duration, interval string, unit string) (types.Duration, bool, error) {
dur, err := types.ExtractDurationValue(unit, interval)
if err != nil {
return types.ZeroDuration, true, handleInvalidTimeError(ctx, err)
}
retDur, err := d.Add(dur)
if err != nil {
return types.ZeroDuration, true, err
}
return retDur, false, nil
}

func (du *baseDateArithmitical) sub(ctx sessionctx.Context, date types.Time, interval string, unit string) (types.Time, bool, error) {
year, month, day, nano, err := types.ParseDurationValue(unit, interval)
if err := handleInvalidTimeError(ctx, err); err != nil {
Expand Down Expand Up @@ -2791,7 +2803,7 @@ func (c *addDateFunctionClass) getFunction(ctx sessionctx.Context, args []Expres
}

dateEvalTp := args[0].GetType().EvalType()
if dateEvalTp != types.ETString && dateEvalTp != types.ETInt {
if dateEvalTp != types.ETString && dateEvalTp != types.ETInt && dateEvalTp != types.ETDuration {
dateEvalTp = types.ETDatetime
}

Expand All @@ -2801,8 +2813,35 @@ func (c *addDateFunctionClass) getFunction(ctx sessionctx.Context, args []Expres
}

argTps := []types.EvalType{dateEvalTp, intervalEvalTp, types.ETString}
bf := newBaseBuiltinFuncWithTp(ctx, args, types.ETDatetime, argTps...)
bf.tp.Flen, bf.tp.Decimal = mysql.MaxDatetimeFullWidth, types.UnspecifiedLength
var bf baseBuiltinFunc
if dateEvalTp == types.ETDuration {
unit, _, err := args[2].EvalString(ctx, chunk.Row{})
if err != nil {
return nil, err
}
internalFsp := 0
switch unit {
// If the unit has micro second, then the fsp must be the MaxFsp.
case "MICROSECOND", "SECOND_MICROSECOND", "MINUTE_MICROSECOND", "HOUR_MICROSECOND", "DAY_MICROSECOND":
internalFsp = types.MaxFsp
// If the unit is second, the fsp is related with the arg[1]'s.
case "SECOND":
internalFsp = types.MaxFsp
if intervalEvalTp != types.ETString {
internalFsp = mathutil.Min(args[1].GetType().Decimal, types.MaxFsp)
}
// Otherwise, the fsp should be 0.
}
bf = newBaseBuiltinFuncWithTp(ctx, args, types.ETDuration, argTps...)
arg0Dec, err := getExpressionFsp(ctx, args[0])
if err != nil {
return nil, err
}
bf.tp.Flen, bf.tp.Decimal = mysql.MaxDurationWidthWithFsp, mathutil.Max(arg0Dec, internalFsp)
} else {
bf = newBaseBuiltinFuncWithTp(ctx, args, types.ETDatetime, argTps...)
bf.tp.Flen, bf.tp.Decimal = mysql.MaxDatetimeFullWidth, types.UnspecifiedLength
}

switch {
case dateEvalTp == types.ETString && intervalEvalTp == types.ETString:
Expand Down Expand Up @@ -2865,6 +2904,21 @@ func (c *addDateFunctionClass) getFunction(ctx sessionctx.Context, args []Expres
baseBuiltinFunc: bf,
baseDateArithmitical: newDateArighmeticalUtil(),
}
case dateEvalTp == types.ETDuration && intervalEvalTp == types.ETString:
sig = &builtinAddDateDurationStringSig{
baseBuiltinFunc: bf,
baseDateArithmitical: newDateArighmeticalUtil(),
}
case dateEvalTp == types.ETDuration && intervalEvalTp == types.ETInt:
sig = &builtinAddDateDurationIntSig{
baseBuiltinFunc: bf,
baseDateArithmitical: newDateArighmeticalUtil(),
}
case dateEvalTp == types.ETDuration && intervalEvalTp == types.ETDecimal:
sig = &builtinAddDateDurationDecimalSig{
baseBuiltinFunc: bf,
baseDateArithmitical: newDateArighmeticalUtil(),
}
}
return sig, nil
}
Expand Down Expand Up @@ -3265,6 +3319,79 @@ func (b *builtinAddDateDatetimeDecimalSig) evalTime(row chunk.Row) (types.Time,
return result, isNull || err != nil, err
}

type builtinAddDateDurationStringSig struct {
baseBuiltinFunc
baseDateArithmitical
}

func (b *builtinAddDateDurationStringSig) evalDuration(row chunk.Row) (types.Duration, bool, error) {
unit, isNull, err := b.args[2].EvalString(b.ctx, row)
if isNull || err != nil {
return types.ZeroDuration, true, err
}

dur, isNull, err := b.args[0].EvalDuration(b.ctx, row)
if isNull || err != nil {
return types.ZeroDuration, true, err
}

interval, isNull, err := b.getIntervalFromString(b.ctx, b.args, row, unit)
if isNull || err != nil {
return types.ZeroDuration, true, err
}

result, isNull, err := b.addDuration(b.ctx, dur, interval, unit)
return result, isNull || err != nil, err
}

type builtinAddDateDurationIntSig struct {
baseBuiltinFunc
baseDateArithmitical
}

func (b *builtinAddDateDurationIntSig) evalDuration(row chunk.Row) (types.Duration, bool, error) {
unit, isNull, err := b.args[2].EvalString(b.ctx, row)
if isNull || err != nil {
return types.ZeroDuration, true, err
}

dur, isNull, err := b.args[0].EvalDuration(b.ctx, row)
if isNull || err != nil {
return types.ZeroDuration, true, err
}
interval, isNull, err := b.getIntervalFromInt(b.ctx, b.args, row, unit)
if isNull || err != nil {
return types.ZeroDuration, true, err
}

result, isNull, err := b.addDuration(b.ctx, dur, interval, unit)
return result, isNull || err != nil, err
}

type builtinAddDateDurationDecimalSig struct {
baseBuiltinFunc
baseDateArithmitical
}

func (b *builtinAddDateDurationDecimalSig) evalDuration(row chunk.Row) (types.Duration, bool, error) {
unit, isNull, err := b.args[2].EvalString(b.ctx, row)
if isNull || err != nil {
return types.ZeroDuration, true, err
}

dur, isNull, err := b.args[0].EvalDuration(b.ctx, row)
if isNull || err != nil {
return types.ZeroDuration, true, err
}
interval, isNull, err := b.getIntervalFromDecimal(b.ctx, b.args, row, unit)
if isNull || err != nil {
return types.ZeroDuration, true, err
}

result, isNull, err := b.addDuration(b.ctx, dur, interval, unit)
return result, isNull || err != nil, err
}

type subDateFunctionClass struct {
baseFunctionClass
}
Expand Down
27 changes: 27 additions & 0 deletions expression/builtin_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1764,6 +1764,33 @@ func (s *testEvaluatorSuite) TestDateArithFuncs(c *C) {
c.Assert(err, IsNil)
c.Assert(v.GetMysqlTime().String(), Equals, test.expected)
}
testDurations := []struct {
dur string
fsp int
unit string
format interface{}
expected string
}{
{
dur: "00:00:00",
fsp: 0,
unit: "MICROSECOND",
format: "100",
expected: "00:00:00.000100",
},
}
for _, tt := range testDurations {
dur, _, ok, err := types.StrToDuration(nil, tt.dur, tt.fsp)
c.Assert(err, IsNil)
c.Assert(ok, IsTrue)
args = types.MakeDatums(dur, tt.format, tt.unit)
f, err = fcAdd.getFunction(s.ctx, s.datumsToConstants(args))
c.Assert(err, IsNil)
c.Assert(f, NotNil)
v, err = evalBuiltinFunc(f, chunk.Row{})
c.Assert(err, IsNil)
c.Assert(v.GetMysqlDuration().String(), Equals, tt.expected)
}
}

func (s *testEvaluatorSuite) TestTimestamp(c *C) {
Expand Down
6 changes: 0 additions & 6 deletions types/const_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package types_test
import (
"context"
"flag"
"testing"

. "github.com/pingcap/check"
"github.com/pingcap/parser"
Expand All @@ -30,11 +29,6 @@ import (
"github.com/pingcap/tidb/util/testleak"
)

func TestT(t *testing.T) {
Copy link
Member Author

Choose a reason for hiding this comment

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

It's a redundant one.

CustomVerboseFlag = true
TestingT(t)
}

var _ = Suite(&testMySQLConstSuite{})

type testMySQLConstSuite struct {
Expand Down
85 changes: 51 additions & 34 deletions types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package types

import (
"github.com/pingcap/errors"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/parser/terror"
parser_types "github.com/pingcap/parser/types"
Expand Down Expand Up @@ -59,28 +60,43 @@ var (
ErrWarnDataOutOfRange = terror.ClassTypes.New(codeDataOutOfRange, mysql.MySQLErrName[mysql.ErrWarnDataOutOfRange])
// ErrDuplicatedValueInType is returned when enum column has duplicated value.
ErrDuplicatedValueInType = terror.ClassTypes.New(codeDuplicatedValueInType, mysql.MySQLErrName[mysql.ErrDuplicatedValueInType])
// ErrDatetimeFunctionOverflow is returned when the calculation in datetime function cause overflow.
ErrDatetimeFunctionOverflow = terror.ClassTypes.New(codeDatetimeFunctionOverflow, mysql.MySQLErrName[mysql.ErrDatetimeFunctionOverflow])
// ErrInvalidTimeFormat is returned when the time format is not correct.
ErrInvalidTimeFormat = terror.ClassTypes.New(mysql.ErrTruncatedWrongValue, "invalid time format: '%v'")
// ErrInvalidWeekModeFormat is returned when the week mode is wrong.
ErrInvalidWeekModeFormat = terror.ClassTypes.New(mysql.ErrTruncatedWrongValue, "invalid week mode format: '%v'")
// ErrInvalidYearFormat is returned when the input is not a valid year format.
ErrInvalidYearFormat = errors.New("invalid year format")
// ErrInvalidYear is returned when the input value is not a valid year.
ErrInvalidYear = errors.New("invalid year")
// ErrIncorrectDatetimeValue is returned when the input is not valid date time value.
ErrIncorrectDatetimeValue = terror.ClassTypes.New(mysql.ErrTruncatedWrongValue, "Incorrect datetime value: '%s'")
// ErrTruncatedWrongValue is returned then
ErrTruncatedWrongValue = terror.ClassTypes.New(mysql.ErrTruncatedWrongValue, mysql.MySQLErrName[mysql.ErrTruncatedWrongValue])
)

const (
codeBadNumber terror.ErrCode = 1

codeDataTooLong = terror.ErrCode(mysql.ErrDataTooLong)
codeIllegalValueForType = terror.ErrCode(mysql.ErrIllegalValueForType)
codeTruncated = terror.ErrCode(mysql.WarnDataTruncated)
codeOverflow = terror.ErrCode(mysql.ErrDataOutOfRange)
codeDivByZero = terror.ErrCode(mysql.ErrDivisionByZero)
codeTooBigDisplayWidth = terror.ErrCode(mysql.ErrTooBigDisplaywidth)
codeTooBigFieldLength = terror.ErrCode(mysql.ErrTooBigFieldlength)
codeTooBigSet = terror.ErrCode(mysql.ErrTooBigSet)
codeTooBigScale = terror.ErrCode(mysql.ErrTooBigScale)
codeTooBigPrecision = terror.ErrCode(mysql.ErrTooBigPrecision)
codeWrongFieldSpec = terror.ErrCode(mysql.ErrWrongFieldSpec)
codeTruncatedWrongValue = terror.ErrCode(mysql.ErrTruncatedWrongValue)
codeUnknown = terror.ErrCode(mysql.ErrUnknown)
codeInvalidDefault = terror.ErrCode(mysql.ErrInvalidDefault)
codeMBiggerThanD = terror.ErrCode(mysql.ErrMBiggerThanD)
codeDataOutOfRange = terror.ErrCode(mysql.ErrWarnDataOutOfRange)
codeDuplicatedValueInType = terror.ErrCode(mysql.ErrDuplicatedValueInType)
codeDataTooLong = terror.ErrCode(mysql.ErrDataTooLong)
codeIllegalValueForType = terror.ErrCode(mysql.ErrIllegalValueForType)
codeTruncated = terror.ErrCode(mysql.WarnDataTruncated)
codeOverflow = terror.ErrCode(mysql.ErrDataOutOfRange)
codeDivByZero = terror.ErrCode(mysql.ErrDivisionByZero)
codeTooBigDisplayWidth = terror.ErrCode(mysql.ErrTooBigDisplaywidth)
codeTooBigFieldLength = terror.ErrCode(mysql.ErrTooBigFieldlength)
codeTooBigSet = terror.ErrCode(mysql.ErrTooBigSet)
codeTooBigScale = terror.ErrCode(mysql.ErrTooBigScale)
codeTooBigPrecision = terror.ErrCode(mysql.ErrTooBigPrecision)
codeWrongFieldSpec = terror.ErrCode(mysql.ErrWrongFieldSpec)
codeTruncatedWrongValue = terror.ErrCode(mysql.ErrTruncatedWrongValue)
codeUnknown = terror.ErrCode(mysql.ErrUnknown)
codeInvalidDefault = terror.ErrCode(mysql.ErrInvalidDefault)
codeMBiggerThanD = terror.ErrCode(mysql.ErrMBiggerThanD)
codeDataOutOfRange = terror.ErrCode(mysql.ErrWarnDataOutOfRange)
codeDuplicatedValueInType = terror.ErrCode(mysql.ErrDuplicatedValueInType)
codeDatetimeFunctionOverflow = terror.ErrCode(mysql.ErrDatetimeFunctionOverflow)
)

var (
Expand All @@ -92,23 +108,24 @@ var (

func init() {
typesMySQLErrCodes := map[terror.ErrCode]uint16{
codeDataTooLong: mysql.ErrDataTooLong,
codeIllegalValueForType: mysql.ErrIllegalValueForType,
codeTruncated: mysql.WarnDataTruncated,
codeOverflow: mysql.ErrDataOutOfRange,
codeDivByZero: mysql.ErrDivisionByZero,
codeTooBigDisplayWidth: mysql.ErrTooBigDisplaywidth,
codeTooBigFieldLength: mysql.ErrTooBigFieldlength,
codeTooBigSet: mysql.ErrTooBigSet,
codeTooBigScale: mysql.ErrTooBigScale,
codeTooBigPrecision: mysql.ErrTooBigPrecision,
codeWrongFieldSpec: mysql.ErrWrongFieldSpec,
codeTruncatedWrongValue: mysql.ErrTruncatedWrongValue,
codeUnknown: mysql.ErrUnknown,
codeInvalidDefault: mysql.ErrInvalidDefault,
codeMBiggerThanD: mysql.ErrMBiggerThanD,
codeDataOutOfRange: mysql.ErrWarnDataOutOfRange,
codeDuplicatedValueInType: mysql.ErrDuplicatedValueInType,
codeDataTooLong: mysql.ErrDataTooLong,
codeIllegalValueForType: mysql.ErrIllegalValueForType,
codeTruncated: mysql.WarnDataTruncated,
codeOverflow: mysql.ErrDataOutOfRange,
codeDivByZero: mysql.ErrDivisionByZero,
codeTooBigDisplayWidth: mysql.ErrTooBigDisplaywidth,
codeTooBigFieldLength: mysql.ErrTooBigFieldlength,
codeTooBigSet: mysql.ErrTooBigSet,
codeTooBigScale: mysql.ErrTooBigScale,
codeTooBigPrecision: mysql.ErrTooBigPrecision,
codeWrongFieldSpec: mysql.ErrWrongFieldSpec,
codeTruncatedWrongValue: mysql.ErrTruncatedWrongValue,
codeUnknown: mysql.ErrUnknown,
codeInvalidDefault: mysql.ErrInvalidDefault,
codeMBiggerThanD: mysql.ErrMBiggerThanD,
codeDataOutOfRange: mysql.ErrWarnDataOutOfRange,
codeDuplicatedValueInType: mysql.ErrDuplicatedValueInType,
codeDatetimeFunctionOverflow: mysql.ErrDatetimeFunctionOverflow,
}
terror.ErrClassToMySQLCodes[terror.ClassTypes] = typesMySQLErrCodes
}
5 changes: 0 additions & 5 deletions types/fsp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,10 @@ package types

import (
"strconv"
"testing"

. "github.com/pingcap/check"
)

func Test(t *testing.T) {
TestingT(t)
}

var _ = Suite(&FspTest{})

type FspTest struct{}
Expand Down
Loading