@@ -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
2931func 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+
773809func encDatumFromEncodedWithDatum (
774810 enc catenumpb.DatumEncoding , encoded []byte , datum tree.Datum ,
775811) rowenc.EncDatum {
0 commit comments