lo icon indicating copy to clipboard operation
lo copied to clipboard

Bug: Intersect should only return distinct values

Open ghostsquad opened this issue 2 years ago • 5 comments

Lodash describes this operation as returning unique entries https://lodash.com/docs/#intersection

Also described in SQL: https://www.postgresql.org/docs/9.4/queries-union.html

INTERSECT returns all rows that are both in the result of query1 and in the result of query2. Duplicate rows are eliminated unless INTERSECT ALL is used.

EXCEPT returns all rows that are in the result of query1 but not in the result of query2. (This is sometimes called the difference between two queries.) Again, duplicates are eliminated unless EXCEPT ALL is used.

https://docs.microsoft.com/en-us/sql/t-sql/language-elements/set-operators-except-and-intersect-transact-sql?view=sql-server-ver15

ghostsquad avatar Mar 18 '22 04:03 ghostsquad

So you are suggesting renaming Intersect into IntersectAll then adding Intersect that returns unique values?

Looks good.

Those intersection helpers are getting a little bit complicated. I think we should use the same naming as SQL. What about coding some LeftJoin, InnerJoin, FullOuterJoin...

I am also wondering if we should accept variadic arguments? Such as Intersect(collection1, collection2, collection3, collection4)

samber avatar Mar 20 '22 01:03 samber

Ya, I like intersect, and intersectAll.

Variadic args seems useful. I'll update/create some more PRs

ghostsquad avatar Mar 20 '22 17:03 ghostsquad

This bug should be fixed.

wirekang avatar Apr 30 '22 10:04 wirekang

Changing what the function fundamentally does, without changing the signature, is going to cause grievances that will not be caught by the compiler, for the current consumers.

THOUSAND-SKY avatar Sep 08 '22 15:09 THOUSAND-SKY

Changing what the function fundamentally does, without changing the signature, is going to cause grievances that will not be caught by the compiler, for the current consumers.

Bugfixes change behavior. That's by design.

The expectation of this function, as mentioned in the description/commit message is that lodash intersect returns unique results. intersect in SQL also returns unique results. This should not differ from the current established expectations.

ghostsquad avatar Sep 08 '22 20:09 ghostsquad