Skip to content

Commit 60c8788

Browse files
committed
rowenc: fix decoding for Canonical types
The definition of a Canonical type is the Canonical type is the type of the datum that a column decodes into. This was only partially true for the two canonical types that use the oid wrapper. The citext type was decoded by the value side decoder, but not the keyside decoder. The name type was decoded by the key side decoder, but not the value side decoder. Release note: none Epic: CRDB-48647
1 parent fbe4b69 commit 60c8788

File tree

5 files changed

+49
-1
lines changed

5 files changed

+49
-1
lines changed

pkg/sql/randgen/type.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ func RandTypeFromSlice(rng *rand.Rand, typs []*types.T) *types.T {
117117
case types.BitFamily:
118118
return types.MakeBit(int32(rng.Intn(50)))
119119
case types.CollatedStringFamily:
120+
if typ.Oid() == oidext.T_citext {
121+
return types.CIText
122+
}
120123
return types.MakeCollatedString(types.String, *RandCollationLocale(rng))
121124
case types.ArrayFamily:
122125
if typ.ArrayContents().Family() == types.AnyFamily {

pkg/sql/rowenc/encoded_datum_test.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ import (
1717
"github.com/cockroachdb/cockroach/pkg/sql/catalog/colinfo"
1818
"github.com/cockroachdb/cockroach/pkg/sql/randgen"
1919
"github.com/cockroachdb/cockroach/pkg/sql/rowenc"
20+
"github.com/cockroachdb/cockroach/pkg/sql/rowenc/keyside"
2021
"github.com/cockroachdb/cockroach/pkg/sql/rowenc/valueside"
2122
"github.com/cockroachdb/cockroach/pkg/sql/sem/eval"
2223
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
2324
"github.com/cockroachdb/cockroach/pkg/sql/types"
2425
"github.com/cockroachdb/cockroach/pkg/util/encoding"
2526
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
2627
"github.com/cockroachdb/cockroach/pkg/util/randutil"
28+
"github.com/stretchr/testify/require"
2729
)
2830

2931
func TestEncDatum(t *testing.T) {
@@ -150,7 +152,6 @@ func TestEncDatumNull(t *testing.T) {
150152
}
151153
}
152154
}
153-
154155
}
155156

156157
// checkEncDatumCmp encodes the given values using the given encodings,
@@ -770,6 +771,41 @@ func TestEncDatumFingerprintMemory(t *testing.T) {
770771
}
771772
}
772773

774+
func TestDecodesToCanonical(t *testing.T) {
775+
rng, _ := randutil.NewTestRand()
776+
for i := 0; i < 20; i++ {
777+
typ := randgen.RandType(rng)
778+
datum := randgen.RandDatum(rng, typ, false)
779+
780+
buf, err := valueside.Encode(nil, valueside.NoColumnID, datum)
781+
require.NoError(t, err)
782+
decoded, _, err := valueside.Decode(&tree.DatumAlloc{}, typ, buf)
783+
784+
valueType := decoded.ResolvedType()
785+
require.NoError(t, err)
786+
require.True(t, valueType.Identical(typ.Canonical()), "value type %+v not identical to canonical type %+v", valueType, typ)
787+
require.Equal(t, decoded.ResolvedType().Oid(), typ.Canonical().Oid())
788+
789+
direction := encoding.Ascending
790+
if rng.Int()%2 == 0 {
791+
direction = encoding.Descending
792+
}
793+
794+
if colinfo.ColumnTypeIsIndexable(typ) {
795+
buf, err = keyside.Encode(nil, datum, direction)
796+
require.NoError(t, err)
797+
798+
decodedKey, _, err := keyside.Decode(&tree.DatumAlloc{}, typ, buf, direction)
799+
require.NoError(t, err)
800+
801+
keyType := decodedKey.ResolvedType()
802+
require.NoError(t, err)
803+
require.True(t, keyType.Identical(typ.Canonical()), "key type %+v not identical to canonical type %+v", keyType, typ)
804+
require.Equal(t, decodedKey.ResolvedType().Oid(), typ.Canonical().Oid())
805+
}
806+
}
807+
}
808+
773809
func encDatumFromEncodedWithDatum(
774810
enc catenumpb.DatumEncoding, encoded []byte, datum tree.Datum,
775811
) rowenc.EncDatum {

pkg/sql/rowenc/keyside/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ go_library(
1414
deps = [
1515
"//pkg/geo",
1616
"//pkg/geo/geopb",
17+
"//pkg/sql/oidext",
1718
"//pkg/sql/pgrepl/lsn",
1819
"//pkg/sql/sem/tree",
1920
"//pkg/sql/types",

pkg/sql/rowenc/keyside/decode.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/cockroachdb/apd/v3"
1212
"github.com/cockroachdb/cockroach/pkg/geo"
1313
"github.com/cockroachdb/cockroach/pkg/geo/geopb"
14+
"github.com/cockroachdb/cockroach/pkg/sql/oidext"
1415
"github.com/cockroachdb/cockroach/pkg/sql/pgrepl/lsn"
1516
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
1617
"github.com/cockroachdb/cockroach/pkg/sql/types"
@@ -131,6 +132,10 @@ func Decode(
131132
if err != nil {
132133
return nil, nil, err
133134
}
135+
if valType.Oid() == oidext.T_citext {
136+
d, err := a.NewDCIText(r)
137+
return d, rkey, err
138+
}
134139
d, err := a.NewDCollatedString(r, valType.Locale())
135140
return d, rkey, err
136141
case types.JsonFamily:

pkg/sql/rowenc/valueside/decode.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ func DecodeUntaggedDatum(
6565
if err != nil {
6666
return nil, b, err
6767
}
68+
if t.Oid() == oid.T_name {
69+
return a.NewDName(tree.DString(data)), b, nil
70+
}
6871
return a.NewDString(tree.DString(data)), b, nil
6972
case types.CollatedStringFamily:
7073
b, data, err := encoding.DecodeUntaggedBytesValue(buf)

0 commit comments

Comments
 (0)