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

Commit 7db623e

Browse files
authored
sql: fix where condition without brackets problem (#372) (#374)
1 parent 73d17ae commit 7db623e

File tree

3 files changed

+68
-4
lines changed

3 files changed

+68
-4
lines changed

tests/basic/run.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,27 @@ expected=$(seq 3 9)
4040
echo "expected ${expected}, actual ${actual}"
4141
[ "$actual" = "$expected" ]
4242

43+
# Test for OR WHERE case. Better dump MySQL here because Dumpling has some special handle for concurrently dump TiDB tables.
44+
export DUMPLING_TEST_PORT=3306
45+
run_sql "drop database if exists \`$DB_NAME\`;"
46+
run_sql "create database \`$DB_NAME\`;"
47+
run_sql "create table \`$DB_NAME\`.\`$TABLE_NAME\` (a int primary key, b int);"
48+
49+
seq 0 99 | xargs -I_ run_sql "insert into \`$DB_NAME\`.\`$TABLE_NAME\` (a,b) values (_, 99-_);"
50+
run_sql "analyze table \`$DB_NAME\`.\`$TABLE_NAME\`;"
51+
run_dumpling --where "b <= 4 or b >= 95" -f "$DB_NAME.$TABLE_NAME" --rows 10
52+
53+
actual=$(grep -w "(.*)" ${DUMPLING_OUTPUT_DIR}/${DB_NAME}.${TABLE_NAME}.000000000.sql | cut -c2-2)
54+
expected=$(seq 0 4)
55+
echo "expected ${DUMPLING_OUTPUT_DIR}/${DB_NAME}.${TABLE_NAME}.000000000.sql ${expected}, actual ${actual}"
56+
[ "$actual" = "$expected" ]
57+
actual=$(grep -w "(.*)" ${DUMPLING_OUTPUT_DIR}/${DB_NAME}.${TABLE_NAME}.000000009.sql | cut -c2-3)
58+
expected=$(seq 95 99)
59+
echo "expected ${DUMPLING_OUTPUT_DIR}/${DB_NAME}.${TABLE_NAME}.000000009.sql ${expected}, actual ${actual}"
60+
[ "$actual" = "$expected" ]
61+
62+
seq 1 8 | xargs -I\? file_not_exist ${DUMPLING_OUTPUT_DIR}/${DB_NAME}.${TABLE_NAME}.00000000\?.sql
63+
4364
# Test for specifying --filetype sql with --sql, should report an error
4465
set +e
4566
run_dumpling --sql "select * from \`$DB_NAME\`.\`$TABLE_NAME\`" --filetype sql > ${DUMPLING_OUTPUT_DIR}/dumpling.log

v4/export/sql.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,18 +1066,24 @@ func parseSnapshotToTSO(pool *sql.DB, snapshot string) (uint64, error) {
10661066
func buildWhereCondition(conf *Config, where string) string {
10671067
var query strings.Builder
10681068
separator := "WHERE"
1069+
leftBracket := " "
1070+
rightBracket := " "
1071+
if conf.Where != "" && where != "" {
1072+
leftBracket = " ("
1073+
rightBracket = ") "
1074+
}
10691075
if conf.Where != "" {
10701076
query.WriteString(separator)
1071-
query.WriteByte(' ')
1077+
query.WriteString(leftBracket)
10721078
query.WriteString(conf.Where)
1073-
query.WriteByte(' ')
1079+
query.WriteString(rightBracket)
10741080
separator = "AND"
1075-
query.WriteByte(' ')
10761081
}
10771082
if where != "" {
10781083
query.WriteString(separator)
1079-
query.WriteString(" ")
1084+
query.WriteString(leftBracket)
10801085
query.WriteString(where)
1086+
query.WriteString(rightBracket)
10811087
}
10821088
return query.String()
10831089
}

v4/export/sql_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,43 @@ func TestBuildPartitionClauses(t *testing.T) {
828828
}
829829
}
830830

831+
func TestBuildWhereCondition(t *testing.T) {
832+
t.Parallel()
833+
834+
conf := DefaultConfig()
835+
testCases := []struct {
836+
confWhere string
837+
chunkWhere string
838+
expectedWhere string
839+
}{
840+
{
841+
"",
842+
"",
843+
"",
844+
},
845+
{
846+
"a >= 1000000 and a <= 2000000",
847+
"",
848+
"WHERE a >= 1000000 and a <= 2000000 ",
849+
},
850+
{
851+
"",
852+
"(`a`>1 and `a`<3)or(`a`=1 and(`b`>=2))or(`a`=3 and(`b`<4))",
853+
"WHERE (`a`>1 and `a`<3)or(`a`=1 and(`b`>=2))or(`a`=3 and(`b`<4)) ",
854+
},
855+
{
856+
"a >= 1000000 and a <= 2000000",
857+
"(`a`>1 and `a`<3)or(`a`=1 and(`b`>=2))or(`a`=3 and(`b`<4))",
858+
"WHERE (a >= 1000000 and a <= 2000000) AND ((`a`>1 and `a`<3)or(`a`=1 and(`b`>=2))or(`a`=3 and(`b`<4))) ",
859+
},
860+
}
861+
for _, testCase := range testCases {
862+
conf.Where = testCase.confWhere
863+
where := buildWhereCondition(conf, testCase.chunkWhere)
864+
require.Equal(t, testCase.expectedWhere, where)
865+
}
866+
}
867+
831868
func TestBuildRegionQueriesWithoutPartition(t *testing.T) {
832869
t.Parallel()
833870

0 commit comments

Comments
 (0)