From 0e4eb8c4a4a470de91a5635a0533009e29064769 Mon Sep 17 00:00:00 2001 From: yisaer Date: Mon, 27 Jun 2022 13:54:20 +0800 Subject: [PATCH 1/5] fix code Signed-off-by: yisaer --- planner/core/optimizer.go | 1 + 1 file changed, 1 insertion(+) diff --git a/planner/core/optimizer.go b/planner/core/optimizer.go index e868160af9800..57ba2023509e0 100644 --- a/planner/core/optimizer.go +++ b/planner/core/optimizer.go @@ -332,6 +332,7 @@ func refineCETrace(sctx sessionctx.Context) { if !ok { logutil.BgLogger().Warn("[OptimizerTrace] Failed to find table in infoschema", zap.Int64("table id", rec.TableID)) + continue } rec.TableName = tbl.Meta().Name.O } From 64b8a4f44f03c6d716ce143d8622cd43f8aee7b2 Mon Sep 17 00:00:00 2001 From: yisaer Date: Mon, 27 Jun 2022 15:06:27 +0800 Subject: [PATCH 2/5] fix partition table Signed-off-by: yisaer --- planner/core/optimizer.go | 15 ++++++++++----- planner/core/stats.go | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/planner/core/optimizer.go b/planner/core/optimizer.go index 57ba2023509e0..fde76b3a41eec 100644 --- a/planner/core/optimizer.go +++ b/planner/core/optimizer.go @@ -326,15 +326,20 @@ func refineCETrace(sctx sessionctx.Context) { return i.RowCount < j.RowCount }) traceRecords := stmtCtx.OptimizerCETrace - is := sctx.GetInfoSchema().(infoschema.InfoSchema) + is := sctx.GetDomainInfoSchema().(infoschema.InfoSchema) for _, rec := range traceRecords { tbl, ok := is.TableByID(rec.TableID) - if !ok { - logutil.BgLogger().Warn("[OptimizerTrace] Failed to find table in infoschema", - zap.Int64("table id", rec.TableID)) + if ok { + rec.TableName = tbl.Meta().Name.O continue } - rec.TableName = tbl.Meta().Name.O + tbl, _, _ = is.FindTableByPartitionID(rec.TableID) + if tbl != nil { + rec.TableName = tbl.Meta().Name.O + continue + } + logutil.BgLogger().Warn("[OptimizerTrace] Failed to find table in infoschema", + zap.Int64("table id", rec.TableID)) } } diff --git a/planner/core/stats.go b/planner/core/stats.go index 216ec5112166a..4f06a0b2cae30 100644 --- a/planner/core/stats.go +++ b/planner/core/stats.go @@ -225,7 +225,7 @@ func (ds *DataSource) initStats(colGroups [][]*expression.Column) { return } if ds.statisticTable == nil { - ds.statisticTable = getStatsTable(ds.ctx, ds.tableInfo, ds.physicalTableID) + ds.statisticTable = getStatsTable(ds.ctx, ds.tableInfo, ds.tableInfo.ID) } tableStats := &property.StatsInfo{ RowCount: float64(ds.statisticTable.Count), From 9bab1776363be0940e17206b3c285c791358b5e0 Mon Sep 17 00:00:00 2001 From: yisaer Date: Mon, 27 Jun 2022 15:07:26 +0800 Subject: [PATCH 3/5] fix partition table Signed-off-by: yisaer --- planner/core/stats.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/planner/core/stats.go b/planner/core/stats.go index 4f06a0b2cae30..216ec5112166a 100644 --- a/planner/core/stats.go +++ b/planner/core/stats.go @@ -225,7 +225,7 @@ func (ds *DataSource) initStats(colGroups [][]*expression.Column) { return } if ds.statisticTable == nil { - ds.statisticTable = getStatsTable(ds.ctx, ds.tableInfo, ds.tableInfo.ID) + ds.statisticTable = getStatsTable(ds.ctx, ds.tableInfo, ds.physicalTableID) } tableStats := &property.StatsInfo{ RowCount: float64(ds.statisticTable.Count), From 19c1262ded9ed17d97692b9649fff32357e84f16 Mon Sep 17 00:00:00 2001 From: yisaer Date: Mon, 27 Jun 2022 15:51:04 +0800 Subject: [PATCH 4/5] fix test Signed-off-by: yisaer --- statistics/trace_test.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/statistics/trace_test.go b/statistics/trace_test.go index 03d3c71e7f8ca..6ddb6ae757721 100644 --- a/statistics/trace_test.go +++ b/statistics/trace_test.go @@ -100,3 +100,28 @@ func TestTraceCE(t *testing.T) { require.ElementsMatch(t, resultJSON, out[i].Trace) } } + +func TestTraceCEPartitionTable(t *testing.T) { + store, _, clean := testkit.CreateMockStoreAndDomain(t) + defer clean() + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t") + tk.MustExec("create table t(a int, b int, d varchar(10), index idx(a, b))") + tk.MustExec(`insert into t values(1, 1, "aaa"), + (1, 1, "bbb"), + (1, 2, "ccc"), + (1, 2, "ddd"), + (2, 2, "aaa"), + (2, 3, "bbb")`) + tk.MustExec("analyze table t") + result := tk.MustQuery("trace plan target='estimation' select * from t where a >=1") + require.Len(t, result.Rows(), 1) + resultStr := result.Rows()[0][0].(string) + var resultJSON []*tracing.CETraceRecord + err := json.Unmarshal([]byte(resultStr), &resultJSON) + require.NoError(t, err) + for _, r := range resultJSON { + require.Equal(t, "t", r.TableName) + } +} From 190d7d877927ac1952c099ce21b19f86a7b467a4 Mon Sep 17 00:00:00 2001 From: yisaer Date: Tue, 28 Jun 2022 12:58:14 +0800 Subject: [PATCH 5/5] fix test Signed-off-by: yisaer --- statistics/trace_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/statistics/trace_test.go b/statistics/trace_test.go index 6ddb6ae757721..144dbff27d530 100644 --- a/statistics/trace_test.go +++ b/statistics/trace_test.go @@ -107,7 +107,7 @@ func TestTraceCEPartitionTable(t *testing.T) { tk := testkit.NewTestKit(t, store) tk.MustExec("use test") tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a int, b int, d varchar(10), index idx(a, b))") + tk.MustExec("create table t(a int, b int, d varchar(10), index idx(a, b)) PARTITION BY RANGE (a) (PARTITION p0 VALUES LESS THAN MAXVALUE);") tk.MustExec(`insert into t values(1, 1, "aaa"), (1, 1, "bbb"), (1, 2, "ccc"),