Skip to content

Commit

Permalink
expression: fix week func format (#9685) (#9753)
Browse files Browse the repository at this point in the history
  • Loading branch information
glock42 authored and zz-jason committed Mar 25, 2019
1 parent 705341b commit e2bcb88
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
12 changes: 11 additions & 1 deletion expression/builtin_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/pingcap/parser/terror"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/stmtctx"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tipb/go-tipb"
Expand Down Expand Up @@ -1284,7 +1285,16 @@ func (b *builtinWeekWithoutModeSig) evalInt(row chunk.Row) (int64, bool, error)
return 0, true, errors.Trace(handleInvalidTimeError(b.ctx, types.ErrIncorrectDatetimeValue.GenWithStackByArgs(date.String())))
}

week := date.Time.Week(0)
mode := 0
modeStr, ok := b.ctx.GetSessionVars().GetSystemVar(variable.DefaultWeekFormat)
if ok && modeStr != "" {
mode, err = strconv.Atoi(modeStr)
if err != nil {
return 0, true, handleInvalidTimeError(b.ctx, types.ErrInvalidWeekModeFormat.GenWithStackByArgs(modeStr))
}
}

week := date.Time.Week(mode)
return int64(week), false, nil
}

Expand Down
28 changes: 27 additions & 1 deletion expression/builtin_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1456,9 +1456,35 @@ func (s *testEvaluatorSuite) TestWeek(c *C) {
c.Assert(err, IsNil)
c.Assert(result.GetInt64(), Equals, test.expect)
}

}

func (s *testEvaluatorSuite) TestWeekWithoutModeSig(c *C) {
tests := []struct {
t string
expect int64
}{
{"2008-02-20", 7},
{"2000-12-31", 53},
{"2000-12-31", 1}, //set default week mode
{"2005-12-3", 48}, //set default week mode
{"2008-02-20", 7},
}

fc := funcs[ast.Week]
for i, test := range tests {
arg1 := types.NewStringDatum(test.t)
f, err := fc.getFunction(s.ctx, s.datumsToConstants([]types.Datum{arg1}))
c.Assert(err, IsNil)
result, err := evalBuiltinFunc(f, chunk.Row{})
c.Assert(err, IsNil)
c.Assert(result.GetInt64(), Equals, test.expect)
if i == 1 {
s.ctx.GetSessionVars().SetSystemVar("default_week_format", "6")
} else if i == 3 {
s.ctx.GetSessionVars().SetSystemVar("default_week_format", "")
}
}
}
func (s *testEvaluatorSuite) TestYearWeek(c *C) {
sc := s.ctx.GetSessionVars().StmtCtx
sc.IgnoreZeroInDate = true
Expand Down
3 changes: 2 additions & 1 deletion expression/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ func init() {

// handleInvalidTimeError reports error or warning depend on the context.
func handleInvalidTimeError(ctx sessionctx.Context, err error) error {
if err == nil || !(terror.ErrorEqual(err, types.ErrInvalidTimeFormat) || types.ErrIncorrectDatetimeValue.Equal(err) || types.ErrTruncatedWrongValue.Equal(err)) {
if err == nil || !(terror.ErrorEqual(err, types.ErrInvalidTimeFormat) || types.ErrIncorrectDatetimeValue.Equal(err) ||
types.ErrTruncatedWrongValue.Equal(err) || types.ErrInvalidWeekModeFormat.Equal(err)) {
return err
}
sc := ctx.GetSessionVars().StmtCtx
Expand Down
1 change: 1 addition & 0 deletions types/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
// Portable analogs of some common call errors.
var (
ErrInvalidTimeFormat = terror.ClassTypes.New(mysql.ErrTruncatedWrongValue, "invalid time format: '%v'")
ErrInvalidWeekModeFormat = terror.ClassTypes.New(mysql.ErrTruncatedWrongValue, "invalid week mode format: '%v'")
ErrInvalidYearFormat = errors.New("invalid year format")
ErrInvalidYear = errors.New("invalid year")
ErrZeroDate = errors.New("datetime zero in date")
Expand Down

0 comments on commit e2bcb88

Please sign in to comment.