Пример #1
0
        public void SimpleSelect()
        {
            EvalStore c = new EvalStore();
            Parser p = new Parser();
            Expression s;
            Expression r;

            s = p.Parse("select '1', 2, 3");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1, 2, 3]");

            s = p.Parse("select 1+2, 1+3, 1+4");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[3, 4, 5]");

            s = p.Parse("select 1+e, pi+e, 100*pi");
            c.Enter();
            c.Put<Element>("pi", new Element(3.13149265));
            c.Put<Element>("e", new Element(2.71828183));
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[3.71828183, 5.84977448, 313.149265]");

            s = p.Parse("select '1+1' as c1, 2+2 as c2, 3+3 as c3");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            //Assert.AreEqual(r.ToString(), "[1, 2, 3]");
        }
Пример #2
0
 public Table Execute(string query)
 {
     try {
         _dataLock.EnterReadLock();
         Parser p = new Parser();
         Expression s = p.Parse(query);
         EvalStore store = new EvalStore();
         store.Enter();
         store.Put<Table>(_facts.Name, _facts);
         store.Put<Table>(_attributes.Name, _attributes);
         store.Put<Table>(_resources.Name, _resources);
         store.Put<Table>(_contexts.Name, _contexts);
         ConstExpr c = s.Evaluate(store) as ConstExpr;
         store.Exit();
         Table result = c.Value.GetObject<Table>();
         return result;
     }
     catch (Exception ex) {
         Trace.WriteLine(ex.Message);
         return null;
     }
     finally {
         _dataLock.ExitReadLock();
     }
 }
Пример #3
0
        public override Expression Evaluate(EvalStore store)
        {
            Table data = store.Get<Table>("$DATA");
            if (data == null || data.Rows == 0) return ConstExpr.Empty;
            int limit;
            int offset=0;

            Expression e = _expr.Evaluate(store);
            if (e.ExpressionType != Language.ExpressionType.CONST) throw new Exception("LIMIT number must be evaluated to an integer.");
            Element l = (e as ConstExpr).Value;
            if (l.DataType != ElementType.LONG) throw new Exception("LIMIT number must be evaluated to an integer.");
            limit = l.ToInt32(null);
            if (limit < 1) throw new Exception("LIMIT must be an positive integer greater than 1.");

            Element o = null;
            if (this.Offset != null) {
                e = _expr.Evaluate(store);
                if (e.ExpressionType != Language.ExpressionType.CONST) throw new Exception("OFFSET number must be evaluated to an integer.");
                o = (e as ConstExpr).Value;
                if (o.DataType != ElementType.LONG) throw new Exception("OFFSET number must be evaluated to an integer.");
                offset = o.ToInt32(null);
                if (offset < 0) throw new Exception("OFFSET must be an positive integer.");
            }

            int end = offset + limit;
            if (end > data.Rows) end = data.Rows;
            List<Element[]> rows = new List<Element[]>();
            for (int i = offset; i < end; i++) {
                rows.Add(data.Row(i));
            }
            Table r = new Table(data.Name, data.ColumnDefinitions, rows);
            store.Put<Table>("$DATA", r);
            return new ConstExpr(r);
        }
Пример #4
0
        public override Expression Evaluate(EvalStore store)
        {
            Table data = store.Get<Table>("$DATA");
            List<List<int>> groups = store.Get<List<List<int>>>("$GROUPS");
            if (groups == null || groups.Count == 0) return ConstExpr.Empty;

            List<List<int>> newGroups = new List<List<int>>();
            using (EvalScope scope = new EvalScope(store, new EvalContext(EvalContext.ContextType.HAVING))) {
                if (!SelectClause.HasAggregate(_expr)) return ConstExpr.Empty;
                foreach (var g in groups) {
                    store.Put<List<int>>("$GROUP", g);
                    ConstExpr c = (ConstExpr)_expr.Evaluate(store);
                    if ((bool)c.Value) {
                        newGroups.Add(g);
                    }
                }
            }
            store.Put("$GROUPS", newGroups);
            return ConstExpr.Empty;
        }
Пример #5
0
        public void LikeOperator()
        {
            EvalStore c = new EvalStore();
            Parser p = new Parser();
            Expression s;
            Expression r;

            s = p.Parse("select 'rosemary' like '%rose%'");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select 'rosemary' like '%rose'");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select 'rosemary' like 'mary%'");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select not ('rosemary' like 'lamb%')");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select 3.13149265 like '3.13149265')");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select 3.13149265 like pi)");
            c.Enter();
            c.Put<Element>("pi", new Element(3.13141592653589793238462));
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select 3.13149265+1 like pi+1)");
            c.Enter();
            c.Put<Element>("pi", new Element(3.13141592653589793238462));
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");
        }
