Skip to content

Commit

Permalink
Require in-support version for array translations
Browse files Browse the repository at this point in the history
  • Loading branch information
austindrenski committed Dec 12, 2018
1 parent 75293ec commit 2ea34f1
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,18 @@ public class NpgsqlArrayMemberTranslator : IMemberTranslator

/// <inheritdoc />
public Expression Translate(MemberExpression e)
=> ArrayInstanceHandler(e) ??
ListInstanceHandler(e);
{
if (!VersionAtLeast(9, 4))
return null;

return ArrayInstanceHandler(e) ??
ListInstanceHandler(e);
}

#region Handlers

[CanBeNull]
Expression ArrayInstanceHandler([NotNull] MemberExpression e)
static Expression ArrayInstanceHandler([NotNull] MemberExpression e)
{
var instance = e.Expression;

Expand All @@ -44,15 +49,15 @@ Expression ArrayInstanceHandler([NotNull] MemberExpression e)

switch (e.Member.Name)
{
case nameof(Array.Length) when VersionAtLeast(8, 4):
case nameof(Array.Length):
return Expression.Coalesce(
new SqlFunctionExpression(
"array_length",
typeof(int?),
new[] { instance, Expression.Constant(1) }),
Expression.Constant(0));

case nameof(Array.Rank) when VersionAtLeast(8, 4):
case nameof(Array.Rank):
return Expression.Coalesce(
new SqlFunctionExpression(
"array_ndims",
Expand All @@ -66,7 +71,7 @@ Expression ArrayInstanceHandler([NotNull] MemberExpression e)
}

[CanBeNull]
Expression ListInstanceHandler([NotNull] MemberExpression e)
static Expression ListInstanceHandler([NotNull] MemberExpression e)
{
var instance = e.Expression;

Expand All @@ -75,7 +80,7 @@ Expression ListInstanceHandler([NotNull] MemberExpression e)

switch (e.Member.Name)
{
case nameof(IList.Count) when VersionAtLeast(8, 4):
case nameof(IList.Count):
return Expression.Coalesce(
new SqlFunctionExpression(
"array_length",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public class NpgsqlArrayMethodCallTranslator : IMethodCallTranslator
[CanBeNull]
public Expression Translate(MethodCallExpression e)
{
if (!VersionAtLeast(9, 4))
return null;

var declaringType = e.Method.DeclaringType;

if (declaringType != null &&
Expand Down Expand Up @@ -68,7 +71,7 @@ Expression EnumerableHandler([NotNull] MethodCallExpression e)

switch (e.Method.Name)
{
case nameof(Enumerable.Count) when VersionAtLeast(8, 4):
case nameof(Enumerable.Count):
return Expression.Coalesce(
new SqlFunctionExpression(
"array_length",
Expand Down Expand Up @@ -104,19 +107,19 @@ Expression NpgsqlArrayExtensionsHandler([NotNull] MethodCallExpression e)

switch (e.Method.Name)
{
case nameof(NpgsqlArrayExtensions.Contains) when VersionAtLeast(8, 2):
case nameof(NpgsqlArrayExtensions.Contains):
return new CustomBinaryExpression(e.Arguments[1], e.Arguments[2], "@>", typeof(bool));

case nameof(NpgsqlArrayExtensions.ContainedBy) when VersionAtLeast(8, 2):
case nameof(NpgsqlArrayExtensions.ContainedBy):
return new CustomBinaryExpression(e.Arguments[1], e.Arguments[2], "<@", typeof(bool));

case nameof(NpgsqlArrayExtensions.Overlaps) when VersionAtLeast(8, 2):
case nameof(NpgsqlArrayExtensions.Overlaps):
return new CustomBinaryExpression(e.Arguments[1], e.Arguments[2], "&&", typeof(bool));

case nameof(NpgsqlArrayExtensions.ArrayFill) when VersionAtLeast(8, 4):
case nameof(NpgsqlArrayExtensions.ArrayFill):
return new SqlFunctionExpression("array_fill", e.Method.ReturnType, e.Arguments.Skip(1));

case nameof(NpgsqlArrayExtensions.ListFill) when VersionAtLeast(8, 4):
case nameof(NpgsqlArrayExtensions.ListFill):
return new SqlFunctionExpression("array_fill", e.Method.ReturnType, e.Arguments.Skip(1));

case nameof(NpgsqlArrayExtensions.ArrayDimensions):
Expand All @@ -125,29 +128,20 @@ Expression NpgsqlArrayExtensionsHandler([NotNull] MethodCallExpression e)
case nameof(NpgsqlArrayExtensions.ArrayPositions) when VersionAtLeast(9, 5):
return new SqlFunctionExpression("array_positions", e.Method.ReturnType, e.Arguments.Skip(1));

case nameof(NpgsqlArrayExtensions.ArrayRemove) when VersionAtLeast(9, 3):
case nameof(NpgsqlArrayExtensions.ArrayRemove):
return new SqlFunctionExpression("array_remove", e.Method.ReturnType, e.Arguments.Skip(1).Take(2));

case nameof(NpgsqlArrayExtensions.ArrayReplace) when VersionAtLeast(9, 3):
case nameof(NpgsqlArrayExtensions.ArrayReplace):
return new SqlFunctionExpression("array_replace", e.Method.ReturnType, e.Arguments.Skip(1).Take(3));

case nameof(NpgsqlArrayExtensions.ArrayToString) when VersionAtLeast(9, 1):
return new SqlFunctionExpression("array_to_string", e.Method.ReturnType, e.Arguments.Skip(1).Take(3));

case nameof(NpgsqlArrayExtensions.ArrayToString):
return new SqlFunctionExpression("array_to_string", e.Method.ReturnType, e.Arguments.Skip(1).Take(2));

case nameof(NpgsqlArrayExtensions.StringToArray) when VersionAtLeast(9, 1):
return new SqlFunctionExpression("string_to_array", e.Method.ReturnType, e.Arguments.Skip(1).Take(3));
return new SqlFunctionExpression("array_to_string", e.Method.ReturnType, e.Arguments.Skip(1).Take(3));

case nameof(NpgsqlArrayExtensions.StringToArray):
return new SqlFunctionExpression("string_to_array", e.Method.ReturnType, e.Arguments.Skip(1).Take(2));

case nameof(NpgsqlArrayExtensions.StringToList) when VersionAtLeast(9, 1):
return new SqlFunctionExpression("string_to_array", e.Method.ReturnType, e.Arguments.Skip(1).Take(3));

case nameof(NpgsqlArrayExtensions.StringToList):
return new SqlFunctionExpression("string_to_array", e.Method.ReturnType, e.Arguments.Skip(1).Take(2));
return new SqlFunctionExpression("string_to_array", e.Method.ReturnType, e.Arguments.Skip(1).Take(3));

default:
return null;
Expand Down Expand Up @@ -189,7 +183,7 @@ Expression ArrayInstanceHandler([NotNull] MethodCallExpression e)

switch (e.Method.Name)
{
case nameof(Array.GetLength) when VersionAtLeast(8, 4):
case nameof(Array.GetLength):
return Expression.Coalesce(
new SqlFunctionExpression(
"array_length",
Expand Down
123 changes: 51 additions & 72 deletions test/EFCore.PG.FunctionalTests/Query/ArrayQueryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -549,22 +549,20 @@ public void List_Prepend_constant()

#region array_fill

[Theory]
[InlineData(8, 4)]
public void Array_ArrayFill_constant(int major, int minor)
[Fact]
public void Array_ArrayFill_constant()
{
using (var ctx = Fixture.CreateContext(new Version(major, minor)))
using (var ctx = Fixture.CreateContext())
{
var _ = ctx.SomeEntities.Select(e => EF.Functions.ArrayFill(e.Id, new[] { 2 })).ToList();
AssertContainsInSql("SELECT array_fill(e.\"Id\", ARRAY[2]::integer[])");
}
}

[Theory]
[InlineData(8, 4)]
public void List_ListFill_constant(int major, int minor)
[Fact]
public void List_ListFill_constant()
{
using (var ctx = Fixture.CreateContext(new Version(major, minor)))
using (var ctx = Fixture.CreateContext())
{
var _ = ctx.SomeEntities.Select(e => EF.Functions.ListFill(e.Id, new[] { 3 })).ToList();
AssertContainsInSql("SELECT array_fill(e.\"Id\", ARRAY[3]::integer[])");
Expand Down Expand Up @@ -715,11 +713,10 @@ public void Array_GetLength_on_literal_not_translated()

#region array_lower

[Theory]
[InlineData(7, 4)]
public void Array_ArrayLower(int major, int minor)
[Fact]
public void Array_ArrayLower()
{
using (var ctx = Fixture.CreateContext(new Version(major, minor)))
using (var ctx = Fixture.CreateContext())
{
var _ = ctx.SomeEntities.Where(e => e.SomeArray.GetLowerBound(0) == 2).ToList();
AssertContainsInSql("WHERE (COALESCE(array_lower(e.\"SomeArray\", 1), 0) - 1) = 2");
Expand All @@ -730,11 +727,10 @@ public void Array_ArrayLower(int major, int minor)

#region array_ndims

[Theory]
[InlineData(8, 4)]
public void Array_Rank(int major, int minor)
[Fact]
public void Array_Rank()
{
using (var ctx = Fixture.CreateContext(new Version(major, minor)))
using (var ctx = Fixture.CreateContext())
{
var _ = ctx.SomeEntities.Where(e => e.SomeArray.Rank == 2).ToList();
AssertContainsInSql("WHERE COALESCE(array_ndims(e.\"SomeArray\"), 1) = 2");
Expand Down Expand Up @@ -797,22 +793,20 @@ public void List_ListPositions_column(int major, int minor)

#region array_remove

[Theory]
[InlineData(9, 3)]
public void Array_ArrayRemove_column(int major, int minor)
[Fact]
public void Array_ArrayRemove_column()
{
using (var ctx = Fixture.CreateContext(new Version(major, minor)))
using (var ctx = Fixture.CreateContext())
{
var _ = ctx.SomeEntities.Select(e => EF.Functions.ArrayRemove(e.SomeArray, 0)).ToList();
AssertContainsInSql("SELECT array_remove(e.\"SomeArray\", 0)");
}
}

[Theory]
[InlineData(9, 3)]
public void List_ArrayRemove_column(int major, int minor)
[Fact]
public void List_ArrayRemove_column()
{
using (var ctx = Fixture.CreateContext(new Version(major, minor)))
using (var ctx = Fixture.CreateContext())
{
var _ = ctx.SomeEntities.Select(e => EF.Functions.ArrayRemove(e.SomeList, 0)).ToList();
AssertContainsInSql("SELECT array_remove(e.\"SomeList\", 0)");
Expand All @@ -823,22 +817,20 @@ public void List_ArrayRemove_column(int major, int minor)

#region array_replace

[Theory]
[InlineData(9, 3)]
public void Array_ArrayReplace_column(int major, int minor)
[Fact]
public void Array_ArrayReplace_column()
{
using (var ctx = Fixture.CreateContext(new Version(major, minor)))
using (var ctx = Fixture.CreateContext())
{
var _ = ctx.SomeEntities.Select(e => EF.Functions.ArrayReplace(e.SomeArray, 0, 1)).ToList();
AssertContainsInSql("SELECT array_replace(e.\"SomeArray\", 0, 1)");
}
}

[Theory]
[InlineData(9, 3)]
public void List_ArrayReplace_column(int major, int minor)
[Fact]
public void List_ArrayReplace_column()
{
using (var ctx = Fixture.CreateContext(new Version(major, minor)))
using (var ctx = Fixture.CreateContext())
{
var _ = ctx.SomeEntities.Select(e => EF.Functions.ArrayReplace(e.SomeList, 0, 1)).ToList();
AssertContainsInSql("SELECT array_replace(e.\"SomeList\", 0, 1)");
Expand All @@ -849,11 +841,10 @@ public void List_ArrayReplace_column(int major, int minor)

#region array_upper

[Theory]
[InlineData(7, 4)]
public void Array_ArrayUpper(int major, int minor)
[Fact]
public void Array_ArrayUpper()
{
using (var ctx = Fixture.CreateContext(new Version(major, minor)))
using (var ctx = Fixture.CreateContext())
{
var _ = ctx.SomeEntities.Where(e => e.SomeArray.GetUpperBound(0) == 2).ToList();
AssertContainsInSql("WHERE (COALESCE(array_upper(e.\"SomeArray\", 1), 0) - 1) = 2");
Expand All @@ -864,46 +855,40 @@ public void Array_ArrayUpper(int major, int minor)

#region array_to_string

[Theory]
[InlineData(7, 4)]
[InlineData(9, 1)]
public void Array_ArrayToString(int major, int minor)
[Fact]
public void Array_ArrayToString()
{
using (var ctx = Fixture.CreateContext(new Version(major, minor)))
using (var ctx = Fixture.CreateContext())
{
var _ = ctx.SomeEntities.Select(e => EF.Functions.ArrayToString(e.SomeArray, "*")).ToList();
AssertContainsInSql("SELECT array_to_string(e.\"SomeArray\", '*')");
}
}

[Theory]
[InlineData(7, 4)]
[InlineData(9, 1)]
public void List_ArrayToString(int major, int minor)
[Fact]
public void List_ArrayToString()
{
using (var ctx = Fixture.CreateContext(new Version(major, minor)))
using (var ctx = Fixture.CreateContext())
{
var _ = ctx.SomeEntities.Select(e => EF.Functions.ArrayToString(e.SomeList, "*")).ToList();
AssertContainsInSql("SELECT array_to_string(e.\"SomeList\", '*')");
}
}

[Theory]
[InlineData(9, 1)]
public void Array_ArrayToString_with_null(int major, int minor)
[Fact]
public void Array_ArrayToString_with_null()
{
using (var ctx = Fixture.CreateContext(new Version(major, minor)))
using (var ctx = Fixture.CreateContext())
{
var _ = ctx.SomeEntities.Select(e => EF.Functions.ArrayToString(e.SomeArray, "*", ";")).ToList();
AssertContainsInSql("SELECT array_to_string(e.\"SomeArray\", '*', ';')");
}
}

[Theory]
[InlineData(9, 1)]
public void List_ArrayToString_with_null(int major, int minor)
[Fact]
public void List_ArrayToString_with_null()
{
using (var ctx = Fixture.CreateContext(new Version(major, minor)))
using (var ctx = Fixture.CreateContext())
{
var _ = ctx.SomeEntities.Select(e => EF.Functions.ArrayToString(e.SomeList, "*", ";")).ToList();
AssertContainsInSql("SELECT array_to_string(e.\"SomeList\", '*', ';')");
Expand All @@ -914,46 +899,40 @@ public void List_ArrayToString_with_null(int major, int minor)

#region string_to_array

[Theory]
[InlineData(7, 4)]
[InlineData(9, 1)]
public void Array_StringToArray(int major, int minor)
[Fact]
public void Array_StringToArray()
{
using (var ctx = Fixture.CreateContext(new Version(major, minor)))
using (var ctx = Fixture.CreateContext())
{
var _ = ctx.SomeEntities.Where(e => EF.Functions.StringToArray<string>(e.SomeText, "*") != null).ToList();
AssertContainsInSql("WHERE string_to_array(e.\"SomeText\", '*') IS NOT NULL");
}
}

[Theory]
[InlineData(7, 4)]
[InlineData(9, 1)]
public void List_StringToList(int major, int minor)
[Fact]
public void List_StringToList()
{
using (var ctx = Fixture.CreateContext(new Version(major, minor)))
using (var ctx = Fixture.CreateContext())
{
var _ = ctx.SomeEntities.Where(e => EF.Functions.StringToList<string>(e.SomeText, "*") != null).ToList();
AssertContainsInSql("WHERE string_to_array(e.\"SomeText\", '*') IS NOT NULL");
}
}

[Theory]
[InlineData(9, 1)]
public void Array_StringToArray_with_null_string(int major, int minor)
[Fact]
public void Array_StringToArray_with_null_string()
{
using (var ctx = Fixture.CreateContext(new Version(major, minor)))
using (var ctx = Fixture.CreateContext())
{
var _ = ctx.SomeEntities.Where(e => EF.Functions.StringToArray<string>(e.SomeText, "*", ";") != null).ToList();
AssertContainsInSql("WHERE string_to_array(e.\"SomeText\", '*', ';') IS NOT NULL");
}
}

[Theory]
[InlineData(9, 1)]
public void List_StringToList_with_null_string(int major, int minor)
[Fact]
public void List_StringToList_with_null_string()
{
using (var ctx = Fixture.CreateContext(new Version(major, minor)))
using (var ctx = Fixture.CreateContext())
{
var _ = ctx.SomeEntities.Where(e => EF.Functions.StringToList<string>(e.SomeText, "*", ";") != null).ToList();
AssertContainsInSql("WHERE string_to_array(e.\"SomeText\", '*', ';') IS NOT NULL");
Expand Down

0 comments on commit 2ea34f1

Please sign in to comment.