From 5a36af206cfb3b97bd1d7f0ce24abc368f7f588b Mon Sep 17 00:00:00 2001 From: Mingcong Han Date: Thu, 7 May 2020 14:27:05 +0800 Subject: [PATCH 1/2] cherry pick #16927 to release-3.0 Signed-off-by: sre-bot --- planner/core/find_best_task.go | 5 +++- planner/core/integration_test.go | 44 ++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/planner/core/find_best_task.go b/planner/core/find_best_task.go index 0030510e1e31d..125b6da8b435c 100644 --- a/planner/core/find_best_task.go +++ b/planner/core/find_best_task.go @@ -66,7 +66,10 @@ func getPropByOrderByItems(items []*ByItems) (*property.PhysicalProperty, bool) } func (p *LogicalTableDual) findBestTask(prop *property.PhysicalProperty) (task, error) { - if !prop.IsEmpty() { + // If the required property is not empty and the row count > 1, + // we cannot ensure this required property. + // But if the row count is 0 or 1, we don't need to care about the property. + if !prop.IsEmpty() && p.RowCount > 1 { return invalidTask, nil } dual := PhysicalTableDual{ diff --git a/planner/core/integration_test.go b/planner/core/integration_test.go index 9781e6ae2ef6f..82b36f0d92e83 100644 --- a/planner/core/integration_test.go +++ b/planner/core/integration_test.go @@ -375,3 +375,47 @@ partition p2 values less than (unix_timestamp('2020-04-15 00:00:00')))`) tk.MustQuery("select count(*) from floor_unix_timestamp where ts <= '2020-04-05 23:00:00'").Check(testkit.Rows("4")) tk.MustQuery("select * from floor_unix_timestamp partition(p1, p2) where ts > '2020-04-14 00:00:00'").Check(testkit.Rows("2020-04-14 00:00:42.000")) } +<<<<<<< HEAD +======= + +func (s *testIntegrationSuite) TestIssue16290And16292(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t;") + tk.MustExec("create table t(a int, b int, primary key(a));") + tk.MustExec("insert into t values(1, 1);") + + for i := 0; i <= 1; i++ { + tk.MustExec(fmt.Sprintf("set session tidb_opt_agg_push_down = %v", i)) + + tk.MustQuery("select avg(a) from (select * from t ta union all select * from t tb) t;").Check(testkit.Rows("1.0000")) + tk.MustQuery("select avg(b) from (select * from t ta union all select * from t tb) t;").Check(testkit.Rows("1.0000")) + tk.MustQuery("select count(distinct a) from (select * from t ta union all select * from t tb) t;").Check(testkit.Rows("1")) + tk.MustQuery("select count(distinct b) from (select * from t ta union all select * from t tb) t;").Check(testkit.Rows("1")) + } +} + +func (s *testIntegrationSuite) TestTableDualWithRequiredProperty(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t1, t2;") + tk.MustExec("create table t1 (a int, b int) partition by range(a) " + + "(partition p0 values less than(10), partition p1 values less than MAXVALUE)") + tk.MustExec("create table t2 (a int, b int)") + tk.MustExec("select /*+ MERGE_JOIN(t1, t2) */ * from t1 partition (p0), t2 where t1.a > 100 and t1.a = t2.a") +} + +func (s *testIntegrationSerialSuite) TestIssue16837(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t") + tk.MustExec("create table t(a int,b int,c int,d int,e int,unique key idx_ab(a,b),unique key(c),unique key(d))") + tk.MustQuery("explain select /*+ use_index_merge(t,c,idx_ab) */ * from t where a = 1 or (e = 1 and c = 1)").Check(testkit.Rows( + "TableReader_7 8000.00 root data:Selection_6", + "└─Selection_6 8000.00 cop[tikv] or(eq(test.t.a, 1), and(eq(test.t.e, 1), eq(test.t.c, 1)))", + " └─TableFullScan_5 10000.00 cop[tikv] table:t keep order:false, stats:pseudo")) + tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1105 IndexMerge is inapplicable or disabled")) + tk.MustExec("insert into t values (2, 1, 1, 1, 2)") + tk.MustQuery("select /*+ use_index_merge(t,c,idx_ab) */ * from t where a = 1 or (e = 1 and c = 1)").Check(testkit.Rows()) +} +>>>>>>> 014d101... planner: allow TableDual to match the required property if the row count is 0 (#16927) From c17cd74cc58f5fc2cdd5fcbafc73e88ffd4b1df2 Mon Sep 17 00:00:00 2001 From: Mingcong Han Date: Thu, 7 May 2020 15:12:51 +0800 Subject: [PATCH 2/2] resolve conflicts --- planner/core/integration_test.go | 34 -------------------------------- 1 file changed, 34 deletions(-) diff --git a/planner/core/integration_test.go b/planner/core/integration_test.go index 82b36f0d92e83..1a055f4fc9395 100644 --- a/planner/core/integration_test.go +++ b/planner/core/integration_test.go @@ -375,25 +375,6 @@ partition p2 values less than (unix_timestamp('2020-04-15 00:00:00')))`) tk.MustQuery("select count(*) from floor_unix_timestamp where ts <= '2020-04-05 23:00:00'").Check(testkit.Rows("4")) tk.MustQuery("select * from floor_unix_timestamp partition(p1, p2) where ts > '2020-04-14 00:00:00'").Check(testkit.Rows("2020-04-14 00:00:42.000")) } -<<<<<<< HEAD -======= - -func (s *testIntegrationSuite) TestIssue16290And16292(c *C) { - tk := testkit.NewTestKit(c, s.store) - tk.MustExec("use test") - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t(a int, b int, primary key(a));") - tk.MustExec("insert into t values(1, 1);") - - for i := 0; i <= 1; i++ { - tk.MustExec(fmt.Sprintf("set session tidb_opt_agg_push_down = %v", i)) - - tk.MustQuery("select avg(a) from (select * from t ta union all select * from t tb) t;").Check(testkit.Rows("1.0000")) - tk.MustQuery("select avg(b) from (select * from t ta union all select * from t tb) t;").Check(testkit.Rows("1.0000")) - tk.MustQuery("select count(distinct a) from (select * from t ta union all select * from t tb) t;").Check(testkit.Rows("1")) - tk.MustQuery("select count(distinct b) from (select * from t ta union all select * from t tb) t;").Check(testkit.Rows("1")) - } -} func (s *testIntegrationSuite) TestTableDualWithRequiredProperty(c *C) { tk := testkit.NewTestKit(c, s.store) @@ -404,18 +385,3 @@ func (s *testIntegrationSuite) TestTableDualWithRequiredProperty(c *C) { tk.MustExec("create table t2 (a int, b int)") tk.MustExec("select /*+ MERGE_JOIN(t1, t2) */ * from t1 partition (p0), t2 where t1.a > 100 and t1.a = t2.a") } - -func (s *testIntegrationSerialSuite) TestIssue16837(c *C) { - tk := testkit.NewTestKit(c, s.store) - tk.MustExec("use test") - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a int,b int,c int,d int,e int,unique key idx_ab(a,b),unique key(c),unique key(d))") - tk.MustQuery("explain select /*+ use_index_merge(t,c,idx_ab) */ * from t where a = 1 or (e = 1 and c = 1)").Check(testkit.Rows( - "TableReader_7 8000.00 root data:Selection_6", - "└─Selection_6 8000.00 cop[tikv] or(eq(test.t.a, 1), and(eq(test.t.e, 1), eq(test.t.c, 1)))", - " └─TableFullScan_5 10000.00 cop[tikv] table:t keep order:false, stats:pseudo")) - tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1105 IndexMerge is inapplicable or disabled")) - tk.MustExec("insert into t values (2, 1, 1, 1, 2)") - tk.MustQuery("select /*+ use_index_merge(t,c,idx_ab) */ * from t where a = 1 or (e = 1 and c = 1)").Check(testkit.Rows()) -} ->>>>>>> 014d101... planner: allow TableDual to match the required property if the row count is 0 (#16927)