예제 #1
0
        public void LessThan()
        {
            SqlInt32 x;
            SqlInt32 y;

            // Case 1: either is SqlInt32.Null
            x = SqlInt32.Null;
            y = new SqlInt32(5);
            Assert.AreEqual(x < y, SqlBoolean.Null, "Less Than operator didn't return Null when one was Null.");
            Assert.AreEqual(SqlInt32.LessThan(x, y), SqlBoolean.Null, "Less Than function didn't return Null when one was Null.");

            // Case 2: both are SqlInt32.Null
            y = SqlInt32.Null;
            Assert.AreEqual(x < y, SqlBoolean.Null, "Less Than operator didn't return Null when both were Null.");
            Assert.AreEqual(SqlInt32.LessThan(x, y), SqlBoolean.Null, "Less Than function didn't return Null when both were Null.");

            // Case 3: x > y
            x = new SqlInt32(5);
            y = new SqlInt32(4);
            Assert.AreEqual(x < y, SqlBoolean.False, "Less than operator didn't return false when x > y.");
            Assert.AreEqual(SqlInt32.LessThan(x, y), SqlBoolean.False, "Less than function didn't return false when x > y.");

            // Case 4: x < y
            x = new SqlInt32(5);
            y = new SqlInt32(6);
            Assert.AreEqual(x < y, SqlBoolean.True, "Less than operator didn't return true when x < y.");
            Assert.AreEqual(SqlInt32.LessThan(x, y), SqlBoolean.True, "Less than function didn't return true when x < y.");
        }
예제 #2
0
        public void LessThan()
        {
            SqlInt32 x;
            SqlInt32 y;

            // Case 1: either is SqlInt32.Null
            x = SqlInt32.Null;
            y = new SqlInt32(5);
            Assert.Equal(x < y, SqlBoolean.Null);
            Assert.Equal(SqlInt32.LessThan(x, y), SqlBoolean.Null);

            // Case 2: both are SqlInt32.Null
            y = SqlInt32.Null;
            Assert.Equal(x < y, SqlBoolean.Null);
            Assert.Equal(SqlInt32.LessThan(x, y), SqlBoolean.Null);

            // Case 3: x > y
            x = new SqlInt32(5);
            y = new SqlInt32(4);
            Assert.Equal(x < y, SqlBoolean.False);
            Assert.Equal(SqlInt32.LessThan(x, y), SqlBoolean.False);

            // Case 4: x < y
            x = new SqlInt32(5);
            y = new SqlInt32(6);
            Assert.Equal(x < y, SqlBoolean.True);
            Assert.Equal(SqlInt32.LessThan(x, y), SqlBoolean.True);
        }
예제 #3
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 /= (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:     // Does not seem to be implemented
                    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);
        }
        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;
                SqlInt32  minValue;
                int       num13;
                SqlInt32  maxValue;
                SqlDouble num15;
                int       num16;
                SqlInt64  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_007D;

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

                case AggregateType.Min:
                    maxValue = SqlInt32.MaxValue;
                    num6     = 0;
                    goto Label_0313;

                case AggregateType.Max:
                    minValue = SqlInt32.MinValue;
                    num5     = 0;
                    goto Label_0392;

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

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

                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_01E6;

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

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

Label_017E:
                num7 = numArray[num8];
                if (!this.IsNull(num7))
                {
                    num3  += this.values[num7].ToSqlDouble();
                    num15 += this.values[num7].ToSqlDouble() * this.values[num7].ToSqlDouble();
                    num++;
                }
                num8++;
Label_01E6:
                if (num8 < numArray.Length)
                {
                    goto Label_017E;
                }
                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_02C3:
                num13 = records[num6];
                if (!this.IsNull(num13))
                {
                    if (SqlInt32.LessThan(this.values[num13], maxValue).IsTrue)
                    {
                        maxValue = this.values[num13];
                    }
                    flag = true;
                }
                num6++;
Label_0313:
                if (num6 < records.Length)
                {
                    goto Label_02C3;
                }
                if (flag)
                {
                    return(maxValue);
                }
                return(base.NullValue);

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

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