Skip to content

Commit

Permalink
Add Manual_LeftAndInnerJoin test
Browse files Browse the repository at this point in the history
  • Loading branch information
yang-er committed Dec 12, 2021
1 parent 47ee931 commit 77cfd5c
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/Relational/Reduce/SelfJoinsPredicateComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ bool VisitSql(SqlExpression sql)
if (binaryExpression.Left is ColumnExpression col
&& binaryExpression.Right is ColumnExpression col2
&& col.Name == accessor(col2)
&& col.Table == left
&& IsColumnBelongTo(col, left)
&& IsColumnBelongTo(col2, right))
{
discovered.Add(col.Name);
Expand All @@ -68,7 +68,7 @@ bool VisitSql(SqlExpression sql)
&& IsOnlyNotNull(right, caseExpr.WhenClauses[0].Test)
&& caseExpr.WhenClauses[0].Result is ColumnExpression col3
&& col1.Name == accessor(col3)
&& col1.Table == left
&& IsColumnBelongTo(col1, left)
&& IsColumnBelongTo(col3, right))
{
discovered.Add(col1.Name);
Expand Down
22 changes: 22 additions & 0 deletions test/MySql/Functional.SelfJoinsRemoval.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,28 @@ public override void SuperOwned()
AssertSql(@"
SELECT `o`.`Id`, `o`.`Happy`, `o`.`Other`, `o`.`Apple_AnotherString`, `o`.`ChangedBy`, `o`.`Apple_Audit_IsDeleted`, `o`.`Apple_What_AnotherString`, `o`.`Audit_Taq`, `o`.`Banana_Taq`, `o`.`Banana_AnotherString`, `o`.`Cherry_AnotherString`, `o`.`Cherry_Taq`
FROM `OwnedThree_{{schema}}` AS `o`
");
}

public override void Manual_OnlyLeftJoin()
{
base.Manual_OnlyLeftJoin();

AssertSql(@"
SELECT `a`.`Id`, `b`.`Id` AS `Id0`, `a`.`Name`, `a`.`Owner`, `b`.`Id` AS `Id3`, `b`.`Name` AS `Name0`, `b`.`Owner` AS `Owner0`, `b`.`Source`
FROM `AppleEntity_{{schema}}` AS `a`
LEFT JOIN `BananaEntity_{{schema}}` AS `b` ON `a`.`Id` = `b`.`Id`
");
}

public override void Manual_LeftAndInnerJoin()
{
base.Manual_LeftAndInnerJoin();

AssertSql(@"
SELECT `a`.`Id`, `b`.`Id` AS `Id0`, `a`.`Name`, `a`.`Owner`, `b`.`Name` AS `Name0`, `b`.`Owner` AS `Owner0`, `b`.`Source`
FROM `AppleEntity_{{schema}}` AS `a`
INNER JOIN `BananaEntity_{{schema}}` AS `b` ON `a`.`Id` = `b`.`Id`
");
}
}
Expand Down
22 changes: 22 additions & 0 deletions test/PostgreSql/Functional.SelfJoinsRemoval.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,28 @@ public override void SuperOwned()
AssertSql(@"
SELECT o.""Id"", o.""Happy"", o.""Other"", o.""Apple_AnotherString"", o.""ChangedBy"", o.""Apple_Audit_IsDeleted"", o.""Apple_What_AnotherString"", o.""Audit_Taq"", o.""Banana_Taq"", o.""Banana_AnotherString"", o.""Cherry_AnotherString"", o.""Cherry_Taq""
FROM ""OwnedThree_{{schema}}"" AS o
");
}

public override void Manual_OnlyLeftJoin()
{
base.Manual_OnlyLeftJoin();

AssertSql(@"
SELECT a.""Id"", b.""Id"" AS ""Id0"", a.""Name"", a.""Owner"", b.""Id"" AS ""Id3"", b.""Name"" AS ""Name0"", b.""Owner"" AS ""Owner0"", b.""Source""
FROM ""AppleEntity_{{schema}}"" AS a
LEFT JOIN ""BananaEntity_{{schema}}"" AS b ON a.""Id"" = b.""Id""
");
}

public override void Manual_LeftAndInnerJoin()
{
base.Manual_LeftAndInnerJoin();

AssertSql(@"
SELECT a.""Id"", b.""Id"" AS ""Id0"", a.""Name"", a.""Owner"", b.""Name"" AS ""Name0"", b.""Owner"" AS ""Owner0"", b.""Source""
FROM ""AppleEntity_{{schema}}"" AS a
INNER JOIN ""BananaEntity_{{schema}}"" AS b ON a.""Id"" = b.""Id""
");
}
}
Expand Down
65 changes: 64 additions & 1 deletion test/Relational/Functional.SelfJoinsRemoval.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,23 @@ public class Audit
public class NormalEntity
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}

public class AppleEntity
{
public int Id { get; set; }
public string Name { get; set; }
public string Owner { get; set; }
}

public int Age { get; set; }
public class BananaEntity
{
public int Id { get; set; }
public string Name { get; set; }
public string Owner { get; set; }
public string Source { get; set; }
}

public class TestingContext : DbContext
Expand All @@ -76,6 +89,8 @@ public class TestingContext : DbContext
public DbSet<MiniInfo> MiniInfos { get; set; }
public DbSet<OwnedThree> OwnedThrees { get; set; }
public DbSet<NormalEntity> NormalEntities { get; set; }
public DbSet<AppleEntity> Apples { get; set; }
public DbSet<BananaEntity> Bananas { get; set; }
public string DefaultSchema { get; }

public TestingContext(string schema, DbContextOptions options)
Expand Down Expand Up @@ -175,6 +190,16 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)

entity.HasKey(e => e.Id);
});

