From 076c097470f7ab1575a13b46acc67b7f868b340e Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Tue, 28 May 2024 17:16:23 +0800 Subject: [PATCH] This is an automated cherry-pick of #53556 Signed-off-by: ti-chi-bot --- pkg/planner/core/casetest/BUILD.bazel | 34 ++++++ planner/core/casetest/plan_test.go | 165 ++++++++++++++++++++++++++ planner/core/logical_plan_builder.go | 11 ++ privilege/privileges/privileges.go | 4 +- 4 files changed, 213 insertions(+), 1 deletion(-) create mode 100644 pkg/planner/core/casetest/BUILD.bazel diff --git a/pkg/planner/core/casetest/BUILD.bazel b/pkg/planner/core/casetest/BUILD.bazel new file mode 100644 index 00000000000000..2170c2e11d4e02 --- /dev/null +++ b/pkg/planner/core/casetest/BUILD.bazel @@ -0,0 +1,34 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_test") + +go_test( + name = "casetest_test", + timeout = "moderate", + srcs = [ + "integration_test.go", + "main_test.go", + "plan_test.go", + "stats_test.go", + "tiflash_selection_late_materialization_test.go", + ], + data = glob(["testdata/**"]), + flaky = True, + shard_count = 25, + deps = [ + "//pkg/domain", + "//pkg/errno", + "//pkg/parser", + "//pkg/parser/model", + "//pkg/planner/core", + "//pkg/planner/core/base", + "//pkg/planner/property", + "//pkg/testkit", + "//pkg/testkit/testdata", + "//pkg/testkit/testmain", + "//pkg/testkit/testsetup", + "//pkg/util/hint", + "//pkg/util/plancodec", + "@com_github_pingcap_failpoint//:failpoint", + "@com_github_stretchr_testify//require", + "@org_uber_go_goleak//:goleak", + ], +) diff --git a/planner/core/casetest/plan_test.go b/planner/core/casetest/plan_test.go index 3c2814d8b67210..ae1907c2888cff 100644 --- a/planner/core/casetest/plan_test.go +++ b/planner/core/casetest/plan_test.go @@ -19,11 +19,21 @@ import ( "strings" "testing" +<<<<<<< HEAD:planner/core/casetest/plan_test.go "github.com/pingcap/tidb/parser/model" "github.com/pingcap/tidb/planner/core" "github.com/pingcap/tidb/testkit" "github.com/pingcap/tidb/testkit/testdata" "github.com/pingcap/tidb/util/plancodec" +======= + "github.com/pingcap/tidb/pkg/errno" + "github.com/pingcap/tidb/pkg/parser/model" + "github.com/pingcap/tidb/pkg/planner/core" + "github.com/pingcap/tidb/pkg/planner/core/base" + "github.com/pingcap/tidb/pkg/testkit" + "github.com/pingcap/tidb/pkg/testkit/testdata" + "github.com/pingcap/tidb/pkg/util/plancodec" +>>>>>>> d2d12574a35 (planner: fix privilege for the view in the CTE and wrong error handle (#53556)):pkg/planner/core/casetest/plan_test.go "github.com/stretchr/testify/require" ) @@ -270,3 +280,158 @@ func TestJSONPlanInExplain(t *testing.T) { } } } +<<<<<<< HEAD:planner/core/casetest/plan_test.go +======= + +func TestHandleEQAll(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec("CREATE TABLE t1 (c1 int, c2 int, UNIQUE i1 (c1, c2));") + tk.MustExec("INSERT INTO t1 VALUES (7, null),(5,1);") + tk.MustQuery("SELECT c1 FROM t1 WHERE ('m' = ALL (SELECT /*+ IGNORE_INDEX(t1, i1) */ c2 FROM t1)) IS NOT UNKNOWN; ").Check(testkit.Rows("5", "7")) + tk.MustQuery("SELECT c1 FROM t1 WHERE ('m' = ALL (SELECT /*+ use_INDEX(t1, i1) */ c2 FROM t1)) IS NOT UNKNOWN; ").Check(testkit.Rows("5", "7")) + tk.MustQuery("select (null = ALL (SELECT /*+ NO_INDEX() */ c2 FROM t1)) IS NOT UNKNOWN").Check(testkit.Rows("0")) + tk.MustExec("CREATE TABLE t2 (c1 int, c2 int, UNIQUE i1 (c1, c2));") + tk.MustExec("INSERT INTO t2 VALUES (7, null),(5,null);") + tk.MustQuery("select (null = ALL (SELECT /*+ NO_INDEX() */ c2 FROM t2)) IS NOT UNKNOWN").Check(testkit.Rows("0")) + tk.MustQuery("SELECT c1 FROM t2 WHERE ('m' = ALL (SELECT /*+ IGNORE_INDEX(t2, i1) */ c2 FROM t2)) IS NOT UNKNOWN; ").Check(testkit.Rows()) + tk.MustQuery("SELECT c1 FROM t2 WHERE ('m' = ALL (SELECT /*+ use_INDEX(t2, i1) */ c2 FROM t2)) IS NOT UNKNOWN; ").Check(testkit.Rows()) + tk.MustExec("truncate table t2") + tk.MustExec("INSERT INTO t2 VALUES (7, null),(7,null);") + tk.MustQuery("select c1 from t2 where (c1 = all (select /*+ IGNORE_INDEX(t2, i1) */ c1 from t2))").Check(testkit.Rows("7", "7")) + tk.MustQuery("select c1 from t2 where (c1 = all (select /*+ use_INDEX(t2, i1) */ c1 from t2))").Check(testkit.Rows("7", "7")) + tk.MustQuery("select c2 from t2 where (c2 = all (select /*+ IGNORE_INDEX(t2, i1) */ c2 from t2))").Check(testkit.Rows()) + tk.MustQuery("select c2 from t2 where (c2 = all (select /*+ use_INDEX(t2, i1) */ c2 from t2))").Check(testkit.Rows()) +} + +func TestCTEErrNotSupportedYet(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec(` +CREATE TABLE pub_branch ( + id int(5) NOT NULL, + code varchar(12) NOT NULL, + type_id int(3) DEFAULT NULL, + name varchar(64) NOT NULL, + short_name varchar(32) DEFAULT NULL, + organ_code varchar(15) DEFAULT NULL, + parent_code varchar(12) DEFAULT NULL, + organ_layer tinyint(1) NOT NULL, + inputcode1 varchar(12) DEFAULT NULL, + inputcode2 varchar(12) DEFAULT NULL, + state tinyint(1) NOT NULL, + modify_empid int(9) NOT NULL, + modify_time datetime NOT NULL, + organ_level int(9) DEFAULT NULL, + address varchar(256) DEFAULT NULL, + db_user varchar(32) DEFAULT NULL, + db_password varchar(64) DEFAULT NULL, + org_no int(3) DEFAULT NULL, + ord int(5) DEFAULT NULL, + org_code_mpa varchar(10) DEFAULT NULL, + org_code_gb varchar(30) DEFAULT NULL, + wdchis_id int(5) DEFAULT NULL, + medins_code varchar(32) DEFAULT NULL, + PRIMARY KEY (id), + UNIQUE KEY pub_barnch_unique (code), + KEY idx_pub_branch_parent (parent_code) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; +`) + tk.MustExec(` +CREATE VIEW udc_branch_test ( + branch_id, + his_branch_id, + branch_code, + branch_name, + pid, + his_pid, + short_name, + inputcode1, + inputcode2, + org_no, + org_code, + org_level, + org_layer, + address, + state, + modify_by, + modify_time, + remark +) +AS +SELECT a.id AS branch_id, a.id AS his_branch_id, a.code AS branch_code, a.name AS branch_name + , a.id + 1000000 AS pid, id AS his_pid, a.short_name AS short_name + , a.inputcode1 AS inputcode1, a.inputcode2 AS inputcode2, a.id AS org_no, a.code AS org_code, a.organ_level AS org_level + , a.organ_layer AS org_layer, a.address AS address, a.state AS state, a.modify_empid AS modify_by, a.modify_time AS modify_time + , NULL AS remark +FROM pub_branch a +WHERE organ_layer = 4 +UNION ALL +SELECT a.id + 1000000 AS branch_id, a.id AS his_branch_id, a.code AS branch_code + , CONCAT(a.name, _UTF8MB4 '(中心)') AS branch_name + , ( + SELECT id AS id + FROM pub_branch a + WHERE organ_layer = 2 + AND state = 1 + LIMIT 1 + ) AS pid, id AS his_pid, a.short_name AS short_name, a.inputcode1 AS inputcode1, a.inputcode2 AS inputcode2 + , a.id AS org_no, a.code AS org_code, a.organ_level AS org_level, a.organ_layer AS org_layer, a.address AS address + , a.state AS state, 1 AS modify_by, a.modify_time AS modify_time, NULL AS remark +FROM pub_branch a +WHERE organ_layer = 4 +UNION ALL +SELECT a.id AS branch_id, a.id AS his_branch_id, a.code AS branch_code, a.name AS branch_name, NULL AS pid + , id AS his_pid, a.short_name AS short_name, a.inputcode1 AS inputcode1, a.inputcode2 AS inputcode2, a.id AS org_no + , a.code AS org_code, a.organ_level AS org_level, a.organ_layer AS org_layer, a.address AS address, a.state AS state + , a.modify_empid AS modify_by, a.modify_time AS modify_time, NULL AS remark +FROM pub_branch a +WHERE organ_layer = 2; +`) + tk.MustExec(` +CREATE TABLE udc_branch_temp ( + branch_id int(11) NOT NULL AUTO_INCREMENT COMMENT '', + his_branch_id varchar(20) DEFAULT NULL COMMENT '', + branch_code varchar(20) DEFAULT NULL COMMENT '', + branch_name varchar(64) NOT NULL COMMENT '', + pid int(11) DEFAULT NULL COMMENT '', + his_pid varchar(20) DEFAULT NULL COMMENT '', + short_name varchar(64) DEFAULT NULL COMMENT '', + inputcode1 varchar(12) DEFAULT NULL COMMENT '辅码1', + inputcode2 varchar(12) DEFAULT NULL COMMENT '辅码2', + org_no int(11) DEFAULT NULL COMMENT '', + org_code varchar(20) DEFAULT NULL COMMENT ',', + org_level tinyint(4) DEFAULT NULL COMMENT '', + org_layer tinyint(4) DEFAULT NULL COMMENT '', + address varchar(255) DEFAULT NULL COMMENT '机构地址', + state tinyint(4) NOT NULL DEFAULT '1' COMMENT '', + modify_by int(11) NOT NULL COMMENT '', + modify_time datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间', + remark varchar(255) DEFAULT NULL COMMENT '备注', + PRIMARY KEY (branch_id) /*T![clustered_index] CLUSTERED */ +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin AUTO_INCREMENT=1030102 COMMENT=''; +`) + tk.MustGetErrCode(` +SELECT res.* +FROM ( + ( + WITH RECURSIVE d AS ( + SELECT ub.* + FROM udc_branch_test ub + WHERE ub.branch_id = 1000102 + UNION ALL + SELECT ub1.* + FROM udc_branch_test ub1 + INNER JOIN d ON d.branch_id = ub1.pid + ) + SELECT d.* + FROM d + ) +) AS res +WHERE res.state != 2 +ORDER BY res.branch_id; +`, errno.ErrNotSupportedYet) +} +>>>>>>> d2d12574a35 (planner: fix privilege for the view in the CTE and wrong error handle (#53556)):pkg/planner/core/casetest/plan_test.go diff --git a/planner/core/logical_plan_builder.go b/planner/core/logical_plan_builder.go index 3955e9092457da..fb5756f5b85ae2 100644 --- a/planner/core/logical_plan_builder.go +++ b/planner/core/logical_plan_builder.go @@ -5403,6 +5403,7 @@ func (b *PlanBuilder) BuildDataSourceFromView(ctx context.Context, dbName model. }() selectLogicalPlan, err := b.Build(ctx, selectNode) if err != nil { +<<<<<<< HEAD:planner/core/logical_plan_builder.go if terror.ErrorNotEqual(err, ErrViewRecursive) && terror.ErrorNotEqual(err, ErrNoSuchTable) && terror.ErrorNotEqual(err, ErrInternal) && @@ -5410,6 +5411,16 @@ func (b *PlanBuilder) BuildDataSourceFromView(ctx context.Context, dbName model. terror.ErrorNotEqual(err, ErrMixOfGroupFuncAndFields) && terror.ErrorNotEqual(err, ErrViewNoExplain) { err = ErrViewInvalid.GenWithStackByArgs(dbName.O, tableInfo.Name.O) +======= + if terror.ErrorNotEqual(err, plannererrors.ErrViewRecursive) && + terror.ErrorNotEqual(err, plannererrors.ErrNoSuchTable) && + terror.ErrorNotEqual(err, plannererrors.ErrInternal) && + terror.ErrorNotEqual(err, plannererrors.ErrFieldNotInGroupBy) && + terror.ErrorNotEqual(err, plannererrors.ErrMixOfGroupFuncAndFields) && + terror.ErrorNotEqual(err, plannererrors.ErrViewNoExplain) && + terror.ErrorNotEqual(err, plannererrors.ErrNotSupportedYet) { + err = plannererrors.ErrViewInvalid.GenWithStackByArgs(dbName.O, tableInfo.Name.O) +>>>>>>> d2d12574a35 (planner: fix privilege for the view in the CTE and wrong error handle (#53556)):pkg/planner/core/logical_plan_builder.go } return nil, err } diff --git a/privilege/privileges/privileges.go b/privilege/privileges/privileges.go index ea056bb041223c..59c6a5f78e94d5 100644 --- a/privilege/privileges/privileges.go +++ b/privilege/privileges/privileges.go @@ -194,10 +194,12 @@ func (p *UserPrivileges) RequestVerificationWithUser(db, table, column string, p if SkipWithGrant { return true } - if user == nil { return false } + if user.Username == "" && user.Hostname == "" { + return true + } // Skip check for INFORMATION_SCHEMA database. // See https://dev.mysql.com/doc/refman/5.7/en/information-schema.html