From c137da6b0a6d2cdf6db834fcbc7e591a4559a81f Mon Sep 17 00:00:00 2001 From: Chunzhu Li Date: Fri, 10 Jul 2020 13:14:46 +0800 Subject: [PATCH] fix unique key null bug --- tests/null_unique_index/run.sh | 24 ++++++++++++++++++++++++ v4/export/ir_impl.go | 6 +++++- 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 tests/null_unique_index/run.sh diff --git a/tests/null_unique_index/run.sh b/tests/null_unique_index/run.sh new file mode 100644 index 00000000..6b67baac --- /dev/null +++ b/tests/null_unique_index/run.sh @@ -0,0 +1,24 @@ +#!/bin/sh + +set -eu +cur=$(cd `dirname $0`; pwd) + +DB_NAME="null_unique_key" + +# drop database on mysql +run_sql "drop database if exists \`$DB_NAME\`;" + +# build data on mysql +run_sql "create database \`$DB_NAME\`;" +run_sql "create table \`$DB_NAME\`.\`t\` (a int unique key, b int);" +run_sql "insert into \`$DB_NAME\`.\`t\` values (1, 2), (NULL, 1);" + + +# dumping +export DUMPLING_TEST_DATABASE=$DB_NAME +run_dumpling -r 1 + +data="NULL" +cnt=$(sed "s/$data/$data\n/g" $DUMPLING_OUTPUT_DIR/$DB_NAME.t.1.sql | grep -c "$data") || true +[ $cnt = 1 ] + diff --git a/v4/export/ir_impl.go b/v4/export/ir_impl.go index cc50494c..321ca549 100644 --- a/v4/export/ir_impl.go +++ b/v4/export/ir_impl.go @@ -323,16 +323,20 @@ func splitTableDataIntoChunks( } chunkIndex := 0 + nullValueCondition := fmt.Sprintf("`%s` IS NULL OR ", escapeString(field)) LOOP: for cutoff <= max { chunkIndex += 1 - where := fmt.Sprintf("(`%s` >= %d AND `%s` < %d)", escapeString(field), cutoff, escapeString(field), cutoff+estimatedStep) + where := fmt.Sprintf("%s(`%s` >= %d AND `%s` < %d)", nullValueCondition, escapeString(field), cutoff, escapeString(field), cutoff+estimatedStep) query = buildSelectQuery(dbName, tableName, selectedField, buildWhereCondition(conf, where), orderByClause) rows, err := db.Query(query) if err != nil { errCh <- errors.WithMessage(err, query) return } + if len(nullValueCondition) > 0 { + nullValueCondition = "" + } td := &tableData{ database: dbName,