Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: let exists subquery return signed int type #57277

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions pkg/expression/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions pkg/planner/core/expression_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions tests/integrationtest/r/executor/issues.result
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions tests/integrationtest/t/executor/issues.test
Original file line number Diff line number Diff line change
Expand Up @@ -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;