• Sergey Glukhov's avatar
    Fixed following problems: · dda99909
    Sergey Glukhov authored
    --Bug#52157 various crashes and assertions with multi-table update, stored function
    --Bug#54475 improper error handling causes cascading crashing failures in innodb/ndb
    --Bug#57703 create view cause Assertion failed: 0, file .\item_subselect.cc, line 846
    --Bug#57352 valgrind warnings when creating view
    --Recently discovered problem when a nested materialized derived table is used
      before being populated and it leads to incorrect result
    
    We have several modes when we should disable subquery evaluation.
    The reasons for disabling are different. It could be
    uselessness of the evaluation as in case of 'CREATE VIEW'
    or 'PREPARE stmt', or we should disable subquery evaluation
    if tables are not locked yet as it happens in bug#54475, or
    too early evaluation of subqueries can lead to wrong result
    as it happened in Bug#19077.
    Main problem is that if subquery items are treated as const
    they are evaluated in ::fix_fields(), ::fix_length_and_dec()
    of the parental items as a lot of these methods have
    Item::val_...() calls inside.
    We have to make subqueries non-const to prevent unnecessary
    subquery evaluation. At the moment we have different methods
    for this. Here is a list of these modes:
    
    1. PREPARE stmt;
    We use UNCACHEABLE_PREPARE flag.
    It is set during parsing in sql_parse.cc, mysql_new_select() for
    each SELECT_LEX object and cleared at the end of PREPARE in
    sql_prepare.cc, init_stmt_after_parse(). If this flag is set
    subquery becomes non-const and evaluation does not happen.
    
    2. CREATE|ALTER VIEW, SHOW CREATE VIEW, I_S tables which
       process FRM files
    We use LEX::view_prepare_mode field. We set it before
    view preparation and check this flag in
    ::fix_fields(), ::fix_length_and_dec().
    Some bugs are fixed using this approach,
    some are not(Bug#57352, Bug#57703). The problem here is
    that we have a lot of ::fix_fields(), ::fix_length_and_dec()
    where we use Item::val_...() calls for const items.
    
    3. Derived tables with subquery = wrong result(Bug19077)
    The reason of this bug is too early subquery evaluation.
    It was fixed by adding Item::with_subselect field
    The check of this field in appropriate places prevents
    const item evaluation if the item have subquery.
    The fix for Bug19077 fixes only the problem with
    convert_constant_item() function and does not cover
    other places(::fix_fields(), ::fix_length_and_dec() again)
    where subqueries could be evaluated.
    
    Example:
    CREATE TABLE t1 (i INT, j BIGINT);
    INSERT INTO t1 VALUES (1, 2), (2, 2), (3, 2);
    SELECT * FROM (SELECT MIN(i) FROM t1
    WHERE j = SUBSTRING('12', (SELECT * FROM (SELECT MIN(j) FROM t1) t2))) t3;
    DROP TABLE t1;
    
    4. Derived tables with subquery where subquery
       is evaluated before table locking(Bug#54475, Bug#52157)
    
    Suggested solution is following:
    
    -Introduce new field LEX::context_analysis_only with the following
     possible flags:
     #define CONTEXT_ANALYSIS_ONLY_PREPARE 1
     #define CONTEXT_ANALYSIS_ONLY_VIEW    2
     #define CONTEXT_ANALYSIS_ONLY_DERIVED 4
    -Set/clean these flags when we perform
     context analysis operation
    -Item_subselect::const_item() returns
     result depending on LEX::context_analysis_only.
     If context_analysis_only is set then we return
     FALSE that means that subquery is non-const.
     As all subquery types are wrapped by Item_subselect
     it allow as to make subquery non-const when
     it's necessary.
    
    
    mysql-test/r/derived.result:
      test case
    mysql-test/r/multi_update.result:
      test case
    mysql-test/r/view.result:
      test case
    mysql-test/suite/innodb/r/innodb_multi_update.result:
      test case
    mysql-test/suite/innodb/t/innodb_multi_update.test:
      test case
    mysql-test/suite/innodb_plugin/r/innodb_multi_update.result:
      test case
    mysql-test/suite/innodb_plugin/t/innodb_multi_update.test:
      test case
    mysql-test/t/derived.test:
      test case
    mysql-test/t/multi_update.test:
      test case
    mysql-test/t/view.test:
      test case
    sql/item.cc:
      --removed unnecessary code
    sql/item_cmpfunc.cc:
      --removed unnecessary checks
      --THD::is_context_analysis_only() is replaced with LEX::is_ps_or_view_context_analysis()
    sql/item_func.cc:
      --refactored context analysis checks
    sql/item_row.cc:
      --removed unnecessary checks
    sql/item_subselect.cc:
      --removed unnecessary code
      --added DBUG_ASSERT into Item_subselect::exec()
        which asserts that subquery execution can not happen
        if LEX::context_analysis_only is set, i.e. at context
        analysis stage.
      --Item_subselect::const_item()
        Return FALSE if LEX::context_analysis_only is set.
        It prevents subquery evaluation in ::fix_fields &
        ::fix_length_and_dec at context analysis stage.
    sql/item_subselect.h:
      --removed unnecessary code
    sql/mysql_priv.h:
      --Added new set of flags.
    sql/sql_class.h:
      --removed unnecessary code
    sql/sql_derived.cc:
      --added LEX::context_analysis_only analysis intialization/cleanup
    sql/sql_lex.cc:
      --init LEX::context_analysis_only field
    sql/sql_lex.h:
      --New LEX::context_analysis_only field
    sql/sql_parse.cc:
      --removed unnecessary code
    sql/sql_prepare.cc:
      --removed unnecessary code
      --added LEX::context_analysis_only analysis intialization/cleanup
    sql/sql_select.cc:
      --refactored context analysis checks
    sql/sql_show.cc:
      --added LEX::context_analysis_only analysis intialization/cleanup
    sql/sql_view.cc:
      --added LEX::context_analysis_only analysis intialization/cleanup
    dda99909
innodb_multi_update.result 1.58 KB