public static DbValue SUM(DbFunctionTools tools, DbAggregatorArguments args)
        {
            string AggregatorName = "SUM";

            double sumd     = 0;
            int    sumi     = 0;
            long   suml     = 0;
            DbType arg0type = DbType.PrepareNull();

            for (int iarg = 0; iarg < args.Length; iarg++)
            {
                args[iarg].EnsureCount(AggregatorName, 1);
                ByteSlice arg0 = args[iarg][0].Eval(out arg0type);
                if (!Types.IsNullValue(arg0))   //ignore null
                {
                    switch (arg0type.ID)
                    {
                    case DbTypeID.INT:
                        sumi += tools.GetInt(arg0);
                        break;

                    case DbTypeID.LONG:
                        suml += tools.GetLong(arg0);
                        break;

                    case DbTypeID.DOUBLE:
                        sumd += tools.GetDouble(arg0);
                        break;

                    default:
                        args[iarg].Expected(AggregatorName, 0, "input INT, LONG or DOUBLE, got " + arg0type.Name.ToUpper());
                        return(null);    // Doesn't reach here.
                    }
                }
            }

            if (args.Length > 0)
            {
                switch (arg0type.ID)
                {
                case DbTypeID.INT:
                    return(tools.AllocValue(sumi));

                    break;

                case DbTypeID.LONG:
                    return(tools.AllocValue(suml));

                    break;

                case DbTypeID.DOUBLE:
                    return(tools.AllocValue(sumd));

                    break;
                }
            }

            return(tools.AllocValue(sumi));
        }
Exemple #2
0
        public static DbValue ExecDbAggregator(string name, DbFunctionTools tools, DbAggregatorArguments args)
        {
            DbValue result = TryExecDbAggregator(name, tools, args);

            if (null == result)
            {
                throw new DbExecException("No such aggregate function named '" + name + "'");
            }
            return(result);
        }
        public static DbValue SUM(DbFunctionTools tools, DbAggregatorArguments args)
        {
            string AggregatorName = "SUM";

            double sumd = 0;
            int sumi = 0;
            long suml = 0;
            DbType arg0type = DbType.PrepareNull();
            for (int iarg = 0; iarg < args.Length; iarg++)
            {
                args[iarg].EnsureCount(AggregatorName, 1);                
                ByteSlice arg0 = args[iarg][0].Eval(out arg0type);
                if (!Types.IsNullValue(arg0))   //ignore null
                {                    
                    switch (arg0type.ID)
                    {
                        case DbTypeID.INT:
                            sumi += tools.GetInt(arg0);
                            break;

                        case DbTypeID.LONG:
                            suml += tools.GetLong(arg0);
                            break;

                        case DbTypeID.DOUBLE:
                            sumd += tools.GetDouble(arg0);
                            break;

                        default:
                            args[iarg].Expected(AggregatorName, 0, "input INT, LONG or DOUBLE, got " + arg0type.Name.ToUpper());
                            return null; // Doesn't reach here.
                    }
                }
            }

            if (args.Length > 0)
            {
                switch (arg0type.ID)
                {
                    case DbTypeID.INT:
                        return tools.AllocValue(sumi);
                        break;

                    case DbTypeID.LONG:
                        return tools.AllocValue(suml);
                        break;

                    case DbTypeID.DOUBLE:
                        return tools.AllocValue(sumd);
                        break;
                }
            }

            return tools.AllocValue(sumi);
        }
        private static DbValue _BITWISE(string aggregatorName, DbFunctionTools tools, DbAggregatorArguments args, int whatop)
        {
            if (args.Length == 0)
            {
                return tools.AllocNullValue();
            }

            DbType arg0type = DbType.PrepareNull();

            for (int iarg = 0; iarg < args.Length; iarg++)
            {
                args[iarg].EnsureCount(aggregatorName, 1);
                ByteSlice arg0 = args[iarg][0].Eval(out arg0type);
                if (Types.IsNullValue(arg0))
                {
                    return tools.AllocNullValue();
                }

                if (bitopbuf == null || bitopbuf.Length != arg0type.Size)
                {
                    bitopbuf = new byte[arg0type.Size];                   
                }

                if (iarg == 0)
                {
                    for (int ib = 0; ib < arg0.Length; ib++)
                    {
                        bitopbuf[ib] = arg0[ib];
                    }
                    continue;
                }

                for (int ib = 1; ib < arg0.Length; ib++)
                {
                    switch (whatop)
                    {
                        case 1:
                            bitopbuf[ib] = (byte)(bitopbuf[ib] & arg0[ib]);
                            break;
                        case 2:
                            bitopbuf[ib] = (byte)(bitopbuf[ib] | arg0[ib]);
                            break;
                        default:
                            bitopbuf[ib] = (byte)(bitopbuf[ib] ^ arg0[ib]);
                            break;
                    }
                }
            }
            ByteSlice bs = ByteSlice.Prepare(bitopbuf);
            return tools.AllocValue(bs, arg0type);
        }
        public static DbValue FIRST(DbFunctionTools tools, DbAggregatorArguments args)
        {
            string AggregatorName = "FIRST";

            if (args.Length > 0)
            {
                args[0].EnsureCount(AggregatorName, 1);
                return(args[0][0]);
            }
            else
            {
                return(tools.AllocNullValue());
            }
        }
        public static DbValue FIRST(DbFunctionTools tools, DbAggregatorArguments args)
        {
            string AggregatorName = "FIRST";

            if (args.Length > 0)
            {
                args[0].EnsureCount(AggregatorName, 1);
                return args[0][0];
            }
            else
            {
                return tools.AllocNullValue();
            }
        }
