Skip to content

Commit 785688e

Browse files
authored
table: fix the issue that the default value for BIT column is wrong (#57303) (#57357)
close #57301, close #57312
1 parent 460e050 commit 785688e

File tree

5 files changed

+70
-49
lines changed

5 files changed

+70
-49
lines changed

ddl/ddl_api.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,20 @@ func checkColumnDefaultValue(ctx sessionctx.Context, col *table.Column, value in
829829
}
830830
}
831831
}
832+
if value != nil && col.GetType() == mysql.TypeBit {
833+
v, ok := value.(string)
834+
if !ok {
835+
return hasDefaultValue, value, types.ErrInvalidDefault.GenWithStackByArgs(col.Name.O)
836+
}
837+
838+
uintVal, err := types.BinaryLiteral(v).ToInt(ctx.GetSessionVars().StmtCtx)
839+
if err != nil {
840+
return hasDefaultValue, value, types.ErrInvalidDefault.GenWithStackByArgs(col.Name.O)
841+
}
842+
if col.GetFlen() < 64 && uintVal >= 1<<(uint64(col.GetFlen())) {
843+
return hasDefaultValue, value, types.ErrInvalidDefault.GenWithStackByArgs(col.Name.O)
844+
}
845+
}
832846
return hasDefaultValue, value, nil
833847
}
834848

@@ -4281,13 +4295,14 @@ func setDefaultValue(ctx sessionctx.Context, col *table.Column, option *ast.Colu
42814295
}
42824296
col.DefaultIsExpr = isSeqExpr
42834297
}
4284-
4285-
if hasDefaultValue, value, err = checkColumnDefaultValue(ctx, col, value); err != nil {
4286-
return hasDefaultValue, errors.Trace(err)
4287-
}
4288-
value, err = convertTimestampDefaultValToUTC(ctx, value, col)
4289-
if err != nil {
4290-
return hasDefaultValue, errors.Trace(err)
4298+
if !col.DefaultIsExpr {
4299+
if hasDefaultValue, value, err = checkColumnDefaultValue(ctx, col, value); err != nil {
4300+
return hasDefaultValue, errors.Trace(err)
4301+
}
4302+
value, err = convertTimestampDefaultValToUTC(ctx, value, col)
4303+
if err != nil {
4304+
return hasDefaultValue, errors.Trace(err)
4305+
}
42914306
}
42924307
err = setDefaultValueWithBinaryPadding(col, value)
42934308
if err != nil {

ddl/integration_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,21 @@ func TestDDLOnCachedTable(t *testing.T) {
144144
tk.MustExec("alter table t nocache;")
145145
tk.MustExec("drop table if exists t;")
146146
}
147+
148+
func TestTooLongDefaultValueForBit(t *testing.T) {
149+
store, clean := testkit.CreateMockStore(t)
150+
defer clean()
151+
tk := testkit.NewTestKit(t, store)
152+
153+
tk.MustExec("use test;")
154+
155+
tk.MustGetErrCode("create table t(a bit(2) default b'111');", 1067)
156+
tk.MustGetErrCode("create table t(a bit(65) default b'111');", 1439)
157+
tk.MustExec("create table t(a bit(64) default b'1111111111111111111111111111111111111111111111111111111111111111');")
158+
tk.MustExec("drop table t")
159+
tk.MustExec("create table t(a bit(3) default b'111');")
160+
tk.MustExec("drop table t")
161+
tk.MustExec("create table t(a bit(3) default b'000111');")
162+
tk.MustExec("drop table t;")
163+
tk.MustExec("create table t(a bit(32) default b'1111111111111111111111111111111');")
164+
}

executor/write_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1947,7 +1947,7 @@ func TestIssue18681(t *testing.T) {
19471947
tk := testkit.NewTestKit(t, store)
19481948
tk.MustExec("use test")
19491949
createSQL := `drop table if exists load_data_test;
1950-
create table load_data_test (a bit(1),b bit(1),c bit(1),d bit(1));`
1950+
create table load_data_test (a bit(1),b bit(1),c bit(1),d bit(1),e bit(32),f bit(1));`
19511951
tk.MustExec(createSQL)
19521952
tk.MustExec("load data local infile '/tmp/nonexistence.csv' ignore into table load_data_test")
19531953
ctx := tk.Session().(sessionctx.Context)
@@ -1957,7 +1957,7 @@ func TestIssue18681(t *testing.T) {
19571957
require.NotNil(t, ld)
19581958

19591959
deleteSQL := "delete from load_data_test"
1960-
selectSQL := "select bin(a), bin(b), bin(c), bin(d) from load_data_test;"
1960+
selectSQL := "select bin(a), bin(b), bin(c), bin(d), bin(e), bin(f) from load_data_test;"
19611961
ctx.GetSessionVars().StmtCtx.DupKeyAsWarning = true
19621962
ctx.GetSessionVars().StmtCtx.BadNullAsWarning = true
19631963
ld.SetMaxRowsInBatch(20000)
@@ -1969,7 +1969,7 @@ func TestIssue18681(t *testing.T) {
19691969
}()
19701970
sc.IgnoreTruncate = false
19711971
tests := []testCase{
1972-
{nil, []byte("true\tfalse\t0\t1\n"), []string{"1|0|0|1"}, nil, "Records: 1 Deleted: 0 Skipped: 0 Warnings: 0"},
1972+
{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"},
19731973
}
19741974
checkCases(tests, ld, t, tk, ctx, selectSQL, deleteSQL)
19751975
require.Equal(t, uint16(0), sc.WarningCount())

types/datum.go

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,38 +1553,13 @@ func (d *Datum) ConvertToMysqlYear(sc *stmtctx.StatementContext, target *FieldTy
15531553
return ret, errors.Trace(err)
15541554
}
15551555

1556-
func (d *Datum) convertStringToMysqlBit(sc *stmtctx.StatementContext) (uint64, error) {
1557-
bitStr, err := ParseBitStr(BinaryLiteral(d.b).ToString())
1558-
if err != nil {
1559-
// It cannot be converted to bit type, so we need to convert it to int type.
1560-
return BinaryLiteral(d.b).ToInt(sc)
1561-
}
1562-
return bitStr.ToInt(sc)
1563-
}
1564-
15651556
func (d *Datum) convertToMysqlBit(sc *stmtctx.StatementContext, target *FieldType) (Datum, error) {
15661557
var ret Datum
15671558
var uintValue uint64
15681559
var err error
15691560
switch d.k {
1570-
case KindBytes:
1561+
case KindString, KindBytes:
15711562
uintValue, err = BinaryLiteral(d.b).ToInt(sc)
1572-
case KindString:
1573-
// For single bit value, we take string like "true", "1" as 1, and "false", "0" as 0,
1574-
// this behavior is not documented in MySQL, but it behaves so, for more information, see issue #18681
1575-
s := BinaryLiteral(d.b).ToString()
1576-
if target.GetFlen() == 1 {
1577-
switch strings.ToLower(s) {
1578-
case "true", "1":
1579-
uintValue = 1
1580-
case "false", "0":
1581-
uintValue = 0
1582-
default:
1583-
uintValue, err = d.convertStringToMysqlBit(sc)
1584-
}
1585-
} else {
1586-
uintValue, err = d.convertStringToMysqlBit(sc)
1587-
}
15881563
case KindInt64:
15891564
// if input kind is int64 (signed), when trans to bit, we need to treat it as unsigned
15901565
d.k = KindUint64

types/datum_test.go

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

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

0 commit comments

Comments
 (0)