From 9d81921d52a548bd39922212e1231ab401225099 Mon Sep 17 00:00:00 2001 From: Mark Carrington <31017244+MarkMpn@users.noreply.github.com> Date: Fri, 21 Jun 2024 19:15:00 +0100 Subject: [PATCH] Fixed tests --- .../ExecutionPlan/FilterNode.cs | 3 +++ .../ExecutionPlan/FoldableJoinNode.cs | 8 +++---- .../ExecutionPlan/HashJoinNode.cs | 23 ++----------------- 3 files changed, 9 insertions(+), 25 deletions(-) diff --git a/MarkMpn.Sql4Cds.Engine/ExecutionPlan/FilterNode.cs b/MarkMpn.Sql4Cds.Engine/ExecutionPlan/FilterNode.cs index 7ceb496e..0f7d8d14 100644 --- a/MarkMpn.Sql4Cds.Engine/ExecutionPlan/FilterNode.cs +++ b/MarkMpn.Sql4Cds.Engine/ExecutionPlan/FilterNode.cs @@ -1470,6 +1470,9 @@ private bool FoldTableSpoolToIndexSpool(NodeCompilationContext context, IList hints, Dictionary subqueryExpressions) { + if (Filter == null) + return false; + var foldedFilters = false; // Find all the data source nodes we could fold this into. Include direct data sources, those from either side of an inner join, or the main side of an outer join diff --git a/MarkMpn.Sql4Cds.Engine/ExecutionPlan/FoldableJoinNode.cs b/MarkMpn.Sql4Cds.Engine/ExecutionPlan/FoldableJoinNode.cs index f7683d8a..42d766a4 100644 --- a/MarkMpn.Sql4Cds.Engine/ExecutionPlan/FoldableJoinNode.cs +++ b/MarkMpn.Sql4Cds.Engine/ExecutionPlan/FoldableJoinNode.cs @@ -117,10 +117,10 @@ public override IDataExecutionPlanNodeInternal FoldQuery(NodeCompilationContext // Left outer join - right key must be non-null // Right outer join - left key must be non-null if (JoinType == QualifiedJoinType.Inner || JoinType == QualifiedJoinType.RightOuter) - LeftSource = AddNotNullFilter(LeftSource, LeftAttribute, context, hints); + LeftSource = AddNotNullFilter(LeftSource, LeftAttribute, context, hints, false); if (JoinType == QualifiedJoinType.Inner || JoinType == QualifiedJoinType.LeftOuter) - RightSource = AddNotNullFilter(RightSource, RightAttribute, context, hints); + RightSource = AddNotNullFilter(RightSource, RightAttribute, context, hints, false); if (FoldSingleRowJoinToNestedLoop(context, hints, leftSchema, rightSchema, out folded)) return folded; @@ -142,7 +142,7 @@ private IDataExecutionPlanNodeInternal PrependFilters(IDataExecutionPlanNodeInte return folded; } - private IDataExecutionPlanNodeInternal AddNotNullFilter(IDataExecutionPlanNodeInternal source, ColumnReferenceExpression attribute, NodeCompilationContext context, IList hints) + protected IDataExecutionPlanNodeInternal AddNotNullFilter(IDataExecutionPlanNodeInternal source, ColumnReferenceExpression attribute, NodeCompilationContext context, IList hints, bool required) { var schema = source.GetSchema(context); if (!schema.ContainsColumn(attribute.GetColumnName(), out var colName)) @@ -163,7 +163,7 @@ private IDataExecutionPlanNodeInternal AddNotNullFilter(IDataExecutionPlanNodeIn var folded = filter.FoldQuery(context, hints); - if (folded != filter) + if (required || folded != filter) { folded.Parent = this; return folded; diff --git a/MarkMpn.Sql4Cds.Engine/ExecutionPlan/HashJoinNode.cs b/MarkMpn.Sql4Cds.Engine/ExecutionPlan/HashJoinNode.cs index 90e627e4..a87dfac0 100644 --- a/MarkMpn.Sql4Cds.Engine/ExecutionPlan/HashJoinNode.cs +++ b/MarkMpn.Sql4Cds.Engine/ExecutionPlan/HashJoinNode.cs @@ -152,27 +152,8 @@ 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 = new FilterNode - { - Source = LeftSource, - Filter = new BooleanIsNullExpression - { - Expression = LeftAttribute, - IsNot = true - } - }.FoldQuery(context, hints); - LeftSource.Parent = this; - - RightSource = new FilterNode - { - Source = RightSource, - Filter = new BooleanIsNullExpression - { - Expression = RightAttribute, - IsNot = true - } - }.FoldQuery(context, hints); - RightSource.Parent = this; + LeftSource = AddNotNullFilter(LeftSource, LeftAttribute, context, hints, true); + RightSource = AddNotNullFilter(RightSource, RightAttribute, context, hints, true); return this; }