diff --git a/mysql-test/r/index_merge_innodb.result b/mysql-test/r/index_merge_innodb.result
index ae25cc28101c0de689826a01cd70cb98bce91029..5af3e291c871f58bf68e724ceefd22f13917fe14 100644
--- a/mysql-test/r/index_merge_innodb.result
+++ b/mysql-test/r/index_merge_innodb.result
@@ -282,3 +282,43 @@ kp1='279' AND kp2='ELM0678' AND kp3='6' AND kp4='10' AND  kp5 = 'R        ';
 COUNT(*)
 1
 drop table t1;
+create table t1
+(
+key1 int not null, 
+key2 int not null default 0,
+key3 int not null default 0
+);
+insert into t1(key1) values (1),(2),(3),(4),(5),(6),(7),(8);
+set @d=8;
+insert into t1 (key1) select key1+@d from t1;
+set @d=@d*2;
+insert into t1 (key1) select key1+@d from t1;
+set @d=@d*2;
+insert into t1 (key1) select key1+@d from t1;
+set @d=@d*2;
+insert into t1 (key1) select key1+@d from t1;
+set @d=@d*2;
+insert into t1 (key1) select key1+@d from t1;
+set @d=@d*2;
+insert into t1 (key1) select key1+@d from t1;
+set @d=@d*2;
+insert into t1 (key1) select key1+@d from t1;
+set @d=@d*2;
+alter table t1 add index i2(key2);
+alter table t1 add index i3(key3);
+update t1 set key2=key1,key3=key1;
+explain select * from t1 where (key3 > 30 and key3<35) or (key2 >32 and key2 < 40);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	SIMPLE	t1	index_merge	i2,i3	i3,i2	4,4	NULL	11	Using sort_union(i3,i2); Using where
+select * from t1 where (key3 > 30 and key3<35) or (key2 >32 and key2 < 40);
+key1	key2	key3
+31	31	31
+32	32	32
+33	33	33
+34	34	34
+35	35	35
+36	36	36
+37	37	37
+38	38	38
+39	39	39
+drop table t1;
diff --git a/mysql-test/t/index_merge_innodb.test b/mysql-test/t/index_merge_innodb.test
index 25f4e0b4e65af6aaf7bdccd01869b5b1efe7be13..7e6c524e81190ac317d23e0111b72b64e44acff4 100644
--- a/mysql-test/t/index_merge_innodb.test
+++ b/mysql-test/t/index_merge_innodb.test
@@ -299,4 +299,31 @@ SELECT COUNT(*) FROM t1 WHERE b = 0 AND a = 0 AND c = 13286427 AND
 
 drop table t1;
 
+# BUG#21277: Index Merge/sort_union: wrong query results
+create table t1
+(
+  key1 int not null, 
+  key2 int not null default 0,
+  key3 int not null default 0
+);
+
+insert into t1(key1) values (1),(2),(3),(4),(5),(6),(7),(8);
+
+let $1=7;
+set @d=8;
+while ($1)
+{
+  eval insert into t1 (key1) select key1+@d from t1;
+  eval set @d=@d*2;
+  dec $1;
+}
+
+alter table t1 add index i2(key2);
+alter table t1 add index i3(key3);
+update t1 set key2=key1,key3=key1;
+
+# to test the bug, the following must use "sort_union":
+explain select * from t1 where (key3 > 30 and key3<35) or (key2 >32 and key2 < 40);
+select * from t1 where (key3 > 30 and key3<35) or (key2 >32 and key2 < 40);
+drop table t1;
 
diff --git a/sql/opt_range.cc b/sql/opt_range.cc
index a5a46ba11b61e89cf5ba4f0520a932435082bcb5..e501a790f909b21ded549098ddfdf25cf91b3a64 100644
--- a/sql/opt_range.cc
+++ b/sql/opt_range.cc
@@ -7584,16 +7584,10 @@ int QUICK_INDEX_MERGE_SELECT::read_keys_and_merge()
   QUICK_RANGE_SELECT* cur_quick;
   int result;
   Unique *unique;
-  MY_BITMAP *save_read_set, *save_write_set;
   handler *file= head->file;
   DBUG_ENTER("QUICK_INDEX_MERGE_SELECT::read_keys_and_merge");
 
-  /* We're going to just read rowids. */
-  save_read_set=  head->read_set;
-  save_write_set= head->write_set;
   file->extra(HA_EXTRA_KEYREAD);
-  bitmap_clear_all(&head->tmp_set);
-  head->column_bitmaps_set(&head->tmp_set, &head->tmp_set);
   head->prepare_for_position();
 
   cur_quick_it.rewind();
@@ -7658,7 +7652,6 @@ int QUICK_INDEX_MERGE_SELECT::read_keys_and_merge()
   doing_pk_scan= FALSE;
   /* index_merge currently doesn't support "using index" at all */
   file->extra(HA_EXTRA_NO_KEYREAD);
-  head->column_bitmaps_set(save_read_set, save_write_set);
   /* start table scan */
   init_read_record(&read_record, thd, head, (SQL_SELECT*) 0, 1, 1);
   DBUG_RETURN(result);