Пример #6
0
        public void SimpleSelectTest()
        {
            StringBuilder src = new StringBuilder();
            src.AppendLine("select 3.1314+3");
            Parser p = new Parser();
            Expression s = p.Parse(src.ToString());
            EvalStore c = new EvalStore();
            Expression r = s.Evaluate(c);

            src = new StringBuilder();
            src.AppendLine("select 123+pi");
            p = new Parser();
            s = p.Parse(src.ToString());
            c = new EvalStore();
            c.Enter();
            c.Put<Element>("pi", new Element(3.13149265358));
            r = s.Evaluate(c);
            c.Exit();
        }
Пример #7
0
        private string CreateContextFilter(EvalStore store, Dictionary<int, List<Expression>> exprGrp)
        {
            Dictionary<string, Table> sel = new Dictionary<string, Table>();

            if (exprGrp[CONTEXT_FILTER].Count == 0) return null;
            Expression expr = exprGrp[CONTEXT_FILTER][0];

            Table contexts = store.Get<Table>("Contexts");
            sel.Add("_", contexts);

            using (EvalScope scope = new EvalScope(store, new EvalContext(EvalContext.ContextType.SEARCH))) {
                store.Put<Dictionary<string, Table>>("$SEL_FACTS", sel);
                Expression e = expr.Evaluate(store);
                if (e.ExpressionType != Language.ExpressionType.CONST) {
                    throw new Exception("The expression should be evaluated to a value. " + e.ToString());
                }
                ConstExpr c = e as ConstExpr;
                if (c.Value.DataType == ElementType.TABLE) {
                    Table s = c.Value.GetObject<Table>();
                    List<string> ctxIds = new List<string>();
                    int ctxIdCol = s.ColumnDefinitions.IndexOf("CntxID");
                    for (int i = 0; i < s.Rows; i++) {
                        ctxIds.Add((string)s[i, ctxIdCol]);
                    }
                    if (ctxIds.Count == 1) {
                        return "ContextID=" + ctxIds[0].ToString();
                    }
                    else if (ctxIds.Count > 0) {
                        return "ContextID in (" + string.Join(",", ctxIds.ToArray()) + ")";
                    }
                    return null;

                }
                else {
                    if (c.Value.DataType != ElementType.EMPTY) {
                        throw new Exception("The expression should be evaluated to a set of rows or an empty set. " + e.ToString());
                    }
                }
            }
            return null;
        }
