Skip to content

Commit

Permalink
narrow down nullability based on JOIN ... ON
Browse files Browse the repository at this point in the history
  • Loading branch information
schlndh committed Feb 16, 2024
1 parent aae116b commit 698ae66
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Analyser/AnalyserState.php
Original file line number Diff line number Diff line change
Expand Up @@ -495,9 +495,9 @@ private function analyseTableReference(TableReference $fromClause, ColumnResolve
$bakResolver = $this->columnResolver;
$this->columnResolver = $columnResolver;

match (true) {
$onResult = match (true) {
$fromClause->joinCondition instanceof Expr\Expr
=> $this->resolveExprType($fromClause->joinCondition),
=> $this->resolveExprType($fromClause->joinCondition, AnalyserConditionTypeEnum::TRUTHY),
/** This is checked in {@see ColumnResolver::mergeAfterJoin()} */
$fromClause->joinCondition instanceof UsingJoinCondition => 1,
$fromClause->joinCondition === null => null,
Expand All @@ -513,6 +513,8 @@ private function analyseTableReference(TableReference $fromClause, ColumnResolve
foreach ($leftTables as $leftTable) {
$columnResolver->registerOuterJoinedTable($leftTable);
}
} elseif ($onResult instanceof ExprTypeResult) {
$columnResolver->addKnowledge($onResult->knowledgeBase);
}

return [array_merge($leftTables, $rightTables), $columnResolver];
Expand Down
6 changes: 6 additions & 0 deletions src/Analyser/ColumnResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,12 @@ public function mergeAfterJoin(self $other, Join $join): void
} else {
$this->allColumns = array_merge($this->allColumns, $other->allColumns);
}

if ($this->knowledgeBase !== null && $other->knowledgeBase !== null) {
$this->knowledgeBase = $this->knowledgeBase->and($other->knowledgeBase);
} elseif ($this->knowledgeBase !== null) {
$this->knowledgeBase = $other->knowledgeBase;
}
}

/** @throws AnalyserException */
Expand Down
31 changes: 31 additions & 0 deletions tests/Analyser/AnalyserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1776,6 +1776,37 @@ public function provideValidNullabilityTestData(): iterable
];
}

yield 'JOIN' => [
'query' => '
SELECT * FROM analyser_test_nullability_1 t1
JOIN analyser_test_nullability_1 t2 ON t1.col_vchar = t2.col_vchar
',
];

yield 'LEFT JOIN' => [
'query' => '
SELECT * FROM analyser_test_nullability_1 t1
LEFT JOIN analyser_test_nullability_1 t2 ON t1.col_vchar = t2.col_vchar
',
];

yield 'RIGHT JOIN' => [
'query' => '
SELECT * FROM analyser_test_nullability_1 t1
RIGHT JOIN analyser_test_nullability_1 t2 ON t1.col_vchar = t2.col_vchar
',
];

yield 'LEFT JOIN (JOIN)' => [
'query' => '
SELECT * FROM analyser_test_nullability_1 t1
LEFT JOIN (
analyser_test_nullability_1 t2
JOIN analyser_test_nullability_1 t3 ON t2.col_vchar = t3.col_vchar
) ON t2.col_vchar = t1.col_vchar
',
];

// TODO: fix this
//yield 'WHERE 0 ORDER BY COUNT(*)' => [
// 'query' => 'SELECT id FROM analyser_test WHERE 0 ORDER BY COUNT(*)',
Expand Down

0 comments on commit 698ae66

Please sign in to comment.