Commit 9a1e5465 authored by Igor Babaev's avatar Igor Babaev

Modified the code backported from mysql 5.6 to make it handle virtual

columns as well.
parent 37f3a801
...@@ -149,7 +149,7 @@ a b c ...@@ -149,7 +149,7 @@ a b c
2 -2 -2 2 -2 -2
explain select * from t3 where a between 1 and 2; explain select * from t3 where a between 1 and 2;
id select_type table type possible_keys key key_len ref rows Extra id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t3 range PRIMARY PRIMARY 4 NULL 1 Using where 1 SIMPLE t3 range PRIMARY PRIMARY 4 NULL 1 Using index condition
# SELECT * FROM tbl_name WHERE <non-indexed vcol expr> # SELECT * FROM tbl_name WHERE <non-indexed vcol expr>
select * from t3 where b between -2 and -1; select * from t3 where b between -2 and -1;
a b c a b c
...@@ -173,7 +173,7 @@ a b c ...@@ -173,7 +173,7 @@ a b c
1 -1 -1 1 -1 -1
explain select * from t3 where a between 1 and 2 order by b; explain select * from t3 where a between 1 and 2 order by b;
id select_type table type possible_keys key key_len ref rows Extra id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t3 range PRIMARY PRIMARY 4 NULL 1 Using where; Using filesort 1 SIMPLE t3 range PRIMARY PRIMARY 4 NULL 1 Using index condition; Using filesort
# SELECT * FROM tbl_name WHERE <non-vcol expr> ORDER BY <indexed vcol> # SELECT * FROM tbl_name WHERE <non-vcol expr> ORDER BY <indexed vcol>
select * from t3 where a between 1 and 2 order by c; select * from t3 where a between 1 and 2 order by c;
a b c a b c
...@@ -181,7 +181,7 @@ a b c ...@@ -181,7 +181,7 @@ a b c
1 -1 -1 1 -1 -1
explain select * from t3 where a between 1 and 2 order by c; explain select * from t3 where a between 1 and 2 order by c;
id select_type table type possible_keys key key_len ref rows Extra id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t3 range PRIMARY PRIMARY 4 NULL 1 Using where; Using filesort 1 SIMPLE t3 range PRIMARY PRIMARY 4 NULL 1 Using index condition; Using where; Using filesort
# SELECT * FROM tbl_name WHERE <non-indexed vcol expr> ORDER BY <non-vcol> # SELECT * FROM tbl_name WHERE <non-indexed vcol expr> ORDER BY <non-vcol>
select * from t3 where b between -2 and -1 order by a; select * from t3 where b between -2 and -1 order by a;
a b c a b c
......
...@@ -4674,9 +4674,10 @@ build_template_needs_field( ...@@ -4674,9 +4674,10 @@ build_template_needs_field(
primary key columns */ primary key columns */
dict_index_t* index, /*!< in: InnoDB index to use */ dict_index_t* index, /*!< in: InnoDB index to use */
const TABLE* table, /*!< in: MySQL table object */ const TABLE* table, /*!< in: MySQL table object */
ulint i) /*!< in: field index in InnoDB table */ ulint i, /*!< in: field index in InnoDB table */
ulint sql_idx) /*!< in: field index in SQL table */
{ {
const Field* field = table->field[i]; const Field* field = table->field[sql_idx];
ut_ad(index_contains == dict_index_contains_col_or_prefix(index, i)); ut_ad(index_contains == dict_index_contains_col_or_prefix(index, i));
...@@ -4693,8 +4694,8 @@ build_template_needs_field( ...@@ -4693,8 +4694,8 @@ build_template_needs_field(
return(field); return(field);
} }
if (bitmap_is_set(table->read_set, i) if (bitmap_is_set(table->read_set, sql_idx)
|| bitmap_is_set(table->write_set, i)) { || bitmap_is_set(table->write_set, sql_idx)) {
/* This field is needed in the query */ /* This field is needed in the query */
return(field); return(field);
...@@ -4802,10 +4803,10 @@ ha_innobase::build_template( ...@@ -4802,10 +4803,10 @@ ha_innobase::build_template(
{ {
dict_index_t* index; dict_index_t* index;
dict_index_t* clust_index; dict_index_t* clust_index;
ulint n_fields; ulint n_fields, n_stored_fields;
ibool fetch_all_in_key = FALSE; ibool fetch_all_in_key = FALSE;
ibool fetch_primary_key_cols = FALSE; ibool fetch_primary_key_cols = FALSE;
ulint i; ulint i, sql_idx;
if (prebuilt->select_lock_type == LOCK_X) { if (prebuilt->select_lock_type == LOCK_X) {
/* We always retrieve the whole clustered index record if we /* We always retrieve the whole clustered index record if we
...@@ -4855,10 +4856,11 @@ ha_innobase::build_template( ...@@ -4855,10 +4856,11 @@ ha_innobase::build_template(
the clustered index. */ the clustered index. */
n_fields = (ulint)table->s->fields; /* number of columns */ n_fields = (ulint)table->s->fields; /* number of columns */
n_stored_fields= (ulint)table->s->stored_fields; /* number of stored columns */
if (!prebuilt->mysql_template) { if (!prebuilt->mysql_template) {
prebuilt->mysql_template = (mysql_row_templ_t*) prebuilt->mysql_template = (mysql_row_templ_t*)
mem_alloc(n_fields * sizeof(mysql_row_templ_t)); mem_alloc(n_stored_fields * sizeof(mysql_row_templ_t));
} }
prebuilt->template_type = whole_row prebuilt->template_type = whole_row
...@@ -4876,7 +4878,12 @@ ha_innobase::build_template( ...@@ -4876,7 +4878,12 @@ ha_innobase::build_template(
if (active_index != MAX_KEY && active_index == pushed_idx_cond_keyno) { if (active_index != MAX_KEY && active_index == pushed_idx_cond_keyno) {
/* Push down an index condition or an end_range check. */ /* Push down an index condition or an end_range check. */
for (i = 0; i < n_fields; i++) { for (i = 0, sql_idx = 0; i < n_stored_fields; i++, sql_idx++) {
while (!table->field[sql_idx]->stored_in_db) {
sql_idx++;
}
const ibool index_contains const ibool index_contains
= dict_index_contains_col_or_prefix(index, i); = dict_index_contains_col_or_prefix(index, i);
...@@ -4903,14 +4910,14 @@ ha_innobase::build_template( ...@@ -4903,14 +4910,14 @@ ha_innobase::build_template(
mysql_row_templ_t* templ; mysql_row_templ_t* templ;
if (whole_row) { if (whole_row) {
field = table->field[i]; field = table->field[sql_idx];
} else { } else {
field = build_template_needs_field( field = build_template_needs_field(
index_contains, index_contains,
prebuilt->read_just_key, prebuilt->read_just_key,
fetch_all_in_key, fetch_all_in_key,
fetch_primary_key_cols, fetch_primary_key_cols,
index, table, i); index, table, i, sql_idx);
if (!field) { if (!field) {
continue; continue;
} }
...@@ -4982,7 +4989,12 @@ ha_innobase::build_template( ...@@ -4982,7 +4989,12 @@ ha_innobase::build_template(
/* Include the fields that are not needed in index condition /* Include the fields that are not needed in index condition
pushdown. */ pushdown. */
for (i = 0; i < n_fields; i++) { for (i = 0, sql_idx = 0; i < n_stored_fields; i++, sql_idx++) {
while (!table->field[sql_idx]->stored_in_db) {
sql_idx++;
}
const ibool index_contains const ibool index_contains
= dict_index_contains_col_or_prefix(index, i); = dict_index_contains_col_or_prefix(index, i);
...@@ -4994,14 +5006,14 @@ ha_innobase::build_template( ...@@ -4994,14 +5006,14 @@ ha_innobase::build_template(
const Field* field; const Field* field;
if (whole_row) { if (whole_row) {
field = table->field[i]; field = table->field[sql_idx];
} else { } else {
field = build_template_needs_field( field = build_template_needs_field(
index_contains, index_contains,
prebuilt->read_just_key, prebuilt->read_just_key,
fetch_all_in_key, fetch_all_in_key,
fetch_primary_key_cols, fetch_primary_key_cols,
index, table, i); index, table, i, sql_idx);
if (!field) { if (!field) {
continue; continue;
} }
...@@ -5018,11 +5030,15 @@ ha_innobase::build_template( ...@@ -5018,11 +5030,15 @@ ha_innobase::build_template(
/* No index condition pushdown */ /* No index condition pushdown */
prebuilt->idx_cond = NULL; prebuilt->idx_cond = NULL;
for (i = 0; i < n_fields; i++) { for (i = 0, sql_idx = 0; i < n_stored_fields; i++, sql_idx++) {
const Field* field; const Field* field;
while (!table->field[sql_idx]->stored_in_db) {
sql_idx++;
}
if (whole_row) { if (whole_row) {
field = table->field[i]; field = table->field[sql_idx];
} else { } else {
field = build_template_needs_field( field = build_template_needs_field(
dict_index_contains_col_or_prefix( dict_index_contains_col_or_prefix(
...@@ -5030,7 +5046,7 @@ ha_innobase::build_template( ...@@ -5030,7 +5046,7 @@ ha_innobase::build_template(
prebuilt->read_just_key, prebuilt->read_just_key,
fetch_all_in_key, fetch_all_in_key,
fetch_primary_key_cols, fetch_primary_key_cols,
index, table, i); index, table, i, sql_idx);
if (!field) { if (!field) {
continue; continue;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment