Skip to content

Commit

Permalink
expression: fix unexpected constant fold when year compare string (#2…
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-srebot authored Sep 2, 2021
1 parent 3bb7f41 commit 0b642f6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
8 changes: 4 additions & 4 deletions expression/builtin_compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -1324,15 +1324,15 @@ func RefineComparedConstant(ctx sessionctx.Context, targetFieldType types.FieldT
// We try to convert the string constant to double.
// If the double result equals the int result, we can return the int result;
// otherwise, the compare function will be false.
// **Notice**
// we can not compare double result to int result directly, because year type will change its value, like
// 2 to 2002, here we just check whether double value equal int(double value). We can assert the int(string)
var doubleDatum types.Datum
doubleDatum, err = dt.ConvertTo(sc, types.NewFieldType(mysql.TypeDouble))
if err != nil {
return con, false
}
if c, err = doubleDatum.CompareDatum(sc, &intDatum); err != nil {
return con, false
}
if c != 0 {
if doubleDatum.GetFloat64() != math.Trunc(doubleDatum.GetFloat64()) {
return con, true
}
return &Constant{
Expand Down
10 changes: 10 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8949,6 +8949,16 @@ func (s *testIntegrationSuite) Test22717(c *C) {
tk.MustQuery("select d from t where d").Check(testkit.Rows("0", "1", "0,1"))
}

func (s *testIntegrationSuite) Test23262(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a year)")
tk.MustExec("insert into t values(2002)")
tk.MustQuery("select * from t where a=2").Check(testkit.Rows("2002"))
tk.MustQuery("select * from t where a='2'").Check(testkit.Rows("2002"))
}

func (s *testIntegrationSerialSuite) TestPartitionPruningRelaxOP(c *C) {
// Discovered while looking at issue 19941 (not completely related)
// relaxOP relax the op > to >= and < to <=
Expand Down

0 comments on commit 0b642f6

Please sign in to comment.