Skip to content

Commit bf71326

Browse files
authored
planner: fix privilege for the view in the CTE and wrong error handle (#53556) (#53628)
close #49721
1 parent 588e3b4 commit bf71326

File tree

6 files changed

+140
-7
lines changed

6 files changed

+140
-7
lines changed

pkg/executor/test/writetest/BUILD.bazel

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ go_test(
1818
"//pkg/meta/autoid",
1919
"//pkg/parser/model",
2020
"//pkg/parser/mysql",
21-
"//pkg/planner/core",
2221
"//pkg/session",
2322
"//pkg/sessionctx",
2423
"//pkg/sessiontxn",

pkg/executor/test/writetest/write_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"github.com/pingcap/tidb/pkg/kv"
3030
"github.com/pingcap/tidb/pkg/parser/model"
3131
"github.com/pingcap/tidb/pkg/parser/mysql"
32-
"github.com/pingcap/tidb/pkg/planner/core"
3332
"github.com/pingcap/tidb/pkg/session"
3433
"github.com/pingcap/tidb/pkg/sessionctx"
3534
"github.com/pingcap/tidb/pkg/sessiontxn"
@@ -1152,7 +1151,7 @@ func TestDelete(t *testing.T) {
11521151

11531152
tk.MustExec("create view v as select * from delete_test")
11541153
err = tk.ExecToErr("delete from v where name = 'aaa'")
1155-
require.EqualError(t, err, core.ErrViewInvalid.GenWithStackByArgs("test", "v").Error())
1154+
require.EqualError(t, err, "delete view v is not supported now")
11561155
tk.MustExec("drop view v")
11571156

11581157
tk.MustExec("create sequence seq")
@@ -2194,7 +2193,7 @@ func TestUpdate(t *testing.T) {
21942193

21952194
tk.MustExec("create view v as select * from t")
21962195
err = tk.ExecToErr("update v set a = '2000-11-11'")
2197-
require.EqualError(t, err, core.ErrViewInvalid.GenWithStackByArgs("test", "v").Error())
2196+
require.EqualError(t, err, "[planner:1288]The target table v of the UPDATE is not updatable")
21982197
tk.MustExec("drop view v")
21992198

22002199
tk.MustExec("create sequence seq")

pkg/planner/core/casetest/BUILD.bazel

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ go_test(
1212
],
1313
data = glob(["testdata/**"]),
1414
flaky = True,
15-
shard_count = 24,
15+
shard_count = 25,
1616
deps = [
1717
"//pkg/domain",
18+
"//pkg/errno",
1819
"//pkg/parser",
1920
"//pkg/parser/model",
2021
"//pkg/planner/core",

pkg/planner/core/casetest/plan_test.go

+131
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"strings"
2020
"testing"
2121

22+
"github.com/pingcap/tidb/pkg/errno"
2223
"github.com/pingcap/tidb/pkg/parser/model"
2324
"github.com/pingcap/tidb/pkg/planner/core"
2425
"github.com/pingcap/tidb/pkg/testkit"
@@ -292,3 +293,133 @@ func TestHandleEQAll(t *testing.T) {
292293
tk.MustQuery("select c2 from t2 where (c2 = all (select /*+ IGNORE_INDEX(t2, i1) */ c2 from t2))").Check(testkit.Rows())
293294
tk.MustQuery("select c2 from t2 where (c2 = all (select /*+ use_INDEX(t2, i1) */ c2 from t2))").Check(testkit.Rows())
294295
}
296+
297+
func TestCTEErrNotSupportedYet(t *testing.T) {
298+
store := testkit.CreateMockStore(t)
299+
tk := testkit.NewTestKit(t, store)
300+
tk.MustExec("use test")
301+
tk.MustExec(`
302+
CREATE TABLE pub_branch (
303+
id int(5) NOT NULL,
304+
code varchar(12) NOT NULL,
305+
type_id int(3) DEFAULT NULL,
306+
name varchar(64) NOT NULL,
307+
short_name varchar(32) DEFAULT NULL,
308+
organ_code varchar(15) DEFAULT NULL,
309+
parent_code varchar(12) DEFAULT NULL,
310+
organ_layer tinyint(1) NOT NULL,
311+
inputcode1 varchar(12) DEFAULT NULL,
312+
inputcode2 varchar(12) DEFAULT NULL,
313+
state tinyint(1) NOT NULL,
314+
modify_empid int(9) NOT NULL,
315+
modify_time datetime NOT NULL,
316+
organ_level int(9) DEFAULT NULL,
317+
address varchar(256) DEFAULT NULL,
318+
db_user varchar(32) DEFAULT NULL,
319+
db_password varchar(64) DEFAULT NULL,
320+
org_no int(3) DEFAULT NULL,
321+
ord int(5) DEFAULT NULL,
322+
org_code_mpa varchar(10) DEFAULT NULL,
323+
org_code_gb varchar(30) DEFAULT NULL,
324+
wdchis_id int(5) DEFAULT NULL,
325+
medins_code varchar(32) DEFAULT NULL,
326+
PRIMARY KEY (id),
327+
UNIQUE KEY pub_barnch_unique (code),
328+
KEY idx_pub_branch_parent (parent_code)
329+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
330+
`)
331+
tk.MustExec(`
332+
CREATE VIEW udc_branch_test (
333+
branch_id,
334+
his_branch_id,
335+
branch_code,
336+
branch_name,
337+
pid,
338+
his_pid,
339+
short_name,
340+
inputcode1,
341+
inputcode2,
342+
org_no,
343+
org_code,
344+
org_level,
345+
org_layer,
346+
address,
347+
state,
348+
modify_by,
349+
modify_time,
350+
remark
351+
)
352+
AS
353+
SELECT a.id AS branch_id, a.id AS his_branch_id, a.code AS branch_code, a.name AS branch_name
354+
, a.id + 1000000 AS pid, id AS his_pid, a.short_name AS short_name
355+
, a.inputcode1 AS inputcode1, a.inputcode2 AS inputcode2, a.id AS org_no, a.code AS org_code, a.organ_level AS org_level
356+
, 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
357+
, NULL AS remark
358+
FROM pub_branch a
359+
WHERE organ_layer = 4
360+
UNION ALL
361+
SELECT a.id + 1000000 AS branch_id, a.id AS his_branch_id, a.code AS branch_code
362+
, CONCAT(a.name, _UTF8MB4 '(中心)') AS branch_name
363+
, (
364+
SELECT id AS id
365+
FROM pub_branch a
366+
WHERE organ_layer = 2
367+
AND state = 1
368+
LIMIT 1
369+
) AS pid, id AS his_pid, a.short_name AS short_name, a.inputcode1 AS inputcode1, a.inputcode2 AS inputcode2
370+
, 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
371+
, a.state AS state, 1 AS modify_by, a.modify_time AS modify_time, NULL AS remark
372+
FROM pub_branch a
373+
WHERE organ_layer = 4
374+
UNION ALL
375+
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
376+
, id AS his_pid, a.short_name AS short_name, a.inputcode1 AS inputcode1, a.inputcode2 AS inputcode2, a.id AS org_no
377+
, 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
378+
, a.modify_empid AS modify_by, a.modify_time AS modify_time, NULL AS remark
379+
FROM pub_branch a
380+
WHERE organ_layer = 2;
381+
`)
382+
tk.MustExec(`
383+
CREATE TABLE udc_branch_temp (
384+
branch_id int(11) NOT NULL AUTO_INCREMENT COMMENT '',
385+
his_branch_id varchar(20) DEFAULT NULL COMMENT '',
386+
branch_code varchar(20) DEFAULT NULL COMMENT '',
387+
branch_name varchar(64) NOT NULL COMMENT '',
388+
pid int(11) DEFAULT NULL COMMENT '',
389+
his_pid varchar(20) DEFAULT NULL COMMENT '',
390+
short_name varchar(64) DEFAULT NULL COMMENT '',
391+
inputcode1 varchar(12) DEFAULT NULL COMMENT '辅码1',
392+
inputcode2 varchar(12) DEFAULT NULL COMMENT '辅码2',
393+
org_no int(11) DEFAULT NULL COMMENT '',
394+
org_code varchar(20) DEFAULT NULL COMMENT ',',
395+
org_level tinyint(4) DEFAULT NULL COMMENT '',
396+
org_layer tinyint(4) DEFAULT NULL COMMENT '',
397+
address varchar(255) DEFAULT NULL COMMENT '机构地址',
398+
state tinyint(4) NOT NULL DEFAULT '1' COMMENT '',
399+
modify_by int(11) NOT NULL COMMENT '',
400+
modify_time datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
401+
remark varchar(255) DEFAULT NULL COMMENT '备注',
402+
PRIMARY KEY (branch_id) /*T![clustered_index] CLUSTERED */
403+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin AUTO_INCREMENT=1030102 COMMENT='';
404+
`)
405+
tk.MustGetErrCode(`
406+
SELECT res.*
407+
FROM (
408+
(
409+
WITH RECURSIVE d AS (
410+
SELECT ub.*
411+
FROM udc_branch_test ub
412+
WHERE ub.branch_id = 1000102
413+
UNION ALL
414+
SELECT ub1.*
415+
FROM udc_branch_test ub1
416+
INNER JOIN d ON d.branch_id = ub1.pid
417+
)
418+
SELECT d.*
419+
FROM d
420+
)
421+
) AS res
422+
WHERE res.state != 2
423+
ORDER BY res.branch_id;
424+
`, errno.ErrNotSupportedYet)
425+
}

pkg/planner/core/logical_plan_builder.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -5784,7 +5784,8 @@ func (b *PlanBuilder) BuildDataSourceFromView(ctx context.Context, dbName model.
57845784
terror.ErrorNotEqual(err, ErrInternal) &&
57855785
terror.ErrorNotEqual(err, ErrFieldNotInGroupBy) &&
57865786
terror.ErrorNotEqual(err, ErrMixOfGroupFuncAndFields) &&
5787-
terror.ErrorNotEqual(err, ErrViewNoExplain) {
5787+
terror.ErrorNotEqual(err, ErrViewNoExplain) &&
5788+
terror.ErrorNotEqual(err, ErrNotSupportedYet) {
57885789
err = ErrViewInvalid.GenWithStackByArgs(dbName.O, tableInfo.Name.O)
57895790
}
57905791
return nil, err

pkg/privilege/privileges/privileges.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,12 @@ func (p *UserPrivileges) RequestVerificationWithUser(db, table, column string, p
194194
if SkipWithGrant {
195195
return true
196196
}
197-
198197
if user == nil {
199198
return false
200199
}
200+
if user.Username == "" && user.Hostname == "" {
201+
return true
202+
}
201203

202204
// Skip check for INFORMATION_SCHEMA database.
203205
// See https://dev.mysql.com/doc/refman/5.7/en/information-schema.html

0 commit comments

Comments
 (0)