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

Fix to #18555 - Query: when rewriting null semantics for comparisons with functions use function specific metadata to get better SQL #19607

Merged
merged 1 commit into from
Jan 22, 2020
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
48 changes: 48 additions & 0 deletions src/EFCore.Relational/Query/ISqlExpressionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,54 @@ SqlFunctionExpression Function(
[NotNull] Type returnType,
[CanBeNull] RelationalTypeMapping typeMapping = null);

SqlFunctionExpression Function(
[NotNull] string name,
[NotNull] IEnumerable<SqlExpression> arguments,
bool nullResultAllowed,
[NotNull] IEnumerable<bool> argumentsPropagateNullability,
[NotNull] Type returnType,
[CanBeNull] RelationalTypeMapping typeMapping = null);

SqlFunctionExpression Function(
[CanBeNull] string schema,
[NotNull] string name,
[NotNull] IEnumerable<SqlExpression> arguments,
bool nullResultAllowed,
Copy link
Member

Choose a reason for hiding this comment

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

canBeNull would be better name

Copy link
Contributor Author

Choose a reason for hiding this comment

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

will go with nullable/isnullable, just like column expression

[NotNull] IEnumerable<bool> argumentsPropagateNullability,
[NotNull] Type returnType,
[CanBeNull] RelationalTypeMapping typeMapping = null);

SqlFunctionExpression Function(
[CanBeNull] SqlExpression instance,
[NotNull] string name,
[NotNull] IEnumerable<SqlExpression> arguments,
bool nullResultAllowed,
bool instancePropagatesNullability,
[NotNull] IEnumerable<bool> argumentsPropagateNullability,
[NotNull] Type returnType,
[CanBeNull] RelationalTypeMapping typeMapping = null);

SqlFunctionExpression Function(
[NotNull] string name,
bool nullResultAllowed,
[NotNull] Type returnType,
[CanBeNull] RelationalTypeMapping typeMapping = null);

SqlFunctionExpression Function(
[NotNull] string schema,
[NotNull] string name,
bool nullResultAllowed,
[NotNull] Type returnType,
[CanBeNull] RelationalTypeMapping typeMapping = null);

SqlFunctionExpression Function(
[CanBeNull] SqlExpression instance,
[NotNull] string name,
bool nullResultAllowed,
bool instancePropagatesNullability,
[NotNull] Type returnType,
[CanBeNull] RelationalTypeMapping typeMapping = null);

ExistsExpression Exists([NotNull] SelectExpression subquery, bool negated);
InExpression In([NotNull] SqlExpression item, [NotNull] SqlExpression values, bool negated);
InExpression In([NotNull] SqlExpression item, [NotNull] SelectExpression subquery, bool negated);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -898,8 +898,7 @@ private SqlExpression RewriteNullSemantics(
return sqlBinaryExpression.Update(left, right);
}

private SqlExpression SimplifyLogicalSqlBinaryExpression(
SqlBinaryExpression sqlBinaryExpression)
private SqlExpression SimplifyLogicalSqlBinaryExpression(SqlBinaryExpression sqlBinaryExpression)
{
var leftUnary = sqlBinaryExpression.Left as SqlUnaryExpression;
var rightUnary = sqlBinaryExpression.Right as SqlUnaryExpression;
Expand Down Expand Up @@ -1253,37 +1252,96 @@ protected virtual SqlExpression ProcessNullNotNull(
sqlUnaryExpression.TypeMapping));
}