Exemple #7
0
        public static DbValue LAST(DbFunctionTools tools, DbAggregatorArguments args)
        {
            string AggregatorName = "LAST";

            if (args.Length > 0)
            {
                int lastindex = args.Length - 1;
                args[lastindex].EnsureCount(AggregatorName, 1);
                return(args[lastindex][0]);
            }
            else
            {
                return(tools.AllocNullValue());
            }
        }
        public static DbValue LAST(DbFunctionTools tools, DbAggregatorArguments args)
        {
            string AggregatorName = "LAST";

            if (args.Length > 0)
            {
                int lastindex = args.Length - 1;
                args[lastindex].EnsureCount(AggregatorName, 1);
                return args[lastindex][0];
            }
            else
            {
                return tools.AllocNullValue();
            }
        }
Exemple #9
0
        private static double _VAR(string aggregatorName, DbFunctionTools tools, DbAggregatorArguments args, bool sample)
        {
            List <double> values = new List <double>();
            double        x      = 0;
            double        sum    = 0;

            for (int iarg = 0; iarg < args.Length; iarg++)
            {
                args[iarg].EnsureCount(aggregatorName, 1);
                DbType    arg0type;
                ByteSlice arg0 = args[iarg][0].Eval(out arg0type);
                if (!Types.IsNullValue(arg0))   //ignore null
                {
                    switch (arg0type.ID)
                    {
                    case DbTypeID.INT:
                        x = (double)tools.GetInt(arg0);
                        break;

                    case DbTypeID.LONG:
                        x = (double)tools.GetLong(arg0);
                        break;

                    case DbTypeID.DOUBLE:
                        x = tools.GetDouble(arg0);
                        break;

                    default:
                        args[iarg].Expected(aggregatorName, 0, "input INT, LONG or DOUBLE, got " + arg0type.Name.ToUpper());
                        return(0);    // Doesn't reach here.
                    }
                    values.Add(x);
                    sum += x;
                }
            }
            double dev = 0;

            if (values.Count > 0)
            {
                double avg = sum / (double)values.Count;
                for (int i = 0; i < values.Count; i++)
                {
                    dev += Math.Pow(values[i] - avg, 2);
                }
                dev = dev / (double)(sample ? values.Count - 1 : values.Count);
            }
            return(dev);
        }
        public static DbValue MAX(DbFunctionTools tools, DbAggregatorArguments args)
        {
            string AggregatorName = "MAX";

            DbValue highest = null;
            DbValue nullest = null;
            DbFunctionArguments compareargs = new DbFunctionArguments(new DbValue[2]);
            ImmediateValue argval = null;
            for (int iarg = 0; iarg < args.Length; iarg++)
            {
                args[iarg].EnsureCount(AggregatorName, 1);
                DbType arg0type;
                ByteSlice arg0 = args[iarg][0].Eval(out arg0type);
                if (Types.IsNullValue(arg0))
                {
                    if (null == nullest)
                    {
                        nullest = tools.AllocValue(arg0, arg0type);
                    }
                }
                else
                {
                    if (null == argval)
                    {
                        argval = tools.AllocValue(arg0type.ID);
                    }
                    argval.SetValue(arg0);
                    compareargs[0] = argval;
                    compareargs[1] = highest;
                    if (null == highest || tools.GetInt(DbFunctions.COMPARE(tools, compareargs)) > 0)
                    {
                        highest = argval; // Keep this one.
                        argval = null; // New one next time.
                    }
                }
            }
            if (null == highest)
            {
                if (null == nullest)
                {
                    return tools.AllocNullValue();
                }
                return nullest;
            }
            return highest;

        }
Exemple #11
0
        public static DbValue MAX(DbFunctionTools tools, DbAggregatorArguments args)
        {
            string AggregatorName = "MAX";

            DbValue             highest     = null;
            DbValue             nullest     = null;
            DbFunctionArguments compareargs = new DbFunctionArguments(new DbValue[2]);
            ImmediateValue      argval      = null;

            for (int iarg = 0; iarg < args.Length; iarg++)
            {
                args[iarg].EnsureCount(AggregatorName, 1);
                DbType    arg0type;
                ByteSlice arg0 = args[iarg][0].Eval(out arg0type);
                if (Types.IsNullValue(arg0))
                {
                    if (null == nullest)
                    {
                        nullest = tools.AllocValue(arg0, arg0type);
                    }
                }
                else
                {
                    if (null == argval)
                    {
                        argval = tools.AllocValue(arg0type.ID);
                    }
                    argval.SetValue(arg0);
                    compareargs[0] = argval;
                    compareargs[1] = highest;
                    if (null == highest || tools.GetInt(DbFunctions.COMPARE(tools, compareargs)) > 0)
                    {
                        highest = argval; // Keep this one.
                        argval  = null;   // New one next time.
                    }
                }
            }
            if (null == highest)
            {
                if (null == nullest)
                {
                    return(tools.AllocNullValue());
                }
                return(nullest);
            }
            return(highest);
        }
        public static DbValue CHOOSERND(DbFunctionTools tools, DbAggregatorArguments args)
        {
            string AggregatorName = "CHOOSERND";

            if (args.Length == 0)
            {
                return tools.AllocNullValue();
            }
            if (anyRnd == -1)
            {
                Random rnd = new Random(System.DateTime.Now.Millisecond / 2 + System.Diagnostics.Process.GetCurrentProcess().Id / 2);
                anyRnd = rnd.Next();
            }
            int index = anyRnd % args.Length;
            args[index].EnsureCount(AggregatorName, 1);
            return args[index][0];
        }
        private static double _VAR(string aggregatorName, DbFunctionTools tools, DbAggregatorArguments args, bool sample)
        {
            List<double> values = new List<double>();
            double x = 0;
            double sum = 0;
            for (int iarg = 0; iarg < args.Length; iarg++)
            {
                args[iarg].EnsureCount(aggregatorName, 1);
                DbType arg0type;
                ByteSlice arg0 = args[iarg][0].Eval(out arg0type);
                if (!Types.IsNullValue(arg0))   //ignore null
                {
                    switch (arg0type.ID)
                    {
                        case DbTypeID.INT:
                            x = (double)tools.GetInt(arg0);
                            break;

                        case DbTypeID.LONG:
                            x = (double)tools.GetLong(arg0);
                            break;

                        case DbTypeID.DOUBLE:
                            x = tools.GetDouble(arg0);
                            break;

                        default:
                            args[iarg].Expected(aggregatorName, 0, "input INT, LONG or DOUBLE, got " + arg0type.Name.ToUpper());
                            return 0; // Doesn't reach here.
                    }
                    values.Add(x);
                    sum += x;
                }
            }
            double dev = 0;
            if (values.Count > 0)
            {
                double avg = sum / (double)values.Count;
                for (int i = 0; i < values.Count; i++)
                {
                    dev += Math.Pow(values[i] - avg, 2);
                }
                dev = dev / (double)(sample ? values.Count - 1 : values.Count);
            }
            return dev;
        }
Exemple #14
0
        public static DbValue CHOOSERND(DbFunctionTools tools, DbAggregatorArguments args)
        {
            string AggregatorName = "CHOOSERND";

            if (args.Length == 0)
            {
                return(tools.AllocNullValue());
            }
            if (anyRnd == -1)
            {
                Random rnd = new Random(System.DateTime.Now.Millisecond / 2 + System.Diagnostics.Process.GetCurrentProcess().Id / 2);
                anyRnd = rnd.Next();
            }
            int index = anyRnd % args.Length;

            args[index].EnsureCount(AggregatorName, 1);
            return(args[index][0]);
        }
        public static DbValue COUNT(DbFunctionTools tools, DbAggregatorArguments args)
        {
            string AggregatorName = "COUNT";

            long count = 0;
            for (int iarg = 0; iarg < args.Length; iarg++)
            {
                args[iarg].EnsureCount(AggregatorName, 1);
                DbType arg0type;
                ByteSlice arg0 = args[iarg][0].Eval(out arg0type);
                if (Types.IsNullValue(arg0))    //ignore null
                {
                    continue;
                }
                count++;
            }
            return tools.AllocValue(count);
        }
Exemple #16
0
        public static DbValue AVG(DbFunctionTools tools, DbAggregatorArguments args)
        {
            string AggregatorName = "AVG";

            double sum   = 0;
            int    count = 0;

            for (int iarg = 0; iarg < args.Length; iarg++)
            {
                args[iarg].EnsureCount(AggregatorName, 1);
                DbType    arg0type;
                ByteSlice arg0 = args[iarg][0].Eval(out arg0type);
                if (!Types.IsNullValue(arg0))   //ignore null
                {
                    count++;
                    switch (arg0type.ID)
                    {
                    case DbTypeID.INT:
                        sum += (double)tools.GetInt(arg0);
                        break;

                    case DbTypeID.LONG:
                        sum += (double)tools.GetLong(arg0);
                        break;

                    case DbTypeID.DOUBLE:
                        sum += tools.GetDouble(arg0);
                        break;

                    default:
                        args[iarg].Expected(AggregatorName, 0, "input INT, LONG or DOUBLE, got " + arg0type.Name.ToUpper());
                        return(null);    // Doesn't reach here.
                    }
                }
            }
            double avg = 0;

            if (count > 0)
            {
                avg = sum / (double)count;
            }
            return(tools.AllocValue(avg));
        }
Exemple #17
0
        public static DbValue COUNT(DbFunctionTools tools, DbAggregatorArguments args)
        {
            string AggregatorName = "COUNT";

            long count = 0;

            for (int iarg = 0; iarg < args.Length; iarg++)
            {
                args[iarg].EnsureCount(AggregatorName, 1);
                DbType    arg0type;
                ByteSlice arg0 = args[iarg][0].Eval(out arg0type);
                if (Types.IsNullValue(arg0))    //ignore null
                {
                    continue;
                }
                count++;
            }
            return(tools.AllocValue(count));
        }
        public static DbValue AVG(DbFunctionTools tools, DbAggregatorArguments args)
        {
            string AggregatorName = "AVG";

            double sum = 0;
            int count = 0;
            for (int iarg = 0; iarg < args.Length; iarg++)
            {
                args[iarg].EnsureCount(AggregatorName, 1);
                DbType arg0type;
                ByteSlice arg0 = args[iarg][0].Eval(out arg0type);
                if (!Types.IsNullValue(arg0))   //ignore null
                {
                    count++;
                    switch (arg0type.ID)
                    {
                        case DbTypeID.INT:
                            sum += (double)tools.GetInt(arg0);
                            break;

                        case DbTypeID.LONG:
                            sum += (double)tools.GetLong(arg0);
                            break;

                        case DbTypeID.DOUBLE:
                            sum += tools.GetDouble(arg0);
                            break;

                        default:
                            args[iarg].Expected(AggregatorName, 0, "input INT, LONG or DOUBLE, got " + arg0type.Name.ToUpper());
                            return null; // Doesn't reach here.
                    }
                }
            }
            double avg = 0;
            if (count > 0)
            {
                avg = sum / (double)count;
            }
            return tools.AllocValue(avg);   
        }
Exemple #19
0
        public static DbValue COUNTDISTINCT(DbFunctionTools tools, DbAggregatorArguments args)
        {
            string AggregatorName = "COUNTDISTINCT";

            Dictionary <ByteSlice, short> dict = new Dictionary <ByteSlice, short>(new ByteSliceEqualityComparer());

            for (int iarg = 0; iarg < args.Length; iarg++)
            {
                args[iarg].EnsureCount(AggregatorName, 1);
                DbType    arg0type;
                ByteSlice arg0 = args[iarg][0].Eval(out arg0type);
                if (Types.IsNullValue(arg0))    //ignore null
                {
                    continue;
                }

                if (!dict.ContainsKey(arg0))
                {
                    dict.Add(arg0, 0);
                }
            }
            return(tools.AllocValue((long)dict.Count));
        }
 public static DbValue BIT_OR(DbFunctionTools tools, DbAggregatorArguments args)
 {
     return _BITWISE("BIT_OR", tools, args, 2);
 }
Exemple #21
0
 public static DbValue STD(DbFunctionTools tools, DbAggregatorArguments args)
 {
     return(tools.AllocValue(_STD("STD", tools, args, false)));
 }
Exemple #22
0
 private static double _STD(string aggregatorName, DbFunctionTools tools, DbAggregatorArguments args, bool sample)
 {
     return(Math.Sqrt(_VAR(aggregatorName, tools, args, sample)));
 }
Exemple #23
0
        private static DbValue _BITWISE(string aggregatorName, DbFunctionTools tools, DbAggregatorArguments args, int whatop)
        {
            if (args.Length == 0)
            {
                return(tools.AllocNullValue());
            }

            DbType arg0type = DbType.PrepareNull();

            for (int iarg = 0; iarg < args.Length; iarg++)
            {
                args[iarg].EnsureCount(aggregatorName, 1);
                ByteSlice arg0 = args[iarg][0].Eval(out arg0type);
                if (Types.IsNullValue(arg0))
                {
                    return(tools.AllocNullValue());
                }

                if (bitopbuf == null || bitopbuf.Length != arg0type.Size)
                {
                    bitopbuf = new byte[arg0type.Size];
                }

                if (iarg == 0)
                {
                    for (int ib = 0; ib < arg0.Length; ib++)
                    {
                        bitopbuf[ib] = arg0[ib];
                    }
                    continue;
                }

                for (int ib = 1; ib < arg0.Length; ib++)
                {
                    switch (whatop)
                    {
                    case 1:
                        bitopbuf[ib] = (byte)(bitopbuf[ib] & arg0[ib]);
                        break;

                    case 2:
                        bitopbuf[ib] = (byte)(bitopbuf[ib] | arg0[ib]);
                        break;

                    default:
                        bitopbuf[ib] = (byte)(bitopbuf[ib] ^ arg0[ib]);
                        break;
                    }
                }
            }
            ByteSlice bs = ByteSlice.Prepare(bitopbuf);

            return(tools.AllocValue(bs, arg0type));
        }
 public static DbValue VAR_POP(DbFunctionTools tools, DbAggregatorArguments args)
 {
     return tools.AllocValue(_VAR("VAR_POP", tools, args, false));
 }
 public static DbValue BIT_OR(DbFunctionTools tools, DbAggregatorArguments args)
 {
     return(_BITWISE("BIT_OR", tools, args, 2));
 }
 public static DbValue STD_SAMP(DbFunctionTools tools, DbAggregatorArguments args)
 {
     return tools.AllocValue(_STD("STD", tools, args, true));
 }
Exemple #27
0
 public static DbValue VAR_SAMP(DbFunctionTools tools, DbAggregatorArguments args)
 {
     return(tools.AllocValue(_VAR("VAR_SAMP", tools, args, true)));
 }
 private static double _STD(string aggregatorName, DbFunctionTools tools, DbAggregatorArguments args, bool sample)
 {
     return Math.Sqrt(_VAR(aggregatorName, tools, args, sample));
 }
Exemple #29
0
        // Returns null if no such function.
        public static DbValue TryExecDbAggregator(string name, DbFunctionTools tools, DbAggregatorArguments args)
        {
            EnsureDbAggregators();

            if (!DbAggregatorExists(name))
            {
                return(null);
            }

            DbAggregatorDelegate dbagg = dbaggregators[name];

            return(dbagg(tools, args));
        }