Skip to content

Commit

Permalink
util: fix wrong convert from string to enum (#27629) (#27632)
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-srebot authored Aug 27, 2021
1 parent 3b39fed commit 05d2210
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
10 changes: 10 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10156,3 +10156,13 @@ func (s *testIntegrationSuite) TestIssue27233(c *C) {
tk.MustQuery("SELECT col2 FROM t AS T1 WHERE ( SELECT count(DISTINCT COL1, COL2) FROM t AS T2 WHERE T2.COL1 > T1.COL1 ) > 2 ;").
Check(testkit.Rows("100"))
}

func (s *testIntegrationSuite) TestIssue27610(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec(`use test;`)
tk.MustExec(`drop table if exists PK_TCOLLATION3966STROBJSTROBJ;`)
tk.MustExec("CREATE TABLE `PK_TCOLLATION3966STROBJSTROBJ` (\n `COL1` enum('ll','aa','bb','cc','dd','ee') COLLATE utf8_general_ci NOT NULL,\n `COL2` varchar(20) COLLATE utf8_general_ci DEFAULT NULL,\n PRIMARY KEY (`COL1`) /*T![clustered_index] CLUSTERED */\n) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;")
tk.MustExec(`insert into PK_TCOLLATION3966STROBJSTROBJ values("ee", "tttt");`)
tk.MustQuery("SELECT col1, COL2 FROM PK_TCOLLATION3966STROBJSTROBJ WHERE COL1 IN ('notexist','6') and col2 not in (\"abcd\");").
Check(testkit.Rows())
}
15 changes: 14 additions & 1 deletion util/ranger/points.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,20 @@ func (r *builder) buildFromIn(expr *expression.ScalarFunction) ([]*point, bool)
dt.SetString(dt.GetString(), colCollate)
}
if expr.GetArgs()[0].GetType().Tp == mysql.TypeEnum {
dt, err = dt.ConvertTo(r.sc, expr.GetArgs()[0].GetType())
switch dt.Kind() {
case types.KindString, types.KindBytes, types.KindBinaryLiteral:
// Can't use ConvertTo directly, since we shouldn't convert numerical string to Enum in select stmt.
targetType := expr.GetArgs()[0].GetType()
enum, parseErr := types.ParseEnumName(targetType.Elems, dt.GetString(), targetType.Collate)
if parseErr == nil {
dt.SetMysqlEnum(enum, targetType.Collate)
} else {
err = parseErr
}
default:
dt, err = dt.ConvertTo(r.sc, expr.GetArgs()[0].GetType())
}

if err != nil {
// in (..., an impossible value (not valid enum), ...), the range is empty, so skip it.
continue
Expand Down

0 comments on commit 05d2210

Please sign in to comment.