internal override bool SelectTable(string table) { _table = table; var path = GetPath(); if (!File.Exists(path)) { return(false); } using (var rdr = new TextFieldParser(path) { TextFieldType = FieldType.Delimited, Delimiters = new string[] { "," }, }) { var row = rdr.ReadFields(); if (_hasid) { row = (new string[] { "Id:number" }) .Concat(row).ToArray(); } var fields = row.Select(r => new CommonField { Name = r, CType = CommonType.Text }).ToArray(); Heading = CommonHeading.Create(fields); } return(true); }
// remove tuples that satisfy predicate public void Delete(string heading, Func <Tup, bool> selfunc) { var head = CommonHeading.Create(heading); var map = head.CreateMap(Heading); Value = new RelValue(Heading, Value.Where(t => !selfunc(RelStatic.CreateByMap <Tup>(t, map)))); }
static IEnumerable <TupBase> While(CommonHeading heading, IEnumerable <TupBase> body, Func <RelNode, RelNode> func) { var stack = new Stack <TupBase>(body); var hash = new HashSet <TupBase>(); while (stack.Count > 0) { var top = stack.Pop(); if (!hash.Contains(top)) { hash.Add(top); var result = func(new WrapperNode(heading, Enumerable.Repeat(top, 1))); var eq = result.Heading.IsEqual(heading); var map = heading.CreateMap(result.Heading); if (result.Heading.Degree != heading.Degree || map.Any(x => x < 0)) { throw Error.Fatal("While", $"heading mismatch: {result.Heading} {heading}"); } // convert compatible if needed foreach (var t in result) { stack.Push(eq ? t : RelStatic.CreateByMap <Tup>(t, map)); } } } return(hash); }
public RenameNode(RelNode source, CommonHeading nodeheading) { _source = source; _nodeheading = nodeheading; _map = _nodeheading.CreateMap(_source.Heading); if (!(_map.Length == 2 && _map[0] >= 0 && _map[1] < 0)) { throw Error.Fatal("Rename", "heading is invalid"); } Heading = _source.Heading.Rename(_nodeheading[0], _nodeheading[1]); }
// update tuples that satisfy predicate public void Update(string heading, Func <Tup, bool> selfunc, object newvalue) { // map for selection tuple var head1 = CommonHeading.Create(heading); var map1 = head1.CreateMap(Heading); // map for replace tuple var head2 = Heading.Remove(head1.Fields.Last()).Append(CommonField.Empty); var map2 = head2.CreateMap(Heading); Value = new RelValue(Heading, Value.Select(t => selfunc(RelStatic.CreateByMap <Tup>(t, map1)) ? RelStatic.CreateByMap <Tup>(t, map2, newvalue) : t)); }
// select the schema as the current table bool SelectSchema() { _table = null; var schema = GetSchema(); var scols = schema.Columns; var fields = Enumerable.Range(0, scols.Count) .Select(x => new CommonField { Name = scols[x].ColumnName, CType = CommonConverter.TypeToCommon(scols[x].DataType) }).ToArray(); Heading = CommonHeading.Create(fields); return(true); }
public RestrictNode(RelNode source, CommonHeading nodeheading, RelFuncBase func) { _source = source; _restheading = nodeheading; _restfunc = func; Heading = _source.Heading; _restmap = _restheading.CreateMap(Heading); if (_restmap.Any(x => x < 0)) { throw Error.Fatal("Restrict", "heading has unknown attribute"); } }
//--- impl // reflection hack to get heading value from tuple // TODO: is this the best way to handle heading not found? internal static CommonHeading GetHeading(Type ttype) { var prop = ttype.GetField("Heading"); if (prop == null) { return(CommonHeading.Empty); } var heading = (string)prop.GetValue(null); if (heading == null) { return(CommonHeading.Empty); } return(CommonHeading.Create(heading)); // TODO: add types }
public ProjectNode(RelNode source, CommonHeading nodeheading, bool isremove = false) { _source = source; var _nodeheading = nodeheading; Heading = (isremove) ? _source.Heading.Minus(_nodeheading) : _nodeheading; _map = Heading.CreateMap(_source.Heading); if (Heading.Degree > _source.Heading.Degree) { throw Error.Fatal("Project", "too many attributes"); } if (_map.Any(x => x < 0)) { throw Error.Fatal("Project", "heading does not match"); } }
public AggregateNode(RelNode source, CommonHeading nodeheading, RelFuncBase func, object initial = null) { _source = source; _heading = nodeheading; _func = func; _initial = initial; Heading = _source.Heading.Rename(_heading[0], _heading[1]); _vmap = _heading.CreateMap(_source.Heading); _jhead = _source.Heading.Minus(_heading); _jmap1 = _jhead.CreateMap(_source.Heading); _jmap2 = Heading.CreateMap(_jhead); if (!(_heading.Degree == 2 && _jmap2.Length == Heading.Degree)) { throw Error.Fatal("Aggregate", "heading is not valid"); } }
public UngroupNode(RelNode source, UngroupingOp op, CommonHeading nodeheading) { _source = source; _op = op; _nodeheading = nodeheading; _nodemap = _nodeheading.CreateMap(_source.Heading); if (!(_nodemap.Length == 1 && _nodemap[0] >= 0)) { throw Error.Fatal("Ungroup", "heading is not valid"); } // on output replace one field by the TVA/RVA Heading = _source.Heading.Remove(_nodeheading[0]).Append(_nodeheading[0].Fields); // var tupheading = CommonHeading.Create(_nodeheading[0].Fields); _map1 = Heading.CreateMap(_source.Heading); _map2 = Heading.CreateMap(tupheading); }
public GroupNode(RelNode source, GroupingOp op, CommonHeading nodeheading) { _source = source; _op = op; _nodeheading = nodeheading; // inner tuple(s) for TVA or RVA var tupheading = _nodeheading.Remove(_nodeheading.Fields.Last()); _tmap = tupheading.CreateMap(_source.Heading); // outer fields used as key for index var keyheading = _source.Heading.Minus(tupheading); _kmap = keyheading.CreateMap(_source.Heading); // final result with new field appended Heading = keyheading.Append(new CommonField(_nodeheading.Fields.Last().Name, (_op == GroupingOp.Group) ? CommonType.Table : CommonType.Row, tupheading.Fields)); _map = Heading.CreateMap(keyheading); if (!(_tmap.All(x => x >= 0) && _kmap.All(x => x >= 0) && _map.Last() < 0)) { throw Error.Fatal("Group", "heading is not valid"); } }
public ExtendNode(RelNode source, CommonHeading nodeheading, RelFuncBase func) { _source = source; _nodeheading = nodeheading; _func = func; Heading = CommonHeading.Create(_source.Heading.Fields.Union(_nodeheading.Fields)); // get the argument fields _argheading = CommonHeading.Create(_nodeheading.Fields.Take(_nodeheading.Fields.Length - 1)); _argmap = _argheading.CreateMap(_source.Heading); // last field can extend or replace, remove name but retain place var outh = Heading.Rename(_nodeheading.Fields.Last(), CommonField.Empty); _outmap = Heading.CreateMap(outh); if (_argmap.Any(x => x < 0)) { throw Error.Fatal("Extend", "heading has unknown attribute"); } if (_outmap.Count(x => x < 0) != 1) { throw Error.Fatal("Extend", "invalid heading, invalid new attribute"); // any use? } }
// Generic input internal override bool SelectTable(string table) { //Logger.WriteLine(2, "Sql Peek '{0}'", table); if (table == null) { return(SelectSchema()); } using (var reader = Open(table)) { if (reader == null) { return(false); } _table = table; //Logger.WriteLine(3, "Table {0} fields {1}", table, String.Join(",", s)); var fields = Enumerable.Range(0, reader.FieldCount) .Where(x => _convdict.ContainsKey(reader.GetDataTypeName(x))) .Select(x => new CommonField { Name = reader.GetName(x), CType = _convdict[reader.GetDataTypeName(x)] }).ToArray(); Heading = CommonHeading.Create(fields); } Close(); return(true); }
public RelVarST(RelNode value) { Heading = RelBaseST <T> .Heading; Init(value); }
public RelVarST(RelValueST <T> tuples) { Value = Rel.Create(tuples); Heading = RelBaseST <T> .Heading; }
public RelVarST() { Value = Rel.Create(new T[0]); Heading = RelBaseST <T> .Heading; }
// Construct a node from a stream public static RelNode Create(string heading, IEnumerable <Tup> source) { return(new WrapperNode(CommonHeading.Create(heading), source)); }
public WrapperNode(CommonHeading heading, IEnumerable <TupBase> source) { _source = source; Heading = heading; }
// create new relation value from body as enumerable public RelValue(CommonHeading heading, IEnumerable <Tup> body) { Heading = heading; _body = new HashSet <Tup>(body); }
// create new relation value from body as set public RelValue(CommonHeading heading, HashSet <Tup> body) { Heading = heading; _body = body; }
// Construct a node from literal data public static RelNode Data(string heading, params Tup[] source) { return(new WrapperNode(CommonHeading.Create(heading), source)); }
//--- ctor // set up heading here when relation not instantiated but used to get heading static RelBaseST() { Heading = GetHeading(typeof(T)); }
public RelBaseST() { Heading = GetHeading(typeof(T)); _body = new HashSet <T>(); }
// Allow heading to be set as a string "name:type,name:type..." internal void SetHeading(string heading) { Heading = CommonHeading.Create(heading); }
public string Format(CommonHeading heading) { return(Enumerable.Range(0, Values.Length) .Select(x => heading[x].Format(Values[x])) .Join(", ")); }