From 9f1e44e4d3ff663028798082482399391f55c1c6 Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Thu, 15 Jun 2023 18:05:10 +0800 Subject: [PATCH] planner: add more test cases for quick binding (#44541) (#44677) ref pingcap/tidb#39199 --- infoschema/cluster_tables_test.go | 158 ++++++++++++++++++++++++++++++ infoschema/tables_test.go | 2 + 2 files changed, 160 insertions(+) diff --git a/infoschema/cluster_tables_test.go b/infoschema/cluster_tables_test.go index 884658871a8d1..60bb77da4267a 100644 --- a/infoschema/cluster_tables_test.go +++ b/infoschema/cluster_tables_test.go @@ -878,6 +878,164 @@ func TestMDLView(t *testing.T) { } } +func TestQuickBinding(t *testing.T) { + s := new(clusterTablesSuite) + s.store, s.dom = testkit.CreateMockStoreAndDomain(t) + s.rpcserver, s.listenAddr = s.setUpRPCService(t, "127.0.0.1:0", nil) + s.httpServer, s.mockAddr = s.setUpMockPDHTTPServer() + s.startTime = time.Now() + defer s.httpServer.Close() + defer s.rpcserver.Stop() + tk := s.newTestKitWithRoot(t) + require.NoError(t, tk.Session().Auth(&auth.UserIdentity{Username: "root", Hostname: "%"}, nil, nil, nil)) + + tk.MustExec("use test") + tk.MustExec(`create table t1 (pk int, a int, b int, c int, primary key(pk), key k_a(a), key k_bc(b, c))`) + tk.MustExec(`create table t2 (a int, b int, c int, key k_a(a), key k_bc(b, c))`) // no primary key + + type testCase struct { + template string + expectedHint string + dmlAndSubqueryTemplates []string + } + defaultDMLAndSubqueryTemplates := []string{ + //"select a from (%v) tx where tx.a<1", // TODO: support sub query + "insert into t1 %v", + // TODO: more templates + } + testCases := []testCase{ + // access path selection with use_index / ignore_index + {`select /*+ use_index(t1, k_a) */ * from t1 where b=?`, "use_index(@`sel_1` `test`.`t1` `k_a`)", defaultDMLAndSubqueryTemplates}, + {`select /*+ use_index(t1, k_bc) */ * from t1 where a=?`, "use_index(@`sel_1` `test`.`t1` `k_bc`)", defaultDMLAndSubqueryTemplates}, + {`select /*+ use_index(t1, primary) */ * from t1 where a=? and b=?`, "use_index(@`sel_1` `test`.`t1` )", defaultDMLAndSubqueryTemplates}, + {`select /*+ ignore_index(t1, k_a, k_bc) */ * from t1 where a=? and b=?`, "use_index(@`sel_1` `test`.`t1` ), ignore_index(`t1` `k_a`, `k_bc`)", defaultDMLAndSubqueryTemplates}, + {`select /*+ use_index(t1) */ * from t1 where a=? and b=?`, "use_index(@`sel_1` `test`.`t1` )", defaultDMLAndSubqueryTemplates}, + {`select /*+ use_index(t2) */ * from t2 where a=? and b=?`, "use_index(@`sel_1` `test`.`t2` )", nil}, + + // aggregation + {`select /*+ hash_agg(), use_index(t1, primary) */ count(*) from t1 where a