Skip to content

SUM() aggregation returns wrong type #460

Closed
@jlherren

Description

@jlherren

I have the following code:

$qb = $this->em->createQueryBuilder()
               ->addSelect('tvs.title')
               ->addSelect('tvs.broadcastDate')
               ->addSelect('COUNT(tva.id) AS count')
               ->addSelect('SUM(CASE WHEN tva.freePeriod = TRUE THEN 1 ELSE 0 END) AS freePeriodCount')
               ->addSelect('SUM(CASE WHEN tva.subscriptions IS NOT NULL THEN 0 ELSE 1 END) AS userStreamingCount')
               ->from(TvActivity::class, 'tva')
               ->join('tva.tvShow', 'tvs')
               ->addGroupBy('tvs.id');
$dbRows = $qb->getQuery()->getResult();
\PHPStan\dumpType($dbRows);

The dumped type is:

list<array{
    title: string,
    broadcastDate: DateTimeImmutable,
    count: int<0, max>|numeric-string,
    freePeriodCount: 0|1|'0'|'1'|null,
    userStreamingCount: 0|1|'0'|'1'|null,
}>

The last two fields can be arbitrary numbers, but are reported as 0 and 1 only. If it helps I can create a full reproduction repo, but hopefully things are clear like this. It's also wrong when doing SUM(1), so the exact entity definitions might not matter.

Using phpstan-doctrine 1.3.40.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions