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

Complex value comparer expressions can not be translated by the in-memory query pipeline #27495

Closed
Hylaean opened this issue Feb 23, 2022 · 6 comments · Fixed by #27654
Closed
Labels
area-in-memory area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Milestone

Comments

@Hylaean
Copy link

Hylaean commented Feb 23, 2022

Exception when querying entities linked by case insensitive string comparison in composite foreign keys

When using custom comparers on fields used in a composite foreign key, some queries can't be generated and result in an InvalidOperationException.
The tests below describe both the bug and its limits.

#nullable disable
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace EfBug;

public class EFComparerCompositeKeyBug
{
    public class Genus
    {
        public string Name { get; set; }
        public string FamilyName { get; set; }
        public ICollection<Specy> Species { get; set; }
    }
    public class Specy
    {
        public string Name { get; set; }
        public string GenusName { get; set; }
        public string FamilyName { get; set; }
        public Genus Genus { get; set; }
    }

    public class TaxonomyContext : DbContext
    {
        protected override void OnModelCreating(ModelBuilder model)
        {
            model.Entity<Genus>(entity =>
            {
                entity.HasKey(e => new { e.Name, e.FamilyName });
                entity.Property(e => e.Name).IsRequired().CaseInsensitive();
                entity.Property(e => e.FamilyName).IsRequired().CaseInsensitive();
            });


            model.Entity<Specy>(entity =>
            {
                entity.HasKey(e => new { e.Name, e.GenusName, e.FamilyName });
                entity.Property(e => e.Name).IsRequired().CaseInsensitive();
                entity.Property(e => e.FamilyName).IsRequired().CaseInsensitive();
                entity.Property(e => e.GenusName).IsRequired().CaseInsensitive();
                entity.HasOne(e => e.Genus).WithMany(e => e.Species)
                    .HasPrincipalKey(e => new { GenusName = e.Name, e.FamilyName })
                    .HasForeignKey(e => new { e.GenusName, e.FamilyName });
            });

        }

        protected override void OnConfiguring(DbContextOptionsBuilder options)
        {
            base.OnConfiguring(options);
            options.UseInMemoryDatabase("Taxonomy");
        }

        public virtual DbSet<Genus> Genera { get; set; } = null!;
        public virtual DbSet<Specy> Species { get; set; } = null!;

    }

    [Fact]
    public void Passes()
    {
        using var taxo = new TaxonomyContext();
        var species = (from g in taxo.Genera
                       where g.FamilyName == "Ornithorhynchidae"
                       from s in g.Species
                       select s).Distinct().ToList();
    }

    [Fact]
    public void Fails()
    {
        using var taxo = new TaxonomyContext();
        var ex = Assert.Throws<InvalidOperationException>(() => (from s in taxo.Species
                                                            where s.Genus.FamilyName == "Ornithorhynchidae"
                                                            select s).ToList());
        Assert.Contains("could not be translated", ex.Message);
    }

    [Fact]
    public void AlsoFails()
    {
        using var taxo = new TaxonomyContext();
        var ex = Assert.Throws<InvalidOperationException>(() => (from g in taxo.Genera
                                                                 where g.FamilyName == "Ornithorhynchidae"
                                                                 from s in g.Species
                                                                 select s).Include(s => s.Genus).Distinct().ToList());
        Assert.Contains("could not be translated", ex.Message);
    }
}

internal static class Comparers
{
    public static ValueComparer<string>? CI { get; } = new(
        (l, r) => string.Equals(l, r, StringComparison.OrdinalIgnoreCase),
        v => StringComparer.OrdinalIgnoreCase.GetHashCode(v)
        );

    public static PropertyBuilder<string> CaseInsensitive(this PropertyBuilder<string> property)
    {
        var md = property.Metadata;
        md.SetKeyValueComparer(CI);
        md.SetValueComparer(CI);
        return property;
    }
}

Exception

System.InvalidOperationException: 'The LINQ expression 'DbSet<Specy>()
    .Join(
        inner: DbSet<Genus>(), 
        outerKeySelector: s => new object[]
        { 
            (object)EF.Property<string>(s, "GenusName"), 
            (object)EF.Property<string>(s, "FamilyName") 
        }, 
        innerKeySelector: g => new object[]
        { 
            (object)EF.Property<string>(g, "Name"), 
            (object)EF.Property<string>(g, "FamilyName") 
        }, 
        resultSelector: (o, i) => new TransparentIdentifier<Specy, Genus>(
            Outer = o, 
            Inner = i
        ))' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.'

  Stack Trace: 
    QueryableMethodTranslatingExpressionVisitor.<VisitMethodCall>g__CheckTranslated|15_0(ShapedQueryExpression translated, <>c__DisplayClass15_0& )
    QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
    InMemoryQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
    MethodCallExpression.Accept(ExpressionVisitor visitor)
    ExpressionVisitor.Visit(Expression node)
    QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
    InMemoryQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
    MethodCallExpression.Accept(ExpressionVisitor visitor)
    ExpressionVisitor.Visit(Expression node)
    QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)

Provider and version information

EF Core version: 6.0.2
Database provider: SqlServer / InMemory / more?
Target framework: .net 6.0.2
Operating system: W11

@Hylaean Hylaean changed the title Case Insensitive string comparison in *Composite Foreign* Keys Exception when navigating between entities linked by case insensitive string comparison in *composite foreign* keys Feb 23, 2022
@Hylaean Hylaean changed the title Exception when navigating between entities linked by case insensitive string comparison in *composite foreign* keys Exception when querying entities linked by case insensitive string comparison in *composite foreign* keys Feb 24, 2022
@Hylaean Hylaean changed the title Exception when querying entities linked by case insensitive string comparison in *composite foreign* keys Exception when querying entities linked by case insensitive string comparison in *composite* foreign keys Feb 24, 2022
@ajcvickers
Copy link
Member

ajcvickers commented Mar 1, 2022

Note for triage: query translation only fails on in-memory database, which is interesting since the in-memory database has case-sensitive keys anyway. So it's not clear how using case-insensitive key comparison for the state manager would work for the in-memory database which is case-sensitive.

@ajcvickers
Copy link
Member

See also #27526

@Hylaean
Copy link
Author

Hylaean commented Mar 1, 2022

@ajcvickers It doesn't only crash in-memory. It crashes everywhere. I only used in-memory as it was easier to demonstrate there.

@ajcvickers
Copy link
Member

@Hylaean I changed the code to use SQL Server and saw no exceptions. Please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing with the SQL Server or SQLite provider so that we can investigate.

@Hylaean
Copy link
Author

Hylaean commented Mar 1, 2022

@ajcvickers: you're right, there was a bug in our test suite that lead to this misconception. thanks.

@ajcvickers ajcvickers added closed-out-of-scope This is not something that will be fixed/implemented and the issue is closed. and removed area-in-memory labels Mar 1, 2022
@ajcvickers
Copy link
Member

Note from triage: closing since this is the in-memory database.

@ajcvickers ajcvickers changed the title Exception when querying entities linked by case insensitive string comparison in *composite* foreign keys Complex value comparer expressions can not be translated by the in-memory query pipeline Mar 16, 2022
@ajcvickers ajcvickers added area-query area-in-memory type-bug closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. and removed closed-out-of-scope This is not something that will be fixed/implemented and the issue is closed. labels Mar 16, 2022
@ajcvickers ajcvickers self-assigned this Mar 16, 2022
@ajcvickers ajcvickers reopened this Mar 16, 2022
@ajcvickers ajcvickers added this to the 7.0.0 milestone Mar 16, 2022
ajcvickers added a commit that referenced this issue Mar 16, 2022
Part of #11597

This change takes the ValueComparer defined for the principal key and uses it for the foreign key, but also accommodating for nulls appropriately. As part of this, we started getting some more complex expressions in value comparers used in the in-memory database. These expressions became part of the query, which then meant they needed to be translated. Therefore, this logic has been changed to call the value comparer as a method when using the in-memory database, and this method is then detected. This incidentally fixes #27495, which was also a case of a value comparer expression that could not be translated, and any other case where a value comparer could not be translated in in-memory queries.
@ajcvickers ajcvickers modified the milestones: 7.0.0, 7.0.0-preview3 Mar 31, 2022
@ajcvickers ajcvickers removed this from the 7.0.0-preview3 milestone Nov 5, 2022
@ajcvickers ajcvickers added this to the 7.0.0 milestone Nov 5, 2022
@ajcvickers ajcvickers removed their assignment Aug 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-in-memory area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants