From b418b60ebf5a870ca27a2c578a405e0ed1af2fee Mon Sep 17 00:00:00 2001 From: Rex Date: Wed, 19 Jun 2024 12:56:45 +1100 Subject: [PATCH] MDEV-30623 JSON_TABLE in subquery not correctly marked as correlated st_select_lex::update_correlated_cache() fails to take JSON_TABLE functions in subqueries into account. Reviewed by Sergei Petrunia (sergey@mariadb.com) --- mysql-test/main/subselect.result | 26 +++++++++++++++++++ mysql-test/main/subselect.test | 22 ++++++++++++++++ .../main/subselect_no_exists_to_in.result | 26 +++++++++++++++++++ mysql-test/main/subselect_no_mat.result | 26 +++++++++++++++++++ mysql-test/main/subselect_no_opts.result | 26 +++++++++++++++++++ mysql-test/main/subselect_no_scache.result | 26 +++++++++++++++++++ mysql-test/main/subselect_no_semijoin.result | 26 +++++++++++++++++++ sql/sql_lex.cc | 3 +++ 8 files changed, 181 insertions(+) diff --git a/mysql-test/main/subselect.result b/mysql-test/main/subselect.result index 9fbf36e3d6e..3b5954754d2 100644 --- a/mysql-test/main/subselect.result +++ b/mysql-test/main/subselect.result @@ -7559,5 +7559,31 @@ Warning 1292 Truncated incorrect DECIMAL value: 'e' drop view v1; drop table t1, t2, t3; # +# MDEV-30623 JSON_TABLE in subquery not correctly marked as correlated +# update_correlated_cache() fails to take JSON_TABLE functions in +# subqueries into account. +# +create table t1(c json); +insert into t1 values ('[{"x":"1"},{"x":"2"}]'), +('[{"x":"10"},{"x":"20"}]'), +('[{"x":"100"},{"x":"200"}]'); +select c, +(SELECT sum(x) FROM json_table(c, "$[*]" columns( x int path "$.x")) +AS jt) +from t1; +c (SELECT sum(x) FROM json_table(c, "$[*]" columns( x int path "$.x")) +AS jt) +[{"x":"1"},{"x":"2"}] 3 +[{"x":"10"},{"x":"20"}] 30 +[{"x":"100"},{"x":"200"}] 300 +explain select c, +(SELECT sum(x) FROM json_table(c, "$[*]" columns( x int path "$.x")) +AS jt) +from t1; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 3 +2 DEPENDENT SUBQUERY jt ALL NULL NULL NULL NULL 40 Table function: json_table +drop table t1; +# # End of 10.6 tests # diff --git a/mysql-test/main/subselect.test b/mysql-test/main/subselect.test index badf55ca958..eca5af78eb7 100644 --- a/mysql-test/main/subselect.test +++ b/mysql-test/main/subselect.test @@ -6434,6 +6434,28 @@ insert into t2 select (('e','e') IN (SELECT v1.id, v1.id FROM v1 JOIN t3)); drop view v1; drop table t1, t2, t3; +--echo # +--echo # MDEV-30623 JSON_TABLE in subquery not correctly marked as correlated +--echo # update_correlated_cache() fails to take JSON_TABLE functions in +--echo # subqueries into account. +--echo # + +create table t1(c json); +insert into t1 values ('[{"x":"1"},{"x":"2"}]'), + ('[{"x":"10"},{"x":"20"}]'), + ('[{"x":"100"},{"x":"200"}]'); +select c, + (SELECT sum(x) FROM json_table(c, "$[*]" columns( x int path "$.x")) + AS jt) + from t1; + +explain select c, + (SELECT sum(x) FROM json_table(c, "$[*]" columns( x int path "$.x")) + AS jt) + from t1; + +drop table t1; + --echo # --echo # End of 10.6 tests --echo # diff --git a/mysql-test/main/subselect_no_exists_to_in.result b/mysql-test/main/subselect_no_exists_to_in.result index 0fdd573b339..806e2f67c63 100644 --- a/mysql-test/main/subselect_no_exists_to_in.result +++ b/mysql-test/main/subselect_no_exists_to_in.result @@ -7559,6 +7559,32 @@ Warning 1292 Truncated incorrect DECIMAL value: 'e' drop view v1; drop table t1, t2, t3; # +# MDEV-30623 JSON_TABLE in subquery not correctly marked as correlated +# update_correlated_cache() fails to take JSON_TABLE functions in +# subqueries into account. +# +create table t1(c json); +insert into t1 values ('[{"x":"1"},{"x":"2"}]'), +('[{"x":"10"},{"x":"20"}]'), +('[{"x":"100"},{"x":"200"}]'); +select c, +(SELECT sum(x) FROM json_table(c, "$[*]" columns( x int path "$.x")) +AS jt) +from t1; +c (SELECT sum(x) FROM json_table(c, "$[*]" columns( x int path "$.x")) +AS jt) +[{"x":"1"},{"x":"2"}] 3 +[{"x":"10"},{"x":"20"}] 30 +[{"x":"100"},{"x":"200"}] 300 +explain select c, +(SELECT sum(x) FROM json_table(c, "$[*]" columns( x int path "$.x")) +AS jt) +from t1; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 3 +2 DEPENDENT SUBQUERY jt ALL NULL NULL NULL NULL 40 Table function: json_table +drop table t1; +# # End of 10.6 tests # set optimizer_switch=default; diff --git a/mysql-test/main/subselect_no_mat.result b/mysql-test/main/subselect_no_mat.result index 5183d50cd49..cef26709fd3 100644 --- a/mysql-test/main/subselect_no_mat.result +++ b/mysql-test/main/subselect_no_mat.result @@ -7552,6 +7552,32 @@ Warning 1292 Truncated incorrect DECIMAL value: 'e' drop view v1; drop table t1, t2, t3; # +# MDEV-30623 JSON_TABLE in subquery not correctly marked as correlated +# update_correlated_cache() fails to take JSON_TABLE functions in +# subqueries into account. +# +create table t1(c json); +insert into t1 values ('[{"x":"1"},{"x":"2"}]'), +('[{"x":"10"},{"x":"20"}]'), +('[{"x":"100"},{"x":"200"}]'); +select c, +(SELECT sum(x) FROM json_table(c, "$[*]" columns( x int path "$.x")) +AS jt) +from t1; +c (SELECT sum(x) FROM json_table(c, "$[*]" columns( x int path "$.x")) +AS jt) +[{"x":"1"},{"x":"2"}] 3 +[{"x":"10"},{"x":"20"}] 30 +[{"x":"100"},{"x":"200"}] 300 +explain select c, +(SELECT sum(x) FROM json_table(c, "$[*]" columns( x int path "$.x")) +AS jt) +from t1; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 3 +2 DEPENDENT SUBQUERY jt ALL NULL NULL NULL NULL 40 Table function: json_table +drop table t1; +# # End of 10.6 tests # set optimizer_switch=default; diff --git a/mysql-test/main/subselect_no_opts.result b/mysql-test/main/subselect_no_opts.result index 603958d2815..7ef17b14791 100644 --- a/mysql-test/main/subselect_no_opts.result +++ b/mysql-test/main/subselect_no_opts.result @@ -7550,6 +7550,32 @@ Warning 1292 Truncated incorrect DECIMAL value: 'e' drop view v1; drop table t1, t2, t3; # +# MDEV-30623 JSON_TABLE in subquery not correctly marked as correlated +# update_correlated_cache() fails to take JSON_TABLE functions in +# subqueries into account. +# +create table t1(c json); +insert into t1 values ('[{"x":"1"},{"x":"2"}]'), +('[{"x":"10"},{"x":"20"}]'), +('[{"x":"100"},{"x":"200"}]'); +select c, +(SELECT sum(x) FROM json_table(c, "$[*]" columns( x int path "$.x")) +AS jt) +from t1; +c (SELECT sum(x) FROM json_table(c, "$[*]" columns( x int path "$.x")) +AS jt) +[{"x":"1"},{"x":"2"}] 3 +[{"x":"10"},{"x":"20"}] 30 +[{"x":"100"},{"x":"200"}] 300 +explain select c, +(SELECT sum(x) FROM json_table(c, "$[*]" columns( x int path "$.x")) +AS jt) +from t1; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 3 +2 DEPENDENT SUBQUERY jt ALL NULL NULL NULL NULL 40 Table function: json_table +drop table t1; +# # End of 10.6 tests # set @optimizer_switch_for_subselect_test=null; diff --git a/mysql-test/main/subselect_no_scache.result b/mysql-test/main/subselect_no_scache.result index 67d738a8a78..9bbaf51fe33 100644 --- a/mysql-test/main/subselect_no_scache.result +++ b/mysql-test/main/subselect_no_scache.result @@ -7565,6 +7565,32 @@ Warning 1292 Truncated incorrect DECIMAL value: 'e' drop view v1; drop table t1, t2, t3; # +# MDEV-30623 JSON_TABLE in subquery not correctly marked as correlated +# update_correlated_cache() fails to take JSON_TABLE functions in +# subqueries into account. +# +create table t1(c json); +insert into t1 values ('[{"x":"1"},{"x":"2"}]'), +('[{"x":"10"},{"x":"20"}]'), +('[{"x":"100"},{"x":"200"}]'); +select c, +(SELECT sum(x) FROM json_table(c, "$[*]" columns( x int path "$.x")) +AS jt) +from t1; +c (SELECT sum(x) FROM json_table(c, "$[*]" columns( x int path "$.x")) +AS jt) +[{"x":"1"},{"x":"2"}] 3 +[{"x":"10"},{"x":"20"}] 30 +[{"x":"100"},{"x":"200"}] 300 +explain select c, +(SELECT sum(x) FROM json_table(c, "$[*]" columns( x int path "$.x")) +AS jt) +from t1; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 3 +2 DEPENDENT SUBQUERY jt ALL NULL NULL NULL NULL 40 Table function: json_table +drop table t1; +# # End of 10.6 tests # set optimizer_switch=default; diff --git a/mysql-test/main/subselect_no_semijoin.result b/mysql-test/main/subselect_no_semijoin.result index 0ec32e0fbf8..7269ad94df7 100644 --- a/mysql-test/main/subselect_no_semijoin.result +++ b/mysql-test/main/subselect_no_semijoin.result @@ -7550,6 +7550,32 @@ Warning 1292 Truncated incorrect DECIMAL value: 'e' drop view v1; drop table t1, t2, t3; # +# MDEV-30623 JSON_TABLE in subquery not correctly marked as correlated +# update_correlated_cache() fails to take JSON_TABLE functions in +# subqueries into account. +# +create table t1(c json); +insert into t1 values ('[{"x":"1"},{"x":"2"}]'), +('[{"x":"10"},{"x":"20"}]'), +('[{"x":"100"},{"x":"200"}]'); +select c, +(SELECT sum(x) FROM json_table(c, "$[*]" columns( x int path "$.x")) +AS jt) +from t1; +c (SELECT sum(x) FROM json_table(c, "$[*]" columns( x int path "$.x")) +AS jt) +[{"x":"1"},{"x":"2"}] 3 +[{"x":"10"},{"x":"20"}] 30 +[{"x":"100"},{"x":"200"}] 300 +explain select c, +(SELECT sum(x) FROM json_table(c, "$[*]" columns( x int path "$.x")) +AS jt) +from t1; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 3 +2 DEPENDENT SUBQUERY jt ALL NULL NULL NULL NULL 40 Table function: json_table +drop table t1; +# # End of 10.6 tests # # diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc index b45b9271269..f8acaa24278 100644 --- a/sql/sql_lex.cc +++ b/sql/sql_lex.cc @@ -5501,6 +5501,9 @@ void st_select_lex::update_correlated_cache() while ((tl= ti++)) { + if (tl->table_function) + is_correlated|= MY_TEST(tl->table_function->used_tables() & + OUTER_REF_TABLE_BIT); // is_correlated|= tl->is_with_table_recursive_reference(); if (tl->on_expr) is_correlated|= MY_TEST(tl->on_expr->used_tables() & OUTER_REF_TABLE_BIT);