From 4c1f97bca9ea5dcd01bfafa688b08b67b62d6d2f Mon Sep 17 00:00:00 2001 From: xufei Date: Mon, 11 Nov 2024 14:09:03 +0800 Subject: [PATCH 1/2] save work Signed-off-by: xufei --- pkg/expression/constant.go | 26 +++++++++++++++++-- pkg/planner/core/expression_rewriter.go | 4 +-- .../integrationtest/r/executor/issues.result | 3 +++ tests/integrationtest/t/executor/issues.test | 3 +++ 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/pkg/expression/constant.go b/pkg/expression/constant.go index c39684f3332c3..6bc3a8b45423c 100644 --- a/pkg/expression/constant.go +++ b/pkg/expression/constant.go @@ -32,7 +32,7 @@ import ( var _ base.HashEquals = &Constant{} -// NewOne stands for a number 1. +// NewOne stands for an unsigned number 1. func NewOne() *Constant { retT := types.NewFieldType(mysql.TypeTiny) retT.AddFlag(mysql.UnsignedFlag) // shrink range to avoid integral promotion @@ -44,7 +44,18 @@ func NewOne() *Constant { } } -// NewZero stands for a number 0. +// NewSignedOne stands for a signed number 1. +func NewSignedOne() *Constant { + retT := types.NewFieldType(mysql.TypeTiny) + retT.SetFlen(1) + retT.SetDecimal(0) + return &Constant{ + Value: types.NewDatum(1), + RetType: retT, + } +} + +// NewZero stands for an unsigned number 0. func NewZero() *Constant { retT := types.NewFieldType(mysql.TypeTiny) retT.AddFlag(mysql.UnsignedFlag) // shrink range to avoid integral promotion @@ -56,6 +67,17 @@ func NewZero() *Constant { } } +// NewSignedZero stands for a signed number 0. +func NewSignedZero() *Constant { + retT := types.NewFieldType(mysql.TypeTiny) + retT.SetFlen(1) + retT.SetDecimal(0) + return &Constant{ + Value: types.NewDatum(0), + RetType: retT, + } +} + // NewUInt64Const stands for constant of a given number. func NewUInt64Const(num int) *Constant { retT := types.NewFieldType(mysql.TypeLonglong) diff --git a/pkg/planner/core/expression_rewriter.go b/pkg/planner/core/expression_rewriter.go index 8024d545cc614..42306c205d43c 100644 --- a/pkg/planner/core/expression_rewriter.go +++ b/pkg/planner/core/expression_rewriter.go @@ -1103,9 +1103,9 @@ func (er *expressionRewriter) handleExistSubquery(ctx context.Context, planCtx * return v, true } if (row != nil && !v.Not) || (row == nil && v.Not) { - er.ctxStackAppend(expression.NewOne(), types.EmptyName) + er.ctxStackAppend(expression.NewSignedOne(), types.EmptyName) } else { - er.ctxStackAppend(expression.NewZero(), types.EmptyName) + er.ctxStackAppend(expression.NewSignedZero(), types.EmptyName) } } return v, true diff --git a/tests/integrationtest/r/executor/issues.result b/tests/integrationtest/r/executor/issues.result index d69b1e6072883..d4e6647eed8db 100644 --- a/tests/integrationtest/r/executor/issues.result +++ b/tests/integrationtest/r/executor/issues.result @@ -1006,3 +1006,6 @@ select from_unixtime( if(col2 >9999999999, col2/1000, col2), '%Y-%m-%d %H:%i:%s' result 2024-09-03 00:00:00 2024-09-03 00:00:00 +select ((exists (select 1)) * -5) as c1; +c1 +-5 \ No newline at end of file diff --git a/tests/integrationtest/t/executor/issues.test b/tests/integrationtest/t/executor/issues.test index ee07ec984db0f..a93478b62beab 100644 --- a/tests/integrationtest/t/executor/issues.test +++ b/tests/integrationtest/t/executor/issues.test @@ -767,3 +767,6 @@ explain analyze select * from pt where val = 126 order by id limit 100; # expec CREATE TABLE test_55837 (col1 int(4) NOT NULL, col2 bigint(4) NOT NULL, KEY col2_index (col2)); insert into test_55837 values(0,1725292800),(0,1725292800); select from_unixtime( if(col2 >9999999999, col2/1000, col2), '%Y-%m-%d %H:%i:%s') as result from test_55837; + +# TestIssue22941 +select ((exists (select 1)) * -5) as c1; \ No newline at end of file From 80ba7b2944f006c0b2490c51bf18bc2c0942b581 Mon Sep 17 00:00:00 2001 From: xufei Date: Mon, 11 Nov 2024 15:35:14 +0800 Subject: [PATCH 2/2] fix tests Signed-off-by: xufei --- tests/integrationtest/r/executor/issues.result | 2 +- tests/integrationtest/t/executor/issues.test | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/integrationtest/r/executor/issues.result b/tests/integrationtest/r/executor/issues.result index d4e6647eed8db..90fbd3efcd6d0 100644 --- a/tests/integrationtest/r/executor/issues.result +++ b/tests/integrationtest/r/executor/issues.result @@ -1008,4 +1008,4 @@ result 2024-09-03 00:00:00 select ((exists (select 1)) * -5) as c1; c1 --5 \ No newline at end of file +-5 diff --git a/tests/integrationtest/t/executor/issues.test b/tests/integrationtest/t/executor/issues.test index a93478b62beab..87d8cf0a424dc 100644 --- a/tests/integrationtest/t/executor/issues.test +++ b/tests/integrationtest/t/executor/issues.test @@ -768,5 +768,5 @@ CREATE TABLE test_55837 (col1 int(4) NOT NULL, col2 bigint(4) NOT NULL, KEY col2 insert into test_55837 values(0,1725292800),(0,1725292800); select from_unixtime( if(col2 >9999999999, col2/1000, col2), '%Y-%m-%d %H:%i:%s') as result from test_55837; -# TestIssue22941 +# TestIssue56641 select ((exists (select 1)) * -5) as c1; \ No newline at end of file