From 0cddaf4699bb1e75e46c8fd981f4677918c0c662 Mon Sep 17 00:00:00 2001 From: qw4990 Date: Thu, 8 Jun 2023 20:49:02 +0800 Subject: [PATCH 1/8] fixup --- .../clustertablestest/cluster_tables_test.go | 134 ++++++++++++++++++ 1 file changed, 134 insertions(+) diff --git a/infoschema/test/clustertablestest/cluster_tables_test.go b/infoschema/test/clustertablestest/cluster_tables_test.go index bbe0bfc891376..7285cd4c2286d 100644 --- a/infoschema/test/clustertablestest/cluster_tables_test.go +++ b/infoschema/test/clustertablestest/cluster_tables_test.go @@ -879,6 +879,140 @@ 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))`) + + 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{ + {`select /*+ use_index(t1, k_a) */ * from t1 where a=? and b=?`, "use_index(@`sel_1` `test`.`t1` `k_a`)", defaultDMLAndSubqueryTemplates}, + {`select /*+ use_index(t1, k_a) */ * from t1 where a Date: Fri, 9 Jun 2023 12:10:41 +0800 Subject: [PATCH 2/8] fixup --- infoschema/test/clustertablestest/cluster_tables_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/infoschema/test/clustertablestest/cluster_tables_test.go b/infoschema/test/clustertablestest/cluster_tables_test.go index 7285cd4c2286d..dbec1a6a76417 100644 --- a/infoschema/test/clustertablestest/cluster_tables_test.go +++ b/infoschema/test/clustertablestest/cluster_tables_test.go @@ -989,6 +989,7 @@ func TestQuickBinding(t *testing.T) { tk.MustExec(fmt.Sprintf(`drop session binding for %s`, firstSQL)) } + // test with DML and sub-query for _, tc := range testCases { for _, temp := range tc.dmlAndSubqueryTemplates { temp = fmt.Sprintf(temp, tc.template) @@ -1009,6 +1010,8 @@ func TestQuickBinding(t *testing.T) { // has the same plan-digest tk.MustQuery(fmt.Sprintf(`select plan_digest from information_schema.statements_summary where digest='%v'`, sqlDigest)).Check(testkit.Rows(planDigest)) } + + tk.MustExec(fmt.Sprintf(`drop session binding for %s`, firstSQL)) } } } From 6402afa5636278c8ea805557db48101669920241 Mon Sep 17 00:00:00 2001 From: qw4990 Date: Fri, 9 Jun 2023 12:20:17 +0800 Subject: [PATCH 3/8] fixup --- .../test/clustertablestest/cluster_tables_test.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/infoschema/test/clustertablestest/cluster_tables_test.go b/infoschema/test/clustertablestest/cluster_tables_test.go index dbec1a6a76417..652fb3a36c99e 100644 --- a/infoschema/test/clustertablestest/cluster_tables_test.go +++ b/infoschema/test/clustertablestest/cluster_tables_test.go @@ -892,6 +892,7 @@ func TestQuickBinding(t *testing.T) { 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 @@ -904,12 +905,12 @@ func TestQuickBinding(t *testing.T) { // TODO: more templates } testCases := []testCase{ - {`select /*+ use_index(t1, k_a) */ * from t1 where a=? and b=?`, "use_index(@`sel_1` `test`.`t1` `k_a`)", defaultDMLAndSubqueryTemplates}, - {`select /*+ use_index(t1, k_a) */ * from t1 where a Date: Fri, 9 Jun 2023 12:27:58 +0800 Subject: [PATCH 4/8] fixup --- infoschema/test/clustertablestest/cluster_tables_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/infoschema/test/clustertablestest/cluster_tables_test.go b/infoschema/test/clustertablestest/cluster_tables_test.go index 652fb3a36c99e..180c3aa0f8534 100644 --- a/infoschema/test/clustertablestest/cluster_tables_test.go +++ b/infoschema/test/clustertablestest/cluster_tables_test.go @@ -905,10 +905,11 @@ func TestQuickBinding(t *testing.T) { // TODO: more templates } testCases := []testCase{ - // access path selection with use_index + // 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}, } From c68a6561012614fd35c58c6f4f984b0d8b870972 Mon Sep 17 00:00:00 2001 From: qw4990 Date: Fri, 9 Jun 2023 14:39:45 +0800 Subject: [PATCH 5/8] fixup --- infoschema/test/clustertablestest/cluster_tables_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/infoschema/test/clustertablestest/cluster_tables_test.go b/infoschema/test/clustertablestest/cluster_tables_test.go index 180c3aa0f8534..4fafb29f72ef2 100644 --- a/infoschema/test/clustertablestest/cluster_tables_test.go +++ b/infoschema/test/clustertablestest/cluster_tables_test.go @@ -996,14 +996,14 @@ func TestQuickBinding(t *testing.T) { for _, temp := range tc.dmlAndSubqueryTemplates { temp = fmt.Sprintf(temp, tc.template) stmtsummary.StmtSummaryByDigestMap.Clear() - firstSQL := fillValues(tc.template) + firstSQL := fillValues(temp) tk.MustExec(firstSQL) result := tk.MustQuery(`select plan_hint, digest, plan_digest from information_schema.statements_summary`).Rows() _, sqlDigest, planDigest := result[0][0].(string), result[0][1].(string), result[0][2].(string) tk.MustExec(fmt.Sprintf(`create session binding from history using plan digest '%v'`, planDigest)) // normal test - sqlWithoutHint := removeHint(tc.template) + sqlWithoutHint := removeHint(temp) for i := 0; i < 5; i++ { stmtsummary.StmtSummaryByDigestMap.Clear() testSQL := fillValues(sqlWithoutHint) From 44a2f94ad6df1c8e282a29b3a2f2f5998bafb751 Mon Sep 17 00:00:00 2001 From: qw4990 Date: Fri, 9 Jun 2023 15:15:07 +0800 Subject: [PATCH 6/8] fixup --- infoschema/test/clustertablestest/cluster_tables_test.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/infoschema/test/clustertablestest/cluster_tables_test.go b/infoschema/test/clustertablestest/cluster_tables_test.go index 4fafb29f72ef2..ff7a44025f91d 100644 --- a/infoschema/test/clustertablestest/cluster_tables_test.go +++ b/infoschema/test/clustertablestest/cluster_tables_test.go @@ -912,6 +912,12 @@ func TestQuickBinding(t *testing.T) { {`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 Date: Fri, 9 Jun 2023 15:19:21 +0800 Subject: [PATCH 7/8] fixup --- infoschema/test/clustertablestest/cluster_tables_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/infoschema/test/clustertablestest/cluster_tables_test.go b/infoschema/test/clustertablestest/cluster_tables_test.go index ff7a44025f91d..ea4335636bede 100644 --- a/infoschema/test/clustertablestest/cluster_tables_test.go +++ b/infoschema/test/clustertablestest/cluster_tables_test.go @@ -918,6 +918,7 @@ func TestQuickBinding(t *testing.T) { {`select /*+ hash_agg(), use_index(t1, primary) */ count(*), b from t1 where a Date: Fri, 9 Jun 2023 15:47:28 +0800 Subject: [PATCH 8/8] fixup --- .../test/clustertablestest/cluster_tables_test.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/infoschema/test/clustertablestest/cluster_tables_test.go b/infoschema/test/clustertablestest/cluster_tables_test.go index ea4335636bede..05abcc6dac68a 100644 --- a/infoschema/test/clustertablestest/cluster_tables_test.go +++ b/infoschema/test/clustertablestest/cluster_tables_test.go @@ -919,6 +919,18 @@ func TestQuickBinding(t *testing.T) { {`select /*+ stream_agg(), use_index(t1, primary) */ count(*) from t1 where a