Skip to content

Commit 5bd91f6

Browse files
committed
MDEV-27898 CREATE VIEW AS SELECT FROM JSON_TABLE column requires global privileges
JSON_TABLE is marked as a special "*any_db*" table. Because this special marking is processed all the way though to get_column_grant where its processed like its in database called "*any_db*". As this doesn't exist, only those users with global privileges can create views on a JSON_TABLE. Under a Prepared Statement protocol a Create_tmp_table is used for the JSON_TABLE, but it gets assigned an "" database name. We correct this to give it "*any_db*" like the SQL parser indicating that no database is needed. To commonly correct the fill_effective_table_privileges by looking explictly for the "*any_db*", those tables that have this as the database name get SELECT privileges. While correcting the database for the JSON_TABLE, lets give it a "json_table" name rather than "(temporary)" for greater clarity in warning messages.
1 parent 1ac2270 commit 5bd91f6

File tree

5 files changed

+64
-16
lines changed

5 files changed

+64
-16
lines changed

mysql-test/suite/json/r/json_table.result

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ id intcol
369369
3 5678
370370
4 5678
371371
Warnings:
372-
Warning 1366 Incorrect integer value: 'asd' for column ``.`(temporary)`.`intcol` at row 1
372+
Warning 1366 Incorrect integer value: 'asd' for column `*any*`.`json_table`.`intcol` at row 1
373373
SELECT COUNT(*) FROM JSON_TABLE('[1, 2]', '$[*]' COLUMNS( I INT PATH '$')) tt;
374374
COUNT(*)
375375
2
@@ -927,7 +927,7 @@ id intcol
927927
3 5678
928928
4 5678
929929
Warnings:
930-
Warning 1366 Incorrect integer value: 'asd' for column ``.`(temporary)`.`intcol` at row 1
930+
Warning 1366 Incorrect integer value: 'asd' for column `*any*`.`json_table`.`intcol` at row 1
931931
#
932932
# MDEV-25377 JSON_TABLE: Wrong value with implicit conversion.
933933
#
@@ -937,17 +937,17 @@ converted original
937937
1 1
938938
127 1000
939939
Warnings:
940-
Warning 1366 Incorrect integer value: 'foo' for column ``.`(temporary)`.`converted` at row 1
940+
Warning 1366 Incorrect integer value: 'foo' for column `*any*`.`json_table`.`converted` at row 1
941941
Warning 1264 Out of range value for column 'converted' at row 3
942942
select * from json_table('{"a":"foo", "b":1, "c":1000}', '$.*' columns(converted tinyint path '$', original text path '$')) as jt order by converted;
943943
converted original
944944
0 foo
945945
1 1
946946
127 1000
947947
Warnings:
948-
Warning 1366 Incorrect integer value: 'foo' for column ``.`(temporary)`.`converted` at row 1
948+
Warning 1366 Incorrect integer value: 'foo' for column `*any*`.`json_table`.`converted` at row 1
949949
Warning 1264 Out of range value for column 'converted' at row 1
950-
Warning 1366 Incorrect integer value: 'foo' for column ``.`(temporary)`.`converted` at row 1
950+
Warning 1366 Incorrect integer value: 'foo' for column `*any*`.`json_table`.`converted` at row 1
951951
Warning 1264 Out of range value for column 'converted' at row 3
952952
select * from json_table('{"a":"foo", "b":1, "c":1000}', '$.*' columns(converted tinyint path '$', original text path '$')) as jt order by original;
953953
converted original
@@ -956,7 +956,7 @@ converted original
956956
0 foo
957957
Warnings:
958958
Warning 1264 Out of range value for column 'converted' at row 2
959-
Warning 1366 Incorrect integer value: 'foo' for column ``.`(temporary)`.`converted` at row 3
959+
Warning 1366 Incorrect integer value: 'foo' for column `*any*`.`json_table`.`converted` at row 3
960960
select * from
961961
json_table('[{"color": "blue", "price": { "high": 10, "low": 5}},
962962
{"color": "white", "price": "pretty low"},
@@ -1213,3 +1213,19 @@ ERROR 42000: You have an error in your SQL syntax; check the manual that corresp
12131213
#
12141214
# End of 10.9 tests
12151215
#
1216+
#
1217+
# MDEV-27898 CREATE VIEW AS SELECT FROM JSON_TABLE column requires global privileges
1218+
#
1219+
create view v1 as (select * from
1220+
json_table('[{"a":"1"}]', '$[*]' columns(a int path '$.a') ) as jt);
1221+
create user u1@localhost;
1222+
grant ALL on test.* to u1@localhost;
1223+
connect con1,localhost,u1,,test;
1224+
create view v2 as
1225+
(select * from json_table('[{"a":"1"}]', '$[*]' columns(a int path '$.a') ) as jt);
1226+
disconnect con1;
1227+
connection default;
1228+
DROP VIEW v2;
1229+
DROP VIEW v1;
1230+
DROP USER u1@localhost;
1231+
# End of 10.11 tests

mysql-test/suite/json/r/json_table_mysql.result

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ id jpath_i jpath_r jsn_path jexst
6161
4 0 0.33 0.33 0
6262
5 0 0 "asd" 0
6363
Warnings:
64-
Warning 1366 Incorrect integer value: 'asd' for column ``.`(temporary)`.`jpath_i` at row 5
65-
Warning 1366 Incorrect double value: 'asd' for column ``.`(temporary)`.`jpath_r` at row 5
64+
Warning 1366 Incorrect integer value: 'asd' for column `*any*`.`json_table`.`jpath_i` at row 5
65+
Warning 1366 Incorrect double value: 'asd' for column `*any*`.`json_table`.`jpath_r` at row 5
6666
select * from
6767
json_table(
6868
'[{"x":"3"},{"a":2},{"b":1},{"a":0},{"a":[1,2]}]',
@@ -406,7 +406,7 @@ SELECT * FROM JSON_TABLE('"asdf"',
406406
a
407407
0
408408
Warnings:
409-
Warning 1366 Incorrect integer value: 'asdf' for column ``.`(temporary)`.`a` at row 1
409+
Warning 1366 Incorrect integer value: 'asdf' for column `*any*`.`json_table`.`a` at row 1
410410
SELECT * FROM
411411
JSON_TABLE('[{"a":1},{"a":2}]',
412412
'$' COLUMNS (a INT PATH '$[*].a' ERROR ON ERROR)) AS jt;
@@ -544,9 +544,9 @@ tm dt i f d
544544
Warnings:
545545
Warning 1265 Data truncated for column 'tm' at row 1
546546
Warning 1265 Data truncated for column 'dt' at row 1
547-
Warning 1366 Incorrect integer value: 'asdf' for column ``.`(temporary)`.`i` at row 1
548-
Warning 1366 Incorrect double value: 'asdf' for column ``.`(temporary)`.`f` at row 1
549-
Warning 1366 Incorrect decimal value: 'asdf' for column ``.`(temporary)`.`d` at row 1
547+
Warning 1366 Incorrect integer value: 'asdf' for column `*any*`.`json_table`.`i` at row 1
548+
Warning 1366 Incorrect double value: 'asdf' for column `*any*`.`json_table`.`f` at row 1
549+
Warning 1366 Incorrect decimal value: 'asdf' for column `*any*`.`json_table`.`d` at row 1
550550
SELECT * FROM
551551
JSON_TABLE('{}', '$' COLUMNS (x INT PATH '$.x' DEFAULT NULL ON EMPTY)) jt;
552552
x
@@ -862,8 +862,8 @@ col18
862862
0.000
863863
0.000
864864
Warnings:
865-
Warning 1366 Incorrect decimal value: 'asdf' for column ``.`(temporary)`.`col18` at row 1
866-
Warning 1366 Incorrect decimal value: 'ghjk' for column ``.`(temporary)`.`col18` at row 2
865+
Warning 1366 Incorrect decimal value: 'asdf' for column `*any*`.`json_table`.`col18` at row 1
866+
Warning 1366 Incorrect decimal value: 'ghjk' for column `*any*`.`json_table`.`col18` at row 2
867867
CREATE TABLE t1(jd JSON);
868868
INSERT INTO t1 VALUES('["asdf"]'),('["ghjk"]');
869869
SELECT * FROM t1,
@@ -875,8 +875,8 @@ jd col18
875875
["asdf"] 0.000
876876
["ghjk"] 0.000
877877
Warnings:
878-
Warning 1366 Incorrect decimal value: 'asdf' for column ``.`(temporary)`.`col18` at row 1
879-
Warning 1366 Incorrect decimal value: 'ghjk' for column ``.`(temporary)`.`col18` at row 1
878+
Warning 1366 Incorrect decimal value: 'asdf' for column `*any*`.`json_table`.`col18` at row 1
879+
Warning 1366 Incorrect decimal value: 'ghjk' for column `*any*`.`json_table`.`col18` at row 1
880880
DROP TABLE t1;
881881
#
882882
# Bug#25540027: SIG 11 IN FIND_FIELD_IN_TABLE | SQL/SQL_BASE.CC

mysql-test/suite/json/t/json_table.test

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,3 +1041,26 @@ COLUMNS
10411041
--echo #
10421042
--echo # End of 10.9 tests
10431043
--echo #
1044+
1045+
--echo #
1046+
--echo # MDEV-27898 CREATE VIEW AS SELECT FROM JSON_TABLE column requires global privileges
1047+
--echo #
1048+
1049+
create view v1 as (select * from
1050+
json_table('[{"a":"1"}]', '$[*]' columns(a int path '$.a') ) as jt);
1051+
1052+
create user u1@localhost;
1053+
grant ALL on test.* to u1@localhost;
1054+
1055+
--connect (con1,localhost,u1,,test)
1056+
1057+
create view v2 as
1058+
(select * from json_table('[{"a":"1"}]', '$[*]' columns(a int path '$.a') ) as jt);
1059+
1060+
disconnect con1;
1061+
connection default;
1062+
DROP VIEW v2;
1063+
DROP VIEW v1;
1064+
DROP USER u1@localhost;
1065+
1066+
--echo # End of 10.11 tests

sql/json_table.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,8 @@ TABLE *Create_json_table::start(THD *thd,
711711
if (!(table= Create_tmp_table::start(thd, param, table_alias)))
712712
DBUG_RETURN(0);
713713
share= table->s;
714+
share->db= any_db;
715+
share->table_name= { STRING_WITH_LEN("json_table") };
714716
share->not_usable_by_query_cache= FALSE;
715717
share->db_plugin= NULL;
716718
if (!(table->file= new (&table->mem_root) ha_json_table(share, jt)))

sql/sql_acl.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13053,6 +13053,13 @@ void fill_effective_table_privileges(THD *thd, GRANT_INFO *grant,
1305313053
DBUG_VOID_RETURN;
1305413054
}
1305513055

13056+
/* JSON_TABLE and other db detached table */
13057+
if (db == any_db.str)
13058+
{
13059+
grant->privilege= SELECT_ACL;
13060+
DBUG_VOID_RETURN;
13061+
}
13062+
1305613063
/* global privileges */
1305713064
grant->privilege= sctx->master_access;
1305813065

0 commit comments

Comments
 (0)