Created
August 28, 2023 08:47
-
-
Save YangKeao/ad2084814f079956de41e2860b382f8a to your computer and use it in GitHub Desktop.
A Go program to test whether the "weight_string" of an unicode character is the same between TiDB and MySQL.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"database/sql" | |
"fmt" | |
"os" | |
"sync" | |
_ "github.com/go-sql-driver/mysql" | |
"golang.org/x/exp/slices" | |
) | |
func main() { | |
mysqlStr := os.Args[1] | |
tidbStr := os.Args[2] | |
collation := os.Args[3] | |
dbMySQL, err := sql.Open("mysql", mysqlStr) | |
if err != nil { | |
panic(err) | |
} | |
dbMySQL.SetMaxOpenConns(100) | |
dbTiDB, err := sql.Open("mysql", tidbStr) | |
if err != nil { | |
panic(err) | |
} | |
checkWg := &sync.WaitGroup{} | |
for i := 0; i < 183969/1000; i++ { | |
rangeStart := i * 1000 | |
rangeEnd := (i + 1) * 1000 | |
if rangeEnd > 183969 { | |
rangeEnd = 183969 | |
} | |
checkWg.Add(1) | |
go func() { | |
connMySQL, err := dbMySQL.Conn(context.Background()) | |
if err != nil { | |
panic(err) | |
} | |
defer connMySQL.Close() | |
connTiDB, err := dbTiDB.Conn(context.Background()) | |
if err != nil { | |
panic(err) | |
} | |
defer connTiDB.Close() | |
mysqlWeightStringStmt, err := connMySQL.PrepareContext(context.Background(), "SELECT WEIGHT_STRING(? collate "+collation+")") | |
if err != nil { | |
panic(err) | |
} | |
defer mysqlWeightStringStmt.Close() | |
tidbWeightStringStmt, err := connTiDB.PrepareContext(context.Background(), "SELECT WEIGHT_STRING(CONVERT(? USING utf8mb4) collate "+collation+")") | |
if err != nil { | |
panic(err) | |
} | |
defer tidbWeightStringStmt.Close() | |
for j := rangeStart; j < rangeEnd; j++ { | |
rows, err := mysqlWeightStringStmt.Query(fmt.Sprintf("%c", rune(j))) | |
if err != nil { | |
panic(err) | |
} | |
var expectedWeight []uint8 | |
for rows.Next() { | |
err = rows.Scan(&expectedWeight) | |
if err != nil { | |
panic(err) | |
} | |
break | |
} | |
rows.Close() | |
rows, err = tidbWeightStringStmt.Query(fmt.Sprintf("%c", rune(j))) | |
if err != nil { | |
panic(err) | |
} | |
var weight []uint8 | |
for rows.Next() { | |
err = rows.Scan(&weight) | |
if err != nil { | |
panic(err) | |
} | |
break | |
} | |
rows.Close() | |
if !slices.Equal(expectedWeight, weight) { | |
fmt.Printf("The weight of %X is not equal: expectedWeight: %v, weight: %v\n", j, expectedWeight, weight) | |
} | |
} | |
fmt.Printf("Finished checking %d - %d\n", rangeStart, rangeEnd) | |
checkWg.Done() | |
}() | |
} | |
checkWg.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment