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 1 commit
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 @@ -59,10 +59,7 @@ private static readonly IReadOnlyDictionary<ExpressionType, IReadOnlyCollection<
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 +95,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 +153,71 @@ protected override Expression VisitBinary(BinaryExpression binaryExpression)
visitedExpression.TypeMapping);
}

if (sqlBinary.OperatorType == ExpressionType.GreaterThan
&& (_functionModuloTypes.Contains(GetProviderType(sqlBinary.Left))
|| _functionModuloTypes.Contains(GetProviderType(sqlBinary.Right))))
bricelam marked this conversation as resolved.
Show resolved Hide resolved
{
return SqlExpressionFactory.Function(
"ef_compare_gt",
bricelam marked this conversation as resolved.
Show resolved Hide resolved
new[] { sqlBinary.Left, sqlBinary.Right },
nullable: true,
argumentsPropagateNullability: new[] { true, true },
visitedExpression.Type,
visitedExpression.TypeMapping);
}

if (sqlBinary.OperatorType == ExpressionType.GreaterThanOrEqual
&& (_functionModuloTypes.Contains(GetProviderType(sqlBinary.Left))
|| _functionModuloTypes.Contains(GetProviderType(sqlBinary.Right))))
{
return SqlExpressionFactory.Function(
"ef_compare_geq",
new[] { sqlBinary.Left, sqlBinary.Right },
nullable: true,
argumentsPropagateNullability: new[] { true, true },
visitedExpression.Type,
visitedExpression.TypeMapping);
}

if (sqlBinary.OperatorType == ExpressionType.LessThan
&& (_functionModuloTypes.Contains(GetProviderType(sqlBinary.Left))
|| _functionModuloTypes.Contains(GetProviderType(sqlBinary.Right))))
{
return SqlExpressionFactory.Function(
"ef_compare_lt",
new[] { sqlBinary.Left, sqlBinary.Right },
nullable: true,
argumentsPropagateNullability: new[] { true, true },
visitedExpression.Type,
visitedExpression.TypeMapping);
}

if (sqlBinary.OperatorType == ExpressionType.LessThanOrEqual
&& (_functionModuloTypes.Contains(GetProviderType(sqlBinary.Left))
|| _functionModuloTypes.Contains(GetProviderType(sqlBinary.Right))))
{
return SqlExpressionFactory.Function(
"ef_compare_leq",
new[] { sqlBinary.Left, sqlBinary.Right },
nullable: true,
argumentsPropagateNullability: new[] { true, true },
visitedExpression.Type,
visitedExpression.TypeMapping);
}

if (sqlBinary.OperatorType == ExpressionType.Equal
&& (_functionModuloTypes.Contains(GetProviderType(sqlBinary.Left))
|| _functionModuloTypes.Contains(GetProviderType(sqlBinary.Right))))
{
return SqlExpressionFactory.Function(
"ef_compare_eq",
new[] { sqlBinary.Left, sqlBinary.Right },
nullable: true,
argumentsPropagateNullability: new[] { true, true },
visitedExpression.Type,
visitedExpression.TypeMapping);
}

if (_restrictedBinaryExpressions.TryGetValue(sqlBinary.OperatorType, out var restrictedTypes)
&& (restrictedTypes.Contains(GetProviderType(sqlBinary.Left))
|| restrictedTypes.Contains(GetProviderType(sqlBinary.Right))))
Expand Down
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,24 +119,98 @@ 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);
});

CreateEfCompareFunctions(sqliteConnection);
}
else
{
_logger.UnexpectedConnectionTypeWarning(connection.GetType());
}
}

private void CreateEfCompareFunctions(SqliteConnection sqliteConnection)
{
var functions = new[]
{
("ef_compare_gt", Comparer.Operator.GreaterThan),
("ef_compare_geq", Comparer.Operator.GreaterThanOrEqual),
("ef_compare_lt", Comparer.Operator.LessThan),
("ef_compare_leq", Comparer.Operator.LessThanOrEqual),
("ef_compare_eq", Comparer.Operator.Equal)
};

foreach (var function in functions)
{
sqliteConnection.CreateFunction<object, object, object>(
function.Item1,
(left, right) =>
{
if (left == null
|| right == null)
{
return null;
}

var leftSide = left is string leftAsString
? decimal.Parse(leftAsString, CultureInfo.CurrentCulture)
: Convert.ToDecimal(left, CultureInfo.CurrentCulture);
var rightSide = right is string rightAsString
? decimal.Parse(rightAsString, CultureInfo.CurrentCulture)
: Convert.ToDecimal(right, CultureInfo.CurrentCulture);

return Comparer.IsTrue(
Convert.ToDecimal(leftSide, CultureInfo.CurrentCulture), function.Item2,
Convert.ToDecimal(rightSide, CultureInfo.CurrentCulture));
});
}
}

internal static class Comparer
{
public static bool IsTrue<T, U>(T value1, Operator comparisonOperator, U value2)
where T : U
where U : IComparable
{
switch (comparisonOperator)
{
case Operator.GreaterThan:
return value1.CompareTo(value2) > 0;
case Operator.GreaterThanOrEqual:
return value1.CompareTo(value2) >= 0;
case Operator.LessThan:
return value1.CompareTo(value2) < 0;
case Operator.LessThanOrEqual:
return value1.CompareTo(value2) <= 0;
case Operator.Equal:
return value1.CompareTo(value2) == 0;
default:
return false;
}
}

internal enum Operator
{
GreaterThan = 1,
GreaterThanOrEqual = 2,
LessThan = 3,
LessThanOrEqual = 4,
Equal = 5
}
}
}
}