From e87252008ccaa6ae40c7585ccadd3285c9d8d363 Mon Sep 17 00:00:00 2001 From: amyangfei Date: Sat, 25 Sep 2021 18:20:46 +0800 Subject: [PATCH] tests: add test case to cover varchar default value (#2799) --- tests/multi_source/main.go | 49 +++++++++++++++++++++++++++++++-- tests/row_format/data/step3.sql | 27 +++++++++++++++++- tests/row_format/data/step4.sql | 2 ++ 3 files changed, 74 insertions(+), 4 deletions(-) diff --git a/tests/multi_source/main.go b/tests/multi_source/main.go index aa5ffdd1c4a..e6208efa94c 100644 --- a/tests/multi_source/main.go +++ b/tests/multi_source/main.go @@ -21,15 +21,15 @@ import ( "os" "reflect" "runtime" + "strconv" "strings" "sync" "time" - "go.uber.org/zap" - "github.com/pingcap/errors" "github.com/pingcap/log" "github.com/pingcap/ticdc/tests/util" + "go.uber.org/zap" ) func main() { @@ -79,7 +79,7 @@ func runDDLTest(srcs []*sql.DB) { }() for i, ddlFunc := range []func(context.Context, *sql.DB){ - createDropSchemaDDL, truncateDDL, addDropColumnDDL, + createDropSchemaDDL, truncateDDL, addDropColumnDDL, addDropColumnDDL2, modifyColumnDDL, addDropIndexDDL, } { testName := getFunctionName(ddlFunc) @@ -282,6 +282,49 @@ func addDropColumnDDL(ctx context.Context, db *sql.DB) { } } +// addDropColumnDDL2 tests the following scenario: +// 1. Create a table, executing DML of this table in background +// 2. alter table add column v1 varchar(20) default "xxx" not null +// 3. Some rows could have no data for column v1, however when we decode them, +// we could use the new table schema (which has the column of v1, caused by +// online DDL). Since the column data doesn't exist, the tidb library will +// fill in a default value, which is a string type. That's why we can get +// either []byte or a string of a column.Value +func addDropColumnDDL2(ctx context.Context, db *sql.DB) { + testName := getFunctionName(addDropColumnDDL2) + mustCreateTable(db, testName) + + for value := 1; ; value++ { + select { + case <-ctx.Done(): + return + default: + } + sql := fmt.Sprintf("alter table test.`%s` drop column v1", testName) + util.MustExec(db, sql) + time.Sleep(100 * time.Millisecond) + + var notNULL string + var defaultValue interface{} + + strValue := strconv.Itoa(value) + if value%5 == 0 { + // use default + defaultValue = strValue + } else if value%5 == 1 { + // use default null + defaultValue = nil + } else { + // use default not null + notNULL = "not null" + defaultValue = strValue + } + sql = fmt.Sprintf("alter table test.`%s` add column v1 varchar(20) default ? %s", testName, notNULL) + util.MustExec(db, sql, defaultValue) + time.Sleep(100 * time.Millisecond) + } +} + func modifyColumnDDL(ctx context.Context, db *sql.DB) { testName := getFunctionName(modifyColumnDDL) mustCreateTable(db, testName) diff --git a/tests/row_format/data/step3.sql b/tests/row_format/data/step3.sql index 28594b9f874..8b9ca4b0ef9 100644 --- a/tests/row_format/data/step3.sql +++ b/tests/row_format/data/step3.sql @@ -51,6 +51,25 @@ create table tp_text primary key (id) ); +create table tp_text2 +( + id int auto_increment, + c_tinytext tinytext null, + c_text text null, + c_mediumtext mediumtext null, + c_longtext longtext null, + c_varchar varchar(16) default "a", + c_char char(16) default "a", + c_tinyblob tinyblob null, + c_blob blob null, + c_mediumblob mediumblob null, + c_longblob longblob null, + c_binary binary(16) default '0xa', + c_varbinary varbinary(16) default '0xa', + constraint pk + primary key (id) +); + create table tp_time ( id int auto_increment, @@ -105,7 +124,13 @@ insert into tp_text(c_tinytext, c_text, c_mediumtext, c_longtext, c_varchar, c_c c_longblob, c_binary, c_varbinary) values ('89504E470D0A1A0A', '89504E470D0A1A0A', '89504E470D0A1A0A', '89504E470D0A1A0A', '89504E470D0A1A0A', '89504E470D0A1A0A', x'89504E470D0A1A0A', x'89504E470D0A1A0A', x'89504E470D0A1A0A' - , x'89504E470D0A1A0A', x'89504E470D0A1A0A', x'89504E470D0A1A0A'); + , x'89504E470D0A1A0A', x'89504E470D0A1A0A', x'89504E470D0A1A0A'), +('', '', '', '', '', '', x'89504E470D0A1A0A', x'89504E470D0A1A0A', x'89504E470D0A1A0A', + x'89504E470D0A1A0A', x'89504E470D0A1A0A', x'89504E470D0A1A0A'), +('89504E470D0A1A0A', '89504E470D0A1A0A', '89504E470D0A1A0A', '89504E470D0A1A0A', '89504E470D0A1A0A', '89504E470D0A1A0A', x'', x'', x'', + x'', x'', x''); + +insert into tp_text2() values(); insert into tp_time() values (); diff --git a/tests/row_format/data/step4.sql b/tests/row_format/data/step4.sql index 4ee73aa9769..f44d9fb9da9 100644 --- a/tests/row_format/data/step4.sql +++ b/tests/row_format/data/step4.sql @@ -23,6 +23,8 @@ values ('89504E470D0A1A0A', '89504E470D0A1A0A', '89504E470D0A1A0A', '89504E470D0 '89504E470D0A1A0A', x'89504E470D0A1A0A', x'89504E470D0A1A0A', x'89504E470D0A1A0A' , x'89504E470D0A1A0A', x'89504E470D0A1A0A', x'89504E470D0A1A0A'); +insert into tp_text2() values(); + insert into tp_time() values ();