internal static bool HasAggregates(SelectExpression expression) { AggregateChecker checker = new AggregateChecker(); checker.Visit(expression); return(checker.hasAggregate); }
private bool CanJoinOnServer(SelectExpression select) { // can add singleton (1:0,1) join if no grouping/aggregates or distinct return(!select.IsDistinct && (select.GroupBy == null || select.GroupBy.Count == 0) && !AggregateChecker.HasAggregates(select)); }
private bool CanJoinOnClient(SelectExpression select) { // can add singleton (1:0,1) join if no grouping/aggregates or distinct return (this.canJoinOnClient && this.currentMember != null && !this.policy.IsDeferLoaded(this.currentMember) && !select.IsDistinct && (select.GroupBy == null || select.GroupBy.Count == 0) && !AggregateChecker.HasAggregates(select)); }
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 { SelectExpression 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; var pc = ColumnProjector.ProjectColumns(this.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); }
private static bool CanMergeWithFrom(SelectExpression select, bool isTopLevel) { SelectExpression fromSelect = GetLeftMostSelect(select.From); if (fromSelect == null) { return(false); } if (!IsColumnProjection(fromSelect)) { return(false); } bool selHasNameMapProjection = IsNameMapProjection(select); bool selHasOrderBy = select.OrderBy != null && select.OrderBy.Count > 0; bool selHasGroupBy = select.GroupBy != null && select.GroupBy.Count > 0; bool selHasAggregates = AggregateChecker.HasAggregates(select); bool selHasJoin = select.From is JoinExpression; bool frmHasOrderBy = fromSelect.OrderBy != null && fromSelect.OrderBy.Count > 0; bool frmHasGroupBy = fromSelect.GroupBy != null && fromSelect.GroupBy.Count > 0; bool frmHasAggregates = AggregateChecker.HasAggregates(fromSelect); // both cannot have orderby if (selHasOrderBy && frmHasOrderBy) { return(false); } // both cannot have groupby if (selHasGroupBy && frmHasGroupBy) { return(false); } // these are distinct operations if (select.IsReverse || fromSelect.IsReverse) { return(false); } // cannot move forward order-by if outer has group-by if (frmHasOrderBy && (selHasGroupBy || selHasAggregates || select.IsDistinct)) { return(false); } // cannot move forward group-by if outer has where clause if (frmHasGroupBy /*&& (select.Where != null)*/) // need to assert projection is the same in order to move group-by forward { return(false); } // cannot move forward a take if outer has take or skip or distinct if (fromSelect.Take != null && (select.Take != null || select.Skip != null || select.IsDistinct || selHasAggregates || selHasGroupBy || selHasJoin)) { return(false); } // cannot move forward a skip if outer has skip or distinct if (fromSelect.Skip != null && (select.Skip != null || select.IsDistinct || selHasAggregates || selHasGroupBy || selHasJoin)) { return(false); } // cannot move forward a distinct if outer has take, skip, groupby or a different projection if (fromSelect.IsDistinct && (select.Take != null || select.Skip != null || !selHasNameMapProjection || selHasGroupBy || selHasAggregates || (selHasOrderBy && !isTopLevel) || selHasJoin)) { return(false); } if (frmHasAggregates && (select.Take != null || select.Skip != null || select.IsDistinct || selHasAggregates || selHasGroupBy || selHasJoin)) { return(false); } return(true); }
protected override Expression VisitSelect(SelectExpression select) { bool saveIsOuterMostSelect = this.isOuterMostSelect; try { this.isOuterMostSelect = false; select = (SelectExpression)base.VisitSelect(select); bool hasOrderBy = select.OrderBy != null && select.OrderBy.Count > 0; bool hasGroupBy = select.GroupBy != null && select.GroupBy.Count > 0; bool canHaveOrderBy = saveIsOuterMostSelect || select.Take != null || select.Skip != null; bool canReceiveOrderings = canHaveOrderBy && !hasGroupBy && !select.IsDistinct && !AggregateChecker.HasAggregates(select); if (hasOrderBy) { this.PrependOrderings(select.OrderBy); } if (select.IsReverse) { this.ReverseOrderings(); } IEnumerable <OrderExpression> orderings = null; if (canReceiveOrderings) { orderings = this.gatheredOrderings; } else if (canHaveOrderBy) { orderings = select.OrderBy; } bool canPassOnOrderings = !saveIsOuterMostSelect && !hasGroupBy && !select.IsDistinct; ReadOnlyCollection <ColumnDeclaration> columns = select.Columns; if (this.gatheredOrderings != null) { if (canPassOnOrderings) { var producedAliases = DeclaredAliasGatherer.Gather(select.From); // reproject order expressions using this select's alias so the outer select will have properly formed expressions BindResult project = this.RebindOrderings(this.gatheredOrderings, select.Alias, producedAliases, select.Columns); this.gatheredOrderings = null; this.PrependOrderings(project.Orderings); columns = project.Columns; } else { this.gatheredOrderings = null; } } if (orderings != select.OrderBy || columns != select.Columns || select.IsReverse) { select = new SelectExpression(select.Alias, columns, select.From, select.Where, orderings, select.GroupBy, select.IsDistinct, select.Skip, select.Take, false); } return(select); } finally { this.isOuterMostSelect = saveIsOuterMostSelect; } }