Skip to content

Commit

Permalink
Improved null handling for NOT LIKE predicate
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkMpn committed Mar 29, 2024
1 parent 1977f0b commit 32d7117
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
3 changes: 2 additions & 1 deletion MarkMpn.Sql4Cds.Engine/ExecutionPlan/BaseDataNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1171,7 +1171,8 @@ private bool TranslateFetchXMLCriteriaWithVirtualAttributes(NodeCompilationConte
if (meta.LogicalName == "solution" && linkName != null && !new FetchEntityType { Items = items }.GetLinkEntities(true).Any(link => link.alias == linkName))
return false;

if ((op == @operator.ne || op == @operator.nebusinessid || op == @operator.neq || op == @operator.neuserid) && type != BooleanComparisonType.IsDistinctFrom)
if ((op == @operator.ne || op == @operator.nebusinessid || op == @operator.neq || op == @operator.neuserid || op == @operator.notlike)
&& type != BooleanComparisonType.IsDistinctFrom)
{
// FetchXML not-equal type operators treat NULL as not-equal to values, but T-SQL treats them as not-not-equal. Add
// an extra not-null condition to keep it compatible with T-SQL
Expand Down
5 changes: 4 additions & 1 deletion MarkMpn.Sql4Cds.Engine/ExecutionPlan/FetchXmlScan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1223,7 +1223,10 @@ private void AddNotNullFilters(ColumnList schema, Dictionary<string, IReadOnlyLi

foreach (var cond in filter.Items.OfType<condition>())
{
if (cond.@operator == @operator.@null || cond.@operator == @operator.ne || cond.@operator == @operator.nebusinessid || cond.@operator == @operator.neq || cond.@operator == @operator.neuserid)
if (cond.@operator == @operator.@null || cond.@operator == @operator.ne || cond.@operator == @operator.nebusinessid ||
cond.@operator == @operator.neq || cond.@operator == @operator.neuserid || cond.@operator == @operator.notlike ||
cond.@operator == @operator.notin || cond.@operator == @operator.notunder || cond.@operator == @operator.notbeginwith ||
cond.@operator == @operator.notbetween || cond.@operator == @operator.notcontainvalues || cond.@operator == @operator.notendwith)
continue;

var fullname = (cond.entityname?.EscapeIdentifier() ?? alias) + "." + (cond.alias ?? cond.attribute).EscapeIdentifier();
Expand Down
4 changes: 2 additions & 2 deletions MarkMpn.Sql4Cds.Engine/ExecutionPlan/FilterNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ private void AddNotNullColumns(NodeSchema schema, BooleanExpression filter, bool
AddNotNullColumns(schema, n.Expression, !not);
}

if (!not && filter is InPredicate inPred)
if (!not && filter is InPredicate inPred && !inPred.NotDefined)
{
AddNotNullColumn(schema, inPred.Expression);
}

if (!not && filter is LikePredicate like)
if (!not && filter is LikePredicate like && !like.NotDefined)
{
AddNotNullColumn(schema, like.FirstExpression);
AddNotNullColumn(schema, like.SecondExpression);
Expand Down

0 comments on commit 32d7117

Please sign in to comment.