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

Commit

Permalink
sql: fix where condition without brackets problem (#372) (#376)
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot authored Oct 15, 2021
1 parent 3a8376f commit beddf63
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
21 changes: 21 additions & 0 deletions tests/basic/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,27 @@ expected=$(seq 3 9)
echo "expected ${expected}, actual ${actual}"
[ "$actual" = "$expected" ]

# Test for OR WHERE case. Better dump MySQL here because Dumpling has some special handle for concurrently dump TiDB tables.
export DUMPLING_TEST_PORT=3306
run_sql "drop database if exists \`$DB_NAME\`;"
run_sql "create database \`$DB_NAME\`;"
run_sql "create table \`$DB_NAME\`.\`$TABLE_NAME\` (a int primary key, b int);"

seq 0 99 | xargs -I_ run_sql "insert into \`$DB_NAME\`.\`$TABLE_NAME\` (a,b) values (_, 99-_);"
run_sql "analyze table \`$DB_NAME\`.\`$TABLE_NAME\`;"
run_dumpling --where "b <= 4 or b >= 95" -f "$DB_NAME.$TABLE_NAME" --rows 10

actual=$(grep -w "(.*)" ${DUMPLING_OUTPUT_DIR}/${DB_NAME}.${TABLE_NAME}.000000000.sql | cut -c2-2)
expected=$(seq 0 4)
echo "expected ${DUMPLING_OUTPUT_DIR}/${DB_NAME}.${TABLE_NAME}.000000000.sql ${expected}, actual ${actual}"
[ "$actual" = "$expected" ]
actual=$(grep -w "(.*)" ${DUMPLING_OUTPUT_DIR}/${DB_NAME}.${TABLE_NAME}.000000009.sql | cut -c2-3)
expected=$(seq 95 99)
echo "expected ${DUMPLING_OUTPUT_DIR}/${DB_NAME}.${TABLE_NAME}.000000009.sql ${expected}, actual ${actual}"
[ "$actual" = "$expected" ]

seq 1 8 | xargs -I\? file_not_exist ${DUMPLING_OUTPUT_DIR}/${DB_NAME}.${TABLE_NAME}.00000000\?.sql

# Test for specifying --filetype sql with --sql, should report an error
set +e
run_dumpling --sql "select * from \`$DB_NAME\`.\`$TABLE_NAME\`" --filetype sql > ${DUMPLING_OUTPUT_DIR}/dumpling.log
Expand Down
14 changes: 10 additions & 4 deletions v4/export/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -1070,18 +1070,24 @@ func parseSnapshotToTSO(pool *sql.DB, snapshot string) (uint64, error) {
func buildWhereCondition(conf *Config, where string) string {
var query strings.Builder
separator := "WHERE"
leftBracket := " "
rightBracket := " "
if conf.Where != "" && where != "" {
leftBracket = " ("
rightBracket = ") "
}
if conf.Where != "" {
query.WriteString(separator)
query.WriteByte(' ')
query.WriteString(leftBracket)
query.WriteString(conf.Where)
query.WriteByte(' ')
query.WriteString(rightBracket)
separator = "AND"
query.WriteByte(' ')
}
if where != "" {
query.WriteString(separator)
query.WriteString(" ")
query.WriteString(leftBracket)
query.WriteString(where)
query.WriteString(rightBracket)
}
return query.String()
}
Expand Down
37 changes: 37 additions & 0 deletions v4/export/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,43 @@ func TestBuildPartitionClauses(t *testing.T) {
}
}

func TestBuildWhereCondition(t *testing.T) {
t.Parallel()

conf := DefaultConfig()
testCases := []struct {
confWhere string
chunkWhere string
expectedWhere string
}{
{
"",
"",
"",
},
{
"a >= 1000000 and a <= 2000000",
"",
"WHERE a >= 1000000 and a <= 2000000 ",
},
{
"",
"(`a`>1 and `a`<3)or(`a`=1 and(`b`>=2))or(`a`=3 and(`b`<4))",
"WHERE (`a`>1 and `a`<3)or(`a`=1 and(`b`>=2))or(`a`=3 and(`b`<4)) ",
},
{
"a >= 1000000 and a <= 2000000",
"(`a`>1 and `a`<3)or(`a`=1 and(`b`>=2))or(`a`=3 and(`b`<4))",
"WHERE (a >= 1000000 and a <= 2000000) AND ((`a`>1 and `a`<3)or(`a`=1 and(`b`>=2))or(`a`=3 and(`b`<4))) ",
},
}
for _, testCase := range testCases {
conf.Where = testCase.confWhere
where := buildWhereCondition(conf, testCase.chunkWhere)
require.Equal(t, testCase.expectedWhere, where)
}
}

func TestBuildRegionQueriesWithoutPartition(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit beddf63

Please sign in to comment.