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 ae4703a commit 3e2eba9
Show file tree
Hide file tree
Showing 8 changed files with 2,136 additions and 8 deletions.
1,327 changes: 1,327 additions & 0 deletions pkg/ddl/add_column.go

Large diffs are not rendered by default.

550 changes: 550 additions & 0 deletions pkg/executor/test/writetest/write_test.go

Large diffs are not rendered by default.

98 changes: 98 additions & 0 deletions tests/integrationtest/r/ddl/column.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
drop table if exists t1;
create table t1 (da date default '1962-03-03 23:33:34', dt datetime default '1962-03-03', ti time default '2020-10-11 12:23:23', ts timestamp default '2020-10-13 12:23:23');
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`da` date DEFAULT '1962-03-03',
`dt` datetime DEFAULT '1962-03-03 00:00:00',
`ti` time DEFAULT '12:23:23',
`ts` timestamp DEFAULT '2020-10-13 12:23:23'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
insert into t1 values();
select * from t1;
da dt ti ts
1962-03-03 1962-03-03 00:00:00 12:23:23 2020-10-13 12:23:23
alter table t1 add column da1 date default '2020-03-27 20:20:20 123456';
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`da` date DEFAULT '1962-03-03',
`dt` datetime DEFAULT '1962-03-03 00:00:00',
`ti` time DEFAULT '12:23:23',
`ts` timestamp DEFAULT '2020-10-13 12:23:23',
`da1` date DEFAULT '2020-03-27'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
select * from t1;
da dt ti ts da1
1962-03-03 1962-03-03 00:00:00 12:23:23 2020-10-13 12:23:23 2020-03-27
alter table t1 change ts da2 date default '2020-10-10 20:20:20';
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`da` date DEFAULT '1962-03-03',
`dt` datetime DEFAULT '1962-03-03 00:00:00',
`ti` time DEFAULT '12:23:23',
`da2` date DEFAULT '2020-10-10',
`da1` date DEFAULT '2020-03-27'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
select * from t1;
da dt ti da2 da1
1962-03-03 1962-03-03 00:00:00 12:23:23 2020-10-13 2020-03-27
drop table if exists t1, t2;
CREATE TABLE t1(id INTEGER PRIMARY KEY, authorId INTEGER AUTO_INCREMENT UNIQUE);
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`id` int NOT NULL,
`authorId` int NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,
UNIQUE KEY `authorId` (`authorId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
CREATE TABLE `t2`( `id` INTEGER PRIMARY KEY, `authorId` int(11) AUTO_INCREMENT, UNIQUE KEY `authorIdx` (`authorId`));
show create table t2;
Table Create Table
t2 CREATE TABLE `t2` (
`id` int NOT NULL,
`authorId` int NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,
UNIQUE KEY `authorIdx` (`authorId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
set @@sql_mode='';
drop table if exists t1;
create table t1(b tinyint default '11111111');
Error 1067 (42000): Invalid default value for 'b'
create table t1(b tinyint default '11abc');
Error 1067 (42000): Invalid default value for 'b'
create table t1(b datetime default '11abc');
Error 1067 (42000): Invalid default value for 'b'
create table t1(b date default '2024-10');
Error 1067 (42000): Invalid default value for 'b'
create table t1(a tinyint, b date default '2024-10-24 12:20');
alter table t1 add column(c tinyint default '11111111');
Error 1067 (42000): Invalid default value for 'c'
alter table t1 add column(c tinyint default '11abc');
Error 1067 (42000): Invalid default value for 'c'
alter table t1 add column(c datetime default '11abc');
Error 1067 (42000): Invalid default value for 'c'
alter table t1 add column d date default '2024-10';
Error 1067 (42000): Invalid default value for 'd'
drop table if exists t;
create table t(a decimal(0,0), b decimal(0));
show create table t;
Table Create Table
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');
32 changes: 32 additions & 0 deletions tests/integrationtest/r/table/tables.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
drop table if exists shard_t;
create table shard_t (a int) shard_row_id_bits = 15;
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;
count(distinct(_tidb_rowid>>48))
4
truncate table shard_t;
set @@tidb_shard_allocate_step=5;
begin;
insert into shard_t values (1), (2), (3);
insert into shard_t values (4), (5), (6);
insert into shard_t values (7), (8), (9);
insert into shard_t values (10);
commit;
select count(distinct(_tidb_rowid>>48)) from shard_t;
count(distinct(_tidb_rowid>>48))
2
truncate table shard_t;
insert into shard_t values (10);
insert into shard_t values (11);
insert into shard_t values (12);
select count(distinct(_tidb_rowid>>48)) from shard_t;
count(distinct(_tidb_rowid>>48))
3
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
60 changes: 60 additions & 0 deletions tests/integrationtest/t/ddl/column.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# TestGetDefaultValueOfColumn
drop table if exists t1;
create table t1 (da date default '1962-03-03 23:33:34', dt datetime default '1962-03-03', ti time default '2020-10-11 12:23:23', ts timestamp default '2020-10-13 12:23:23');
show create table t1;
insert into t1 values();
select * from t1;
alter table t1 add column da1 date default '2020-03-27 20:20:20 123456';
show create table t1;
select * from t1;
alter table t1 change ts da2 date default '2020-10-10 20:20:20';
show create table t1;
select * from t1;

# TestIssue39080
drop table if exists t1, t2;
CREATE TABLE t1(id INTEGER PRIMARY KEY, authorId INTEGER AUTO_INCREMENT UNIQUE);
show create table t1;
CREATE TABLE `t2`( `id` INTEGER PRIMARY KEY, `authorId` int(11) AUTO_INCREMENT, UNIQUE KEY `authorIdx` (`authorId`));
show create table t2;

# TestIssue52972
# Test creating columns with error defaults when sql_mode is empty.
set @@sql_mode='';
drop table if exists t1;
--error 1067
create table t1(b tinyint default '11111111');
--error 1067
create table t1(b tinyint default '11abc');
--error 1067
create table t1(b datetime default '11abc');
--error 1067
create table t1(b date default '2024-10');
create table t1(a tinyint, b date default '2024-10-24 12:20');
--error 1067
alter table t1 add column(c tinyint default '11111111');
--error 1067
alter table t1 add column(c tinyint default '11abc');
--error 1067
alter table t1 add column(c datetime default '11abc');
--error 1067
alter table t1 add column d date default '2024-10';

# TestIssue53779
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');
30 changes: 30 additions & 0 deletions tests/integrationtest/t/table/tables.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# TestShardRowIDBitsStep
drop table if exists shard_t;
create table shard_t (a int) shard_row_id_bits = 15;
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;

truncate table shard_t;
set @@tidb_shard_allocate_step=5;
begin;
insert into shard_t values (1), (2), (3);
insert into shard_t values (4), (5), (6);
insert into shard_t values (7), (8), (9);
insert into shard_t values (10);
commit;
select count(distinct(_tidb_rowid>>48)) from shard_t;

truncate table shard_t;
insert into shard_t values (10);
insert into shard_t values (11);
insert into shard_t values (12);
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;
9 changes: 9 additions & 0 deletions types/datum.go
Original file line number Diff line number Diff line change
Expand Up @@ -1559,6 +1559,7 @@ func (d *Datum) ConvertToMysqlYear(sc *stmtctx.StatementContext, target *FieldTy
return ret, errors.Trace(err)
}

<<<<<<< HEAD:types/datum.go
func (d *Datum) convertStringToMysqlBit(sc *stmtctx.StatementContext) (uint64, error) {
bitStr, err := ParseBitStr(BinaryLiteral(d.b).ToString())
if err != nil {
Expand All @@ -1569,10 +1570,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)):pkg/types/datum.go
var ret Datum
var uintValue uint64
var err error
switch d.k {
<<<<<<< HEAD:types/datum.go
case KindBytes:
uintValue, err = BinaryLiteral(d.b).ToInt(sc)
case KindString:
Expand All @@ -1591,6 +1596,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)):pkg/types/datum.go
case KindInt64:
// if input kind is int64 (signed), when trans to bit, we need to treat it as unsigned
d.k = KindUint64
Expand Down
38 changes: 30 additions & 8 deletions types/datum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,16 +528,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:types/datum_test.go
sc := new(stmtctx.StatementContext)
sc.IgnoreTruncate = true
tp := NewFieldType(mysql.TypeBit)
Expand All @@ -546,6 +553,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)):pkg/types/datum_test.go
}
}

Expand Down

0 comments on commit 3e2eba9

Please sign in to comment.