Skip to content

Commit

Permalink
Query: Allow client projection to cause materialization
Browse files Browse the repository at this point in the history
This changes SqlTranslatingExpressionVisitor to return null rather than throwing InvalidCastExpression when something failed to translate to Sql

Part of #15866
  • Loading branch information
smitpatel committed Jun 4, 2019
1 parent a044aa5 commit f9e8c4a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,24 +189,30 @@ protected override Expression VisitMethodCall(MethodCallExpression methodCallExp
}
}

var @object = (SqlExpression)Visit(methodCallExpression.Object);
var failed = TranslationFailed(methodCallExpression.Object, @object);
var @object = Visit(methodCallExpression.Object);
if (TranslationFailed(methodCallExpression.Object, @object))
{
return null;
}

var arguments = new SqlExpression[methodCallExpression.Arguments.Count];
for (var i = 0; i < arguments.Length; i++)
{
arguments[i] = (SqlExpression)Visit(methodCallExpression.Arguments[i]);
failed |= (methodCallExpression.Arguments[i] != null && arguments[i] == null);
var argument = Visit(methodCallExpression.Arguments[i]);
if (TranslationFailed(methodCallExpression.Arguments[i], argument))
{
return null;
}
arguments[i] = (SqlExpression)argument;
}

return failed
? null
: _methodCallTranslatorProvider.Translate(_model, @object, methodCallExpression.Method, arguments);
return _methodCallTranslatorProvider.Translate(_model, (SqlExpression)@object, methodCallExpression.Method, arguments);
}

protected override Expression VisitBinary(BinaryExpression binaryExpression)
{
var left = (SqlExpression)Visit(binaryExpression.Left);
var right = (SqlExpression)Visit(binaryExpression.Right);
var left = Visit(binaryExpression.Left);
var right = Visit(binaryExpression.Right);

if (TranslationFailed(binaryExpression.Left, left)
|| TranslationFailed(binaryExpression.Right, right))
Expand All @@ -216,8 +222,8 @@ protected override Expression VisitBinary(BinaryExpression binaryExpression)

return _sqlExpressionFactory.MakeBinary(
binaryExpression.NodeType,
left,
right,
(SqlExpression)left,
(SqlExpression)right,
null);
}

Expand Down Expand Up @@ -277,9 +283,9 @@ protected override Expression VisitExtension(Expression extensionExpression)

protected override Expression VisitConditional(ConditionalExpression conditionalExpression)
{
var test = (SqlExpression)Visit(conditionalExpression.Test);
var ifTrue = (SqlExpression)Visit(conditionalExpression.IfTrue);
var ifFalse = (SqlExpression)Visit(conditionalExpression.IfFalse);
var test = Visit(conditionalExpression.Test);
var ifTrue = Visit(conditionalExpression.IfTrue);
var ifFalse = Visit(conditionalExpression.IfFalse);

if (TranslationFailed(conditionalExpression.Test, test)
|| TranslationFailed(conditionalExpression.IfTrue, ifTrue)
Expand All @@ -291,36 +297,11 @@ protected override Expression VisitConditional(ConditionalExpression conditional
return _sqlExpressionFactory.Case(
new[]
{
new CaseWhenClause(test, ifTrue)
new CaseWhenClause((SqlExpression)test,(SqlExpression) ifTrue)
},
ifFalse);
(SqlExpression)ifFalse);
}

//protected override Expression VisitNew(NewExpression newExpression)
//{
// if (newExpression.Members == null
// || newExpression.Arguments.Count == 0)
// {
// return null;
// }

// var bindings = new Expression[newExpression.Arguments.Count];

// for (var i = 0; i < bindings.Length; i++)
// {
// var translation = Visit(newExpression.Arguments[i]);

// if (translation == null)
// {
// return null;
// }

// bindings[i] = translation;
// }

// return Expression.Constant(bindings);
//}

protected override Expression VisitUnary(UnaryExpression unaryExpression)
{
var operand = Visit(unaryExpression.Operand);
Expand Down Expand Up @@ -361,7 +342,7 @@ protected override Expression VisitUnary(UnaryExpression unaryExpression)
[DebuggerStepThrough]
private bool TranslationFailed(Expression original, Expression translation)
{
return original != null && translation == null;
return original == null ? false : !(translation is SqlExpression);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1154,5 +1154,27 @@ public virtual Task Cast_on_top_level_projection_brings_explicit_Cast(bool isAsy
isAsync,
os => os.Select(o => (double?)o.OrderID));
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Client_method_in_projection_requiring_materialization_1(bool isAsync)
{
return AssertQuery<Customer>(
isAsync,
cs => cs.Where(c => c.CustomerID.StartsWith("A")).Select(c => c.ToString()),
entryCount: 4);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Client_method_in_projection_requiring_materialization_2(bool isAsync)
{
return AssertQuery<Customer>(
isAsync,
cs => cs.Where(c => c.CustomerID.StartsWith("A")).Select(c => ClientMethod(c)),
entryCount: 4);
}

private static string ClientMethod(Customer c) => c.CustomerID;
}
}

0 comments on commit f9e8c4a

Please sign in to comment.