Aggregation.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. DataRowChangeEventHandler RowChangeHandler;
  46. DataTable table ;
  47. public Aggregation (bool cacheResults, DataRow[] rows, AggregationFunction function, ColumnReference column)
  48. {
  49. this.cacheResults = cacheResults;
  50. this.rows = rows;
  51. this.column = column;
  52. this.function = function;
  53. this.result = null;
  54. if (cacheResults)
  55. RowChangeHandler = new DataRowChangeEventHandler (InvalidateCache);
  56. }
  57. public override bool Equals(object obj)
  58. {
  59. if (!base.Equals (obj))
  60. return false;
  61. if (!(obj is Aggregation))
  62. return false;
  63. Aggregation other = (Aggregation) obj;
  64. if (!other.function.Equals( function))
  65. return false;
  66. if (!other.column.Equals (column))
  67. return false;
  68. if (other.rows != null && rows != null) {
  69. if (other.rows.Length != rows.Length)
  70. return false;
  71. for (int i=0; i < rows.Length; i++)
  72. if (other.rows [i] != rows [i])
  73. return false;
  74. }
  75. else if (!(other.rows == null && rows == null))
  76. return false;
  77. return true;
  78. }
  79. public override int GetHashCode()
  80. {
  81. int hashCode = base.GetHashCode ();
  82. hashCode ^= function.GetHashCode ();
  83. hashCode ^= column.GetHashCode ();
  84. for (int i=0; i < rows.Length; i++)
  85. hashCode ^= rows [i].GetHashCode ();
  86. return hashCode;
  87. }
  88. public override object Eval (DataRow row)
  89. {
  90. //TODO: implement a better caching strategy and a mechanism for cache invalidation.
  91. //for now only aggregation over the table owning 'row' (e.g. 'sum(parts)'
  92. //in constrast to 'sum(child.parts)') is cached.
  93. if (cacheResults && result != null && column.ReferencedTable == ReferencedTable.Self)
  94. return result;
  95. count = 0;
  96. result = null;
  97. object[] values;
  98. if (rows == null)
  99. values = column.GetValues (column.GetReferencedRows (row));
  100. else
  101. values = column.GetValues (rows);
  102. foreach (object val in values) {
  103. if (val == null)
  104. continue;
  105. count++;
  106. Aggregate ((IConvertible)val);
  107. }
  108. switch (function) {
  109. case AggregationFunction.StDev:
  110. case AggregationFunction.Var:
  111. result = CalcStatisticalFunction (values);
  112. break;
  113. case AggregationFunction.Avg:
  114. result = ((count == 0) ? DBNull.Value : Numeric.Divide (result, count));
  115. break;
  116. case AggregationFunction.Count:
  117. result = count;
  118. break;
  119. }
  120. if (result == null)
  121. result = DBNull.Value;
  122. if (cacheResults && column.ReferencedTable == ReferencedTable.Self)
  123. {
  124. table = row.Table;
  125. row.Table.RowChanged += RowChangeHandler;
  126. }
  127. return result;
  128. }
  129. override public bool DependsOn(DataColumn other)
  130. {
  131. return column.DependsOn(other);
  132. }
  133. private void Aggregate (IConvertible val)
  134. {
  135. switch (function) {
  136. case AggregationFunction.Min:
  137. result = (result != null ? Numeric.Min (result, val) : val);
  138. return;
  139. case AggregationFunction.Max:
  140. result = (result != null ? Numeric.Max (result, val) : val);
  141. return;
  142. case AggregationFunction.Sum:
  143. case AggregationFunction.Avg:
  144. case AggregationFunction.StDev:
  145. case AggregationFunction.Var:
  146. result = (result != null ? Numeric.Add (result, val) : val);
  147. return;
  148. }
  149. }
  150. private IConvertible CalcStatisticalFunction (object[] values)
  151. {
  152. if (count < 2)
  153. return DBNull.Value;
  154. double average = (double)Convert.ChangeType(result, TypeCode.Double) / count;
  155. double res = 0.0;
  156. foreach (object val in values) {
  157. if (val == null)
  158. continue;
  159. double diff = average - (double)Convert.ChangeType(val, TypeCode.Double);
  160. res += System.Math.Pow (diff, 2);
  161. }
  162. res /= (count - 1);
  163. if (function == AggregationFunction.StDev)
  164. res = System.Math.Sqrt (res);
  165. return res;
  166. }
  167. public override void ResetExpression ()
  168. {
  169. if (table != null)
  170. InvalidateCache (table, null);
  171. }
  172. private void InvalidateCache (Object sender, DataRowChangeEventArgs args)
  173. {
  174. result = null;
  175. ((DataTable)sender).RowChanged -= RowChangeHandler;
  176. }
  177. }
  178. }