Skip to content

Commit

Permalink
cherry pick pingcap#31806 to release-5.3
Browse files Browse the repository at this point in the history
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
  • Loading branch information
xiongjiwei authored and ti-srebot committed Jan 20, 2022
1 parent 83b273a commit 846fb40
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
22 changes: 22 additions & 0 deletions cmd/explaintest/r/collation.result
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,25 @@ coercibility(convert('a' using utf8mb4))
select coercibility(convert('a' using utf8mb4) collate utf8mb4_general_ci);
coercibility(convert('a' using utf8mb4) collate utf8mb4_general_ci)
0
DROP TABLE IF EXISTS t2;
CREATE TABLE t2 (
id INT NOT NULL PRIMARY KEY auto_increment,
`col_91` char(47) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`col_92` int(10) unsigned DEFAULT '2478966067',
`col_97` char(32) COLLATE utf8mb4_bin NOT NULL
) collate utf8mb4_general_ci;
INSERT INTO `t2` VALUES (17,'UUtJeaV',497551109,'snRXXCZHBPW');
SET names utf8mb4 collate utf8mb4_bin;
SELECT greatest( col_91 , col_97 ) as expr1 FROM t2 ORDER BY id;
expr1
snRXXCZHBPW
SELECT least( col_91 , col_97 ) as expr1 FROM t2 ORDER BY id;
expr1
UUtJeaV
SET names utf8mb4 collate utf8mb4_general_ci;
SELECT greatest( col_91 , col_97 ) as expr1 FROM t2 ORDER BY id;
expr1
snRXXCZHBPW
SELECT least( col_91 , col_97 ) as expr1 FROM t2 ORDER BY id;
expr1
UUtJeaV
20 changes: 19 additions & 1 deletion cmd/explaintest/t/collation.test
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,22 @@ insert into t values ('啊', '撒旦');
select coercibility(concat(a, b)) from t;
select coercibility(convert(concat(a, b) using utf8mb4) collate utf8mb4_general_ci) from t;
select coercibility(convert('a' using utf8mb4));
select coercibility(convert('a' using utf8mb4) collate utf8mb4_general_ci);
select coercibility(convert('a' using utf8mb4) collate utf8mb4_general_ci);

# test greatest and least function with collation.
DROP TABLE IF EXISTS t2;
CREATE TABLE t2 (
id INT NOT NULL PRIMARY KEY auto_increment,
`col_91` char(47) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`col_92` int(10) unsigned DEFAULT '2478966067',
`col_97` char(32) COLLATE utf8mb4_bin NOT NULL
) collate utf8mb4_general_ci;

INSERT INTO `t2` VALUES (17,'UUtJeaV',497551109,'snRXXCZHBPW');

SET names utf8mb4 collate utf8mb4_bin;
SELECT greatest( col_91 , col_97 ) as expr1 FROM t2 ORDER BY id;
SELECT least( col_91 , col_97 ) as expr1 FROM t2 ORDER BY id;
SET names utf8mb4 collate utf8mb4_general_ci;
SELECT greatest( col_91 , col_97 ) as expr1 FROM t2 ORDER BY id;
SELECT least( col_91 , col_97 ) as expr1 FROM t2 ORDER BY id;
2 changes: 1 addition & 1 deletion expression/collation.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func deriveCoercibilityForColumn(c *Column) Coercibility {

func deriveCollation(ctx sessionctx.Context, funcName string, args []Expression, retType types.EvalType, argTps ...types.EvalType) (ec *ExprCollation, err error) {
switch funcName {
case ast.Concat, ast.ConcatWS, ast.Lower, ast.Lcase, ast.Reverse, ast.Upper, ast.Ucase, ast.Quote, ast.Coalesce:
case ast.Concat, ast.ConcatWS, ast.Lower, ast.Lcase, ast.Reverse, ast.Upper, ast.Ucase, ast.Quote, ast.Coalesce, ast.Greatest, ast.Least:
return CheckAndDeriveCollationFromExprs(ctx, funcName, retType, args...)
case ast.Left, ast.Right, ast.Repeat, ast.Trim, ast.LTrim, ast.RTrim, ast.Substr, ast.SubstringIndex, ast.Replace, ast.Substring, ast.Mid, ast.Translate:
return CheckAndDeriveCollationFromExprs(ctx, funcName, retType, args[0])
Expand Down
2 changes: 1 addition & 1 deletion expression/collation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ func TestDeriveCollation(t *testing.T) {
},
{
[]string{
ast.Concat, ast.ConcatWS, ast.Coalesce, ast.In,
ast.Concat, ast.ConcatWS, ast.Coalesce, ast.In, ast.Greatest, ast.Least,
},
[]Expression{
newConstString("a", CoercibilityCoercible, charset.CharsetUTF8MB4, charset.CollationUTF8MB4),
Expand Down

0 comments on commit 846fb40

Please sign in to comment.