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

SelectMany over primitive collection property fails #32505

Closed
roji opened this issue Dec 4, 2023 · 2 comments · Fixed by #32506
Closed

SelectMany over primitive collection property fails #32505

roji opened this issue Dec 4, 2023 · 2 comments · Fixed by #32506
Assignees
Labels
area-primitive-collections area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@roji
Copy link
Member

roji commented Dec 4, 2023

Query:

_ = await ctx.Blogs.SelectMany(b => b.Tags).ToListAsync();
Full repro
await using var ctx = new BlogContext();
await ctx.Database.EnsureDeletedAsync();
await ctx.Database.EnsureCreatedAsync();

ctx.Blogs.AddRange(
    new Blog { Tags = new List<string> { "tag1", "tag2" } },
    new Blog { Tags = new List<string> { "tag2", "tag3" } });

_ = await ctx.Blogs.SelectMany(b => b.Tags).ToListAsync();

public class BlogContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseSqlServer("Server=localhost;Database=test;User=SA;Password=Abcd5678;Connect Timeout=60;ConnectRetryCount=0;Encrypt=false")
            .LogTo(Console.WriteLine, LogLevel.Information)
            .EnableSensitiveDataLogging();
}

public class Blog
{
    public int Id { get; set; }
    public List<string> Tags { get; set; }
}

The cause of the issue is that the SelectMany lambda is visited by the top-level QueryableMethodTranslatingExpressionVisitor - not by a subquery visitor. Our code for identifying the property access (b.Tags) kicks in only after the regular translation failed (code); but the base visitation throws before that when translation fails on a non-subquery visitor (code).

@roji roji self-assigned this Dec 4, 2023
@roji roji changed the title SelectMany over primitive collection fails SelectMany over primitive collection property fails Dec 4, 2023
@roji
Copy link
Member Author

roji commented Dec 4, 2023

Fix options:

  1. Move identification of property access to before calling base.Visit (possibly a bit heavy)
  2. Stop eagerly throwing in VisitMethodCall, throw only on top-level Translate method
  3. Instantiate a subquery visitor instance for the SelectMany lambda translation
  4. Preferred: move property access identification up to core (QueryableMethodTranslatingExpressionVisitor); this would start building support for primitive collections in non-relational providers.

roji added a commit to roji/efcore that referenced this issue Dec 4, 2023
@roji
Copy link
Member Author

roji commented Dec 4, 2023

Did option 4 above.

If further users run into the SelectMany issue, we can consider patching with a simpler fix.

@roji roji added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Dec 4, 2023
roji added a commit to roji/efcore that referenced this issue Dec 4, 2023
roji added a commit to roji/efcore that referenced this issue Dec 4, 2023
roji added a commit to roji/efcore that referenced this issue Dec 22, 2023
roji added a commit to roji/efcore that referenced this issue Dec 22, 2023
roji added a commit that referenced this issue Dec 23, 2023
@roji roji added this to the 9.0.0 milestone Dec 23, 2023
@ajcvickers ajcvickers modified the milestones: 9.0.0, 9.0.0-preview1 Jan 31, 2024
@roji roji modified the milestones: 9.0.0-preview1, 9.0.0 Oct 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-primitive-collections area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants