Skip to content

Commit 69ae99a

Browse files
committed
Implement improvements from code review
1 parent bbe125a commit 69ae99a

File tree

1 file changed

+28
-36
lines changed

1 file changed

+28
-36
lines changed

src/EFCore.Relational/Query/SqlNullabilityProcessor.cs

+28-36
Original file line numberDiff line numberDiff line change
@@ -579,23 +579,15 @@ protected virtual SqlExpression VisitCase(CaseExpression caseExpression, bool al
579579
}
580580

581581
// optimize expressions such as expr != null ? expr : null and expr == null ? null : expr
582-
if (testIsCondition && whenClauses is [var clause] && (elseResult == null || IsNull(clause.Result)))
582+
if (testIsCondition && whenClauses is [var clause] && (elseResult is null || IsNull(clause.Result)))
583583
{
584584
HashSet<SqlExpression> nullPropagatedOperands = [];
585-
SqlExpression test, expr;
586585

587-
if (elseResult == null)
588-
{
589-
expr = clause.Result;
590-
test = clause.Test;
591-
}
592-
else
593-
{
594-
expr = elseResult;
595-
test = _sqlExpressionFactory.Not(clause.Test);
596-
}
586+
var (test, expr) = elseResult is null
587+
? (clause.Test, clause.Result)
588+
: (_sqlExpressionFactory.Not(clause.Test), elseResult);
597589

598-
NullPropagatedOperands(expr, nullPropagatedOperands);
590+
DetectNullPropagatingNodes(expr, nullPropagatedOperands);
599591
test = DropNotNullChecks(test, nullPropagatedOperands);
600592

601593
if (IsTrue(test))
@@ -632,38 +624,38 @@ when nullPropagatedOperands.Contains(isNotNull.Operand)
632624
};
633625

634626
// FIXME: unify nullability computations
635-
static void NullPropagatedOperands(SqlExpression expression, HashSet<SqlExpression> operands)
627+
static void DetectNullPropagatingNodes(SqlExpression expression, HashSet<SqlExpression> operands)
636628
{
637629
operands.Add(expression);
638630

639-
if (expression is SqlUnaryExpression unary
640-
&& unary.OperatorType is ExpressionType.Not or ExpressionType.Negate or ExpressionType.Convert)
631+
switch (expression)
641632
{
642-
NullPropagatedOperands(unary.Operand, operands);
643-
}
644-
else if (expression is SqlBinaryExpression binary
645-
&& binary.OperatorType is not (ExpressionType.AndAlso or ExpressionType.OrElse))
646-
{
647-
NullPropagatedOperands(binary.Left, operands);
648-
NullPropagatedOperands(binary.Right, operands);
649-
}
650-
else if (expression is SqlFunctionExpression { IsNullable: true } func)
651-
{
652-
if (func.InstancePropagatesNullability == true)
653-
{
654-
NullPropagatedOperands(func.Instance!, operands);
655-
}
633+
case SqlUnaryExpression { OperatorType: not (ExpressionType.Equal or ExpressionType.NotEqual) } unary:
634+
DetectNullPropagatingNodes(unary.Operand, operands);
635+
break;
656636

657-
if (!func.IsNiladic)
658-
{
659-
for (var i = 0; i < func.ArgumentsPropagateNullability.Count; i++)
637+
case SqlBinaryExpression { OperatorType: not (ExpressionType.AndAlso or ExpressionType.OrElse) } binary:
638+
DetectNullPropagatingNodes(binary.Left, operands);
639+
DetectNullPropagatingNodes(binary.Right, operands);
640+
break;
641+
642+
case SqlFunctionExpression { IsNullable: true } func:
643+
if (func.InstancePropagatesNullability is true)
660644
{
661-
if (func.ArgumentsPropagateNullability[i])
645+
DetectNullPropagatingNodes(func.Instance!, operands);
646+
}
647+
648+
if (!func.IsNiladic)
649+
{
650+
for (var i = 0; i < func.ArgumentsPropagateNullability.Count; i++)
662651
{
663-
NullPropagatedOperands(func.Arguments[i], operands);
652+
if (func.ArgumentsPropagateNullability[i])
653+
{
654+
DetectNullPropagatingNodes(func.Arguments[i], operands);
655+
}
664656
}
665657
}
666-
}
658+
break;
667659
}
668660
}
669661
}

0 commit comments

Comments
 (0)