public void ModOperator() { EvalStore c = new EvalStore(); Parser p = new Parser(); Expression s; Expression r; p = new Parser(); s = p.Parse("select 71 % 13"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[" + (71 % 13) + "]"); s = p.Parse("select d'01/01/2015 11:35:31' % 1m"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[00:00:31]"); s = p.Parse("select d'01/01/2015 11:35:31' % 1h"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[00:35:31]"); s = p.Parse("select d\"01/01/2015 11:35:31\" % 1d"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[11:35:31]"); }
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]"); }
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(); } }
public void ConcatnateOperator() { EvalStore c = new EvalStore(); Parser p = new Parser(); Expression s; Expression r; s = p.Parse("select 123 + 'abc'"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[123abc]"); s = p.Parse("select 'abc' + 123"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[abc123]"); }
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]"); }
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(); }
public void NowFunctionTest() { EvalStore c = new EvalStore(); Parser p = new Parser(); Expression s; Expression r; s = p.Parse("select now()"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[" + DateTime.Now.ToString() + "]"); s = p.Parse("select now()==now()"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[1]"); s = p.Parse("select now()-3d"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[" + (DateTime.Now - new TimeSpan(3,0,0,0)).ToString() + "]"); s = p.Parse("select now()+30d"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[" + (DateTime.Now + new TimeSpan(30, 0, 0, 0)).ToString() + "]"); s = p.Parse("select now()+1d*5"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[" + (DateTime.Now + new TimeSpan(5, 0, 0, 0)).ToString() + "]"); s = p.Parse("select now() between now()-3d/3 and now()+3d"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[1]"); }
public void LogicalNotOperator() { EvalStore c = new EvalStore(); Parser p = new Parser(); Expression s; Expression r; s = p.Parse("select !1"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[0]"); s = p.Parse("select !0"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[1]"); s = p.Parse("select !NULL"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[NULL]"); s = p.Parse("select !3.1314"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[0]"); s = p.Parse("select 3*!0"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[3]"); //unary operator does not allow !!value, but this can be rewritten as !(!value) //(see Unary Operators in Engineering a Compiler (Keith Cooper) Ch 3.6) try { s = p.Parse("select !!3.1314"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.Fail("error not thrown for select !!3.1314"); } catch (Exception ex) { Assert.AreEqual(ex.Message, "Stack empty."); } }
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]"); }
public void LogicalAndAndOperator() { EvalStore c = new EvalStore(); Parser p = new Parser(); Expression s; Expression r; s = p.Parse("select 0 && 0"); 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(), "[0]"); s = p.Parse("select 1 && 0"); 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(), "[1]"); s = p.Parse("select 0 && NULL"); 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 && 0"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[0]"); s = p.Parse("select NULL && 1"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[NULL]"); s = p.Parse("select NULL && NULL"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[NULL]"); s = p.Parse("select 1 && 'hello'"); c.Enter(); try { r = s.Evaluate(c); Assert.Fail("Exception not thrown for 1 AND 'hello'"); } catch (Exception ex) { Assert.AreEqual(ex.GetType(), typeof(InvalidOperationException)); Assert.AreEqual(ex.Message, "Error executing select: Error converting 'hello' to Boolean"); } c.Exit(); s = p.Parse("select 1 && 3d"); c.Enter(); r = s.Evaluate(c); Assert.AreEqual(r.ToString(), "[1]"); c.Exit(); s = p.Parse("select 3d && 1"); c.Enter(); r = s.Evaluate(c); Assert.AreEqual(r.ToString(), "[1]"); c.Exit(); s = p.Parse("select 1 && d'01/12/2015'"); c.Enter(); r = s.Evaluate(c); Assert.AreEqual(r.ToString(), "[1]"); c.Exit(); s = p.Parse("select d'01/12/2015' && 1"); c.Enter(); r = s.Evaluate(c); Assert.AreEqual(r.ToString(), "[1]"); c.Exit(); }
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]"); }
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]"); }
public void BitwiseAndOperator() { EvalStore c = new EvalStore(); Parser p = new Parser(); Expression s; Expression r; //basic cases s = p.Parse("select 0 & 0"); 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(), "[0]"); s = p.Parse("select 1 & 0"); 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(), "[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 0 & NULL"); 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(), "[NULL]"); s = p.Parse("select NULL & NULL"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[NULL]"); }
public void BitwiseXorOperator() { EvalStore c = new EvalStore(); Parser p = new Parser(); Expression s; Expression r; s = p.Parse("select 0 ^ 0"); 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 1 ^ 0"); 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 ^ 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 0 ^ NULL"); 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(), "[NULL]"); s = p.Parse("select NULL ^ NULL"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[NULL]"); }
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"; }
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]"); }
//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(); } }
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]"); }
public void UnaryMinusOperator() { EvalStore c = new EvalStore(); Parser p = new Parser(); Expression s; Expression r; s = p.Parse("select -1"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[-1]"); s = p.Parse("select -(-1)"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[1]"); s = p.Parse("select !(!0)"); 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 -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]"); }
public void BitwiseNegateOperator() { EvalStore c = new EvalStore(); Parser p = new Parser(); Expression s; Expression r; s = p.Parse("select ~0xFFFFFFFFFFFFFFFF"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[0]"); s = p.Parse("select ~0x1"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[-2]"); s = p.Parse("select ~NULL"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[NULL]"); s = p.Parse("select ~3.13149265"); c.Enter(); try { r = s.Evaluate(c); Assert.Fail("Exception not thrown for ~DOUBLE"); } catch (Exception ex) { Assert.AreEqual(ex.GetType(), typeof(InvalidOperationException)); Assert.AreEqual(ex.Message, "Error executing select: Unsupported operand types for ~: 'DOUBLE'"); } c.Exit(); }
public void LogicalOrOrOperator() { EvalStore c = new EvalStore(); Parser p = new Parser(); Expression s; Expression r; s = p.Parse("select 0 || 0"); 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 1 || 0"); 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 0 || NULL"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[NULL]"); s = p.Parse("select 1 || NULL"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[1]"); s = p.Parse("select NULL || 0"); 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 NULL || NULL"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[NULL]"); s = p.Parse("select 1 || 'hello'"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[1]"); s = p.Parse("select 'hello' || 1"); c.Enter(); try { r = s.Evaluate(c); Assert.Fail("Exception not thrown for select 'hello' OR 1"); } catch (Exception ex) { Assert.AreEqual(ex.GetType(), typeof(InvalidOperationException)); Assert.AreEqual(ex.Message, "Error executing select: Error converting 'hello' to Boolean"); } c.Exit(); s = p.Parse("select 0 || 'hello'"); c.Enter(); try { r = s.Evaluate(c); Assert.Fail("Exception not thrown for 0 OR 'hello'"); } catch (Exception ex) { Assert.AreEqual(ex.GetType(), typeof(InvalidOperationException)); Assert.AreEqual(ex.Message, "Error executing select: Error converting 'hello' to Boolean"); } c.Exit(); s = p.Parse("select 0 || 3d"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[1]"); s = p.Parse("select 3d || 1"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[1]"); s = p.Parse("select 0 || 0d"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[0]"); s = p.Parse("select 0d || 0"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[0]"); s = p.Parse("select 1 || d'01/12/2015'"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[1]"); s = p.Parse("select d'01/12/2015' || 1"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[1]"); }
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(); }
public void BitwiseShiftRightOperator() { EvalStore c = new EvalStore(); Parser p = new Parser(); Expression s; Expression r; s = p.Parse("select 0 >> 0"); 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(), "[0]"); s = p.Parse("select 1 >> 0"); 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 >> 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 0 >> NULL"); 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(), "[NULL]"); s = p.Parse("select NULL >> NULL"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[NULL]"); }
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]"); }
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]"); }
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(); } }
public void BetweenOperator() { EvalStore c = new EvalStore(); Parser p = new Parser(); Expression s; Expression r; s = p.Parse("select 1000 between 900 and 1001"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[1]"); s = p.Parse("select ! (1000 between 900 and 1001)"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[0]"); s = p.Parse("select not (1000 between 900 and 1001)"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[0]"); try { s = p.Parse("select 1000 not between 900 and 1001"); c.Enter(); r = s.Evaluate(c); Assert.Fail("Error not thrown: select 1000 not between 900 and 1001"); } catch { } finally { c.Exit(); } s = p.Parse("select (1000 between '-900' and '1001')"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[1]"); s = p.Parse("select d'12/12/2014' between d'1/12/2013' and d'1/1/2016')"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[1]"); s = p.Parse("select 3d between 1d and 5d"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[1]"); s = p.Parse("select 3d between +1d and +5d"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[1]"); s = p.Parse("select 3d between 86400000*3 and 86400000*3"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[1]"); }