RedundantSubqueryRemover possible bug

Jan 13, 2011 at 3:59 PM
Assuming a model where a client has several invoices and the following query:
from c in ctx.GetTable<Clients>() select c.Invoices.First()
the RedundantSubqueryRemover fails to update all relevant aliases.

Before the call to RedundantSubqueryRemover:
{Project(
  @"SELECT NULL 
  FROM (
    SELECT t1.CustCode
    FROM Clients AS t1
    ) AS t0",
  Project(
    @"SELECT TOP (1) t0.CustCode, t0.Num_Fac
    FROM (
      SELECT t1.CustCode, t1.Num_Fac
      FROM (
        SELECT t2.CustCode, t2.Num_Fac
        FROM YEARTRN AS t2
        ) AS t1
      WHERE (t1.CustCode = A19386821?.CustCode)
      ) AS t0",
    new Invoice() {
      CustCode = A1.Column("CustCode"),
      Num_Fac = A1.Column("Num_Fac")
    },
    p => Enumerable.SingleOrDefault(p)
  ),
  p => Queryable.AsQueryable(p)
)}

After the call to RedundantSubqueryRemover: (the A19386821 select for clients is removed but the alias is not updated)

{Project(
  @"SELECT NULL 
  FROM Clients AS t0",
  Project(
    @"SELECT TOP (1) t0.CustCode, t0.Num_Fac
    FROM YEARTRN AS t0
    WHERE (t0.CustCode = A19386821?.CustCode)",
    new Invoice() {
      CustCode = A1.Column("CustCode"),
      Num_Fac = A1.Column("Num_Fac")
    },
    p => Enumerable.SingleOrDefault(p)
  ),
  p => Queryable.AsQueryable(p)
)}

Removing the following code from the VisitSelect method in the RedundantSubqueryRemover seems to fix the problem.
// first remove all purely redundant subqueries
List<SelectExpression> redundant = RedundantSubqueryGatherer.Gather(select.From);
if (redundant != null)
{
     select = SubqueryRemover.Remove(select, redundant);
}

Since I am not very familiar with this library, I just wanted to make sure that it was safe to remove these lines.
Maybe the definition of what is a purely redundant subquery needs to be updated?