Skip to content
This repository has been archived by the owner on Aug 21, 2023. It is now read-only.

Commit

Permalink
fix unique key null bug (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
lichunzhu authored Jul 14, 2020
1 parent 7365129 commit 539f93b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
24 changes: 24 additions & 0 deletions tests/null_unique_index/run.sh
Original file line number Diff line number Diff line change
@@ -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 ]

6 changes: 5 additions & 1 deletion v4/export/ir_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 539f93b

Please sign in to comment.