public override object Read(CompactBinaryReader reader) { ISerializationSurrogate decimalSurrogate = TypeSurrogateSelector.GetSurrogateForType(typeof(decimal), null); AverageResult result = new AverageResult(); result.Sum = (decimal)decimalSurrogate.Read(reader); result.Count = (decimal)decimalSurrogate.Read(reader); return result; }
internal override void Execute(QueryContext queryContext, Predicate nextPredicate) { if(ChildPredicate!=null) ChildPredicate.Execute(queryContext, nextPredicate); queryContext.Tree.Reduce(); CacheEntry entry = null; decimal sum = 0; if (queryContext.Tree.LeftList.Count > 0) { foreach (string key in queryContext.Tree.LeftList) { CacheEntry cacheentry = queryContext.Cache.GetEntryInternal(key, false); object attribValue = queryContext.Index.GetAttributeValue(key, AttributeName, cacheentry.IndexInfo); if (attribValue != null) { Type type = attribValue.GetType(); if ((type == typeof(bool)) || (type == typeof(DateTime)) || (type == typeof(string)) || (type == typeof(char))) throw new Exception("AVG can only be applied to integral data types."); sum += Convert.ToDecimal(attribValue); } } AverageResult avgResult = new AverageResult(); avgResult.Sum = sum; avgResult.Count = queryContext.Tree.LeftList.Count; //put the count and the sum base.SetResult(queryContext, AggregateFunctionType.AVG, avgResult); } else { base.SetResult(queryContext, AggregateFunctionType.AVG, null); } }
public void Compile(QueryResultSet resultSet) { if (!this._isInitialized) { Initialize(resultSet); return; } switch (this.Type) { case QueryType.AggregateFunction: switch ((AggregateFunctionType)this.AggregateFunctionResult.Key) { case AggregateFunctionType.SUM: decimal a; decimal b; object thisVal = this.AggregateFunctionResult.Value; object otherVal = resultSet.AggregateFunctionResult.Value; Nullable<decimal> sum = null; if (thisVal == null && otherVal != null) { sum = (decimal)otherVal; } else if (thisVal != null && otherVal == null) { sum = (decimal)thisVal; } else if (thisVal != null && otherVal != null) { a = (decimal)thisVal; b = (decimal)otherVal; sum = a + b; } if (sum != null) { this.AggregateFunctionResult = new DictionaryEntry(AggregateFunctionType.SUM, sum); } else { this.AggregateFunctionResult = new DictionaryEntry(AggregateFunctionType.SUM, null); } break; case AggregateFunctionType.COUNT: a = (decimal)this.AggregateFunctionResult.Value; b = (decimal)resultSet.AggregateFunctionResult.Value; decimal count = a + b; this.AggregateFunctionResult = new DictionaryEntry(AggregateFunctionType.COUNT, count); break; case AggregateFunctionType.MIN: IComparable thisValue = (IComparable)this.AggregateFunctionResult.Value; IComparable otherValue = (IComparable)resultSet.AggregateFunctionResult.Value; IComparable min = thisValue; if (thisValue == null && otherValue != null) { min = otherValue; } else if (thisValue != null && otherValue == null) { min = thisValue; } else if (thisValue == null && otherValue == null) { min = null; } else if (otherValue.CompareTo(thisValue) < 0) { min = otherValue; } this.AggregateFunctionResult = new DictionaryEntry(AggregateFunctionType.MIN, min); break; case AggregateFunctionType.MAX: thisValue = (IComparable)this.AggregateFunctionResult.Value; otherValue = (IComparable)resultSet.AggregateFunctionResult.Value; IComparable max = thisValue; if (thisValue == null && otherValue != null) { max = otherValue; } else if (thisValue != null && otherValue == null) { max = thisValue; } else if (thisValue == null && otherValue == null) { max = null; } else if (otherValue.CompareTo(thisValue) > 0) { max = otherValue; } this.AggregateFunctionResult = new DictionaryEntry(AggregateFunctionType.MAX, max); break; case AggregateFunctionType.AVG: thisVal = this.AggregateFunctionResult.Value; otherVal = resultSet.AggregateFunctionResult.Value; AverageResult avg = null; if (thisVal == null && otherVal != null) { avg = (AverageResult)otherVal; } else if (thisVal != null && otherVal == null) { avg = (AverageResult)thisVal; } else if (thisVal != null && otherVal != null) { AverageResult thisResult = (AverageResult)thisVal; AverageResult otherResult = (AverageResult)otherVal; avg = new AverageResult(); avg.Sum = thisResult.Sum + otherResult.Sum; avg.Count = thisResult.Count + otherResult.Count; } if (avg != null) { this.AggregateFunctionResult = new DictionaryEntry(AggregateFunctionType.AVG, avg); } else { this.AggregateFunctionResult = new DictionaryEntry(AggregateFunctionType.AVG, null); } break; } break; case QueryType.SearchKeys: if (this.SearchKeysResult == null) this.SearchKeysResult = resultSet.SearchKeysResult; else if (resultSet.SearchKeysResult != null) this.SearchKeysResult.AddRange(resultSet.SearchKeysResult); break; case QueryType.SearchEntries: if (this.SearchEntriesResult == null) this.SearchEntriesResult = resultSet.SearchEntriesResult; else { IDictionaryEnumerator ide = resultSet.SearchEntriesResult.GetEnumerator(); while (ide.MoveNext()) { this.SearchEntriesResult[ide.Key]=ide.Value; } } break; } }