Skip to content

Commit

Permalink
Change passive skipping to active tagging if PruneSelfJoins is required
Browse files Browse the repository at this point in the history
  • Loading branch information
yang-er committed Dec 12, 2021
1 parent 34d5e59 commit 89dd492
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ If you want to try TableSplittingJoinsRemoval to remove useless self-joins, whic

```csharp
options.UseTableSplittingJoinsRemoval();
someQueryable.TagWith("PruneSelfJoins").ToList()
```

The InMemory project is only used for unit tests, which is not efficient and may behave a little different from the relational providers.
Expand Down
4 changes: 2 additions & 2 deletions src/Relational/Reduce/SelfJoinsPruningExpressionVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public SelfJoinsPruningExpressionVisitor(
Check.NotNull(queryCompilationContext, nameof(queryCompilationContext));
Check.NotNull(sqlExpressionFactory, nameof(sqlExpressionFactory));

_skipped = queryCompilationContext.Tags.Contains("SkipSelfJoinsPruning");
if (_skipped) queryCompilationContext.Tags.Remove("SkipSelfJoinsPruning");
_skipped = !queryCompilationContext.Tags.Contains("PruneSelfJoins");
if (!_skipped) queryCompilationContext.Tags.Remove("PruneSelfJoins");
_comparer = new SelfJoinsPredicateComparer(queryCompilationContext.Model);
_columnRewriting = new ColumnRewritingExpressionVisitor(sqlExpressionFactory);
}
Expand Down
19 changes: 13 additions & 6 deletions test/Relational/Functional.SelfJoinsRemoval.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,23 +217,23 @@ public virtual void Owned()
{
using var context = CreateContext();
using var scope = CatchCommand();
context.ChangeLogs.Load();
context.ChangeLogs.TagWith("PruneSelfJoins").Load();
}

[ConditionalFact, TestPriority(1)]
public virtual void HasOneWithOne_SharedTable()
{
using var context = CreateContext();
using var scope = CatchCommand();
context.MiniInfos.Include(e => e.Full).Load();
context.MiniInfos.Include(e => e.Full).TagWith("PruneSelfJoins").Load();
}

[ConditionalFact, TestPriority(2)]
public virtual void SuperOwned()
{
using var context = CreateContext();
using var scope = CatchCommand();
context.OwnedThrees.Load();
context.OwnedThrees.TagWith("PruneSelfJoins").Load();
}

[ConditionalFact, TestPriority(3)]
Expand All @@ -244,6 +244,7 @@ public virtual void InnerJoin_3_2()

context.OwnedThrees
.Join(context.ChangeLogs, o => o.Id, o => o.ChangeLogId, (o, p) => new { o, p })
.TagWith("PruneSelfJoins")
.Load();
}

Expand All @@ -255,6 +256,7 @@ public virtual void InnerJoin_2_3()

context.ChangeLogs
.Join(context.OwnedThrees, o => o.ChangeLogId, o => o.Id, (o, p) => new { o, p })
.TagWith("PruneSelfJoins")
.Load();
}

Expand All @@ -267,6 +269,7 @@ public virtual void GroupJoin_3_2()
context.OwnedThrees
.GroupJoin(context.ChangeLogs, o => o.Id, o => o.ChangeLogId, (o, p) => new { o, p })
.SelectMany(a => a.p.DefaultIfEmpty(), (a, p) => new { a.o, p })
.TagWith("PruneSelfJoins")
.Load();
}

Expand All @@ -279,6 +282,7 @@ public virtual void GroupJoin_2_3()
context.ChangeLogs
.GroupJoin(context.OwnedThrees, o => o.ChangeLogId, o => o.Id, (o, p) => new { o, p })
.SelectMany(a => a.p.DefaultIfEmpty(), (a, p) => new { a.o, p })
.TagWith("PruneSelfJoins")
.Load();
}

Expand All @@ -293,6 +297,7 @@ public virtual void ReallyJoin()
.SelectMany(a => a.p.DefaultIfEmpty(), (a, p) => new { a.o, p })
.Join(context.OwnedThrees, o => o.o.Id, o => o.Other, (a, o) => new { a.o, a.p, o2 = o })
.Where(o => o.o.Other != 3)
.TagWith("PruneSelfJoins")
.Load();
}

Expand All @@ -304,7 +309,6 @@ public virtual void Owned_SkipTrimming()

context.NormalEntities
.Join(context.NormalEntities, a => a.Id, a => a.Id, (a, b) => a)
.TagWith("SkipSelfJoinsPruning")
.Load();
}

Expand All @@ -323,6 +327,7 @@ public virtual void ShaperChanged()
context.NormalEntities
.Where(a => a.Age == 50))
.TagWith("hello")
.TagWith("PruneSelfJoins")
.Load();
}

Expand All @@ -343,6 +348,7 @@ public virtual void ReallyUnionDistinct()
context.NormalEntities
.Where(a => a.Age == 50)
.Select(n => new { n.Id, n.Name, n.Age, Type = 3 }))
.TagWith("PruneSelfJoins")
.Load();
}

Expand All @@ -360,6 +366,7 @@ public virtual void OwnedThenUnionDistinct()
.Union(
context.ChangeLogs
.Where(a => a.ChangeLogId == 50))
.TagWith("PruneSelfJoins")
.Load();
}

Expand All @@ -379,7 +386,7 @@ public virtual void Manual_OnlyLeftJoin()
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();
query.TagWith("PruneSelfJoins").Load();
}

[ConditionalFact, TestPriority(13)]
Expand All @@ -398,7 +405,7 @@ public virtual void Manual_LeftAndInnerJoin()
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();
query.TagWith("PruneSelfJoins").Load();
}
}
}

0 comments on commit 89dd492

Please sign in to comment.