From 1bf3f99c6e8e8c95f0057161c4083a4a7181df4b Mon Sep 17 00:00:00 2001 From: "HyeonGyu Lee (Vazrupe)" Date: Thu, 15 Aug 2019 13:54:00 +0900 Subject: [PATCH] sql: unwrap in CastToCollatedString Unwarp once in CastToCollatedString. In the assert message, type the output as is. Fixes #38865 Release note: None --- .../logictest/testdata/logic_test/collatedstring | 15 +++++++++++++++ pkg/sql/opt/norm/custom_funcs.go | 9 +++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/pkg/sql/logictest/testdata/logic_test/collatedstring b/pkg/sql/logictest/testdata/logic_test/collatedstring index 1cead0e7316b..3653db03280a 100644 --- a/pkg/sql/logictest/testdata/logic_test/collatedstring +++ b/pkg/sql/logictest/testdata/logic_test/collatedstring @@ -19,6 +19,21 @@ SELECT 'A' COLLATE en ---- A +query T +SELECT ('A' COLLATE de) COLLATE en +---- +A + +query T +SELECT NAME 'A' COLLATE en +---- +A + +query T +SELECT (NAME 'A' COLLATE de) COLLATE en +---- +A + query T SELECT NULL COLLATE en ---- diff --git a/pkg/sql/opt/norm/custom_funcs.go b/pkg/sql/opt/norm/custom_funcs.go index 5fe8945ecebc..8cb593592544 100644 --- a/pkg/sql/opt/norm/custom_funcs.go +++ b/pkg/sql/opt/norm/custom_funcs.go @@ -1600,14 +1600,19 @@ func (c *CustomFuncs) ConvertConstArrayToTuple(scalar opt.ScalarExpr) opt.Scalar // CastToCollatedString returns the given string or collated string as a // collated string constant with the given locale. func (c *CustomFuncs) CastToCollatedString(str opt.ScalarExpr, locale string) opt.ScalarExpr { + datum := str.(*memo.ConstExpr).Value + if wrap, ok := datum.(*tree.DOidWrapper); ok { + datum = wrap.Wrapped + } + var value string - switch t := str.(*memo.ConstExpr).Value.(type) { + switch t := datum.(type) { case *tree.DString: value = string(*t) case *tree.DCollatedString: value = t.Contents default: - panic(errors.AssertionFailedf("unexpected type for COLLATE: %T", log.Safe(str.(*memo.ConstExpr).Value))) + panic(errors.AssertionFailedf("unexpected type for COLLATE: %T", t)) } return c.f.ConstructConst(tree.NewDCollatedString(value, locale, &c.f.evalCtx.CollationEnv))