Skip to content

Commit 2ec3e3e

Browse files
authored
table: Revert "table: fix the issue that the default value for BIT column is wrong (#57303)" (#59768)
1 parent 0f05777 commit 2ec3e3e

File tree

5 files changed

+49
-69
lines changed

5 files changed

+49
-69
lines changed

ddl/ddl_api.go

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -961,20 +961,6 @@ func checkColumnDefaultValue(ctx sessionctx.Context, col *table.Column, value in
961961
}
962962
}
963963
}
964-
if value != nil && col.GetType() == mysql.TypeBit {
965-
v, ok := value.(string)
966-
if !ok {
967-
return hasDefaultValue, value, types.ErrInvalidDefault.GenWithStackByArgs(col.Name.O)
968-
}
969-
970-
uintVal, err := types.BinaryLiteral(v).ToInt(ctx.GetSessionVars().StmtCtx)
971-
if err != nil {
972-
return hasDefaultValue, value, types.ErrInvalidDefault.GenWithStackByArgs(col.Name.O)
973-
}
974-
if col.GetFlen() < 64 && uintVal >= 1<<(uint64(col.GetFlen())) {
975-
return hasDefaultValue, value, types.ErrInvalidDefault.GenWithStackByArgs(col.Name.O)
976-
}
977-
}
978964
return hasDefaultValue, value, nil
979965
}
980966

