revision-id: ff51e6038a6d64cf596f0ecda6c28aaf3dcfa3bf (fb-prod201903-163-gff51e6038a6) parent(s): c2789fdd09f84fbf4321c5bde74851e5b58d01f7 author: Sergei Petrunia committer: Sergei Petrunia timestamp: 2019-09-15 00:27:02 +0300 message: Issue #790, MyRocks/MRR: Fix a problem with ORDER BY queries SQL layer can request Sorted Mode, even if we indicated we don't support it. When this happens, the expected action is to switch to the default MRR implementation. (EXPLAIN will still work correctly, see the issue comment on github for explanations how) --- mysql-test/suite/rocksdb/r/rocksdb_mrr.result | 29 +++++++++++++++++++++++++++ mysql-test/suite/rocksdb/t/rocksdb_mrr.test | 19 ++++++++++++++++++ storage/rocksdb/ha_rocksdb.cc | 3 ++- 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/mysql-test/suite/rocksdb/r/rocksdb_mrr.result b/mysql-test/suite/rocksdb/r/rocksdb_mrr.result index bf90d75e696..7ece91c1fa4 100644 --- a/mysql-test/suite/rocksdb/r/rocksdb_mrr.result +++ b/mysql-test/suite/rocksdb/r/rocksdb_mrr.result @@ -344,4 +344,33 @@ attached_conditions_computation": [ set optimizer_trace=0; drop table t0, t2; +# +# Test for the "fallback to default MRR implementation if SQL layer +# requests the sorted mode". +# +create table t0(a int primary key); +insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9); +create table t1 (kp1 int, kp2 int, kp3 int, col1 int, key (kp1, kp2, kp3)); +insert into t1 select A.a, B.a, C.a, 123456 from t0 A, t0 B, t0 C; +set global rocksdb_force_flush_memtable_now=1; +analyze table t1; +Table Op Msg_type Msg_text +test.t1 analyze status OK +explain +select * from t1 where kp1=1 order by kp2 limit 10; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 range kp1 kp1 5 NULL # Using index condition +select * from t1 where kp1=1 order by kp2 limit 10; +kp1 kp2 kp3 col1 +1 0 0 123456 +1 0 1 123456 +1 0 2 123456 +1 0 3 123456 +1 0 4 123456 +1 0 5 123456 +1 0 6 123456 +1 0 7 123456 +1 0 8 123456 +1 0 9 123456 +drop table t0, t1; set optimizer_switch=@save_optimizer_switch; diff --git a/mysql-test/suite/rocksdb/t/rocksdb_mrr.test b/mysql-test/suite/rocksdb/t/rocksdb_mrr.test index 77a78cff6d1..dc8c260f754 100644 --- a/mysql-test/suite/rocksdb/t/rocksdb_mrr.test +++ b/mysql-test/suite/rocksdb/t/rocksdb_mrr.test @@ -252,4 +252,23 @@ set optimizer_trace=0; drop table t0, t2; +--echo # +--echo # Test for the "fallback to default MRR implementation if SQL layer +--echo # requests the sorted mode". +--echo # +create table t0(a int primary key); +insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9); + +create table t1 (kp1 int, kp2 int, kp3 int, col1 int, key (kp1, kp2, kp3)); +insert into t1 select A.a, B.a, C.a, 123456 from t0 A, t0 B, t0 C; + +set global rocksdb_force_flush_memtable_now=1; +analyze table t1; +--replace_column 9 # +explain +select * from t1 where kp1=1 order by kp2 limit 10; +# This shouldn't fail an assert: +select * from t1 where kp1=1 order by kp2 limit 10; + +drop table t0, t1; set optimizer_switch=@save_optimizer_switch; diff --git a/storage/rocksdb/ha_rocksdb.cc b/storage/rocksdb/ha_rocksdb.cc index cd2a195f088..94b8c3741f3 100644 --- a/storage/rocksdb/ha_rocksdb.cc +++ b/storage/rocksdb/ha_rocksdb.cc @@ -15312,7 +15312,8 @@ int ha_rocksdb::multi_range_read_init(RANGE_SEQ_IF *seq, void *seq_init_param, if (!current_thd->optimizer_switch_flag(OPTIMIZER_SWITCH_MRR) || (mode & HA_MRR_USE_DEFAULT_IMPL) || - (buf->buffer_end - buf->buffer < mrr_get_length_per_rec())) { + (buf->buffer_end - buf->buffer < mrr_get_length_per_rec()) || + (active_index != table->s->primary_key && (mode & HA_MRR_SORTED))) { mrr_uses_default_impl = true; res = handler::multi_range_read_init(seq, seq_init_param, n_ranges, mode, buf);