Commit graph

62 commits

Author SHA1 Message Date
Igor Babaev
46960365b1 MDEV-17871 Crash when running explain with CTE
When the with clause of a query contains a recursive CTE that is not used
then processing of EXPLAIN for this query does not require optimization
of the unit specifying this CTE. In this case if 'derived' is the
TABLE_LIST object created for this CTE then derived->derived_result is NULL
and any assignment to derived->derived_result->table causes a crash.
After fixing this problem in the code of st_select_lex_unit::prepare()
EXPLAIN for such a query worked without crashes. Yet an execution
plan for the recursive CTE appeared there. The cause of this problem was
an incorrect condition used in JOIN::save_explain_data_intern() that
determined whether CTE was to be optimized or not. A similar condition was
used in select_describe() and this patch has corrected it as well.
2018-12-01 15:06:04 -08:00
Igor Babaev
4142589207 MDEV-17635 Server hangs after the query with recursive CTE
This bug in the code of the function With_element::check_unrestricted_recursive()
could force a recursive CTE to be executed in a non-standard compliant mode
in which recursive UNION ALL could lead to an infinite execution. This
problem could occur only in the case when this CTE was used by another
recursive CTE at least twice.
2018-11-07 12:08:15 -08:00
Igor Babaev
6858d5346c MDEV-17201 dropped anchor rows with non-null recursion query
The function st_select_lex_unit::exec_recursive() missed resetting of
select_limit_cnt and offset_limit_cnt before execution of union parts.
As a result recursive CTEs specified by UNIONs whose SELECTs contained
LIMIT/OFFSET could return wrong sets of records.
2018-09-17 23:15:56 -07:00
Igor Babaev
4d991abd4f MDEV-17024 Crash on large query
This problem manifested itself when a join query used two or more
materialized CTE such that each of them employed the same recursive CTE.
The bug caused a crash. The crash happened because the cleanup()
function was performed premature for recursive CTE. This clean up was
induced by the cleanup of the first CTE referenced the recusrsive CTE.
This cleanup destroyed the structures that would allow to read from the
temporary table containing the rows of the recursive CTE and an attempt to read
these rows for the second CTE referencing the recursive CTE triggered a
crash.
The clean up for a recursive CTE R should be performed after the cleanup
of the last materialized CTE that uses R.
2018-09-07 20:10:45 -07:00
Igor Babaev
e9b78a1055 Removed the test case for MDEV-15151 for the following reasons:
1. The changed variant did not fail without the patch for MDEV-16629
while the original test case did fail.
2. In any case the test case should go to cte_recursive_not_embedded.test
that was not created yet.
2018-07-05 15:10:47 -07:00
Marko Mäkelä
1b335a74b4 Clean up a test
At the end of a test, 'connection default' should be in a usable state.
This was not the case, because there was a preceding 'send' without a
'reap'. If 'reap' was added, an error would be reported because the
server was restarted after the 'send'. It is easiest to 'send' from a
separate connection and do the restart from 'connection default'.
2018-07-05 16:47:54 +03:00
Igor Babaev
058554027f MDEV-16629 "Table Does Not Exist" Error from Recursive CTE Query Inside Function
When processing a query containing with clauses a call of the function
check_dependencies_in_with_clauses() before opening tables used in the
query is necessary if with clauses include specifications of recursive
CTEs.
This call was missing if such a query belonged to a stored function.
This caused misbehavior of the server: it could report a fake error
as in the test case for MDEV-16629 or the executed query could hang
as in the test cases for MDEV-16661 and MDEV-15151.
2018-07-05 00:06:39 -07:00
Igor Babaev
de86997160 MDEV-15581 Incorrect result (missing row) with UNION DISTINCT in anchor parts
The current code does not support recursive CTEs whose specifications
contain a mix of ALL UNION and DISTINCT UNION operations.
This patch catches such specifications and reports errors for them.
2018-05-17 22:58:21 -07:00
Igor Babaev
d309c2fc88 MDEV-16212 Memory leak with recursive CTE that uses global ORDER BY
with recursive subquery