modelBuilder.Entity<AppleEntity>(entity =>
{
entity.ToTable(nameof(AppleEntity) + "_" + DefaultSchema);
});

modelBuilder.Entity<BananaEntity>(entity =>
{
entity.ToTable(nameof(BananaEntity) + "_" + DefaultSchema);
});
}
}

Expand Down Expand Up @@ -337,5 +362,43 @@ public virtual void OwnedThenUnionDistinct()
.Where(a => a.ChangeLogId == 50))
.Load();
}

[ConditionalFact, TestPriority(12)]
public virtual void Manual_OnlyLeftJoin()
{
using var context = CreateContext();
using var scope = CatchCommand();

var query =
from a in context.Apples.Select(a => new { a.Id })
join b in context.Bananas.Select(b => new { b.Id }) on a.Id equals b.Id into bb from b in bb.DefaultIfEmpty()
join t in context.Apples.Where(a0 => a0.Name != null).Select(a0 => new { a0.Id, a0.Name }) on a.Id equals t.Id into aa0 from t in aa0.DefaultIfEmpty()
join t0 in context.Apples.Where(a1 => a1.Owner != null).Select(a1 => new { a1.Id, a1.Owner }) on t.Id equals t0.Id into aa1 from t0 in aa1.DefaultIfEmpty()
join t1 in context.Bananas.Where(b0 => b0.Name != null).Select(b0 => new { b0.Id, b0.Name }) on b.Id equals t1.Id into bb0 from t1 in bb0.DefaultIfEmpty()
join t2 in context.Bananas.Where(b1 => b1.Owner != null).Select(b1 => new { b1.Id, b1.Owner }) on t1.Id equals t2.Id into bb1 from t2 in bb1.DefaultIfEmpty()
join t3 in context.Bananas.Where(b2 => b2.Source != null).Select(b2 => new { b2.Id, b2.Source }) on t2.Id equals t3.Id into bb2 from t3 in bb2.DefaultIfEmpty()
select new { a.Id, Id0 = (int?)b.Id, Id1 = (int?)t.Id, t.Name, Id2 = (int?)t0.Id, t0.Owner, Id3 = (int?)t1.Id, Name0 = t1.Name, Id4 = (int?)t2.Id, Owner0 = t2.Owner, Id5 = (int?)t3.Id, t3.Source };

query.Load();
}

