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

[5.2] Cross join support for query builder #12950

Merged
merged 1 commit into from
Apr 1, 2016
Merged

[5.2] Cross join support for query builder #12950

merged 1 commit into from
Apr 1, 2016

Conversation

raphaelsaunier
Copy link
Contributor

This PR adds a crossJoin($table) method to Laravel's query builder to support SQL cross joins.

Cross joins are useful in a variety of situations and they are supported by MySQL, PostgreSQL and SQLite so I think it makes sense to add them to the query builder.

@GrahamCampbell
Copy link
Member

What about SQL Server?

@raphaelsaunier
Copy link
Contributor Author

Looks like SQL Server supports them too, with no difference in the syntax: https://technet.microsoft.com/en-us/library/ms190690(v=sql.105).aspx

Verified

This commit was signed with the committer’s verified signature.
Eomm Manuel Spigolon
@taylorotwell taylorotwell merged commit 99bc4c3 into laravel:5.2 Apr 1, 2016
@xavadu
Copy link

xavadu commented Apr 14, 2016

I looks to broke the old way to crossjoin

 ->join('tableA'', tableA.column1', '=', 'tableB.column2', 'cross')

The output query of this, after upgrade from 5.2.20 to 5.2.29 doesn't include the ON statement, it broke the query.

Also the new crossjoin syntax doesn't support an ON statement.

@shahidnajam
Copy link

As i see the changes from this patch

        // Cross joins generate a cartesian product between the first table and the joined
        // table. Since they don't expect any "on" clauses to perform the join, we just
        // just append the SQL statement and jump to the next iteration of this loop.
        if ($type === 'cross') {
            $sql[] = "cross join $table";

            continue;
        }

@GrahamCampbell I recommend to add ON statement support to cross join type. because I have used cross join ON statement with previous latest of Laravel and now it return query without ON statement.

5.2.x => where new (x) shouldn't break any functionality with old(x)

@raphaelsaunier
Copy link
Contributor Author

@xavadu @shahidnajam interesting, so there are use cases that require adding ON clauses to a cross join, after all… I did a bit of research on this and, while the documentation on cross joins is relatively scarce, I didn't find any examples that used them. Some even explicitly stated that they weren't supported.

I'll submit a PR with an new test case and fix for this asap!

I'll also do some research on this, but based on the output from MySQL's EXPLAIN, it seems that the following query:

select * from tableA cross join tableB on tableA.column1 = tableB.column2;

Is equivalent to the following:

select * from tableA cross join tableB where tableA.column1 = tableB.column2;

So you could use that as a hotfix?

Also, @shahidnajam, cross joins were never officially supported (there's no reference to them in the code, documentation and tests), so you can't really call that a regression.

@xavadu
Copy link

xavadu commented Apr 14, 2016

@raphaelsaunier Thanks for the answer, we move to use where, @shahidnajam did some benchmark and it have the same performance that using ON (we are working with huge amount of data).

The unexpected was that the app got broken after update a patch version upgrade.

@raphaelsaunier
Copy link
Contributor Author

@xavadu Ok, cool! Sorry about that guys! At least it's good to know that you're getting the same performance results as I did.

Can you take a quick look at the updated test case here and let me know if it solves your use case?

raphaelsaunier@85b627d

@shahidnajam
Copy link

I looked at test case. It is OK. Thank you!

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

Successfully merging this pull request may close these issues.

None yet

5 participants