public AggregateExpression(Type type, AggregateType aggregateType, Expression argument, bool distinct)
     : base(MongoExpressionType.Aggregate, type)
 {
     AggregateType = aggregateType;
     Argument = argument;
     Distinct = distinct;
 }
        internal AggregateNode(DataTable table, FunctionId aggregateType, string columnName, bool local, string relationName) : base(table) {
            Debug.Assert(columnName != null, "Invalid parameter column name (null).");
            this.aggregate = (Aggregate)(int)aggregateType;

            if (aggregateType == FunctionId.Sum)
                this.type = AggregateType.Sum;
            else if (aggregateType == FunctionId.Avg)
                this.type = AggregateType.Mean;
            else if (aggregateType == FunctionId.Min)
                this.type = AggregateType.Min;
            else if (aggregateType == FunctionId.Max)
                this.type = AggregateType.Max;
            else if (aggregateType == FunctionId.Count)
                this.type = AggregateType.Count;
            else if (aggregateType == FunctionId.Var)
                this.type = AggregateType.Var;
            else if (aggregateType == FunctionId.StDev)
                this.type = AggregateType.StDev;
            else {
                throw ExprException.UndefinedFunction(Function.FunctionName[(Int32)aggregateType]);
            }

            this.local = local;
            this.relationName = relationName;
            this.columnName = columnName;
        }
Beispiel #3
0
        public override object Aggregate(int[] records, AggregateType kind)
        {
            try
            {
                switch (kind)
                {
                    case AggregateType.First:
                        if (records.Length > 0)
                        {
                            return _values[records[0]];
                        }
                        return null;// no data => null

                    case AggregateType.Count:
                        int count = 0;
                        for (int i = 0; i < records.Length; i++)
                        {
                            if (!IsNull(records[i]))
                                count++;
                        }
                        return count;
                }
            }
            catch (OverflowException)
            {
                throw ExprException.Overflow(typeof(SqlBytes));
            }
            throw ExceptionBuilder.AggregateException(kind, _dataType);
        }
        internal override AttributeDefinition CreateAggregateAttributeDefinition(AggregateType aggregateType)
        {
            if (aggregateType == AggregateType.Count)
                return new AggregateCountAttributeDefinition(this);

            return base.CreateAggregateAttributeDefinition(aggregateType);
        }
Beispiel #5
0
 private SqlExpressionParser(int indentLevel, SqlExpressionParser outerStatement,
                            AggregateType aggregateType)
 {
     this.indentLevel = indentLevel;
     this.outerStatement = outerStatement;
     this.aggregateType = aggregateType;
 }
        override public Object Aggregate(int[] recordNos, AggregateType kind) {
            try {
            	int i;
                switch (kind) {
                case AggregateType.Min:
                    int min = -1;
                    for (i = 0; i < recordNos.Length; i++) {
                        if (IsNull(recordNos[i]))
                            continue;
                        min = recordNos[i];
                        break;
                    }
                    if (min >= 0) {
                        for (i = i+1; i < recordNos.Length; i++) {
                            if (IsNull(recordNos[i]))
                                continue;
                            if (Compare(min, recordNos[i]) > 0) {
                                min = recordNos[i];
                            }
                        }
                        return Get(min);
                    }
                    return NullValue;


                case AggregateType.Max:
                    int max = -1;
                    for (i = 0; i < recordNos.Length; i++) {
                        if (IsNull(recordNos[i]))
                            continue;
                        max = recordNos[i];
                        break;
                    }
                    if (max >= 0) {
                        for (i = i+1; i < recordNos.Length; i++) {
                            if (Compare(max, recordNos[i]) < 0) {
                                max = recordNos[i];
                            }
                        }
                        return Get(max);
                    }
                    return NullValue;

                case AggregateType.Count:
                    int count = 0;
                    for (i = 0; i < recordNos.Length; i++) {
                        if (!IsNull(recordNos[i]))
                            count++;
                    }
                    return count;

                }
            }
            catch (OverflowException) {
                throw ExprException.Overflow(typeof(SqlString));
            }
            throw ExceptionBuilder.AggregateException(kind, DataType);
        }
Beispiel #7
0
        public override object Aggregate(int[] records, AggregateType kind)
        {
            bool hasData = false;
            try
            {
                switch (kind)
                {
                    case AggregateType.Min:
                        char min = char.MaxValue;
                        for (int i = 0; i < records.Length; i++)
                        {
                            int record = records[i];
                            if (IsNull(record))
                                continue;
                            min = (_values[record] < min) ? _values[record] : min;
                            hasData = true;
                        }
                        if (hasData)
                        {
                            return min;
                        }
                        return _nullValue;

                    case AggregateType.Max:
                        char max = char.MinValue;
                        for (int i = 0; i < records.Length; i++)
                        {
                            int record = records[i];
                            if (IsNull(record))
                                continue;
                            max = (_values[record] > max) ? _values[record] : max;
                            hasData = true;
                        }
                        if (hasData)
                        {
                            return max;
                        }
                        return _nullValue;

                    case AggregateType.First:
                        if (records.Length > 0)
                        {
                            return _values[records[0]];
                        }
                        return null;

                    case AggregateType.Count:
                        return base.Aggregate(records, kind);
                }
            }
            catch (OverflowException)
            {
                throw ExprException.Overflow(typeof(char));
            }
            throw ExceptionBuilder.AggregateException(kind, _dataType);
        }
Beispiel #8
0
        /// <summary>
        /// 创建 <see cref="AggregateKey"/> 实例
        /// </summary>
        /// <param name="nameSpace">表示Metric命名空间</param>
        /// <param name="name">表示Metric名称</param>
        /// <param name="tags">表示Metric标签</param>
        public AggregateKey(string nameSpace, string name, IDictionary<String, String> tags, AggregateType aggType)
        {
            if (nameSpace == null) throw new ArgumentNullException("nameSpace");
            if (name == null) throw new ArgumentNullException("name");

            this.fullName = NameHelper.GetFullName(nameSpace, name);
            this.aggType = aggType;
            this.keyStr = GenerateKeyString(nameSpace, name, tags);
            this.hashCode = keyStr.GetHashCode();
        }
        override public Object Aggregate(int[] records, AggregateType kind) {
            bool hasData = false;
            try {
                switch (kind) {
                    case AggregateType.Min:
                        SqlDateTime min = SqlDateTime.MaxValue;
                        for (int i = 0; i < records.Length; i++) {
                            int record = records[i];
                            if (IsNull(record))
                                continue;
                            if ((SqlDateTime.LessThan(values[record], min)).IsTrue)
                                min = values[record];         
                            hasData = true;
                        }
                        if (hasData) {
                            return min;
                        }
                        return NullValue;

                    case AggregateType.Max:
                        SqlDateTime max = SqlDateTime.MinValue;
                        for (int i = 0; i < records.Length; i++) {
                            int record = records[i];
                            if (IsNull(record))
                                continue;
                            if ((SqlDateTime.GreaterThan(values[record], max)).IsTrue)
                                max = values[record];
                            hasData = true;
                        }
                        if (hasData) {
                            return max;
                        }
                        return NullValue;

                    case AggregateType.First:
                        if (records.Length > 0) {
                            return values[records[0]];
                        }
                        return null;// no data => null

                    case AggregateType.Count:
                        int count = 0;
                        for (int i = 0; i < records.Length; i++) {
                            if (!IsNull(records[i]))
                                count++;
                        }
                        return count;
                }
            }
            catch (OverflowException) {
                throw ExprException.Overflow(typeof(SqlDateTime));
            }
            throw ExceptionBuilder.AggregateException(kind, DataType);
        }