Пример #8
0
        public void SimpleSelectFrom()
        {
            EvalStore c = new EvalStore();
            Parser p = new Parser();
            Expression e;
            Expression r;

            Stopwatch watch = Stopwatch.StartNew();

            ContextTable contexts = new ContextTable();
            contexts.Load();

            AttributeTable attributes = new AttributeTable();
            attributes.Load();

            FactTable fact = new FactTable();
            fact.Load();

            watch.Stop();
            Trace.WriteLine("LoadStaticData              : " + watch.ElapsedMilliseconds + "ms");

            StringBuilder q = new StringBuilder();

            //q.AppendLine("SELECT Context, time, format('%2.2f', avg(value)), format('%2.2f', min(value)), format('%2.2f', max(value)), format('%2.2f', stdv(value)), count(value), HostName");
            //q.AppendLine("FROM 277");
            //q.AppendLine("WHERE time between now()-5d and now(), ResID=33, Context='C:' or Context='D:'");
            //q.AppendLine("GROUP BY Context, time(24h)");
            //q.AppendLine("ORDER by Context, time");
            //q.AppendLine("LIMIT 30");

            //q.AppendLine("FROM 'growth_rate_running_average' as V, 'growth_space' as V, 'journal_file_size' as V, 'journal_secondary_directory' as V");

            q.AppendLine("SELECT HostName, AttrName, time, format('%2.2f',avg(value))");
            q.AppendLine("FROM /^CPU/");
            q.AppendLine("WHERE time BETWEEN now(-1d) AND now()");
            q.AppendLine("GROUP BY HostName, AttrName, time(24h)");
            q.AppendLine("HAVING avg(value)>30");
            //q.AppendLine("ORDER BY HostName DESC, AttrName, time DESC");
            //q.AppendLine("LIMIT 10 OFFSET 5");

            //q.AppendLine("SELECT AttrID, time, min(value), max(value), mean(value), stdv(value)");
            //q.AppendLine("FROM 79,80");
            //q.AppendLine("WHERE time BETWEEN now(-7d) AND now(), ResID=15");
            //q.AppendLine("GROUP BY AttrID, time(4h)");
            //q.AppendLine("ORDER BY AttrID, time DESC");

            //q.AppendLine("SELECT Context, time,  AVG(value)");
            //q.AppendLine("FROM \*\");
            //q.AppendLine("WHERE time >= now()-5d and time < now(), ResID=23, AttrID=4125 | AttrID=123, hour(time) between '08' and '21' ");
            //q.AppendLine("GROUP BY Context, time(1d)");

            //q.AppendLine("SELECT Context, time,  AVG(value)");
            //q.AppendLine("FROM \^CPU\");
            //q.AppendLine("WHERE time >= now()-5d and time < now(), ResID=23, AttrID=4125 | AttrID=123, hour(time) between '08' and '21' ");
            //q.AppendLine("GROUP BY AttrID, Context, time(1d)");

            //q.AppendLine("GROUP BY time(1d)");
            // show peak avg group by day of the week for the past 30 day
            // peak time range
            // average on all Mondays, Tues, ..., of a month
            // concepts day of week
            // typical 9-noon monday cpu 50%
            //q.AppendLine("SELECT time, AVG(value)");
            //q.AppendLine("FROM 101");
            //q.AppendLine("WHERE time >= now()-10d and time < now(), ResID=3");
            //q.AppendLine("GROUP BY time(4h)");

            //q.AppendLine("SELECT time, AVG(value)");
            //q.AppendLine("FROM 365,359");
            //q.AppendLine("WHERE time >= now()-10d and time < now(), ResID=3");
            //q.AppendLine("GROUP BY HostName, time(1d)");

            e = p.Parse(q.ToString());

            c.Enter();
            c.Put<Table>(fact.Name, fact);
            c.Put<Table>(attributes.Name, attributes);
            c.Put<Table>(contexts.Name, contexts);

            //Dictionary<string, string> validFields = new Dictionary<string, string>();
            //for (int i = 0; i < fact.Cols; i++) {
            //	validFields.Add(fact.Schema[i].Name, null);
            //}

            //validFields.Add("contextid", null);
            //validFields.Add("time", null);
            //validFields.Add("value", null);
            //validFields.Add("severity", null);
            //c.Add(SelectStatement.VALID_FIELDS_KEY, validFields);

            r = e.Evaluate(c);
            Table t = (Table) r.Value;
            List<string> data = t.Print();
            for (int i = 0; i < data.Count; i++) {
                Trace.WriteLine(data[i]);
            }
            c.Exit();
        }
Пример #9
0
        private void InitializeData(Object threadContext)
        {
            Stopwatch watch = Stopwatch.StartNew();

            _parser = new Parser();
            _store = new EvalStore();
            StringBuilder cmd = new StringBuilder();

            _attributes = new AttributeTable();
            _attributes.Load();
            statusStrip.Text = "Loading Attributes...";

            _facts = new FactTable();
            _facts.Load();
            statusStrip.Text = "Loading Facts...";

            _contexts = new ContextTable();
            _contexts.Load();
            statusStrip.Text = "Loading Contexts...";

            _resources = new ResourceTable();
            _resources.Load();
            statusStrip.Text = "Loading Resources...";

            watch.Stop();

            _popupMenu = new AutocompleteMenu(queryCode);
            _popupMenu.SearchPattern = @"[\w\.:=!<>]";
            _popupMenu.AllowTabKey = true;

            List<AutocompleteItem> items = new List<AutocompleteItem>();
            for (int i = 0; i < _facts.ColumnDefinitions.Count; i++) {
                items.Add(new AutocompleteItem(_facts.ColumnDefinitions[i].Name));
            }

            items.Add(new AutocompleteItem("select"));
            items.Add(new AutocompleteItem("where"));
            items.Add(new AutocompleteItem("order"));
            items.Add(new AutocompleteItem("group"));
            items.Add(new AutocompleteItem("by"));
            items.Add(new AutocompleteItem("limit"));
            items.Add(new AutocompleteItem("desc"));
            items.Add(new AutocompleteItem("asc"));
            items.Add(new AutocompleteItem("between"));

            for (int i=0; i<_resources.Cols; i++) {
                items.Add(new AutocompleteItem(_resources.ColumnDefinitions[i].Name));
            }

            items.Add(new AutocompleteItem("Context"));
            items.Add(new AutocompleteItem("value"));
            items.Add(new AutocompleteItem("time"));

            foreach (string func in _parser.GetFunctionNames()) {
                SnippetAutocompleteItem n = new SnippetAutocompleteItem(func + "(^)");
                n.ToolTipText = n.Text;
                n.ToolTipTitle = "";
                items.Add(n);
            }

            _popupMenu.Items.SetAutocompleteItems(items);
            queryCode.Language = FastColoredTextBoxNS.Language.SQL;

            _store.Enter();
            _store.Put<Element>("pi", new Element(3.131492653586));
            _store.Put<Element>("e", new Element(2.71828183));
            _store.Put<Table>(_facts.Name, _facts);
            _store.Put<Table>(_attributes.Name, _attributes);
            _store.Put<Table>(_contexts.Name, _contexts);
            _store.Put<Table>(_resources.Name, _resources);

            Trace.WriteLine("InitializeData              : " + watch.ElapsedMilliseconds + "ms");
            toolStrip1.Enabled = true;
            statusStrip.Text = "OK";
        }
