Exemple #1
0
        public void Lessers()
        {
            SqlInt64 Test10   = new SqlInt64(10);
            SqlInt64 Test10II = new SqlInt64(10);
            SqlInt64 Test110  = new SqlInt64(110);

            // LessThan()
            Assert.IsTrue(SqlInt64.LessThan(Test10, Test110).Value, "#L01");
            Assert.IsTrue(!SqlInt64.LessThan(Test110, Test10).Value, "#L02");
            Assert.IsTrue(!SqlInt64.LessThan(Test10II, Test10).Value, "#L03");

            // LessThanOrEqual ()
            Assert.IsTrue(SqlInt64.LessThanOrEqual(Test10, Test110).Value, "#L04");
            Assert.IsTrue(!SqlInt64.LessThanOrEqual(Test110, Test10).Value, "#L05");
            Assert.IsTrue(SqlInt64.LessThanOrEqual(Test10II, Test10).Value, "#L06");
            Assert.IsTrue(SqlInt64.LessThanOrEqual(Test10II, SqlInt64.Null).IsNull, "#L07");
        }
        public void Lessers()
        {
            SqlInt64 test10   = new SqlInt64(10);
            SqlInt64 test10II = new SqlInt64(10);
            SqlInt64 test110  = new SqlInt64(110);

            // LessThan()
            Assert.True(SqlInt64.LessThan(test10, test110).Value);
            Assert.False(SqlInt64.LessThan(test110, test10).Value);
            Assert.False(SqlInt64.LessThan(test10II, test10).Value);

            // LessThanOrEqual ()
            Assert.True(SqlInt64.LessThanOrEqual(test10, test110).Value);
            Assert.False(SqlInt64.LessThanOrEqual(test110, test10).Value);
            Assert.True(SqlInt64.LessThanOrEqual(test10II, test10).Value);
            Assert.True(SqlInt64.LessThanOrEqual(test10II, SqlInt64.Null).IsNull);
        }
        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:
                    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)
                    {
                        SqlInt64 mean = 0;
                        checked { mean = (meanSum / meanCount).ToSqlInt64(); }
                        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:
                    SqlInt64 min = SqlInt64.MaxValue;
                    for (int i = 0; i < records.Length; i++)
                    {
                        int record = records[i];
                        if (IsNull(record))
                        {
                            continue;
                        }
                        if ((SqlInt64.LessThan(_values[record], min)).IsTrue)
                        {
                            min = _values[record];
                        }
                        hasData = true;
                    }
                    if (hasData)
                    {
                        return(min);
                    }
                    return(_nullValue);

                case AggregateType.Max:
                    SqlInt64 max = SqlInt64.MinValue;
                    for (int i = 0; i < records.Length; i++)
                    {
                        int record = records[i];
                        if (IsNull(record))
                        {
                            continue;
                        }

                        if ((SqlInt64.GreaterThan(_values[record], max)).IsTrue)
                        {
                            max = _values[record];
                        }
                        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 !);   // 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(SqlInt64));
            }
            throw ExceptionBuilder.AggregateException(kind, _dataType);
        }
        public override object Aggregate(int[] records, AggregateType kind)
        {
            bool flag = false;

            try
            {
                int        num;
                SqlDouble  num2;
                SqlDouble  num3;
                int        num4;
                int        num5;
                int        num6;
                int        num7;
                int        num8;
                int        num9;
                int        num10;
                int        num11;
                SqlInt64   minValue;
                int        num13;
                SqlInt64   maxValue;
                SqlDouble  num15;
                int        num16;
                SqlDecimal num17;
                SqlInt64   num18;
                int[]      numArray;
                SqlDouble  num19;
                int        num21;
                int[]      numArray2;
                int        num22;
                int[]      numArray3;
                switch (kind)
                {
                case AggregateType.Sum:
                    num18     = 0L;
                    numArray3 = records;
                    num10     = 0;
                    goto Label_0078;

                case AggregateType.Mean:
                    num17     = 0L;
                    num16     = 0;
                    numArray2 = records;
                    num9      = 0;
                    goto Label_00EA;

                case AggregateType.Min:
                    maxValue = SqlInt64.MaxValue;
                    num6     = 0;
                    goto Label_030F;

                case AggregateType.Max:
                    minValue = SqlInt64.MinValue;
                    num5     = 0;
                    goto Label_038E;

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

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

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

                default:
                    goto Label_0418;
                }
Label_0044:
                num22 = numArray3[num10];
                if (!this.IsNull(num22))
                {
                    num18 += this.values[num22];
                    flag   = true;
                }
                num10++;
Label_0078:
                if (num10 < numArray3.Length)
                {
                    goto Label_0044;
                }
                if (flag)
                {
                    return(num18);
                }
                return(base.NullValue);

Label_00B0:
                num21 = numArray2[num9];
                if (!this.IsNull(num21))
                {
                    num17 += this.values[num21].ToSqlDecimal();
                    num16++;
                    flag = true;
                }
                num9++;
Label_00EA:
                if (num9 < numArray2.Length)
                {
                    goto Label_00B0;
                }
                if (flag)
                {
                    SqlDecimal num23 = num17 / ((long)num16);
                    return(num23.ToSqlInt64());
                }
                return(base.NullValue);

Label_017A:
                num7 = numArray[num8];
                if (!this.IsNull(num7))
                {
                    num3  += this.values[num7].ToSqlDouble();
                    num15 += this.values[num7].ToSqlDouble() * this.values[num7].ToSqlDouble();
                    num++;
                }
                num8++;
Label_01E2:
                if (num8 < numArray.Length)
                {
                    goto Label_017A;
                }
                if (num <= 1)
                {
                    return(base.NullValue);
                }
                num2  = ((SqlDouble)(num * num15)) - (num3 * num3);
                num19 = num2 / (num3 * num3);
                bool x = num19 < 1E-15;
                if (!SqlBoolean.op_True(x))
                {
                }
                if (SqlBoolean.op_True(x | (num2 < 0.0)))
                {
                    num2 = 0.0;
                }
                else
                {
                    num2 /= (double)(num * (num - 1));
                }
                if (kind == AggregateType.StDev)
                {
                    return(Math.Sqrt(num2.Value));
                }
                return(num2);

Label_02BF:
                num13 = records[num6];
                if (!this.IsNull(num13))
                {
                    if (SqlInt64.LessThan(this.values[num13], maxValue).IsTrue)
                    {
                        maxValue = this.values[num13];
                    }
                    flag = true;
                }
                num6++;
Label_030F:
                if (num6 < records.Length)
                {
                    goto Label_02BF;
                }
                if (flag)
                {
                    return(maxValue);
                }
                return(base.NullValue);

Label_033E:
                num11 = records[num5];
                if (!this.IsNull(num11))
                {
                    if (SqlInt64.GreaterThan(this.values[num11], minValue).IsTrue)
                    {
                        minValue = this.values[num11];
                    }
                    flag = true;
                }
                num5++;
Label_038E:
                if (num5 < records.Length)
                {
                    goto Label_033E;
                }
                if (flag)
                {
                    return(minValue);
                }
                return(base.NullValue);

Label_03DD:
                if (!this.IsNull(records[num4]))
                {
                    num++;
                }
                num4++;
Label_03F3:
                if (num4 < records.Length)
                {
                    goto Label_03DD;
                }
                return(num);
            }
            catch (OverflowException)
            {
                throw ExprException.Overflow(typeof(SqlInt64));
            }
Label_0418:
            throw ExceptionBuilder.AggregateException(kind, base.DataType);
        }