diff --git a/pkg/planner/core/logical_plan_builder.go b/pkg/planner/core/logical_plan_builder.go index 579e6cabfb935..40b81243284d7 100644 --- a/pkg/planner/core/logical_plan_builder.go +++ b/pkg/planner/core/logical_plan_builder.go @@ -413,6 +413,7 @@ func (b *PlanBuilder) buildAggregation(ctx context.Context, p LogicalPlan, aggFu if _, ok = b.correlatedAggMapper[aggFuncList[j]]; !ok { b.correlatedAggMapper[aggFuncList[j]] = &expression.CorrelatedColumn{ Column: *schema4Agg.Columns[aggIndexMap[j]], + Data: new(types.Datum), } } b.correlatedAggMapper[aggFunc] = b.correlatedAggMapper[aggFuncList[j]] @@ -434,6 +435,7 @@ func (b *PlanBuilder) buildAggregation(ctx context.Context, p LogicalPlan, aggFu if _, ok := correlatedAggMap[aggFunc]; ok { b.correlatedAggMapper[aggFunc] = &expression.CorrelatedColumn{ Column: column, + Data: new(types.Datum), } } } diff --git a/tests/integrationtest/r/planner/core/tests/prepare/issue.result b/tests/integrationtest/r/planner/core/tests/prepare/issue.result index ee850afa574b9..f1885588db9f4 100644 --- a/tests/integrationtest/r/planner/core/tests/prepare/issue.result +++ b/tests/integrationtest/r/planner/core/tests/prepare/issue.result @@ -561,3 +561,24 @@ select @@last_plan_from_cache; @@last_plan_from_cache 1 set tidb_enable_prepared_plan_cache=DEFAULT; +use test; +drop table if exists t0; +CREATE TABLE t0 (c1 double, c2 double); +select +exists ( +select +subq_2.c0 as c8 +from +(select +ref_153.c1 as c0 +from +t0 as ref_153 +group by ref_153.c1 having 0 <> ( +select 1 +from +t0 as ref_173 +where count(ref_153.c2) = avg(ref_153.c2) +order by c1 desc limit 1)) as subq_2 +) as c10; +c10 +0 diff --git a/tests/integrationtest/t/planner/core/tests/prepare/issue.test b/tests/integrationtest/t/planner/core/tests/prepare/issue.test index 5a2f9ba2a5766..50a7448d34eb1 100644 --- a/tests/integrationtest/t/planner/core/tests/prepare/issue.test +++ b/tests/integrationtest/t/planner/core/tests/prepare/issue.test @@ -405,3 +405,24 @@ select @@last_plan_from_cache; set tidb_enable_prepared_plan_cache=DEFAULT; +# TestIssue42739 + +use test; +drop table if exists t0; +CREATE TABLE t0 (c1 double, c2 double); +select + exists ( + select + subq_2.c0 as c8 + from + (select + ref_153.c1 as c0 + from + t0 as ref_153 + group by ref_153.c1 having 0 <> ( + select 1 + from + t0 as ref_173 + where count(ref_153.c2) = avg(ref_153.c2) + order by c1 desc limit 1)) as subq_2 + ) as c10;