Skip to content

Commit a0e137c

Browse files
committed
SQL: RIGHT JOIN in derived [fix #383]
1 parent cb4657e commit a0e137c

File tree

4 files changed

+163
-14
lines changed

4 files changed

+163
-14
lines changed

mysql-test/suite/versioning/r/derived.result

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,5 +215,83 @@ select y from t2 for system_time as of @t1;
215215
x
216216
1
217217
10
218+
# LEFT/RIGHT JOIN
219+
create or replace table t1 (x int, y int) with system versioning;
220+
create or replace table t2 (x int, y int) with system versioning;
221+
insert into t1 values (1, 1), (1, 2), (1, 3), (4, 4), (5, 5);
222+
insert into t2 values (1, 2), (2, 1), (3, 1);
223+
## LEFT JOIN: t1, t2 versioned
224+
select * from (
225+
select t1.x as LJ1_x1, t1.y as y1, t2.x as x2, t2.y as y2
226+
from t1 left join t2 on t1.x = t2.x)
227+
as derived;
228+
LJ1_x1 y1 x2 y2
229+
1 1 1 2
230+
1 2 1 2
231+
1 3 1 2
232+
4 4 NULL NULL
233+
5 5 NULL NULL
234+
alter table t2 drop system versioning;
235+
## LEFT JOIN: t1 versioned
236+
select * from (
237+
select t1.x as LJ2_x1, t1.y as y1, t2.x as x2, t2.y as y2
238+
from t1 left join t2 on t1.x = t2.x)
239+
as derived;
240+
LJ2_x1 y1 x2 y2
241+
1 1 1 2
242+
1 2 1 2
243+
1 3 1 2
244+
4 4 NULL NULL
245+
5 5 NULL NULL
246+
alter table t1 drop system versioning;
247+
alter table t2 add system versioning;
248+
## LEFT JOIN: t2 versioned
249+
select * from (
250+
select t1.x as LJ3_x1, t1.y as y1, t2.x as x2, t2.y as y2
251+
from t1 left join t2 on t1.x = t2.x)
252+
as derived;
253+
LJ3_x1 y1 x2 y2
254+
1 1 1 2
255+
1 2 1 2
256+
1 3 1 2
257+
4 4 NULL NULL
258+
5 5 NULL NULL
259+
alter table t1 add system versioning;
260+
## RIGHT JOIN: t1, t2 versioned
261+
select * from (
262+
select t1.x as RJ1_x1, t1.y as y1, t2.x as x2, t2.y as y2
263+
from t1 right join t2 on t1.x = t2.x)
264+
as derived;
265+
RJ1_x1 y1 x2 y2
266+
1 1 1 2
267+
1 2 1 2
268+
1 3 1 2
269+
NULL NULL 2 1
270+
NULL NULL 3 1
271+
alter table t2 drop system versioning;
272+
## RIGHT JOIN: t1 versioned
273+
select * from (
274+
select t1.x as RJ2_x1, t1.y as y1, t2.x as x2, t2.y as y2
275+
from t1 right join t2 on t1.x = t2.x)
276+
as derived;
277+
RJ2_x1 y1 x2 y2
278+
1 1 1 2
279+
1 2 1 2
280+
1 3 1 2
281+
NULL NULL 2 1
282+
NULL NULL 3 1
283+
alter table t1 drop system versioning;
284+
alter table t2 add system versioning;
285+
## RIGHT JOIN: t2 versioned
286+
select * from (
287+
select t1.x as RJ3_x1, t1.y as y1, t2.x as x2, t2.y as y2
288+
from t1 right join t2 on t1.x = t2.x)
289+
as derived;
290+
RJ3_x1 y1 x2 y2
291+
1 1 1 2
292+
1 2 1 2
293+
1 3 1 2
294+
NULL NULL 2 1
295+
NULL NULL 3 1
218296
drop table t1, t2;
219297
drop view vt1, vt2;

mysql-test/suite/versioning/t/derived.test

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,5 +151,61 @@ select y from t2 for system_time as of @t1;
151151
select x from t1 for system_time as of @t0 union
152152
select y from t2 for system_time as of @t1;
153153

154+
--echo # LEFT/RIGHT JOIN
155+
create or replace table t1 (x int, y int) with system versioning;
156+
create or replace table t2 (x int, y int) with system versioning;
157+
158+
insert into t1 values (1, 1), (1, 2), (1, 3), (4, 4), (5, 5);
159+
insert into t2 values (1, 2), (2, 1), (3, 1);
160+
161+
--echo ## LEFT JOIN: t1, t2 versioned
162+
select * from (
163+
select t1.x as LJ1_x1, t1.y as y1, t2.x as x2, t2.y as y2
164+
from t1 left join t2 on t1.x = t2.x)
165+
as derived;
166+
167+
alter table t2 drop system versioning;
168+
169+
--echo ## LEFT JOIN: t1 versioned
170+
select * from (
171+
select t1.x as LJ2_x1, t1.y as y1, t2.x as x2, t2.y as y2
172+
from t1 left join t2 on t1.x = t2.x)
173+
as derived;
174+
175+
alter table t1 drop system versioning;
176+
alter table t2 add system versioning;
177+
178+
--echo ## LEFT JOIN: t2 versioned
179+
select * from (
180+
select t1.x as LJ3_x1, t1.y as y1, t2.x as x2, t2.y as y2
181+
from t1 left join t2 on t1.x = t2.x)
182+
as derived;
183+
184+
alter table t1 add system versioning;
185+
186+
--echo ## RIGHT JOIN: t1, t2 versioned
187+
select * from (
188+
select t1.x as RJ1_x1, t1.y as y1, t2.x as x2, t2.y as y2
189+
from t1 right join t2 on t1.x = t2.x)
190+
as derived;
191+
192+
alter table t2 drop system versioning;
193+
194+
--echo ## RIGHT JOIN: t1 versioned
195+
select * from (
196+
select t1.x as RJ2_x1, t1.y as y1, t2.x as x2, t2.y as y2
197+
from t1 right join t2 on t1.x = t2.x)
198+
as derived;
199+
200+
alter table t1 drop system versioning;
201+
alter table t2 add system versioning;
202+
203+
--echo ## RIGHT JOIN: t2 versioned
204+
select * from (
205+
select t1.x as RJ3_x1, t1.y as y1, t2.x as x2, t2.y as y2
206+
from t1 right join t2 on t1.x = t2.x)
207+
as derived;
208+
154209
drop table t1, t2;
155210
drop view vt1, vt2;
211+

sql/sql_select.cc

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,9 @@ int SELECT_LEX::vers_setup_conds(THD *thd, TABLE_LIST *tables, COND **where_expr
818818
}
819819
}
820820

