From 592b7fbac9cfdd47a1f360e55465b250f2432254 Mon Sep 17 00:00:00 2001
From: Igor Babaev <igor@askmonty.org>
Date: Tue, 28 Oct 2014 14:33:31 -0700
Subject: [PATCH] Fixed bug mdev-6325.

Field::selectivity should be set for all fields used in range conditions.
---
 mysql-test/r/selectivity.result        | 35 ++++++++++++++++++++++++
 mysql-test/r/selectivity_innodb.result | 35 ++++++++++++++++++++++++
 mysql-test/t/selectivity.test          | 25 +++++++++++++++++
 sql/opt_range.cc                       | 38 ++++++++++++++++++--------
 sql/sql_select.cc                      |  3 +-
 5 files changed, 123 insertions(+), 13 deletions(-)

diff --git a/mysql-test/r/selectivity.result b/mysql-test/r/selectivity.result
index 9899b894ff6..088e169ed0c 100644
--- a/mysql-test/r/selectivity.result
+++ b/mysql-test/r/selectivity.result
@@ -1346,4 +1346,39 @@ foo	foo	1
 foo	foo	2
 set optimizer_use_condition_selectivity=@save_optimizer_use_condition_selectivity;
 DROP TABLE t1,t2;
+#
+# Bug mdev-6325: wrong selectivity of a column with ref access 
+#
+create table t0(a int);
+insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
+create table t1(a int);
+insert into t1 select A.a + B.a* 10 + C.a * 100 from t0 A, t0 B, t0 C;
+create table t2 (a int, b int, key(a));
+insert into t2 select A.a + 10*B.a, 12345 from t0 A, t0 B, t0 C;
+set use_stat_tables='preferably';
+set histogram_size=100;
+set optimizer_use_condition_selectivity=4;
+analyze table t1 persistent for all;
+Table	Op	Msg_type	Msg_text
+test.t1	analyze	status	Engine-independent statistics collected
+test.t1	analyze	status	OK
+analyze table t2 persistent for all;
+Table	Op	Msg_type	Msg_text
+test.t2	analyze	status	Engine-independent statistics collected
+test.t2	analyze	status	Table is already up to date
+explain extended 
+select * from t1 straight_join t2 where t1.a=t2.a and t1.a<10;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
+1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	1000	0.99	Using where
+1	SIMPLE	t2	ref	a	a	5	test.t1.a	10	100.00	
+Warnings:
+Note	1003	select `test`.`t1`.`a` AS `a`,`test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b` from `test`.`t1` straight_join `test`.`t2` where ((`test`.`t2`.`a` = `test`.`t1`.`a`) and (`test`.`t1`.`a` < 10))
+explain extended 
+select * from t1 straight_join t2 where t1.a=t2.a and t2.a<10;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
+1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	1000	0.99	Using where
+1	SIMPLE	t2	ref	a	a	5	test.t1.a	10	100.00	
+Warnings:
+Note	1003	select `test`.`t1`.`a` AS `a`,`test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b` from `test`.`t1` straight_join `test`.`t2` where ((`test`.`t2`.`a` = `test`.`t1`.`a`) and (`test`.`t1`.`a` < 10))
+drop table t0,t1,t2;
 set use_stat_tables=@save_use_stat_tables;
diff --git a/mysql-test/r/selectivity_innodb.result b/mysql-test/r/selectivity_innodb.result
index 013fb1d876c..af9c31afae1 100644
--- a/mysql-test/r/selectivity_innodb.result
+++ b/mysql-test/r/selectivity_innodb.result
@@ -1356,6 +1356,41 @@ foo	foo	1
 foo	foo	2
 set optimizer_use_condition_selectivity=@save_optimizer_use_condition_selectivity;
 DROP TABLE t1,t2;
