@@ -107,6 +107,9 @@ class QueryResultTypeWalker extends SqlWalker
107107 /** @var bool */
108108 private $ hasGroupByClause ;
109109
110+ /** @var bool */
111+ private $ hasCondition ;
112+
110113 /**
111114 * @param Query<mixed> $query
112115 */
@@ -135,6 +138,7 @@ public function __construct($query, $parserResult, array $queryComponents)
135138 $ this ->nullableQueryComponents = [];
136139 $ this ->hasAggregateFunction = false ;
137140 $ this ->hasGroupByClause = false ;
141+ $ this ->hasCondition = false ;
138142
139143 // The object is instantiated by Doctrine\ORM\Query\Parser, so receiving
140144 // dependencies through the constructor is not an option. Instead, we
@@ -590,6 +594,8 @@ public function walkOrderByItem($orderByItem)
590594 */
591595 public function walkHavingClause ($ havingClause )
592596 {
597+ $ this ->hasCondition = true ;
598+
593599 return $ this ->marshalType (new MixedType ());
594600 }
595601
@@ -994,6 +1000,8 @@ public function walkUpdateItem($updateItem)
9941000 */
9951001 public function walkWhereClause ($ whereClause )
9961002 {
1003+ $ this ->hasCondition = true ;
1004+
9971005 return $ this ->marshalType (new MixedType ());
9981006 }
9991007
@@ -1281,7 +1289,10 @@ public function walkResultVariable($resultVariable)
12811289 */
12821290 private function addScalar ($ alias , Type $ type ): void
12831291 {
1284- if ($ type instanceof UnionType) {
1292+ // Since we don't check the condition inside the WHERE or HAVING
1293+ // conditions, we cannot be sure all the union types are correct.
1294+ // For exemple, a condition `WHERE foo.bar IS NOT NULL` could be added.
1295+ if ($ this ->hasCondition && $ type instanceof UnionType) {
12851296 $ type = TypeUtils::toBenevolentUnion ($ type );
12861297 }
12871298
0 commit comments