Пример #10
0
        public void IsOperator()
        {
            EvalStore c = new EvalStore();
            Parser p = new Parser();
            Expression s;
            Expression r;

            s = p.Parse("select 1 IS NULL");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[0]");

            s = p.Parse("select 1 IS NOT NULL");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select NULL is NULL");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select !3.1314");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[0]");

            s = p.Parse("select NULL is NOT NULL");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[0]");

            s = p.Parse("select x is NULL");
            c.Enter();
            c.Put<Element>("x", Element.Empty);
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select x+1 is NULL");
            c.Enter();
            c.Put<Element>("x", Element.Empty);
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select x+1 ==1");
            c.Enter();
            c.Put<Element>("x", Element.Empty);
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[NULL]");

            s = p.Parse("select x < 1 is NULL");
            c.Enter();
            c.Put<Element>("x", Element.Empty);
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select +x is NULL");
            c.Enter();
            c.Put<Element>("x", Element.Empty);
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[0]");

            s = p.Parse("select (0 AND 0) is false");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select (0 AND 1) is true");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[0]");

            s = p.Parse("select (1 AND 0) is false");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select (1 AND 1) is true");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select (0 AND NULL) is false");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select (1 AND NULL) is null");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select (NULL AND 0) is not null");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select (NULL AND 1) is null");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select (NULL AND NULL) is not null");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[0]");
        }
Пример #11
0
        public void EqualOperator()
        {
            EvalStore c = new EvalStore();
            Parser p = new Parser();
            Expression s;
            Expression r;

            s = p.Parse("select 1=1");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select 1==1");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select 1==\"1\"");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select 1=='2'");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[0]");

            s = p.Parse("select 3.131492==3.131492");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select 1==1 OR 2==1");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select 1==1 AND 2==1");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[0]");

            s = p.Parse("select 1==1 AND 2==2 AND 3==3");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select 1==NULL");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[NULL]");

            s = p.Parse("select NULL==1");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[NULL]");

            s = p.Parse("select +NULL==0");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select +NULL==0+NULL");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[NULL]");

            s = p.Parse("select 33+1==32+2");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select 4*4==2*8");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select pi==3.131492");
            c.Enter();
            c.Put<Element>("pi", new Element(3.131492));
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select pi==3.1314926");
            c.Enter();
            c.Put<Element>("pi", new Element(3.131492));
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[0]");

            s = p.Parse("select -pi==-3.131492");
            c.Enter();
            c.Put<Element>("pi", new Element(3.131492));
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select pi-pi=0");
            c.Enter();
            c.Put<Element>("pi", new Element(3.131492));
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select 3.13149265+1 == pi+1)");
            c.Enter();
            c.Put<Element>("pi", new Element(3.131492));
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");
        }
