Skip to content
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
17 changes: 12 additions & 5 deletions Microsoft.Azure.Cosmos/src/Linq/SubtreeEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected override Expression VisitMemberInit(MemberInitExpression node)

private Expression EvaluateMemberAccess(Expression expression)
{
while (expression.CanReduce)
while (expression?.CanReduce ?? false)
{
expression = expression.Reduce();
}
Expand All @@ -57,7 +57,7 @@ private Expression EvaluateMemberAccess(Expression expression)
// This is done because the compilation of a delegate takes a global lock which causes highly
// threaded clients to exhibit async-over-sync thread exhaustion behaviour on this call path
// even when doing relatively straightforward queries.
if (!(expression is MemberExpression memberExpression))
if (expression is not MemberExpression memberExpression)
{
return expression;
}
Expand All @@ -66,19 +66,26 @@ private Expression EvaluateMemberAccess(Expression expression)
// nested property access (x.y.z) without needing to fall back on delegate compilation.
Expression targetExpression = this.EvaluateMemberAccess(memberExpression.Expression);

if (!(targetExpression is ConstantExpression targetConstant))
// NOTE: When evaluating static field or property access, we may have a null targetExpression.
// In this situation, we should pass the null value to the GetValue(...) methods below to
// indicate that we are accessing a static member.
ConstantExpression targetConstant = targetExpression as ConstantExpression;

// If we have a target expression but it cannot be resolved to a constant, then we should skip
// using reflectoin here and instead rely on the fallback delegate compilation approach.
if (targetExpression is not null && targetConstant is null)
{
return expression;
}

if (memberExpression.Member is FieldInfo fieldInfo)
{
return Expression.Constant(fieldInfo.GetValue(targetConstant.Value), memberExpression.Type);
return Expression.Constant(fieldInfo.GetValue(targetConstant?.Value), memberExpression.Type);
}

if (memberExpression.Member is PropertyInfo propertyInfo)
{
return Expression.Constant(propertyInfo.GetValue(targetConstant.Value), memberExpression.Type);
return Expression.Constant(propertyInfo.GetValue(targetConstant?.Value), memberExpression.Type);
}

return expression;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,40 @@ FROM root
WHERE (root["NumericField"] = 3)]]></SqlQuery>
</Output>
</Result>
<Result>
<Input>
<Description><![CDATA[Filter on Static Field value]]></Description>
<Expression><![CDATA[query.Where(doc => (doc.NumericField == AmbientContextObject.StaticFieldAccess))]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE root
FROM root
WHERE (root["NumericField"] = 4)]]></SqlQuery>
</Output>
</Result>
<Result>
<Input>
<Description><![CDATA[Filter on Static Property value]]></Description>
<Expression><![CDATA[query.Where(doc => (doc.NumericField == AmbientContextObject.StaticPropertyAccess))]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE root
FROM root
WHERE (root["NumericField"] = 5)]]></SqlQuery>
</Output>
</Result>
<Result>
<Input>
<Description><![CDATA[Filter on Const value]]></Description>
<Expression><![CDATA[query.Where(doc => (doc.NumericField == 6))]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE root
FROM root
WHERE (root["NumericField"] = 6)]]></SqlQuery>
</Output>
</Result>
</Results>
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,16 @@ internal class AmbientContextObject

public double PropertyAccess { get; set; }

public double MethodAccess() => 1.0;
public double MethodAccess()
{
return 1.0;
}

public static double StaticFieldAccess = 4.0;

public static double StaticPropertyAccess => 5.0;

public const double ConstAccess = 6.0;
}

[TestMethod]
Expand Down Expand Up @@ -560,6 +569,9 @@ public void TestMemberAccess()
// performance boost, especially under highly concurrent workloads).
new LinqTestInput("Filter on Field value", b => getQuery(b).Where(doc => doc.NumericField == ambientContext.FieldAccess)),
new LinqTestInput("Filter on Property value", b => getQuery(b).Where(doc => doc.NumericField == ambientContext.PropertyAccess)),
new LinqTestInput("Filter on Static Field value", b => getQuery(b).Where(doc => doc.NumericField == AmbientContextObject.StaticFieldAccess)),
new LinqTestInput("Filter on Static Property value", b => getQuery(b).Where(doc => doc.NumericField == AmbientContextObject.StaticPropertyAccess)),
new LinqTestInput("Filter on Const value", b => getQuery(b).Where(doc => doc.NumericField == AmbientContextObject.ConstAccess)),
};
this.ExecuteTestSuite(inputs);
}
Expand Down