Skip to content

Commit

Permalink
Added not-null column checking to FilterNode
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkMpn committed Mar 8, 2022
1 parent a27c888 commit 620b9ec
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
4 changes: 2 additions & 2 deletions MarkMpn.Sql4Cds.Engine/ExecutionPlan/FetchXmlScan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -777,10 +777,10 @@ private void AddSchemaAttributes(NodeSchema schema, IAttributeMetadataCache meta

private void AddNotNullFilters(NodeSchema schema, string alias, filter filter)
{
if (filter.type == filterType.or)
if (filter.Items == null)
return;

if (filter.Items == null)
if (filter.type == filterType.or && filter.Items.Length > 1)
return;

foreach (var cond in filter.Items.OfType<condition>())
Expand Down
68 changes: 67 additions & 1 deletion MarkMpn.Sql4Cds.Engine/ExecutionPlan/FilterNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,73 @@ public override IEnumerable<IExecutionPlanNode> GetSources()

public override INodeSchema GetSchema(IDictionary<string, DataSource> dataSources, IDictionary<string, DataTypeReference> parameterTypes)
{
return Source.GetSchema(dataSources, parameterTypes);
var schema = new NodeSchema(Source.GetSchema(dataSources, parameterTypes));

AddNotNullColumns(schema, Filter, false);

return schema;
}

private void AddNotNullColumns(NodeSchema schema, BooleanExpression filter, bool not)
{
if (filter is BooleanBinaryExpression binary)
{
if (binary.BinaryExpressionType == BooleanBinaryExpressionType.Or)
return;

AddNotNullColumns(schema, binary.FirstExpression, not);
AddNotNullColumns(schema, binary.SecondExpression, not);
}

if (filter is BooleanIsNullExpression isNull)
{
if (not ^ isNull.IsNot)
AddNotNullColumn(schema, isNull.Expression);
}

if (!not && filter is BooleanComparisonExpression cmp)
{
AddNotNullColumn(schema, cmp.FirstExpression);
AddNotNullColumn(schema, cmp.SecondExpression);
}

if (filter is BooleanParenthesisExpression paren)
{
AddNotNullColumns(schema, paren.Expression, not);
}

if (filter is BooleanNotExpression n)
{
AddNotNullColumns(schema, n.Expression, !not);
}

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

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

if (!not && filter is FullTextPredicate fullText)
{
foreach (var col in fullText.Columns)
AddNotNullColumn(schema, col);
}
}

private void AddNotNullColumn(NodeSchema schema, ScalarExpression expr)
{
if (!(expr is ColumnReferenceExpression col))
return;

if (!schema.ContainsColumn(col.GetColumnName(), out var colName))
return;

schema.NotNullColumns.Add(colName);
}

public override IDataExecutionPlanNodeInternal FoldQuery(IDictionary<string, DataSource> dataSources, IQueryExecutionOptions options, IDictionary<string, DataTypeReference> parameterTypes, IList<OptimizerHint> hints)
Expand Down

0 comments on commit 620b9ec

Please sign in to comment.