go-sql-driver / mysql

Go MySQL Driver is a MySQL driver for Go's (golang) database/sql package
https://pkg.go.dev/github.com/go-sql-driver/mysql
Mozilla Public License 2.0
14.45k stars 2.3k forks source link

Connection collations limit use of custom collation specification #1603

Closed SpencerMalone closed 2 weeks ago

SpencerMalone commented 2 months ago

Issue description

According to: https://github.com/go-sql-driver/mysql?tab=readme-ov-file#unicode-support

When only the charset is specified, the SET NAMES query is sent and the server's default collation is used. When both the charset and collation are specified, the SET NAMES COLLATE query is sent. When only the collation is specified, the collation is specified in the protocol handshake and the SET NAMES query is not sent. This can save one roundtrip, but note that the server may ignore the specified collation silently and use the server's default charset/collation instead.

Unfortunately, the limits imposed by https://github.com/go-sql-driver/mysql/blob/master/packets.go#L325-L336 seem to apply regardless of if you are the only collation case or the both charset and collation case, which limits the use of "similar but not the same" newer mysql collations, such as utf8mb4_0900_as_ci (notice this is the as / accent sensitive collation, not ai_ci)

Example code

    db, err := sql.Open("mysql", "user:password@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&collation=utf8mb4_0900_as_ci")
    if err != nil {
        panic(err.Error())
    }
    defer db.Close()

    err = db.Ping()
    if err != nil {
        panic(err.Error())
    }

Error log

*errors.errorString: unknown collation: "utf8mb4_0900_as_ci"

Configuration

Driver version (or git SHA):v1.8.1

Go version: go version go1.22.0 darwin/amd64

Server version: Mysql 8.x