Skip to content

Commit 6ccae73

Browse files
committed
SQL: fixed LEFT JOIN, RIGHT JOIN
1 parent 8211417 commit 6ccae73

File tree

4 files changed

+36
-12
lines changed

4 files changed

+36
-12
lines changed

sql/sql_lex.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2200,7 +2200,7 @@ void st_select_lex::init_query()
22002200
join= 0;
22012201
having= prep_having= where= prep_where= 0;
22022202
cond_pushed_into_where= cond_pushed_into_having= 0;
2203-
saved_conds= 0;
2203+
saved_where= 0;
22042204
olap= UNSPECIFIED_OLAP_TYPE;
22052205
having_fix_field= 0;
22062206
context.select_lex= this;

sql/sql_lex.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ class st_select_lex: public st_select_lex_node
810810
Item *prep_having;/* saved HAVING clause for prepared statement processing */
811811
Item *cond_pushed_into_where; /* condition pushed into the select's WHERE */
812812
Item *cond_pushed_into_having; /* condition pushed into the select's HAVING */
813-
Item *saved_conds;
813+
Item *saved_where;
814814
/* Saved values of the WHERE and HAVING clauses*/
815815
Item::cond_result cond_value, having_value;
816816
/* point on lex in which it was created, used in view subquery detection */

sql/sql_select.cc

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ setup_without_group(THD *thd, Ref_ptr_array ref_pointer_array,
668668
}
669669

670670
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)
672672
{
673673
DBUG_ENTER("setup_for_system_time");
674674

@@ -697,23 +697,46 @@ setup_for_system_time(THD *thd, TABLE_LIST *tables, COND **conds, SELECT_LEX *se
697697
because they must outlive execution phase for multiple executions. */
698698
arena= thd->activate_stmt_arena_if_needed(&backup);
699699

700-
if (select_lex->saved_conds)
700+
if (select_lex->saved_where)
701701
{
702702
DBUG_ASSERT(thd->stmt_arena->is_sp_execute());
703-
*conds= select_lex->saved_conds;
703+
*where_expr= select_lex->saved_where;
704704
}
705705
else if (thd->stmt_arena->is_sp_execute())
706706
{
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);
711713
}
712714

713715
for (table= tables; table; table= table->next_local)
714716
{
715717
if (table->table && table->table->versioned())
716718
{
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+
}
717740
Field *fstart= table->table->vers_start_field();
718741
Field *fend= table->table->vers_end_field();
719742

@@ -782,15 +805,15 @@ setup_for_system_time(THD *thd, TABLE_LIST *tables, COND **conds, SELECT_LEX *se
782805
if (cond1)
783806
{
784807
cond1= and_items(thd,
785-
*conds,
808+
*dst_cond,
786809
and_items(thd,
787810
cond2,
788811
cond1));
789812

790813
if (arena)
791-
*conds= cond1;
814+
*dst_cond= cond1;
792815
else
793-
thd->change_item_tree(conds, cond1);
816+
thd->change_item_tree(dst_cond, cond1);
794817

795818
table->vers_moved_to_where= true;
796819
}

sql/table.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1908,6 +1908,7 @@ struct TABLE_LIST
19081908
char *db, *alias, *table_name, *schema_table_name;
19091909
char *option; /* Used by cache index */
19101910
Item *on_expr; /* Used with outer join */
1911+
Item *saved_on_expr; /* Used with SP and System Versioning */
19111912

19121913
Item *sj_on_expr;
19131914
/*

0 commit comments

Comments
 (0)