Skip to content
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

Handle expressions that result in top-level case statements in Search… #7331

Closed
wants to merge 1 commit into from
Closed
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 @@ -1652,68 +1652,47 @@ public static bool IsSearchCondition(Expression expression)

protected override Expression VisitBinary(BinaryExpression expression)
{
Expression newLeft;
Expression newRight;
var parentIsSearchCondition = _isSearchCondition;

if (_isSearchCondition)
if (expression.IsLogicalOperation())
Copy link
Contributor

Choose a reason for hiding this comment

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

simplify this as _isSearchCondition = expression.IsLogicalOperation()

{
if (expression.IsComparisonOperation()
|| expression.NodeType == ExpressionType.Or
|| expression.NodeType == ExpressionType.And)
{
var parentIsSearchCondition = _isSearchCondition;

_isSearchCondition = false;

newLeft = AdjustExpressionType(Visit(expression.Left), expression.Left.Type);
newRight = AdjustExpressionType(Visit(expression.Right), expression.Right.Type);

_isSearchCondition = parentIsSearchCondition;

return Expression.MakeBinary(expression.NodeType, newLeft, newRight);
}
_isSearchCondition = true;
}
else
{
if (expression.IsLogicalOperation()
|| expression.NodeType == ExpressionType.Or
|| expression.NodeType == ExpressionType.And)
{
var parentIsSearchCondition = _isSearchCondition;
_isSearchCondition = expression.IsLogicalOperation();

newLeft = Visit(expression.Left);
newRight = Visit(expression.Right);
_isSearchCondition = false;
}

_isSearchCondition = parentIsSearchCondition;
}
else
{
newLeft = Visit(expression.Left);
newRight = Visit(expression.Right);
}
var newLeft = AdjustExpressionType(Visit(expression.Left), expression.Left.Type);
var newRight = AdjustExpressionType(Visit(expression.Right), expression.Right.Type);

newLeft = AdjustExpressionType(newLeft, expression.Left.Type);
newRight = AdjustExpressionType(newRight, expression.Right.Type);
_isSearchCondition = parentIsSearchCondition;

var newExpression
= expression.Update(newLeft, expression.Conversion, newRight);
var newExpression = expression.Update(newLeft, expression.Conversion, newRight);

if (IsSearchCondition(newExpression))
{
return Expression.Condition(
newExpression.Type == typeof(bool)
? (Expression)newExpression
: Expression.Convert(newExpression, typeof(bool)),
Expression.Constant(true, typeof(bool)),
Expression.Constant(false, typeof(bool)));
}
if (!_isSearchCondition
&& IsSearchCondition(newExpression))
{
return Expression.Condition(
newExpression.Type == typeof(bool)
? (Expression)newExpression
: Expression.Convert(newExpression, typeof(bool)),
Expression.Constant(true, typeof(bool)),
Expression.Constant(false, typeof(bool)));
}
else if (_isSearchCondition
&& newExpression.Left is ConditionalExpression
&& newExpression.Right is ConditionalExpression
&& (newExpression.NodeType == ExpressionType.Or
|| newExpression.NodeType == ExpressionType.And))
{
return Expression.MakeBinary(
ExpressionType.Equal,
newExpression,
Expression.Constant(true, typeof(bool)));
}

newLeft = AdjustExpressionType(Visit(expression.Left), expression.Left.Type);
newRight = AdjustExpressionType(Visit(expression.Right), expression.Right.Type);

return expression.Update(newLeft, expression.Conversion, newRight);
return newExpression;
}

private static Expression AdjustExpressionType(Expression expression, Type expectedType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2028,6 +2028,43 @@ public virtual void Where_Is_on_same_type()
entryCount: 91);
}

[ConditionalFact]
Copy link
Contributor

Choose a reason for hiding this comment

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

Also add same tests in AsyncQueryTestBase. You don't need to assert SQL gen for those.

public virtual void Where_binary_or_with_logical_and()
{
AssertQuery<Order>(os => os
.Where(o => o.OrderID > 54321 | o.OrderID > 98765)
.Where(o => o.OrderID > 1234567));
}

[ConditionalFact]
public virtual void Where_binary_and_with_logical_and()
{
AssertQuery<Order>(os => os
.Where(o => o.OrderID > 54321 & o.OrderID > 98765)
.Where(o => o.OrderID > 1234567));
}

[ConditionalFact]
public virtual void Where_binary_or_with_logical_or()
{
AssertQuery<Order>(os => os
.Where(o => (o.OrderID > 54321 | o.OrderID > 98765) || o.OrderID > 1234567));
}

[ConditionalFact]
public virtual void Where_binary_and_with_logical_or()
{
AssertQuery<Order>(os => os
.Where(o => (o.OrderID > 54321 & o.OrderID > 98765) || o.OrderID > 1234567));
}

[ConditionalFact]
public virtual void Where_binary_or_with_binary_or()
{
AssertQuery<Order>(os => os
.Where(o => o.OrderID > 54321 | o.OrderID > 98765 | o.OrderID > 1234567));
}

[ConditionalFact]
Copy link
Contributor

Choose a reason for hiding this comment

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

Just for coverage - Test for binary and with binary and.

public virtual void Select_scalar()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4452,6 +4452,94 @@ public override void Where_Is_on_same_type()
Sql);
}

public override void Where_binary_or_with_logical_and()
{
base.Where_binary_or_with_logical_and();

Assert.Equal(
@"SELECT [o].[OrderID], [o].[CustomerID], [o].[EmployeeID], [o].[OrderDate]
FROM [Orders] AS [o]
WHERE ((CASE
WHEN [o].[OrderID] > 54321
THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
END | CASE
WHEN [o].[OrderID] > 98765
THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
END) = 1) AND ([o].[OrderID] > 1234567)",
Sql);
}

public override void Where_binary_and_with_logical_and()
{
base.Where_binary_and_with_logical_and();

Assert.Equal(
@"SELECT [o].[OrderID], [o].[CustomerID], [o].[EmployeeID], [o].[OrderDate]
FROM [Orders] AS [o]
WHERE ((CASE
WHEN [o].[OrderID] > 54321
THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
END & CASE
WHEN [o].[OrderID] > 98765
THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
END) = 1) AND ([o].[OrderID] > 1234567)",
Sql);
}

public override void Where_binary_or_with_logical_or()
{
base.Where_binary_or_with_logical_or();

Assert.Equal(
@"SELECT [o].[OrderID], [o].[CustomerID], [o].[EmployeeID], [o].[OrderDate]
FROM [Orders] AS [o]
WHERE ((CASE
WHEN [o].[OrderID] > 54321
THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
END | CASE
WHEN [o].[OrderID] > 98765
THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
END) = 1) OR ([o].[OrderID] > 1234567)",
Sql);
}

public override void Where_binary_and_with_logical_or()
{
base.Where_binary_and_with_logical_or();

Assert.Equal(
@"SELECT [o].[OrderID], [o].[CustomerID], [o].[EmployeeID], [o].[OrderDate]
FROM [Orders] AS [o]
WHERE ((CASE
WHEN [o].[OrderID] > 54321
THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
END & CASE
WHEN [o].[OrderID] > 98765
THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
END) = 1) OR ([o].[OrderID] > 1234567)",
Sql);
}

public override void Where_binary_or_with_binary_or()
{
base.Where_binary_or_with_binary_or();

Assert.Equal(
@"SELECT [o].[OrderID], [o].[CustomerID], [o].[EmployeeID], [o].[OrderDate]
FROM [Orders] AS [o]
WHERE ((CASE
WHEN [o].[OrderID] > 54321
THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
END | CASE
WHEN [o].[OrderID] > 98765
THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
END) | CASE
WHEN [o].[OrderID] > 1234567
THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
END) = 1",
Sql);
}

public override void Single_Predicate()
{
base.Single_Predicate();
Expand Down