Пример #1
0
        public virtual Expression GetOuterJoinTest(SelectExpression select)
        {
            // if the column is used in the join condition (equality test)
            // if it is null in the database then the join test won't match (null != null) so the row won't appear
            // we can safely use this existing column as our test to determine if the outer join produced a row

            // find a column that is used in equality test
            HashSet <TableAlias>    aliases     = DeclaredAliasGatherer.Gather(select.From);
            List <ColumnExpression> joinColumns = JoinColumnGatherer.Gather(aliases, select).ToList();

            if (joinColumns.Count > 0)
            {
                // prefer one that is already in the projection list.
                foreach (ColumnExpression jc in joinColumns)
                {
                    foreach (ColumnDeclaration col in select.Columns)
                    {
                        if (jc.Equals(col.Expression))
                        {
                            return(jc);
                        }
                    }
                }
                return(joinColumns[0]);
            }

            // fall back to introducing a constant
            return(Expression.Constant(1, typeof(int?)));
        }
Пример #2
0
        protected override Expression VisitSelect(SelectExpression select)
        {
            bool saveIsOuterMostSelect = isOuterMostSelect;

            try {
                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)
                {
                    PrependOrderings(select.OrderBy);
                }

                if (select.IsReverse)
                {
                    ReverseOrderings();
                }

                IEnumerable <OrderExpression> orderings = null;
                if (canReceiveOrderings)
                {
                    orderings = gatheredOrderings;
                }
                else if (canHaveOrderBy)
                {
                    orderings = select.OrderBy;
                }
                bool canPassOnOrderings = !saveIsOuterMostSelect && !hasGroupBy && !select.IsDistinct;
                ReadOnlyCollection <ColumnDeclaration> columns = select.Columns;
                if (gatheredOrderings != null)
                {
                    if (canPassOnOrderings)
                    {
                        HashSet <TableAlias> producedAliases = DeclaredAliasGatherer.Gather(select.From);
                        // reproject order expressions using this select's alias so the outer select will have properly formed expressions
                        BindResult project = RebindOrderings(gatheredOrderings, select.Alias, producedAliases, select.Columns);
                        gatheredOrderings = null;
                        PrependOrderings(project.Orderings);
                        columns = project.Columns;
                    }
                    else
                    {
                        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 {
                isOuterMostSelect = saveIsOuterMostSelect;
            }
        }
Пример #3
0
 private void MapAliases(Expression a, Expression b)
 {
     TableAlias[] prodA = DeclaredAliasGatherer.Gather(a).ToArray();
     TableAlias[] prodB = DeclaredAliasGatherer.Gather(b).ToArray();
     for (int i = 0, n = prodA.Length; i < n; i++)
     {
         aliasScope.Add(prodA[i], prodB[i]);
     }
 }
Пример #4
0
        private Expression MakeSubquery(Expression expression)
        {
            var newAlias = new TableAlias();
            HashSet <TableAlias> aliases = DeclaredAliasGatherer.Gather(expression);

            var decls = new List <ColumnDeclaration>();

            foreach (TableAlias ta in aliases)
            {
                foreach (ColumnExpression col in columns[ta])
                {
                    string name = decls.GetAvailableColumnName(col.Name);
                    var    decl = new ColumnDeclaration(name, col, col.QueryType);
                    decls.Add(decl);
                    var newCol = new ColumnExpression(col.Type, col.QueryType, newAlias, col.Name);
                    map.Add(col, newCol);
                }
            }

            return(new SelectExpression(newAlias, decls, expression, null));
        }
Пример #5
0
 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);
 }
Пример #6
0
        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);
        }