From 3aa520f61c9aea53910999f8459e82bb96e56865 Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Wed, 29 Nov 2023 17:19:20 +0800 Subject: [PATCH] planner: fix nil pointer at expression.(*CorrelatedColumn).Eval (#42789) (#48977) 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 0140375675105..dc91f4519315e 100644 --- a/planner/core/logical_plan_builder.go +++ b/planner/core/logical_plan_builder.go @@ -288,6 +288,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]] @@ -309,6 +310,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 c4d31a6aaceff..89330fb4b5e01 100644 --- a/planner/core/prepare_test.go +++ b/planner/core/prepare_test.go @@ -2806,3 +2806,28 @@ func TestIssue37901(t *testing.T) { tk.MustExec(`execute st1 using @t`) tk.MustQuery(`select count(*) from t4`).Check(testkit.Rows("2")) } + +func TestIssue42739(t *testing.T) { + store := testkit.CreateMockStore(t) + 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;`) +}