Ejemplo n.º 1
0
        private static IEnumerable <CosmosElement> ProjectOnGroupings(
            IEnumerable <IGrouping <GroupByKey, CosmosElement> > groupings,
            SqlSelectClause sqlSelectClause)
        {
            foreach (IGrouping <GroupByKey, CosmosElement> grouping in groupings)
            {
                IEnumerable <CosmosElement> dataSource = grouping;
                if (AggregateProjectionDector.HasAggregate(sqlSelectClause.SelectSpec))
                {
                    // If there is an aggregate then we need to just project out the one document
                    // But we need to transform the query to first evaluate the aggregate on all the documents
                    AggregateProjectionTransformer aggregateProjectionTransformer = new AggregateProjectionTransformer(dataSource);
                    SqlSelectSpec transformedSpec = aggregateProjectionTransformer
                                                    .TransformAggregatesInProjection(sqlSelectClause.SelectSpec);
                    CosmosElement aggregationResult = transformedSpec.Accept(
                        Projector.Singleton,
                        dataSource.FirstOrDefault());
                    if (aggregationResult != null)
                    {
                        dataSource = new CosmosElement[] { aggregationResult };
                    }
                    else
                    {
                        dataSource = Array.Empty <CosmosElement>();
                    }
                }
                else
                {
                    dataSource = dataSource
                                 .Select(element => sqlSelectClause.SelectSpec.Accept(
                                             Projector.Singleton,
                                             element))
                                 .Where(projection => projection != Undefined);
                }

                if (dataSource.Any())
                {
                    yield return(dataSource.First());
                }
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Determines whether or not the SqlSelectSpec has an aggregate in the outer most query.
 /// </summary>
 /// <param name="selectSpec">The select spec to traverse.</param>
 /// <returns>Whether or not the SqlSelectSpec has an aggregate in the outer most query.</returns>
 public static bool HasAggregate(SqlSelectSpec selectSpec)
 {
     return(selectSpec.Accept(AggregateProjectionDectorVisitor.Singleton));
 }
Ejemplo n.º 3
0
        private static void AssertEvaluation(CosmosElement expected, SqlSelectSpec sqlSelectSpec, CosmosElement document = null)
        {
            CosmosElement evaluation = sqlSelectSpec.Accept(Projector.Singleton, document);

            Assert.AreEqual(expected, evaluation);
        }
 public SqlSelectSpec TransformAggregatesInProjection(SqlSelectSpec selectSpec)
 {
     return(selectSpec.Accept(this.visitor));
 }