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); }
private Expression EvaluateWhere(EvalStore store, Expression[] args) { if (args[0] is IdentifierExpr && args[1] is ConstExpr) { string colName; string tableName; if (args[0] is QualifiedNameExpr) { QualifiedNameExpr name = args[0] as QualifiedNameExpr; colName = name.QualifiedName[0]; tableName = name.QualifiedName[1]; } else { colName = ((IdentifierExpr)args[0]).Name; tableName = "_"; } Element value = args[1].Value; var selAttrs = store.Get<Dictionary<string, Table>>("$SEL_FACTS"); Table attrs = selAttrs[tableName]; if (attrs.ColumnDefinitions.IndexOf(colName) < 0) throw new Exception(tableName + "." + colName + " does not exist."); Table s = attrs.Select(new Predicate(colName, Predicate.PredicateOperator.REGEXEQ, value)); if (s.Rows == 0) return ConstExpr.Empty; else return new ConstExpr(s); } return ConstExpr.Empty; }
public override Expression Evaluate(EvalStore store) { Table data = store.Get<Table>("$DATA"); int col = -1; if (data != null) { col = data.ColumnDefinitions.IndexOf(this.Name); } Element e = null; if (col >= 0) { List<int> group = store.Get<List<int>>("$GROUP"); if (group != null) { Element[] values = new Element[group.Count]; int i=0; foreach (int row in group) { values[i++]= data[row, col]; } e = new Element(values); } else { Element[] row = store.Get<Element[]>("$ROW"); e = row[col]; } } else { if (store.Exists(this.Name)) { e = store.Get<Element>(this.Name); } else { EvalContext ctx = store.Get<EvalContext>("$EVAL_CONTEXT"); if (ctx == null || ctx.Context != EvalContext.ContextType.SELECT) { if (!this.Name.Contains(" ")) { e = new Element(this.Name); } } } } if (e != null) { return new ConstExpr(e); } else { throw new Exception("No such column: " + this.Name); } }
public override Expression Evaluate(EvalStore store) { Element e = store.Get<Element>(this.Name); if (e != null) { return new ConstExpr(e); } return ConstExpr.Empty; }
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; }
public override Expression Evaluate(EvalStore store, params Expression[] args) { EvalContext ctx = store.Get<EvalContext>("$EVAL_CONTEXT"); if (ctx != null && ctx.Context == EvalContext.ContextType.SEARCH) { return EvaluateWhere(store, args); } switch (IsMatch(store, args)) { case OperationResult.TRUE: return ConstExpr.True; case OperationResult.FALSE: return ConstExpr.False; case OperationResult.EMPTY: return ConstExpr.Empty; } return ConstExpr.False; }
//------------------------------------------------- // Truth Table for AND operator //------------------------------------------------- // a b a AND b // 0 0 0 // 0 1 0 // 1 0 0 // 1 1 1 // 0 null 0 // 1 null null // null 0 0 // null 1 null // null null null // //Note: 1. Nothing equals NULL, 2. Select NULL is NULL public override Expression Evaluate(EvalStore store, params Expression[] args) { Expression a = args[0]; Expression b = args[1]; EvalContext ctx = store.Get<EvalContext>("$EVAL_CONTEXT"); if (ctx != null && ctx.Context == EvalContext.ContextType.SEARCH) { return EvaluateWhere(store, a,b); } if (a.ExpressionType != ExpressionType.CONST) a = a.Evaluate(store); if (a.Value.DataType != ElementType.EMPTY && !a.Value.ToBoolean(null)) return ConstExpr.False; if (b.ExpressionType != ExpressionType.CONST) b = b.Evaluate(store); if (b.Value.DataType != ElementType.EMPTY && !b.Value.ToBoolean(null)) return ConstExpr.False; if (a.Value.DataType == ElementType.EMPTY) return ConstExpr.Empty; if (b.Value.DataType == ElementType.EMPTY) return ConstExpr.Empty; return ConstExpr.True; }
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); }
public override Expression Evaluate(EvalStore store) { var e = _expr.To<IdentifierExpr>(); object o = store.Get<object>(e.Name); if (o is Table) { return new ConstExpr(o as Table); } else if (o is Element) { Element v = o as Element; List<TableColumn> cols = new List<TableColumn>(); cols.Add(new TableColumn(null,v.DataType, false)); List<Element[]> row = new List<Element[]>(); row.Add(new Element[1]); row[0][0]=v; Table t = new Table(null, cols, row); return new ConstExpr(t); } return ConstExpr.Empty; }
public override Expression Evaluate(EvalStore store) { Element n = store.Get<Element>("$NOW"); DateTime now; if (n != null) { now = (DateTime)n; } else { now = DateTime.Now; } if (this.Arguments != null) { ConstExpr c = this.Arguments[0].Evaluate(store) as ConstExpr; if (c == null) throw new Exception(Arguments[0] + " can't be evaluated to null."); switch (c.Value.DataType) { case ElementType.LONG: now = c.Value.ToDateTime(null); break; case ElementType.DATETIME: now = c.Value.ToDateTime(null); break; case ElementType.TIMESPAN: now = now + c.Value.ToTimeSpan(null); break; } ////align the time to the start of an interval //if (this.Arguments.Count > 1) { // c = (ConstExpr)this.Arguments[1].Evaluate(store); // if (c == null || c.Value.DataType != ElementType.TIMESPAN) throw new Exception(Arguments[0] + "must be evaluated to a duration."); // TimeSpan align = c.Value.ToTimeSpan(null); // long ticks = (now.Ticks / align.Ticks) * align.Ticks; // now = new DateTime(ticks); //} } return new ConstExpr(now); }
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); }
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; }
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(); } }