Skip to content
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

release-21.2: sql: preserve tuple types when decoding #70392

Merged
merged 1 commit into from
Nov 19, 2021
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
3 changes: 0 additions & 3 deletions pkg/sql/colencoding/value_encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
package colencoding

import (
"fmt"
"testing"

"github.com/cockroachdb/cockroach/pkg/col/coldata"
Expand Down Expand Up @@ -40,7 +39,6 @@ func TestDecodeTableValueToCol(t *testing.T) {
typs[i] = ct
datums[i] = datum
var err error
fmt.Println(datum)
buf, err = rowenc.EncodeTableValue(buf, descpb.ColumnID(encoding.NoColumnID), datum, scratch)
if err != nil {
t.Fatal(err)
Expand All @@ -49,7 +47,6 @@ func TestDecodeTableValueToCol(t *testing.T) {
batch := coldata.NewMemBatchWithCapacity(typs, 1 /* capacity */, coldataext.NewExtendedColumnFactory(nil /*evalCtx */))
for i := 0; i < nCols; i++ {
typeOffset, dataOffset, _, typ, err := encoding.DecodeValueTag(buf)
fmt.Println(typ)
if err != nil {
t.Fatal(err)
}
Expand Down
6 changes: 2 additions & 4 deletions pkg/sql/rowenc/column_type_encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -1060,10 +1060,8 @@ func decodeTuple(a *DatumAlloc, tupTyp *types.T, b []byte) (tree.Datum, []byte,
return nil, nil, err
}

result := tree.DTuple{
D: a.NewDatums(len(tupTyp.TupleContents())),
}

result := *(tree.NewDTuple(tupTyp))
result.D = a.NewDatums(len(tupTyp.TupleContents()))
var datum tree.Datum
for i := range tupTyp.TupleContents() {
datum, b, err = DecodeTableValue(a, tupTyp.TupleContents()[i], b)
Expand Down
19 changes: 19 additions & 0 deletions pkg/sql/rowenc/column_type_encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,3 +338,22 @@ func TestDecodeTableValueOutOfRangeTimestamp(t *testing.T) {
})
}
}

// This test ensures that decoding a tuple value with a specific, labeled tuple
// type preserves the labels.
func TestDecodeTupleValueWithType(t *testing.T) {
tupleType := types.MakeLabeledTuple([]*types.T{types.Int, types.String}, []string{"a", "b"})
datum := tree.NewDTuple(tupleType, tree.NewDInt(tree.DInt(1)), tree.NewDString("foo"))
buf, err := rowenc.EncodeTableValue(nil, descpb.ColumnID(encoding.NoColumnID), datum, nil)
if err != nil {
t.Fatal(err)
}
da := rowenc.DatumAlloc{}
var decoded tree.Datum
decoded, _, err = rowenc.DecodeTableValue(&da, tupleType, buf)
if err != nil {
t.Fatal(err)
}

require.Equal(t, decoded, datum)
}