Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expression: set a collation according to the arguments for ifnull in constant folding (#52119) #52131

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions expression/constant_fold.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,19 @@ 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() {
<<<<<<< HEAD:expression/constant_fold.go
return foldConstant(args[1])
=======
foldedExpr, isConstant := foldConstant(ctx, 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
>>>>>>> 7b8fd3729f7 (expression: set a collation according to the arguments for `ifnull` in constant folding (#52119)):pkg/expression/constant_fold.go
}
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;