public override ulong LogicAggCE(LogicAgg node) { ulong card = 1; if (node.groupby_ is null) { card = 1; } else { ulong distinct = 1; foreach (var v in node.groupby_) { ulong ndistinct = 1; if (v is ColExpr vc && vc.tabRef_ is BaseTableRef bvc) { var stat = Catalog.sysstat_.GetColumnStat(bvc.relname_, vc.colName_); ndistinct = stat.n_distinct_; } // stop accumulating in case of overflow if (distinct * ndistinct > distinct) { distinct *= ndistinct; } } card = (ulong)distinct; } // it won't go beyond the number of output rows return(Math.Min(card, node.child_().Card())); }
public override bool Appliable(CGroupMember expr) { LogicAgg agg = expr.logic_ as LogicAgg; if (agg is null || agg.isLocal_ || agg.isDerived_) { return(false); } return(true); }
public override CGroupMember Apply(CGroupMember expr) { // for manually binding the expression void manualbindexpr(Expr e) { e.bounded_ = true; e.type_ = new BoolType(); } // for transformaing original having according to the new agg func BinExpr processhaving(Expr e, Dictionary <Expr, Expr> dict) { var be = e as BinExpr; Debug.Assert(be != null); bool isreplace = false; List <Expr> children = new List <Expr>(); foreach (var child in be.children_) { if (dict.ContainsKey(child)) { children.Add(dict[child]); isreplace = true; } else { children.Add(child); } } Debug.Assert(isreplace); return(new BinExpr(children[0], children[1], be.op_)); } LogicAgg origAggNode = (expr.logic_ as LogicAgg); var childNode = (origAggNode.child_() as LogicMemoRef).Deref <LogicNode>(); var groupby = origAggNode.groupby_?.CloneList(); var having = origAggNode.having_?.Clone(); // process the aggregation functions origAggNode.GenerateAggrFns(false); List <AggFunc> aggfns = new List <AggFunc>(); origAggNode.aggrFns_.ForEach(x => aggfns.Add(x.Clone() as AggFunc)); // need to make aggrFns_ back to null list origAggNode.aggrFns_ = new List <AggFunc>(); var globalfns = new List <Expr>(); var localfns = new List <Expr>(); // record the processed aggregate functions var derivedAggFuncDict = new Dictionary <Expr, Expr>(); foreach (var func in aggfns) { Expr processed = func.SplitAgg(); // if splitagg is returning null, end the transformation process if (processed is null) { return(expr); } // force the id to be equal. processed._ = func._; globalfns.Add(processed); derivedAggFuncDict.Add(func, processed); } var local = new LogicAgg(childNode, groupby, localfns, null); local.isLocal_ = true; // having is placed on the global agg and the agg func need to be processed var newhaving = having; if (having != null) { newhaving = processhaving(having, derivedAggFuncDict); manualbindexpr(newhaving); } // assuming having is an expression involving agg func, // it is only placed on the global agg var global = new LogicAgg(local, groupby, globalfns, newhaving); global.isDerived_ = true; global.Overridesign(origAggNode); global.deriveddict_ = derivedAggFuncDict; return(new CGroupMember(global, expr.group_)); }
public abstract ulong LogicAggCE(LogicAgg node);
public static void Run(SQLStatement stmt, ExecContext context) { PhysicCollect PhysicCollect108 = stmt.physicPlan_.LocateNode("108") as PhysicCollect; PhysicProfiling PhysicProfiling109 = stmt.physicPlan_.LocateNode("109") as PhysicProfiling; LogicLimit LogicLimit109 = PhysicProfiling109.logic_ as LogicLimit; var filter109 = LogicLimit109.filter_; var output109 = LogicLimit109.output_; PhysicLimit PhysicLimit110 = stmt.physicPlan_.LocateNode("110") as PhysicLimit; LogicLimit LogicLimit110 = PhysicLimit110.logic_ as LogicLimit; var filter110 = LogicLimit110.filter_; var output110 = LogicLimit110.output_; PhysicProfiling PhysicProfiling111 = stmt.physicPlan_.LocateNode("111") as PhysicProfiling; LogicAgg LogicAgg111 = PhysicProfiling111.logic_ as LogicAgg; var filter111 = LogicAgg111.filter_; var output111 = LogicAgg111.output_; PhysicHashAgg PhysicHashAgg112 = stmt.physicPlan_.LocateNode("112") as PhysicHashAgg; LogicAgg LogicAgg112 = PhysicHashAgg112.logic_ as LogicAgg; var filter112 = LogicAgg112.filter_; var output112 = LogicAgg112.output_; PhysicProfiling PhysicProfiling113 = stmt.physicPlan_.LocateNode("113") as PhysicProfiling; LogicJoin LogicJoin113 = PhysicProfiling113.logic_ as LogicJoin; var filter113 = LogicJoin113.filter_; var output113 = LogicJoin113.output_; PhysicHashJoin PhysicHashJoin114 = stmt.physicPlan_.LocateNode("114") as PhysicHashJoin; LogicJoin LogicJoin114 = PhysicHashJoin114.logic_ as LogicJoin; var filter114 = LogicJoin114.filter_; var output114 = LogicJoin114.output_; PhysicProfiling PhysicProfiling115 = stmt.physicPlan_.LocateNode("115") as PhysicProfiling; LogicJoin LogicJoin115 = PhysicProfiling115.logic_ as LogicJoin; var filter115 = LogicJoin115.filter_; var output115 = LogicJoin115.output_; PhysicHashJoin PhysicHashJoin116 = stmt.physicPlan_.LocateNode("116") as PhysicHashJoin; LogicJoin LogicJoin116 = PhysicHashJoin116.logic_ as LogicJoin; var filter116 = LogicJoin116.filter_; var output116 = LogicJoin116.output_; PhysicProfiling PhysicProfiling117 = stmt.physicPlan_.LocateNode("117") as PhysicProfiling; LogicScanTable LogicScanTable117 = PhysicProfiling117.logic_ as LogicScanTable; var filter117 = LogicScanTable117.filter_; var output117 = LogicScanTable117.output_; PhysicScanTable PhysicScanTablea = stmt.physicPlan_.LocateNode("a") as PhysicScanTable; LogicScanTable LogicScanTablea = PhysicScanTablea.logic_ as LogicScanTable; var filtera = LogicScanTablea.filter_; var outputa = LogicScanTablea.output_; PhysicProfiling PhysicProfiling118 = stmt.physicPlan_.LocateNode("118") as PhysicProfiling; LogicScanTable LogicScanTable118 = PhysicProfiling118.logic_ as LogicScanTable; var filter118 = LogicScanTable118.filter_; var output118 = LogicScanTable118.output_; PhysicScanTable PhysicScanTablec = stmt.physicPlan_.LocateNode("c") as PhysicScanTable; LogicScanTable LogicScanTablec = PhysicScanTablec.logic_ as LogicScanTable; var filterc = LogicScanTablec.filter_; var outputc = LogicScanTablec.output_; var hm116 = new Dictionary <KeyList, List <TaggedRow> >(); PhysicProfiling PhysicProfiling119 = stmt.physicPlan_.LocateNode("119") as PhysicProfiling; LogicScanTable LogicScanTable119 = PhysicProfiling119.logic_ as LogicScanTable; var filter119 = LogicScanTable119.filter_; var output119 = LogicScanTable119.output_; PhysicScanTable PhysicScanTableb = stmt.physicPlan_.LocateNode("b") as PhysicScanTable; LogicScanTable LogicScanTableb = PhysicScanTableb.logic_ as LogicScanTable; var filterb = LogicScanTableb.filter_; var outputb = LogicScanTableb.output_; var hm114 = new Dictionary <KeyList, List <TaggedRow> >(); var aggrcore112 = LogicAgg112.aggrFns_; var hm112 = new Dictionary <KeyList, Row>(); var nrows110 = 0; PhysicProfiling109.nloops_++; PhysicProfiling111.nloops_++; PhysicProfiling113.nloops_++; PhysicProfiling115.nloops_++; PhysicProfiling117.nloops_++; var heapa = (LogicScanTablea.tabref_).Table().heap_.GetEnumerator(); for (;;) { Row ra = null; if (context.stop_) { break; } if (heapa.MoveNext()) { ra = heapa.Current; } else { break; } { { // projection on PhysicScanTablea: Output: a.a1[0],a.a2[1] Row rproj = new Row(2); rproj[0] = ra[0]; rproj[1] = ra[1]; ra = rproj; } PhysicProfiling117.nrows_++; var r117 = ra; var keys116 = KeyList.ComputeKeys(context, LogicJoin116.leftKeys_, r117); if (hm116.TryGetValue(keys116, out List <TaggedRow> exist)) { exist.Add(new TaggedRow(r117)); } else { var rows = new List <TaggedRow>(); rows.Add(new TaggedRow(r117)); hm116.Add(keys116, rows); } } } if (hm116.Count == 0) { return; } PhysicProfiling118.nloops_++; var heapc = (LogicScanTablec.tabref_).Table().heap_.GetEnumerator(); for (;;) { Row rc = null; if (context.stop_) { break; } if (heapc.MoveNext()) { rc = heapc.Current; } else { break; } { { // projection on PhysicScanTablec: Output: c.c2[1] Row rproj = new Row(1); rproj[0] = rc[1]; rc = rproj; } PhysicProfiling118.nrows_++; var r118 = rc; if (context.stop_) { return; } Row fakel116 = new Row(2); Row r116 = new Row(fakel116, r118); var keys116 = KeyList.ComputeKeys(context, LogicJoin116.rightKeys_, r116); bool foundOneMatch116 = false; if (hm116.TryGetValue(keys116, out List <TaggedRow> exist116)) { foundOneMatch116 = true; foreach (var v116 in exist116) { r116 = new Row(v116.row_, r118); { // projection on PhysicHashJoin116: Output: a.a1[0],a.a2[1] Row rproj = new Row(2); rproj[0] = r116[0]; rproj[1] = r116[1]; r116 = rproj; } PhysicProfiling115.nrows_++; var r115 = r116; var keys114 = KeyList.ComputeKeys(context, LogicJoin114.leftKeys_, r115); if (hm114.TryGetValue(keys114, out List <TaggedRow> exist)) { exist.Add(new TaggedRow(r115)); } else { var rows = new List <TaggedRow>(); rows.Add(new TaggedRow(r115)); hm114.Add(keys114, rows); } } } else { // no match for antisemi } } } if (hm114.Count == 0) { return; } PhysicProfiling119.nloops_++; var heapb = (LogicScanTableb.tabref_).Table().heap_.GetEnumerator(); for (;;) { Row rb = null; if (context.stop_) { break; } if (heapb.MoveNext()) { rb = heapb.Current; } else { break; } { { // projection on PhysicScanTableb: Output: b.b1[0] Row rproj = new Row(1); rproj[0] = rb[0]; rb = rproj; } PhysicProfiling119.nrows_++; var r119 = rb; if (context.stop_) { return; } Row fakel114 = new Row(2); Row r114 = new Row(fakel114, r119); var keys114 = KeyList.ComputeKeys(context, LogicJoin114.rightKeys_, r114); bool foundOneMatch114 = false; if (hm114.TryGetValue(keys114, out List <TaggedRow> exist114)) { foundOneMatch114 = true; foreach (var v114 in exist114) { r114 = new Row(v114.row_, r119); { // projection on PhysicHashJoin114: Output: a.a1[0],a.a2[1] Row rproj = new Row(2); rproj[0] = r114[0]; rproj[1] = r114[1]; r114 = rproj; } PhysicProfiling113.nrows_++; var r113 = r114; var keys = KeyList.ComputeKeys(context, LogicAgg112.keys_, r113); if (hm112.TryGetValue(keys, out Row exist)) { for (int i = 0; i < 1; i++) { var old = exist[i]; exist[i] = aggrcore112[i].Accum(context, old, r113); } } else { hm112.Add(keys, PhysicHashAgg112.AggrCoreToRow(r113)); exist = hm112[keys]; for (int i = 0; i < 1; i++) { exist[i] = aggrcore112[i].Init(context, r113); } } } } else { // no match for antisemi } } } foreach (var v112 in hm112) { if (context.stop_) { break; } var keys112 = v112.Key; Row aggvals112 = v112.Value; for (int i = 0; i < 1; i++) { aggvals112[i] = aggrcore112[i].Finalize(context, aggvals112[i]); } var r112 = new Row(keys112, aggvals112); if (true || LogicAgg112.having_.Exec(context, r112) is true) { { // projection on PhysicHashAgg112: Output: {a.a2}[0]*2,{count(a.a1)}[1],repeat('a',{a.a2}[0]) Row rproj = new Row(3); rproj[0] = ((dynamic)r112[0] * (dynamic)2); rproj[1] = r112[1]; rproj[2] = ExprSearch.Locate("74").Exec(context, r112) /*repeat('a',{a.a2}[0])*/; r112 = rproj; } PhysicProfiling111.nrows_++; var r111 = r112; nrows110++; Debug.Assert(nrows110 <= 2); if (nrows110 == 2) { context.stop_ = true; } var r110 = r111; PhysicProfiling109.nrows_++; var r109 = r110; Row newr = new Row(3); newr[0] = r109[0]; newr[1] = r109[1]; newr[2] = r109[2]; PhysicCollect108.rows_.Add(newr); Console.WriteLine(newr); } } }
public LogicNode CreateSetOpPlan(bool top = true) { if (top) { // traversal on top node is the time to examine the setop tree Debug.Assert(!IsLeaf()); VerifySelection(); } if (IsLeaf()) { return(stmt_.CreatePlan()); } else { LogicNode plan = null; var lplan = left_.CreateSetOpPlan(false); var rplan = right_.CreateSetOpPlan(false); // try to reuse existing operators to implment because users may write // SQL code like this and this helps reduce optimizer search space // switch (op_) { case "unionall": // union all keeps all rows, including duplicates plan = new LogicAppend(lplan, rplan); break; case "union": // union collect rows from both sides, and remove duplicates plan = new LogicAppend(lplan, rplan); var groupby = new List <Expr>(first_.selection_.CloneList()); plan = new LogicAgg(plan, groupby, null, null); break; case "except": // except keeps left rows not found in right case "intersect": // intersect keeps rows found in both sides var filter = FilterHelper.MakeFullComparator( left_.first_.selection_, right_.first_.selection_); var join = new LogicJoin(lplan, rplan); if (op_.Contains("except")) { join.type_ = JoinType.AntiSemi; } if (op_.Contains("intersect")) { join.type_ = JoinType.Semi; } var logfilter = new LogicFilter(join, filter); groupby = new List <Expr>(first_.selection_.CloneList()); plan = new LogicAgg(logfilter, groupby, null, null); break; case "exceptall": case "intersectall": // the 'all' semantics is a bit confusing than intuition: // {1,1,1} exceptall {1,1} => {1} // {1,1,1} intersectall {1,1} => {1,1} // throw new NotImplementedException(); default: throw new InvalidProgramException(); } return(plan); } }