+#
+# Bug mdev-6325: wrong selectivity of a column with ref access 
+#
+create table t0(a int);
+insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
+create table t1(a int);
+insert into t1 select A.a + B.a* 10 + C.a * 100 from t0 A, t0 B, t0 C;
+create table t2 (a int, b int, key(a));
+insert into t2 select A.a + 10*B.a, 12345 from t0 A, t0 B, t0 C;
+set use_stat_tables='preferably';
+set histogram_size=100;
+set optimizer_use_condition_selectivity=4;
+analyze table t1 persistent for all;
+Table	Op	Msg_type	Msg_text
+test.t1	analyze	status	Engine-independent statistics collected
+test.t1	analyze	status	OK
+analyze table t2 persistent for all;
+Table	Op	Msg_type	Msg_text
+test.t2	analyze	status	Engine-independent statistics collected
+test.t2	analyze	status	OK
+explain extended 
+select * from t1 straight_join t2 where t1.a=t2.a and t1.a<10;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
+1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	1000	0.99	Using where
+1	SIMPLE	t2	ref	a	a	5	test.t1.a	10	100.00	
+Warnings:
+Note	1003	select `test`.`t1`.`a` AS `a`,`test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b` from `test`.`t1` straight_join `test`.`t2` where ((`test`.`t2`.`a` = `test`.`t1`.`a`) and (`test`.`t1`.`a` < 10))
+explain extended 
+select * from t1 straight_join t2 where t1.a=t2.a and t2.a<10;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
+1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	1000	0.99	Using where
+1	SIMPLE	t2	ref	a	a	5	test.t1.a	10	100.00	
+Warnings:
+Note	1003	select `test`.`t1`.`a` AS `a`,`test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b` from `test`.`t1` straight_join `test`.`t2` where ((`test`.`t2`.`a` = `test`.`t1`.`a`) and (`test`.`t1`.`a` < 10))
+drop table t0,t1,t2;
 set use_stat_tables=@save_use_stat_tables;
 set optimizer_switch=@save_optimizer_switch_for_selectivity_test;
 set @tmp_ust= @@use_stat_tables;
diff --git a/mysql-test/t/selectivity.test b/mysql-test/t/selectivity.test
index 8cb2620550e..16226b07751 100644
--- a/mysql-test/t/selectivity.test
+++ b/mysql-test/t/selectivity.test
@@ -889,4 +889,29 @@ set optimizer_use_condition_selectivity=@save_optimizer_use_condition_selectivit
 
 DROP TABLE t1,t2;
 
+--echo #
+--echo # Bug mdev-6325: wrong selectivity of a column with ref access 
+--echo #
+
+create table t0(a int);
+insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
+create table t1(a int);
+insert into t1 select A.a + B.a* 10 + C.a * 100 from t0 A, t0 B, t0 C;
+create table t2 (a int, b int, key(a));
+insert into t2 select A.a + 10*B.a, 12345 from t0 A, t0 B, t0 C;
+
+set use_stat_tables='preferably';
+set histogram_size=100;
+
+set optimizer_use_condition_selectivity=4;
+analyze table t1 persistent for all;
+analyze table t2 persistent for all;
+
+explain extended 
+select * from t1 straight_join t2 where t1.a=t2.a and t1.a<10;
+explain extended 
+select * from t1 straight_join t2 where t1.a=t2.a and t2.a<10;
+
+drop table t0,t1,t2;
+
 set use_stat_tables=@save_use_stat_tables;
diff --git a/sql/opt_range.cc b/sql/opt_range.cc
index 00e81767203..6871a6bd0af 100644
--- a/sql/opt_range.cc
+++ b/sql/opt_range.cc
@@ -3487,6 +3487,8 @@ bool calculate_cond_selectivity_for_table(THD *thd, TABLE *table, Item *cond)
   my_bitmap_init(&handled_columns, buf, table->s->fields, FALSE);
 
   /*
+    Calculate the selectivity of the range conditions supported by indexes.
+
     First, take into account possible range accesses. 
     range access estimates are the most precise, we prefer them to any other
     estimate sources.
@@ -3532,6 +3534,7 @@ bool calculate_cond_selectivity_for_table(THD *thd, TABLE *table, Item *cond)
 	    break; 
           bitmap_set_bit(&handled_columns, key_part->fieldnr-1);
         }
+        double selectivity_mult;
         if (i)
         {
           /* 
@@ -3547,7 +3550,6 @@ bool calculate_cond_selectivity_for_table(THD *thd, TABLE *table, Item *cond)
             */
             double f1= key_info->actual_rec_per_key(i-1);
             double f2= key_info->actual_rec_per_key(i);
