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

Basic arithmetics #20212

Merged
merged 20 commits into from
Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from 13 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 @@ -22,15 +22,9 @@ private static readonly IReadOnlyDictionary<ExpressionType, IReadOnlyCollection<
{
typeof(DateTime),
typeof(DateTimeOffset),
typeof(decimal),
typeof(TimeSpan)
},
[ExpressionType.Divide] = new HashSet<Type>
{
typeof(decimal),
typeof(TimeSpan),
typeof(ulong)
Copy link
Contributor

Choose a reason for hiding this comment

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

We should file an issue to do the ulong ones too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess it would make sense, yes. I'd be happy to work on this, when decimals are finished, if OK.

},
[ExpressionType.Divide] = new HashSet<Type> { typeof(TimeSpan), typeof(ulong) },
[ExpressionType.GreaterThan] = new HashSet<Type>
{
typeof(DateTimeOffset),
Expand All @@ -56,17 +50,11 @@ private static readonly IReadOnlyDictionary<ExpressionType, IReadOnlyCollection<
typeof(ulong)
},
[ExpressionType.Modulo] = new HashSet<Type> { typeof(ulong) },
[ExpressionType.Multiply] = new HashSet<Type>
{
typeof(decimal),
typeof(TimeSpan),
typeof(ulong)
},
[ExpressionType.Multiply] = new HashSet<Type> { typeof(TimeSpan), typeof(ulong) },
[ExpressionType.Subtract] = new HashSet<Type>
{
typeof(DateTime),
typeof(DateTimeOffset),
typeof(decimal),
typeof(TimeSpan)
}
};
Expand Down Expand Up @@ -113,8 +101,17 @@ protected override Expression VisitUnary(UnaryExpression unaryExpression)
&& sqlUnary.OperatorType == ExpressionType.Negate)
{
var operandType = GetProviderType(sqlUnary.Operand);
if (operandType == typeof(decimal)
|| operandType == typeof(TimeSpan))
if (operandType == typeof(decimal))
{
return SqlExpressionFactory.Function(
name: "ef_negate",
new[] { sqlUnary.Operand },
nullable: true,
new[] { true },
visitedExpression.Type);
}

if (operandType == typeof(TimeSpan))
{
return null;
}
Expand Down Expand Up @@ -154,6 +151,11 @@ protected override Expression VisitBinary(BinaryExpression binaryExpression)
return DoDecimalCompare(visitedExpression, sqlBinary.OperatorType, sqlBinary.Left, sqlBinary.Right);
}

if (AttemptDecimalArithmetic(sqlBinary))
{
return DoDecimalArithmetics(visitedExpression, sqlBinary.OperatorType, sqlBinary.Left, sqlBinary.Right);
}

if (_restrictedBinaryExpressions.TryGetValue(sqlBinary.OperatorType, out var restrictedTypes)
&& (restrictedTypes.Contains(GetProviderType(sqlBinary.Left))
|| restrictedTypes.Contains(GetProviderType(sqlBinary.Right))))
Expand Down Expand Up @@ -232,9 +234,11 @@ private static Type GetProviderType(SqlExpression expression)
?? expression.TypeMapping?.ClrType
?? expression.Type).UnwrapNullableType();

private static bool AreOperandsDecimals(SqlBinaryExpression sqlExpression) => GetProviderType(sqlExpression.Left) == typeof(decimal)
&& GetProviderType(sqlExpression.Right) == typeof(decimal);

private static bool AttemptDecimalCompare(SqlBinaryExpression sqlBinary) =>
GetProviderType(sqlBinary.Left) == typeof(decimal)
&& GetProviderType(sqlBinary.Right) == typeof(decimal)
AreOperandsDecimals(sqlBinary)
&& new[]
{
ExpressionType.GreaterThan, ExpressionType.GreaterThanOrEqual, ExpressionType.LessThan, ExpressionType.LessThanOrEqual
Expand All @@ -259,5 +263,56 @@ private Expression DoDecimalCompare(SqlExpression visitedExpression, ExpressionT
_ => visitedExpression
};
}

private static bool AttemptDecimalArithmetic(SqlBinaryExpression sqlBinary) =>
AreOperandsDecimals(sqlBinary)
&& new[] { ExpressionType.Add, ExpressionType.Subtract, ExpressionType.Multiply, ExpressionType.Divide }.Contains(
sqlBinary.OperatorType);

private Expression DoDecimalArithmetics(SqlExpression visitedExpression, ExpressionType op, SqlExpression left, SqlExpression right)
{
return op switch
{
ExpressionType.Add => DecimalArithmeticExpressionFactoryMethod(ResolveFunctionNameFromExpressionType(op), left, right),
ExpressionType.Divide => DecimalArithmeticExpressionFactoryMethod(ResolveFunctionNameFromExpressionType(op), left, right),
ExpressionType.Multiply => DecimalArithmeticExpressionFactoryMethod(ResolveFunctionNameFromExpressionType(op), left, right),
ExpressionType.Subtract => DecimalSubtractExpressionFactoryMethod(left, right),
_ => visitedExpression
};

string ResolveFunctionNameFromExpressionType(ExpressionType expressionType)
Copy link
Member

Choose a reason for hiding this comment

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

static

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

{
return expressionType switch
{
ExpressionType.Add => "ef_add",
ExpressionType.Divide => "ef_divide",
ExpressionType.Multiply => "ef_multiply",
ExpressionType.Subtract => "ef_add",
_ => throw new InvalidOperationException()
};
}

Expression DecimalArithmeticExpressionFactoryMethod(string name, SqlExpression left, SqlExpression right)
{
return SqlExpressionFactory.Function(
name,
new[] { left, right },
nullable: true,
new[] { true, true },
visitedExpression.Type);
}

Expression DecimalSubtractExpressionFactoryMethod(SqlExpression left, SqlExpression right)
{
var subtrahend = SqlExpressionFactory.Function(
"ef_negate",
new[] { right },
nullable: true,
new[] { true },
visitedExpression.Type);

return DecimalArithmeticExpressionFactoryMethod(ResolveFunctionNameFromExpressionType(op), left, subtrahend);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Query.Internal;
using Microsoft.EntityFrameworkCore.Sqlite.Infrastructure.Internal;
using Microsoft.EntityFrameworkCore.Sqlite.Internal;
using Microsoft.EntityFrameworkCore.Storage;
Expand Down Expand Up @@ -135,14 +134,35 @@ private void InitializeDbConnection(DbConnection connection)
% Convert.ToDouble(divisor, CultureInfo.InvariantCulture);
});

sqliteConnection.CreateFunction(
name: "ef_add",
(decimal? left, decimal? right) => left + right,
isDeterministic: true);

sqliteConnection.CreateFunction(
name: "ef_divide",
(decimal? dividend, decimal? divisor) => dividend / divisor,
isDeterministic: true);

sqliteConnection.CreateFunction(
name: "ef_compare",
(decimal? left, decimal? right) => left.HasValue && right.HasValue
? decimal.Compare(left.Value, right.Value)
: default(int?),
isDeterministic: true);

sqliteConnection.CreateFunction(
name: "ef_multiply",
(decimal? left, decimal? right) => left * right,
isDeterministic: true);

sqliteConnection.CreateFunction(
name: "ef_negate",
(decimal? m) => -m,
isDeterministic: true);
}
else

{
_logger.UnexpectedConnectionTypeWarning(connection.GetType());
}
Expand Down