Пример #12
0
        //static MemoryStream CreateStringStream(string value)
        //{
        //    byte[] bytes = System.Text.Encoding.UTF8.GetBytes(value);
        //    MemoryStream ms = new MemoryStream();
        //    ms.Write(bytes, 0, bytes.Length);
        //    ms.Seek(0, SeekOrigin.Begin);
        //    return ms;
        //}
        //static void TestParser()
        //{
        //    Console.WriteLine("Enter statements followed by a ; and enter 'bye' to exit:");
        //    while (true) {
        //        StringBuilder sb = new StringBuilder();
        //        while (true) {
        //            string line = Console.ReadLine();
        //            if (line.ToUpper() == "BYE") return;
        //            if (line == ";") break;
        //            if (line.EndsWith(";")) {
        //                sb.AppendLine(line.Substring(0, line.Length - 1));
        //                break;
        //            }
        //            sb.AppendLine(line);
        //        }
        //        using (MemoryStream ms = CreateStringStream(sb.ToString())) {
        //            Parser p = new Parser();
        //            //Expression e = p.GetExpression();
        //        }
        //    }
        //}
        //static string indent(int level)
        //{
        //    string x = "";
        //    for (int i = 0; i < level; i++) {
        //        x = x + " ";
        //    }
        //    return x;
        //}
        static void TestParser()
        {
            Parser p = new Parser();
            EvalStore c = new EvalStore();
            StringBuilder cmd = new StringBuilder();

            Stopwatch watch = Stopwatch.StartNew();

            ContextTable contexts = new ContextTable();
            contexts.Load();

            AttributeTable attributes = new AttributeTable();
            attributes.Load();

            FactTable fact = new FactTable();
            fact.Load();

            watch.Stop();
            Trace.WriteLine("LoadStaticData              : " + watch.ElapsedMilliseconds + "ms");

            try {

                c.Enter();
                c.Put<Element>("pi", new Element(3.131492653586));
                c.Put<Element>("e", new Element(2.71828183));

                c.Put<Table>(fact.Name, fact);
                c.Put<Table>(attributes.Name, attributes);
                c.Put<Table>(contexts.Name, contexts);

                while (true) {

                    while (true) {
                        string v = Console.ReadLine();
                        v = v.Trim();
                        if (v.StartsWith(".")) {
                            v = v.ToUpper();
                            switch (v) {
                                case ".EXIT":
                                    return;
                            }
                        }

                        if (v.EndsWith(";")) {
                            v = v.Substring(0, v.Length - 1);
                            cmd.AppendLine(v);
                            break;
                        }

                        cmd.AppendLine(v);
                    }

                    string query = cmd.ToString();
                    cmd.Clear();

                    Expression s = null;
                    try {
                        s = p.Parse(query);
                    }
                    catch (Exception ex) {
                        Console.WriteLine("Invalid syntax: " + ex.Message);
                    }

                    Expression r = null;
                    try {
                        r = s.Evaluate(c);
                        Table t = (Table) r.Value;
                        List<string> data = t.Print();
                        for (int i = 0; i < data.Count; i++) {
                            Console.WriteLine(data[i]);
                        }
                    }
                    catch (Exception ex) {
                        Console.WriteLine("Error evaluating statement: " + ex.Message);
                    }
                    //Console.WriteLine(r);
                }
            }
            finally {
                c.Exit();
            }
        }
Пример #13
0
        public override Expression Evaluate(EvalStore store)
        {
            Table data = store.Get<Table>("$DATA");

            if (data == null) return ConstExpr.Empty;

            if (data.Rows == 0) return new ConstExpr(data);

            List<Mapper> mappers = new List<Mapper>();

            foreach (Expression expr in _exprs) {
                switch (expr.ExpressionType) {
                    case Language.ExpressionType.ID: {
                            int col = data.ColumnDefinitions.IndexOf((expr as IdentifierExpr).Name);
                            Mapper m = new Mapper(col);
                            mappers.Add(m);
                        }
                        break;
                    case Language.ExpressionType.FUNCTION: {
                            FuncExpr f = expr as FuncExpr;
                            int col = data.ColumnDefinitions.IndexOf(f.Name.ToLower());
                            Expression c = f.Arguments[0].Evaluate(store);
                            TimeMapper m = new TimeMapper(col, c.Value);
                            mappers.Add(m);
                        }
                        break;
                }
            }

            List<int> p = new List<int>();
            for (int i = 0; i < data.Rows; i++) { p.Add(i); }

            List<List<int>> groups = CreateGroups(data, p, mappers);

            store.Put<List<List<int>>>("$GROUPS", groups);

            if (_havingClause != null) {
                _havingClause.Evaluate(store);
            }

            return new ConstExpr(data);
        }
Пример #14
0
        public override Expression Evaluate(EvalStore store)
        {
            try {
                Stopwatch w;

                store.Enter();

                HashSet<string> refIds = FindReferencedIds();

                store.Put<Element>("$NOW", new Element(DateTime.Now));
                store.Put<EvalContext>("$EVALPHASE", new EvalContext());
                store.Put<HashSet<string>>("$REFIDS", refIds);

                //evaluate the from clause to find out a list of attributes to load
                if (_fromClause != null) {
                    w = Stopwatch.StartNew();
                    _fromClause.Evaluate(store);
                    w.Stop();
                    Trace.WriteLine("select.from : " + w.ElapsedMilliseconds + "ms");
                }

                //evaluate the where clause to narrow down the target attributes and time range
                if (_whereClause != null) {
                    if (_fromClause == null) {
                        throw new Exception("Syntax error: missing from clause");
                    }
                    w = Stopwatch.StartNew();
                    Expression c = _whereClause.Evaluate(store);
                    w.Stop();
                    Trace.WriteLine("select.where : " + w.ElapsedMilliseconds + "ms");
                }

                //create the master table that has all the referenced columns
                w = Stopwatch.StartNew();
                Table data = store.Get<Table>("$DATA");
                Table facts = store.Get<Table>("Facts");

                data = CreateMasterDataTable(data, facts, refIds);
                store.Put<Table>("$DATA", data);

                w.Stop();
                Trace.WriteLine("select.CreateMasterDataTable : " + w.ElapsedMilliseconds + "ms");

                if (_groupByClause != null) {
                    w = Stopwatch.StartNew();
                    _groupByClause.Evaluate(store);
                    w.Stop();
                    Trace.WriteLine("select.group_by : " + w.ElapsedMilliseconds + "ms");
                }

                Expression ret;
                w = Stopwatch.StartNew();
                ret = _selectClause.Evaluate(store);
                w.Stop();
                Trace.WriteLine("select.select : " + w.ElapsedMilliseconds + "ms");

                if (_orderByClause != null) {
                    w = Stopwatch.StartNew();
                    ret = _orderByClause.Evaluate(store);
                    w.Stop();
                    Trace.WriteLine("select.order_by : " + w.ElapsedMilliseconds + "ms");
                }

                if (_limitClause != null) {
                    w = Stopwatch.StartNew();
                    ret =_limitClause.Evaluate(store);
                    w.Stop();
                    Trace.WriteLine("select.limit : " + w.ElapsedMilliseconds + "ms");
                }

                return ret;
            }
            catch (Exception ex) {
                throw new InvalidOperationException("Error executing select: " + ex.Message, ex);
            }
            finally {
                store.Exit();
            }
        }
Пример #15
0
        public void PlusOperator()
        {
            EvalStore c = new EvalStore();
            Parser p = new Parser();
            Expression s;
            Expression r;

            p = new Parser();
            s = p.Parse("select 1+1");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[2]");

            s = p.Parse("select 1+(-1)");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[0]");

            s = p.Parse("select 1+!(!0)+1");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[2]");

            s = p.Parse("select pi+e");
            c.Enter();
            c.Put<Element>("pi", new Element(3.13141592653589793238462));
            c.Put<Element>("e", new Element(2.718281828459045235360));
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[5.85987448204884]");

            s = p.Parse("select +e");
            c.Enter();
            c.Put<Element>("pi", new Element(3.13141592653589793238462));
            c.Put<Element>("e", new Element(2.718281828459045235360));
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[2.71828182845905]");

            s = p.Parse("select 1+e*pi");
            c.Enter();
            c.Put<Element>("pi", new Element(3.13141592653589793238462));
            c.Put<Element>("e", new Element(2.718281828459045235360));
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[9.53973422267357]");

            s = p.Parse("select " + int.MaxValue + "+1");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[2147483648]");

            s = p.Parse("select -1*5+3*!0*2+99");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[100]");

            s = p.Parse("select !(!(!0))*-1+99");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[98]");

            s = p.Parse("select 'cat' + 'dog'");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[catdog]");
        }
Пример #16
0
        public void UnaryPlusOperator()
        {
            EvalStore c = new EvalStore();
            Parser p = new Parser();
            Expression s;
            Expression r;

            p = new Parser();
            s = p.Parse("select +1");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select +1+(-1)");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[0]");

            s = p.Parse("select +(+(+1))");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select +pi+e");
            c.Enter();
            c.Put<Element>("pi", new Element(3.13141592653589793238462));
            c.Put<Element>("e", new Element(2.718281828459045235360));
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[5.85987448204884]");

            s = p.Parse("select +e");
            c.Enter();
            c.Put<Element>("pi", new Element(3.13141592653589793238462));
            c.Put<Element>("e", new Element(2.718281828459045235360));
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[2.71828182845905]");

            s = p.Parse("select  + '123'");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[123]");

            s = p.Parse("select  +d'01/01/2015'");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1420092000000]");

            s = p.Parse("select  +5d");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[432000000]");

            try {
                s = p.Parse("select  100+5d");
                c.Enter();
                r = s.Evaluate(c);
                Assert.Fail("no error thrown for select  100+5d");
            }
            catch (Exception ex) {
                Assert.AreEqual(ex.GetType(), typeof(InvalidOperationException));
                Assert.AreEqual(ex.Message, "Error executing select: Unsupported operand types for +: 'LONG' and 'TIMESPAN'");
            }
            finally {
                c.Exit();
            }

            s = p.Parse("select  100+(+5d)");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[432000100]");

            s = p.Parse("select  100+(+NULL)");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[100]");
        }
Пример #17
0
        public override Expression Evaluate(EvalStore store)
        {
            Stopwatch watch;

            store.Remove("$DATA");
            var facts = store.Get<Table>("Facts");
            var fromList = store.Get<Dictionary<string, Table>>("$FROM_LIST");

            if (fromList == null || fromList.Count == 0) {
                return ConstExpr.Empty;
            }

            HashSet<string> refIds = store.Get<HashSet<string>>("$REFIDS");

            var selFacts = SelectFactTableRows(fromList, facts);

            for (int i = 0; i < _exprs.Count; i++) {
                _exprs[i] = Reduce(store, _exprs[i]);
            }

            Dictionary<int, List<Expression>> exprGroups;
            Dictionary<string, Expression> idExprMap;
            NormalizeExpressions(selFacts, _exprs, out exprGroups, out idExprMap);

            //find out the time range
            if (exprGroups[TIME_FILTER].Count == 0) {
                throw new Exception("Time range expression is not specified.");
            }
            DateTime min = DateTime.MaxValue;
            DateTime max = DateTime.MinValue;
            DateTime[] range = ExtraTimeRange(store, exprGroups[TIME_FILTER][0]);
            if (range != null) {
                if (range[0] < min) min = range[0];
                if (range[1] > max) max = range[1];
            }
            else {
                throw new Exception("Time range is not specified.");
            }

            //find a list of data log by applying filters of various properties from the fact table
            Table sel = null;
            using (EvalScope scope = new EvalScope(store, new EvalContext(EvalContext.ContextType.SEARCH))) {
                if (exprGroups[ATTR_FILTER].Count == 0) {
                    sel = (Table) selFacts["_"];
                }
                else {
                    store.Put<Dictionary<string, Table>>("$SEL_FACTS", selFacts);
                    foreach (Expression expr in exprGroups[ATTR_FILTER]) {
                        Expression e = expr.Evaluate(store);
                        if (e.ExpressionType != Language.ExpressionType.CONST) {
                            throw new Exception("The expression should be evaluated to a value. " + e.ToString());
                        }
                        ConstExpr c = e as ConstExpr;
                        if (c.Value.DataType == ElementType.TABLE) {
                            Table s = c.Value.GetObject<Table>();
                            if (sel == null) sel = s;
                            else sel.IntersectWith(s);
                        }
                        else {
                            if (c.Value.DataType != ElementType.EMPTY) {
                                throw new Exception("The expression should be evaluated to a set of rows or an empty set. " + e.ToString());
                            }
                        }
                    }
                }
            }
            if (sel == null || sel.Rows == 0) return ConstExpr.Empty;

            //creating additional filters for the metric loading queries
            string resFilter = CreateResourceFilters(sel);
            string ctxFilter = CreateContextFilter(store, exprGroups);
            string filters;
            if (!string.IsNullOrEmpty(resFilter) && !string.IsNullOrEmpty(ctxFilter)) {
                filters = (resFilter + " AND " + ctxFilter);
            }
            else if (string.IsNullOrEmpty(resFilter)) {
                filters = ctxFilter;
            }
            else {
                filters = resFilter;
            }

            MetricsDataLoader dataLoader = new MetricsDataLoader();
            Table data = dataLoader.Load(sel, min, max, filters, refIds);
            Trace.WriteLine("WhereClase.Evaluate.TotalRows: " + data.Rows);

            //applies filters that involve result columns
            watch = Stopwatch.StartNew();
            if (exprGroups[OTHER_FILTER].Count > 0) {
                using (EvalScope scope = new EvalScope(store, new EvalContext(EvalContext.ContextType.FILTER))) {
                    store.Put<Table>("$DATA", data);
                    HashSet<int> selRows = new HashSet<int>();
                    for (int i = 0; i < data.Rows; i++) {
                        store.Put<Element[]>("$ROW", data.Row(i));
                        bool include = true;
                        foreach (Expression expr in exprGroups[OTHER_FILTER]) {
                            ConstExpr c = expr.Evaluate(store) as ConstExpr;
                            if (!((bool)c.Value)) {
                                include = false;
                                break;
                            }
                        }
                        if (include) {
                            selRows.Add(i);
                        }
                    }
                    data = data.Select(selRows);
                }
            }

            if (data == null) data = new Table();

            watch.Stop();
            Trace.WriteLine("WhereClase.Evaluate.Filter: " + watch.ElapsedMilliseconds + "ms");
            Trace.WriteLine("WhereClase.Evaluate.SelectedRows: " + data.Rows);

            store.Put<Table>("$DATA", data);
            return new ConstExpr(data);
        }
