Commit 74895090 authored by Sergei Petrunia's avatar Sergei Petrunia Committed by Alexey Botchkov

MDEV-25202: JSON_TABLE: Early table reference leads to unexpected result set

Followup: also handle NATURAL JOIN, extend the new approach with
Name_resolution_context::ignored_tables
parent 5a8abbb7
...@@ -658,6 +658,23 @@ json_table((select concat(a,js) from t2), ...@@ -658,6 +658,23 @@ json_table((select concat(a,js) from t2),
ERROR 42S22: Unknown column 'js' in 'field list' ERROR 42S22: Unknown column 'js' in 'field list'
drop table t1,t2; drop table t1,t2;
# #
# Now, a testcase with JSON_TABLEs inside NATURAL JOIN
#
create table t1 (a int, b int);
create table t2 (a int, c int);
select * from
t1,
( t2
natural join
(
json_table(JT2.d, '$' COLUMNS (d for ordinality)) as JT
natural join
json_table(JT.d, '$' COLUMNS (d for ordinality)) as JT2
)
);
ERROR 42S22: Unknown column 'JT2.d' in 'JSON_TABLE argument'
drop table t1, t2;
#
# MDEV-25256: JSON_TABLE: Error ER_VIEW_INVALID upon running query via view # MDEV-25256: JSON_TABLE: Error ER_VIEW_INVALID upon running query via view
# #
SELECT * FROM SELECT * FROM
......
...@@ -542,6 +542,27 @@ from ...@@ -542,6 +542,27 @@ from
drop table t1,t2; drop table t1,t2;
--echo #
--echo # Now, a testcase with JSON_TABLEs inside NATURAL JOIN
--echo #
create table t1 (a int, b int);
create table t2 (a int, c int);
--error ER_BAD_FIELD_ERROR
select * from
t1,
( t2
natural join
(
json_table(JT2.d, '$' COLUMNS (d for ordinality)) as JT
natural join
json_table(JT.d, '$' COLUMNS (d for ordinality)) as JT2
)
);
drop table t1, t2;
--echo # --echo #
--echo # MDEV-25256: JSON_TABLE: Error ER_VIEW_INVALID upon running query via view --echo # MDEV-25256: JSON_TABLE: Error ER_VIEW_INVALID upon running query via view
--echo # --echo #
......
...@@ -6966,7 +6966,7 @@ int mysql_table_grant(THD *thd, TABLE_LIST *table_list, ...@@ -6966,7 +6966,7 @@ int mysql_table_grant(THD *thd, TABLE_LIST *table_list,
Field *f=find_field_in_table_ref(thd, table_list, column->column.ptr(), Field *f=find_field_in_table_ref(thd, table_list, column->column.ptr(),
column->column.length(), column->column.length(),
column->column.ptr(), NULL, NULL, column->column.ptr(), NULL, NULL,
NULL, TRUE, FALSE, table_map(0), NULL, TRUE, FALSE,
&unused_field_idx, FALSE, &dummy); &unused_field_idx, FALSE, &dummy);
if (unlikely(f == (Field*)0)) if (unlikely(f == (Field*)0))
{ {
......
...@@ -6101,7 +6101,8 @@ Field * ...@@ -6101,7 +6101,8 @@ Field *
find_field_in_table_ref(THD *thd, TABLE_LIST *table_list, find_field_in_table_ref(THD *thd, TABLE_LIST *table_list,
const char *name, size_t length, const char *name, size_t length,
const char *item_name, const char *db_name, const char *item_name, const char *db_name,
const char *table_name, Item **ref, const char *table_name, table_map ignored_tables,
Item **ref,
bool check_privileges, bool allow_rowid, bool check_privileges, bool allow_rowid,
uint *cached_field_index_ptr, uint *cached_field_index_ptr,
bool register_tree_change, TABLE_LIST **actual_table) bool register_tree_change, TABLE_LIST **actual_table)
...@@ -6193,9 +6194,11 @@ find_field_in_table_ref(THD *thd, TABLE_LIST *table_list, ...@@ -6193,9 +6194,11 @@ find_field_in_table_ref(THD *thd, TABLE_LIST *table_list,
TABLE_LIST *table; TABLE_LIST *table;
while ((table= it++)) while ((table= it++))
{ {
if (table->table && (table->table->map & ignored_tables))
continue;
if ((fld= find_field_in_table_ref(thd, table, name, length, item_name, if ((fld= find_field_in_table_ref(thd, table, name, length, item_name,
db_name, table_name, ref, db_name, table_name, ignored_tables,
check_privileges, allow_rowid, ref, check_privileges, allow_rowid,
cached_field_index_ptr, cached_field_index_ptr,
register_tree_change, actual_table))) register_tree_change, actual_table)))
DBUG_RETURN(fld); DBUG_RETURN(fld);
...@@ -6404,8 +6407,9 @@ find_field_in_tables(THD *thd, Item_ident *item, ...@@ -6404,8 +6407,9 @@ find_field_in_tables(THD *thd, Item_ident *item,
} }
else else
found= find_field_in_table_ref(thd, table_ref, name, length, item->name.str, found= find_field_in_table_ref(thd, table_ref, name, length, item->name.str,
NULL, NULL, ref, check_privileges, NULL, NULL, ignored_tables, ref,
TRUE, &(item->cached_field_index), check_privileges, TRUE,
&(item->cached_field_index),
register_tree_change, register_tree_change,
&actual_table); &actual_table);
if (found) if (found)
...@@ -6475,7 +6479,8 @@ find_field_in_tables(THD *thd, Item_ident *item, ...@@ -6475,7 +6479,8 @@ find_field_in_tables(THD *thd, Item_ident *item,
continue; continue;
Field *cur_field= find_field_in_table_ref(thd, cur_table, name, length, Field *cur_field= find_field_in_table_ref(thd, cur_table, name, length,
item->name.str, db, table_name, ref, item->name.str, db, table_name,
ignored_tables, ref,
(thd->lex->sql_command == (thd->lex->sql_command ==
SQLCOM_SHOW_FIELDS) SQLCOM_SHOW_FIELDS)
? false : check_privileges, ? false : check_privileges,
...@@ -6492,8 +6497,8 @@ find_field_in_tables(THD *thd, Item_ident *item, ...@@ -6492,8 +6497,8 @@ find_field_in_tables(THD *thd, Item_ident *item,
thd->clear_error(); thd->clear_error();
cur_field= find_field_in_table_ref(thd, cur_table, name, length, cur_field= find_field_in_table_ref(thd, cur_table, name, length,
item->name.str, db, table_name, ref, item->name.str, db, table_name,
false, ignored_tables, ref, false,
allow_rowid, allow_rowid,
&(item->cached_field_index), &(item->cached_field_index),
register_tree_change, register_tree_change,
......
...@@ -202,8 +202,8 @@ Field * ...@@ -202,8 +202,8 @@ Field *
find_field_in_table_ref(THD *thd, TABLE_LIST *table_list, find_field_in_table_ref(THD *thd, TABLE_LIST *table_list,
const char *name, size_t length, const char *name, size_t length,
const char *item_name, const char *db_name, const char *item_name, const char *db_name,
const char *table_name, Item **ref, const char *table_name, table_map ignored_tables,
bool check_privileges, bool allow_rowid, Item **ref, bool check_privileges, bool allow_rowid,
uint *cached_field_index_ptr, uint *cached_field_index_ptr,
bool register_tree_change, TABLE_LIST **actual_table); bool register_tree_change, TABLE_LIST **actual_table);
Field * Field *
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment