From de6e2ba2b21c48769e99aea8ee4faf5d7f8d894a Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Wed, 29 Nov 2023 16:31:19 +0800 Subject: [PATCH] planner: fix nil pointer at expression.(*CorrelatedColumn).Eval (#42789) (#48975) close pingcap/tidb#42739 --- planner/core/logical_plan_builder.go | 2 ++ planner/core/prepare_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/planner/core/logical_plan_builder.go b/planner/core/logical_plan_builder.go index abaa903ea6a5a..48821d35a17e7 100644 --- a/planner/core/logical_plan_builder.go +++ b/planner/core/logical_plan_builder.go @@ -249,6 +249,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]] @@ -270,6 +271,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/planner/core/prepare_test.go b/planner/core/prepare_test.go index 118a522152590..43745cece0503 100644 --- a/planner/core/prepare_test.go +++ b/planner/core/prepare_test.go @@ -3065,3 +3065,28 @@ func TestIssue42150(t *testing.T) { tk.MustExec("execute st") tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1")) } + +func TestIssue42739(t *testing.T) { + store, clean := testkit.CreateMockStore(t) + defer clean() + tk := testkit.NewTestKit(t, store) + tk.MustExec(`use test`) + tk.MustExec("drop table if exists t0;") + tk.MustExec("CREATE TABLE t0 (c1 double, c2 double);") + tk.MustExec(`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;`) +}