Пример #18
0
        public void NotEqualOperator()
        {
            EvalStore c = new EvalStore();
            Parser p = new Parser();
            Expression s;
            Expression r;

            s = p.Parse("select 1!=1");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[0]");

            s = p.Parse("select 1<>1");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[0]");

            s = p.Parse("select 1!=\"1\"");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[0]");

            s = p.Parse("select 1<>'2'");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select 3.131492<>3.131492");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[0]");

            s = p.Parse("select 1.112!=1.11111 OR 1.2223==1");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select 333<>'1' AND 2<>1.99999999");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select 1!=3 AND 2!=3 AND 3!=3");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[0]");

            s = p.Parse("select 1!=NULL");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[NULL]");

            s = p.Parse("select NULL<>1");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[NULL]");

            s = p.Parse("select +NULL!=0");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[0]");

            s = p.Parse("select +NULL!=0+NULL");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[NULL]");

            s = p.Parse("select 33+1!=32+2");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[0]");

            s = p.Parse("select 4*4==2*8.00000");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select pi!=3.131492");
            c.Enter();
            c.Put<Element>("pi", new Element(3.131492));
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[0]");

            s = p.Parse("select pi!=3.1314926");
            c.Enter();
            c.Put<Element>("pi", new Element(3.131492));
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select -pi!=-3.131492");
            c.Enter();
            c.Put<Element>("pi", new Element(3.131492));
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[0]");

            s = p.Parse("select pi-pi<>0");
            c.Enter();
            c.Put<Element>("pi", new Element(3.131492));
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[0]");
        }
Пример #19
0
        public void LessThanOperator()
        {
            EvalStore c = new EvalStore();
            Parser p = new Parser();
            Expression s;
            Expression r;

            s = p.Parse("select 1<1");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[0]");

            s = p.Parse("select 0<1");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select 0!<1");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[0]");

            s = p.Parse("select 0!>1");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select 10!>10");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select 10!<10");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select 0.5<\"1\"");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select 0.000009<'.000009001'");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select d'01/01/2015'<d'02/01/2015'");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select 30d<30m");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[0]");

            s = p.Parse("select 100<'1000' AND 2<>1.99999999");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select 1<3 AND 2<3 AND 10000000<10000001");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select 1<NULL");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[NULL]");

            s = p.Parse("select NULL<1");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[NULL]");

            s = p.Parse("select +NULL<1");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select 19<=19");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select 0<=1");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select 4*4<=2*2.00000*2*2.0");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select pi<3.131492");
            c.Enter();
            c.Put<Element>("pi", new Element(3.131492));
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[0]");

            s = p.Parse("select pi<3.1314926");
            c.Enter();
            c.Put<Element>("pi", new Element(3.131492));
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select -pi<pi");
            c.Enter();
            c.Put<Element>("pi", new Element(3.131492));
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select pi-pi<=0");
            c.Enter();
            c.Put<Element>("pi", new Element(3.131492));
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");
        }
Пример #20
0
        public void RegexMatchOperator()
        {
            EvalStore c = new EvalStore();
            Parser p = new Parser();
            Expression s;
            Expression r;

            s = p.Parse("select 'a123' =~ /a[1..3]+/");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select 'a11234' =~ pattern");
            c.Enter();
            c.Put<Element>("pattern", new Element("a[1..3]+"));
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select '1998' =~ 1900+98");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");

            s = p.Parse("select '3.131492' =~ 3.131492");
            c.Enter();
            r = s.Evaluate(c);
            c.Exit();
            Assert.AreEqual(r.ToString(), "[1]");
        }