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

Support for set operators: Intersect, Union, Except, in TypeOrm #8584

Open
firmbase-tal opened this issue Jan 30, 2022 · 3 comments
Open

Support for set operators: Intersect, Union, Except, in TypeOrm #8584

firmbase-tal opened this issue Jan 30, 2022 · 3 comments

Comments

@firmbase-tal
Copy link

Feature Description

The Problem

I would like for support to be added for the INTERSECT, UNION, and EXCEPT operators for Postgres/MySQL.
Unfortunately, there is currently no way to build such queries, without using raw queries.
These operators are very situationally useful, and would enhance querying capabilities for TypeORM.

The Solution

I think the best way to implement the change would be to extend the functionality of the SelectQueryBuilder to include these operators as functions:

for example, for given sql:

select id as t1_id from table1
INTERSECT
select table1_id as t1_id from table2

either:

query1 = manager.createQueryBuilder().select('id', 't1_id').from('table1');
query2 = manager.createQueryBuilder().select('table1_id', 't1_id').from('table2');
query = manager.createQueryBuilder().intersect([query1, query2])

or:

query = manager.createQueryBuilder().select('id', 't1_id').from('table1').intersect((subQuery) => subQuery.select('table1_id', 't1_id').from('table2'))

Considered Alternatives

For INTERSECT, I considered combining inner joins with distinct.
For UNIONS, I created two different queries, and appended their results in the back end.
For EXCEPT, I have yet to encounter a use case while working with TypeORM, therefore I have no good idea for a workaround.

There is a workaround that I picked up from a related issue from 2018, which is linked as additional context.

Additional Context

The feature has been partially requested and closed in the past, without explanation about the rejection: #2992

Relevant Database Driver(s)

DB Type Relevant
aurora-data-api no
aurora-data-api-pg no
better-sqlite3 no
cockroachdb no
cordova no
expo no
mongodb no
mysql yes
nativescript no
oracle yes
postgres yes
react-native no
sap no
sqlite yes
sqlite-abstract no
sqljs no
sqlserver no

Are you willing to resolve this issue by submitting a Pull Request?

  • ✅ Yes, I have the time, and I know how to start.
  • ✖️ Yes, I have the time, but I don't know how to start. I would need guidance.
  • ✖️ No, I don’t have the time, but I can support (using donations) development.
  • ✖️ No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.
@Ginden
Copy link
Collaborator

Ginden commented Feb 3, 2022

This is blocked by #8466

It can be workarounded using CTEs after merging #8534

@alumni
Copy link
Contributor

alumni commented Jun 20, 2022

The full syntax for set operators is <UNION | EXCEPT | INTERSECT> [ALL | DISTINCT]. DISTINCT is implicit in most dialects, but ALL needs to be explicitly added if you don't want to filter duplicate results. Most dialects only support UNION ALL (MS SqlServer, Oracle, SAP HANA, Sqlite don't have EXCEPT ALL or INTERSECT ALL in their docs), while some dialects use MINUS instead of EXCEPT (Oracle only?)

MariaDB:

MS SqlServer

Oracle

Postgres:

SAP HANA:

Sqlite

@john21323
Copy link

Is there anything new here? 😳😳😳

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants