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

sql: fix where condition without brackets problem (#372) #375

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1069,18 +1069,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