821+
COND** dst_cond= where_expr;
822+
COND* vers_cond= NULL;
823+
821824
for (table= tables; table; table= table->next_local)
822825
{
823826
if (table->table && table->table->versioned())
@@ -869,7 +872,6 @@ int SELECT_LEX::vers_setup_conds(THD *thd, TABLE_LIST *tables, COND **where_expr
869872
continue;
870873
} // if (vers_conditions)
871874

872-
COND** dst_cond= where_expr;
873875
if (table->on_expr)
874876
{
875877
dst_cond= &table->on_expr;
@@ -888,7 +890,6 @@ int SELECT_LEX::vers_setup_conds(THD *thd, TABLE_LIST *tables, COND **where_expr
888890
newx Item_field(thd, &this->context, table->db, table->alias, fstart);
889891
Item *row_end=
890892
newx Item_field(thd, &this->context, table->db, table->alias, fend);
891-
Item *row_end2= row_end;
892893

893894
bool tmp_from_ib=
894895
table->table->s->table_category == TABLE_CATEGORY_TEMPORARY &&
@@ -929,7 +930,7 @@ int SELECT_LEX::vers_setup_conds(THD *thd, TABLE_LIST *tables, COND **where_expr
929930
else
930931
{
931932
curr= newx Item_int(thd, ULONGLONG_MAX);
932-
cond1= newx Item_func_eq(thd, row_end2, curr);
933+
cond1= newx Item_func_eq(thd, row_end, curr);
933934
}
934935
break;
935936
case FOR_SYSTEM_TIME_AS_OF:
@@ -968,7 +969,7 @@ int SELECT_LEX::vers_setup_conds(THD *thd, TABLE_LIST *tables, COND **where_expr
968969
{
969970
case FOR_SYSTEM_TIME_UNSPECIFIED:
970971
curr= newx Item_int(thd, ULONGLONG_MAX);
971-
cond1= newx Item_func_eq(thd, row_end2, curr);
972+
cond1= newx Item_func_eq(thd, row_end, curr);
972973
break;
973974
case FOR_SYSTEM_TIME_AS_OF:
974975
trx_id0= vers_conditions.unit_start == UNIT_TIMESTAMP ?
@@ -1003,23 +1004,33 @@ int SELECT_LEX::vers_setup_conds(THD *thd, TABLE_LIST *tables, COND **where_expr
10031004

10041005
if (cond1)
10051006
{
1006-
cond1= and_items(thd,
1007-
*dst_cond,
1007+
vers_cond= and_items(thd,
1008+
vers_cond,
10081009
and_items(thd,
10091010
cond2,
10101011
cond1));
1011-
1012-
if (on_stmt_arena.arena_replaced())
1013-
*dst_cond= cond1;
1014-
else
1015-
thd->change_item_tree(dst_cond, cond1);
1016-
1017-
this->where= *dst_cond;
1018-
this->where->top_level_item();
1012+
if (table->is_view_or_derived())
1013+
vers_cond= or_items(thd, vers_cond, newx Item_func_isnull(thd, row_end));
10191014
}
10201015
} // if (... table->table->versioned())
10211016
} // for (table= tables; ...)
10221017

1018+
if (vers_cond)
1019+
{
1020+
COND *all_cond= and_items(thd, *dst_cond, vers_cond);
1021+
bool from_where= dst_cond == where_expr;
1022+
if (on_stmt_arena.arena_replaced())
1023+
*dst_cond= all_cond;
1024+
else
1025+
thd->change_item_tree(dst_cond, all_cond);
1026+
1027+
if (from_where)
1028+
{
1029+
this->where= *dst_cond;
1030+
this->where->top_level_item();
1031+
}
1032+
}
1033+
10231034
DBUG_RETURN(0);
10241035
#undef newx
10251036
}

sql/sql_select.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2215,6 +2215,10 @@ inline Item * and_items(THD *thd, Item* cond, Item *item)
22152215
{
22162216
return (cond ? (new (thd->mem_root) Item_cond_and(thd, cond, item)) : item);
22172217
}
2218+
inline Item * or_items(THD *thd, Item* cond, Item *item)
2219+
{
2220+
return (cond ? (new (thd->mem_root) Item_cond_or(thd, cond, item)) : item);
2221+
}
22182222
bool choose_plan(JOIN *join, table_map join_tables);
22192223
void optimize_wo_join_buffering(JOIN *join, uint first_tab, uint last_tab,
22202224
table_map last_remaining_tables,

0 commit comments

Comments
 (0)