private SqlOrderbyClause Substitute(SqlSelectSpec spec, SqlIdentifier inputParam, SqlOrderbyClause orderByClause) { if (orderByClause == null) { return(null); } if (spec is SqlSelectStarSpec) { return(orderByClause); } SqlSelectValueSpec selValue = spec as SqlSelectValueSpec; if (selValue != null) { SqlScalarExpression replaced = selValue.Expression; SqlOrderByItem[] substitutedItems = new SqlOrderByItem[orderByClause.OrderbyItems.Count]; for (int i = 0; i < substitutedItems.Length; ++i) { SqlScalarExpression substituted = SqlExpressionManipulation.Substitute(replaced, inputParam, orderByClause.OrderbyItems[i].Expression); substitutedItems[i] = SqlOrderByItem.Create(substituted, orderByClause.OrderbyItems[i].IsDescending); } SqlOrderbyClause result = SqlOrderbyClause.Create(substitutedItems); return(result); } throw new DocumentQueryException("Unexpected SQL select clause type: " + spec.Kind); }
private SqlWhereClause Substitute(SqlSelectSpec spec, SqlIdentifier inputParam, SqlWhereClause whereClause) { if (whereClause == null) { return(null); } if (spec is SqlSelectStarSpec) { return(whereClause); } else { SqlSelectValueSpec selValue = spec as SqlSelectValueSpec; if (selValue != null) { SqlScalarExpression replaced = selValue.Expression; SqlScalarExpression original = whereClause.FilterExpression; SqlScalarExpression substituted = SqlExpressionManipulation.Substitute(replaced, inputParam, original); SqlWhereClause result = SqlWhereClause.Create(substituted); return(result); } } throw new DocumentQueryException("Unexpected SQL select clause type: " + spec.Kind); }
public override SqlObject VisitSelect_clause([NotNull] sqlParser.Select_clauseContext context) { SqlSelectSpec sqlSelectSpec = (SqlSelectSpec)this.Visit(context.selection()); SqlTopSpec sqlTopSpec; if (context.top_spec() != default) { sqlTopSpec = (SqlTopSpec)this.Visit(context.top_spec()); } else { sqlTopSpec = default; } bool distinct = context.K_DISTINCT() != default; return(SqlSelectClause.Create(sqlSelectSpec, sqlTopSpec, distinct)); }
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()); } } }
private SqlSelectClause Substitute(SqlSelectClause inputSelectClause, SqlTopSpec topSpec, SqlIdentifier inputParam, SqlSelectClause selectClause) { SqlSelectSpec selectSpec = inputSelectClause.SelectSpec; if (selectClause == null) { return(selectSpec != null?SqlSelectClause.Create(selectSpec, topSpec, inputSelectClause.HasDistinct) : null); } if (selectSpec is SqlSelectStarSpec) { return(SqlSelectClause.Create(selectSpec, topSpec, inputSelectClause.HasDistinct)); } SqlSelectValueSpec selValue = selectSpec as SqlSelectValueSpec; if (selValue != null) { SqlSelectSpec intoSpec = selectClause.SelectSpec; if (intoSpec is SqlSelectStarSpec) { return(SqlSelectClause.Create(selectSpec, topSpec, selectClause.HasDistinct || inputSelectClause.HasDistinct)); } SqlSelectValueSpec intoSelValue = intoSpec as SqlSelectValueSpec; if (intoSelValue != null) { SqlScalarExpression replacement = SqlExpressionManipulation.Substitute(selValue.Expression, inputParam, intoSelValue.Expression); SqlSelectValueSpec selValueReplacement = SqlSelectValueSpec.Create(replacement); return(SqlSelectClause.Create(selValueReplacement, topSpec, selectClause.HasDistinct || inputSelectClause.HasDistinct)); } throw new DocumentQueryException("Unexpected SQL select clause type: " + intoSpec.Kind); } throw new DocumentQueryException("Unexpected SQL select clause type: " + selectSpec.Kind); }
/// <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)); }
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)); }