Aggregation.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 : IExpression {
  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 object Eval (DataRow row)
  54. {
  55. //TODO: implement a better caching strategy and a mechanism for cache invalidation.
  56. //for now only aggregation over the table owning 'row' (e.g. 'sum(parts)'
  57. //in constrast to 'sum(parent.parts)' and 'sum(child.parts)') is cached.
  58. if (cacheResults && result != null && column.ReferencedTable == ReferencedTable.Self)
  59. return result;
  60. count = 0;
  61. result = null;
  62. object[] values;
  63. if (rows == null)
  64. values = column.GetValues (column.GetReferencedRows (row));
  65. else
  66. values = column.GetValues (rows);
  67. foreach (object val in values) {
  68. if (val == null)
  69. continue;
  70. count++;
  71. Aggregate ((IConvertible)val);
  72. }
  73. switch (function) {
  74. case AggregationFunction.StDev:
  75. case AggregationFunction.Var:
  76. result = CalcStatisticalFunction (values);
  77. break;
  78. case AggregationFunction.Avg:
  79. result = Numeric.Divide (result, count);
  80. break;
  81. case AggregationFunction.Count:
  82. result = count;
  83. break;
  84. }
  85. if (result == null)
  86. result = 0;
  87. return result;
  88. }
  89. private void Aggregate (IConvertible val)
  90. {
  91. switch (function) {
  92. case AggregationFunction.Min:
  93. result = (result != null ? Numeric.Min (result, val) : val);
  94. return;
  95. case AggregationFunction.Max:
  96. result = (result != null ? Numeric.Max (result, val) : val);
  97. return;
  98. case AggregationFunction.Sum:
  99. case AggregationFunction.Avg:
  100. case AggregationFunction.StDev:
  101. case AggregationFunction.Var:
  102. result = (result != null ? Numeric.Add (result, val) : val);
  103. return;
  104. }
  105. }
  106. private IConvertible CalcStatisticalFunction (object[] values)
  107. {
  108. double average = (double)Convert.ChangeType(result, TypeCode.Double) / count;
  109. double res = 0.0;
  110. foreach (object val in values) {
  111. if (val == null)
  112. continue;
  113. double diff = average - (double)Convert.ChangeType(val, TypeCode.Double);
  114. res += System.Math.Pow (diff, 2);
  115. }
  116. res /= (count - 1);
  117. if (function == AggregationFunction.StDev)
  118. res = System.Math.Sqrt (res);
  119. return res;
  120. }
  121. }
  122. }