From 73d3b903865146cdb595713d67da0ce67a7cffaf Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Thu, 26 Dec 2024 16:51:48 +0800 Subject: [PATCH] expression: use maximum length for integer display (#56463) (#56927) close pingcap/tidb#41719, close pingcap/tidb#45338, close pingcap/tidb#56462 --- pkg/expression/builtin_control.go | 23 +++++++++++++++---- .../r/expression/issues.result | 6 +++++ .../integrationtest/t/expression/issues.test | 6 +++++ 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/pkg/expression/builtin_control.go b/pkg/expression/builtin_control.go index af34824fa52e9..f592d0131252b 100644 --- a/pkg/expression/builtin_control.go +++ b/pkg/expression/builtin_control.go @@ -83,12 +83,25 @@ func setFlenFromArgs(evalType types.EvalType, resultFieldType *types.FieldType, } else if evalType == types.ETString { maxLen := 0 for i := range argTps { - argFlen := argTps[i].GetFlen() - if argFlen == types.UnspecifiedLength { - resultFieldType.SetFlen(types.UnspecifiedLength) - return + switch argTps[i].GetType() { + case mysql.TypeTiny: + maxLen = maxlen(4, maxLen) + case mysql.TypeShort: + maxLen = maxlen(6, maxLen) + case mysql.TypeInt24: + maxLen = maxlen(9, maxLen) + case mysql.TypeLong: + maxLen = maxlen(11, maxLen) + case mysql.TypeLonglong: + maxLen = maxlen(20, maxLen) + default: + argFlen := argTps[i].GetFlen() + if argFlen == types.UnspecifiedLength { + resultFieldType.SetFlen(types.UnspecifiedLength) + return + } + maxLen = maxlen(argFlen, maxLen) } - maxLen = maxlen(argFlen, maxLen) } resultFieldType.SetFlen(maxLen) } else { diff --git a/tests/integrationtest/r/expression/issues.result b/tests/integrationtest/r/expression/issues.result index ca2c8e2dcd66d..902f07aac13d4 100644 --- a/tests/integrationtest/r/expression/issues.result +++ b/tests/integrationtest/r/expression/issues.result @@ -3192,3 +3192,9 @@ Projection 8.00 root test.ast.i, Column#32 └─TableReader(Probe) 10.00 root data:Selection └─Selection 10.00 cop[tikv] eq("astp2019121731703151", test.acc.m) └─TableFullScan 10000.00 cop[tikv] table:b keep order:false, stats:pseudo +DROP TABLE IF EXISTS test.t; +CREATE TABLE test.t (id bigint(11) UNSIGNED PRIMARY KEY); +INSERT INTO test.t VALUES (1234567890123456); +SELECT IFNULL(id, 'abcdef') FROM test.t; +IFNULL(id, 'abcdef') +1234567890123456 diff --git a/tests/integrationtest/t/expression/issues.test b/tests/integrationtest/t/expression/issues.test index 4c74962b84244..5b78120efd441 100644 --- a/tests/integrationtest/t/expression/issues.test +++ b/tests/integrationtest/t/expression/issues.test @@ -2161,3 +2161,9 @@ drop table if exists test.t; create table if not exists test.ast (i varchar(20)); create table if not exists test.acc (j varchar(20), k varchar(20), l varchar(20), m varchar(20)); explain format='brief' with t as(select i, (case when b.j = '20001' then b.l else b.k end) an from test.ast a inner join test.acc b on (a.i = b.m) and a.i = 'astp2019121731703151'), t1 as (select i, group_concat(an order by an separator '; ') an from t group by i) select * from t1; + +# TestIssue56462 +DROP TABLE IF EXISTS test.t; +CREATE TABLE test.t (id bigint(11) UNSIGNED PRIMARY KEY); +INSERT INTO test.t VALUES (1234567890123456); +SELECT IFNULL(id, 'abcdef') FROM test.t;