-            double selectivity_mult;
             if (f1 > 0 && f2 > 0)
               selectivity_mult= f1 / f2;
             else
@@ -3559,8 +3561,23 @@ bool calculate_cond_selectivity_for_table(THD *thd, TABLE *table, Item *cond)
               */
               selectivity_mult= ((double)(i+1)) / i;
             }
-            table->cond_selectivity*= selectivity_mult;
+            table->cond_selectivity*= selectivity_mult;            
           }
+          /*
+            We need to set selectivity for fields supported by indexes.
+            For single-component indexes and for some first components
+            of other indexes we do it here. For the remaining fields
+            we do it later in this function, in the same way as for the
+            fields not used in any indexes.
+	  */
+	  if (i == 1)
+	  {
+            uint fieldnr= key_info->key_part[0].fieldnr;
+            table->field[fieldnr-1]->cond_selectivity= quick_cond_selectivity;
+            if (i != used_key_parts)
+	      table->field[fieldnr-1]->cond_selectivity*= selectivity_mult;
+            bitmap_clear_bit(used_fields, fieldnr-1);
+	  }
         }
       }
     }
@@ -3568,10 +3585,9 @@ bool calculate_cond_selectivity_for_table(THD *thd, TABLE *table, Item *cond)
    
   /* 
     Second step: calculate the selectivity of the range conditions not 
-    supported by any index
+    supported by any index and selectivity of the range condition
+    over the fields whose selectivity has not been set yet.
   */
-  bitmap_subtract(used_fields, &handled_columns);
-  /* no need to do: my_bitmap_free(&handled_columns); */
 
   if (thd->variables.optimizer_use_condition_selectivity > 2 &&
       !bitmap_is_clear_all(used_fields))
@@ -3647,9 +3663,12 @@ bool calculate_cond_selectivity_for_table(THD *thd, TABLE *table, Item *cond)
     for (Field **field_ptr= table->field; *field_ptr; field_ptr++)
     {
       Field *table_field= *field_ptr;   
-      if (bitmap_is_set(table->read_set, table_field->field_index) &&
+      if (bitmap_is_set(used_fields, table_field->field_index) &&
           table_field->cond_selectivity < 1.0)
-        table->cond_selectivity*= table_field->cond_selectivity;
+      {
+        if (!bitmap_is_set(&handled_columns, table_field->field_index))
+          table->cond_selectivity*= table_field->cond_selectivity;
+      }
     }
 
   free_alloc:
@@ -3658,10 +3677,7 @@ bool calculate_cond_selectivity_for_table(THD *thd, TABLE *table, Item *cond)
 
   }
 
-  /* Calculate the selectivity of the range conditions supported by indexes */
-
-  bitmap_clear_all(used_fields);
-
+  bitmap_union(used_fields, &handled_columns);
 
   /* Check if we can improve selectivity estimates by using sampling */
   ulong check_rows=
diff --git a/sql/sql_select.cc b/sql/sql_select.cc
index 127c73ca873..636b5702a93 100644
--- a/sql/sql_select.cc
+++ b/sql/sql_select.cc
@@ -4407,8 +4407,7 @@ add_key_field(JOIN *join,
       if (is_const)
       {
         stat[0].const_keys.merge(possible_keys);
-        if (possible_keys.is_clear_all())
-          bitmap_set_bit(&field->table->cond_set, field->field_index);
+        bitmap_set_bit(&field->table->cond_set, field->field_index);
       }
       else if (!eq_func)
       {
-- 
2.30.9