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

SQLite: Enable ef_compare for decimals #20035

Merged
merged 7 commits into from
Mar 3, 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
Original file line number Diff line number Diff line change
Expand Up @@ -34,35 +34,28 @@ private static readonly IReadOnlyDictionary<ExpressionType, IReadOnlyCollection<
[ExpressionType.GreaterThan] = new HashSet<Type>
{
typeof(DateTimeOffset),
typeof(decimal),
typeof(TimeSpan),
typeof(ulong)
},
[ExpressionType.GreaterThanOrEqual] = new HashSet<Type>
{
typeof(DateTimeOffset),
typeof(decimal),
typeof(TimeSpan),
typeof(ulong)
},
[ExpressionType.LessThan] = new HashSet<Type>
{
typeof(DateTimeOffset),
typeof(decimal),
typeof(TimeSpan),
typeof(ulong)
},
[ExpressionType.LessThanOrEqual] = new HashSet<Type>
{
typeof(DateTimeOffset),
typeof(decimal),
typeof(TimeSpan),
typeof(ulong)
},
[ExpressionType.Modulo] = new HashSet<Type>
{
typeof(ulong)
},
[ExpressionType.Modulo] = new HashSet<Type> { typeof(ulong) },
[ExpressionType.Multiply] = new HashSet<Type>
{
typeof(decimal),
Expand Down Expand Up @@ -98,7 +91,7 @@ protected override Expression VisitUnary(UnaryExpression unaryExpression)
Check.NotNull(unaryExpression, nameof(unaryExpression));

if (unaryExpression.NodeType == ExpressionType.ArrayLength
&& unaryExpression.Operand.Type == typeof(byte[]))
&& unaryExpression.Operand.Type == typeof(byte[]))
{
return base.Visit(unaryExpression.Operand) is SqlExpression sqlExpression
? SqlExpressionFactory.Function(
Expand Down Expand Up @@ -156,6 +149,11 @@ protected override Expression VisitBinary(BinaryExpression binaryExpression)
visitedExpression.TypeMapping);
}

if (AttemptDecimalCompare(sqlBinary))
{
return DoDecimalCompare(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 @@ -233,5 +231,33 @@ private static Type GetProviderType(SqlExpression expression)
: (expression.TypeMapping?.Converter?.ProviderClrType
?? expression.TypeMapping?.ClrType
?? expression.Type).UnwrapNullableType();

private static bool AttemptDecimalCompare(SqlBinaryExpression sqlBinary) =>
GetProviderType(sqlBinary.Left) == typeof(decimal)
&& GetProviderType(sqlBinary.Right) == typeof(decimal)
&& new[]
{
ExpressionType.GreaterThan, ExpressionType.GreaterThanOrEqual, ExpressionType.LessThan, ExpressionType.LessThanOrEqual
}.Contains(sqlBinary.OperatorType);

private Expression DoDecimalCompare(SqlExpression visitedExpression, ExpressionType op, SqlExpression left, SqlExpression right)
{
var actual = SqlExpressionFactory.Function(
name: "ef_compare",
new[] { left, right },
nullable: true,
new[] { true, true },
typeof(int));
var oracle = SqlExpressionFactory.Constant(value: 0);
Copy link
Member

Choose a reason for hiding this comment

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

what is oracle!?

Copy link
Member

Choose a reason for hiding this comment

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

oracle: a person or thing regarded as an infallible authority or guide on something.

Copy link
Contributor

Choose a reason for hiding this comment

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

Kind of like a sentinel but non-arbitrary and with canonical semantics

Copy link
Contributor Author

@lokalmatador lokalmatador Mar 4, 2020

Choose a reason for hiding this comment

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

Exactly. The oracle predicts the expected result (which in this case is 0). But if desired, I'm happy to rename it to, e.g., expectedResult or sth similar.


return op switch
{
ExpressionType.GreaterThan => SqlExpressionFactory.GreaterThan(left: actual, right: oracle),
ExpressionType.GreaterThanOrEqual => SqlExpressionFactory.GreaterThanOrEqual(left: actual, right: oracle),
ExpressionType.LessThan => SqlExpressionFactory.LessThan(left: actual, right: oracle),
ExpressionType.LessThanOrEqual => SqlExpressionFactory.LessThanOrEqual(left: actual, right: oracle),
_ => visitedExpression
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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 @@ -92,7 +93,8 @@ protected override DbConnection CreateDbConnection()
/// </summary>
public virtual ISqliteRelationalConnection CreateReadOnlyConnection()
{
var connectionStringBuilder = new SqliteConnectionStringBuilder(GetCheckedConnectionString()) { Mode = SqliteOpenMode.ReadOnly };
var connectionStringBuilder =
new SqliteConnectionStringBuilder(GetCheckedConnectionString()) { Mode = SqliteOpenMode.ReadOnly };

var contextOptions = new DbContextOptionsBuilder().UseSqlite(connectionStringBuilder.ToString()).Options;

Expand All @@ -117,19 +119,28 @@ private void InitializeDbConnection(DbConnection connection)
"ef_mod",
(dividend, divisor) =>
{
if (dividend == null || divisor == null)
if (dividend == null
|| divisor == null)
{
return null;
}

if (dividend is string s)
{
return decimal.Parse(s, CultureInfo.InvariantCulture) %
Convert.ToDecimal(divisor, CultureInfo.InvariantCulture);
return decimal.Parse(s, CultureInfo.InvariantCulture)
% Convert.ToDecimal(divisor, CultureInfo.InvariantCulture);
}

return Convert.ToDouble(dividend, CultureInfo.InvariantCulture) %
Convert.ToDouble(divisor, CultureInfo.InvariantCulture);
return Convert.ToDouble(dividend, CultureInfo.InvariantCulture)
% Convert.ToDouble(divisor, CultureInfo.InvariantCulture);
});

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