Skip to content
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

Non-versioned fields should display NULL value for historical queries #60

Closed
midenok opened this issue Oct 26, 2016 · 19 comments
Closed
Assignees
Labels

Comments

@midenok
Copy link

midenok commented Oct 26, 2016

As continuation of discussion in #51 this is the least we can do to maintain value correctness.

@midenok midenok added the task label Oct 26, 2016
@midenok midenok added this to the 0.4 AS OF TRX_ID (directly specify transaction id in AS OF) milestone Nov 1, 2016
@kevgs
Copy link

kevgs commented Nov 2, 2016

Test case which doesn't work.

create table t(
  a int,
  b int without system versioning
) engine innodb with system versioning;

insert into t values(1, 2);
select * from t;
select * from t for system_time as of timestamp now(6);

drop table t;

Second query should return NULL instead of 2.

@midenok
Copy link
Author

midenok commented Nov 2, 2016

Probably, it is better to detect whether query is really historical. Do you have any suggestions how to do it?

@kevgs
Copy link

kevgs commented Nov 2, 2016

If query has "for system_time" we need to replace non-versioned fields with NULL literals as soon as possible. And probably issue a warning to user.

@midenok
Copy link
Author

midenok commented Nov 3, 2016

This is for sure. I mean by really historical that timestamp now() is not historical since it selects latest data. The code should detect if the query selects not latest data.

@kevgs
Copy link

kevgs commented Nov 3, 2016

create table t(
  a int,
  b int without system versioning
) with system versioning;

insert into t values(1, 2);
insert into t values(1, 3);
select count(*) from t group by b for system_time as of now(6);

Should warn and return one row with count(*) = 2 because all b becomes NULL in this case.

@kevgs
Copy link

kevgs commented Nov 3, 2016

create table t(
  a int,
  b int without system versioning
) with system versioning;

insert into t values(1, 2);
insert into t values(3, 4);
select count(*) from t for system_time as of now(6) order by b asc;
select count(*) from t for system_time as of now(6) order by b desc;

Order should be the same as if we would sort by NULL literal. Also warn.

@midenok
Copy link
Author

midenok commented Nov 3, 2016

Can you just create special field item Item_null_field which will return NULL in any case? In this case you don't need to delve in all countless cases of data variations.

@midenok
Copy link
Author

midenok commented Nov 3, 2016

This is minor task and with risk of depreciation, so it should be fixed in a quick way.

@kevgs
Copy link

kevgs commented Nov 3, 2016

Item_null already does it. But we need to walk through all Item_field in query and replace it with Item_null if they're not versioned.

Item_fields are created by parser but they're actually initialized in JOIN::prepare(). Specifically, we're interested in Item_field->field which we need to check.

Another option is to replace nothing but check for unversioned fields and warn user that query results may be incorrect. What do you think?

@midenok
Copy link
Author

midenok commented Nov 4, 2016

Where conds_init, order_init, group_init, etc. are built? Pls, stack traces. Do they have same items with all_fields or different?

@kevgs
Copy link

kevgs commented Nov 4, 2016

#0  JOIN::prepare (this=0x55555cfb8548, tables_init=0x0, 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=0x55555cfa8278, unit_arg=0x55555cfa7b38) at /home/kevg/Desktop/mariadb_10.2/sql/sql_select.cc:918
#1  0x0000555555b01b74 in mysql_select (thd=0x55555cfa4070, tables=0x0, wild_num=0, fields=..., conds=0x0, og_num=0, order=0x0, group=0x0, having=0x0, proc_param=0x0,
    select_options=2147748608, result=0x55555cfb8528, unit=0x55555cfa7b38, select_lex=0x55555cfa8278) at /home/kevg/Desktop/mariadb_10.2/sql/sql_select.cc:3797
#2  0x0000555555af5aa4 in handle_select (thd=0x55555cfa4070, lex=0x55555cfa7a70, result=0x55555cfb8528, setup_tables_done_option=0)
    at /home/kevg/Desktop/mariadb_10.2/sql/sql_select.cc:362
#3  0x0000555555ac35f2 in execute_sqlcom_select (thd=0x55555cfa4070, all_tables=0x0) at /home/kevg/Desktop/mariadb_10.2/sql/sql_parse.cc:6351
#4  0x0000555555ab8d35 in mysql_execute_command (thd=0x55555cfa4070) at /home/kevg/Desktop/mariadb_10.2/sql/sql_parse.cc:3376
#5  0x0000555555ac6f8b in mysql_parse (thd=0x55555cfa4070, rawbuf=0x55555cfb8088 "SELECT 'No such row' = 'No such row'", length=36, parser_state=0x7ffff7f28dc0,
    is_com_multi=false, is_next_command=false) at /home/kevg/Desktop/mariadb_10.2/sql/sql_parse.cc:7794
#6  0x0000555555ab4995 in dispatch_command (command=COM_QUERY, thd=0x55555cfa4070, packet=0x55555c3bc071 "SELECT 'No such row' = 'No such row'", packet_length=36,
    is_com_multi=false, is_next_command=false) at /home/kevg/Desktop/mariadb_10.2/sql/sql_parse.cc:1805

conds_init, order_init, group_init are basically fields of SELECT_LEX https://github.com/natsys/mariadb_10.2/blob/natsys/trunk/sql/sql_select.cc#L362

Fields of SELECT_LEX are filed with parser here for example.

Those struct from parser are just names. We need to find column names in table and it happens here

#0  setup_fields (thd=0x55555cfa4070, ref_pointer_array=..., fields=..., mark_used_columns=MARK_COLUMNS_READ, sum_func_list=0x55555cfb8858, allow_sum_func=true)
    at /home/kevg/Desktop/mariadb_10.2/sql/sql_base.cc:6911
#1  0x0000555555af7eee in JOIN::prepare (this=0x55555cfb8548, tables_init=0x0, 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=0x55555cfa8278, unit_arg=0x55555cfa7b38) at /home/kevg/Desktop/mariadb_10.2/sql/sql_select.cc:1029
#2  0x0000555555b01b74 in mysql_select (thd=0x55555cfa4070, tables=0x0, wild_num=0, fields=..., conds=0x0, og_num=0, order=0x0, group=0x0, having=0x0, proc_param=0x0, 
    select_options=2147748608, result=0x55555cfb8528, unit=0x55555cfa7b38, select_lex=0x55555cfa8278) at /home/kevg/Desktop/mariadb_10.2/sql/sql_select.cc:3797
#3  0x0000555555af5aa4 in handle_select (thd=0x55555cfa4070, lex=0x55555cfa7a70, result=0x55555cfb8528, setup_tables_done_option=0)
    at /home/kevg/Desktop/mariadb_10.2/sql/sql_select.cc:362
#4  0x0000555555ac35f2 in execute_sqlcom_select (thd=0x55555cfa4070, all_tables=0x0) at /home/kevg/Desktop/mariadb_10.2/sql/sql_parse.cc:6351
#5  0x0000555555ab8d35 in mysql_execute_command (thd=0x55555cfa4070) at /home/kevg/Desktop/mariadb_10.2/sql/sql_parse.cc:3376
#6  0x0000555555ac6f8b in mysql_parse (thd=0x55555cfa4070, rawbuf=0x55555cfb8088 "SELECT 'No such row' = 'No such row'", length=36, parser_state=0x7ffff7f28dc0, 
    is_com_multi=false, is_next_command=false) at /home/kevg/Desktop/mariadb_10.2/sql/sql_parse.cc:7794
#0  setup_group (thd=0x55555cfa4070, ref_pointer_array=..., tables=0x0, fields=..., all_fields=..., order=0x0, hidden_group_fields=0x55555cfb881f, search_in_all_fields=true)
    at /home/kevg/Desktop/mariadb_10.2/sql/sql_select.cc:22394
#1  0x0000555555af6619 in setup_without_group (thd=0x55555cfa4070, ref_pointer_array=..., tables=0x0, leaves=..., fields=..., all_fields=..., conds=0x55555cfb8940, order=0x0,
    group=0x0, win_specs=..., win_funcs=..., hidden_group_fields=0x55555cfb881f, reserved=0x55555cfa852c) at /home/kevg/Desktop/mariadb_10.2/sql/sql_select.cc:660
#2  0x0000555555af8079 in JOIN::prepare (this=0x55555cfb8548, tables_init=0x0, 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=0x55555cfa8278, unit_arg=0x55555cfa7b38) at /home/kevg/Desktop/mariadb_10.2/sql/sql_select.cc:1041

After all this stuff initialized with Field references it becomes possible to check where their corresponding fields are optimized. And it's time to replace them with Item_null.

@kevgs
Copy link

kevgs commented Nov 4, 2016

Checking whether Item_field::context initialized in a different places:

  • select list: true
  • group list: true
  • order list: true
  • having: false
  • where list: true

@kevgs
Copy link

kevgs commented Nov 5, 2016

select a, b from t for system_time as of timestamp now(6)

Watching b.

#0  Field::make_field (this=0x55555d06eac8, field=0x7ffff7f27340) at /home/kevg/Desktop/mariadb_10.2/sql/field.cc:1924
#1  0x0000555555cee057 in Field_num::make_field (this=0x55555d06eac8, field=0x7ffff7f27340) at /home/kevg/Desktop/mariadb_10.2/sql/field.cc:2115
#2  0x0000555555d403c4 in Item_field::make_field (this=0x55555cfac288, thd=0x55555cf98070, tmp_field=0x7ffff7f27340) at /home/kevg/Desktop/mariadb_10.2/sql/item.cc:5941
#3  0x00005555559f3abc in Protocol::send_result_set_metadata (this=0x55555cf98608, list=0x55555cf9c3a8, flags=5) at /home/kevg/Desktop/mariadb_10.2/sql/protocol.cc:821
#4  0x0000555555a71e35 in select_send::send_result_set_metadata (this=0x55555cfacc88, list=..., flags=5) at /home/kevg/Desktop/mariadb_10.2/sql/sql_class.cc:2858
#5  0x0000555555afe540 in JOIN::exec_inner (this=0x55555cfacca8) at /home/kevg/Desktop/mariadb_10.2/sql/sql_select.cc:3620
#6  0x0000555555afdaa6 in JOIN::exec (this=0x55555cfacca8) at /home/kevg/Desktop/mariadb_10.2/sql/sql_select.cc:3432
#7  0x0000555555afebf4 in mysql_select (thd=0x55555cf98070, tables=0x55555cfac3c8, wild_num=0, fields=..., conds=0x0, og_num=0, order=0x0, group=0x0, having=0x0, 
    proc_param=0x0, select_options=2147748608, result=0x55555cfacc88, unit=0x55555cf9bb38, select_lex=0x55555cf9c278) at /home/kevg/Desktop/mariadb_10.2/sql/sql_select.cc:3817
#8  0x0000555555af2b54 in handle_select (thd=0x55555cf98070, lex=0x55555cf9ba70, result=0x55555cfacc88, setup_tables_done_option=0)
    at /home/kevg/Desktop/mariadb_10.2/sql/sql_select.cc:374
#9  0x0000555555ac07cd in execute_sqlcom_select (thd=0x55555cf98070, all_tables=0x55555cfac3c8) at /home/kevg/Desktop/mariadb_10.2/sql/sql_parse.cc:6351
#10 0x0000555555ab5f6e in mysql_execute_command (thd=0x55555cf98070) at /home/kevg/Desktop/mariadb_10.2/sql/sql_parse.cc:3376
#11 0x0000555555ac412b in mysql_parse (thd=0x55555cf98070, rawbuf=0x55555cfac088 "select a, b from t for system_time as of timestamp now(6)", length=57, 
    parser_state=0x7ffff7f28dc0, is_com_multi=false, is_next_command=false) at /home/kevg/Desktop/mariadb_10.2/sql/sql_parse.cc:7794
