-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Derived: right join is broken #383
Comments
LEFT JOINexplain extended select * from (select t1.x as RJ1_x1, t1.y as y1, t2.x as x2, t2.y as y2 from t1 left join t2 on t1.x = t2.x) as t; RIGHT JOINexplain extended select * from (select t1.x as RJ1_x1, t1.y as y1, t2.x as x2, t2.y as y2 from t1 right join t2 on t1.x = t2.x) as t; 1.#0 st_select_lex::vers_setup_conds (this=0x7fff84013a80, thd=0x7fff84000d50, tables=0x7fff84014b38, where_expr=0x7fff84017dd0) at /home/midenok/src/mariadb/trunk/src/sql/sql_select.cc:1013
#1 0x0000000000790b2c in JOIN::prepare (this=0x7fff840179c8, tables_init=0x7fff84014b38, wild_num=0, conds_init=0x0, og_num=0, order_init=0x0, skip_order_by=false, group_init=0x0, having_init=0x0, proc_param_init=0x0, select_lex_arg=0x7fff84013a80, unit_arg=0x7fff84013ec0) at /home/midenok/src/mariadb/trunk/src/sql/sql_select.cc:1108
#2 0x0000000000863588 in st_select_lex_unit::prepare_join (this=0x7fff84013ec0, thd_arg=0x7fff84000d50, sl=0x7fff84013a80, tmp_result=0x7fff840178e0, additional_options=0, is_union_select=false) at /home/midenok/src/mariadb/trunk/src/sql/sql_union.cc:660
#3 0x000000000085e949 in st_select_lex_unit::prepare (this=0x7fff84013ec0, thd_arg=0x7fff84000d50, sel_result=0x7fff840178e0, additional_options=0) at /home/midenok/src/mariadb/trunk/src/sql/sql_union.cc:945
#4 0x00000000006f6d6c in mysql_derived_prepare (thd=0x7fff84000d50, lex=0x7fff840049f8, derived=0x7fff84016318) at /home/midenok/src/mariadb/trunk/src/sql/sql_derived.cc:868
#5 0x00000000006f9607 in mysql_handle_single_derived (lex=0x7fff840049f8, derived=0x7fff84016318, phases=2) at /home/midenok/src/mariadb/trunk/src/sql/sql_derived.cc:197
#6 0x00000000008963af in TABLE_LIST::handle_derived (this=0x7fff84016318, lex=0x7fff840049f8, phases=2) at /home/midenok/src/mariadb/trunk/src/sql/table.cc:7994
#7 0x000000000071f8a3 in st_select_lex::handle_derived (this=0x7fff84005220, lex=0x7fff840049f8, phases=2) at /home/midenok/src/mariadb/trunk/src/sql/sql_lex.cc:4033
#8 0x0000000000790912 in JOIN::prepare (this=0x7fff84017158, tables_init=0x7fff84016318, wild_num=1, conds_init=0x0, og_num=0, order_init=0x0, skip_order_by=false, group_init=0x0, having_init=0x0, proc_param_init=0x0, select_lex_arg=0x7fff84005220, unit_arg=0x7fff84004ac0) at /home/midenok/src/mariadb/trunk/src/sql/sql_select.cc:1074
#9 0x000000000078d0e9 in mysql_select (thd=0x7fff84000d50, tables=0x7fff84016318, wild_num=1, fields=..., conds=0x0, og_num=0, order=0x0, group=0x0, having=0x0, proc_param=0x0, select_options=2147748612, result=0x7fff84016a28, unit=0x7fff84004ac0, select_lex=0x7fff84005220) at /home/midenok/src/mariadb/trunk/src/sql/sql_select.cc:4173
#10 0x00000000007d73d3 in mysql_explain_union (thd=0x7fff84000d50, unit=0x7fff84004ac0, result=0x7fff84016a28) at /home/midenok/src/mariadb/trunk/src/sql/sql_select.cc:25816
#11 0x000000000074d85a in execute_sqlcom_select (thd=0x7fff84000d50, all_tables=0x7fff84016318) at /home/midenok/src/mariadb/trunk/src/sql/sql_parse.cc:6442
#12 0x0000000000742bf8 in mysql_execute_command (thd=0x7fff84000d50) at /home/midenok/src/mariadb/trunk/src/sql/sql_parse.cc:3746
#13 0x000000000073d81f in mysql_parse (thd=0x7fff84000d50, rawbuf=0x7fff840137e8 "explain extended select * from (select t1.x as RJ1_x1, t1.y as y1, t2.x as x2, t2.y as y2 from t1 left join t2 on t1.x = t2.x) as t", length=131, parser_state=0x7fffe524d650, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/trunk/src/sql/sql_parse.cc:7959 LEFT
RIGHT
where_expr is empty. 2.#0 st_select_lex::vers_setup_conds (this=0x7fff84013a80, thd=0x7fff84000d50, tables=0x7fff84014b38, where_expr=0x7fff84017dd0) at /home/midenok/src/mariadb/trunk/src/sql/sql_select.cc:1013
#1 0x0000000000790b2c in JOIN::prepare (this=0x7fff840179c8, tables_init=0x7fff84014b38, wild_num=0, conds_init=0x0, og_num=0, order_init=0x0, skip_order_by=false, group_init=0x0, having_init=0x0, proc_param_init=0x0, select_lex_arg=0x7fff84013a80, unit_arg=0x7fff84013ec0) at /home/midenok/src/mariadb/trunk/src/sql/sql_select.cc:1108
#2 0x0000000000863588 in st_select_lex_unit::prepare_join (this=0x7fff84013ec0, thd_arg=0x7fff84000d50, sl=0x7fff84013a80, tmp_result=0x7fff840178e0, additional_options=0, is_union_select=false) at /home/midenok/src/mariadb/trunk/src/sql/sql_union.cc:660
#3 0x000000000085e949 in st_select_lex_unit::prepare (this=0x7fff84013ec0, thd_arg=0x7fff84000d50, sel_result=0x7fff840178e0, additional_options=0) at /home/midenok/src/mariadb/trunk/src/sql/sql_union.cc:945
#4 0x00000000006f6d6c in mysql_derived_prepare (thd=0x7fff84000d50, lex=0x7fff840049f8, derived=0x7fff84016318) at /home/midenok/src/mariadb/trunk/src/sql/sql_derived.cc:868
#5 0x00000000006f9607 in mysql_handle_single_derived (lex=0x7fff840049f8, derived=0x7fff84016318, phases=2) at /home/midenok/src/mariadb/trunk/src/sql/sql_derived.cc:197
#6 0x00000000008963af in TABLE_LIST::handle_derived (this=0x7fff84016318, lex=0x7fff840049f8, phases=2) at /home/midenok/src/mariadb/trunk/src/sql/table.cc:7994
#7 0x000000000071f8a3 in st_select_lex::handle_derived (this=0x7fff84005220, lex=0x7fff840049f8, phases=2) at /home/midenok/src/mariadb/trunk/src/sql/sql_lex.cc:4033
#8 0x0000000000790912 in JOIN::prepare (this=0x7fff84017158, tables_init=0x7fff84016318, wild_num=1, conds_init=0x0, og_num=0, order_init=0x0, skip_order_by=false, group_init=0x0, having_init=0x0, proc_param_init=0x0, select_lex_arg=0x7fff84005220, unit_arg=0x7fff84004ac0) at /home/midenok/src/mariadb/trunk/src/sql/sql_select.cc:1074
#9 0x000000000078d0e9 in mysql_select (thd=0x7fff84000d50, tables=0x7fff84016318, wild_num=1, fields=..., conds=0x0, og_num=0, order=0x0, group=0x0, having=0x0, proc_param=0x0, select_options=2147748612, result=0x7fff84016a28, unit=0x7fff84004ac0, select_lex=0x7fff84005220) at /home/midenok/src/mariadb/trunk/src/sql/sql_select.cc:4173
#10 0x00000000007d73d3 in mysql_explain_union (thd=0x7fff84000d50, unit=0x7fff84004ac0, result=0x7fff84016a28) at /home/midenok/src/mariadb/trunk/src/sql/sql_select.cc:25816
#11 0x000000000074d85a in execute_sqlcom_select (thd=0x7fff84000d50, all_tables=0x7fff84016318) at /home/midenok/src/mariadb/trunk/src/sql/sql_parse.cc:6442
#12 0x0000000000742bf8 in mysql_execute_command (thd=0x7fff84000d50) at /home/midenok/src/mariadb/trunk/src/sql/sql_parse.cc:3746
#13 0x000000000073d81f in mysql_parse (thd=0x7fff84000d50, rawbuf=0x7fff840137e8 "explain extended select * from (select t1.x as RJ1_x1, t1.y as y1, t2.x as x2, t2.y as y2 from t1 left join t2 on t1.x = t2.x) as t", length=131, parser_state=0x7fffe524d650, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/trunk/src/sql/sql_parse.cc:7959 LEFT
where_expr is NOT empty. RIGHT
3.#0 st_select_lex::vers_setup_conds (this=0x7fff84005220, thd=0x7fff84000d50, tables=0x7fff84016318, where_expr=0x7fff84017560) at /home/midenok/src/mariadb/trunk/src/sql/sql_select.cc:1013
#1 0x0000000000790b2c in JOIN::prepare (this=0x7fff84017158, tables_init=0x7fff84016318, wild_num=1, conds_init=0x0, og_num=0, order_init=0x0, skip_order_by=false, group_init=0x0, having_init=0x0, proc_param_init=0x0, select_lex_arg=0x7fff84005220, unit_arg=0x7fff84004ac0) at /home/midenok/src/mariadb/trunk/src/sql/sql_select.cc:1108
#2 0x000000000078d0e9 in mysql_select (thd=0x7fff84000d50, tables=0x7fff84016318, wild_num=1, fields=..., conds=0x0, og_num=0, order=0x0, group=0x0, having=0x0, proc_param=0x0, select_options=2147748612, result=0x7fff84016a28, unit=0x7fff84004ac0, select_lex=0x7fff84005220) at /home/midenok/src/mariadb/trunk/src/sql/sql_select.cc:4173
#3 0x00000000007d73d3 in mysql_explain_union (thd=0x7fff84000d50, unit=0x7fff84004ac0, result=0x7fff84016a28) at /home/midenok/src/mariadb/trunk/src/sql/sql_select.cc:25816
#4 0x000000000074d85a in execute_sqlcom_select (thd=0x7fff84000d50, all_tables=0x7fff84016318) at /home/midenok/src/mariadb/trunk/src/sql/sql_parse.cc:6442
#5 0x0000000000742bf8 in mysql_execute_command (thd=0x7fff84000d50) at /home/midenok/src/mariadb/trunk/src/sql/sql_parse.cc:3746
#6 0x000000000073d81f in mysql_parse (thd=0x7fff84000d50, rawbuf=0x7fff840137e8 "explain extended select * from (select t1.x as RJ1_x1, t1.y as y1, t2.x as x2, t2.y as y2 from t1 left join t2 on t1.x = t2.x) as t", length=131, parser_state=0x7fffe524d650, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/trunk/src/sql/sql_parse.cc:7959 LEFT
RIGHT
|
JOIN printed#0 print_table_array (thd=0x7fff84000d50, eliminated_tables=0, str=0x7fffe5249a60, table=0x7fff840be2a8, end=0x7fff840be2b8, query_type=QT_EXPLAIN_EXTENDED) at /home/midenok/src/mariadb/trunk/src/sql/sql_select.cc:25863
#1 0x00000000007d8379 in print_join (thd=0x7fff84000d50, eliminated_tables=0, str=0x7fffe5249a60, tables=0x7fff840053b8, query_type=QT_EXPLAIN_EXTENDED) at /home/midenok/src/mariadb/trunk/src/sql/sql_select.cc:25994
#2 0x00000000007d88e9 in st_select_lex::print (this=0x7fff84005220, thd=0x7fff84000d50, str=0x7fffe5249a60, query_type=QT_EXPLAIN_EXTENDED) at /home/midenok/src/mariadb/trunk/src/sql/sql_select.cc:26280
#3 0x000000000071c5d7 in st_select_lex_unit::print (this=0x7fff84004ac0, str=0x7fffe5249a60, query_type=QT_EXPLAIN_EXTENDED) at /home/midenok/src/mariadb/trunk/src/sql/sql_lex.cc:2855
#4 0x000000000074d976 in execute_sqlcom_select (thd=0x7fff84000d50, all_tables=0x7fff84013e50) at /home/midenok/src/mariadb/trunk/src/sql/sql_parse.cc:6469
#5 0x0000000000742bf8 in mysql_execute_command (thd=0x7fff84000d50) at /home/midenok/src/mariadb/trunk/src/sql/sql_parse.cc:3746
#6 0x000000000073d81f in mysql_parse (thd=0x7fff84000d50, rawbuf=0x7fff840137e8 "explain extended select t1.x as RJ1_x1, t1.y as y1, t2.x as x2, t2.y as y2 from t1 right join t2 on t1.x = t2.x", length=111, parser_state=0x7fffe524d650, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/trunk/src/sql/sql_parse.cc:7959 25860 if (curr->outer_join & (JOIN_TYPE_LEFT|JOIN_TYPE_RIGHT))
25861 {
25862 /* MySQL converts right to left joins */
25863 str->append(STRING_WITH_LEN(" left join "));
25864 } #define JOIN_TYPE_LEFT 1U
#define JOIN_TYPE_RIGHT 2U
#define JOIN_TYPE_OUTER 4U /* Marker that this is an outer join */ |
|
Good: right join, not derived
Good: left join, derived or not derived
Bad: 4376 is not executed
s->on_expr_ref= &tables->on_expr; Cause
|
Bad:
|
Bad:
|
Bad:
|
LJ, no derived
RJ, no derived
LJ, derived
call 1
call 2
RJ, derivedcall 1
call 2
In call 2 |
Bad:
|
|
Bad:
|
Good:
|
Fields are translated via void Field_iterator_view::set(TABLE_LIST *table)
{
DBUG_ASSERT(table->field_translation);
view= table;
ptr= table->field_translation;
array_end= table->field_translation_end;
} struct Field_translator
{
Item *item;
LEX_CSTRING name;
};
|
Field translation created#0 TABLE_LIST::create_field_translation (this=0x7fff7c0162f8, thd=0x7fff7c000d50) at /home/midenok/src/mariadb/trunk/src/sql/table.cc:4728
#1 0x00000000008966e2 in TABLE_LIST::init_derived (this=0x7fff7c0162f8, thd=0x7fff7c000d50, init_view=false) at /home/midenok/src/mariadb/trunk/src/sql/table.cc:8132
#2 0x00000000006f6e60 in mysql_derived_prepare (thd=0x7fff7c000d50, lex=0x7fff7c0049f8, derived=0x7fff7c0162f8) at /home/midenok/src/mariadb/trunk/src/sql/sql_derived.cc:882
#3 0x00000000006f9607 in mysql_handle_single_derived (lex=0x7fff7c0049f8, derived=0x7fff7c0162f8, phases=2) at /home/midenok/src/mariadb/trunk/src/sql/sql_derived.cc:197
#4 0x00000000008963af in TABLE_LIST::handle_derived (this=0x7fff7c0162f8, lex=0x7fff7c0049f8, phases=2) at /home/midenok/src/mariadb/trunk/src/sql/table.cc:7994
#5 0x000000000071f8a3 in st_select_lex::handle_derived (this=0x7fff7c005220, lex=0x7fff7c0049f8, phases=2) at /home/midenok/src/mariadb/trunk/src/sql/sql_lex.cc:4033
#6 0x0000000000790912 in JOIN::prepare (this=0x7fff7c016a28, tables_init=0x7fff7c0162f8, wild_num=1, conds_init=0x0, og_num=0, order_init=0x0, skip_order_by=false, group_init=0x0, having_init=0x0, proc_param_init=0x0, select_lex_arg=0x7fff7c005220, unit_arg=0x7fff7c004ac0) at /home/midenok/src/mariadb/trunk/src/sql/sql_select.cc:1074
#7 0x000000000078d0e9 in mysql_select (thd=0x7fff7c000d50, tables=0x7fff7c0162f8, wild_num=1, fields=..., conds=0x0, og_num=0, order=0x0, group=0x0, having=0x0, proc_param=0x0, select_options=2147748608, result=0x7fff7c016a08, unit=0x7fff7c004ac0, select_lex=0x7fff7c005220) at /home/midenok/src/mariadb/trunk/src/sql/sql_select.cc:4173
#8 0x000000000078ca63 in handle_select (thd=0x7fff7c000d50, lex=0x7fff7c0049f8, result=0x7fff7c016a08, setup_tables_done_option=0) at /home/midenok/src/mariadb/trunk/src/sql/sql_select.cc:368
#9 0x000000000074dd7f in execute_sqlcom_select (thd=0x7fff7c000d50, all_tables=0x7fff7c0162f8) at /home/midenok/src/mariadb/trunk/src/sql/sql_parse.cc:6503
#10 0x0000000000742bf8 in mysql_execute_command (thd=0x7fff7c000d50) at /home/midenok/src/mariadb/trunk/src/sql/sql_parse.cc:3746
#11 0x000000000073d81f in mysql_parse (thd=0x7fff7c000d50, rawbuf=0x7fff7c0137e8 "select * from (select t1.x as RJ1_x1, t1.y as y1, t2.x as x2, t2.y as y2 from t1 left join t2 on t1.x = t2.x) as t", length=114, parser_state=0x7fffe524d650, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/trunk/src/sql/sql_parse.cc:7959 |
System field is added to
|
Trivial sample where z represents sys_trx_end: select * from (select p1.x as RJ1_x1, p1.y as y1, p2.x as x2, p2.y as y2, p1.z from p1 right join p2 on p1.x = p2.x) as t where z = 1;
+--------+------+------+------+------+
| RJ1_x1 | y1 | x2 | y2 | z |
+--------+------+------+------+------+
| 1 | 1 | 1 | 2 | 1 |
| 1 | 2 | 1 | 2 | 1 |
| 1 | 3 | 1 | 2 | 1 |
+--------+------+------+------+------+ Correct result will be when we add select * from (select p1.x as RJ1_x1, p1.y as y1, p2.x as x2, p2.y as y2, p1.z from p1 right join p2 on p1.x = p2.x) as t where z = 1 or z is null;
+--------+------+------+------+------+
| RJ1_x1 | y1 | x2 | y2 | z |
+--------+------+------+------+------+
| 1 | 1 | 1 | 2 | 1 |
| 1 | 2 | 1 | 2 | 1 |
| 1 | 3 | 1 | 2 | 1 |
| NULL | NULL | 2 | 1 | NULL |
| NULL | NULL | 3 | 1 | NULL |
+--------+------+------+------+------+ Fix (more general)In |
breaks.gdb
|
Deprecates #160. |
Reproduce
Result
Expected
Notes
Trying with
one can see that right join is converted to the inner join:
This does not happen without derived:
Or with left join
The text was updated successfully, but these errors were encountered: