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..90fbd3efcd6d0 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 diff --git a/tests/integrationtest/t/executor/issues.test b/tests/integrationtest/t/executor/issues.test index ee07ec984db0f..87d8cf0a424dc 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; + +# TestIssue56641 +select ((exists (select 1)) * -5) as c1; \ No newline at end of file