Aggregation.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // Aggregation.cs
  3. //
  4. // Author:
  5. // Juraj Skripsky ([email protected])
  6. //
  7. // (C) 2004 HotFeet GmbH (http://www.hotfeet.ch)
  8. //
  9. //
  10. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Collections;
  33. using System.Data;
  34. namespace Mono.Data.SqlExpressions {
  35. internal enum AggregationFunction {
  36. Count, Sum, Min, Max, Avg, StDev, Var
  37. }
  38. internal class Aggregation : BaseExpression {
  39. bool cacheResults;
  40. DataRow[] rows;
  41. ColumnReference column;
  42. AggregationFunction function;
  43. int count;
  44. IConvertible result;
  45. public Aggregation (bool cacheResults, DataRow[] rows, AggregationFunction function, ColumnReference column)
  46. {
  47. this.cacheResults = cacheResults;
  48. this.rows = rows;
  49. this.column = column;
  50. this.function = function;
  51. this.result = null;
  52. }
  53. public override bool Equals(object obj)
  54. {
  55. if (!base.Equals (obj))
  56. return false;
  57. if (!(obj is Aggregation))
  58. return false;
  59. Aggregation other = (Aggregation) obj;
  60. if (!other.function.Equals( function))
  61. return false;
  62. if (!other.column.Equals (column))
  63. return false;
  64. if (other.rows.Length != rows.Length)
  65. return false;
  66. for (int i=0; i < rows.Length; i++)
  67. if (other.rows [i] != rows [i])
  68. return false;
  69. return true;
  70. }
  71. public override int GetHashCode()
  72. {
  73. int hashCode = base.GetHashCode ();
  74. hashCode ^= function.GetHashCode ();
  75. hashCode ^= column.GetHashCode ();
  76. for (int i=0; i < rows.Length; i++)
  77. hashCode ^= rows [i].GetHashCode ();
  78. return hashCode;
  79. }
  80. public override object Eval (DataRow row)
  81. {
  82. //TODO: implement a better caching strategy and a mechanism for cache invalidation.
  83. //for now only aggregation over the table owning 'row' (e.g. 'sum(parts)'
  84. //in constrast to 'sum(parent.parts)' and 'sum(child.parts)') is cached.
  85. if (cacheResults && result != null && column.ReferencedTable == ReferencedTable.Self)
  86. return result;
  87. count = 0;
  88. result = null;
  89. object[] values;
  90. if (rows == null)
  91. values = column.GetValues (column.GetReferencedRows (row));
  92. else
  93. values = column.GetValues (rows);
  94. foreach (object val in values) {
  95. if (val == null)
  96. continue;
  97. count++;
  98. Aggregate ((IConvertible)val);
  99. }
  100. switch (function) {
  101. case AggregationFunction.StDev:
  102. case AggregationFunction.Var:
  103. result = CalcStatisticalFunction (values);
  104. break;
  105. case AggregationFunction.Avg:
  106. result = Numeric.Divide (result, count);
  107. break;
  108. case AggregationFunction.Count:
  109. result = count;
  110. break;
  111. }
  112. if (result == null)
  113. result = DBNull.Value;
  114. return result;
  115. }
  116. override public bool DependsOn(DataColumn other)
  117. {
  118. return column.DependsOn(other);
  119. }
  120. private void Aggregate (IConvertible val)
  121. {
  122. switch (function) {
  123. case AggregationFunction.Min:
  124. result = (result != null ? Numeric.Min (result, val) : val);
  125. return;
  126. case AggregationFunction.Max:
  127. result = (result != null ? Numeric.Max (result, val) : val);
  128. return;
  129. case AggregationFunction.Sum:
  130. case AggregationFunction.Avg:
  131. case AggregationFunction.StDev:
  132. case AggregationFunction.Var:
  133. result = (result != null ? Numeric.Add (result, val) : val);
  134. return;
  135. }
  136. }
  137. private IConvertible CalcStatisticalFunction (object[] values)
  138. {
  139. double average = (double)Convert.ChangeType(result, TypeCode.Double) / count;
  140. double res = 0.0;
  141. foreach (object val in values) {
  142. if (val == null)
  143. continue;
  144. double diff = average - (double)Convert.ChangeType(val, TypeCode.Double);
  145. res += System.Math.Pow (diff, 2);
  146. }
  147. res /= (count - 1);
  148. if (function == AggregationFunction.StDev)
  149. res = System.Math.Sqrt (res);
  150. return res;
  151. }
  152. }
  153. }