case SqlFunctionExpression sqlFunctionExpression
when sqlFunctionExpression.IsBuiltIn && string.Equals("COALESCE", sqlFunctionExpression.Name, StringComparison.OrdinalIgnoreCase):
case SqlFunctionExpression sqlFunctionExpression:
{
// for coalesce:
// (a ?? b) == null -> a == null && b == null
// (a ?? b) != null -> a != null || b != null
var left = ProcessNullNotNull(
SqlExpressionFactory.MakeUnary(
sqlUnaryExpression.OperatorType,
sqlFunctionExpression.Arguments[0],
typeof(bool),
sqlUnaryExpression.TypeMapping),
operandNullable: null);
if (sqlFunctionExpression.IsBuiltIn && string.Equals("COALESCE", sqlFunctionExpression.Name, StringComparison.OrdinalIgnoreCase))
{
// for coalesce:
// (a ?? b) == null -> a == null && b == null
// (a ?? b) != null -> a != null || b != null
var left = ProcessNullNotNull(
SqlExpressionFactory.MakeUnary(
sqlUnaryExpression.OperatorType,
sqlFunctionExpression.Arguments[0],
typeof(bool),
sqlUnaryExpression.TypeMapping),
operandNullable: null);

var right = ProcessNullNotNull(
SqlExpressionFactory.MakeUnary(
sqlUnaryExpression.OperatorType,
sqlFunctionExpression.Arguments[1],
typeof(bool),
sqlUnaryExpression.TypeMapping),
operandNullable: null);

var right = ProcessNullNotNull(
SqlExpressionFactory.MakeUnary(
sqlUnaryExpression.OperatorType,
sqlFunctionExpression.Arguments[1],
typeof(bool),
sqlUnaryExpression.TypeMapping),
operandNullable: null);
return SimplifyLogicalSqlBinaryExpression(
SqlExpressionFactory.MakeBinary(
sqlUnaryExpression.OperatorType == ExpressionType.Equal
? ExpressionType.AndAlso
: ExpressionType.OrElse,
left,
right,
sqlUnaryExpression.TypeMapping));
}

return SimplifyLogicalSqlBinaryExpression(
SqlExpressionFactory.MakeBinary(
sqlUnaryExpression.OperatorType == ExpressionType.Equal
? ExpressionType.AndAlso
: ExpressionType.OrElse,
left,
right,
sqlUnaryExpression.TypeMapping));
if (!sqlFunctionExpression.NullResultAllowed)
{
// when we know that function can't be nullable:
// non_nullable_function() is null-> false
// non_nullable_function() is not null -> true
return SqlExpressionFactory.Constant(
sqlUnaryExpression.OperatorType == ExpressionType.NotEqual,
sqlUnaryExpression.TypeMapping);
}

// see if we can derive function nullability from it's instance and/or arguments
// rather than evaluating nullability of the entire function
var nullabilityPropagationElements = new List<SqlExpression>();
if (sqlFunctionExpression.Instance != null
&& sqlFunctionExpression.InstancPropagatesNullability == true)
{
nullabilityPropagationElements.Add(sqlFunctionExpression.Instance);
}

for (var i = 0; i < sqlFunctionExpression.Arguments.Count; i++)
Copy link
Member

Choose a reason for hiding this comment

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

Check if is niladic to avoid for loop.

{
if (sqlFunctionExpression.ArgumentsPropagateNullability[i])
{
nullabilityPropagationElements.Add(sqlFunctionExpression.Arguments[i]);
}
}

if (nullabilityPropagationElements.Count > 0)
{
var result = ProcessNullNotNull(
Copy link
Member

Choose a reason for hiding this comment

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

Using aggregate could be cleaner.

SqlExpressionFactory.MakeUnary(
sqlUnaryExpression.OperatorType,
nullabilityPropagationElements[0],
sqlUnaryExpression.Type,
sqlUnaryExpression.TypeMapping),
operandNullable: null);

foreach (var element in nullabilityPropagationElements.Skip(1))
{
result = SimplifyLogicalSqlBinaryExpression(
sqlUnaryExpression.OperatorType == ExpressionType.Equal
? SqlExpressionFactory.OrElse(
result,
ProcessNullNotNull(
SqlExpressionFactory.IsNull(element),
operandNullable: null))
: SqlExpressionFactory.AndAlso(
result,
ProcessNullNotNull(
SqlExpressionFactory.IsNotNull(element),
operandNullable: null)));
}

return result;
}
}
break;
}

