Skip to content

Commit b18697f

Browse files
committed
MDEV-27398 DESC index causes wrong (empty) result on Federated tables
take descending indexes into account when generating a query
1 parent f00236a commit b18697f

File tree

3 files changed

+61
-5
lines changed

3 files changed

+61
-5
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#
2+
# MDEV-27398 DESC index causes wrong (empty) result on Federated tables
3+
#
4+
create table t (a int);
5+
insert into t values (1),(5),(2),(3),(4);
6+
create table tf (a int, key(a desc)) connection 'mysql://root@127.0.0.1:$MASTER_MYPORT/test/t' engine=Federated;
7+
select * from tf;
8+
a
9+
1
10+
2
11+
3
12+
4
13+
5
14+
select * from tf where a > 2;
15+
a
16+
3
17+
4
18+
5
19+
select * from tf where a < 3;
20+
a
21+
1
22+
2
23+
select * from tf where a >= 3;
24+
a
25+
3
26+
4
27+
5
28+
select * from tf where a <= 3;
29+
a
30+
1
31+
2
32+
3
33+
drop table tf, t;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--source include/not_embedded.inc
2+
--source suite/federated/have_federatedx.inc
3+
4+
--echo #
5+
--echo # MDEV-27398 DESC index causes wrong (empty) result on Federated tables
6+
--echo #
7+
8+
create table t (a int);
9+
insert into t values (1),(5),(2),(3),(4);
10+
11+
--evalp create table tf (a int, key(a desc)) connection 'mysql://root@127.0.0.1:$MASTER_MYPORT/test/t' engine=Federated
12+
--sorted_result
13+
select * from tf;
14+
--sorted_result
15+
select * from tf where a > 2;
16+
--sorted_result
17+
select * from tf where a < 3;
18+
--sorted_result
19+
select * from tf where a >= 3;
20+
--sorted_result
21+
select * from tf where a <= 3;
22+
drop table tf, t;

storage/federatedx/ha_federatedx.cc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,6 +1261,7 @@ bool ha_federatedx::create_where_from_key(String *to,
12611261
uint store_length= key_part->store_length;
12621262
uint part_length= MY_MIN(store_length, length);
12631263
bool needs_quotes= field->str_needs_quotes();
1264+
bool reverse= key_part->key_part_flag & HA_REVERSE_SORT;
12641265
static const LEX_CSTRING lt={STRING_WITH_LEN(" < ") };
12651266
static const LEX_CSTRING gt={STRING_WITH_LEN(" > ") };
12661267
static const LEX_CSTRING le={STRING_WITH_LEN(" <= ") };
@@ -1338,12 +1339,12 @@ bool ha_federatedx::create_where_from_key(String *to,
13381339

13391340
if (i > 0) /* end key */
13401341
{
1341-
if (tmp.append(le))
1342+
if (tmp.append(reverse ? ge : le))
13421343
goto err;
13431344
}
13441345
else /* start key */
13451346
{
1346-
if (tmp.append(gt))
1347+
if (tmp.append(reverse ? lt : gt))
13471348
goto err;
13481349
}
13491350

@@ -1358,7 +1359,7 @@ bool ha_federatedx::create_where_from_key(String *to,
13581359
case HA_READ_KEY_OR_NEXT:
13591360
DBUG_PRINT("info", ("federatedx HA_READ_KEY_OR_NEXT %d", i));
13601361
if (emit_key_part_name(&tmp, key_part) ||
1361-
tmp.append(ge) ||
1362+
tmp.append(reverse ? le : ge) ||
13621363
emit_key_part_element(&tmp, key_part, needs_quotes, 0, ptr,
13631364
part_length))
13641365
goto err;
@@ -1368,7 +1369,7 @@ bool ha_federatedx::create_where_from_key(String *to,
13681369
if (store_length >= length)
13691370
{
13701371
if (emit_key_part_name(&tmp, key_part) ||
1371-
tmp.append(lt) ||
1372+
tmp.append(reverse ? gt : lt) ||
13721373
emit_key_part_element(&tmp, key_part, needs_quotes, 0, ptr,
13731374
part_length))
13741375
goto err;
@@ -1378,7 +1379,7 @@ bool ha_federatedx::create_where_from_key(String *to,
13781379
case HA_READ_KEY_OR_PREV:
13791380
DBUG_PRINT("info", ("federatedx HA_READ_KEY_OR_PREV %d", i));
13801381
if (emit_key_part_name(&tmp, key_part) ||
1381-
tmp.append(le) ||
1382+
tmp.append(reverse ? ge : le) ||
13821383
emit_key_part_element(&tmp, key_part, needs_quotes, 0, ptr,
13831384
part_length))
13841385
goto err;

0 commit comments

Comments
 (0)