There were two problems:
1. The code did not report that usage of global ORDER BY / LIMIT clauses
was not supported yet.
2. The code just reset fake_select_lex of the the unit specifying
a recursive CTE to NULL and that caused memory leaks in some cases.
2018-05-17 15:47:48 -07:00
Igor Babaev
9bcd0f5fea Added the test case from MDEV-16086 tfixed by the patch for MDEV-15575 2018-05-07 13:22:00 -07:00
Igor Babaev
f033fbd9f2 Changed the test case for MDEV-15571
It has been done to demonstrate that the fix of this bug is good for 10.3
as well. The previous test case is not good for this purpose because
10.2 and 10.3 use different rules for determining the types of recursive
CTEs.
2018-04-24 12:39:41 -07:00
Igor Babaev
1d98333ad9 MDEV-15894 Error, while using aggregated functions/window functions in anchor part
Usage of aggregate/window functions in non-recursive parts of recursive CTEs
is allowed. Error messages complaining about this were reported by mistake.
2018-04-17 10:35:55 -07:00
Igor Babaev
e34d3184fd MDEV-15556 MariaDB crash with big_tables=1 and CTE
This bug manifested itself when the optimizer chose an execution plan with
an access of the recursive CTE in a recursive query by key and ARIA/MYISAM
temporary tables were used to store recursive tables.
The problem appeared due to passing an incorrect parameter to the call of
instantiate_tmp_table() in the function With_element::instantiate_tmp_tables().
2018-04-16 10:31:30 -07:00
Igor Babaev
612850782d MDEV-15571 Wrong results with big_tables=1 and CTE
The cause of this bug was the same as for the bug mdev-15575.
Fixed by the patch for the latter.
2018-04-16 08:55:15 -07:00
Igor Babaev
224f7af911 MDEV-15575 different results when using CTE and big_tables=1.
This bug happened due to a defect of the implementation of the handler
function ha_delete_all_rows() for the ARIA engine.
The function maria_delete_all_rows() truncated the table, but it didn't
touch the write cache, so the cache's write offset was not reset.
In the scenario like in the function st_select_lex_unit::exec_recursive
when first all records were deleted from the table and then several new
records were added some metadata became inconsistent with the state of
the cache. As a result the table scan function could not read records
at the end of the table.
The same defect could be found in the implementation of ha_delete_all_rows()
for the MYISAM engine mi_delete_all_rows().

Additionally made late instantiation for the temporary table used to store
rows that were used for each new iteration when executing a recursive CTE.
2018-04-16 07:22:26 -07:00
Igor Babaev
2ef41622e2 Fixed mdev-15162 Query with CTE hangs if assignment operator (:=) is used
If setting user variable was used in the specification of a recursive CTE
then Item_func_set_user_var::fix_fields() went into an infinite loop.
2018-02-06 12:12:19 -08:00
Igor Babaev
7d201d7b30 Fixed mdev-14879 Lost rows for query using recursive CTE
with recursive reference in subquery

If a recursive CTE uses a subquery with recursive reference then
the virtual function reset() must be called after each iteration
performed at the execution of the CTE.
2018-01-09 09:29:16 -08:00
Igor Babaev
15b1840f43 Added the test case from for mdev-14777: Crash in MariaDB 10.2.12 on query
using VIEW and WITH RECURSIVE.

The cause of this crash was the same as of the crash reported in mdev-14755.
2018-01-05 12:13:23 -08:00
Galina Shalygina
079c359971 MDEV-14629: failing assertion when a user-defined variable is defined by the recursive CTE
During the user-defined variable defined by the recursive CTE handling procedure
check_dependencies_in_with_clauses that checks dependencies between the tables
that are defined in the CTE and find recursive definitions wasn't called.
2017-12-19 11:49:40 +02:00
Igor Babaev
e0cd6f4b07 Fixed bugs: mdev-13780 CTE not found, mdev-14184 recursive CTE not found
The support of embedded CTEs was not correct in the cases when
embedded CTEs were used multiple times. The problems occurred with
both non-recursive (bug mdev-13780) and recursive (bug mdev-14184)
embedded CTEs.
2017-11-05 18:46:05 -08:00
Igor Babaev
7a29ca2776 Fixed the bug mdev-12563.
The bug happened when the specification of a recursive CTE had
no recursive references at the top level of the specification.
In this case the regular processing of derived table references
of the select containing a non-recursive reference to this
recursive CTE misses handling the specification unit.
At the preparation stage any non-recursive reference to a
recursive CTE must be handled after the preparation of the
specification unit for this CTE. So we have to force this
preparation when regular handling of derived tables does not
do it.
2017-04-28 21:59:11 -07:00
Igor Babaev
c7a153453e Fixed the bug mdev-12556.
When the rows produced on the current iteration are sent to the
temporary table T of the UNION type created for CTE the rows
that were not there simultaneously are sent to the temporary
table D that contains rows for the next iteration. The test
whether a row was in T checks the return code of writing into T.
If just a HEAP table is used for T then the return code is
HA_ERR_FOUND_DUPP_KEY, but if an ARIA table is used for T then
the return code is HA_ERR_FOUND_DUPP_UNIQUE.
The implementation of select_union_recursive::send_data()
erroneously checked only for the first return code. So if an Aria
table was used for T then all rows produced by the current iteration
went to D and and in most cases D grew with each iteration.
Whether T has reached stabilization is detected by
checking whether D is empty. So as a result, the iterations were
never stopped unless a limit for them was set.

Fixed by checking for both HA_ERR_FOUND_DUPP_KEY and
HA_ERR_FOUND_DUPP_UNIQUE as return codes returned by
the function writing a row into the temporary table T.
2017-04-26 10:16:03 -07:00
Igor Babaev
d66c171e0f Fixed the bug mdev-12554.
When the WHERE condition is always FALSE then JOIN::join_tab is NULL.
2017-04-22 23:45:38 -07:00
Igor Babaev
54a995cd22 Fixed the bug mdev-12519.
This patch fixed some problems that occurred with subqueries that
contained directly or indirectly recursive references to recursive CTEs.

1. A [NOT] IN predicate with a constant left operand and a non-correlated
subquery as the right operand used in the specification of a recursive CTE
was considered as a constant predicate and was evaluated only once.
Now such a predicate is re-evaluated after every iteration of the process
that produces the records of the recursive CTE.
2. The Exists-To-IN transformation could be applied to [NOT] IN predicates
with recursive references. This opened a possibility of materialization
for the subqueries used as right operands. Yet, materialization
is prohibited for the subqueries if they contain a recursive reference.
Now the Exists-To-IN transformation cannot be applied for subquery
predicates with recursive references.

The function st_select_lex::check_subqueries_with_recursive_references()
is called now only for the first execution of the SELECT.
2017-04-21 09:33:36 -07:00
Igor Babaev
93dd70ced8 Fixed bug mdev-12375.
The function st_select_lex_unit::exec_recursive() incorrectly determined
that a CTE mutually recursive with some others was stabilized in the case
when the non-recursive part of the CTE returned an empty set. As a result
the server fell into an infinite loop when executing a query using
this CTE.
2017-03-27 14:41:17 -07:00
Igor Babaev
ad7da60ded Fixed bug mdev-12368.
Mutually recursive CTE could cause a crash of the server in the case
when they were not Standard compliant. The crash happened in
mysql_derived_prepare(), because the destructor the derived_result
object created for a CTE that was mutually recursive with some others
was called twice. Yet this destructor should not be called for resursive
references.
2017-03-26 23:00:28 -07:00
Igor Babaev
5a4537f092 Fixed bug mdev-12360.
The method With_element::check_unrestricted_recursive() icorrectly performed
the check that no recursive reference is not encountered in inner parts of
outer joins. As a result the server reported errors for valid specifications
with outer joins.
2017-03-26 23:00:28 -07:00
Sergei Golubchik
f3914d10b6 Merge branch 'bb-10.2-serg-merge' into 10.2 2017-02-11 09:45:34 +01:00
Sergei Golubchik
2195bb4e41 Merge branch '10.1' into 10.2 2017-02-10 17:01:45 +01:00
Nirbhay Choubey
185d140f19 MDEV-7635: Renamed standards_compliant_cte to standard_compliant_cte 2017-02-10 06:30:42 -05:00
Sergei Golubchik
9f918b9cf4 take into account scientific notation of floats in regex_replace
this fixes occasional main.cte_recursive failures
2017-01-30 17:57:02 +01:00
Igor Babaev
abfcdb8fbc Fixed bug mdev-10773.
The temporary tables created for recursive table references
should be closed in close_thread_tables(), because they might
be used in the statements like ANALYZE WITH r AS (...) SELECT * from r
where r is defined through recursion.
2017-01-28 15:47:11 -08:00
Sergei Petrunia
25e5ce1982 NOT FIXED: MDEV-10773: ANALYZE FORMAT=JSON query_with_CTE crashes
Remove the testcase. It turns out, the crash still happens, just not
on all platforms.
2017-01-18 14:13:11 +03:00
Sergei Petrunia
45f11a729c MDEV-10773: ANALYZE FORMAT=JSON query_with_CTE crashes
Added testcase
2017-01-18 00:56:24 +03:00
Igor Babaev
348ccb6f03 Fixed bug mdev-11674.
1. The rows of a recursive CTE at some point may overflow
the HEAP temporary table containing them. At this point
the table is converted to a MyISAM temporary table and the
new added rows are placed into this MyISAM table.
A bug in the of select_union_recursive::send_data prevented
the server from writing the row that caused the overflow
into the temporary table used for the result of the iteration
steps. This could lead, in particular,to a premature end
of the iterations.
2. The method TABLE::insert_all_rows_into() that was used
to copy all rows of one temporary table into another
did not take into account that the destination temporary
table must be converted to a MyISAM table at some point.
This patch fixed this problem. It also renamed the method
into TABLE::insert_all_rows_into_tmp_table() and added
an extra parameter needed for the conversion.
2017-01-04 14:33:24 -08:00
Igor Babaev
0b869814dc Made the result output deterministic. 2016-11-15 09:25:33 -08:00
Igor Babaev
8283d7f70e Added the test case from mdev-11259. 2016-11-13 14:56:30 -08:00
Igor Babaev
92bcb906a0 Fixed bug mdev-11278.
If a recursive CTE referred to a materialized view/derived table then
the query that used this CTE returned a bogus error message.
2016-11-13 14:56:29 -08:00
Igor Babaev
3b314ec633 Fixed bug mdev-10933.
The bug was caused by a misplaced construct opt_with_clause
for one of the variants of CREATE ... SELECT.
2016-09-30 17:40:43 -07:00
Igor Babaev
6aeaebd8cf Fixed the bug number in the comment of the test case for mdev-10899. 2016-09-30 17:40:40 -07:00
Igor Babaev
b91bd822fa Fixed bug mdev-10889
The bug was in the code of the recursive method
With_element::check_unrestricted_recursive. For recursive
calls of this method sel->get_with_element()->owner != owner.
2016-09-26 10:40:44 -07:00
Igor Babaev
54efb08022 Fixed bug mdev-10881
The server missed to call check_dependencies_in_with_clauses()
when processing PREPARE ... FROM CREATE ... SELECT / INSERT ... SELECT
with WITH clause before SELECT.
2016-09-24 21:05:36 -07:00
Igor Babaev
61ab7333db Fixed bug mdev-10883.
When a prepared statement uses a CTE definition with a column list
renaming of columns of the CTE expression  must be performed
for every execution of the prepared statement.
2016-09-24 21:05:36 -07:00
Igor Babaev
48b4e33596 Allowed to use WITH clauses before SELECT in CREATE ... SELECT
and INSERT ... SELECT.
Added test cases.
2016-09-22 01:45:05 -07:00
Igor Babaev
5c7d829346 Another attempt to fix bug mdev-10736. 2016-09-09 13:25:02 -07:00
Igor Babaev
d6f87e68f1 For some unclear reasons the test case for bug mdev-10736
does not work in non-debug modes.
2016-09-06 14:36:59 -07:00
Igor Babaev
2d36e5aa38 Fixed bug mdev-10736 that caused crashes.
The bug manifested itself for recursive definitions that
used anchors over tables with blobs.
2016-09-06 10:05:36 -07:00
Igor Babaev
8ae65920fa Fixed bug mdev-10737.
This bug in st_select_lex_node::move_node could result
in invalid select lists in recursive units that could
cause falling into infinite loops when iterating over
selects in such units.
2016-09-05 23:07:31 -07:00
Igor Babaev
501fc1a9e2 Returned the test case that was removed by mistake. 2016-08-29 22:58:01 -07:00
Igor Babaev
9ac235ab7d mdev-9864: cleanup, re-factoring.
Added comments.
Added reaction for exceeding maximum number of elements in with clause.
Added a test case to check this reaction.
Added a test case where the specification of a recursive table
uses two non-recursive with tables.
2016-08-29 22:45:17 -07:00