Skip to content

Commit

Permalink
expression: fix BIT type columns are not padded with left zeros (#25575)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoxugang authored Jun 21, 2021
1 parent 9f18723 commit 6454885
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
12 changes: 12 additions & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8655,3 +8655,15 @@ func (s *testStaleTxnSuite) TestInvalidReadTemporaryTable(c *C) {
tk.MustGetErrMsg(query.sql, "can not read temporary table when 'tidb_snapshot' is set")
}
}

func (s *testSuite) TestIssue25506(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists tbl_3, tbl_23")
tk.MustExec("create table tbl_3 (col_15 bit(20))")
tk.MustExec("insert into tbl_3 values (0xFFFF)")
tk.MustExec("insert into tbl_3 values (0xFF)")
tk.MustExec("create table tbl_23 (col_15 bit(15))")
tk.MustExec("insert into tbl_23 values (0xF)")
tk.MustQuery("(select col_15 from tbl_23) union all (select col_15 from tbl_3 for update)").Check(testkit.Rows("\x00\x00\x0F", "\x00\xFF\xFF", "\x00\x00\xFF"))
}
6 changes: 4 additions & 2 deletions expression/chunk_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,12 @@ func evalOneVec(ctx sessionctx.Context, expr Expression, input *chunk.Chunk, out
i64s := result.Int64s()
buf := chunk.NewColumn(ft, input.NumRows())
buf.ReserveBytes(input.NumRows())
byteSize := (ft.Flen + 7) >> 3
for i := range i64s {
if result.IsNull(i) {
buf.AppendNull()
} else {
buf.AppendBytes(types.NewBinaryLiteralFromUint(uint64(i64s[i]), -1))
buf.AppendBytes(types.NewBinaryLiteralFromUint(uint64(i64s[i]), byteSize))
}
}
// TODO: recycle all old Columns returned here.
Expand Down Expand Up @@ -262,7 +263,8 @@ func executeToInt(ctx sessionctx.Context, expr Expression, fieldType *types.Fiel
return nil
}
if fieldType.Tp == mysql.TypeBit {
output.AppendBytes(colID, types.NewBinaryLiteralFromUint(uint64(res), -1))
byteSize := (fieldType.Flen + 7) >> 3
output.AppendBytes(colID, types.NewBinaryLiteralFromUint(uint64(res), byteSize))
return nil
}
if fieldType.Tp == mysql.TypeEnum {
Expand Down
2 changes: 1 addition & 1 deletion expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3861,7 +3861,7 @@ func (s *testIntegrationSuite) TestCompareBuiltin(c *C) {

result = tk.MustQuery("select coalesce(NULL, a), coalesce(NULL, b, a), coalesce(c, NULL, a, b), coalesce(d, NULL), coalesce(d, c), coalesce(NULL, NULL, e, 1), coalesce(f), coalesce(1, a, b, c, d, e, f) from t2")
// coalesce(col_bit) is not same with MySQL, because it's a bug of MySQL(https://bugs.mysql.com/bug.php?id=103289&thanks=4)
result.Check(testkit.Rows(fmt.Sprintf("1 1.1 2017-08-01 12:01:01 12:01:01 %s 12:01:01 abcdef \x15 1", time.Now().In(tk.Se.GetSessionVars().Location()).Format("2006-01-02"))))
result.Check(testkit.Rows(fmt.Sprintf("1 1.1 2017-08-01 12:01:01 12:01:01 %s 12:01:01 abcdef \x00\x15 1", time.Now().In(tk.Se.GetSessionVars().Location()).Format("2006-01-02"))))

// nullif
result = tk.MustQuery(`SELECT NULLIF(NULL, 1), NULLIF(1, NULL), NULLIF(1, 1), NULLIF(NULL, NULL);`)
Expand Down

0 comments on commit 6454885

Please sign in to comment.