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

IN predicate returns wrong results when used with GROUP BY ... HAVING subquery #4365

Closed
knutwannheden opened this issue Sep 17, 2022 · 5 comments · Fixed by dolthub/go-mysql-server#1368 or dolthub/go-mysql-server#1379
Assignees
Labels
bug Something isn't working good repro Easily reproducible bugs sql Issue with SQL

Comments

@knutwannheden
Copy link

The example data looks like this:

create table t(i int, t varchar(2));

insert into t values (1, 'a'), (1, 'a2'),
                     (2, 'b'),
                     (3, 'c'), (3, 'c2'),
                     (4, 'd'),
                     (5, 'e'), (5, 'e2');

The following query is expected to return 2 tuples ((2, 'b') and (4, 'd')) but only returns one of them ((4, 'd')):

select *
from t
where i in (select i
            from t
            group by i
            having count(1) = 1)
;

When the HAVING clause is changed to count(1) != 1 the query returns the same result, even though it should have returned the other 6 tuples (where i in (1, 3, 5)):

select *
from t
where i in (select i
            from t
            group by i
            having count(1) != 1)

I checked that this isn't an aliasing issue, so I don't know what is going on here.

I can change the query to use a CTE or a JOIN with the subquery and then the result returned is correct.

@timsehn timsehn added bug Something isn't working sql Issue with SQL labels Sep 19, 2022
@fulghum
Copy link
Contributor

fulghum commented Sep 19, 2022

Thanks for the bug report Knut. I was able to reproduce this with dolt version 0.41.4. We'll debug through this one and see what's going on.

@jennifersp jennifersp self-assigned this Oct 27, 2022
@fulghum fulghum added the good repro Easily reproducible bugs label Oct 27, 2022
@fulghum
Copy link
Contributor

fulghum commented Nov 4, 2022

Great work on this fix @jennifersp!

@knutwannheden – thanks again for helping us find all these great issues to fix! 🙏 Very appreciated! This fix will go out with the next Dolt release. Let us know if you find anything else odd for us to dig into.

@knutwannheden
Copy link
Author

AFAICT the PR was reverted again. Should this issue be reopened again?

@jennifersp
Copy link
Contributor

Hi @knutwannheden, ah yes I'll re-open it for now. It needs fix for the tests only. I'll merge it back again with the tests fixed tomorrow. Sorry about that.

@jennifersp
Copy link
Contributor

Hey @knutwannheden, the fix has been merged with fixed tests, it will be included in the next Dolt release. Thank you again for helping us find this bug! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good repro Easily reproducible bugs sql Issue with SQL
Projects
None yet
4 participants