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

Narrow types via join / join-condition #531

Merged
merged 16 commits into from
Mar 14, 2023
Merged

Narrow types via join / join-condition #531

merged 16 commits into from
Mar 14, 2023

Conversation

staabm
Copy link
Owner

@staabm staabm commented Feb 20, 2023

closes #527

@staabm
Copy link
Owner Author

staabm commented Feb 20, 2023

@gharlan could you do me a favour and verify the expected types?

assertType('PDOStatement<array{adaid: int<-32768, 32767>, 0: int<-32768, 32767>, akid: int<-2147483648, 2147483647>, 1: int<-2147483648, 2147483647>}>', $stmt);

$stmt = $pdo->query('SELECT adaid, akid from ada left join ak on (adaid = eladaid)');
assertType('PDOStatement<array{adaid: int<-32768, 32767>, 0: int<-32768, 32767>, akid: int<-2147483648, 2147483647>|null, 1: int<-2147483648, 2147483647>|null}>', $stmt);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rows not matched in the main result, will be filled up with null, therefore even a non-nullable column can get null on outer joins

$stmt = $pdo->query('SELECT adaid, eladaid from ada join ak on (adaid = eladaid)');
assertType('PDOStatement<array{adaid: int<-32768, 32767>, 0: int<-32768, 32767>, eladaid: int<-2147483648, 2147483647>, 1: int<-2147483648, 2147483647>}>', $stmt);

$stmt = $pdo->query('SELECT adaid, eladaid from ada inner join ak on (adaid = eladaid)');
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on inner join, nullable columns get non nullable

Copy link

@gharlan gharlan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these expectations are correct 👍

@staabm staabm force-pushed the joins branch 2 times, most recently from b15c06c to 2cab3d4 Compare February 23, 2023 21:21
/**
* @api
*/
final class Join
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure it makes sense to model joins here. maybe we should instead use SqlFtw\Sql\Dml\TableReference\Join instead, as we need the low level information

Comment on lines 115 to 118
if ($join->getType() === Join::TYPE_OUTER) {
return TypeCombinator::addNull($column->getType());
}
return TypeCombinator::removeNull($column->getType());
Copy link
Owner Author

@staabm staabm Feb 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should only add/remove NULLs for columns which are part of the join condition, but not regular columns selected from joined tables - without beeing part of the join condition.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

before doing so, we may wait a bit for sqlftw stabilization as we would need a deep dive into the AST

grafik

@staabm staabm changed the title Added join expectations Narrow types via join / join-condition Mar 14, 2023
@staabm staabm marked this pull request as ready for review March 14, 2023 20:39
@staabm staabm merged commit 364de7e into main Mar 14, 2023
@staabm staabm deleted the joins branch March 14, 2023 20:40
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.

nullability and join types
2 participants