Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: fix privilege for the view in the CTE and wrong error handle #53556

Merged
merged 8 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pkg/planner/core/casetest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ go_test(
],
data = glob(["testdata/**"]),
flaky = True,
shard_count = 24,
shard_count = 25,
deps = [
"//pkg/domain",
"//pkg/errno",
"//pkg/parser",
"//pkg/parser/model",
"//pkg/planner/core",
Expand Down
131 changes: 131 additions & 0 deletions pkg/planner/core/casetest/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"strings"
"testing"

"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"
Expand Down Expand Up @@ -330,3 +331,133 @@ func TestHandleEQAll(t *testing.T) {
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)
}
3 changes: 2 additions & 1 deletion pkg/planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5434,7 +5434,8 @@ func (b *PlanBuilder) BuildDataSourceFromView(ctx context.Context, dbName model.
terror.ErrorNotEqual(err, plannererrors.ErrInternal) &&
terror.ErrorNotEqual(err, plannererrors.ErrFieldNotInGroupBy) &&
terror.ErrorNotEqual(err, plannererrors.ErrMixOfGroupFuncAndFields) &&
terror.ErrorNotEqual(err, plannererrors.ErrViewNoExplain) {
terror.ErrorNotEqual(err, plannererrors.ErrViewNoExplain) &&
terror.ErrorNotEqual(err, plannererrors.ErrNotSupportedYet) {
err = plannererrors.ErrViewInvalid.GenWithStackByArgs(dbName.O, tableInfo.Name.O)
}
return nil, err
Expand Down
4 changes: 3 additions & 1 deletion pkg/privilege/privileges/privileges.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down