Ejemplo n.º 1
0
        /// <summary>
        /// Checks whether an expression (no aggregation) is valid, in terms of grouping expressions.
        /// That is to say, if the group by is set, the clause provides expressions to group the results with.
        /// This function should check, whether the expressions in other clauses are the same as in the grouping exp.
        /// Because, only the same expressions can be referenced throughout the query + aggregates.
        ///
        /// If no group by is set and aggregation is referenced, then only aggregations can be referenced in the entire query -> single group group by.
        ///
        /// This function is very simplified becuase expressions contain only one block.
        /// Thus, it must be reimplemented in the future.
        /// So far called only from Select clause parser.
        /// </summary>
        /// <param name="expressionHolder"> An expression. </param>
        /// <returns> Returns a position of the passed expression in the main expression list.  </returns>
        public int AddExpression(ExpressionHolder expressionHolder)
        {
            CheckNullExpression(expressionHolder);

            // Only expressions from group by clause and aggregates can be used.
            if (this.IsSetGroupBy)
            {
                // Aggregates in expressions can be referenced freely
                if (!expressionHolder.ContainsAggregate())
                {
                    // There can be referenced only expressions from group by.
                    if (this.GroupByhashExprs.IndexOf(expressionHolder) == -1)
                    {
                        throw new ArgumentException($"{this.GetType()}, expression in the query can contain only references from group by clause.");
                    }
                    else
                    {
                        return(this.Exprs.IndexOf(expressionHolder));
                    }
                    // The check is not done, because the aggregate reference is create at the same time as the corresponding aggregate.
                }
                else
                {
                    return(InsertExpr(expressionHolder));
                }
            }
            else
            {
                // No group by is set.
                if ((expressionHolder.ContainsAggregate() && ContainsSimpleExpr()) || (!expressionHolder.ContainsAggregate() && this.Aggregates.Count > 0))
                {
                    throw new ArgumentException($"{this.GetType()}, there was references an aggregate and a simple expression while group by is not set.");
                }
                else
                {
                    return(InsertExpr(expressionHolder));
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Adds a expression for order by.
        /// Cannot contain multiple occurrences of the same expression.
        /// </summary>
        /// <param name="expressionHolder"> An expression to sort with. </param>
        /// <returns> Returns a position of the passed expression in the main expression list. </returns>
        public int AddOrderByComp(ExpressionHolder expressionHolder)
        {
            CheckNullExpression(expressionHolder);

            if (this.OrderByComparerExprs.Contains(expressionHolder))
            {
                throw new ArgumentException($"{this.GetType()}, order by clause cannot contain the same comparer expression multiple times.");
            }
            else if (expressionHolder.ContainsAggregate())
            {
                throw new ArgumentException($"{this.GetType()}, order by clause cannot contain aggregates.");
            }
            else
            {
                var pos = InsertExpr(expressionHolder);
                this.OrderByComparerExprs.Add(this.Exprs[pos]);
                return(pos);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Adds a hash expression for group by.
        /// Cannot contain aggregations.
        /// Each hash can be added only once.
        /// Note that this method is called only when the group by keys are created.
        /// Thus, hashes and expr have the same count.
        /// </summary>
        /// <param name="expressionHolder"> An expression to hash with. </param>
        /// <returns> Returns a position of the passed expression in the main expression list. </returns>
        public int AddGroupByHash(ExpressionHolder expressionHolder)
        {
            CheckNullExpression(expressionHolder);

            if (expressionHolder.ContainsAggregate())
            {
                throw new ArgumentException($"{this.GetType()}, group by clause cannot contain aggregates.");
            }
            else if (this.GroupByhashExprs.Contains(expressionHolder))
            {
                throw new ArgumentException($"{this.GetType()}, group by clause cannot contain the same key multiple times.");
            }
            else
            {
                var pos = InsertExpr(expressionHolder);
                this.GroupByhashExprs.Add(this.Exprs[pos]);
                return(pos);
            }
        }