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

RFC-0007 Bag Operators DISTINCT Inconsistencies #75

Open
johnedquinn opened this issue Apr 17, 2024 · 0 comments
Open

RFC-0007 Bag Operators DISTINCT Inconsistencies #75

johnedquinn opened this issue Apr 17, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@johnedquinn
Copy link
Member

I believe there are some inconsistencies with the wording of RFC-0007 with regards to distinctness.

From the RFC:

T1 UNION DISTINCT T2     = DISTINCT(SQL_UNION(T1, T2))
T1 INTERSECT DISTINCT T2 = DISTINCT(SQL_INTERSECT(T1, T2))
T1 EXCEPT DISTINCT T2    = DISTINCT(SQL_EXCEPT(T1, T2))

However, SQL doesn't actually operate this way. See this DB Fiddle Example for PostgreSQL. Please use the link and update the query to use both EXCEPT and EXCEPT ALL.

When you use EXCEPT (distinct), it will return the following table:

id
11
12

When you use EXCEPT ALL, it will return the following table:

id
10
11
12

As you can see, the use of DISTINCT does NOT perform the DISTINCT operation on the output of SQL_EXCEPT. It's a bit more complicated than that. Specifically, for EXCEPT DISTINCT, it executes DISTINCT on both inputs, then it runs EXCEPT on the new inputs. A similar thing occurs for INTERSECT.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant