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

Commit

Permalink
sql: fix table sample sql generator bug (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
lichunzhu authored Apr 7, 2021
1 parent 59b763e commit 51b465b
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 61 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ require (
github.com/pingcap/tidb-tools v4.0.9-0.20201127090955-2707c97b3853+incompatible
github.com/prometheus/client_golang v1.5.1
github.com/prometheus/client_model v0.2.0
github.com/siddontang/go-mysql v0.0.0-20200222075837-12e89848f047
github.com/soheilhy/cmux v0.1.4
github.com/spf13/pflag v1.0.5
github.com/syndtr/goleveldb v1.0.1-0.20190625010220-02440ea7a285 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,7 @@ github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sasha-s/go-deadlock v0.2.0/go.mod h1:StQn567HiB1fF2yJ44N9au7wOhrPS3iZqiDbRupzT10=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/satori/go.uuid v1.2.1-0.20181028125025-b2ce2384e17b h1:gQZ0qzfKHQIybLANtM3mBXNUtOfsCFXeTsnBqCsx1KM=
github.com/satori/go.uuid v1.2.1-0.20181028125025-b2ce2384e17b/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/segmentio/kafka-go v0.1.0/go.mod h1:X6itGqS9L4jDletMsxZ7Dz+JFWxM6JHfPOCvTvk+EJo=
github.com/segmentio/kafka-go v0.2.0/go.mod h1:X6itGqS9L4jDletMsxZ7Dz+JFWxM6JHfPOCvTvk+EJo=
Expand All @@ -789,9 +790,12 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeV
github.com/shurcooL/vfsgen v0.0.0-20181020040650-a97a25d856ca/go.mod h1:TrYk7fJVaAttu97ZZKrO9UbRa8izdowaMIZcxYMbVaw=
github.com/shurcooL/vfsgen v0.0.0-20181202132449-6a9ea43bcacd h1:ug7PpSOB5RBPK1Kg6qskGBoP3Vnj/aNYFTznWvlkGo0=
github.com/shurcooL/vfsgen v0.0.0-20181202132449-6a9ea43bcacd/go.mod h1:TrYk7fJVaAttu97ZZKrO9UbRa8izdowaMIZcxYMbVaw=
github.com/siddontang/go v0.0.0-20180604090527-bdc77568d726 h1:xT+JlYxNGqyT+XcU8iUrN18JYed2TvG9yN5ULG2jATM=
github.com/siddontang/go v0.0.0-20180604090527-bdc77568d726/go.mod h1:3yhqj7WBBfRhbBlzyOC3gUxftwsU0u8gqevxwIHQpMw=
github.com/siddontang/go-log v0.0.0-20180807004314-8d05993dda07/go.mod h1:yFdBgwXP24JziuRl2NMUahT7nGLNOKi1SIiFxMttVD4=
github.com/siddontang/go-log v0.0.0-20190221022429-1e957dd83bed h1:KMgQoLJGCq1IoZpLZE3AIffh9veYWoVlsvA4ib55TMM=
github.com/siddontang/go-log v0.0.0-20190221022429-1e957dd83bed/go.mod h1:yFdBgwXP24JziuRl2NMUahT7nGLNOKi1SIiFxMttVD4=
github.com/siddontang/go-mysql v0.0.0-20200222075837-12e89848f047 h1:boyJ8EgQN/aC3grvx8QUoJrptt7RvneezSJSCbW25a4=
github.com/siddontang/go-mysql v0.0.0-20200222075837-12e89848f047/go.mod h1:+W4RCzesQDI11HvIkaDjS8yM36SpAnGNQ7jmTLn5BnU=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.3.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
Expand Down
4 changes: 3 additions & 1 deletion v4/export/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,9 @@ func selectTiDBTableSample(conn *sql.Conn, dbName, tableName string) (pkFields [
if hasImplicitRowID {
pkFields, pkColTypes = []string{"_tidb_rowid"}, []string{"BIGINT"}
}
if len(pkFields) == 0 {
return pkFields, pkVals, nil
}
query := buildTiDBTableSampleQuery(pkFields, dbName, tableName)
rows, err := conn.QueryContext(context.Background(), query)
if err != nil {
Expand All @@ -638,7 +641,6 @@ func selectTiDBTableSample(conn *sql.Conn, dbName, tableName string) (pkFields [
pkValRow = append(pkValRow, buf.String())
buf.Reset()
}
rowRec.WriteToBuffer(buf, true)
pkVals = append(pkVals, pkValRow)
iter.Next()
}
Expand Down
2 changes: 1 addition & 1 deletion v4/export/dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (s *testSQLSuite) TestDumpBlock(c *C) {
WillReturnRows(sqlmock.NewRows([]string{"Database", "Create Database"}).
AddRow("test", "CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET utf8mb4 */"))

tctx, cancel := tcontext.Background().WithCancel()
tctx, cancel := tcontext.Background().WithLogger(appLogger).WithCancel()
defer cancel()
conn, err := db.Conn(tctx)
c.Assert(err, IsNil)
Expand Down
36 changes: 12 additions & 24 deletions v4/export/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,26 +675,14 @@ func buildCompareClause(buf *bytes.Buffer, quotaCols []string, bound []string, c
}
}

// Compare returns an integer comparing two string arrays lexicographically.
// return (compare result, common prefix length)
// return -1 if low < up
// return 0 if low == up
// return 1 if low > up
func compareArrString(low []string, up []string) (compareRes, commonLength int) {
l := len(low)
c := -1
if u := len(up); u < l {
l = u
c = 1
} else if u == l {
c = 0
}
for i := 0; i < l; i++ {
if d := strings.Compare(low[i], up[i]); d != 0 {
return d, i
// getCommonLength returns the common length of low and up
func getCommonLength(low []string, up []string) int {
for i := range low {
if low[i] != up[i] {
return i
}
}
return c, l
return len(low)
}

// buildBetweenClause build clause in a specified table range.
Expand All @@ -713,13 +701,13 @@ func buildBetweenClause(buf *bytes.Buffer, quotaCols []string, low []string, up
buf.WriteString(up[0])
}
// handle special cases with common prefix
compare, commonLen := compareArrString(low, up)
// unexpected case for low >= up, return empty result
if compare >= 0 {
buf.WriteString("false")
return
}
commonLen := getCommonLength(low, up)
if commonLen > 0 {
// unexpected case for low == up, return empty result
if commonLen == len(low) {
buf.WriteString("false")
return
}
for i := 0; i < commonLen; i++ {
if i > 0 {
buf.WriteString(" and ")
Expand Down
Loading

0 comments on commit 51b465b

Please sign in to comment.