[MDEV-22011] DISTINCT with JSON_ARRAYAGG gives wrong results Created: 2020-03-23 Updated: 2020-06-16 Resolved: 2020-06-12 |
|
| Status: | Closed |
| Project: | MariaDB Server |
| Component/s: | Optimizer |
| Affects Version/s: | 10.5 |
| Fix Version/s: | 10.5.4 |
| Type: | Bug | Priority: | Major |
| Reporter: | Varun Gupta (Inactive) | Assignee: | Varun Gupta (Inactive) |
| Resolution: | Fixed | Votes: | 0 |
| Labels: | None | ||
| Issue Links: |
|
||||||||||||||||
| Description |
|
As JSON_ARRAYAGG is implemented as a wrapper over Item_func_group_concat, The problem with DISTINCT in JSON_ARRAYAGG is that it expects the values read from the table for each record. It does not use the key but instead calls val_str() for all the arguments in the GROUP_CONCAT function. |
| Comments |
| Comment by Varun Gupta (Inactive) [ 2020-03-24 ] | ||||||||||||||||||
|
Patch | ||||||||||||||||||
| Comment by Sergei Petrunia [ 2020-06-07 ] | ||||||||||||||||||
|
Just an observation: DISTINCT modifier inside JSON_ARRAYAGG is not part of the standard. And MySQL 8 doesn't support this, either. | ||||||||||||||||||
| Comment by Sergei Petrunia [ 2020-06-07 ] | ||||||||||||||||||
|
... and the patch breaks something in ORDER BY handling for JSON_ARRAYAGG. Testcase:
Output:
and in the current 10.5, I get the correct output:
| ||||||||||||||||||
| Comment by Sergei Petrunia [ 2020-06-07 ] | ||||||||||||||||||
|
The above is a showstopper for this bug. I personally would not mind (actually I would prefer this) if the issue is resolved by | ||||||||||||||||||
| Comment by Alexey Botchkov [ 2020-06-08 ] | ||||||||||||||||||
|
I belive the DISTINCT can be helpful to some users in JSON_ARRAYAGG. | ||||||||||||||||||
| Comment by Varun Gupta (Inactive) [ 2020-06-08 ] | ||||||||||||||||||
|
I checked against 95da2113a050ad739fdaf60ee871329468a01554, we get incorrect results with ORDER BY in JSON_ARRAYAGG function.
This was fixed by | ||||||||||||||||||
| Comment by Varun Gupta (Inactive) [ 2020-06-08 ] | ||||||||||||||||||
|
So here is a case which does not work with ORDER BY
Well handling of NULL columns cannot work with the current implementation of JSON_ARRAYAGG | ||||||||||||||||||
| Comment by Varun Gupta (Inactive) [ 2020-06-09 ] | ||||||||||||||||||
|
So lets report a proper case here, the problem that exists in current 10.5 with distinct
Now lets force temp files be created (making sure data does not fit in memory)
This is the same problem that is reported in | ||||||||||||||||||
| Comment by Varun Gupta (Inactive) [ 2020-06-09 ] | ||||||||||||||||||
|
Now let us port the patch for and now running the above test
I get the correct output. | ||||||||||||||||||
| Comment by Varun Gupta (Inactive) [ 2020-06-09 ] | ||||||||||||||||||
|
But even with the above port, the NULLS are still not handled with DISTINCT
This is correct, now let try DISTINCT
So we get incorrect results with NULL value, even after the patch. | ||||||||||||||||||
| Comment by Varun Gupta (Inactive) [ 2020-06-09 ] | ||||||||||||||||||
|
Patches are pushed to the branch 10.5-varun2. | ||||||||||||||||||
| Comment by Varun Gupta (Inactive) [ 2020-06-09 ] | ||||||||||||||||||
|
Patch | ||||||||||||||||||
| Comment by Sergei Petrunia [ 2020-06-11 ] | ||||||||||||||||||
|
Review input: Ok to push after addressed. |