Aggregation.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // Aggregation.cs
  3. //
  4. // Author:
  5. // Juraj Skripsky ([email protected])
  6. //
  7. // (C) 2004 HotFeet GmbH (http://www.hotfeet.ch)
  8. //
  9. using System;
  10. using System.Collections;
  11. using System.Data;
  12. namespace Mono.Data.SqlExpressions {
  13. public enum AggregationFunction {
  14. Count, Sum, Min, Max, Avg, StDev, Var
  15. }
  16. public class Aggregation : IExpression {
  17. bool cacheResults;
  18. DataRow[] rows;
  19. ColumnReference column;
  20. AggregationFunction function;
  21. int count;
  22. IConvertible result;
  23. public Aggregation (bool cacheResults, DataRow[] rows, AggregationFunction function, ColumnReference column)
  24. {
  25. this.cacheResults = cacheResults;
  26. this.rows = rows;
  27. this.column = column;
  28. this.function = function;
  29. this.result = null;
  30. }
  31. public object Eval (DataRow row)
  32. {
  33. //TODO: implement a better caching strategy and a mechanism for cache invalidation.
  34. //for now only aggregation over the table owning 'row' (e.g. 'sum(parts)'
  35. //in constrast to 'sum(parent.parts)' and 'sum(child.parts)') is cached.
  36. if (cacheResults && result != null && column.ReferencedTable == ReferencedTable.Self)
  37. return result;
  38. count = 0;
  39. result = null;
  40. object[] values;
  41. if (rows == null)
  42. values = column.GetValues (column.GetReferencedRows (row));
  43. else
  44. values = column.GetValues (rows);
  45. foreach (object val in values) {
  46. if (val == null)
  47. continue;
  48. count++;
  49. Aggregate ((IConvertible)val);
  50. }
  51. switch (function) {
  52. case AggregationFunction.StDev:
  53. case AggregationFunction.Var:
  54. result = CalcStatisticalFunction (values);
  55. break;
  56. case AggregationFunction.Avg:
  57. result = Numeric.Divide (result, count);
  58. break;
  59. case AggregationFunction.Count:
  60. result = count;
  61. break;
  62. }
  63. if (result == null)
  64. result = 0;
  65. return result;
  66. }
  67. private void Aggregate (IConvertible val)
  68. {
  69. switch (function) {
  70. case AggregationFunction.Min:
  71. result = (result != null ? Numeric.Min (result, val) : val);
  72. return;
  73. case AggregationFunction.Max:
  74. result = (result != null ? Numeric.Max (result, val) : val);
  75. return;
  76. case AggregationFunction.Sum:
  77. case AggregationFunction.Avg:
  78. case AggregationFunction.StDev:
  79. case AggregationFunction.Var:
  80. result = (result != null ? Numeric.Add (result, val) : val);
  81. return;
  82. }
  83. }
  84. private IConvertible CalcStatisticalFunction (object[] values)
  85. {
  86. double average = (double)Convert.ChangeType(result, TypeCode.Double) / count;
  87. double res = 0.0;
  88. foreach (object val in values) {
  89. if (val == null)
  90. continue;
  91. double diff = average - (double)Convert.ChangeType(val, TypeCode.Double);
  92. res += System.Math.Pow (diff, 2);
  93. }
  94. res /= (count - 1);
  95. if (function == AggregationFunction.StDev)
  96. res = System.Math.Sqrt (res);
  97. return res;
  98. }
  99. }
  100. }