Skip to content

CSHARP-5436: Optimize special case of Any with constant array and fie… #1585

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,53 @@ static bool OperatorMapsNullToNull(AstUnaryOperator @operator)
}
}

public override AstNode VisitExprFilter(AstExprFilter node)
{
var optimizedNode = base.VisitExprFilter(node);

if (optimizedNode is AstExprFilter exprFilter &&
exprFilter.Expression is AstUnaryExpression unaryExpression &&
unaryExpression.Operator == AstUnaryOperator.AnyElementTrue &&
unaryExpression.Arg is AstMapExpression mapExpression &&
mapExpression.Input is AstConstantExpression inputConstant &&
inputConstant.Value is BsonArray inputArrayValue &&
mapExpression.In is AstBinaryExpression inBinaryExpression &&
inBinaryExpression.Operator == AstBinaryOperator.Eq &&
TryGetBinaryExpressionArguments(inBinaryExpression, out AstFieldPathExpression fieldPathExpression, out AstVarExpression varExpression) &&
fieldPathExpression.Path.Length > 1 && fieldPathExpression.Path[0] == '$' && fieldPathExpression.Path[1] != '$' &&
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have some more elegant way to check if fieldPath points to the "current" variable?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what you mean.

But anything that starts with $$ is not a field so I think that's all we need to check.

varExpression == mapExpression.As)
{
// { $expr : { $anyElementTrue : { $map : { input : <constantArray>, as : "<var>", in : { $eq : ["$<dottedFieldName>", "$$<var>"] } } } } }
// => { "<dottedFieldName>" : { $in : <constantArray> } }
return AstFilter.In(AstFilter.Field(fieldPathExpression.Path.Substring(1)), inputArrayValue);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment just before this line to visualize the simplification being done:

// { $expr : { $anyElementTrue : { $map : { input : <constantArray>, as : "<var>", in : { $eq : ["$<dottedFieldName>", "$$<var>"] } } } } }
//      => { "<dottedFieldName>" : { $in : <constantArray> } }                   
return AstFilter.In(AstFilter.Field(fieldPathExpression.Path.Substring(1)), inputArrayValue);                                                         

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}

return optimizedNode;

static bool TryGetBinaryExpressionArguments<T1, T2>(AstBinaryExpression binaryExpression, out T1 arg1, out T2 arg2)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Theoretically could be reusable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can wait until there is a need.

where T1 : AstNode
where T2 : AstNode
{
if (binaryExpression.Arg1 is T1 arg1AsT1 && binaryExpression.Arg2 is T2 arg2AsT2)
{
arg1 = arg1AsT1;
arg2 = arg2AsT2;
return true;
}

if (binaryExpression.Arg1 is T2 arg1AsT2 && binaryExpression.Arg1 is T1 arg2AsT1)
{
arg1 = arg2AsT1;
arg2 = arg1AsT2;
return true;
}

arg1 = null;
arg2 = null;
return false;
}
}

public override AstNode VisitFieldOperationFilter(AstFieldOperationFilter node)
{
node = (AstFieldOperationFilter)base.VisitFieldOperationFilter(node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ public void Any_with_predicate_should_work(
results.Should().Equal(false, false, false, true);
}

[Fact]
public void Any_on_constant_array_should_be_optimized()
{
var collection = CreateCollection();

var obj = new[] { 1, 2, 3 };
var queryable = collection.AsQueryable()
.Where(x => obj.Any(y => x.Id == y));

var stages = Translate(collection, queryable);
AssertStages(stages, "{ $match : { _id : { $in : [1, 2, 3] } } }");

var results = queryable.ToList();
results.Select(x => x.Id).Should().Equal(1, 2, 3);
}

private IMongoCollection<C> CreateCollection()
{
var collection = GetCollection<C>("test");
Expand Down