@@ -106,6 +106,9 @@ class QueryResultTypeWalker extends SqlWalker
106106	/** @var bool */ 
107107	private  $ hasGroupByClause
108108
109+ 	/** @var bool */ 
110+ 	private  $ hasCondition
111+ 
109112	/** 
110113	 * @param Query<mixed> $query 
111114	 */ 
@@ -134,6 +137,7 @@ public function __construct($query, $parserResult, array $queryComponents)
134137		$ this nullableQueryComponents  = [];
135138		$ this hasAggregateFunction  = false ;
136139		$ this hasGroupByClause  = false ;
140+ 		$ this hasCondition  = false ;
137141
138142		// The object is instantiated by Doctrine\ORM\Query\Parser, so receiving 
139143		// dependencies through the constructor is not an option. Instead, we 
@@ -589,6 +593,8 @@ public function walkOrderByItem($orderByItem)
589593	 */ 
590594	public  function  walkHavingClause ($ havingClause
591595	{
596+ 		$ this hasCondition  = true ;
597+ 
592598		return  $ this marshalType (new  MixedType ());
593599	}
594600
@@ -993,6 +999,8 @@ public function walkUpdateItem($updateItem)
993999	 */ 
9941000	public  function  walkWhereClause ($ whereClause
9951001	{
1002+ 		$ this hasCondition  = true ;
1003+ 
9961004		return  $ this marshalType (new  MixedType ());
9971005	}
9981006
@@ -1280,7 +1288,10 @@ public function walkResultVariable($resultVariable)
12801288	 */ 
12811289	private  function  addScalar ($ aliasType $ typevoid 
12821290	{
1283- 		if  ($ typeinstanceof  UnionType) {
1291+ 		// Since we don't check the condition inside the WHERE or HAVING 
1292+ 		// conditions, we cannot be sure all the union types are correct. 
1293+ 		// For exemple, a condition `WHERE foo.bar IS NOT NULL` could be added. 
1294+ 		if  ($ this hasCondition  && $ typeinstanceof  UnionType) {
12841295			$ typetoBenevolentUnion ($ type
12851296		}
12861297
0 commit comments