• unknown's avatar
    Fixed bug MDEV-288 · 7ddd5418
    unknown authored
    CHEAP SQ: Valgrind warnings "Memory lost" with IN and EXISTS nested subquery, materialization+semijoin
    
    Analysis:
    The memory leak was a result of the interaction of semi-join optimization
    with early optimization of constant subqueries. The function:
    setup_jtbm_semi_joins() created a dummy temporary table "dummy_table"
    in order to make some JOIN_TAB objects complete. Normally, such temporary
    tables are freed inside JOIN_TAB::cleanup.
    
    However, the inner-most subquery is pre-optimized, which allows the
    optimization fo the MAX subquery to determine that its WHERE is TRUE,
    and thus to compute the result of the MAX during optimization. This
    ultimately allows the optimize phase of the outer query to find that
    it WHERE clause is FALSE. Once JOIN::optimize finds that the result
    set is empty, it sets zero_result_cause, and returns *before* it ever
    reached make_join_statistics(). As a result the query plan has no
    JOIN_TABs at all. Since the temporary table is supposed to be cleanup
    via JOIN_TAB::cleanup, this never happens because there is no JOIN_TAB
    for this table. Hence we get a memory leak.
    
    Solution:
    Whenever there are no JOIN_TABs, iterate over all table reference in
    JOIN::join_list, and free the ones that contain semi-join temporary
    tables.
    7ddd5418
subselect_no_semijoin.result 232 KB