From 1a455d0ff3cf309cca3c8d7ea52946283f22e867 Mon Sep 17 00:00:00 2001 From: xzhangxian1008 Date: Fri, 3 Jan 2025 18:01:34 +0800 Subject: [PATCH] expression: fix incorrect short path in `truncate` function (#58656) close pingcap/tidb#57608 --- pkg/expression/builtin_math.go | 7 ++++--- .../integration_test/integration_test.go | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/pkg/expression/builtin_math.go b/pkg/expression/builtin_math.go index 0e53a432f6..f2c62f6cd0 100644 --- a/pkg/expression/builtin_math.go +++ b/pkg/expression/builtin_math.go @@ -2157,15 +2157,16 @@ func (b *builtinTruncateIntSig) evalInt(ctx EvalContext, row chunk.Row) (int64, if isNull || err != nil { return 0, isNull, err } - if mysql.HasUnsignedFlag(b.args[1].GetType(ctx).GetFlag()) { - return x, false, nil - } d, isNull, err := b.args[1].EvalInt(ctx, row) if isNull || err != nil { return 0, isNull, err } + if mysql.HasUnsignedFlag(b.args[1].GetType(ctx).GetFlag()) { + return x, false, nil + } + if d >= 0 { return x, false, nil } diff --git a/pkg/expression/integration_test/integration_test.go b/pkg/expression/integration_test/integration_test.go index 3943b9239e..485d10677f 100644 --- a/pkg/expression/integration_test/integration_test.go +++ b/pkg/expression/integration_test/integration_test.go @@ -4158,3 +4158,19 @@ func TestIssue55886(t *testing.T) { " (t1 as ref_4 right outer join t2 as ref_5 on ref_5.c_g7eofzlxn != 1)), cte_4 as (select 1 as c1 from t2) select ref_34.c1 as c5 from" + " cte_0 as ref_34 where exists (select 1 from cte_4 as ref_35 where ref_34.c1 <= case when ref_34.c5 then cast(1 as char) else ref_34.c5 end);") } + +func TestIssue57608(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t1;") + tk.MustExec("create table t1 ( c1 int primary key);") + tk.MustExec("insert into t1 (c1) values (1), (2), (3), (4), (5), (6), (7), (11), (12), (13), (14), (15), (16), (17), (21), (22), (23), (24), (25), (26), (27), (116), (127), (121), (122), (113), (214), (251), (261), (217), (91), (92), (39), (94), (95), (69), (79), (191), (129);") + tk.MustExec("create view v2 as select 0 as q2 from t1;") + + for i := 0; i < 10; i++ { + tk.MustQuery("select distinct 1 between NULL and 1 as w0, truncate(1, (cast(ref_1.q2 as unsigned) % 0)) as w1, (1 between truncate(1, (cast(ref_1.q2 as unsigned) % 0)) and 1) as w2 from (v2 as ref_0 inner join v2 as ref_1 on (1=1));").Check(testkit.Rows( + " ", + )) + } +}