return sqlUnaryExpression;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public virtual SqlExpression Translate(
dbFunction.Schema,
dbFunction.Name,
arguments,
nullResultAllowed: true,
argumentsPropagateNullability: arguments.Select(a => true).ToList(),
Copy link
Member

Choose a reason for hiding this comment

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

default should be false.

method.ReturnType);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,20 @@ public virtual SqlExpression TranslateAverage([NotNull] Expression expression)
return inputType == typeof(float)
? SqlExpressionFactory.Convert(
SqlExpressionFactory.Function(
"AVG", new[] { sqlExpression }, typeof(double)),
"AVG",
new[] { sqlExpression },
nullResultAllowed: true,
argumentsPropagateNullability: new[] { false },
typeof(double)),
sqlExpression.Type,
sqlExpression.TypeMapping)
: (SqlExpression)SqlExpressionFactory.Function(
"AVG", new[] { sqlExpression }, sqlExpression.Type, sqlExpression.TypeMapping);
"AVG",
new[] { sqlExpression },
nullResultAllowed: true,
argumentsPropagateNullability: new[] { false },
sqlExpression.Type,
sqlExpression.TypeMapping);
}

public virtual SqlExpression TranslateCount([CanBeNull] Expression expression = null)
Expand All @@ -115,7 +124,12 @@ public virtual SqlExpression TranslateCount([CanBeNull] Expression expression =
}

return SqlExpressionFactory.ApplyDefaultTypeMapping(
SqlExpressionFactory.Function("COUNT", new[] { SqlExpressionFactory.Fragment("*") }, typeof(int)));
SqlExpressionFactory.Function(
"COUNT",
new[] { SqlExpressionFactory.Fragment("*") },
nullResultAllowed: false,
argumentsPropagateNullability: new[] { false },
typeof(int)));
}

public virtual SqlExpression TranslateLongCount([CanBeNull] Expression expression = null)
Expand All @@ -127,7 +141,12 @@ public virtual SqlExpression TranslateLongCount([CanBeNull] Expression expressio
}

return SqlExpressionFactory.ApplyDefaultTypeMapping(
SqlExpressionFactory.Function("COUNT", new[] { SqlExpressionFactory.Fragment("*") }, typeof(long)));
SqlExpressionFactory.Function(
"COUNT",
new[] { SqlExpressionFactory.Fragment("*") },
nullResultAllowed: false,
argumentsPropagateNullability: new[] { false },
typeof(long)));
}

public virtual SqlExpression TranslateMax([NotNull] Expression expression)
Expand All @@ -140,7 +159,13 @@ public virtual SqlExpression TranslateMax([NotNull] Expression expression)
}

return sqlExpression != null
? SqlExpressionFactory.Function("MAX", new[] { sqlExpression }, sqlExpression.Type, sqlExpression.TypeMapping)
? SqlExpressionFactory.Function(
"MAX",
new[] { sqlExpression },
nullResultAllowed: true,
argumentsPropagateNullability: new[] { false },
sqlExpression.Type,
sqlExpression.TypeMapping)
: null;
}

Expand All @@ -154,7 +179,13 @@ public virtual SqlExpression TranslateMin([NotNull] Expression expression)
}

return sqlExpression != null
? SqlExpressionFactory.Function("MIN", new[] { sqlExpression }, sqlExpression.Type, sqlExpression.TypeMapping)
? SqlExpressionFactory.Function(
"MIN",
new[] { sqlExpression },
nullResultAllowed: true,
argumentsPropagateNullability: new[] { false },
sqlExpression.Type,
sqlExpression.TypeMapping)
: null;
}

Expand All @@ -176,11 +207,21 @@ public virtual SqlExpression TranslateSum([NotNull] Expression expression)

return inputType == typeof(float)
? SqlExpressionFactory.Convert(
SqlExpressionFactory.Function("SUM", new[] { sqlExpression }, typeof(double)),
SqlExpressionFactory.Function(
"SUM",
new[] { sqlExpression },
nullResultAllowed: true,
argumentsPropagateNullability: new[] { false },
typeof(double)),
inputType,
sqlExpression.TypeMapping)
: (SqlExpression)SqlExpressionFactory.Function(
"SUM", new[] { sqlExpression }, inputType, sqlExpression.TypeMapping);
"SUM",
new[] { sqlExpression },
nullResultAllowed: true,
argumentsPropagateNullability: new[] { false },
inputType,
sqlExpression.TypeMapping);
}

private sealed class SqlTypeMappingVerifyingExpressionVisitor : ExpressionVisitor
Expand Down
Loading