#0  Field::is_null (this=0x55555d06eac8, row_offset=0) at /home/kevg/Desktop/mariadb_10.2/sql/field.h:1097
#1  0x00005555559f534b in Protocol_text::store (this=0x55555cf98608, field=0x55555d06eac8) at /home/kevg/Desktop/mariadb_10.2/sql/protocol.cc:1229
#2  0x0000555555d42d47 in Item_field::send (this=0x55555cfac288, protocol=0x55555cf98608, buffer=0x7ffff7f274b0) at /home/kevg/Desktop/mariadb_10.2/sql/item.cc:6769
#3  0x00005555559f452d in Protocol::send_result_set_row (this=0x55555cf98608, row_items=0x55555cf9c3a8) at /home/kevg/Desktop/mariadb_10.2/sql/protocol.cc:978
#4  0x0000555555a7200e in select_send::send_data (this=0x55555cfacc88, items=...) at /home/kevg/Desktop/mariadb_10.2/sql/sql_class.cc:2919
#5  0x0000555555b265c7 in end_send (join=0x55555cfacca8, join_tab=0x55555cfaef50, end_of_records=false) at /home/kevg/Desktop/mariadb_10.2/sql/sql_select.cc:19759
#6  0x0000555555b240e0 in evaluate_join_record (join=0x55555cfacca8, join_tab=0x55555cfaeba0, error=0) at /home/kevg/Desktop/mariadb_10.2/sql/sql_select.cc:18812
#7  0x0000555555b239cb in sub_select (join=0x55555cfacca8, join_tab=0x55555cfaeba0, end_of_records=false) at /home/kevg/Desktop/mariadb_10.2/sql/sql_select.cc:18587
#8  0x0000555555b22f88 in do_select (join=0x55555cfacca8, procedure=0x0) at /home/kevg/Desktop/mariadb_10.2/sql/sql_select.cc:18131
#0  Field_long::val_str (this=0x55555d06eac8, val_buffer=0x7ffff7f27110, val_ptr=0x7ffff7f27110) at /home/kevg/Desktop/mariadb_10.2/sql/field.cc:4216
#1  0x00005555559f7037 in Field::val_str (this=0x55555d06eac8, str=0x7ffff7f27110) at /home/kevg/Desktop/mariadb_10.2/sql/field.h:827
#2  0x00005555559f5435 in Protocol_text::store (this=0x55555cf98608, field=0x55555d06eac8) at /home/kevg/Desktop/mariadb_10.2/sql/protocol.cc:1244
#3  0x0000555555d42d47 in Item_field::send (this=0x55555cfac288, protocol=0x55555cf98608, buffer=0x7ffff7f274b0) at /home/kevg/Desktop/mariadb_10.2/sql/item.cc:6769
#4  0x00005555559f452d in Protocol::send_result_set_row (this=0x55555cf98608, row_items=0x55555cf9c3a8) at /home/kevg/Desktop/mariadb_10.2/sql/protocol.cc:978
#5  0x0000555555a7200e in select_send::send_data (this=0x55555cfacc88, items=...) at /home/kevg/Desktop/mariadb_10.2/sql/sql_class.cc:2919
#6  0x0000555555b265c7 in end_send (join=0x55555cfacca8, join_tab=0x55555cfaef50, end_of_records=false) at /home/kevg/Desktop/mariadb_10.2/sql/sql_select.cc:19759
#7  0x0000555555b240e0 in evaluate_join_record (join=0x55555cfacca8, join_tab=0x55555cfaeba0, error=0) at /home/kevg/Desktop/mariadb_10.2/sql/sql_select.cc:18812
#8  0x0000555555b239cb in sub_select (join=0x55555cfacca8, join_tab=0x55555cfaeba0, end_of_records=false) at /home/kevg/Desktop/mariadb_10.2/sql/sql_select.cc:18587
#9  0x0000555555b22f88 in do_select (join=0x55555cfacca8, procedure=0x0) at /home/kevg/Desktop/mariadb_10.2/sql/sql_select.cc:18131

@kevgs
Copy link

kevgs commented Nov 5, 2016

select count(*) from t group by b for system_time as of timestamp now(6)

Watching b.

#0  Field::is_null (this=0x55555d06eac8, row_offset=0) at /home/kevg/Desktop/mariadb_10.2/sql/field.h:1097
#1  0x0000555555d40600 in Item_field::save_org_in_field (this=0x55555cfac9e8, to=0x55555d06eff0, fast_field_copier_func=0x555555d1441d <field_conv_memcpy(Field*, Field*)>)
    at /home/kevg/Desktop/mariadb_10.2/sql/item.cc:6021
#2  0x0000555555b27483 in end_update (join=0x55555cfacdf0, join_tab=0x55555cfaf0f0, end_of_records=false) at /home/kevg/Desktop/mariadb_10.2/sql/sql_select.cc:20066
#3  0x0000555555b381eb in AGGR_OP::put_record (this=0x55555cfafeb0, end_of_records=false) at /home/kevg/Desktop/mariadb_10.2/sql/sql_select.cc:26444
#4  0x0000555555b3cc25 in AGGR_OP::put_record (this=0x55555cfafeb0) at /home/kevg/Desktop/mariadb_10.2/sql/sql_select.h:971
#5  0x0000555555b234ba in sub_select_postjoin_aggr (join=0x55555cfacdf0, join_tab=0x55555cfaf0f0, end_of_records=false)
    at /home/kevg/Desktop/mariadb_10.2/sql/sql_select.cc:18308