@@ -4506,14 +4492,13 @@ func SetDefaultValue(ctx sessionctx.Context, col *table.Column, option *ast.Colu
45064492
}
45074493
col.DefaultIsExpr = isSeqExpr
45084494
}
4509-
if !col.DefaultIsExpr {
4510-
if hasDefaultValue, value, err = checkColumnDefaultValue(ctx, col, value); err != nil {
4511-
return hasDefaultValue, errors.Trace(err)
4512-
}
4513-
value, err = convertTimestampDefaultValToUTC(ctx, value, col)
4514-
if err != nil {
4515-
return hasDefaultValue, errors.Trace(err)
4516-
}
4495+
4496+
if hasDefaultValue, value, err = checkColumnDefaultValue(ctx, col, value); err != nil {
4497+
return hasDefaultValue, errors.Trace(err)
4498+
}
4499+
value, err = convertTimestampDefaultValToUTC(ctx, value, col)
4500+
if err != nil {
4501+
return hasDefaultValue, errors.Trace(err)
45174502
}
45184503
err = setDefaultValueWithBinaryPadding(col, value)
45194504
if err != nil {

ddl/integration_test.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -156,20 +156,3 @@ func TestExchangePartitionAfterDropForeignKey(t *testing.T) {
156156
tk.MustExec("alter table child drop foreign key fk_1;")
157157
tk.MustExec("alter table child_with_partition exchange partition p1 with table child;")
158158
}
159-
160-
func TestTooLongDefaultValueForBit(t *testing.T) {
161-
store := testkit.CreateMockStore(t)
162-
tk := testkit.NewTestKit(t, store)
163-
164-
tk.MustExec("use test;")
165-
166-
tk.MustGetErrCode("create table t(a bit(2) default b'111');", 1067)
167-
tk.MustGetErrCode("create table t(a bit(65) default b'111');", 1439)
168-
tk.MustExec("create table t(a bit(64) default b'1111111111111111111111111111111111111111111111111111111111111111');")
169-
tk.MustExec("drop table t")
170-
tk.MustExec("create table t(a bit(3) default b'111');")
171-
tk.MustExec("drop table t")
172-
tk.MustExec("create table t(a bit(3) default b'000111');")
173-
tk.MustExec("drop table t;")
174-
tk.MustExec("create table t(a bit(32) default b'1111111111111111111111111111111');")
175-
}

executor/write_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1930,7 +1930,7 @@ func TestIssue18681(t *testing.T) {
19301930
tk := testkit.NewTestKit(t, store)
19311931
tk.MustExec("use test")
19321932
createSQL := `drop table if exists load_data_test;
1933-
create table load_data_test (a bit(1),b bit(1),c bit(1),d bit(1),e bit(32),f bit(1));`
1933+
create table load_data_test (a bit(1),b bit(1),c bit(1),d bit(1));`
19341934
tk.MustExec(createSQL)
19351935
tk.MustExec("load data local infile '/tmp/nonexistence.csv' ignore into table load_data_test")
19361936
ctx := tk.Session().(sessionctx.Context)
@@ -1940,7 +1940,7 @@ func TestIssue18681(t *testing.T) {
19401940
require.NotNil(t, ld)
19411941

19421942
deleteSQL := "delete from load_data_test"
1943-
selectSQL := "select bin(a), bin(b), bin(c), bin(d), bin(e), bin(f) from load_data_test;"
1943+
selectSQL := "select bin(a), bin(b), bin(c), bin(d) from load_data_test;"
19441944
ctx.GetSessionVars().StmtCtx.DupKeyAsWarning = true
19451945
ctx.GetSessionVars().StmtCtx.BadNullAsWarning = true
19461946
ld.SetMaxRowsInBatch(20000)
@@ -1952,7 +1952,7 @@ func TestIssue18681(t *testing.T) {
19521952
}()
19531953
sc.IgnoreTruncate = false
19541954
tests := []testCase{
1955-
{nil, []byte("true\tfalse\t0\t1\tb'1'\tb'1'\n"), []string{"1|1|1|1|1100010001001110011000100100111|1"}, nil, "Records: 1 Deleted: 0 Skipped: 0 Warnings: 5"},
1955+
{nil, []byte("true\tfalse\t0\t1\n"), []string{"1|0|0|1"}, nil, "Records: 1 Deleted: 0 Skipped: 0 Warnings: 0"},
19561956
}
19571957
checkCases(tests, ld, t, tk, ctx, selectSQL, deleteSQL)
19581958
require.Equal(t, uint16(0), sc.WarningCount())

types/datum.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1559,13 +1559,38 @@ func (d *Datum) ConvertToMysqlYear(sc *stmtctx.StatementContext, target *FieldTy
15591559
return ret, errors.Trace(err)
15601560
}
15611561

1562+
func (d *Datum) convertStringToMysqlBit(sc *stmtctx.StatementContext) (uint64, error) {
1563+
bitStr, err := ParseBitStr(BinaryLiteral(d.b).ToString())
1564+
if err != nil {
1565+
// It cannot be converted to bit type, so we need to convert it to int type.
1566+
return BinaryLiteral(d.b).ToInt(sc)
1567+
}
1568+
return bitStr.ToInt(sc)
1569+
}
1570+
15621571
func (d *Datum) convertToMysqlBit(sc *stmtctx.StatementContext, target *FieldType) (Datum, error) {
15631572
var ret Datum
15641573
var uintValue uint64
15651574
var err error
15661575
switch d.k {
1567-
case KindString, KindBytes:
1576+
case KindBytes:
15681577
uintValue, err = BinaryLiteral(d.b).ToInt(sc)
1578+
case KindString:
1579+
// For single bit value, we take string like "true", "1" as 1, and "false", "0" as 0,
1580+
// this behavior is not documented in MySQL, but it behaves so, for more information, see issue #18681
1581+
s := BinaryLiteral(d.b).ToString()
1582+
if target.GetFlen() == 1 {
1583+
switch strings.ToLower(s) {
1584+
case "true", "1":
1585+
uintValue = 1
1586+
case "false", "0":
1587+
uintValue = 0
1588+
default:
1589+
uintValue, err = d.convertStringToMysqlBit(sc)
1590+
}
1591+
} else {
1592+
uintValue, err = d.convertStringToMysqlBit(sc)
1593+
}
15691594
case KindInt64:
15701595
// if input kind is int64 (signed), when trans to bit, we need to treat it as unsigned
15711596
d.k = KindUint64

types/datum_test.go

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -528,37 +528,24 @@ func prepareCompareDatums() ([]Datum, []Datum) {
528528

529529
func TestStringToMysqlBit(t *testing.T) {
530530
tests := []struct {
531-
a Datum
532-
out []byte
533-
flen int
534-
truncated bool
531+
a Datum
532+
out []byte
535533
}{
536-
{NewStringDatum("true"), []byte{1}, 1, true},
537-
{NewStringDatum("true"), []byte{0x74, 0x72, 0x75, 0x65}, 32, false},
538-
{NewStringDatum("false"), []byte{0x1}, 1, true},
539-
{NewStringDatum("false"), []byte{0x66, 0x61, 0x6c, 0x73, 0x65}, 40, false},
540-
{NewStringDatum("1"), []byte{1}, 1, true},
541-
{NewStringDatum("1"), []byte{0x31}, 8, false},
542-
{NewStringDatum("0"), []byte{1}, 1, true},
543-
{NewStringDatum("0"), []byte{0x30}, 8, false},
544-
{NewStringDatum("b'1'"), []byte{0x62, 0x27, 0x31, 0x27}, 32, false},
545-
{NewStringDatum("b'0'"), []byte{0x62, 0x27, 0x30, 0x27}, 32, false},
534+
{NewStringDatum("true"), []byte{1}},
535+
{NewStringDatum("false"), []byte{0}},
536+
{NewStringDatum("1"), []byte{1}},
537+
{NewStringDatum("0"), []byte{0}},
538+
{NewStringDatum("b'1'"), []byte{1}},
539+
{NewStringDatum("b'0'"), []byte{0}},
546540
}
547541
sc := new(stmtctx.StatementContext)
548542
sc.IgnoreTruncate = true
543+
tp := NewFieldType(mysql.TypeBit)
544+
tp.SetFlen(1)
549545
for _, tt := range tests {
550-
t.Run(fmt.Sprintf("%s %d %t", tt.a.GetString(), tt.flen, tt.truncated), func(t *testing.T) {
551-
tp := NewFieldType(mysql.TypeBit)
552-
tp.SetFlen(tt.flen)
553-
554-
bin, err := tt.a.convertToMysqlBit(sc, tp)
555-
if tt.truncated {
556-
require.Contains(t, err.Error(), "Data Too Long")
557-
} else {
558-
require.NoError(t, err)
559-
}
560-
require.Equal(t, tt.out, bin.b)
561-
})
546+
bin, err := tt.a.convertToMysqlBit(nil, tp)
547+
require.NoError(t, err)
548+
require.Equal(t, tt.out, bin.b)
562549
}
563550
}
564551

0 commit comments

Comments
 (0)