Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

Commit

Permalink
Patch sqltypes.MakeRowTrusted to lean on field count (#126)
Browse files Browse the repository at this point in the history
Patch to handle this case while we move towards a permanent fix upstream.
cf. vitessio#4661 vitessio#4669
  • Loading branch information
setassociative committed Mar 4, 2021
1 parent 4150d5e commit 7a8d85f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
7 changes: 4 additions & 3 deletions go/sqltypes/result.go
Expand Up @@ -159,13 +159,14 @@ func ResultsEqual(r1, r2 []Result) bool {
// Every place this function is called, a comment is needed that explains
// why it's justified.
func MakeRowTrusted(fields []*querypb.Field, row *querypb.Row) []Value {
sqlRow := make([]Value, len(row.Lengths))
sqlRow := make([]Value, len(fields))
var offset int64
for i, length := range row.Lengths {
for i, fld := range fields {
length := row.Lengths[i]
if length < 0 {
continue
}
sqlRow[i] = MakeTrusted(fields[i].Type, row.Values[offset:offset+length])
sqlRow[i] = MakeTrusted(fld.Type, row.Values[offset:offset+length])
offset += length
}
return sqlRow
Expand Down
55 changes: 55 additions & 0 deletions go/sqltypes/result_test.go
Expand Up @@ -23,6 +23,61 @@ import (
querypb "vitess.io/vitess/go/vt/proto/query"
)

func TestMakeRowTrusted(t *testing.T) {
fields := MakeTestFields(
"some_int|some_text|another_int",
"int8|varchar|int8",
)

values := []byte{}
hw := []byte("hello, world")
values = append(values, hw...)
values = append(values, byte(42))

row := &querypb.Row{
Lengths: []int64{-1, int64(len(hw)), 1},
Values: values,
}

want := []Value{
MakeTrusted(querypb.Type_NULL_TYPE, nil),
MakeTrusted(querypb.Type_VARCHAR, []byte("hello, world")),
MakeTrusted(querypb.Type_INT8, []byte{byte(42)}),
}

result := MakeRowTrusted(fields, row)
if !reflect.DeepEqual(result, want) {
t.Errorf("MakeRowTrusted:\ngot: %#v\nwant: %#v", result, want)
}
}

func TestMakeRowTrustedDoesNotPanicOnNewColumns(t *testing.T) {
fields := MakeTestFields(
"some_int|some_text",
"int8|varchar",
)

values := []byte{byte(123)}
hw := []byte("hello, world")
values = append(values, hw...)
values = append(values, byte(42))

row := &querypb.Row{
Lengths: []int64{1, int64(len(hw)), 1},
Values: values,
}

want := []Value{
MakeTrusted(querypb.Type_INT8, []byte{byte(123)}),
MakeTrusted(querypb.Type_VARCHAR, []byte("hello, world")),
}

result := MakeRowTrusted(fields, row)
if !reflect.DeepEqual(result, want) {
t.Errorf("MakeRowTrusted:\ngot: %#v\nwant: %#v", result, want)
}
}

func TestRepair(t *testing.T) {
fields := []*querypb.Field{{
Type: Int64,
Expand Down

0 comments on commit 7a8d85f

Please sign in to comment.