DDC-593: Subquery parenthesis omitted in generated SQL #725

Closed
opened 2026-01-22 12:48:11 +01:00 by admin · 7 comments
Owner

Originally created by @doctrinebot on GitHub (May 16, 2010).

Jira issue originally created by user jkleijn:

$dQuery = $this->_em->createQuery(
'SELECT p FROM entity\system\Group p WHERE (p.lft >= (SELECT t.lft FROM entity\system\Group t WHERE t.name = :name)) AND (p.rgt <= (SELECT t2.rgt FROM entity\system\Group t2 WHERE t2.name = :name))');

As you see this includes brackets around the subqueries.

var_dump($dQuery->getSQL());

SELECT s0_.level AS level0, s0_.lft AS lft1, s0_.rgt AS rgt2, s0_.id AS id3, s0_.name AS name4, s0_.description AS description5 FROM system_group s0_ WHERE (s0_.lft >= SELECT s1_.lft FROM system_group s1_ WHERE s1_.name = ?) AND (s0_.rgt <= SELECT s2_.rgt FROM system_group s2_ WHERE s2_.name = ?)

Brackets gone, resulting in:

'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT s1_.lft FROM system_group s1_ WHERE s1_.name = 'root') AND (s0_.rgt <= SE' at line 1' in /usr/share/php/lib/Doctrine/DBAL/Connection.php:566

Brackets added and executed against database

SELECT s0_.level AS level0, s0_.lft AS lft1, s0_.rgt AS rgt2, s0_.id AS id3, s0_.name AS name4, s0_.description AS description5 FROM system_group s0_ WHERE (s0_.lft >= (SELECT s1_.lft FROM system_group s1_ WHERE s1_.name = "root")) AND (s0_.rgt <= (SELECT s2_.rgt FROM system_group s2_ WHERE s2_.name = "root"))

Works (MySQL).

Originally created by @doctrinebot on GitHub (May 16, 2010). Jira issue originally created by user jkleijn: $dQuery = $this->_em->createQuery( 'SELECT p FROM entity\system\Group p WHERE (p.lft >= (SELECT t.lft FROM entity\system\Group t WHERE t.name = :name)) AND (p.rgt <= (SELECT t2.rgt FROM entity\system\Group t2 WHERE t2.name = :name))'); As you see this includes brackets around the subqueries. var_dump($dQuery->getSQL()); SELECT s0_.level AS level0, s0_.lft AS lft1, s0_.rgt AS rgt2, s0_.id AS id3, s0_.name AS name4, s0_.description AS description5 FROM system_group s0_ WHERE (s0_.lft >= SELECT s1_.lft FROM system_group s1_ WHERE s1_.name = ?) AND (s0_.rgt <= SELECT s2_.rgt FROM system_group s2_ WHERE s2_.name = ?) Brackets gone, resulting in: 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT s1_.lft FROM system_group s1_ WHERE s1_.name = 'root') AND (s0_.rgt <= SE' at line 1' in /usr/share/php/lib/Doctrine/DBAL/Connection.php:566 Brackets added and executed against database SELECT s0_.level AS level0, s0_.lft AS lft1, s0_.rgt AS rgt2, s0_.id AS id3, s0_.name AS name4, s0_.description AS description5 FROM system_group s0_ WHERE (s0_.lft >= (SELECT s1_.lft FROM system_group s1_ WHERE s1_.name = "root")) AND (s0_.rgt <= (SELECT s2_.rgt FROM system_group s2_ WHERE s2_.name = "root")) Works (MySQL).
admin added the Bug label 2026-01-22 12:48:11 +01:00
admin closed this issue 2026-01-22 12:48:11 +01:00
Author
Owner

@doctrinebot commented on GitHub (May 16, 2010):

Comment created by jkleijn:

Double brackets in the DQL results in

"exception 'Doctrine\ORM\Query\QueryException' with message '[Syntax Error] line 0, col 62: Error: Expected Literal, got 'SELECT'' in /usr/share/php/lib/Doctrine/ORM/Query/QueryException.php:42

@doctrinebot commented on GitHub (May 16, 2010): Comment created by jkleijn: Double brackets in the DQL results in "exception 'Doctrine\ORM\Query\QueryException' with message '[Syntax Error] line 0, col 62: Error: Expected Literal, got 'SELECT'' in /usr/share/php/lib/Doctrine/ORM/Query/QueryException.php:42
Author
Owner

@doctrinebot commented on GitHub (May 16, 2010):

Comment created by romanb:

This might be caused by recent AST optimizations and thus I suspect this to be a regression.

@doctrinebot commented on GitHub (May 16, 2010): Comment created by romanb: This might be caused by recent AST optimizations and thus I suspect this to be a regression.
Author
Owner

@doctrinebot commented on GitHub (May 16, 2010):

Comment created by jkleijn:

Is there a workaround (other than not using a subquery)?

@doctrinebot commented on GitHub (May 16, 2010): Comment created by jkleijn: Is there a workaround (other than not using a subquery)?
Author
Owner

@doctrinebot commented on GitHub (May 16, 2010):

Comment created by romanb:

Well, a workaround for any DQL issues that is always available is a NativeQuery (createNativeQuery). In essence, a DQL query is just a high-level abstraction for a native SQL query + a ResultSetMapping. http://www.doctrine-project.org/projects/orm/2.0/docs/reference/native-sql/en#native-sql

The resulting objects from a native query are still fully managed and all, so its just a difference in query abstraction.

Nevertheless, this should be fixed soon.

@doctrinebot commented on GitHub (May 16, 2010): Comment created by romanb: Well, a workaround for any DQL issues that is always available is a NativeQuery (createNativeQuery). In essence, a DQL query is just a high-level abstraction for a native SQL query + a ResultSetMapping. http://www.doctrine-project.org/projects/orm/2.0/docs/reference/native-sql/en#native-sql The resulting objects from a native query are still fully managed and all, so its just a difference in query abstraction. Nevertheless, this should be fixed soon.
Author
Owner

@doctrinebot commented on GitHub (May 16, 2010):

Comment created by romanb:

Reproduced this successfully and already have a potential fix. Might not be a regression after all but a bug nevertheless.

@doctrinebot commented on GitHub (May 16, 2010): Comment created by romanb: Reproduced this successfully and already have a potential fix. Might not be a regression after all but a bug nevertheless.
Author
Owner

@doctrinebot commented on GitHub (May 16, 2010):

Comment created by romanb:

Should be fixed now in HEAD (doctrine2/master).

@doctrinebot commented on GitHub (May 16, 2010): Comment created by romanb: Should be fixed now in HEAD (doctrine2/master).
Author
Owner

@doctrinebot commented on GitHub (May 16, 2010):

Issue was closed with resolution "Fixed"

@doctrinebot commented on GitHub (May 16, 2010): Issue was closed with resolution "Fixed"
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#725