From 0aa312bbeff4fb204c39aa06e2d3bee81a3a2053 Mon Sep 17 00:00:00 2001 From: zimulala Date: Mon, 1 Aug 2016 18:05:39 +0800 Subject: [PATCH] plan: fix convert to physical plan, when there are multiple index ranges (#1532) --- plan/match_property.go | 3 ++- plan/new_plan_test.go | 8 ++++++++ plan/physical_plan_builder.go | 13 +++++++++++-- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/plan/match_property.go b/plan/match_property.go index a08a202ed6..ec83f9a979 100644 --- a/plan/match_property.go +++ b/plan/match_property.go @@ -14,8 +14,9 @@ package plan import ( - "github.com/pingcap/tidb/util/types" "math" + + "github.com/pingcap/tidb/util/types" ) // matchProperty implements PhysicalPlan matchProperty interface. diff --git a/plan/new_plan_test.go b/plan/new_plan_test.go index 7c7dac7557..daa71c5d49 100644 --- a/plan/new_plan_test.go +++ b/plan/new_plan_test.go @@ -362,6 +362,14 @@ func (s *testPlanSuite) TestRefine(c *C) { sql: "select a from t where c <= 5 and c >= 3 and d = 1", best: "Index(t.c_d_e)[[3,5]]->Selection->Projection", }, + { + sql: "select a from t where c = 1 or c = 2 or c = 3", + best: "Index(t.c_d_e)[[1,1] [2,2] [3,3]]->Projection", + }, + { + sql: "select a from t where c = 1 or c = 2 or c = 3 or c = 4 or c = 5", + best: "Table(t)->Selection->Projection", + }, } for _, ca := range cases { comment := Commentf("for %s", ca.sql) diff --git a/plan/physical_plan_builder.go b/plan/physical_plan_builder.go index 205c6b5dbe..8ffeae62a7 100644 --- a/plan/physical_plan_builder.go +++ b/plan/physical_plan_builder.go @@ -14,13 +14,14 @@ package plan import ( + "math" + "github.com/juju/errors" "github.com/pingcap/tidb/expression" "github.com/pingcap/tidb/model" "github.com/pingcap/tidb/mysql" "github.com/pingcap/tidb/plan/statistics" "github.com/pingcap/tidb/util/types" - "math" ) const ( @@ -47,7 +48,15 @@ func getRowCountByIndexRange(table *statistics.Table, indexRange *IndexRange, in rowCount, err = table.Columns[offset].LessRowCount(r) rowCount = table.Count - rowCount } else { - rowCount, err = table.Columns[offset].BetweenRowCount(l, r) + compare, err1 := l.CompareDatum(r) + if err1 != nil { + return 0, errors.Trace(err1) + } + if compare == 0 { + rowCount, err = table.Columns[offset].EqualRowCount(l) + } else { + rowCount, err = table.Columns[offset].BetweenRowCount(l, r) + } } if err != nil { return 0, errors.Trace(err)