Skip to content

Commit

Permalink
expression: set a collation according to the arguments for ifnull i…
Browse files Browse the repository at this point in the history
…n constant folding (#52119) (#52130)

close #51765
  • Loading branch information
ti-chi-bot authored May 21, 2024
1 parent 5989364 commit 0f107c2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
10 changes: 9 additions & 1 deletion pkg/expression/constant_fold.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,15 @@ func ifNullFoldHandler(expr *ScalarFunction) (Expression, bool) {
// evaluated to constArg.Value after foldConstant(args[0]), it's not
// needed to be checked.
if constArg.Value.IsNull() {
return foldConstant(args[1])
foldedExpr, isConstant := foldConstant(args[1])

// See https://github.com/pingcap/tidb/issues/51765. If the first argument can
// be folded into NULL, the collation of IFNULL should be the same as the second
// arguments.
expr.GetType().SetCharset(args[1].GetType().GetCharset())
expr.GetType().SetCollate(args[1].GetType().GetCollate())

return foldedExpr, isConstant
}
return constArg, isDeferred
}
Expand Down
26 changes: 26 additions & 0 deletions tests/integrationtest/r/expression/constant_fold.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
drop table if exists t, t1;
create table t (id varbinary(16));
create table t1(id char(16) charset utf8mb4 collate utf8mb4_general_ci);
insert into t values ();
insert into t1 values ("Hello World");
select collation(ifnull(concat(NULL),'~'));
collation(ifnull(concat(NULL),'~'))
utf8mb4_general_ci
select collation(ifnull(concat(NULL),ifnull(concat(NULL),'~')));
collation(ifnull(concat(NULL),ifnull(concat(NULL),'~')))
utf8mb4_general_ci
select collation(ifnull(concat(id),'~')) from t;
collation(ifnull(concat(id),'~'))
binary
select collation(ifnull(concat(NULL),ifnull(concat(id),'~'))) from t;
collation(ifnull(concat(NULL),ifnull(concat(id),'~')))
binary
select collation(ifnull(concat(id),ifnull(concat(id),'~'))) from t;
collation(ifnull(concat(id),ifnull(concat(id),'~')))
binary
select collation(ifnull(concat(NULL),id)) from t1;
collation(ifnull(concat(NULL),id))
utf8mb4_general_ci
select collation(ifnull(concat(NULL),ifnull(concat(NULL),id))) from t1;
collation(ifnull(concat(NULL),ifnull(concat(NULL),id)))
utf8mb4_general_ci
13 changes: 13 additions & 0 deletions tests/integrationtest/t/expression/constant_fold.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# TestFoldIfNull
drop table if exists t, t1;
create table t (id varbinary(16));
create table t1(id char(16) charset utf8mb4 collate utf8mb4_general_ci);
insert into t values ();
insert into t1 values ("Hello World");
select collation(ifnull(concat(NULL),'~'));
select collation(ifnull(concat(NULL),ifnull(concat(NULL),'~')));
select collation(ifnull(concat(id),'~')) from t;
select collation(ifnull(concat(NULL),ifnull(concat(id),'~'))) from t;
select collation(ifnull(concat(id),ifnull(concat(id),'~'))) from t;
select collation(ifnull(concat(NULL),id)) from t1;
select collation(ifnull(concat(NULL),ifnull(concat(NULL),id))) from t1;

0 comments on commit 0f107c2

Please sign in to comment.