From b0c3ab1c10371f76f3c0cc8d1f69f69cca1de94a Mon Sep 17 00:00:00 2001 From: tuziemon Date: Thu, 8 Aug 2024 00:33:15 +0900 Subject: [PATCH] expression: fix incorrect copy of flag in truncate function --- pkg/expression/builtin_math.go | 5 ++++- tests/integrationtest/r/expression/constant_fold.result | 6 ++++++ tests/integrationtest/t/expression/constant_fold.test | 6 ++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/pkg/expression/builtin_math.go b/pkg/expression/builtin_math.go index bc909802c19aa..a98007e10035f 100644 --- a/pkg/expression/builtin_math.go +++ b/pkg/expression/builtin_math.go @@ -1921,7 +1921,10 @@ func (c *truncateFunctionClass) getFunction(ctx BuildContext, args []Expression) bf.tp.SetDecimalUnderLimit(calculateDecimal4RoundAndTruncate(ctx, args, argTp)) bf.tp.SetFlenUnderLimit(args[0].GetType(ctx.GetEvalCtx()).GetFlen() - args[0].GetType(ctx.GetEvalCtx()).GetDecimal() + bf.tp.GetDecimal()) } - bf.tp.AddFlag(args[0].GetType(ctx.GetEvalCtx()).GetFlag()) + argFieldTp := args[0].GetType(ctx.GetEvalCtx()) + if mysql.HasUnsignedFlag(argFieldTp.GetFlag()) { + bf.tp.AddFlag(mysql.UnsignedFlag) + } var sig builtinFunc switch argTp { diff --git a/tests/integrationtest/r/expression/constant_fold.result b/tests/integrationtest/r/expression/constant_fold.result index 05becb4d7a29e..d50ad250c7552 100644 --- a/tests/integrationtest/r/expression/constant_fold.result +++ b/tests/integrationtest/r/expression/constant_fold.result @@ -24,3 +24,9 @@ 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 +drop table if exists t1; +create table t1 (c1 int); +insert into t1 values (null); +select truncate(1,c1), truncate(1,c1) is not NULL from t1; +truncate(1,c1) truncate(1,c1) is not NULL +NULL 0 diff --git a/tests/integrationtest/t/expression/constant_fold.test b/tests/integrationtest/t/expression/constant_fold.test index 43a09302ca740..2db03c24f0a0c 100644 --- a/tests/integrationtest/t/expression/constant_fold.test +++ b/tests/integrationtest/t/expression/constant_fold.test @@ -11,3 +11,9 @@ 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; + +# https://github.com/pingcap/tidb/issues/53546 +drop table if exists t1; +create table t1 (c1 int); +insert into t1 values (null); +select truncate(1,c1), truncate(1,c1) is not NULL from t1;