Skip to content

fix: return null values properly in proxy mode #721

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 23, 2022
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
13 changes: 9 additions & 4 deletions server/resp.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,16 @@ func (c *Conn) writeFieldList(fs []*Field, data []byte) error {
func (c *Conn) writeFieldValues(fv []FieldValue) error {
data := make([]byte, 4, 1024)
for _, v := range fv {
tv, err := FormatTextValue(v.Value())
if err != nil {
return err
if v.Value() == nil {
// NULL value is encoded as 0xfb here
data = append(data, []byte{0xfb}...)
} else {
tv, err := FormatTextValue(v.Value())
if err != nil {
return err
}
data = append(data, PutLengthEncodedString(tv)...)
}
data = append(data, PutLengthEncodedString(tv)...)
}

return c.WritePacket(data)
Expand Down
40 changes: 40 additions & 0 deletions server/resp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,43 @@ func (t *respConnTestSuite) TestConnWriteFieldList(c *check.C) {
c.Assert(clientConn.WriteBuffered[:27], check.BytesEquals, []byte{23, 0, 0, 0, 3, 100, 101, 102, 0, 0, 0, 1, 'c', 0, 12, 33, 0, 0, 0, 0, 0, 253, 0, 0, 0, 0, 0})
c.Assert(clientConn.WriteBuffered[27:], check.BytesEquals, []byte{1, 0, 0, 1, mysql.EOF_HEADER})
}

func (t *respConnTestSuite) TestConnWriteFieldValues(c *check.C) {
clientConn := &mockconn.MockConn{MultiWrite: true}
conn := &Conn{Conn: packet.NewConn(clientConn)}

r, err := mysql.BuildSimpleTextResultset([]string{"c"}, [][]interface{}{
[]interface{}{"d"},
[]interface{}{nil},
})

c.Assert(err, check.IsNil)
err = conn.writeFieldList(r.Fields, nil)
c.Assert(err, check.IsNil)

// fields and EOF
c.Assert(clientConn.WriteBuffered[:27], check.BytesEquals, []byte{23, 0, 0, 0, 3, 100, 101, 102, 0, 0, 0, 1, 'c', 0, 12, 33, 0, 0, 0, 0, 0, 253, 0, 0, 0, 0, 0})
c.Assert(clientConn.WriteBuffered[27:32], check.BytesEquals, []byte{1, 0, 0, 1, mysql.EOF_HEADER})

r.Values = make([][]mysql.FieldValue, len(r.RowDatas))
for i := range r.Values {
r.Values[i], err = r.RowDatas[i].Parse(r.Fields, false, r.Values[i])

c.Assert(err, check.IsNil)

err = conn.writeFieldValues(r.Values[i])
c.Assert(err, check.IsNil)
}

err = conn.writeEOF()
c.Assert(err, check.IsNil)

// first row
c.Assert(clientConn.WriteBuffered[32:38], check.BytesEquals, []byte{2, 0, 0, 2, 1, 'd'})

// second row with NULL value
c.Assert(clientConn.WriteBuffered[38:43], check.BytesEquals, []byte{1, 0, 0, 3, 251})

// EOF
c.Assert(clientConn.WriteBuffered[43:], check.BytesEquals, []byte{1, 0, 0, 4, mysql.EOF_HEADER})
}