@@ -681,6 +681,7 @@ setup_for_system_time(THD *thd, TABLE_LIST *tables, COND **where_expr, SELECT_LE
681681 && !is_prepare
682682 && !thd->stmt_arena->is_sp_execute())
683683 {
684+ // statement is already prepared
684685 DBUG_RETURN(0);
685686 }
686687
@@ -716,27 +717,30 @@ setup_for_system_time(THD *thd, TABLE_LIST *tables, COND **where_expr, SELECT_LE
716717 {
717718 if (table->table && table->table->versioned())
718719 {
719- COND** dst_cond;
720- if (table->on_expr)
720+ COND** dst_cond= where_expr ;
721+ if (table->saved_on_expr) // same logic as saved_where
721722 {
722- if (table->saved_on_expr) // same logic as saved_where
723+ DBUG_ASSERT(thd->stmt_arena->is_sp_execute());
724+ if (table->on_expr)
723725 {
724- DBUG_ASSERT(thd->stmt_arena->is_sp_execute());
725726 table->on_expr= table->saved_on_expr;
727+ dst_cond= &table->on_expr;
726728 }
727- else if (thd->stmt_arena->is_sp_execute())
729+ else
728730 {
729- if (thd->stmt_arena->is_stmt_execute()) // SP executed second time (STMT_EXECUTED )
730- table->on_expr= 0;
731- else if (table->on_expr) // SP executed first time (STMT_INITIALIZED_FOR_SP)
732- table->saved_on_expr= table->on_expr->copy_andor_structure(thd );
731+ // on_expr was moved to WHERE (see below: Add ON expression to the WHERE )
732+ *dst_cond= and_items(thd,
733+ *where_expr,
734+ table->saved_on_expr);
733735 }
734- dst_cond= &table->on_expr;
735736 }
736- else
737+ else if (table->on_expr)
737738 {
738- dst_cond= where_expr;
739+ dst_cond= &table->on_expr;
740+ if (thd->stmt_arena->state == Query_arena::STMT_INITIALIZED_FOR_SP)
741+ table->saved_on_expr= table->on_expr->copy_andor_structure(thd);
739742 }
743+
740744 Field *fstart= table->table->vers_start_field();
741745 Field *fend= table->table->vers_end_field();
742746
0 commit comments