diff --git a/sql/json_table.cc b/sql/json_table.cc index 701aeff5020..a7bad26cfc6 100644 --- a/sql/json_table.cc +++ b/sql/json_table.cc @@ -1153,9 +1153,8 @@ int Table_function_json_table::setup(THD *thd, TABLE_LIST *sql_table, if (m_dep_tables) { - sql_table->dep_tables|= m_dep_tables; t->no_cache= TRUE; - if (unlikely(sql_table->dep_tables & sql_table->get_map())) + if (unlikely(m_dep_tables & sql_table->get_map())) { /* Table itself is used in the argument. */ my_error(ER_WRONG_USAGE, MYF(0), "JSON_TABLE", "argument"); @@ -1308,8 +1307,9 @@ static void add_extra_deps(List *join_list, table_map deps) /* @brief - Add extra dependencies implied by table functions so that the join - optimizer does not construct "dead-end" join prefixes. + Add table dependencies that are directly caused by table functions, also + add extra dependencies so that the join optimizer does not construct + "dead-end" join prefixes. @detail There are two kinds of limitations on join order: @@ -1321,6 +1321,9 @@ static void add_extra_deps(List *join_list, table_map deps) current table in the query text. The table maybe outside of the current nested join and/or inside another nested join. + One may think that adding dependency according to #2 would be sufficient, + but this is not the case. + @example select ... @@ -1387,7 +1390,10 @@ table_map add_table_function_dependencies(List *join_list, nested_join->used_tables); } else if (table->table_function) + { + table->dep_tables |= table->table_function->used_tables(); res |= table->dep_tables; + } } res= res & ~nest_tables & ~PSEUDO_TABLE_BITS; // Then, make all "peers" have them: diff --git a/sql/sql_select.cc b/sql/sql_select.cc index c9c6fcdfb54..ff87678a656 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -16546,7 +16546,7 @@ simplify_joins(JOIN *join, List *join_list, COND *conds, bool top, if (table->outer_join && !table->embedding && table->table) table->table->maybe_null= FALSE; table->outer_join= 0; - if (!(straight_join || table->straight || table->table_function)) + if (!(straight_join || table->straight)) { table->dep_tables= 0; TABLE_LIST *embedding= table->embedding;