From 83ea321e5c5e723779e0701270bb8725587005cb Mon Sep 17 00:00:00 2001 From: Zhang Jian Date: Fri, 30 Mar 2018 16:20:01 +0800 Subject: [PATCH] plan: statsInfo.count for LogicalTableDual should be its RowCount (#6190) --- plan/cbo_test.go | 29 +++++++++++++++++++++++++++++ plan/stats.go | 12 ++++++++++++ 2 files changed, 41 insertions(+) diff --git a/plan/cbo_test.go b/plan/cbo_test.go index fdc5dfaaaa620..2e89071d9abbb 100644 --- a/plan/cbo_test.go +++ b/plan/cbo_test.go @@ -123,6 +123,35 @@ func (s *testAnalyzeSuite) TestStraightJoin(c *C) { )) } +func (s *testAnalyzeSuite) TestTableDual(c *C) { + defer testleak.AfterTest(c)() + store, dom, err := newStoreWithBootstrap() + c.Assert(err, IsNil) + defer func() { + dom.Close() + store.Close() + }() + + testKit := testkit.NewTestKit(c, store) + testKit.MustExec(`use test`) + h := dom.StatsHandle() + testKit.MustExec(`create table t(a int)`) + testKit.MustExec("insert into t values (1), (2), (3), (4), (5), (6), (7), (8), (9), (10)") + c.Assert(h.HandleDDLEvent(<-h.DDLEventCh()), IsNil) + + h.DumpStatsDeltaToKV() + c.Assert(h.Update(dom.InfoSchema()), IsNil) + + testKit.MustQuery(`explain select * from t where 1 = 0`).Check(testkit.Rows( + `TableDual_6 Projection_5 root rows:0 0.00`, + `Projection_5 TableDual_6 root test.t.a 0.00`, + )) + + testKit.MustQuery(`explain select * from t where 1 = 1 limit 0`).Check(testkit.Rows( + `TableDual_5 root rows:0 0.00`, + )) +} + func (s *testAnalyzeSuite) TestEstimation(c *C) { defer testleak.AfterTest(c)() store, dom, err := newStoreWithBootstrap() diff --git a/plan/stats.go b/plan/stats.go index 5e69208b0d549..fb1b9a886f74f 100644 --- a/plan/stats.go +++ b/plan/stats.go @@ -62,6 +62,18 @@ func (p *basePhysicalPlan) StatsInfo() *statsInfo { return p.stats } +func (p *LogicalTableDual) deriveStats() *statsInfo { + profile := &statsInfo{ + count: float64(p.RowCount), + cardinality: make([]float64, p.Schema().Len()), + } + for i := range profile.cardinality { + profile.cardinality[i] = float64(p.RowCount) + } + p.stats = profile + return p.stats +} + func (p *baseLogicalPlan) deriveStats() *statsInfo { if len(p.children) == 0 { profile := &statsInfo{