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

Columns from outer reference are not projected correctly in Outer Apply #22223

Closed
smitpatel opened this issue Aug 25, 2020 · 0 comments · Fixed by #24734
Closed

Columns from outer reference are not projected correctly in Outer Apply #22223

smitpatel opened this issue Aug 25, 2020 · 0 comments · Fixed by #24734
Assignees
Labels
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

@smitpatel
Copy link
Member

 public class Program
    {
        public static async Task Main(string[] args)
        {
            using (var db = new MyContext())
            {
                // Recreate database
                db.Database.EnsureDeleted();
                db.Database.EnsureCreated();

                // Seed database


                db.SaveChanges();
            }

            using (var db = new MyContext())
            {
                // Run queries
                var query = db.Blogs
                    .Select(b => new
                    {
                        Posts = b.Posts
                            .OrderBy(a => a.Ordering)
                            .Take(1)
                            .Select(p => new
                        {
                            Title = p.Id == p.Blog.Id ? "A" : "B"
                        }).ToList()
                    }).FirstOrDefault();
            }
            Console.WriteLine("Program finished.");
        }
    }


    public class MyContext : DbContext
    {
        private static ILoggerFactory ContextLoggerFactory
            => LoggerFactory.Create(b =>
            {
                b
                .AddConsole()
                .AddFilter("", LogLevel.Debug);
            });

        // Declare DBSets
        public DbSet<Blog> Blogs { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            // Select 1 provider
            optionsBuilder
                .UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=_ModelApp;Trusted_Connection=True;Connect Timeout=5;ConnectRetryCount=0")
                //.UseSqlite("filename=_modelApp.db")
                //.UseInMemoryDatabase(databaseName: "_modelApp")
                //.UseCosmos("https://localhost:8081", @"C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==", "_ModelApp")
                .EnableSensitiveDataLogging()
                .UseLoggerFactory(ContextLoggerFactory);
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            // Configure model
        }
    }

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

        public List<Post> Posts { get; set; }
    }

    public class Post
    {
        public int Id { get; set; }
        public int Ordering { get; set; }
        public int BlogId { get; set; }
        public Blog Blog { get; set; }
    }

Generated SQL

      SELECT [t].[Id], [t1].[Title], [t1].[Id], [t1].[Id0]
      FROM (
          SELECT TOP(1) [b].[Id]
          FROM [Blogs] AS [b]
      ) AS [t]
      OUTER APPLY (
          SELECT CASE
              WHEN [t].[Id] = [b0].[Id] THEN N'A'
              ELSE N'B'
          END AS [Title], [t].[Id], [b0].[Id] AS [Id0], [t].[Ordering] -- [t].[Ordering] is not projected in [t] (outer)
          FROM (
              SELECT TOP(1) [p].[Id], [p].[BlogId], [p].[Ordering]
              FROM [Post] AS [p]
              WHERE [t].[Id] = [p].[BlogId]
              ORDER BY [p].[Ordering]
          ) AS [t0]
          INNER JOIN [Blogs] AS [b0] ON [t].[BlogId] = [b0].[Id]
      ) AS [t1]
      ORDER BY [t].[Id], [t1].[Ordering], [t1].[Id], [t1].[Id0]

This demonstrates #22222 also.

@ajcvickers ajcvickers added this to the Backlog milestone Aug 28, 2020
@ajcvickers ajcvickers modified the milestones: Backlog, 6.0.0 Nov 5, 2020
@smitpatel smitpatel removed the blocked label Apr 21, 2021
smitpatel added a commit that referenced this issue Apr 22, 2021
@smitpatel smitpatel added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Apr 22, 2021
@ghost ghost closed this as completed in #24734 Apr 22, 2021
ghost pushed a commit that referenced this issue Apr 22, 2021
@ajcvickers ajcvickers modified the milestones: 6.0.0, 6.0.0-preview5 Apr 26, 2021
@ajcvickers ajcvickers modified the milestones: 6.0.0-preview5, 6.0.0 Nov 8, 2021
This issue was closed.
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. customer-reported type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants