Skip to content

Commit

Permalink
expression: handle hybrid field types for where clause (#21724)
Browse files Browse the repository at this point in the history
  • Loading branch information
ichn-hu authored Dec 15, 2020
1 parent e01a142 commit 41eb594
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
12 changes: 12 additions & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7142,3 +7142,15 @@ func (s *testSuite) Test15492(c *C) {
tk.MustExec("insert into t values (2, 20), (1, 10), (3, 30)")
tk.MustQuery("select a + 1 as field1, a as field2 from t order by field1, field2 limit 2").Check(testkit.Rows("2 1", "3 2"))
}

func (s *testSuite) Test12201(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists e")
tk.MustExec("create table e (e enum('a', 'b'))")
tk.MustExec("insert into e values ('a'), ('b')")
tk.MustQuery("select * from e where case 1 when 0 then e end").Check(testkit.Rows())
tk.MustQuery("select * from e where case 1 when 1 then e end").Check(testkit.Rows("a", "b"))
tk.MustQuery("select * from e where case e when 1 then e end").Check(testkit.Rows("a"))
tk.MustQuery("select * from e where case 1 when e then e end").Check(testkit.Rows("a"))
}
36 changes: 26 additions & 10 deletions expression/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,8 @@ func VecEvalBool(ctx sessionctx.Context, exprList CNFExprs, input *chunk.Chunk,
isZero := allocZeroSlice(n)
defer deallocateZeroSlice(isZero)
for _, expr := range exprList {
eType := expr.GetType().EvalType()
if expr.GetType().Hybrid() {
eType = types.ETInt
}
tp := expr.GetType()
eType := tp.EvalType()
buf, err := globalColumnAllocator.get(eType, n)
if err != nil {
return nil, nil, err
Expand All @@ -344,7 +342,7 @@ func VecEvalBool(ctx sessionctx.Context, exprList CNFExprs, input *chunk.Chunk,
return nil, nil, err
}

err = toBool(ctx.GetSessionVars().StmtCtx, eType, buf, sel, isZero)
err = toBool(ctx.GetSessionVars().StmtCtx, tp, buf, sel, isZero)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -384,7 +382,8 @@ func VecEvalBool(ctx sessionctx.Context, exprList CNFExprs, input *chunk.Chunk,
return selected, nulls, nil
}

func toBool(sc *stmtctx.StatementContext, eType types.EvalType, buf *chunk.Column, sel []int, isZero []int8) error {
func toBool(sc *stmtctx.StatementContext, tp *types.FieldType, buf *chunk.Column, sel []int, isZero []int8) error {
eType := tp.EvalType()
switch eType {
case types.ETInt:
i64s := buf.Int64s()
Expand Down Expand Up @@ -443,11 +442,28 @@ func toBool(sc *stmtctx.StatementContext, eType types.EvalType, buf *chunk.Colum
if buf.IsNull(i) {
isZero[i] = -1
} else {
iVal, err := types.StrToFloat(sc, buf.GetString(i), false)
if err != nil {
return err
var fVal float64
var err error
sVal := buf.GetString(i)
if tp.Hybrid() {
switch tp.Tp {
case mysql.TypeEnum, mysql.TypeSet:
fVal = float64(len(sVal))
case mysql.TypeBit:
var bl types.BinaryLiteral = buf.GetBytes(i)
iVal, err := bl.ToInt(sc)
if err != nil {
return err
}
fVal = float64(iVal)
}
} else {
fVal, err = types.StrToFloat(sc, sVal, false)
if err != nil {
return err
}
}
if iVal == 0 {
if fVal == 0 {
isZero[i] = 0
} else {
isZero[i] = 1
Expand Down

0 comments on commit 41eb594

Please sign in to comment.