Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/1.3.x' into 1.4.x
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Feb 12, 2024
2 parents 6d758ad + f3abbd8 commit 5ca0f53
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Doctrine/Mapping/ClassMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(string $tmpDir)
protected function initialize(): void
{
$drivers = [];
if (class_exists(AnnotationReader::class)) {
if (class_exists(AnnotationDriver::class) && class_exists(AnnotationReader::class)) {
$docParser = new DocParser();
$docParser->setIgnoreNotImportedAnnotations(true);
$drivers[] = new AnnotationDriver(new AnnotationReader($docParser));
Expand Down
17 changes: 17 additions & 0 deletions src/Type/Doctrine/Query/QueryResultTypeWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -933,12 +933,29 @@ public function walkAggregateExpression($aggExpression): string
switch ($aggExpression->functionName) {
case 'MAX':
case 'MIN':
$type = $this->unmarshalType(
$this->walkSimpleArithmeticExpression($aggExpression->pathExpression)
);

return $this->marshalType(TypeCombinator::addNull($type));

case 'AVG':
$type = $this->unmarshalType(
$this->walkSimpleArithmeticExpression($aggExpression->pathExpression)
);

$type = TypeCombinator::union($type, $type->toFloat());
$type = TypeUtils::generalizeType($type, GeneralizePrecision::lessSpecific());

return $this->marshalType(TypeCombinator::addNull($type));

case 'SUM':
$type = $this->unmarshalType(
$this->walkSimpleArithmeticExpression($aggExpression->pathExpression)
);

$type = TypeUtils::generalizeType($type, GeneralizePrecision::lessSpecific());

return $this->marshalType(TypeCombinator::addNull($type));

case 'COUNT':
Expand Down
76 changes: 76 additions & 0 deletions tests/Type/Doctrine/Query/QueryResultTypeWalkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,82 @@ public function getTestData(): iterable
',
];

yield 'aggregate on literal' => [
$this->constantArray([
[
new ConstantIntegerType(1),
TypeCombinator::union(
new ConstantStringType('1'),
new ConstantIntegerType(1),
new NullType()
),
],
[
new ConstantIntegerType(2),
TypeCombinator::union(
new ConstantStringType('0'),
new ConstantIntegerType(0),
new ConstantStringType('1'),
new ConstantIntegerType(1),
new NullType()
),
],
[
new ConstantIntegerType(3),
TypeCombinator::union(
new ConstantStringType('1'),
new ConstantIntegerType(1),
new NullType()
),
],
[
new ConstantIntegerType(4),
TypeCombinator::union(
new ConstantStringType('0'),
new ConstantIntegerType(0),
new ConstantStringType('1'),
new ConstantIntegerType(1),
new NullType()
),
],
[
new ConstantIntegerType(5),
TypeCombinator::union(
$this->intStringified(),
new FloatType(),
new NullType()
),
],
[
new ConstantIntegerType(6),
TypeCombinator::union(
$this->intStringified(),
new FloatType(),
new NullType()
),
],
[
new ConstantIntegerType(7),
TypeCombinator::addNull($this->intStringified()),
],
[
new ConstantIntegerType(8),
TypeCombinator::addNull($this->intStringified()),
],
]),
'
SELECT MAX(1),
MAX(CASE WHEN m.intColumn = 0 THEN 1 ELSE 0 END),
MIN(1),
MIN(CASE WHEN m.intColumn = 0 THEN 1 ELSE 0 END),
AVG(1),
AVG(CASE WHEN m.intColumn = 0 THEN 1 ELSE 0 END),
SUM(1),
SUM(CASE WHEN m.intColumn = 0 THEN 1 ELSE 0 END)
FROM QueryResult\Entities\Many m
',
];

yield 'literal' => [
$this->constantArray([
[
Expand Down

0 comments on commit 5ca0f53

Please sign in to comment.