-
Notifications
You must be signed in to change notification settings - Fork 11.4k
[5.7] Fix Postgres grammar when using union queries #27589
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
Conversation
@@ -873,7 +901,7 @@ public function testUnionAggregate() | |||
$builder->getProcessor()->shouldReceive('processSelect')->once(); | |||
$builder->from('posts')->select('id')->union($this->getMySqlBuilder()->from('videos')->select('id'))->count(); | |||
|
|||
$expected = 'select count(*) as aggregate from (select * from "posts" union select * from "videos") as "temp_table"'; | |||
$expected = 'select count(*) as aggregate from ((select * from "posts") union (select * from "videos")) as "temp_table"'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR now adds them always, even if not necessary. But I guess this is fine? 🤷♀️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, just only a select clause is not necessary, but if a LIMIT
or GROUP BY
clause is added, parentheses are necessary in Postgres. I guess that this behavior is not a problem. 😄
PS: can confirm the problem; as soon as |
Any chance this can get merged into lumen as well? Having same issue and lumen 5.7 doesn't produce the parentheses. |
@jnguyen32 I don't see how this affects lumen? Lumen just uses the DB component from Laravel. It doesn't has custom query builder functionality. |
Laravel's QueryBuilder doesn't do unions in postgres until v5.7. Since we run v5.5, we need to manually piece the union sql string together. See: laravel/framework#27589
Laravel's QueryBuilder doesn't do unions in postgres until v5.7. Since we run v5.5, we need to manually piece the union sql string together. See: laravel/framework#27589
Laravel's QueryBuilder doesn't do unions in postgres until v5.7. Since we run v5.5, we need to manually piece the union sql string together. See: laravel/framework#27589
Laravel's QueryBuilder doesn't do unions in postgres until v5.7. Since we run v5.5, we need to manually piece the union sql string together. See: laravel/framework#27589
Laravel's QueryBuilder doesn't do unions in postgres until v5.7. Since we run v5.5, we need to manually piece the union sql string together. See: laravel/framework#27589
Laravel's QueryBuilder doesn't do unions in postgres until v5.7. Since we run v5.5, we need to manually piece the union sql string together. See: laravel/framework#27589
Currently Postgres grammar creates SQL invalid when query has
union
orunion all
, usinglimit
and/oroffset
clauses in its subqueries.Postgres syntax needs that subquery is between parentheses:
Wrong
Correct
This approach is based in PR #17890.