Skip to content

SUM() aggregation returns wrong type #460

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jlherren opened this issue Jun 1, 2023 · 2 comments · Fixed by #543
Closed

SUM() aggregation returns wrong type #460

jlherren opened this issue Jun 1, 2023 · 2 comments · Fixed by #543

Comments

@jlherren
Copy link

jlherren commented Jun 1, 2023

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.

@ondrejmirtes
Copy link
Member

Fixed #543

Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants