Skip to content

Commit 6bac48d

Browse files
spetruniaAlexey Botchkov
authored andcommitted
MDEV-25381: JSON_TABLE: ER_WRONG_OUTER_JOIN upon query with LEFT and RIGHT joins and view
Table_function_json_table::m_dep_tables attempts to cache the value of m_json->used_tables(), poorly. Remove the cache and use the value directly.
1 parent 4a10dd0 commit 6bac48d

File tree

4 files changed

+95
-28
lines changed

4 files changed

+95
-28
lines changed

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,5 +750,48 @@ id select_type table type possible_keys key key_len ref rows Extra
750750
1 PRIMARY jt ALL NULL NULL NULL NULL 40 Table function: json_table; Using where; FirstMatch(t1)
751751
drop table t1;
752752
#
753+
# MDEV-25381: JSON_TABLE: ER_WRONG_OUTER_JOIN upon query with LEFT and RIGHT joins and view
754+
#
755+
CREATE TABLE t1 (a INT);
756+
INSERT INTO t1 VALUES (1),(2);
757+
CREATE TABLE t2 (b INT, c TEXT);
758+
INSERT INTO t2 VALUES (1,'{}'),(2,'[]');
759+
CREATE VIEW v2 AS SELECT * FROM t2;
760+
SELECT *
761+
FROM
762+
t1 RIGHT JOIN
763+
t2 AS tt
764+
LEFT JOIN
765+
JSON_TABLE(tt.c, '$' COLUMNS(o FOR ORDINALITY)) AS jt
766+
ON tt.b = jt.o
767+
ON t1.a = tt.b;
768+
a b c o
769+
1 1 {} 1
770+
2 2 [] NULL
771+
SELECT *
772+
FROM
773+
t1 RIGHT JOIN
774+
v2 AS tt
775+
LEFT JOIN
776+
JSON_TABLE(tt.c, '$' COLUMNS(o FOR ORDINALITY)) AS jt
777+
ON tt.b = jt.o
778+
ON t1.a = tt.b;
779+
a b c o
780+
1 1 {} 1
781+
2 2 [] NULL
782+
SELECT *
783+
FROM
784+
t1 RIGHT JOIN
785+
v2 AS tt
786+
LEFT JOIN
787+
JSON_TABLE(CONCAT(tt.c,''), '$' COLUMNS(o FOR ORDINALITY)) AS jt
788+
ON tt.b = jt.o
789+
ON t1.a = tt.b;
790+
a b c o
791+
1 1 {} 1
792+
2 2 [] NULL
793+
DROP VIEW v2;
794+
DROP TABLE t1, t2;
795+
#
753796
# End of 10.6 tests
754797
#

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,47 @@ WHERE
644644

645645
drop table t1;
646646

647+
--echo #
648+
--echo # MDEV-25381: JSON_TABLE: ER_WRONG_OUTER_JOIN upon query with LEFT and RIGHT joins and view
649+
--echo #
650+
651+
CREATE TABLE t1 (a INT);
652+
INSERT INTO t1 VALUES (1),(2);
653+
654+
CREATE TABLE t2 (b INT, c TEXT);
655+
INSERT INTO t2 VALUES (1,'{}'),(2,'[]');
656+
CREATE VIEW v2 AS SELECT * FROM t2;
657+
658+
SELECT *
659+
FROM
660+
t1 RIGHT JOIN
661+
t2 AS tt
662+
LEFT JOIN
663+
JSON_TABLE(tt.c, '$' COLUMNS(o FOR ORDINALITY)) AS jt
664+
ON tt.b = jt.o
665+
ON t1.a = tt.b;
666+
667+
SELECT *
668+
FROM
669+
t1 RIGHT JOIN
670+
v2 AS tt
671+
LEFT JOIN
672+
JSON_TABLE(tt.c, '$' COLUMNS(o FOR ORDINALITY)) AS jt
673+
ON tt.b = jt.o
674+
ON t1.a = tt.b;
675+
676+
SELECT *
677+
FROM
678+
t1 RIGHT JOIN
679+
v2 AS tt
680+
LEFT JOIN
681+
JSON_TABLE(CONCAT(tt.c,''), '$' COLUMNS(o FOR ORDINALITY)) AS jt
682+
ON tt.b = jt.o
683+
ON t1.a = tt.b;
684+
685+
DROP VIEW v2;
686+
DROP TABLE t1, t2;
687+
647688
--echo #
648689
--echo # End of 10.6 tests
649690
--echo #

sql/json_table.cc

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,20 +1324,6 @@ int Table_function_json_table::setup(THD *thd, TABLE_LIST *sql_table,
13241324
jc->m_field->charset= jc->m_explicit_cs;
13251325
}
13261326
}
1327-
1328-
m_dep_tables= m_json->used_tables();
1329-
1330-
if (m_dep_tables)
1331-
{
1332-
t->no_cache= TRUE;
1333-
if (unlikely(m_dep_tables & sql_table->get_map()))
1334-
{
1335-
/* Table itself is used in the argument. */
1336-
my_error(ER_WRONG_USAGE, MYF(0), "JSON_TABLE", "argument");
1337-
return TRUE;
1338-
}
1339-
}
1340-
13411327
return FALSE;
13421328
}
13431329

@@ -1475,12 +1461,8 @@ int Table_function_json_table::print(THD *thd, TABLE_LIST *sql_table,
14751461
void Table_function_json_table::fix_after_pullout(TABLE_LIST *sql_table,
14761462
st_select_lex *new_parent, bool merge)
14771463
{
1478-
sql_table->dep_tables&= ~m_dep_tables;
1479-
14801464
m_json->fix_after_pullout(new_parent, &m_json, merge);
1481-
m_dep_tables= m_json->used_tables();
1482-
1483-
sql_table->dep_tables|= m_dep_tables;
1465+
sql_table->dep_tables= used_tables();
14841466
}
14851467

14861468

sql/json_table.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,16 @@ class Table_function_json_table : public Sql_alloc
208208
st_select_lex *new_parent, bool merge);
209209
void update_used_tables() { m_json->update_used_tables(); }
210210

211-
table_map used_tables() const { return m_dep_tables; }
212-
bool join_cache_allowed() const { return !m_dep_tables; }
211+
table_map used_tables() const { return m_json->used_tables(); }
212+
bool join_cache_allowed() const
213+
{
214+
/*
215+
Can use join cache when we have an outside reference.
216+
If there's dependency on any other table or randomness,
217+
cannot use it.
218+
*/
219+
return !(used_tables() & ~OUTER_REF_TABLE_BIT);
220+
}
213221
void get_estimates(ha_rows *out_rows,
214222
double *scan_time, double *startup_cost);
215223

@@ -242,13 +250,6 @@ class Table_function_json_table : public Sql_alloc
242250
/* Context to be used for resolving the first argument. */
243251
Name_resolution_context *m_context;
244252

245-
/*
246-
the JSON argument can be taken from other tables.
247-
We have to mark these tables as dependent so the
248-
mask of these dependent tables is calculated in ::setup().
249-
*/
250-
table_map m_dep_tables;
251-
252253
/* Current NESTED PATH level being parsed */
253254
Json_table_nested_path *cur_parent;
254255

0 commit comments

Comments
 (0)