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

Query type inference failures reproductions #446

Draft
wants to merge 1 commit into
base: 1.3.x
Choose a base branch
from

Conversation

ondrejmirtes
Copy link
Member

No description provided.

@ondrejmirtes
Copy link
Member Author

Alright @VincentLanglet, here's one:

--- Expected
+++ Actual
@@ @@
-'list<array{id: int}>'
+'list<array{id: int|numeric-string|null}>'

@VincentLanglet
Copy link
Contributor

Alright @VincentLanglet, here's one:

--- Expected
+++ Actual
@@ @@
-'list<array{id: int}>'
+'list<array{id: int|numeric-string|null}>'

If I dump the Query, I get

Doctrine\ORM\Query<null, array{id: int|numeric-string|null}>

so QueryResultDynamicReturnTypeExtension works fine but the issue seems to be with QueryResultTypeWalker

This is a consequence of #273 (cc @arnaud-lb)
and there is the following test case.

'identity function' => [
				$this->constantArray([
					[new ConstantIntegerType(1), TypeCombinator::addNull($this->numericStringOrInt())],
					[new ConstantIntegerType(2), $this->numericStringOrInt()],
					[new ConstantIntegerType(3), TypeCombinator::addNull($this->numericStringOrInt())],
					[new ConstantIntegerType(4), TypeCombinator::addNull(new StringType())],
					[new ConstantIntegerType(5), TypeCombinator::addNull(new StringType())],
					[new ConstantIntegerType(6), TypeCombinator::addNull($this->numericStringOrInt())],
					[new ConstantIntegerType(7), TypeCombinator::addNull(new MixedType())],
					[new ConstantIntegerType(8), TypeCombinator::addNull($this->numericStringOrInt())],
				]),
				'
					SELECT		IDENTITY(m.oneNull),
								IDENTITY(m.one),
								IDENTITY(m.oneDefaultNullability),
								IDENTITY(m.compoundPk),
								IDENTITY(m.compoundPk, \'id\'),
								IDENTITY(m.compoundPk, \'version\'),
								IDENTITY(m.compoundPkAssoc),
								IDENTITY(m.compoundPkAssoc, \'version\')
					FROM		QueryResult\Entities\Many m
				',
			],

I'll need to investigate why numericStringOrInt is used

@VincentLanglet
Copy link
Contributor

I identified that the numeric-string is because of the following code

} else {
// Expressions default to Doctrine's StringType, whose
// convertToPHPValue() is a no-op. So the actual type depends on
// the driver and PHP version.
// Here we assume that the value may or may not be casted to
// string by the driver.
$type = TypeTraverser::map($type, static function (Type $type, callable $traverse): Type {
if ($type instanceof UnionType || $type instanceof IntersectionType) {
return $traverse($type);
}
if ($type instanceof IntegerType || $type instanceof FloatType) {
return TypeCombinator::union($type->toString(), $type);
}
if ($type instanceof BooleanType) {
return TypeCombinator::union($type->toInteger()->toString(), $type);
}
return $traverse($type);
});
}

@VincentLanglet
Copy link
Contributor

I try finding some "solutions" for this issue.

But I discovered a new one.

$result = $this->entityManager->createQueryBuilder()
			->select('p.id AS id')
			->from(OrderItem::class, 'oi')
			->join('oi.product', 'p')
			->getQuery()
			->getArrayResult();
		assertType('list<array{id: int}>', $result);

Works

$result = $this->entityManager->createQueryBuilder()
			->select('p.id AS id')
			->from(OrderItem::class, 'oi')
			->leftJoin('oi.product', 'p')
			->getQuery()
			->getArrayResult();
		assertType('list<array{id: int|null}>', $result);

Also works But

$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();
		assertType('list<array{id: int}>', $result);

doesn't.

The where condition are not supported, and this may be too complicated to support them.

So far, I think I'll have to change every Union to BenevolentUnion then...

@VincentLanglet
Copy link
Contributor

I will need your opinion on this solution @ondrejmirtes #447

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.

None yet

2 participants