Skip to content

Commit

Permalink
Only add not-null filter to hash join sources based on join type
Browse files Browse the repository at this point in the history
Fixes #516
  • Loading branch information
MarkMpn committed Jul 16, 2024
1 parent 538e508 commit 2cbb845
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
30 changes: 30 additions & 0 deletions MarkMpn.Sql4Cds.Engine.Tests/ExecutionPlanTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8038,5 +8038,35 @@ FROM account as a
</entity>
</fetch>");
}

[TestMethod]
public void MetadataOuterJoin()
{
var query = @"
SELECT a.entitylogicalname,
a.attributetypename,
a.logicalname,
a.targets,
p.logicalname
FROM metadata.attribute AS a
LEFT OUTER JOIN
metadata.entity AS p
ON a.targets = p.logicalname
WHERE a.entitylogicalname IN ('team')";

var planBuilder = new ExecutionPlanBuilder(_localDataSources.Values, this);

var plans = planBuilder.Build(query, null, out _);

Assert.AreEqual(1, plans.Length);

var select = AssertNode<SelectNode>(plans[0]);
var join = AssertNode<HashJoinNode>(select.Source);
Assert.AreEqual(QualifiedJoinType.RightOuter, join.JoinType);
var metadata_p = AssertNode<MetadataQueryNode>(join.LeftSource);
Assert.AreEqual("p", metadata_p.EntityAlias);
var metadata_a = AssertNode<MetadataQueryNode>(join.RightSource);
Assert.AreEqual("a", metadata_a.AttributeAlias);
}
}
}
7 changes: 5 additions & 2 deletions MarkMpn.Sql4Cds.Engine/ExecutionPlan/HashJoinNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,11 @@ public override IDataExecutionPlanNodeInternal FoldQuery(NodeCompilationContext

// Make sure the join keys are not null - the SqlType classes override == to prevent NULL = NULL
// but .Equals used by the hash table allows them to match
LeftSource = AddNotNullFilter(LeftSource, LeftAttribute, context, hints, true);
RightSource = AddNotNullFilter(RightSource, RightAttribute, context, hints, true);
if (JoinType == QualifiedJoinType.Inner || JoinType == QualifiedJoinType.RightOuter)
LeftSource = AddNotNullFilter(LeftSource, LeftAttribute, context, hints, true);

if (JoinType == QualifiedJoinType.Inner || JoinType == QualifiedJoinType.LeftOuter)
RightSource = AddNotNullFilter(RightSource, RightAttribute, context, hints, true);

return this;
}
Expand Down

0 comments on commit 2cbb845

Please sign in to comment.