protected override Expression VisitJoin(JoinExpression join) { join = (JoinExpression)base.VisitJoin(join); if (join.Join == JoinType.CrossJoin && currentWhere != null) { // try to figure out which parts of the current where expression can be used for a join condition HashSet <TableAlias> declaredLeft = DeclaredAliasGatherer.Gather(join.Left); HashSet <TableAlias> declaredRight = DeclaredAliasGatherer.Gather(join.Right); var declared = new HashSet <TableAlias>(declaredLeft.Union(declaredRight)); Expression[] exprs = currentWhere.Split(ExpressionType.And, ExpressionType.AndAlso); List <Expression> good = exprs.Where(e => CanBeJoinCondition(e, declaredLeft, declaredRight, declared)).ToList(); if (good.Count > 0) { Expression condition = good.Join(ExpressionType.And); join = UpdateJoin(join, JoinType.InnerJoin, join.Left, join.Right, condition); Expression newWhere = exprs.Where(e => !good.Contains(e)).Join(ExpressionType.And); currentWhere = newWhere; } } return(join); }
protected override Expression VisitJoin(JoinExpression join) { join = (JoinExpression)base.VisitJoin(join); if (join.Join == JoinType.CrossApply || join.Join == JoinType.OuterApply) { if (join.Right is TableExpression) { return(new JoinExpression(JoinType.CrossJoin, join.Left, join.Right, null)); } else { var select = join.Right as SelectExpression; // Only consider rewriting cross apply if // 1) right side is a select // 2) other than in the where clause in the right-side select, no left-side declared aliases are referenced // 3) and has no behavior that would change semantics if the where clause is removed (like groups, aggregates, take, skip, etc). // Note: it is best to attempt this after redundant subqueries have been removed. if (select != null && select.Take == null && select.Skip == null && !AggregateChecker.HasAggregates(select) && (select.GroupBy == null || select.GroupBy.Count == 0)) { SelectExpression selectWithoutWhere = select.SetWhere(null); HashSet <TableAlias> referencedAliases = ReferencedAliasGatherer.Gather(selectWithoutWhere); HashSet <TableAlias> declaredAliases = DeclaredAliasGatherer.Gather(join.Left); referencedAliases.IntersectWith(declaredAliases); if (referencedAliases.Count == 0) { Expression where = select.Where; select = selectWithoutWhere; ProjectedColumns pc = ColumnProjector.ProjectColumns(language, where, select.Columns, select.Alias, DeclaredAliasGatherer.Gather(select.From)); select = select.SetColumns(pc.Columns); where = pc.Projector; JoinType jt = (where == null) ? JoinType.CrossJoin : (join.Join == JoinType.CrossApply ? JoinType.InnerJoin : JoinType.LeftOuter); return(new JoinExpression(jt, join.Left, select, where)); } } } } return(join); }
protected override Expression VisitJoin(JoinExpression join) { VisitJoinLeft(join.Left); WriteLine(Indentation.Same); switch (join.Join) { case JoinType.CrossJoin: Write("CROSS JOIN "); break; case JoinType.InnerJoin: Write("INNER JOIN "); break; case JoinType.CrossApply: Write("CROSS APPLY "); break; case JoinType.OuterApply: Write("OUTER APPLY "); break; case JoinType.LeftOuter: case JoinType.SingletonLeftOuter: Write("LEFT OUTER JOIN "); break; } VisitJoinRight(join.Right); if (join.Condition != null) { WriteLine(Indentation.Inner); Write("ON "); VisitPredicate(join.Condition); Indent(Indentation.Outer); } return(join); }
protected override Expression VisitProjection(ProjectionExpression proj) { SelectExpression save = currentSelect; currentSelect = proj.Select; try { if (!isTopLevel) { if (CanJoinOnClient(currentSelect)) { // make a query that combines all the constraints from the outer queries into a single select var newOuterSelect = (SelectExpression)QueryDuplicator.Duplicate(save); // remap any references to the outer select to the new alias; var newInnerSelect = (SelectExpression)ColumnMapper.Map(proj.Select, newOuterSelect.Alias, save.Alias); // add outer-join test ProjectionExpression newInnerProjection = language.AddOuterJoinTest(new ProjectionExpression(newInnerSelect, proj.Projector)); newInnerSelect = newInnerProjection.Select; Expression newProjector = newInnerProjection.Projector; var newAlias = new TableAlias(); ProjectedColumns pc = ColumnProjector.ProjectColumns(language, newProjector, null, newAlias, newOuterSelect.Alias, newInnerSelect.Alias); var join = new JoinExpression(JoinType.OuterApply, newOuterSelect, newInnerSelect, null); var joinedSelect = new SelectExpression(newAlias, pc.Columns, join, null, null, null, proj.IsSingleton, null, null, false); // apply client-join treatment recursively currentSelect = joinedSelect; newProjector = Visit(pc.Projector); // compute keys (this only works if join condition was a single column comparison) var outerKeys = new List <Expression>(); var innerKeys = new List <Expression>(); if (GetEquiJoinKeyExpressions(newInnerSelect.Where, newOuterSelect.Alias, outerKeys, innerKeys)) { // outerKey needs to refer to the outer-scope's alias IEnumerable <Expression> outerKey = outerKeys.Select(k => ColumnMapper.Map(k, save.Alias, newOuterSelect.Alias)); // innerKey needs to refer to the new alias for the select with the new join IEnumerable <Expression> innerKey = innerKeys.Select(k => ColumnMapper.Map(k, joinedSelect.Alias, ((ColumnExpression)k).Alias)); var newProjection = new ProjectionExpression(joinedSelect, newProjector, proj.Aggregator); return(new ClientJoinExpression(newProjection, outerKey, innerKey)); } } else { bool saveJoin = canJoinOnClient; canJoinOnClient = false; Expression result = base.VisitProjection(proj); canJoinOnClient = saveJoin; return(result); } } else { isTopLevel = false; } return(base.VisitProjection(proj)); } finally { currentSelect = save; } }