Beispiel #10
0
        protected AggregateColumn(AggregateType aggregate, FieldColumn column, string alias = null)
        {
            if (column == null)
            {
                throw new ArgumentNullException("column");
            }

            Type = aggregate;
            Column = column;
            Alias = alias;
        }
 protected XrmAggregate(string recordTypeWithAggregate, string aggregateField, string recordTypeAggregated,
     string aggregatedField, AggregateType aggregateType)
 {
     RecordTypeWithAggregate = recordTypeWithAggregate;
     AggregateField = aggregateField;
     RecordTypeAggregated = recordTypeAggregated;
     AggregateType = aggregateType;
     AggregatedField = aggregatedField;
     Filters = new ConditionExpression[] { };
     AddFilter("statecode", XrmPicklists.State.Active);
     LinkEntity = null;
 }
        override public Object Aggregate(int[] records, AggregateType kind) {
            bool hasData = false;
            try {
                switch (kind) {
                    case AggregateType.Min:
                        DateTimeOffset min = DateTimeOffset.MaxValue;
                        for (int i = 0; i < records.Length; i++) {
                            int record = records[i];
                            if (HasValue(record)) {
                                min=(DateTimeOffset.Compare(values[record],min) < 0) ? values[record] : min;
                                hasData = true;
                            }
                        }
                        if (hasData) {
                            return min;
                        }
                        return NullValue;

                    case AggregateType.Max:
                        DateTimeOffset max = DateTimeOffset.MinValue;
                        for (int i = 0; i < records.Length; i++) {
                            int record = records[i];
                            if (HasValue(record)) {
                                max=(DateTimeOffset.Compare(values[record],max) >= 0) ? values[record] : max;
                                hasData = true;
                            }
                        }
                        if (hasData) {
                            return max;
                        }
                        return NullValue;

                    case AggregateType.First:
                        if (records.Length > 0) {
                            return values[records[0]];
                        }
                        return null;

                    case AggregateType.Count:
                        int count = 0;
                        for (int i = 0; i < records.Length; i++) {
                            if (HasValue(records[i])) {
                                count++;
                            }
                        }
                        return count;
                }
            }
            catch (OverflowException) {
                throw ExprException.Overflow(typeof(DateTimeOffset));
            }
            throw ExceptionBuilder.AggregateException(kind, DataType);
        }
        override public Object Aggregate(int[] records, AggregateType kind) {
            bool hasData = false;
            try {
                switch (kind) {
                    case AggregateType.Min:
                        SqlBoolean min = true;
                        for (int i = 0; i < records.Length; i++) {
                            int record = records[i];
                            if (IsNull(record))
                                continue;
                            min= SqlBoolean.And(values[record], min);
                            hasData = true;
                        }
                        if (hasData) {
                            return min;
                        }
                        return NullValue;

                    case AggregateType.Max:
                        SqlBoolean max = false;
                        for (int i = 0; i < records.Length; i++) {
                            int record = records[i];
                            if (IsNull(record))
                                continue;
                            max= SqlBoolean.Or(values[record], max);
                            hasData = true;
                        }
                        if (hasData) {
                            return max;
                        }
                        return NullValue;

                    case AggregateType.First:
                        if (records.Length > 0) {
                            return values[records[0]];
                        }
                        return NullValue;

                    case AggregateType.Count:
                        int count = 0;
                        for (int i = 0; i < records.Length; i++) {
                            if (!IsNull(records[i]))
                                count++;
                        }
                        return count;
                }
            }
            catch (OverflowException) {
                throw ExprException.Overflow(typeof(SqlBoolean));
            }
            throw ExceptionBuilder.AggregateException(kind, DataType);
        }
 public CrmAttributeXml(
     string name = "",
     string alias = "",
     AggregateType aggregate = AggregateType.None,
     bool groupBy = false,
     DateGroupingType dateGrouping = DateGroupingType.None
     )
 {
     this.Name = name;
     this.Alias = alias;
     this.Aggregate = aggregate;
     this.GroupBy = groupBy;
     this.DateGrouping = dateGrouping;
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="Grouping"/> class.
        /// </summary>
        /// <param name="groupOn">The property to group on.</param>
        /// <param name="sortDirection">The sort direction.</param>
        /// <param name="aggregateOn">The property to aggregate on.</param>
        /// <param name="aggregateType">The type of aggregate to calculate.</param>
        public Grouping(
            PropertyDefinitionBase groupOn,
            SortDirection sortDirection,
            PropertyDefinitionBase aggregateOn,
            AggregateType aggregateType)
            : this()
        {
            EwsUtilities.ValidateParam(groupOn, "groupOn");
            EwsUtilities.ValidateParam(aggregateOn, "aggregateOn");

            this.groupOn = groupOn;
            this.sortDirection = sortDirection;
            this.aggregateOn = aggregateOn;
            this.aggregateType = aggregateType;
        }
        override public Object Aggregate(int[] records, AggregateType kind) {
            bool hasData = false;
            try {
                switch (kind) {
                    case AggregateType.Min:
                        Boolean min = true;
                        for (int i = 0; i < records.Length; i++) {
                            int record = records[i];
                            if (IsNull(record))
                                continue;
                            min=values[record] && min;
                            hasData = true;
                        }
                        if (hasData) {
                            return min;
                        }
                        return NullValue;

                    case AggregateType.Max:
                        Boolean max = false;
                        for (int i = 0; i < records.Length; i++) {
                            int record = records[i];
                            if (IsNull(record))
                                continue;
                            max=values[record] || max;
                            hasData = true;
                        }
                        if (hasData) {
                            return max;
                        }
                        return NullValue;

                    case AggregateType.First:
                        if (records.Length > 0) {
                            return values[records[0]];
                        }
                        return null;

                    case AggregateType.Count:
                        return base.Aggregate(records, kind);

                }
            }
            catch (OverflowException) {
                throw ExprException.Overflow(typeof(Boolean));
            }
            throw ExceptionBuilder.AggregateException(kind, DataType);
        }
Beispiel #17
0
        /// <summary>
        /// Returns the checksum of the values in a group. Null values are ignored. Can be followed by the OVER clause.
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="aggType"></param>
        /// <param name="expressionAlias"></param>
        /// <returns></returns>
        public static string ChecksumAgg(string expression, AggregateType aggType = AggregateType.Default, string expressionAlias = null)
        {
            StringBuilder builder = new StringBuilder(expression.Length + 24);

            builder.Append("CHECKSUM_AGG(");

            AppendAggregateType(builder, aggType);

            builder.Append(InternalConstants.CharSpace);
            builder.Append(expression);
            builder.Append(")");

            Aliases.AppendAlias(builder, expressionAlias);

            return builder.ToString();
        }
        public override object Aggregate(int[] records, AggregateType kind)
        {
            try
            {
                int num;
                int num2;
                switch (kind)
                {
                    case AggregateType.First:
                        if (records.Length <= 0)
                        {
                            return null;
                        }
                        return this.values[records[0]];

                    case AggregateType.Count:
                        num2 = 0;
                        num = 0;
                        goto Label_0044;

                    default:
                        goto Label_0068;
                }
            Label_0031:
                if (!this.IsNull(records[num]))
                {
                    num2++;
                }
                num++;
            Label_0044:
                if (num < records.Length)
                {
                    goto Label_0031;
                }
                return num2;
            }
            catch (OverflowException)
            {
                throw ExprException.Overflow(typeof(SqlXml));
            }
        Label_0068:
            throw ExceptionBuilder.AggregateException(kind, base.DataType);
        }
 public BaseUnionIntersectionCommand(string key, IEnumerable<String> keys,
     IEnumerable<Double> weights, AggregateType aggregateType) 
     : base(key)
 {
     if (keys == null)
         throw new ArgumentNullException("keys");
     _keys = new List<String>(keys);
     if (_keys.Count < 2)
         throw new CommandArgumentException("keys", "At least 2 keys must be specified");
     if (weights != null)
     {
         _weights = new List<Double>(_weights);
         if (_weights.Count != _keys.Count)
         {
             throw new CommandArgumentException("weights",
                 "The number of weights must match the number of keys.");
         }
     }
     _aggregate = aggregateType;
 }
 public static string GetValue(AggregateType aggregateType)
 {
     switch (aggregateType)
     {
         case AggregateType.Count:
             return "count";
         case AggregateType.CountColumn:
             return "countcolumn";
         case AggregateType.Sum:
             return "sum";
         case AggregateType.Avg:
             return "avg";
         case AggregateType.Min:
             return "min";
         case AggregateType.Max:
             return "max";
         default:
             return string.Empty;
     }
 }
        internal override AttributeDefinition CreateAggregateAttributeDefinition(AggregateType aggregateType)
        {
            if (IsMultiValue)
            {
                if (aggregateType == AggregateType.Count)
                    return new AggregateCountAttributeDefinition(this);

                if (IsNumeric)
                {
                    if (aggregateType == AggregateType.Sum)
                        return new AggregateSumAttributeDefinition(this);
                    else if (aggregateType == AggregateType.Max)
                        return new AggregateMaxAttributeDefinition(this);
                    else if (aggregateType == AggregateType.Min)
                        return new AggregateMinAttributeDefinition(this);
                }
            }

            return base.CreateAggregateAttributeDefinition(aggregateType);
        }
Beispiel #22
0
        /// <summary>
        /// Returns the number of items in a group.
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="aggType"></param>
        /// <param name="overExpression"></param>
        /// <param name="expressionAlias"></param>
        /// <returns></returns>
        public static string Count(string expression, AggregateType aggType = AggregateType.Default, string overExpression = null, string expressionAlias = null)
        {
            StringBuilder builder = new StringBuilder(expression.Length + (overExpression ?? string.Empty).Length + 24);

            builder.Append("COUNT(");

            AppendAggregateType(builder, aggType);

            builder.Append(InternalConstants.CharSpace);
            builder.Append(expression);
            builder.Append(InternalConstants.CharSpace);

            if (overExpression != null) {
                builder.Append(") OVER");
                builder.Append(overExpression);
            }

            builder.Append(')');

            Aliases.AppendAlias(builder, expressionAlias);

            return builder.ToString();
        }
 public override string ToString()
 {
     return($"{AggregateType.PrettyPrint()} v{AggregateSequenceNumber}/{EventType.PrettyPrint()}:{AggregateIdentity}");
 }
Beispiel #24
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BaseStatAttribute"/> class.
 /// </summary>
 /// <param name="definition">The definition.</param>
 /// <param name="aggregateType">Type of the aggregate.</param>
 protected BaseStatAttribute(AttributeDefinition definition, AggregateType aggregateType)
     : base(definition, aggregateType)
 {
 }
Beispiel #25
0
        public override object Aggregate(int[] records, AggregateType kind)
        {
            bool hasData = false;

            try
            {
                switch (kind)
                {
                case AggregateType.Sum:
                    decimal sum = DefaultValue;
                    foreach (int record in records)
                    {
                        if (HasValue(record))
                        {
                            checked { sum += _values[record]; }
                            hasData = true;
                        }
                    }
                    if (hasData)
                    {
                        return(sum);
                    }
                    return(_nullValue);

                case AggregateType.Mean:
                    decimal meanSum   = DefaultValue;
                    int     meanCount = 0;
                    foreach (int record in records)
                    {
                        if (HasValue(record))
                        {
                            checked { meanSum += _values[record]; }
                            meanCount++;
                            hasData = true;
                        }
                    }
                    if (hasData)
                    {
                        decimal mean;
                        checked { mean = (meanSum / meanCount); }
                        return(mean);
                    }
                    return(_nullValue);

                case AggregateType.Var:
                case AggregateType.StDev:
                    int    count  = 0;
                    double var    = (double)DefaultValue;
                    double prec   = (double)DefaultValue;
                    double dsum   = (double)DefaultValue;
                    double sqrsum = (double)DefaultValue;

                    foreach (int record in records)
                    {
                        if (HasValue(record))
                        {
                            dsum   += (double)_values[record];
                            sqrsum += (double)_values[record] * (double)_values[record];
                            count++;
                        }
                    }

                    if (count > 1)
                    {
                        var  = count * sqrsum - (dsum * dsum);
                        prec = var / (dsum * dsum);

                        // we are dealing with the risk of a cancellation error
                        // double is guaranteed only for 15 digits so a difference
                        // with a result less than 1e-15 should be considered as zero

                        if ((prec < 1e-15) || (var < 0))
                        {
                            var = 0;
                        }
                        else
                        {
                            var = var / (count * (count - 1));
                        }

                        if (kind == AggregateType.StDev)
                        {
                            return(Math.Sqrt(var));
                        }
                        return(var);
                    }
                    return(_nullValue);

                case AggregateType.Min:
                    decimal min = decimal.MaxValue;
                    for (int i = 0; i < records.Length; i++)
                    {
                        int record = records[i];
                        if (HasValue(record))
                        {
                            min     = Math.Min(_values[record], min);
                            hasData = true;
                        }
                    }
                    if (hasData)
                    {
                        return(min);
                    }
                    return(_nullValue);

                case AggregateType.Max:
                    decimal max = decimal.MinValue;
                    for (int i = 0; i < records.Length; i++)
                    {
                        int record = records[i];
                        if (HasValue(record))
                        {
                            max     = Math.Max(_values[record], max);
                            hasData = true;
                        }
                    }
                    if (hasData)
                    {
                        return(max);
                    }
                    return(_nullValue);

                case AggregateType.First:     // Does not seem to be implemented
                    if (records.Length > 0)
                    {
                        return(_values[records[0]]);
                    }
                    return(null !);

                case AggregateType.Count:
                    return(base.Aggregate(records, kind));
                }
            }
            catch (OverflowException)
            {
                throw ExprException.Overflow(typeof(decimal));
            }
            throw ExceptionBuilder.AggregateException(kind, _dataType);
        }
        public void ErrAppendType(CType pType, SubstContext pctx, bool fArgs)
        {
            if (pctx != null)
            {
                if (!pctx.FNop())
                {
                    pType = GetTypeManager().SubstType(pType, pctx);
                }
                // We shouldn't use the SubstContext again so set it to NULL.
                pctx = null;
            }

            switch (pType.GetTypeKind())
            {
            case TypeKind.TK_AggregateType:
            {
                AggregateType pAggType = pType.AsAggregateType();

                // Check for a predefined class with a special "nice" name for
                // error reported.
                string text = PredefinedTypes.GetNiceName(pAggType.getAggregate());
                if (text != null)
                {
                    // Found a nice name.
                    ErrAppendString(text);
                }
                else if (pAggType.getAggregate().IsAnonymousType())
                {
                    ErrAppendPrintf("AnonymousType#{0}", GetTypeID(pAggType));
                    break;
                }
                else
                {
                    if (pAggType.outerType != null)
                    {
                        ErrAppendType(pAggType.outerType, pctx);
                        ErrAppendChar('.');
                    }
                    else
                    {
                        // In a namespace.
                        ErrAppendParentSym(pAggType.getAggregate(), pctx);
                    }
                    ErrAppendName(pAggType.getAggregate().name);
                }
                ErrAppendTypeParameters(pAggType.GetTypeArgsThis(), pctx, true);
                break;
            }

            case TypeKind.TK_TypeParameterType:
                if (null == pType.GetName())
                {
                    // It's a standard type variable.
                    if (pType.AsTypeParameterType().IsMethodTypeParameter())
                    {
                        ErrAppendChar('!');
                    }
                    ErrAppendChar('!');
                    ErrAppendPrintf("{0}", pType.AsTypeParameterType().GetIndexInTotalParameters());
                }
                else
                {
                    ErrAppendName(pType.GetName());
                }
                break;

            case TypeKind.TK_ErrorType:
                if (pType.AsErrorType().HasParent())
                {
                    Debug.Assert(pType.AsErrorType().nameText != null && pType.AsErrorType().typeArgs != null);
                    ErrAppendParentType(pType, pctx);
                    ErrAppendName(pType.AsErrorType().nameText);
                    ErrAppendTypeParameters(pType.AsErrorType().typeArgs, pctx, true);
                }
                else
                {
                    // Load the string "<error>".
                    Debug.Assert(null == pType.AsErrorType().typeArgs);
                    ErrAppendId(MessageID.ERRORSYM);
                }
                break;

            case TypeKind.TK_NullType:
                // Load the string "<null>".
                ErrAppendId(MessageID.NULL);
                break;

            case TypeKind.TK_OpenTypePlaceholderType:
                // Leave blank.
                break;

            case TypeKind.TK_BoundLambdaType:
                ErrAppendId(MessageID.AnonMethod);
                break;

            case TypeKind.TK_UnboundLambdaType:
                ErrAppendId(MessageID.Lambda);
                break;

            case TypeKind.TK_MethodGroupType:
                ErrAppendId(MessageID.MethodGroup);
                break;

            case TypeKind.TK_ArgumentListType:
                ErrAppendString(TokenFacts.GetText(TokenKind.ArgList));
                break;

            case TypeKind.TK_ArrayType:
            {
                CType elementType = pType.AsArrayType().GetBaseElementType();
                int   rank;

                if (null == elementType)
                {
                    Debug.Assert(false, "No element type");
                    break;
                }

                ErrAppendType(elementType, pctx);

                for (elementType = pType;
                     elementType != null && elementType.IsArrayType();
                     elementType = elementType.AsArrayType().GetElementType())
                {
                    rank = elementType.AsArrayType().rank;

                    // Add [] with (rank-1) commas inside
                    ErrAppendChar('[');

#if !CSEE
                    // known rank.
                    if (rank > 1)
                    {
                        ErrAppendChar('*');
                    }
#endif

                    for (int i = rank; i > 1; --i)
                    {
                        ErrAppendChar(',');
#if !CSEE
                        ErrAppendChar('*');
#endif
                    }

                    ErrAppendChar(']');
                }
                break;
            }

            case TypeKind.TK_VoidType:
                ErrAppendName(GetNameManager().Lookup(TokenFacts.GetText(TokenKind.Void)));
                break;

            case TypeKind.TK_ParameterModifierType:
                // add ref or out
                ErrAppendString(pType.AsParameterModifierType().isOut ? "out " : "ref ");

                // add base type name
                ErrAppendType(pType.AsParameterModifierType().GetParameterType(), pctx);
                break;

            case TypeKind.TK_PointerType:
                // Generate the base type.
                ErrAppendType(pType.AsPointerType().GetReferentType(), pctx);
                {
                    // add the trailing *
                    ErrAppendChar('*');
                }
                break;

            case TypeKind.TK_NullableType:
                ErrAppendType(pType.AsNullableType().GetUnderlyingType(), pctx);
                ErrAppendChar('?');
                break;

            case TypeKind.TK_NaturalIntegerType:
            default:
                // Shouldn't happen.
                Debug.Assert(false, "Bad type kind");
                break;
            }
        }
Beispiel #27
0
        /// <include file='doc\Int16Storage.uex' path='docs/doc[@for="Int16Storage.Aggregate"]/*' />
        /// <internalonly/>
        override public Object Aggregate(int[] records, AggregateType kind)
        {
            bool hasData = false;

            try {
                switch (kind)
                {
                case AggregateType.Sum:
                    Int64 sum = defaultValue;
                    foreach (int record in records)
                    {
                        if (IsNull(record))
                        {
                            continue;
                        }
                        checked { sum += values[record]; }
                        hasData = true;
                    }
                    if (hasData)
                    {
                        return(sum);
                    }
                    return(DBNull.Value);

                case AggregateType.Mean:
                    Int64 meanSum   = (Int64)defaultValue;
                    int   meanCount = 0;
                    foreach (int record in records)
                    {
                        if (IsNull(record))
                        {
                            continue;
                        }
                        checked { meanSum += (Int64)values[record]; }
                        meanCount++;
                        hasData = true;
                    }
                    if (hasData)
                    {
                        Int16 mean;
                        checked { mean = (Int16)(meanSum / meanCount); }
                        return(mean);
                    }
                    return(DBNull.Value);

                case AggregateType.Var:
                case AggregateType.StDev:
                    int    count  = 0;
                    double var    = (double)defaultValue;
                    double prec   = (double)defaultValue;
                    double dsum   = (double)defaultValue;
                    double sqrsum = (double)defaultValue;

                    foreach (int record in records)
                    {
                        if (IsNull(record))
                        {
                            continue;
                        }
                        dsum   += (double)values[record];
                        sqrsum += (double)values[record] * (double)values[record];
                        count++;
                    }

                    if (count > 1)
                    {
                        var  = ((double)count * sqrsum - (dsum * dsum));
                        prec = var / (dsum * dsum);

                        // we are dealing with the risk of a cancellation error
                        // double is guaranteed only for 15 digits so a difference
                        // with a result less than 1e-15 should be considered as zero

                        if ((prec < 1e-15) || (var < 0))
                        {
                            var = 0;
                        }
                        else
                        {
                            var = var / (count * (count - 1));
                        }

                        if (kind == AggregateType.StDev)
                        {
                            return(Math.Sqrt(var));
                        }
                        return(var);
                    }
                    return(DBNull.Value);


                case AggregateType.Min:
                    Int16 min = Int16.MaxValue;
                    for (int i = 0; i < records.Length; i++)
                    {
                        int record = records[i];
                        if (IsNull(record))
                        {
                            continue;
                        }
                        min     = Math.Min(values[record], min);
                        hasData = true;
                    }
                    if (hasData)
                    {
                        return(min);
                    }
                    return(DBNull.Value);

                case AggregateType.Max:
                    Int16 max = Int16.MinValue;
                    for (int i = 0; i < records.Length; i++)
                    {
                        int record = records[i];
                        if (IsNull(record))
                        {
                            continue;
                        }
                        max     = Math.Max(values[record], max);
                        hasData = true;
                    }
                    if (hasData)
                    {
                        return(max);
                    }
                    return(DBNull.Value);

                case AggregateType.First:
                    if (records.Length > 0)
                    {
                        return(values[records[0]]);
                    }
                    return(null);

                case AggregateType.Count:
                    count = 0;
                    for (int i = 0; i < records.Length; i++)
                    {
                        if (!IsNull(records[i]))
                        {
                            count++;
                        }
                    }
                    return(count);
                }
            }
            catch (OverflowException) {
                throw ExprException.Overflow(typeof(Int16));
            }
            throw ExceptionBuilder.AggregateException(kind.ToString(), DataType);
        }
 public RootCauseLocalizationInput(DateTime anomalyTimestamp, Dictionary <string, Object> anomalyDimension, List <MetricSlice> slices, AggregateType aggregateType, Object aggregateSymbol)
 {
     AnomalyTimestamp = anomalyTimestamp;
     AnomalyDimension = anomalyDimension;
     Slices           = slices;
     AggType          = aggregateType;
     AggSymbol        = aggregateSymbol;
 }
        override public Object Aggregate(int[] records, AggregateType kind)
        {
            bool hasData = false;

            try {
                switch (kind)
                {
                case AggregateType.Min:
                    DateTimeOffset min = DateTimeOffset.MaxValue;
                    for (int i = 0; i < records.Length; i++)
                    {
                        int record = records[i];
                        if (HasValue(record))
                        {
                            min     = (DateTimeOffset.Compare(values[record], min) < 0) ? values[record] : min;
                            hasData = true;
                        }
                    }
                    if (hasData)
                    {
                        return(min);
                    }
                    return(NullValue);

                case AggregateType.Max:
                    DateTimeOffset max = DateTimeOffset.MinValue;
                    for (int i = 0; i < records.Length; i++)
                    {
                        int record = records[i];
                        if (HasValue(record))
                        {
                            max     = (DateTimeOffset.Compare(values[record], max) >= 0) ? values[record] : max;
                            hasData = true;
                        }
                    }
                    if (hasData)
                    {
                        return(max);
                    }
                    return(NullValue);

                case AggregateType.First:
                    if (records.Length > 0)
                    {
                        return(values[records[0]]);
                    }
                    return(null);

                case AggregateType.Count:
                    int count = 0;
                    for (int i = 0; i < records.Length; i++)
                    {
                        if (HasValue(records[i]))
                        {
                            count++;
                        }
                    }
                    return(count);
                }
            }
            catch (OverflowException) {
                throw ExprException.Overflow(typeof(DateTimeOffset));
            }
            throw ExceptionBuilder.AggregateException(kind, DataType);
        }
Beispiel #30
0
        public int ZInterStore(string destination, string[] keys, double[] weights = null, AggregateType aggregateType = AggregateType.Sum)
        {
            var sets = keys.Select(k => GetSortedSet(k) ?? new SortedSet <ZSetEntry>()).ToArray();

            return(sets.Any(s => s.Count == 0) ? 0 : ZSetOperationAndStoreImpl(destination, sets, weights, aggregateType, SetOperation.Intersection));
        }
Beispiel #31
0
 private bool HasPredicateArg(AggregateType aggregateType)
 {
     return(aggregateType == AggregateType.Count);
 }
Beispiel #32
0
        private Expression BindAggregate(Expression source, MethodInfo method, LambdaExpression argument, bool isRoot)
        {
            Type          returnType           = method.ReturnType;
            AggregateType aggType              = GetAggregateType(method.Name);
            bool          hasPredicateArg      = HasPredicateArg(aggType);
            bool          isDistinct           = false;
            bool          argumentWasPredicate = false;
            bool          useAlternateArg      = false;

            // check for distinct
            MethodCallExpression mcs = source as MethodCallExpression;

            if (mcs != null && !hasPredicateArg && argument == null)
            {
                if (mcs.Method.Name == "Distinct" && mcs.Arguments.Count == 1 &&
                    (mcs.Method.DeclaringType == typeof(Queryable) || mcs.Method.DeclaringType == typeof(Enumerable)))
                {
                    source     = mcs.Arguments[0];
                    isDistinct = true;
                }
            }

            if (argument != null && hasPredicateArg)
            {
                // convert query.Count(predicate) into query.Where(predicate).Count()
                source               = Expression.Call(typeof(Queryable), "Where", method.GetGenericArguments(), source, argument);
                argument             = null;
                argumentWasPredicate = true;
            }

            ProjectionExpression projection = VisitSequence(source);

            Expression argExpr = null;

            if (argument != null)
            {
                map[argument.Parameters[0]] = projection.Projector;
                argExpr = Visit(argument.Body);
            }
            else if (!hasPredicateArg || useAlternateArg)
            {
                argExpr = projection.Projector;
            }

            var              alias   = GetNextAlias();
            var              pc      = ProjectColumns(projection.Projector, alias, projection.Source.Alias);
            Expression       aggExpr = new AggregateExpression(returnType, aggType, argExpr, isDistinct);
            SelectExpression select  = new SelectExpression(alias, new[] { new ColumnDeclaration("", aggExpr) },
                                                            projection.Source, null);

            if (isRoot)
            {
                ParameterExpression p     = Expression.Parameter(typeof(IEnumerable <>).MakeGenericType(aggExpr.Type), "p");
                LambdaExpression    gator =
                    Expression.Lambda(Expression.Call(typeof(Enumerable), "Single", new[] { returnType }, p), p);
                return(new ProjectionExpression(select, new ColumnExpression(returnType, alias, ""), gator));
            }

            ScalarExpression subquery = new ScalarExpression(returnType, select);

            // if we can find the corresponding group-info we can build a special AggregateSubquery node that will enable us to
            // optimize the aggregate expression later using AggregateRewriter
            GroupByInfo info;

            if (!argumentWasPredicate && groupByMap.TryGetValue(projection, out info))
            {
                // use the element expression from the group-by info to rebind the argument so the resulting expression is one that
                // would be legal to add to the columns in the select expression that has the corresponding group-by clause.
                if (argument != null)
                {
                    map[argument.Parameters[0]] = info.Element;
                    argExpr = Visit(argument.Body);
                }
                else if (!hasPredicateArg || useAlternateArg)
                {
                    argExpr = info.Element;
                }
                aggExpr = new AggregateExpression(returnType, aggType, argExpr, isDistinct);

                // check for easy to optimize case.  If the projection that our aggregate is based on is really the 'group' argument from
                // the query.GroupBy(xxx, (key, group) => yyy) method then whatever expression we return here will automatically
                // become part of the select expression that has the group-by clause, so just return the simple aggregate expression.
                if (projection == currentGroupElement)
                {
                    return(aggExpr);
                }

                return(new AggregateSubqueryExpression(info.Alias, aggExpr, subquery));
            }

            return(subquery);
        }
Beispiel #33
0
 private CollectionView(AggregateType type)
 {
     AggregateType = type;
 }
Beispiel #34
0
 public AggregateData(int column, AggregateType aggregation = AggregateType.All)
 {
     Column      = column;
     Aggregation = aggregation;
 }
        public override object Aggregate(int[] records, AggregateType kind)
        {
            bool hasData = false;

            try
            {
                switch (kind)
                {
                case AggregateType.Min:
                    SqlDateTime min = SqlDateTime.MaxValue;
                    for (int i = 0; i < records.Length; i++)
                    {
                        int record = records[i];
                        if (IsNull(record))
                        {
                            continue;
                        }
                        if ((SqlDateTime.LessThan(_values[record], min)).IsTrue)
                        {
                            min = _values[record];
                        }
                        hasData = true;
                    }
                    if (hasData)
                    {
                        return(min);
                    }
                    return(_nullValue);

                case AggregateType.Max:
                    SqlDateTime max = SqlDateTime.MinValue;
                    for (int i = 0; i < records.Length; i++)
                    {
                        int record = records[i];
                        if (IsNull(record))
                        {
                            continue;
                        }
                        if ((SqlDateTime.GreaterThan(_values[record], max)).IsTrue)
                        {
                            max = _values[record];
                        }
                        hasData = true;
                    }
                    if (hasData)
                    {
                        return(max);
                    }
                    return(_nullValue);

                case AggregateType.First:
                    if (records.Length > 0)
                    {
                        return(_values[records[0]]);
                    }
                    return(null);    // no data => null

                case AggregateType.Count:
                    int count = 0;
                    for (int i = 0; i < records.Length; i++)
                    {
                        if (!IsNull(records[i]))
                        {
                            count++;
                        }
                    }
                    return(count);
                }
            }
            catch (OverflowException)
            {
                throw ExprException.Overflow(typeof(SqlDateTime));
            }
            throw ExceptionBuilder.AggregateException(kind, _dataType);
        }
Beispiel #36
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BaseAttribute"/> class.
 /// </summary>
 /// <param name="definition">The definition.</param>
 /// <param name="aggregateType">Type of the aggregate.</param>
 protected BaseAttribute(AttributeDefinition definition, AggregateType aggregateType)
 {
     this.Definition    = definition;
     this.AggregateType = aggregateType;
 }
 public override object Aggregate(int[] records, AggregateType kind)
 {
     throw ExceptionBuilder.AggregateException(kind, _dataType);
 }
Beispiel #38
0
        public CollectionView(IEnumerable <object> dataSource, string groupBy, string aggregateOn, AggregateType aggregateType = AggregateType.Sum, string name = null)
        {
            this.SourceCollection = dataSource;
            this.Name             = name;
            this._aggregateOn     = aggregateOn;
            var views = dataSource.GroupBy(p => GetProperty(p, groupBy)).Select(groupItem => new CollectionView(aggregateType)
            {
                Parent           = this,
                Name             = groupItem.Key.ToString(),
                Value            = groupItem.Aggregate(k => (double)GetProperty(k, aggregateOn), aggregateType),
                SourceCollection = groupItem,
            });

            this.Views = views.ToArray();
        }
        override public Object Aggregate(int[] records, AggregateType kind)
        {
            bool hasData = false;

            try {
                switch (kind)
                {
                case AggregateType.Min:
                    TimeSpan min = TimeSpan.MaxValue;
                    for (int i = 0; i < records.Length; i++)
                    {
                        int record = records[i];
                        if (IsNull(record))
                        {
                            continue;
                        }
                        min     = (TimeSpan.Compare(values[record], min) < 0) ? values[record] : min;
                        hasData = true;
                    }
                    if (hasData)
                    {
                        return(min);
                    }
                    return(NullValue);

                case AggregateType.Max:
                    TimeSpan max = TimeSpan.MinValue;
                    for (int i = 0; i < records.Length; i++)
                    {
                        int record = records[i];
                        if (IsNull(record))
                        {
                            continue;
                        }
                        max     = (TimeSpan.Compare(values[record], max) >= 0) ? values[record] : max;
                        hasData = true;
                    }
                    if (hasData)
                    {
                        return(max);
                    }
                    return(NullValue);

                case AggregateType.First:
                    if (records.Length > 0)
                    {
                        return(values[records[0]]);
                    }
                    return(null);

                case AggregateType.Count:
                    return(base.Aggregate(records, kind));

                case AggregateType.Sum: {
                    decimal sum = 0;
                    foreach (int record in records)
                    {
                        if (IsNull(record))
                        {
                            continue;
                        }
                        sum    += values[record].Ticks;
                        hasData = true;
                    }
                    if (hasData)
                    {
                        return(TimeSpan.FromTicks((long)Math.Round(sum)));
                    }
                    return(null);
                }

                case AggregateType.Mean: {
                    decimal meanSum   = 0;
                    int     meanCount = 0;
                    foreach (int record in records)
                    {
                        if (IsNull(record))
                        {
                            continue;
                        }
                        meanSum += values[record].Ticks;
                        meanCount++;
                    }
                    if (meanCount > 0)
                    {
                        return(TimeSpan.FromTicks((long)Math.Round(meanSum / meanCount)));
                    }
                    return(null);
                }

                case AggregateType.StDev: {
                    int     count   = 0;
                    decimal meanSum = 0;

                    foreach (int record in records)
                    {
                        if (IsNull(record))
                        {
                            continue;
                        }
                        meanSum += values[record].Ticks;
                        count++;
                    }

                    if (count > 1)
                    {
                        double  varSum = 0;
                        decimal mean   = meanSum / count;
                        foreach (int record in records)
                        {
                            if (IsNull(record))
                            {
                                continue;
                            }
                            double x = (double)(values[record].Ticks - mean);
                            varSum += x * x;
                        }
                        ulong stDev = (ulong)Math.Round(Math.Sqrt(varSum / (count - 1)));
                        if (stDev > long.MaxValue)
                        {
                            stDev = long.MaxValue;
                        }
                        return(TimeSpan.FromTicks((long)stDev));
                    }
                    return(null);
                }
                }
            }
            catch (OverflowException) {
                throw ExprException.Overflow(typeof(TimeSpan));
            }
            throw ExceptionBuilder.AggregateException(kind, DataType);
        }
Beispiel #40
0
        void RunTest(AggregateType aggregate)
        {
            try
            {
                AggregateTestResultSet myResults = AggregateTestResultSet.LoadFromXMLFile(
                    String.Format(@"{0}TestResult.xml", Enum.GetName(typeof(AggregateType), aggregate)));

                for (int i = 0; i < myResults.Count; i++)
                {
                    AggregateTestResult testResult = myResults[i] as AggregateTestResult;

                    Debug.WriteLine(String.Format("Test Data: {0}", testResult.TestDataName));
                    Debug.WriteLine(String.Format("Start time: {0}\tEnd time: {1}\tInterval: {2}", 
                        testResult.Details.StartTime.TimeOfDay,
                        testResult.Details.EndTime.TimeOfDay,
                        testResult.Details.ProcessingInterval));
                    // get expected values
                    List<DataValue> expected = new List<DataValue>(testResult.DataValues.Count);
                    for (int ii = 0; ii < testResult.DataValues.Count; ii++)
                        expected.Add(testResult.DataValues[ii].GetDataValue());

                    // configure the aggregate calculator
                    NewAggregateFilter filter = new NewAggregateFilter()
                    {
                        StartTime = testResult.Details.StartTime,
                        EndTime = testResult.Details.EndTime,
                        AggregateType = AggregateLookup[aggregate],
                        AggregateConfiguration = TestData[myResults[i].TestDataName].Configuration.AggregateConfiguration,
                        ProcessingInterval = testResult.Details.ProcessingInterval
                    };
                    TestData testData = TestData[testResult.TestDataName];
                    AggregateCalculatorImpl calculator = Aggregators.CreateAggregator(filter, testData.Configuration.Stepped);
                    /*
                    calculator.Configuration = new AggregateConfiguration()
                    {
                        PercentDataBad = 0,
                        PercentDataGood = 100,
                        SteppedSlopedExtrapolation = false,
                        TreatUncertainAsBad = true
                    };
                     */
                    HistoryData rawHistoryData = new HistoryData();
                    for (int ii = 0; ii < testData.DataValues.Count; ii++)
                    {
                        DataValue dv = testData.DataValues[ii].GetDataValue();
                        rawHistoryData.DataValues.Add(dv);
                    }

                    HistoryData historyData = new HistoryData();
                    var sr = new ServiceResult(StatusCodes.Good);
                    foreach (DataValue raw in rawHistoryData.DataValues)
                    {
                        IList<DataValue> released = calculator.ProcessValue(raw, sr);
                        if (StatusCode.IsGood(sr.StatusCode) && released.Count > 0)
                        {
                            historyData.DataValues.AddRange(released);
                        }
                    }
                    var lsr = new ServiceResult(StatusCodes.Good);
                    historyData.DataValues.AddRange(calculator.ProcessTermination(lsr));

                    // obtain the actual values
                    List<DataValue> actual = new List<DataValue>(historyData.DataValues);

                    // compare the two value sets
                    bool assertion = true;
                    HelperMethods.CompareResults(expected, actual, testResult.TestDataName, assertion);
                    Console.WriteLine("Test {0} passed", i);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.StackTrace);
                throw;
            }
        }
Beispiel #41
0
        public static double Aggregate <TSource>(this IEnumerable <TSource> source, Func <TSource, double> selector, AggregateType type)
        {
            switch (type)
            {
            case AggregateType.Avg:
                return(source.Average(selector));

            case AggregateType.Min:
                return(source.Min(selector));

            case AggregateType.Max:
                return(source.Max(selector));

            case AggregateType.Count:
                return(source.Count());

            case AggregateType.Sum:
            default:
                return(source.Sum(selector));
            }
        }
        override public Object Aggregate(int[] records, AggregateType kind) {
            bool hasData = false;
            try {
                switch (kind) {
                    case AggregateType.Sum:
                        UInt64 sum = defaultValue;
                        foreach (int record in records) {
                            if (HasValue(record)) {
                                checked { sum += (UInt64) values[record];}
                                hasData = true;
                            }
                        }
                        if (hasData) {
                            return sum;
                        }
                        return NullValue;

                    case AggregateType.Mean:
                        Int64 meanSum = (Int64)defaultValue;
                        int meanCount = 0;
                        foreach (int record in records) {
                            if (HasValue(record)) {
                                checked { meanSum += (Int64)values[record];}
                                meanCount++;
                                hasData = true;
                            }
                        }
                        if (hasData) {
                            UInt32 mean;
                            checked {mean = (UInt32)(meanSum / meanCount);}
                            return mean;
                        }
                        return NullValue;

                    case AggregateType.Var:
                    case AggregateType.StDev:
                        int count = 0;
                        double var = 0.0f;
                        double prec = 0.0f;
                        double dsum = 0.0f;
                        double sqrsum = 0.0f;

                        foreach (int record in records) {
                            if (HasValue(record)) {
                                dsum += (double)values[record];
                                sqrsum += (double)values[record]*(double)values[record];
                                count++;
                            }
                        }

                        if (count > 1) {
                            var = ((double)count * sqrsum - (dsum * dsum));
                            prec = var / (dsum * dsum);

                            // we are dealing with the risk of a cancellation error
                            // double is guaranteed only for 15 digits so a difference
                            // with a result less than 1e-15 should be considered as zero

                            if ((prec < 1e-15) || (var <0))
                                var = 0;
                            else
                                var = var / (count * (count -1));

                            if (kind == AggregateType.StDev) {
                                return Math.Sqrt(var);
                            }
                            return var;
                        }
                        return NullValue;

                    case AggregateType.Min:
                        UInt32 min = UInt32.MaxValue;
                        for (int i = 0; i < records.Length; i++) {
                            int record = records[i];
                            if (HasValue(record)) {
                                min=Math.Min(values[record], min);
                                hasData = true;
                            }
                        }
                        if (hasData) {
                            return min;
                        }
                        return NullValue;

                    case AggregateType.Max:
                        UInt32 max = UInt32.MinValue;
                        for (int i = 0; i < records.Length; i++) {
                            int record = records[i];
                            if (HasValue(record)) {
                                max=Math.Max(values[record], max);
                                hasData = true;
                            }
                        }
                        if (hasData) {
                            return max;
                        }
                        return NullValue;

                    case AggregateType.First:
                        if (records.Length > 0) {
                            return values[records[0]];
                        }
                        return null;

                    case AggregateType.Count:
                        count = 0;
                        for (int i = 0; i < records.Length; i++) {
                            if (HasValue(records[i]))
                                count++;
                        }
                        return count;
                }
            }
            catch (OverflowException) {
                throw ExprException.Overflow(typeof(UInt32));
            }
            throw ExceptionBuilder.AggregateException(kind, DataType);
        }
Beispiel #43
0
 private static bool HasPredicateArgument(AggregateType aggregateType)
 {
     return(aggregateType == AggregateType.Count);
 }
Beispiel #44
0
        public override object Aggregate(int[] records, AggregateType kind)
        {
            bool hasData = false;
            try
            {
                switch (kind)
                {
                    case AggregateType.Sum:
                        float sum = defaultValue;
                        foreach (int record in records)
                        {
                            if (IsNull(record))
                                continue;
                            checked { sum += _values[record]; }
                            hasData = true;
                        }
                        if (hasData)
                        {
                            return sum;
                        }
                        return _nullValue;

                    case AggregateType.Mean:
                        double meanSum = defaultValue;
                        int meanCount = 0;
                        foreach (int record in records)
                        {
                            if (IsNull(record))
                                continue;
                            checked { meanSum += _values[record]; }
                            meanCount++;
                            hasData = true;
                        }
                        if (hasData)
                        {
                            float mean;
                            checked { mean = (float)(meanSum / meanCount); }
                            return mean;
                        }
                        return _nullValue;

                    case AggregateType.Var:
                    case AggregateType.StDev:
                        int count = 0;
                        double var = defaultValue;
                        double prec = defaultValue;
                        double dsum = defaultValue;
                        double sqrsum = defaultValue;

                        foreach (int record in records)
                        {
                            if (IsNull(record))
                                continue;
                            dsum += _values[record];
                            sqrsum += _values[record] * (double)_values[record];
                            count++;
                        }

                        if (count > 1)
                        {
                            var = count * sqrsum - (dsum * dsum);
                            prec = var / (dsum * dsum);

                            // we are dealing with the risk of a cancellation error
                            // double is guaranteed only for 15 digits so a difference
                            // with a result less than 1e-15 should be considered as zero

                            if ((prec < 1e-15) || (var < 0))
                                var = 0;
                            else
                                var = var / (count * (count - 1));

                            if (kind == AggregateType.StDev)
                            {
                                return Math.Sqrt(var);
                            }
                            return var;
                        }
                        return _nullValue;


                    case AggregateType.Min:
                        float min = float.MaxValue;
                        for (int i = 0; i < records.Length; i++)
                        {
                            int record = records[i];
                            if (IsNull(record))
                                continue;
                            min = Math.Min(_values[record], min);
                            hasData = true;
                        }
                        if (hasData)
                        {
                            return min;
                        }
                        return _nullValue;

                    case AggregateType.Max:
                        float max = float.MinValue;
                        for (int i = 0; i < records.Length; i++)
                        {
                            int record = records[i];
                            if (IsNull(record))
                                continue;
                            max = Math.Max(_values[record], max);
                            hasData = true;
                        }
                        if (hasData)
                        {
                            return max;
                        }
                        return _nullValue;

                    case AggregateType.First:
                        if (records.Length > 0)
                        {
                            return _values[records[0]];
                        }
                        return null;

                    case AggregateType.Count:
                        return base.Aggregate(records, kind);
                }
            }
            catch (OverflowException)
            {
                throw ExprException.Overflow(typeof(float));
            }
            throw ExceptionBuilder.AggregateException(kind, _dataType);
        }
Beispiel #45
0
        public override object Aggregate(int[] records, AggregateType kind)
        {
            bool hasData = false;
            try
            {
                switch (kind)
                {
                    case AggregateType.Sum:
                        SqlInt64 sum = 0;
                        foreach (int record in records)
                        {
                            if (IsNull(record))
                                continue;
                            checked { sum += _values[record]; }
                            hasData = true;
                        }
                        if (hasData)
                        {
                            return sum;
                        }
                        return _nullValue;

                    case AggregateType.Mean:
                        SqlInt64 meanSum = 0;
                        int meanCount = 0;
                        foreach (int record in records)
                        {
                            if (IsNull(record))
                                continue;
                            checked { meanSum += (_values[record]).ToSqlInt64(); }
                            meanCount++;
                            hasData = true;
                        }
                        if (hasData)
                        {
                            SqlInt32 mean = 0;
                            checked { mean = (meanSum / meanCount).ToSqlInt32(); }
                            return mean;
                        }
                        return _nullValue;

                    case AggregateType.Var:
                    case AggregateType.StDev:
                        int count = 0;
                        SqlDouble var = 0;
                        SqlDouble prec = 0;
                        SqlDouble dsum = 0;
                        SqlDouble sqrsum = 0;

                        foreach (int record in records)
                        {
                            if (IsNull(record))
                                continue;
                            dsum += (_values[record]).ToSqlDouble();
                            sqrsum += (_values[record]).ToSqlDouble() * (_values[record]).ToSqlDouble();
                            count++;
                        }

                        if (count > 1)
                        {
                            var = count * sqrsum - (dsum * dsum);
                            prec = var / (dsum * dsum);

                            // we are dealing with the risk of a cancellation error
                            // double is guaranteed only for 15 digits so a difference
                            // with a result less than 1e-15 should be considered as zero

                            if ((prec < 1e-15) || (var < 0))
                                var = 0;
                            else
                                var = var / (count * (count - 1));

                            if (kind == AggregateType.StDev)
                            {
                                return Math.Sqrt(var.Value);
                            }
                            return var;
                        }
                        return _nullValue;

                    case AggregateType.Min:
                        SqlInt32 min = SqlInt32.MaxValue;
                        for (int i = 0; i < records.Length; i++)
                        {
                            int record = records[i];
                            if (IsNull(record))
                                continue;
                            if ((SqlInt32.LessThan(_values[record], min)).IsTrue)
                                min = _values[record];
                            hasData = true;
                        }
                        if (hasData)
                        {
                            return min;
                        }
                        return _nullValue;

                    case AggregateType.Max:
                        SqlInt32 max = SqlInt32.MinValue;
                        for (int i = 0; i < records.Length; i++)
                        {
                            int record = records[i];
                            if (IsNull(record))
                                continue;
                            if ((SqlInt32.GreaterThan(_values[record], max)).IsTrue)
                                max = _values[record];
                            hasData = true;
                        }
                        if (hasData)
                        {
                            return max;
                        }
                        return _nullValue;

                    case AggregateType.First:
                        if (records.Length > 0)
                        {
                            return _values[records[0]];
                        }
                        return null;

                    case AggregateType.Count:
                        count = 0;
                        for (int i = 0; i < records.Length; i++)
                        {
                            if (!IsNull(records[i]))
                                count++;
                        }
                        return count;
                }
            }
            catch (OverflowException)
            {
                throw ExprException.Overflow(typeof(SqlInt32));
            }
            throw ExceptionBuilder.AggregateException(kind, _dataType);
        }
Beispiel #46
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Aggregate" /> class.
 /// </summary>
 /// <param name="aggregateType">The type of the aggregate (e.g. sum, or count).</param>
 /// <param name="field">The field to be aggregated.</param>
 public Aggregate(AggregateType aggregateType, Field field)
 {
     this.AggregateType = aggregateType;
     this.Field         = field;
 }
Beispiel #47
0
 public ZUnionCommand(string key, IEnumerable<string> keys, IEnumerable<double> weights, AggregateType aggregateType) :
     base(key, keys, weights, aggregateType)
 {
 }
Beispiel #48
0
 //
 // Storage
 //
 public static Exception AggregateException(AggregateType aggregateType, Type type) => _Data(SR.Format(SR.DataStorage_AggregateException, aggregateType, type.Name));
        public override object Aggregate(int[] records, AggregateType kind)
        {
            bool flag = false;
            try
            {
                int num;
                double num2;
                double num3;
                int num4;
                int num5;
                int num6;
                int num7;
                int num8;
                int num9;
                int num10;
                ushort num11;
                ushort num12;
                double num13;
                int num14;
                long num15;
                ulong defaultValue;
                int num17;
                int num18;
                int[] numArray;
                double num19;
                int num20;
                int[] numArray2;
                int num21;
                int[] numArray3;
                switch (kind)
                {
                    case AggregateType.Sum:
                        defaultValue = UInt16Storage.defaultValue;
                        numArray3 = records;
                        num10 = 0;
                        goto Label_006B;

                    case AggregateType.Mean:
                        num15 = UInt16Storage.defaultValue;
                        num14 = 0;
                        numArray2 = records;
                        num9 = 0;
                        goto Label_00D0;

                    case AggregateType.Min:
                        num12 = 0xffff;
                        num6 = 0;
                        goto Label_0229;

                    case AggregateType.Max:
                        num11 = 0;
                        num5 = 0;
                        goto Label_027E;

                    case AggregateType.First:
                        if (records.Length <= 0)
                        {
                            return null;
                        }
                        return this.values[records[0]];

                    case AggregateType.Count:
                        num = 0;
                        num4 = 0;
                        goto Label_02D7;

                    case AggregateType.Var:
                    case AggregateType.StDev:
                        num = 0;
                        num2 = 0.0;
                        num19 = 0.0;
                        num3 = 0.0;
                        num13 = 0.0;
                        numArray = records;
                        num8 = 0;
                        goto Label_0176;

                    default:
                        goto Label_02FC;
                }
            Label_0043:
                num21 = numArray3[num10];
                if (base.HasValue(num21))
                {
                    defaultValue += this.values[num21];
                    flag = true;
                }
                num10++;
            Label_006B:
                if (num10 < numArray3.Length)
                {
                    goto Label_0043;
                }
                if (flag)
                {
                    return defaultValue;
                }
                return base.NullValue;
            Label_00A2:
                num20 = numArray2[num9];
                if (base.HasValue(num20))
                {
                    num15 += this.values[num20];
                    num14++;
                    flag = true;
                }
                num9++;
            Label_00D0:
                if (num9 < numArray2.Length)
                {
                    goto Label_00A2;
                }
                if (flag)
                {
                    return (ushort) (num15 / ((long) num14));
                }
                return base.NullValue;
            Label_0132:
                num7 = numArray[num8];
                if (base.HasValue(num7))
                {
                    num3 += this.values[num7];
                    num13 += this.values[num7] * this.values[num7];
                    num++;
                }
                num8++;
            Label_0176:
                if (num8 < numArray.Length)
                {
                    goto Label_0132;
                }
                if (num <= 1)
                {
                    return base.NullValue;
                }
                num2 = (num * num13) - (num3 * num3);
                num19 = num2 / (num3 * num3);
                if ((num19 < 1E-15) || (num2 < 0.0))
                {
                    num2 = 0.0;
                }
                else
                {
                    num2 /= (double) (num * (num - 1));
                }
                if (kind == AggregateType.StDev)
                {
                    return Math.Sqrt(num2);
                }
                return num2;
            Label_01FF:
                num18 = records[num6];
                if (base.HasValue(num18))
                {
                    num12 = Math.Min(this.values[num18], num12);
                    flag = true;
                }
                num6++;
            Label_0229:
                if (num6 < records.Length)
                {
                    goto Label_01FF;
                }
                if (flag)
                {
                    return num12;
                }
                return base.NullValue;
            Label_0254:
                num17 = records[num5];
                if (base.HasValue(num17))
                {
                    num11 = Math.Max(this.values[num17], num11);
                    flag = true;
                }
                num5++;
            Label_027E:
                if (num5 < records.Length)
                {
                    goto Label_0254;
                }
                if (flag)
                {
                    return num11;
                }
                return base.NullValue;
            Label_02C1:
                if (base.HasValue(records[num4]))
                {
                    num++;
                }
                num4++;
            Label_02D7:
                if (num4 < records.Length)
                {
                    goto Label_02C1;
                }
                return num;
            }
            catch (OverflowException)
            {
                throw ExprException.Overflow(typeof(ushort));
            }
        Label_02FC:
            throw ExceptionBuilder.AggregateException(kind, base.DataType);
        }
Beispiel #50
0
        public override object Aggregate(int[] recordNos, AggregateType kind)
        {
            int i;

            switch (kind)
            {
            case AggregateType.Min:
                int min = -1;
                for (i = 0; i < recordNos.Length; i++)
                {
                    if (IsNull(recordNos[i]))
                    {
                        continue;
                    }
                    min = recordNos[i];
                    break;
                }
                if (min >= 0)
                {
                    for (i = i + 1; i < recordNos.Length; i++)
                    {
                        if (IsNull(recordNos[i]))
                        {
                            continue;
                        }
                        if (Compare(min, recordNos[i]) > 0)
                        {
                            min = recordNos[i];
                        }
                    }
                    return(Get(min));
                }
                return(_nullValue);

            case AggregateType.Max:
                int max = -1;
                for (i = 0; i < recordNos.Length; i++)
                {
                    if (IsNull(recordNos[i]))
                    {
                        continue;
                    }
                    max = recordNos[i];
                    break;
                }
                if (max >= 0)
                {
                    for (i = i + 1; i < recordNos.Length; i++)
                    {
                        if (Compare(max, recordNos[i]) < 0)
                        {
                            max = recordNos[i];
                        }
                    }
                    return(Get(max));
                }
                return(_nullValue);

            case AggregateType.Count:
                int count = 0;
                for (i = 0; i < recordNos.Length; i++)
                {
                    object value = _values[recordNos[i]];
                    if (value != null)
                    {
                        count++;
                    }
                }
                return(count);
            }
            throw ExceptionBuilder.AggregateException(kind, _dataType);
        }
Beispiel #51
0
 public AggregateItem(AggregateType type, TObject item, TKey key)
 {
     _type = type;
     _item = item;
     _key  = key;
 }
        override public Object Aggregate(int[] records, AggregateType kind)
        {
            bool hasData = false;

            try {
                switch (kind)
                {
                case AggregateType.Sum:
                    SqlInt64 sum = 0;
                    foreach (int record in records)
                    {
                        if (IsNull(record))
                        {
                            continue;
                        }
                        checked { sum += values[record]; }
                        hasData = true;
                    }
                    if (hasData)
                    {
                        return(sum);
                    }
                    return(NullValue);

                case AggregateType.Mean:
                    SqlInt64 meanSum   = 0;
                    int      meanCount = 0;
                    foreach (int record in records)
                    {
                        if (IsNull(record))
                        {
                            continue;
                        }
                        checked { meanSum += values[record].ToSqlInt64(); }
                        meanCount++;
                        hasData = true;
                    }
                    if (hasData)
                    {
                        SqlByte mean = 0;
                        checked { mean = (meanSum / (SqlInt64)meanCount).ToSqlByte(); }
                        return(mean);
                    }
                    return(NullValue);

                case AggregateType.Var:
                case AggregateType.StDev:
                    int       count  = 0;
                    SqlDouble var    = (SqlDouble)0;
                    SqlDouble prec   = (SqlDouble)0;
                    SqlDouble dsum   = (SqlDouble)0;
                    SqlDouble sqrsum = (SqlDouble)0;

                    foreach (int record in records)
                    {
                        if (IsNull(record))
                        {
                            continue;
                        }
                        dsum   += values[record].ToSqlDouble();
                        sqrsum += values[record].ToSqlDouble() * values[record].ToSqlDouble();
                        count++;
                    }

                    if (count > 1)
                    {
                        var  = ((SqlDouble)count * sqrsum - (dsum * dsum));
                        prec = var / (dsum * dsum);

                        // we are dealing with the risk of a cancellation error
                        // double is guaranteed only for 15 digits so a difference
                        // with a result less than 1e-15 should be considered as zero

                        if ((prec < 1e-15) || (var < 0))
                        {
                            var = 0;
                        }
                        else
                        {
                            var = var / (count * (count - 1));
                        }

                        if (kind == AggregateType.StDev)
                        {
                            return(Math.Sqrt(var.Value));
                        }
                        return(var);
                    }
                    return(NullValue);

                case AggregateType.Min:
                    SqlByte min = SqlByte.MaxValue;
                    for (int i = 0; i < records.Length; i++)
                    {
                        int record = records[i];
                        if (IsNull(record))
                        {
                            continue;
                        }
                        if ((SqlByte.LessThan(values[record], min)).IsTrue)
                        {
                            min = values[record];
                        }
                        hasData = true;
                    }
                    if (hasData)
                    {
                        return(min);
                    }
                    return(NullValue);

                case AggregateType.Max:
                    SqlByte max = SqlByte.MinValue;
                    for (int i = 0; i < records.Length; i++)
                    {
                        int record = records[i];
                        if (IsNull(record))
                        {
                            continue;
                        }
                        if ((SqlByte.GreaterThan(values[record], max)).IsTrue)
                        {
                            max = values[record];
                        }
                        hasData = true;
                    }
                    if (hasData)
                    {
                        return(max);
                    }
                    return(NullValue);

                case AggregateType.First:
                    if (records.Length > 0)
                    {
                        return(values[records[0]]);
                    }
                    return(null);   // no data => null

                case AggregateType.Count:
                    count = 0;
                    for (int i = 0; i < records.Length; i++)
                    {
                        if (!IsNull(records[i]))
                        {
                            count++;
                        }
                    }
                    return(count);
                }
            }
            catch (OverflowException) {
                throw ExprException.Overflow(typeof(SqlByte));
            }
            throw ExceptionBuilder.AggregateException(kind, DataType);
        }
 private static bool HasPredicateArgument(AggregateType aggregateType)
 {
     return aggregateType == AggregateType.Count;
 }
        public override object Aggregate(int[] records, AggregateType kind)
        {
            bool hasData = false;

            try
            {
                switch (kind)
                {
                case AggregateType.Min:
                    SqlBoolean min = true;
                    for (int i = 0; i < records.Length; i++)
                    {
                        int record = records[i];
                        if (IsNull(record))
                        {
                            continue;
                        }
                        min     = SqlBoolean.And(_values[record], min);
                        hasData = true;
                    }
                    if (hasData)
                    {
                        return(min);
                    }
                    return(_nullValue);

                case AggregateType.Max:
                    SqlBoolean max = false;
                    for (int i = 0; i < records.Length; i++)
                    {
                        int record = records[i];
                        if (IsNull(record))
                        {
                            continue;
                        }
                        max     = SqlBoolean.Or(_values[record], max);
                        hasData = true;
                    }
                    if (hasData)
                    {
                        return(max);
                    }
                    return(_nullValue);

                case AggregateType.First:
                    if (records.Length > 0)
                    {
                        return(_values[records[0]]);
                    }
                    return(_nullValue);

                case AggregateType.Count:
                    int count = 0;
                    for (int i = 0; i < records.Length; i++)
                    {
                        if (!IsNull(records[i]))
                        {
                            count++;
                        }
                    }
                    return(count);
                }
            }
            catch (OverflowException)
            {
                throw ExprException.Overflow(typeof(SqlBoolean));
            }
            throw ExceptionBuilder.AggregateException(kind, _dataType);
        }
        public override object Aggregate(int[] records, AggregateType kind)
        {
            bool flag = false;

            try
            {
                double num;
                double num2;
                int    num3;
                int    num4;
                int    num5;
                int    num6;
                int    num7;
                int    num8;
                int    num9;
                byte   num10;
                byte   num11;
                double num12;
                int    num13;
                long   num14;
                ulong  num15;
                int    num16;
                int    num17;
                int[]  numArray;
                double num18;
                int    num19;
                int[]  numArray2;
                int    num20;
                int[]  numArray3;
                switch (kind)
                {
                case AggregateType.Sum:
                    num15     = 0L;
                    numArray3 = records;
                    num9      = 0;
                    goto Label_0067;

                case AggregateType.Mean:
                    num14     = 0L;
                    num13     = 0;
                    numArray2 = records;
                    num8      = 0;
                    goto Label_00C8;

                case AggregateType.Min:
                    num11 = 0xff;
                    num5  = 0;
                    goto Label_0221;

                case AggregateType.Max:
                    num10 = 0;
                    num4  = 0;
                    goto Label_0276;

                case AggregateType.First:
                    if (records.Length <= 0)
                    {
                        return(null);
                    }
                    return(this.values[records[0]]);

                case AggregateType.Count:
                    return(base.Aggregate(records, kind));

                case AggregateType.Var:
                case AggregateType.StDev:
                    num3     = 0;
                    num      = 0.0;
                    num18    = 0.0;
                    num2     = 0.0;
                    num12    = 0.0;
                    numArray = records;
                    num7     = 0;
                    goto Label_016E;

                default:
                    goto Label_02CF;
                }
Label_003F:
                num20 = numArray3[num9];
                if (!this.IsNull(num20))
                {
                    num15 += this.values[num20];
                    flag   = true;
                }
                num9++;
Label_0067:
                if (num9 < numArray3.Length)
                {
                    goto Label_003F;
                }
                if (flag)
                {
                    return(num15);
                }
                return(base.NullValue);

Label_009A:
                num19 = numArray2[num8];
                if (!this.IsNull(num19))
                {
                    num14 += this.values[num19];
                    num13++;
                    flag = true;
                }
                num8++;
Label_00C8:
                if (num8 < numArray2.Length)
                {
                    goto Label_009A;
                }
                if (flag)
                {
                    return((byte)(num14 / ((long)num13)));
                }
                return(base.NullValue);

Label_012A:
                num6 = numArray[num7];
                if (!this.IsNull(num6))
                {
                    num2  += this.values[num6];
                    num12 += this.values[num6] * this.values[num6];
                    num3++;
                }
                num7++;
Label_016E:
                if (num7 < numArray.Length)
                {
                    goto Label_012A;
                }
                if (num3 <= 1)
                {
                    return(base.NullValue);
                }
                num   = (num3 * num12) - (num2 * num2);
                num18 = num / (num2 * num2);
                if ((num18 < 1E-15) || (num < 0.0))
                {
                    num = 0.0;
                }
                else
                {
                    num /= (double)(num3 * (num3 - 1));
                }
                if (kind == AggregateType.StDev)
                {
                    return(Math.Sqrt(num));
                }
                return(num);

Label_01F7:
                num17 = records[num5];
                if (!this.IsNull(num17))
                {
                    num11 = Math.Min(this.values[num17], num11);
                    flag  = true;
                }
                num5++;
Label_0221:
                if (num5 < records.Length)
                {
                    goto Label_01F7;
                }
                if (flag)
                {
                    return(num11);
                }
                return(base.NullValue);

Label_024C:
                num16 = records[num4];
                if (!this.IsNull(num16))
                {
                    num10 = Math.Max(this.values[num16], num10);
                    flag  = true;
                }
                num4++;
Label_0276:
                if (num4 < records.Length)
                {
                    goto Label_024C;
                }
                if (flag)
                {
                    return(num10);
                }
                return(base.NullValue);
            }
            catch (OverflowException)
            {
                throw ExprException.Overflow(typeof(byte));
            }
Label_02CF:
            throw ExceptionBuilder.AggregateException(kind, base.DataType);
        }
Beispiel #56
0
        public CollectionView(IEnumerable <object> dataSource, string[] paths, string aggregateOn, AggregateType type = AggregateType.Sum, string name = null)
        {
            this.SourceCollection = dataSource;
            this.Name             = name;
            var views = GetViews(dataSource, new Queue <string>(paths), aggregateOn, name);

            this.Views = views.ToArray();
        }
Beispiel #57
0
        override public Object Aggregate(int[] records, AggregateType kind) {
            bool hasData = false;
            try {
                switch (kind) {
                    case AggregateType.Sum:
                        SqlDecimal sum =  0;
                        foreach (int record in records) {
                            if (IsNull(record))
                                continue;
                            checked { sum += values[record];}
                            hasData = true;
                        }
                        if (hasData) {
                            return sum;
                        }
                        return NullValue;

                    case AggregateType.Mean:
                        SqlDecimal meanSum = 0;
                        int meanCount = 0;
                        foreach (int record in records) {
                            if (IsNull(record))
                                continue;
                            checked { meanSum += (values[record]).ToSqlDecimal();}
                            meanCount++;
                            hasData = true;
                        }
                        if (hasData) {
                            SqlMoney mean = 0;
                            checked {mean = (meanSum / (SqlDecimal)meanCount).ToSqlMoney();}
                            return mean;
                        }
                        return NullValue;

                    case AggregateType.Var:
                    case AggregateType.StDev:
                        int count = 0;
                        SqlDouble var = (SqlDouble)0;
                        SqlDouble prec = (SqlDouble)0;
                        SqlDouble dsum = (SqlDouble)0;
                        SqlDouble sqrsum = (SqlDouble)0;

                        foreach (int record in records) {
                            if (IsNull(record))
                                continue;
                            dsum += values[record].ToSqlDouble();
                            sqrsum += (values[record]).ToSqlDouble() * (values[record]).ToSqlDouble();
                            count++;
                        }

                        if (count > 1) {
                            var = ((SqlDouble)count * sqrsum - (dsum * dsum));
                            prec = var / (dsum * dsum);
                            
                            // we are dealing with the risk of a cancellation error
                            // double is guaranteed only for 15 digits so a difference 
                            // with a result less than 1e-15 should be considered as zero

                            if ((prec < 1e-15) || (var <0))
                                var = 0;
                            else
                                var = var / (count * (count -1));
                            
                            if (kind == AggregateType.StDev) {
                               return  Math.Sqrt(var.Value);
                            }
                            return var;
                        }
                        return NullValue;

                    case AggregateType.Min:
                        SqlMoney min = SqlMoney.MaxValue;
                        for (int i = 0; i < records.Length; i++) {
                            int record = records[i];
                            if (IsNull(record))
                                continue;
                            if ((SqlMoney.LessThan(values[record], min)).IsTrue)
                                min = values[record];         
                            hasData = true;
                        }
                        if (hasData) {
                            return min;
                        }
                        return NullValue;

                    case AggregateType.Max:
                        SqlMoney max = SqlMoney.MinValue;
                        for (int i = 0; i < records.Length; i++) {
                            int record = records[i];
                            if (IsNull(record))
                                continue;
                            if ((SqlMoney.GreaterThan(values[record], max)).IsTrue)
                                max = values[record];
                            hasData = true;
                        }
                        if (hasData) {
                            return max;
                        }
                        return NullValue;

                    case AggregateType.First:
                        if (records.Length > 0) {
                            return values[records[0]];
                        }
                        return null;

                    case AggregateType.Count:
                        count = 0;
                        for (int i = 0; i < records.Length; i++) {
                            if (!IsNull(records[i]))
                                count++;
                        }
                        return count;
                }
            }
            catch (OverflowException) {
                throw ExprException.Overflow(typeof(SqlMoney));
            }
            throw ExceptionBuilder.AggregateException(kind, DataType);
        }
Beispiel #58
0
        private void GetRootCauseDirectionAndScore(Dictionary <string, Point> dimPointMapping, Dictionary <string, Object> anomalyRoot, RootCause dst, double beta, PointTree pointTree, AggregateType aggType, Object aggSymbol)
        {
            Point anomalyPoint = GetPointByDimension(dimPointMapping, anomalyRoot, pointTree, aggType, aggSymbol);

            if (dst.Items.Count > 1)
            {
                //get surprise value and explanatory power value
                List <RootCauseScore> scoreList = new List <RootCauseScore>();

                foreach (RootCauseItem item in dst.Items)
                {
                    Point rootCausePoint = GetPointByDimension(dimPointMapping, item.Dimension, pointTree, aggType, aggSymbol);
                    if (anomalyPoint != null && rootCausePoint != null)
                    {
                        Tuple <double, double> scores = GetSurpriseAndExplanatoryScore(rootCausePoint, anomalyPoint);
                        scoreList.Add(new RootCauseScore(scores.Item1, scores.Item2));
                        item.Direction = GetRootCauseDirection(rootCausePoint);
                    }
                }

                //get final score
                for (int i = 0; i < scoreList.Count; i++)
                {
                    if (aggType.Equals(AggregateType.Max) || aggType.Equals(AggregateType.Min))
                    {
                        dst.Items[i].Score = 1;
                    }
                    else
                    {
                        dst.Items[i].Score = GetFinalScore(scoreList[i].Surprise, Math.Abs(scoreList[i].ExplanatoryScore), beta);
                    }
                }
            }
            else if (dst.Items.Count == 1)
            {
                Point rootCausePoint = GetPointByDimension(dimPointMapping, dst.Items[0].Dimension, pointTree, aggType, aggSymbol);
                if (anomalyPoint != null && rootCausePoint != null)
                {
                    Tuple <double, double> scores = GetSurpriseAndExplanatoryScore(rootCausePoint, anomalyPoint);
                    if (aggType.Equals(AggregateType.Max) || aggType.Equals(AggregateType.Min))
                    {
                        dst.Items[0].Score = 1;
                    }
                    else
                    {
                        dst.Items[0].Score = GetFinalScore(scores.Item1, scores.Item2, beta);
                    }
                    dst.Items[0].Direction = GetRootCauseDirection(rootCausePoint);
                }
            }
        }
 override public Object Aggregate(int[] records, AggregateType kind) {
     throw ExceptionBuilder.AggregateException(kind, DataType);
 }
Beispiel #60
0
        private Point GetPointByDimension(Dictionary <string, Point> dimPointMapping, Dictionary <string, Object> dimension, PointTree pointTree, AggregateType aggType, Object aggSymbol)
        {
            if (dimPointMapping.ContainsKey(GetDicCode(dimension)))
            {
                return(dimPointMapping[GetDicCode(dimension)]);
            }

            int           count                = 0;
            Point         p                    = new Point(dimension);
            DimensionInfo dimensionInfo        = SeparateDimension(dimension, aggSymbol);
            Dictionary <string, Object> subDim = GetSubDim(dimension, dimensionInfo.DetailDims);

            foreach (Point leave in pointTree.Leaves)
            {
                if (ContainsAll(leave.Dimension, subDim))
                {
                    count++;

                    p.Value         = +leave.Value;
                    p.ExpectedValue = +leave.ExpectedValue;
                    p.Delta         = +leave.Delta;
                }
            }
            if (aggType.Equals(AggregateType.Avg))
            {
                p.Value         = p.Value / count;
                p.ExpectedValue = p.ExpectedValue / count;
                p.Delta         = p.Delta / count;
            }

            if (count > 0)
            {
                return(p);
            }
            else
            {
                return(null);
            }
        }