@@ -108,6 +108,9 @@ class QueryResultTypeWalker extends SqlWalker
108
108
/** @var bool */
109
109
private $ hasGroupByClause ;
110
110
111
+ /** @var bool */
112
+ private $ hasCondition ;
113
+
111
114
/**
112
115
* @param Query<mixed> $query
113
116
*/
@@ -136,6 +139,7 @@ public function __construct($query, $parserResult, array $queryComponents)
136
139
$ this ->nullableQueryComponents = [];
137
140
$ this ->hasAggregateFunction = false ;
138
141
$ this ->hasGroupByClause = false ;
142
+ $ this ->hasCondition = false ;
139
143
140
144
// The object is instantiated by Doctrine\ORM\Query\Parser, so receiving
141
145
// dependencies through the constructor is not an option. Instead, we
@@ -592,6 +596,8 @@ public function walkOrderByItem($orderByItem): string
592
596
*/
593
597
public function walkHavingClause ($ havingClause ): string
594
598
{
599
+ $ this ->hasCondition = true ;
600
+
595
601
return $ this ->marshalType (new MixedType ());
596
602
}
597
603
@@ -1016,6 +1022,8 @@ public function walkWhereClause($whereClause): string
1016
1022
*/
1017
1023
public function walkConditionalExpression ($ condExpr ): string
1018
1024
{
1025
+ $ this ->hasCondition = true ;
1026
+
1019
1027
return $ this ->marshalType (new MixedType ());
1020
1028
}
1021
1029
@@ -1303,7 +1311,10 @@ public function walkResultVariable($resultVariable): string
1303
1311
*/
1304
1312
private function addScalar ($ alias , Type $ type ): void
1305
1313
{
1306
- if ($ type instanceof UnionType) {
1314
+ // Since we don't check the condition inside the WHERE or HAVING
1315
+ // conditions, we cannot be sure all the union types are correct.
1316
+ // For exemple, a condition `WHERE foo.bar IS NOT NULL` could be added.
1317
+ if ($ this ->hasCondition && $ type instanceof UnionType) {
1307
1318
$ type = TypeUtils::toBenevolentUnion ($ type );
1308
1319
}
1309
1320
0 commit comments