Skip to content

Commit

Permalink
Merge pull request #70392 from cockroachdb/blathers/backport-release-…
Browse files Browse the repository at this point in the history
…21.2-70375

release-21.2: sql: preserve tuple types when decoding
  • Loading branch information
rafiss authored Nov 19, 2021
2 parents a0006d0 + 192a89f commit dfed5b0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
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 @@ -1072,10 +1072,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)
}

0 comments on commit dfed5b0

Please sign in to comment.