Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

count(distinct col) renturn null #4680

Closed
yidwf opened this issue Mar 10, 2020 · 7 comments · Fixed by #6118
Closed

count(distinct col) renturn null #4680

yidwf opened this issue Mar 10, 2020 · 7 comments · Fixed by #6118

Comments

@yidwf
Copy link

yidwf commented Mar 10, 2020

mybatis:int getMsgDay(@param("account_uid") String account_uid);

SQL:SELECT COUNT(DISTINCT DATE_FORMAT(create_time,'%Y%m%d')) as count FROM table_name
WHERE account_uid = #{account_uid}

if there is no data, then return null, not return 0, it case an exection:
org.apache.ibatis.binding.BindingException: Mapper method 'com.*.getMsgDay attempted to return null from a method with a primitive return type (int).

@kimmking
Copy link
Member

I will try to reproduce this issue later, maybe tomorrow.

@kimmking
Copy link
Member

I reproduced it. It's a bug.

@kimmking
Copy link
Member

AggregationType(MAX, MIN, SUM, COUNT, AVG) + Distinct all have this problem.
I will fix later.

@kimmking
Copy link
Member

kimmking commented Jun 18, 2020

I will fix to show:

mysql> select avg(distinct id),name from test;
+--------------------------------+------+
| AGGREGATION_DISTINCT_DERIVED_0 | name |
+--------------------------------+------+
|                           NULL | NULL |
+--------------------------------+------+
1 row in set (0.04 sec)

mysql> select sum(distinct id),name from test;
+--------------------------------+------+
| AGGREGATION_DISTINCT_DERIVED_0 | name |
+--------------------------------+------+
|                           NULL | NULL |
+--------------------------------+------+
1 row in set (0.02 sec)

mysql> select count(distinct id),name from test;
+--------------------------------+------+
| AGGREGATION_DISTINCT_DERIVED_0 | name |
+--------------------------------+------+
|                              0 | NULL |
+--------------------------------+------+
1 row in set (0.01 sec)

mysql> select min(distinct id),name from test;
+--------------------------------+------+
| AGGREGATION_DISTINCT_DERIVED_0 | name |
+--------------------------------+------+
|                           NULL | NULL |
+--------------------------------+------+
1 row in set (0.02 sec)

mysql>
mysql> select max(distinct id),name from test;
+--------------------------------+------+
| AGGREGATION_DISTINCT_DERIVED_0 | name |
+--------------------------------+------+
|                           NULL | NULL |
+--------------------------------+------+
1 row in set (0.01 sec)


mysql> select max(distinct id) as a,name from test;
+---+------+
| a | name |
+---+------+
| NULL | NULL |
+---+------+
1 row in set (0.02 sec)

mysql> select count(distinct id) as a,name from test;
+---+------+
| a | name |
+---+------+
| 0 | NULL |
+---+------+
1 row in set (0.01 sec)

mysql> select avg(distinct id) as a,name from test;
+---+------+
| a | name |
+---+------+
| NULL | NULL |
+---+------+
1 row in set (0.01 sec)

@changchangjie
Copy link

+1

@tristaZero
Copy link
Contributor

+1

Hi this issue has been fixed already. If it is convenient, you can give it a test on the master branch. Thx.

@changchangjie
Copy link

+1

Hi this issue has been fixed already. If it is convenient, you can give it a test on the master branch. Thx.

I'm using version 4.0.0-RC3. Which version should I upgrade to

Malaydewangan09 added a commit to Malaydewangan09/shardingsphere that referenced this issue Oct 27, 2024
Malaydewangan09 added a commit to Malaydewangan09/shardingsphere that referenced this issue Oct 28, 2024
Malaydewangan09 added a commit to Malaydewangan09/shardingsphere that referenced this issue Oct 28, 2024
strongduanmu pushed a commit that referenced this issue Oct 31, 2024
* fix: SQL COUNT with GROUP BY to prevent incorrect row returns

* test: Add test cases for empty result with GROUP BY and ORDER BY

* fix: update db types and scenario type for e2e test case

* fix: update column names for e2e test

* fix: fix unit tests for empty result set

* test: add e2e tests for issue #4680

* fix: fix e2e tests for issue #4680

* update e2e tests for isssue #4680

* fix: fix failing checks

* fix: update conditions for group by and aggregate functions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants