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 CROSS JOIN and CROSS JOIN LATERAL #1112

Open
mertalev opened this issue Aug 10, 2024 · 7 comments
Open

Support CROSS JOIN and CROSS JOIN LATERAL #1112

mertalev opened this issue Aug 10, 2024 · 7 comments
Labels
api Related to library's API enhancement New feature or request mssql Related to MS SQL Server (MSSQL) mysql Related to MySQL oracle Related to Oracle postgres Related to PostgreSQL sqlite Related to sqlite

Comments

@mertalev
Copy link

mertalev commented Aug 10, 2024

Kysely supports LEFT JOIN LATERAL and INNER JOIN LATERAL, but doesn't support lateral subqueries of the syntax SELECT * FROM a, LATERAL ( ) l, or the equivalent SELECT * FROM a CROSS JOIN LATERAL ( ) l. #140 discussed the reference of multiple tables in selectFrom as an alternative to cross joins, but this doesn't work for subqueries as far as I can tell.

This is the kind of query I'd like to use this feature for:

WITH RECURSIVE cte AS (
  (
    SELECT city, "assetId"
    FROM exif
    ORDER BY city
    LIMIT 1
  )

  UNION ALL

  SELECT l.city, l."assetId"
  FROM cte c
  CROSS JOIN LATERAL (
    SELECT city, "assetId"
    FROM exif
    WHERE city > c.city
    ORDER BY city
    LIMIT 1
  )  l
)
select "assets".*, jsonb_strip_nulls(to_jsonb(exif)) as "exifInfo"
from "assets"
inner join "exif" on "assets"."id" = "exif"."assetId"
inner join "cte" on "assets"."id" = "cte"."assetId";
@mertalev mertalev changed the title Support lateral subqueries / cross joins Support lateral subqueries Aug 10, 2024
@koskimas
Copy link
Member

koskimas commented Aug 10, 2024

Doesn't leftJoinLateral work just as well in this case since you're only joining max 1 row (limit 1)?

@mertalev
Copy link
Author

mertalev commented Aug 10, 2024

In the example I shared with the exif and assets tables both having 2 million rows (one-one), using LEFT JOIN LATERAL ... ON true took at least 60s - I canceled it at that point. CROSS JOIN LATERAL took 7ms.

@koskimas
Copy link
Member

How about inner join on true? Isn't that 100% equivalent to cross join?

@koskimas
Copy link
Member

But I guess we should add cross join and cross join lateral to Kysely.

@mertalev
Copy link
Author

How about inner join on true? Isn't that 100% equivalent to cross join?

Nice suggestion! This one does have the same query plan and performance as cross join.

@igalklebanov igalklebanov added enhancement New feature or request postgres Related to PostgreSQL api Related to library's API mysql Related to MySQL sqlite Related to sqlite labels Aug 11, 2024
@igalklebanov igalklebanov changed the title Support lateral subqueries Support CROSS JOIN and CROSS JOIN LATERAL Aug 11, 2024
@igalklebanov igalklebanov added mssql Related to MS SQL Server (MSSQL) oracle Related to Oracle labels Aug 11, 2024
@bakasmarius
Copy link

How about inner join on true? Isn't that 100% equivalent to cross join?

Probably no because on MS SQL Server 2016 I get this error when I try to run inner join MyTable as mt on true:
SQL Error [4145] [S0001]: An expression of non-boolean type specified in a context where a condition is expected

It works fine though if I replace on true with on 1 = 1.

Looks like .crossJoin('MyTable') would be a more elegant solution than .innerJoin('MyTable as mt', (join) => join.on(sql.val(1), '=', 1))).

@drew-marsh
Copy link

MS SQL's cross join does not have an on clause. This PR addresses on-less joins as a solution for mssql apply statements #1074

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Related to library's API enhancement New feature or request mssql Related to MS SQL Server (MSSQL) mysql Related to MySQL oracle Related to Oracle postgres Related to PostgreSQL sqlite Related to sqlite
Projects
None yet
Development

No branches or pull requests

5 participants