Skip to content

Commit

Permalink
This is an automated cherry-pick of pingcap#57303
Browse files Browse the repository at this point in the history
Signed-off-by: ti-chi-bot <[email protected]>
  • Loading branch information
YangKeao authored and ti-chi-bot committed Nov 13, 2024
1 parent 9d5e282 commit 6f3f76c
Show file tree
Hide file tree
Showing 8 changed files with 1,414 additions and 11 deletions.
1,327 changes: 1,327 additions & 0 deletions pkg/ddl/add_column.go

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions pkg/executor/test/writetest/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1301,7 +1301,7 @@ func TestIssue18681(t *testing.T) {
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
createSQL := `drop table if exists load_data_test;
create table load_data_test (a bit(1),b bit(1),c bit(1),d bit(1));`
create table load_data_test (a bit(1),b bit(1),c bit(1),d bit(1),e bit(32),f bit(1));`
tk.MustExec(createSQL)
tk.MustExec("load data local infile '/tmp/nonexistence.csv' ignore into table load_data_test")
ctx := tk.Session().(sessionctx.Context)
Expand All @@ -1311,9 +1311,16 @@ func TestIssue18681(t *testing.T) {
require.NotNil(t, ld)

deleteSQL := "delete from load_data_test"
<<<<<<< HEAD
selectSQL := "select bin(a), bin(b), bin(c), bin(d) from load_data_test;"
ctx.GetSessionVars().StmtCtx.DupKeyAsWarning = true
ctx.GetSessionVars().StmtCtx.BadNullAsWarning = true
=======
selectSQL := "select bin(a), bin(b), bin(c), bin(d), bin(e), bin(f) from load_data_test;"
levels := ctx.GetSessionVars().StmtCtx.ErrLevels()
levels[errctx.ErrGroupDupKey] = errctx.LevelWarn
levels[errctx.ErrGroupBadNull] = errctx.LevelWarn
>>>>>>> 95b04c76703 (table: fix the issue that the default value for `BIT` column is wrong (#57303))

sc := ctx.GetSessionVars().StmtCtx
originIgnoreTruncate := sc.IgnoreTruncate.Load()
Expand All @@ -1322,7 +1329,7 @@ func TestIssue18681(t *testing.T) {
}()
sc.IgnoreTruncate.Store(false)
tests := []testCase{
{[]byte("true\tfalse\t0\t1\n"), []string{"1|0|0|1"}, "Records: 1 Deleted: 0 Skipped: 0 Warnings: 0"},
{[]byte("true\tfalse\t0\t1\tb'1'\tb'1'\n"), []string{"1|1|1|1|1100010001001110011000100100111|1"}, "Records: 1 Deleted: 0 Skipped: 0 Warnings: 5"},
}
checkCases(tests, ld, t, tk, ctx, selectSQL, deleteSQL)
require.Equal(t, uint16(0), sc.WarningCount())
Expand Down
9 changes: 9 additions & 0 deletions pkg/types/datum.go
Original file line number Diff line number Diff line change
Expand Up @@ -1576,6 +1576,7 @@ func (d *Datum) ConvertToMysqlYear(sc *stmtctx.StatementContext, target *FieldTy
return ret, errors.Trace(err)
}

<<<<<<< HEAD
func (d *Datum) convertStringToMysqlBit(sc *stmtctx.StatementContext) (uint64, error) {
bitStr, err := ParseBitStr(BinaryLiteral(d.b).ToString())
if err != nil {
Expand All @@ -1586,10 +1587,14 @@ func (d *Datum) convertStringToMysqlBit(sc *stmtctx.StatementContext) (uint64, e
}

func (d *Datum) convertToMysqlBit(sc *stmtctx.StatementContext, target *FieldType) (Datum, error) {
=======
func (d *Datum) convertToMysqlBit(ctx Context, target *FieldType) (Datum, error) {
>>>>>>> 95b04c76703 (table: fix the issue that the default value for `BIT` column is wrong (#57303))
var ret Datum
var uintValue uint64
var err error
switch d.k {
<<<<<<< HEAD
case KindBytes:
uintValue, err = BinaryLiteral(d.b).ToInt(sc)
case KindString:
Expand All @@ -1608,6 +1613,10 @@ func (d *Datum) convertToMysqlBit(sc *stmtctx.StatementContext, target *FieldTyp
} else {
uintValue, err = d.convertStringToMysqlBit(sc)
}
=======
case KindString, KindBytes:
uintValue, err = BinaryLiteral(d.b).ToInt(ctx)
>>>>>>> 95b04c76703 (table: fix the issue that the default value for `BIT` column is wrong (#57303))
case KindInt64:
// if input kind is int64 (signed), when trans to bit, we need to treat it as unsigned
d.k = KindUint64
Expand Down
40 changes: 31 additions & 9 deletions pkg/types/datum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,16 +527,23 @@ func prepareCompareDatums() ([]Datum, []Datum) {

func TestStringToMysqlBit(t *testing.T) {
tests := []struct {
a Datum
out []byte
a Datum
out []byte
flen int
truncated bool
}{
{NewStringDatum("true"), []byte{1}},
{NewStringDatum("false"), []byte{0}},
{NewStringDatum("1"), []byte{1}},
{NewStringDatum("0"), []byte{0}},
{NewStringDatum("b'1'"), []byte{1}},
{NewStringDatum("b'0'"), []byte{0}},
}
{NewStringDatum("true"), []byte{1}, 1, true},
{NewStringDatum("true"), []byte{0x74, 0x72, 0x75, 0x65}, 32, false},
{NewStringDatum("false"), []byte{0x1}, 1, true},
{NewStringDatum("false"), []byte{0x66, 0x61, 0x6c, 0x73, 0x65}, 40, false},
{NewStringDatum("1"), []byte{1}, 1, true},
{NewStringDatum("1"), []byte{0x31}, 8, false},
{NewStringDatum("0"), []byte{1}, 1, true},
{NewStringDatum("0"), []byte{0x30}, 8, false},
{NewStringDatum("b'1'"), []byte{0x62, 0x27, 0x31, 0x27}, 32, false},
{NewStringDatum("b'0'"), []byte{0x62, 0x27, 0x30, 0x27}, 32, false},
}
<<<<<<< HEAD
sc := stmtctx.NewStmtCtx()
sc.IgnoreTruncate.Store(true)
tp := NewFieldType(mysql.TypeBit)
Expand All @@ -545,6 +552,21 @@ func TestStringToMysqlBit(t *testing.T) {
bin, err := tt.a.convertToMysqlBit(nil, tp)
require.NoError(t, err)
require.Equal(t, tt.out, bin.b)
=======
for _, tt := range tests {
t.Run(fmt.Sprintf("%s %d %t", tt.a.GetString(), tt.flen, tt.truncated), func(t *testing.T) {
tp := NewFieldType(mysql.TypeBit)
tp.SetFlen(tt.flen)

bin, err := tt.a.convertToMysqlBit(DefaultStmtNoWarningContext, tp)
if tt.truncated {
require.Contains(t, err.Error(), "Data Too Long")
} else {
require.NoError(t, err)
}
require.Equal(t, tt.out, bin.b)
})
>>>>>>> 95b04c76703 (table: fix the issue that the default value for `BIT` column is wrong (#57303))
}
}

Expand Down
12 changes: 12 additions & 0 deletions tests/integrationtest/r/ddl/column.result
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,15 @@ t CREATE TABLE `t` (
`a` decimal(10,0) DEFAULT NULL,
`b` decimal(10,0) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
drop table if exists t;
create table t(a bit(2) default b'111');
Error 1067 (42000): Invalid default value for 'a'
create table t(a bit(65) default b'111');
Error 1439 (42000): Display width out of range for column 'a' (max = 64)
create table t(a bit(64) default b'1111111111111111111111111111111111111111111111111111111111111111');
drop table t;
create table t(a bit(3) default b'111');
drop table t;
create table t(a bit(3) default b'000111');
drop table t;
create table t(a bit(32) default b'1111111111111111111111111111111');
6 changes: 6 additions & 0 deletions tests/integrationtest/r/table/tables.result
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ select count(distinct(_tidb_rowid>>48)) from shard_t;
count(distinct(_tidb_rowid>>48))
4
set @@tidb_shard_allocate_step=default;
drop table if exists t;
create table t(a bit(32) default b'1100010001001110011000100100111');
insert into t values ();
select hex(a) from t;
hex(a)
62273127
14 changes: 14 additions & 0 deletions tests/integrationtest/t/ddl/column.test
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,17 @@ show create table t2;
drop table if exists t;
create table t(a decimal(0,0), b decimal(0));
show create table t;

# TestTooLongDefaultValueForBit
drop table if exists t;
-- error 1067
create table t(a bit(2) default b'111');
-- error 1439
create table t(a bit(65) default b'111');
create table t(a bit(64) default b'1111111111111111111111111111111111111111111111111111111111111111');
drop table t;
create table t(a bit(3) default b'111');
drop table t;
create table t(a bit(3) default b'000111');
drop table t;
create table t(a bit(32) default b'1111111111111111111111111111111');
6 changes: 6 additions & 0 deletions tests/integrationtest/t/table/tables.test
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ set @@tidb_shard_allocate_step=3;
insert into shard_t values (1), (2), (3), (4), (5), (6), (7), (8), (9), (10), (11);
select count(distinct(_tidb_rowid>>48)) from shard_t;
set @@tidb_shard_allocate_step=default;

# TestInsertBitDefaultValue
drop table if exists t;
create table t(a bit(32) default b'1100010001001110011000100100111');
insert into t values ();
select hex(a) from t;

0 comments on commit 6f3f76c

Please sign in to comment.