最近在使用starrocks时,发现一个比较离谱的、基础的计算引擎bug。进行数据统计时,同时使用count(1) 和count(distinct),count(1)的统计结果会受count(distinct)的影响,返回的是distinct去重后的记录数。理论上,返回的应该是原始数据集的记录数。
以下是现象描述:
select语句同时使用count(1),count(distinct account_id),count(1)的统计值与count(distinct account_id)一致。刚开始我还以是入库异常了,一个账号只入了一条流水记录。
##
mysql> select date,count(1),count(distinct account_id) from dcl.mytable where date='2023-04-24' group by date order by date;
+------------+-------------+----------------------------+
| date | count(date) | count(DISTINCT account_id) |
+------------+-------------+----------------------------+
| 2023-04-24 | 31 | 31 |
+------------+-------------+----------------------------+
1 row in set (0.02 sec)
但我对表,只进行count(1)时,发现统计值是正常的。
mysql> select date,count(1) from dcl.mytable where date='2023-04-24' group by date order by date;
+------------+----------+
| date | count(1) |
+------------+----------+
| 2023-04-24 | 111681 |
+------------+----------+
为了获取到正确的记录统计值,我们可以将count(1)改为sum(1),这时sum(1)返回的统计值符合预期。
mysql> select date,count(1),sum(1),count(distinct account_id) from dcl.mytable where date='2023-04-24' group by date order by date;
+------------+----------+--------+----------------------------+
| date | count(1) | sum(1) | count(DISTINCT account_id) |
+------------+----------+--------+----------------------------+
| 2023-04-24 | 31 | 111681 | 31 |
+------------+----------+--------+----------------------------+
1 row in set (0.03 sec)
在同一个语句中,如果有多个count(distinct),那count(1)返回的统计值是符合预期的。
mysql> select date,count(1),sum(1),count(distinct account_id),count(distinct geo) from dcl.mytable where date='2023-04-24' group by date order by date;
+------------+----------+--------+----------------------------+---------------------+
| date | count(1) | sum(1) | count(DISTINCT account_id) | count(DISTINCT geo) |
+------------+----------+--------+----------------------------+---------------------+
| 2023-04-24 | 111681 | 111681 | 31 | 236 |
+------------+----------+--------+----------------------------+---------------------+
1 row in set (0.06 sec)
这个bug只会在count(1)与count(distinct)同时一对一出现时才会出现。猜测是生成执行计划时,stage划分产生异常。该问题已反馈到社区,应该会在近期修复。在此期间我们可以使用sum(1) 替代count(1)以规避这个坑。