@@ -668,7 +668,7 @@ setup_without_group(THD *thd, Ref_ptr_array ref_pointer_array,
668
668
}
669
669
670
670
static int
671
- setup_for_system_time(THD *thd, TABLE_LIST *tables, COND **conds , SELECT_LEX *select_lex)
671
+ setup_for_system_time(THD *thd, TABLE_LIST *tables, COND **where_expr , SELECT_LEX *select_lex)
672
672
{
673
673
DBUG_ENTER("setup_for_system_time");
674
674
@@ -697,23 +697,46 @@ setup_for_system_time(THD *thd, TABLE_LIST *tables, COND **conds, SELECT_LEX *se
697
697
because they must outlive execution phase for multiple executions. */
698
698
arena= thd->activate_stmt_arena_if_needed(&backup);
699
699
700
- if (select_lex->saved_conds )
700
+ if (select_lex->saved_where )
701
701
{
702
702
DBUG_ASSERT(thd->stmt_arena->is_sp_execute());
703
- *conds = select_lex->saved_conds ;
703
+ *where_expr = select_lex->saved_where ;
704
704
}
705
705
else if (thd->stmt_arena->is_sp_execute())
706
706
{
707
- if (thd->stmt_arena->is_stmt_execute())
708
- *conds= 0;
709
- else if (*conds)
710
- select_lex->saved_conds= (*conds)->copy_andor_structure(thd);
707
+ if (thd->stmt_arena->is_stmt_execute()) // SP executed second time (STMT_EXECUTED)
708
+ *where_expr= 0;
709
+ else if (*where_expr) // SP executed first time (STMT_INITIALIZED_FOR_SP)
710
+ /* copy_andor_structure() is required since this andor tree
711
+ is modified later (and on shorter arena) */
712
+ select_lex->saved_where= (*where_expr)->copy_andor_structure(thd);
711
713
}
712
714
713
715
for (table= tables; table; table= table->next_local)
714
716
{
715
717
if (table->table && table->table->versioned())
716
718
{
719
+ COND** dst_cond;
720
+ if (table->on_expr)
721
+ {
722
+ if (table->saved_on_expr) // same logic as saved_where
723
+ {
724
+ DBUG_ASSERT(thd->stmt_arena->is_sp_execute());
725
+ table->on_expr= table->saved_on_expr;
726
+ }
727
+ else if (thd->stmt_arena->is_sp_execute())
728
+ {
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);
733
+ }
734
+ dst_cond= &table->on_expr;
735
+ }
736
+ else
737
+ {
738
+ dst_cond= where_expr;
739
+ }
717
740
Field *fstart= table->table->vers_start_field();
718
741
Field *fend= table->table->vers_end_field();
719
742
@@ -782,15 +805,15 @@ setup_for_system_time(THD *thd, TABLE_LIST *tables, COND **conds, SELECT_LEX *se
782
805
if (cond1)
783
806
{
784
807
cond1= and_items(thd,
785
- *conds ,
808
+ *dst_cond ,
786
809
and_items(thd,
787
810
cond2,
788
811
cond1));
789
812
790
813
if (arena)
791
- *conds = cond1;
814
+ *dst_cond = cond1;
792
815
else
793
- thd->change_item_tree(conds , cond1);
816
+ thd->change_item_tree(dst_cond , cond1);
794
817
795
818
table->vers_moved_to_where= true;
796
819
}
0 commit comments