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

expression: fix week func format (#9685) #9753

Merged
merged 8 commits into from
Mar 25, 2019
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
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