#6  0x0000555555b240e0 in evaluate_join_record (join=0x55555cfacdf0, join_tab=0x55555cfaed40, error=0) at /home/kevg/Desktop/mariadb_10.2/sql/sql_select.cc:18812
#7  0x0000555555b239cb in sub_select (join=0x55555cfacdf0, join_tab=0x55555cfaed40, end_of_records=false) at /home/kevg/Desktop/mariadb_10.2/sql/sql_select.cc:18587
#8  0x0000555555b22f88 in do_select (join=0x55555cfacdf0, procedure=0x0) at /home/kevg/Desktop/mariadb_10.2/sql/sql_select.cc:18131
#9  0x0000555555afe55a in JOIN::exec_inner (this=0x55555cfacdf0) at /home/kevg/Desktop/mariadb_10.2/sql/sql_select.cc:3621
#10 0x0000555555afdaa6 in JOIN::exec (this=0x55555cfacdf0) at /home/kevg/Desktop/mariadb_10.2/sql/sql_select.cc:3432
#11 0x0000555555afebf4 in mysql_select (thd=0x55555cf98070, tables=0x55555cfac3b8, wild_num=0, fields=..., conds=0x0, og_num=1, order=0x0, group=0x55555cfacae0, having=0x0, 
    proc_param=0x0, select_options=2147748608, result=0x55555cfacdd0, unit=0x55555cf9bb38, select_lex=0x55555cf9c278) at /home/kevg/Desktop/mariadb_10.2/sql/sql_select.cc:3817

@kevgs
Copy link

kevgs commented Nov 5, 2016

select * from t group by a having a=2 for system_time as of timestamp now(6)

Watching a.

#0  Field::is_null (this=0x55555d06ee08, row_offset=0) at /home/kevg/Desktop/mariadb_10.2/sql/field.h:1097
#1  0x0000555555d3771c in Item_field::val_int_result (this=0x55555cfb04b0) at /home/kevg/Desktop/mariadb_10.2/sql/item.cc:2763
#2  0x0000555555d452c0 in Item_ref::val_int (this=0x55555cfaca60) at /home/kevg/Desktop/mariadb_10.2/sql/item.cc:7596
#3  0x0000555555d58fd7 in Arg_comparator::compare_int_signed (this=0x55555cfacca8) at /home/kevg/Desktop/mariadb_10.2/sql/item_cmpfunc.cc:967
#4  0x0000555555d6ba7e in Arg_comparator::compare (this=0x55555cfacca8) at /home/kevg/Desktop/mariadb_10.2/sql/item_cmpfunc.h:88
#5  0x0000555555d5b25f in Item_func_eq::val_int (this=0x55555cfacbe8) at /home/kevg/Desktop/mariadb_10.2/sql/item_cmpfunc.cc:1742
#6  0x0000555555ba31c2 in SQL_SELECT::skip_record (this=0x55555cfb06c0, thd=0x55555cf98070) at /home/kevg/Desktop/mariadb_10.2/sql/opt_range.h:1633
#7  0x0000555555d1713a in find_all_keys (thd=0x55555cf98070, param=0x7ffff7f27450, select=0x55555cfb06c0, fs_info=0x555557e1a600, buffpek_pointers=0x7ffff7f27660, 
    tempfile=0x7ffff7f274e0, pq=0x0, found_rows=0x555557e1a7f0) at /home/kevg/Desktop/mariadb_10.2/sql/filesort.cc:843
#8  0x0000555555d154fb in filesort (thd=0x55555cf98070, table=0x55555c0f5088, filesort=0x55555cfb08a0, tracker=0x55555cfb0b78, join=0x55555cfad010, first_table_bit=1)
    at /home/kevg/Desktop/mariadb_10.2/sql/filesort.cc:284
#9  0x0000555555b2b2be in create_sort_index (thd=0x55555cf98070, join=0x55555cfad010, tab=0x55555cfaf7d0, fsort=0x55555cfb08a0)
    at /home/kevg/Desktop/mariadb_10.2/sql/sql_select.cc:21715
#10 0x0000555555b25d2c in st_join_table::sort_table (this=0x55555cfaf7d0) at /home/kevg/Desktop/mariadb_10.2/sql/sql_select.cc:19552
#11 0x0000555555b25a24 in join_init_read_record (tab=0x55555cfaf7d0) at /home/kevg/Desktop/mariadb_10.2/sql/sql_select.cc:19499
#12 0x0000555555b383d1 in AGGR_OP::end_send (this=0x55555cfb04a0) at /home/kevg/Desktop/mariadb_10.2/sql/sql_select.cc:26501
#13 0x0000555555b2347f in sub_select_postjoin_aggr (join=0x55555cfad010, join_tab=0x55555cfaf7d0, end_of_records=true)
    at /home/kevg/Desktop/mariadb_10.2/sql/sql_select.cc:18302
#14 0x0000555555b237a3 in sub_select (join=0x55555cfad010, join_tab=0x55555cfaf420, end_of_records=true) at /home/kevg/Desktop/mariadb_10.2/sql/sql_select.cc:18538
#15 0x0000555555b22fdd in do_select (join=0x55555cfad010, procedure=0x0) at /home/kevg/Desktop/mariadb_10.2/sql/sql_select.cc:18133
#16 0x0000555555afe55a in JOIN::exec_inner (this=0x55555cfad010) at /home/kevg/Desktop/mariadb_10.2/sql/sql_select.cc:3621
#17 0x0000555555afdaa6 in JOIN::exec (this=0x55555cfad010) at /home/kevg/Desktop/mariadb_10.2/sql/sql_select.cc:3432
#18 0x0000555555afebf4 in mysql_select (thd=0x55555cf98070, tables=0x55555cfac2d8, wild_num=1, fields=..., conds=0x0, og_num=1, order=0x0, group=0x55555cfaca00, 
    having=0x55555cfacbe8, proc_param=0x0, select_options=2147748608, result=0x55555cfacff0, unit=0x55555cf9bb38, select_lex=0x55555cf9c278)
    at /home/kevg/Desktop/mariadb_10.2/sql/sql_select.cc:3817

@midenok
Copy link
Author

midenok commented Nov 7, 2016

select x from t1;
#0  Item_field::set_field (this=0x7f6fe7860128, field_par=0x7f6fe78202a8) at /home/midenok/src/mariadb/midenok/src/sql/item.cc:2522
#1  0x0000000000966a2d in Item_field::fix_fields (this=0x7f6fe7860128, thd=0x7f6fe7816070, reference=0x7f6fe7860228) at /home/midenok/src/mariadb/midenok/src/sql/item.cc:5293
#2  0x0000000000664aa9 in setup_fields (thd=0x7f6fe7816070, ref_pointer_array=..., fields=..., mark_used_columns=MARK_COLUMNS_READ, sum_func_list=0x7f6fe7860c88, allow_sum_func=0x1) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.cc:6964
#3  0x000000000070c58d in JOIN::prepare (this=0x7f6fe7860978, tables_init=0x7f6fe7860268, wild_num=0x0, conds_init=0x0, og_num=0x0, order_init=0x0, skip_order_by=0x0, group_init=0x0, having_init=0x0, proc_param_init=0x0, select_lex_arg=0x7f6fe781a278, unit_arg=0x7f6fe7819b38) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:989
#4  0x00000000007164d7 in mysql_select (thd=0x7f6fe7816070, tables=0x7f6fe7860268, wild_num=0x0, fields=..., conds=0x0, og_num=0x0, order=0x0, group=0x0, having=0x0, proc_param=0x0, select_options=0x80040b00, result=0x7f6fe7860958, unit=0x7f6fe7819b38, select_lex=0x7f6fe781a278) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:3735
#5  0x000000000070a1eb in handle_select (thd=0x7f6fe7816070, lex=0x7f6fe7819a70, result=0x7f6fe7860958, setup_tables_done_option=0x0) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:374
#6  0x00000000006d5f9b in execute_sqlcom_select (thd=0x7f6fe7816070, all_tables=0x7f6fe7860268) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:6351
#7  0x00000000006cb750 in mysql_execute_command (thd=0x7f6fe7816070) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:3376
#8  0x00000000006d9aa9 in mysql_parse (thd=0x7f6fe7816070, rawbuf=0x7f6fe7860088 "select x from t2", length=0x10, parser_state=0x7f7028682f80, is_com_multi=0x0, is_next_command=0x0) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:7794
#0  Field::val_str (this=0x7f6fe78202a8, str=0x7f7028680df0) at /home/midenok/src/mariadb/midenok/src/sql/field.h:827
#1  0x00000000006042ad in Protocol_text::store (this=0x7f6fe7816608, field=0x7f6fe78202a8) at /home/midenok/src/mariadb/midenok/src/sql/protocol.cc:1244
#2  0x000000000096b14b in Item_field::send (this=0x7f6fe7860128, protocol=0x7f6fe7816608, buffer=0x7f7028681190) at /home/midenok/src/mariadb/midenok/src/sql/item.cc:6750
#3  0x000000000060337b in Protocol::send_result_set_row (this=0x7f6fe7816608, row_items=0x7f6fe781a3a8) at /home/midenok/src/mariadb/midenok/src/sql/protocol.cc:978
#4  0x0000000000685423 in select_send::send_data (this=0x7f6fe7860958, items=...) at /home/midenok/src/mariadb/midenok/src/sql/sql_class.cc:2919
#5  0x000000000073f2c6 in end_send (join=0x7f6fe7860978, join_tab=0x7f6fe7861cf0, end_of_records=0x0) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:19699
#6  0x000000000073ccfd in evaluate_join_record (join=0x7f6fe7860978, join_tab=0x7f6fe7861940, error=0x0) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:18752
#7  0x000000000073c502 in sub_select (join=0x7f6fe7860978, join_tab=0x7f6fe7861940, end_of_records=0x0) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:18527
#8  0x000000000073b9d7 in do_select (join=0x7f6fe7860978, procedure=0x0) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:18071
#9  0x0000000000715de0 in JOIN::exec_inner (this=0x7f6fe7860978) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:3561
#10 0x00000000007151c0 in JOIN::exec (this=0x7f6fe7860978) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:3372
#11 0x0000000000716589 in mysql_select (thd=0x7f6fe7816070, tables=0x7f6fe7860268, wild_num=0x0, fields=..., conds=0x0, og_num=0x0, order=0x0, group=0x0, having=0x0, proc_param=0x0, select_options=0x80040b00, result=0x7f6fe7860958, unit=0x7f6fe7819b38, select_lex=0x7f6fe781a278) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:3757
#12 0x000000000070a1eb in handle_select (thd=0x7f6fe7816070, lex=0x7f6fe7819a70, result=0x7f6fe7860958, setup_tables_done_option=0x0) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:374
#13 0x00000000006d5f9b in execute_sqlcom_select (thd=0x7f6fe7816070, all_tables=0x7f6fe7860268) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:6351
#14 0x00000000006cb750 in mysql_execute_command (thd=0x7f6fe7816070) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:3376
#15 0x00000000006d9aa9 in mysql_parse (thd=0x7f6fe7816070, rawbuf=0x7f6fe7860088 "select x from t2", length=0x10, parser_state=0x7f7028682f80, is_com_multi=0x0, is_next_command=0x0) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:7794

@kevgs
Copy link

kevgs commented Nov 7, 2016

I've considered two ways to solve this:

  • As soon as Item_field have their Item_field::field set check them and replace with Item_null.
  • Perform additional checks inside Item_field methods to check whether is should be NULL.

The first method is simple. It requires using a simple check over SELECT list, GROUP BY list, ORDER BY list, HAVING and WHERE list. Rewriting of a tree could be done using Item::transform(). But Item::transform() is disallowed in prepared statements. And this is the core issue.

The second way to fix that is alter Item_field methods to check whether it should be NULL. Check could be performed with Item_field::context::SELECT_LEX::vers_conditions and Item_field::field::is_versioning_disabled().

Internally Item_field rely on Field::is_null to check whether it should be NULL. And there are a lot of places where this checks are performed. At least we're interested in those ones:

  • 12 methods val_int(), val_real(), etc.
  • 5 methods val_result(), val_int_result(), etc.
  • Inside Item_field::send()

@midenok
Copy link
Author

midenok commented Nov 7, 2016

Pls, try the following:

  1. add bool force_null flag to Field;
  2. Modify Item_field::set_field() to check your condition and set force_null if needed (don't forget to set maybe_null to true in this case);
  3. Modify Field::is_null() to respect force_null.

@kevgs
Copy link

kevgs commented Nov 8, 2016

Suggested solution works

Fixed in 4f893dc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants