1
0
mirror of https://github.com/MariaDB/server.git synced 2025-08-08 11:22:35 +03:00

bugfix: move vcol calculations down into the handler

This fixes a bug where handler::read_range_first (for example)
needed to compare vcol values that were not calculated yet.

As a bonus it fixes few cases where vcols were calculated twice
This commit is contained in:
Sergei Golubchik
2016-10-22 17:33:42 +02:00
parent b8f51c04d3
commit 0e401bf7bf
9 changed files with 72 additions and 25 deletions

View File

@@ -0,0 +1,25 @@
CREATE TABLE t1 (
pk INT AUTO_INCREMENT PRIMARY KEY,
col_int_nokey INT NULL,
col_int_key INT AS (col_int_nokey) VIRTUAL,
KEY (col_int_key)
);
INSERT INTO t1 (col_int_nokey)
VALUES (0), (5), (4), (3), (7), (42), (5), (0), (3);
SELECT * FROM t1 WHERE col_int_key IN (3, 4) AND col_int_key <= 83 ORDER BY 1;
pk col_int_nokey col_int_key
3 4 4
4 3 3
9 3 3
set optimizer_switch='index_condition_pushdown=off';
SELECT * FROM t1 WHERE col_int_key IN (3, 4) ORDER BY 1;
pk col_int_nokey col_int_key
3 4 4
4 3 3
9 3 3
SELECT * FROM t1 WHERE col_int_key IN (3, 4) AND col_int_key <= 83 ORDER BY 1;
pk col_int_nokey col_int_key
3 4 4
4 3 3
9 3 3
DROP TABLE t1;

View File

@@ -0,0 +1,13 @@
CREATE TABLE t1 (
pk INT AUTO_INCREMENT PRIMARY KEY,
col_int_nokey INT NULL,
col_int_key INT AS (col_int_nokey) VIRTUAL,
KEY (col_int_key)
);
INSERT INTO t1 (col_int_nokey)
VALUES (0), (5), (4), (3), (7), (42), (5), (0), (3);
SELECT * FROM t1 WHERE col_int_key IN (3, 4) AND col_int_key <= 83 ORDER BY 1;
set optimizer_switch='index_condition_pushdown=off';
SELECT * FROM t1 WHERE col_int_key IN (3, 4) ORDER BY 1;
SELECT * FROM t1 WHERE col_int_key IN (3, 4) AND col_int_key <= 83 ORDER BY 1;
DROP TABLE t1;

View File

@@ -31,7 +31,7 @@
#include <m_ctype.h> #include <m_ctype.h>
#include "sql_sort.h" #include "sql_sort.h"
#include "probes_mysql.h" #include "probes_mysql.h"
#include "sql_base.h" // update_virtual_fields #include "sql_base.h"
#include "sql_test.h" // TEST_filesort #include "sql_test.h" // TEST_filesort
#include "opt_range.h" // SQL_SELECT #include "opt_range.h" // SQL_SELECT
#include "bounded_queue.h" #include "bounded_queue.h"
@@ -784,8 +784,6 @@ static ha_rows find_all_keys(THD *thd, Sort_param *param, SQL_SELECT *select,
{ {
if ((error= select->quick->get_next())) if ((error= select->quick->get_next()))
break; break;
if (!error && sort_form->vfield)
sort_form->update_virtual_fields(VCOL_UPDATE_FOR_READ);
file->position(sort_form->record[0]); file->position(sort_form->record[0]);
DBUG_EXECUTE_IF("debug_filesort", dbug_print_record(sort_form, TRUE);); DBUG_EXECUTE_IF("debug_filesort", dbug_print_record(sort_form, TRUE););
} }
@@ -793,8 +791,6 @@ static ha_rows find_all_keys(THD *thd, Sort_param *param, SQL_SELECT *select,
{ {
{ {
error= file->ha_rnd_next(sort_form->record[0]); error= file->ha_rnd_next(sort_form->record[0]);
if (!error && sort_form->vfield)
sort_form->update_virtual_fields(VCOL_UPDATE_FOR_READ);
if (!flag) if (!flag)
{ {
my_store_ptr(ref_pos,ref_length,record); // Position to row my_store_ptr(ref_pos,ref_length,record); // Position to row

View File

@@ -2579,6 +2579,8 @@ int handler::ha_rnd_next(uchar *buf)
if (!result) if (!result)
{ {
update_rows_read(); update_rows_read();
if (table->vfield && buf == table->record[0])
table->update_virtual_fields(VCOL_UPDATE_FOR_READ);
increment_statistics(&SSV::ha_read_rnd_next_count); increment_statistics(&SSV::ha_read_rnd_next_count);
} }
else if (result == HA_ERR_RECORD_DELETED) else if (result == HA_ERR_RECORD_DELETED)
@@ -2603,7 +2605,11 @@ int handler::ha_rnd_pos(uchar *buf, uchar *pos)
{ result= rnd_pos(buf, pos); }) { result= rnd_pos(buf, pos); })
increment_statistics(&SSV::ha_read_rnd_count); increment_statistics(&SSV::ha_read_rnd_count);
if (!result) if (!result)
{
update_rows_read(); update_rows_read();
if (table->vfield && buf == table->record[0])
table->update_virtual_fields(VCOL_UPDATE_FOR_READ);
}
table->status=result ? STATUS_NOT_FOUND: 0; table->status=result ? STATUS_NOT_FOUND: 0;
DBUG_RETURN(result); DBUG_RETURN(result);
} }
@@ -2622,7 +2628,11 @@ int handler::ha_index_read_map(uchar *buf, const uchar *key,
{ result= index_read_map(buf, key, keypart_map, find_flag); }) { result= index_read_map(buf, key, keypart_map, find_flag); })
increment_statistics(&SSV::ha_read_key_count); increment_statistics(&SSV::ha_read_key_count);
if (!result) if (!result)
{
update_index_statistics(); update_index_statistics();
if (table->vfield && buf == table->record[0])
table->update_virtual_fields(VCOL_UPDATE_FOR_READ);
}
table->status=result ? STATUS_NOT_FOUND: 0; table->status=result ? STATUS_NOT_FOUND: 0;
DBUG_RETURN(result); DBUG_RETURN(result);
} }
@@ -2649,6 +2659,8 @@ int handler::ha_index_read_idx_map(uchar *buf, uint index, const uchar *key,
{ {
update_rows_read(); update_rows_read();
index_rows_read[index]++; index_rows_read[index]++;
if (table->vfield && buf == table->record[0])
table->update_virtual_fields(VCOL_UPDATE_FOR_READ);
} }
table->status=result ? STATUS_NOT_FOUND: 0; table->status=result ? STATUS_NOT_FOUND: 0;
return result; return result;
@@ -2666,7 +2678,11 @@ int handler::ha_index_next(uchar * buf)
{ result= index_next(buf); }) { result= index_next(buf); })
increment_statistics(&SSV::ha_read_next_count); increment_statistics(&SSV::ha_read_next_count);
if (!result) if (!result)
{
update_index_statistics(); update_index_statistics();
if (table->vfield && buf == table->record[0])
table->update_virtual_fields(VCOL_UPDATE_FOR_READ);
}
table->status=result ? STATUS_NOT_FOUND: 0; table->status=result ? STATUS_NOT_FOUND: 0;
DBUG_RETURN(result); DBUG_RETURN(result);
} }
@@ -2683,7 +2699,11 @@ int handler::ha_index_prev(uchar * buf)
{ result= index_prev(buf); }) { result= index_prev(buf); })
increment_statistics(&SSV::ha_read_prev_count); increment_statistics(&SSV::ha_read_prev_count);
if (!result) if (!result)
{
update_index_statistics(); update_index_statistics();
if (table->vfield && buf == table->record[0])
table->update_virtual_fields(VCOL_UPDATE_FOR_READ);
}
table->status=result ? STATUS_NOT_FOUND: 0; table->status=result ? STATUS_NOT_FOUND: 0;
DBUG_RETURN(result); DBUG_RETURN(result);
} }
@@ -2699,7 +2719,11 @@ int handler::ha_index_first(uchar * buf)
{ result= index_first(buf); }) { result= index_first(buf); })
increment_statistics(&SSV::ha_read_first_count); increment_statistics(&SSV::ha_read_first_count);
if (!result) if (!result)
{
update_index_statistics(); update_index_statistics();
if (table->vfield && buf == table->record[0])
table->update_virtual_fields(VCOL_UPDATE_FOR_READ);
}
table->status=result ? STATUS_NOT_FOUND: 0; table->status=result ? STATUS_NOT_FOUND: 0;
return result; return result;
} }
@@ -2715,7 +2739,11 @@ int handler::ha_index_last(uchar * buf)
{ result= index_last(buf); }) { result= index_last(buf); })
increment_statistics(&SSV::ha_read_last_count); increment_statistics(&SSV::ha_read_last_count);
if (!result) if (!result)
{
update_index_statistics(); update_index_statistics();
if (table->vfield && buf == table->record[0])
table->update_virtual_fields(VCOL_UPDATE_FOR_READ);
}
table->status=result ? STATUS_NOT_FOUND: 0; table->status=result ? STATUS_NOT_FOUND: 0;
return result; return result;
} }
@@ -2731,7 +2759,11 @@ int handler::ha_index_next_same(uchar *buf, const uchar *key, uint keylen)
{ result= index_next_same(buf, key, keylen); }) { result= index_next_same(buf, key, keylen); })
increment_statistics(&SSV::ha_read_next_count); increment_statistics(&SSV::ha_read_next_count);
if (!result) if (!result)
{
update_index_statistics(); update_index_statistics();
if (table->vfield && buf == table->record[0])
table->update_virtual_fields(VCOL_UPDATE_FOR_READ);
}
table->status=result ? STATUS_NOT_FOUND: 0; table->status=result ? STATUS_NOT_FOUND: 0;
return result; return result;
} }

View File

@@ -561,7 +561,7 @@ bool mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds,
{ {
explain->tracker.on_record_read(); explain->tracker.on_record_read();
if (table->vfield) if (table->vfield)
table->update_virtual_fields(VCOL_UPDATE_FOR_READ_WRITE); table->update_virtual_fields(VCOL_UPDATE_FOR_WRITE);
thd->inc_examined_row_count(1); thd->inc_examined_row_count(1);
// thd->is_error() is tested to disallow delete row on error // thd->is_error() is tested to disallow delete row on error
if (!select || select->skip_record(thd) > 0) if (!select || select->skip_record(thd) > 0)

View File

@@ -929,9 +929,6 @@ retry:
} }
goto ok; goto ok;
} }
/* Generate values for virtual fields */
if (table->vfield)
table->update_virtual_fields(VCOL_UPDATE_FOR_READ);
if (cond && !cond->val_int()) if (cond && !cond->val_int())
{ {
if (thd->is_error()) if (thd->is_error())

View File

@@ -3371,7 +3371,6 @@ int JOIN_TAB_SCAN::next()
int skip_rc; int skip_rc;
READ_RECORD *info= &join_tab->read_record; READ_RECORD *info= &join_tab->read_record;
SQL_SELECT *select= join_tab->cache_select; SQL_SELECT *select= join_tab->cache_select;
TABLE *table= join_tab->table;
THD *thd= join->thd; THD *thd= join->thd;
if (is_first_record) if (is_first_record)
@@ -3382,8 +3381,6 @@ int JOIN_TAB_SCAN::next()
if (!err) if (!err)
{ {
join_tab->tracker->r_rows++; join_tab->tracker->r_rows++;
if (table->vfield)
table->update_virtual_fields(VCOL_UPDATE_FOR_READ);
} }
while (!err && select && (skip_rc= select->skip_record(thd)) <= 0) while (!err && select && (skip_rc= select->skip_record(thd)) <= 0)
@@ -3398,8 +3395,6 @@ int JOIN_TAB_SCAN::next()
if (!err) if (!err)
{ {
join_tab->tracker->r_rows++; join_tab->tracker->r_rows++;
if (table->vfield)
table->update_virtual_fields(VCOL_UPDATE_FOR_READ);
} }
} }
@@ -3923,8 +3918,6 @@ int JOIN_TAB_SCAN_MRR::next()
DBUG_ASSERT(cache->buff <= (uchar *) (*ptr) && DBUG_ASSERT(cache->buff <= (uchar *) (*ptr) &&
(uchar *) (*ptr) <= cache->end_pos); (uchar *) (*ptr) <= cache->end_pos);
*/ */
if (join_tab->table->vfield)
join_tab->table->update_virtual_fields(VCOL_UPDATE_FOR_READ);
} }
return rc; return rc;
} }

View File

@@ -18442,9 +18442,6 @@ evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
join_tab->tracker->r_rows++; join_tab->tracker->r_rows++;
if (join_tab->table->vfield)
join_tab->table->update_virtual_fields(VCOL_UPDATE_FOR_READ);
if (select_cond) if (select_cond)
{ {
select_cond_result= MY_TEST(select_cond->val_int()); select_cond_result= MY_TEST(select_cond->val_int());
@@ -18895,8 +18892,6 @@ join_read_system(JOIN_TAB *tab)
empty_record(table); // Make empty record empty_record(table); // Make empty record
return -1; return -1;
} }
if (table->vfield)
table->update_virtual_fields(VCOL_UPDATE_FOR_READ);
store_record(table,record[1]); store_record(table,record[1]);
} }
else if (!table->status) // Only happens with left join else if (!table->status) // Only happens with left join
@@ -18942,8 +18937,6 @@ join_read_const(JOIN_TAB *tab)
return report_error(table, error); return report_error(table, error);
return -1; return -1;
} }
if (table->vfield)
table->update_virtual_fields(VCOL_UPDATE_FOR_READ);
store_record(table,record[1]); store_record(table,record[1]);
} }
else if (!(table->status & ~STATUS_NULL_ROW)) // Only happens with left join else if (!(table->status & ~STATUS_NULL_ROW)) // Only happens with left join

View File

@@ -9803,8 +9803,6 @@ copy_data_between_tables(THD *thd, TABLE *from, TABLE *to,
error= 1; error= 1;
break; break;
} }
if (from->vfield)
from->update_virtual_fields(VCOL_UPDATE_FOR_READ);
if (++thd->progress.counter >= time_to_report_progress) if (++thd->progress.counter >= time_to_report_progress)
{ {
time_to_report_progress+= MY_HOW_OFTEN_TO_WRITE/10; time_to_report_progress+= MY_HOW_OFTEN_TO_WRITE/10;