[ConditionalFact, TestPriority(13)]
public virtual void Manual_LeftAndInnerJoin()
{
using var context = CreateContext();
using var scope = CatchCommand();

var query =
from a in context.Apples.Select(a => new { a.Id })
join b in context.Bananas.Select(b => new { b.Id }) on a.Id equals b.Id
join t in context.Apples.Where(a0 => a0.Name != null).Select(a0 => new { a0.Id, a0.Name }) on a.Id equals t.Id into aa0 from t in aa0.DefaultIfEmpty()
join t0 in context.Apples.Where(a1 => a1.Owner != null).Select(a1 => new { a1.Id, a1.Owner }) on t.Id equals t0.Id into aa1 from t0 in aa1.DefaultIfEmpty()
join t1 in context.Bananas.Where(b0 => b0.Name != null).Select(b0 => new { b0.Id, b0.Name }) on b.Id equals t1.Id into bb0 from t1 in bb0.DefaultIfEmpty()
join t2 in context.Bananas.Where(b1 => b1.Owner != null).Select(b1 => new { b1.Id, b1.Owner }) on t1.Id equals t2.Id into bb1 from t2 in bb1.DefaultIfEmpty()
join t3 in context.Bananas.Where(b2 => b2.Source != null).Select(b2 => new { b2.Id, b2.Source }) on t2.Id equals t3.Id into bb2 from t3 in bb2.DefaultIfEmpty()
select new { a.Id, Id0 = b.Id, Id1 = (int?)t.Id, t.Name, Id2 = (int?)t0.Id, t0.Owner, Id3 = (int?)t1.Id, Name0 = t1.Name, Id4 = (int?)t2.Id, Owner0 = t2.Owner, Id5 = (int?)t3.Id, t3.Source };

query.Load();
}
}
}
22 changes: 22 additions & 0 deletions test/SqlServer/Functional.SelfJoinsRemoval.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,28 @@ public override void SuperOwned()
AssertSql(@"
SELECT [o].[Id], [o].[Happy], [o].[Other], [o].[Apple_AnotherString], [o].[ChangedBy], [o].[Apple_Audit_IsDeleted], [o].[Apple_What_AnotherString], [o].[Audit_Taq], [o].[Banana_Taq], [o].[Banana_AnotherString], [o].[Cherry_AnotherString], [o].[Cherry_Taq]
FROM [OwnedThree_{{schema}}] AS [o]
");
}

public override void Manual_OnlyLeftJoin()
{
base.Manual_OnlyLeftJoin();

AssertSql(@"
SELECT [a].[Id], [b].[Id] AS [Id0], [a].[Name], [a].[Owner], [b].[Id] AS [Id3], [b].[Name] AS [Name0], [b].[Owner] AS [Owner0], [b].[Source]
FROM [AppleEntity_{{schema}}] AS [a]
LEFT JOIN [BananaEntity_{{schema}}] AS [b] ON [a].[Id] = [b].[Id]
");
}

public override void Manual_LeftAndInnerJoin()
{
base.Manual_LeftAndInnerJoin();

AssertSql(@"
SELECT [a].[Id], [b].[Id] AS [Id0], [a].[Name], [a].[Owner], [b].[Name] AS [Name0], [b].[Owner] AS [Owner0], [b].[Source]
FROM [AppleEntity_{{schema}}] AS [a]
INNER JOIN [BananaEntity_{{schema}}] AS [b] ON [a].[Id] = [b].[Id]
");
}
}
Expand Down
22 changes: 22 additions & 0 deletions test/Sqlite/Functional.SelfJoinsRemoval.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,28 @@ public override void SuperOwned()
AssertSql(@"
SELECT ""o"".""Id"", ""o"".""Happy"", ""o"".""Other"", ""o"".""Apple_AnotherString"", ""o"".""ChangedBy"", ""o"".""Apple_Audit_IsDeleted"", ""o"".""Apple_What_AnotherString"", ""o"".""Audit_Taq"", ""o"".""Banana_Taq"", ""o"".""Banana_AnotherString"", ""o"".""Cherry_AnotherString"", ""o"".""Cherry_Taq""
FROM ""OwnedThree_{{schema}}"" AS ""o""
");
}

public override void Manual_OnlyLeftJoin()
{
base.Manual_OnlyLeftJoin();

AssertSql(@"
SELECT ""a"".""Id"", ""b"".""Id"" AS ""Id0"", ""a"".""Name"", ""a"".""Owner"", ""b"".""Id"" AS ""Id3"", ""b"".""Name"" AS ""Name0"", ""b"".""Owner"" AS ""Owner0"", ""b"".""Source""
FROM ""AppleEntity_{{schema}}"" AS ""a""
LEFT JOIN ""BananaEntity_{{schema}}"" AS ""b"" ON ""a"".""Id"" = ""b"".""Id""
");
}

public override void Manual_LeftAndInnerJoin()
{
base.Manual_LeftAndInnerJoin();

AssertSql(@"
SELECT ""a"".""Id"", ""b"".""Id"" AS ""Id0"", ""a"".""Name"", ""a"".""Owner"", ""b"".""Name"" AS ""Name0"", ""b"".""Owner"" AS ""Owner0"", ""b"".""Source""
FROM ""AppleEntity_{{schema}}"" AS ""a""
INNER JOIN ""BananaEntity_{{schema}}"" AS ""b"" ON ""a"".""Id"" = ""b"".""Id""
");
}
}
Expand Down

0 comments on commit 77cfd5c

Please sign in to comment.