Skip to content

Commit 3a92631

Browse files
committed
tree: audit blind assertions to *DCollatedString
Now that we support CIText via the DOidWrapper on top of DCollatedString, we need to be careful when using "blind" type assertions like `cs := d.(*tree.DCollatedString)` since it might fail. This commit audits all of these assertions in non-test code and fixes them to use `AsDCollatedString` where appropriate. Release note: None
1 parent f08b1ac commit 3a92631

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

pkg/ccl/changefeedccl/avro/avro.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,11 @@ func typeToSchema(typ *types.T) (*SchemaField, error) {
450450
setNullable(
451451
SchemaTypeString,
452452
func(d tree.Datum, _ interface{}) (interface{}, error) {
453-
return string(*d.(*tree.DString)), nil
453+
s, ok := tree.AsDString(d)
454+
if !ok {
455+
return nil, errors.Newf("expected string type, got %T", d)
456+
}
457+
return string(s), nil
454458
},
455459
func(x interface{}) (tree.Datum, error) {
456460
return tree.NewDString(x.(string)), nil
@@ -460,7 +464,11 @@ func typeToSchema(typ *types.T) (*SchemaField, error) {
460464
setNullable(
461465
SchemaTypeString,
462466
func(d tree.Datum, _ interface{}) (interface{}, error) {
463-
return d.(*tree.DCollatedString).Contents, nil
467+
cs, ok := tree.AsDCollatedString(d)
468+
if !ok {
469+
return nil, errors.Newf("expected collated string type, got %T", d)
470+
}
471+
return cs.Contents, nil
464472
},
465473
func(x interface{}) (tree.Datum, error) {
466474
return tree.NewDCollatedString(x.(string), typ.Locale(), &tree.CollationEnvironment{})

pkg/sql/rowenc/valueside/legacy.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,7 @@ func MarshalLegacy(colType *types.T, val tree.Datum) (roachpb.Value, error) {
203203
return r, nil
204204
}
205205
case types.CollatedStringFamily:
206-
if colType.Oid() == oidext.T_citext {
207-
val = tree.UnwrapDOidWrapper(val)
208-
}
209-
if v, ok := val.(*tree.DCollatedString); ok {
206+
if v, ok := tree.AsDCollatedString(val); ok {
210207
if lex.LocaleNamesAreEqual(v.Locale, colType.Locale()) {
211208
r.SetString(v.Contents)
212209
return r, nil

pkg/sql/sem/tree/datum.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6572,7 +6572,7 @@ func AdjustValueToType(typ *types.T, inVal Datum) (outVal Datum, err error) {
65726572
if v, ok := AsDString(inVal); ok {
65736573
sv = string(v)
65746574
isString = true
6575-
} else if v, ok := inVal.(*DCollatedString); ok {
6575+
} else if v, ok := AsDCollatedString(inVal); ok {
65766576
sv = v.Contents
65776577
isCollatedString = true
65786578
}
@@ -6619,6 +6619,8 @@ func AdjustValueToType(typ *types.T, inVal Datum) (outVal Datum, err error) {
66196619
}
66206620
return NewDString(sv), nil
66216621
} else if isCollatedString {
6622+
// Note that we cannot have CITEXT here (because of Oid check
6623+
// above).
66226624
return NewDCollatedString(sv, typ.Locale(), &CollationEnvironment{})
66236625
}
66246626
}

0 commit comments

Comments
 (0)