Skip to content
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

Use benevolent union for scalar in queries #447

Closed
wants to merge 8 commits into from

Conversation

VincentLanglet
Copy link
Contributor

@VincentLanglet VincentLanglet commented Apr 5, 2023

This PR could be an idea of solution for #446

I assume that before #412, the resultType of Query were less used a lot so we didn't have a lot of feedback about the type resolution. But I tried to reproduce your "bug", and even the merge of my PR

$result = $this->entityManager->createQueryBuilder()
			->select('DISTINCT IDENTITY(oi.product) AS id')
			->from(OrderItem::class, 'oi')
			->join('oi.product', 'p')
			->getQuery()
			->getResult();

were reporting list<array{id: int|numeric-string|null}>. I just give the same result with getArrayResult now.

For Identity, I found some specific solution to improve the return type,
but still, I anticipate a lot of similar issue with results like

$result = $this->entityManager->createQueryBuilder()
			->select('p.id AS id')
			->from(OrderItem::class, 'oi')
			->leftJoin('oi.product', 'p')
			->where('p.id IS NOT NULL')
			->getQuery()
			->getArrayResult();

which is reported as list<array{id: int|null}> when we would expect list<array{id: int}>. Notice that in the same way, before #412, the query was already returning list<array{id: int|null}> for getResult calls.

Since we currently cannot be sure that all those |null are true because, for example, we don't check the WHERE condition (and this would be complex). I think the best solution so far would be to use BenevolentUnion for this instead of default unions.

Before fixing the tests I'd like your opinion on such change.

@ondrejmirtes
Copy link
Member

Hi, I need tests to understand what will this fix :) I'd say we need benevolent union even for the case without WHERE because the int|numeric-string for int keys is really annoying.

@VincentLanglet
Copy link
Contributor Author

VincentLanglet commented Apr 6, 2023

I'd say we need benevolent union even for the case without WHERE because the int|numeric-string for int keys is really annoying.

I added benevolent union in two situations so far:

  • When there are WHERE condition in the queries
  • When expression (like IDENTITY) are casted to string because we don't know how the database will behave (your case of int|numeric-string).

I updated the tests (about int|numeric-string) and added one test about where conditions @ondrejmirtes.

@VincentLanglet VincentLanglet marked this pull request as ready for review April 6, 2023 08:38
@VincentLanglet
Copy link
Contributor Author

I added benevolent union in two situations so far:

  • When there are WHERE condition in the queries
  • When expression (like IDENTITY) are casted to string because we don't know how the database will behave (your case of int|numeric-string).

I updated the tests (about int|numeric-string) and added one test about where conditions @ondrejmirtes.

Does this solution seems ok to you @ondrejmirtes ?

This should avoid false positive from #412 but still be an improvement of query results. And possibly allow you to make new releases, unless you find others issues. :)

@ondrejmirtes
Copy link
Member

FYI I had to revert some of your commits so I can release phpstan-doctrine with other changes. Unfortunately I currently cannot give this the work priority and the needed timeframe of multiple days in order to finish this. 84bf895...f1499e5

@VincentLanglet
Copy link
Contributor Author

FYI I had to revert some of your commits so I can release phpstan-doctrine with other changes. Unfortunately I currently cannot give this the work priority and the needed timeframe of multiple days in order to finish this. 84bf895...f1499e5

I understand.

I recreated the PR with the commits reverted #453 then

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants