staabm / phpstan-dba

PHPStan based SQL static analysis and type inference for the database access layer
https://staabm.github.io/archive.html#phpstan-dba
MIT License
250 stars 17 forks source link

Right vs left join #588

Open jakubvojacek opened 1 year ago

jakubvojacek commented 1 year ago

Hello @staabm

 public function rightLeftJoin(PDO $pdo): void
    {
        $stmt = $pdo->query('SELECT adaid from ada left join ak on adaid = eladaid');
        assertType('PDOStatement<array{adaid: int<-32768, 32767>, 0: int<-32768, 32767>}>', $stmt);

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

I found one RIGHT JOIN in our codebase that colleague of mine used (I've never used right join actually, always just left/inner).

Anyway, if I understand it right, I should be able to switch left vs right when I switch the table names an get same results, or not?

I tried that using the test above but it fails the 2nd assertion with

-'PDOStatement<array{adaid: int<-32768, 32767>, 0: int<-32768, 32767>}>'
+'PDOStatement<array{adaid: int<-32768, 32767>|null, 0: int<-32768, 32767>|null}>'

But I dont think that adaid will ever be null, or will it?

staabm commented 1 year ago

I never used right join personally. Have no idea when/why to use them

staabm commented 1 year ago

to clarify: this should not mean that we don't want support for right joins, but it needs some more intensive testing on your end how the sql actually behaves and what types to expect