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

Query contains non-consecutive table aliases when tables are pruned #32757

Closed
roji opened this issue Jan 9, 2024 · 0 comments · Fixed by #32785
Closed

Query contains non-consecutive table aliases when tables are pruned #32757

roji opened this issue Jan 9, 2024 · 0 comments · Fixed by #32785
Assignees
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement

Comments

@roji
Copy link
Member

roji commented Jan 9, 2024

Note: I do not think this is a bug

We generate table aliases based on the first character of the table name, followed by an incrementing counter for uniquification; aliases are thus generally consecutive (we even have DEBUG-only checks that attempt to validate this).

However, when a table gets pruned, that causes a "hole" in the alias numbering. For example, with the following contrived TPT query:

_ = ctx.Blogs.Count(b =>
    ctx.Blogs.Count(b => b.Name == "foo") == 0 && ctx.BlogsExtraSpecial.Count(b => b.Name == "foo") == 0);
Full repro
await using var ctx = new BlogContext();
await ctx.Database.EnsureDeletedAsync();
await ctx.Database.EnsureCreatedAsync();

_ = ctx.Blogs.Count(b =>
    ctx.Blogs.Count(b => b.Name == "foo") == 0 && ctx.BlogsExtraSpecial.Count(b => b.Name == "foo") == 0);

public class BlogContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }
    public DbSet<BlogSpecial> BlogsSpecial { get; set; }
    public DbSet<BlogExtraSpecial> BlogsExtraSpecial { 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();

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Blog>().UseTptMappingStrategy();
    }
}

public class Blog
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class BlogSpecial : Blog
{
    public string SpecialProperty { get; set; }
}

public class BlogExtraSpecial : BlogSpecial
{
    public string ExtraSpecialProperty { get; set; }
}

... we generate the following SQL:

SELECT COUNT(*)
FROM [Blogs] AS [b]
WHERE (
    SELECT COUNT(*)
    FROM [Blogs] AS [b2]
    WHERE [b2].[Name] = N'foo') = 0 AND (
    SELECT COUNT(*)
    FROM [Blogs] AS [b5]

... with only [b], [b2] and [b5]. This is because ctx.Blogs generally causes 3 tables to be added (entire TPT hierarchy), but COUNT(*) causes the child tables to be pruned, isnce the only referenced property is in the root (Name).

I do not really consider this a bug, but there's some effort in the code to maintain consecutive aliases. Fully maintaining that in all cases requires another pass to check and rewrite aliases, and that doesn't seem worth it.

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

Successfully merging a pull request may close this issue.

2 participants