Details
-
Bug
-
Status: Closed (View Workflow)
-
Critical
-
Resolution: Fixed
-
1.2.5
-
None
-
debian 9
-
2020-3, 2020-4, 2020-5
Description
bug not present in 1.1.7 and definitely present in 1.2.5
order by is not correctly processed for trivial queries
let's say we want to compute sales for region 'x' and 'y', for each of 3 business units 1,2 and 3 like this:
bu | x | y |
---|---|---|
1 | 10 | 12 |
2 | 11 | 11 |
3 | 12 | 10 |
when applying an order by clause, 'order by bu' or 'order by x' work but 'order by y' is ignored
drop table if exists t; |
create table t(a int, b varchar(10), c decimal(7,2)) engine=columnstore; |
insert into t(a,b,c) values |
(1,'x',10),(2,'x',11), (3, 'x', 12), |
(1,'y',12),(2,'y',11), (3, 'y', 10); |
|
-- order by column bu or x work fine
|
select
|
a as bu, |
sum(case when b='x' then c else null end) as 'x', |
sum(case when b='y' then c else null end) as 'y' |
from t |
group by bu |
order by bu /* or order by x */ |
;
|
|
-- order by column y produces a random ordering of rows
|
select
|
a as bu, |
sum(case when b='x' then c else null end) as 'x', |
sum(case when b='y' then c else null end) as 'y' |
from t |
group by bu |
order by y asc |
;
|
It looks like a bug in our fork code.
There are at least two workarounds though. First is to put the ORDER BY key column 'y' in the second place after the projected bu.
MariaDB [test]> select a as bu, sum(case when b='y' then c else null end) as 'y',sum(case when b='x' then c else null end) as 'x' from t group by bu order by y desc;
+------+-------+-------+
| bu | y | x |
+------+-------+-------+
| 1 | 12.00 | 10.00 |
| 2 | 11.00 | 11.00 |
| 3 | 10.00 | 12.00 |
+------+-------+-------+
3 rows in set (0.038 sec)
Another is to use CS internal sorting wrapping the original query into a subquery if it is possible:
MariaDB [test]> select * from (select a as bu, sum(case when b='x' then c else null end) as 'x', sum(case when b='y' then c else null end) as 'y' from t group by bu order by y asc limit 10) t;
+------+-------+-------+
| bu | x | y |
+------+-------+-------+
| 3 | 12.00 | 10.00 |
| 2 | 11.00 | 11.00 |
| 1 | 10.00 | 12.00 |
+------+-------+-------+
3 rows in set (0.028 sec)