public void ApplyMoveHelperForces(FixedUpdateEvent evt, PhysicNode selfPhysics) { Rigidbody rigidbody = selfPhysics.rigidbody.Rigidbody; Transform rigidbodyTransform = selfPhysics.rigidbody.RigidbodyTransform; if (rigidbody != null) { Vector3 vector = ((BoxCollider)selfPhysics.tankColliders.TankToTankCollider).size * 0.5f; Vector3 origin = rigidbodyTransform.TransformPoint(new Vector3(-vector.x, vector.y * 0.5f, vector.z)); Vector3 vector3 = rigidbodyTransform.TransformPoint(new Vector3(vector.x, vector.y * 0.5f, vector.z)); float magnitude = Vector3.Project(rigidbody.velocity, rigidbodyTransform.forward).magnitude; float length = 0.1f + (magnitude * 0.25f); bool flag = this.StaticCollided(origin, rigidbodyTransform.forward, length); bool flag2 = !this.StaticCollided(origin + (rigidbodyTransform.right * 0.3f), rigidbodyTransform.forward, length * 1.2f); bool flag3 = this.StaticCollided(vector3, rigidbodyTransform.forward, length); bool flag4 = !this.StaticCollided(vector3 - (rigidbodyTransform.right * 0.3f), rigidbodyTransform.forward, length * 1.2f); if (Vector3.Dot(rigidbody.velocity.normalized, rigidbodyTransform.forward) > 0f) { if (flag && (flag2 && !flag3)) { rigidbody.AddTorqueSafe(((new Vector3(0f, 1f, 0f) * magnitude) * rigidbody.mass) * 2f); } if (!flag && (flag4 && flag3)) { rigidbody.AddTorqueSafe(((-new Vector3(0f, 1f, 0f) * magnitude) * rigidbody.mass) * 2f); } } } }
public List <Row> show() { bind(null); // TBD: route to optimizer here QueryOption queryOpt = new QueryOption(); physicPlan_ = logicPlan_.DirectToPhysical(queryOpt); logicPlan_.ResolveColumnOrdinal(outputs_); // actual execution var finalplan = new PhysicCollect(physicPlan_); physicPlan_ = finalplan; var context = new ExecContext(queryOpt); Console.WriteLine(physicPlan_.Explain()); finalplan.ValidateThis(); var code = finalplan.Open(context); code += finalplan.Exec(null); code += finalplan.Close(); return(finalplan.rows_); }
protected Expr identifyJoinPred(PhysicNode T1, PhysicNode T2, bool canBeCrossJoin = false) { var key = ValueTuple.Create(T1, T2); if (!joinpreds_.ContainsKey(key)) { var predContained = graph_.predContained_; // decide the join predicate: they shall touch both T1 and T2 // eg. T1 x (T3 x T4) predicate: T1.a = T4.b ok // var list = new List <Expr>(); for (int i = 0; i < predContained.Count; i++) { var cover = predContained[i]; if ((cover & T1.tableContained_) != 0 && (cover & T2.tableContained_) != 0) { list.Add(graph_.preds_[i]); } } // we shall be able to locate the predicate because we don't generate cross join here Debug.Assert(canBeCrossJoin || list.Count != 0); if (list.Count > 0) { var expr = list.AndListToExpr(); joinpreds_.Add(key, expr); } } Debug.Assert(canBeCrossJoin || joinpreds_.ContainsKey(key)); return(joinpreds_.ContainsKey(key) ? joinpreds_[key] : null); }
// debug purpose: save the existing one to candidate list internal void dbg_AddToCandidate(BitVector key, PhysicNode node) { Debug.Assert(node != null); List <PhysicNode> list; if (!dbg_candidates_.TryGetValue(key, out list)) { list = new List <PhysicNode>(); dbg_candidates_[key] = list; } list.Add(node); }
public PhysicNode CopyOutMinLogicPhysicPlan(PhysicNode physic = null) { if (physic is null) { // get the lowest inclusive cost one from the member list var minmember = CalculateMinInclusiveCostMember(); physic = minmember.physic_; } // recursively repeat the process for all its children if (physic.children_.Count > 0 && !(physic is PhysicProfiling)) { var phychildren = new List <PhysicNode>(); var logchildren = new List <LogicNode>(); foreach (var v in physic.children_) { // children shall be min cost PhysicNode phychild; if (!(v is PhysicMemoRef)) { phychild = CopyOutMinLogicPhysicPlan(v); } else { var g = (v as PhysicMemoRef).Group(); phychild = g.CopyOutMinLogicPhysicPlan(); } phychildren.Add(phychild); logchildren.Add(phychild.logic_); } // rewrite children physic.children_ = phychildren; physic.logic_.children_ = logchildren; } physic.logic_ = RetrieveLogicTree(physic.logic_); Debug.Assert(!(physic is PhysicMemoRef)); Debug.Assert(!(physic.logic_ is LogicMemoRef)); // enable profiling: we want to do it here instead of mark at the // end of plan copy out to avoid revisit plan tree (including expr // tree again) // if (memo_.stmt_.queryOpt_.profile_.enabled_) { physic = new PhysicProfiling(physic); } return(physic); }
// create join tree of (T1, T2) with commutative transform and record it in bestTree_ // protected PhysicNode CreateMinimalJoinTree(PhysicNode T1, PhysicNode T2, bool canBeCrossJoin = false) { Expr pred = identifyJoinPred(T1, T2, canBeCrossJoin); // for all join implmentations identify the lowest cost one PhysicNode bestJoin = null; var implmentations = Enum.GetValues(typeof(PhysicJoin.Implmentation)); foreach (PhysicJoin.Implmentation impl in implmentations) { // right deep tree // Notes: we are suppposed to verify existing.Card equals plan.Card but // propogated round error prevent us to do so. // PhysicNode plan = PhysicJoin.CreatePhysicJoin(impl, T1, T2, pred); Debug.Assert(!double.IsNaN(plan.InclusiveCost())); if (bestJoin == null || bestJoin.InclusiveCost() > plan.InclusiveCost()) { bestJoin = plan; } // left deep tree plan = PhysicJoin.CreatePhysicJoin(impl, T2, T1, pred); Debug.Assert(!double.IsNaN(plan.InclusiveCost())); if (bestJoin == null || bestJoin.InclusiveCost() > plan.InclusiveCost()) { bestJoin = plan; } } Debug.Assert(bestJoin != null); // remember this join in the bestTree_ BitVector key = bestJoin.tableContained_; PhysicNode existing = bestTree_[key]; if (existing == null || existing.InclusiveCost() > bestJoin.InclusiveCost()) { bestTree_[key] = bestJoin; } return(bestTree_[key]); }
public void ApplyWingForces(FixedUpdateEvent evt, PhysicNode selfPhysics, [JoinAll] MapNode mapPhysic) { Rigidbody rigidbody = selfPhysics.rigidbody.Rigidbody; Transform rigidbodyTransform = selfPhysics.rigidbody.RigidbodyTransform; if (rigidbody != null) { float num = Vector3.Dot(rigidbodyTransform.up, Vector3.up); if (num < 0.9f) { float num2 = Physics.gravity.y / -10f; Vector3 normalized = rigidbodyTransform.InverseTransformDirection(rigidbody.angularVelocity).normalized; float num3 = 0.5f; if (num <= 0f) { if ((num3 > 0f) && (rigidbody.angularVelocity.magnitude > 0.2f)) { float num4 = ((num3 * rigidbody.mass) * num2) * Mathf.Sign(normalized.z); rigidbody.AddRelativeTorqueSafe(new Vector3(0f, 0f, 1f) * num4); } } else { float num5 = (4f * rigidbody.mass) * num2; if (!this.ApplyWingForce(rigidbody, rigidbodyTransform.right, new Vector3(0f, 0f, 1f) * num5)) { this.ApplyWingForce(rigidbody, -rigidbodyTransform.right, new Vector3(0f, 0f, -1f) * num5); } float num6 = (4f * rigidbody.mass) * num2; if (!this.ApplyWingForce(rigidbody, rigidbodyTransform.forward, new Vector3(-1f, 0f, 0f) * num6)) { this.ApplyWingForce(rigidbody, -rigidbodyTransform.forward, new Vector3(1f, 0f, 0f) * num6); } } } } }
public override PhysicNode InstallSelectPlan(PhysicNode select) => InstallUnder <PhysicInsert>(select);
static void Main(string[] args) { Catalog.Init(); string sql = ""; if (false) { JOBench.CreateTables(); sql = File.ReadAllText("../../../jobench/10a.sql"); goto doit; } if (false) { Tpch.CreateTables(); Tpch.LoadTables("0001"); //Tpch.CreateIndexes(); Tpch.AnalyzeTables(); sql = File.ReadAllText("../../../tpch/q20.sql"); goto doit; } if (false) { Tpcds.CreateTables(); Tpcds.LoadTables("tiny"); Tpcds.AnalyzeTables(); // 1, 2,3,7,10, // long time: 4 bad plan // 6: distinct not supported, causing wrong result sql = File.ReadAllText("../../../tpcds/q7.sql"); goto doit; } doit: sql = "select a2,b2,c2,d2 from ad, bd, cd, dd where a2=b2 and c2 = b2 and c2=d2 order by a2"; sql = "select count(*) from ast group by tumble(a0, interval '10' second)"; sql = "select round(a1, 10), count(*) from a group by round(a1, 10)"; sql = "select count(*) from a group by round(a1, 10)"; sql = "select count(*) from ast group by hop(a0, interval '5' second, interval '10' second)"; sql = "select round(a1, 10) from a group by a1;"; sql = "select abs(-a1*2), count(*) from a group by round(a1, 10);"; sql = "select abs(-a1*2), count(*) from a group by a1;"; sql = "select tumble_start(a0, interval '10' second), tumble_end(a0, interval '10' second), count(*) from ast group by tumble(a0, interval '10' second)"; var datetime = new DateTime(); datetime = DateTime.Now; var stopWatch = new Stopwatch(); stopWatch.Start(); // query options might be conflicting or incomplete Console.WriteLine(sql); var a = RawParser.ParseSingleSqlStatement(sql); ExplainOption.show_tablename_ = false; a.queryOpt_.profile_.enabled_ = true; a.queryOpt_.optimize_.enable_subquery_unnest_ = false; a.queryOpt_.optimize_.remove_from_ = false; a.queryOpt_.optimize_.use_memo_ = true; a.queryOpt_.optimize_.enable_cte_plan_ = false; a.queryOpt_.optimize_.use_codegen_ = false; a.queryOpt_.optimize_.memo_disable_crossjoin_ = false; a.queryOpt_.optimize_.memo_use_joinorder_solver_ = false; a.queryOpt_.explain_.show_output_ = true; a.queryOpt_.explain_.show_id_ = false; a.queryOpt_.explain_.show_cost_ = a.queryOpt_.optimize_.use_memo_; // -- Semantic analysis: // - bind the query a.queryOpt_.optimize_.ValidateOptions(); a.Bind(null); // -- generate an initial plan var rawplan = a.CreatePlan(); Console.WriteLine("***************** raw plan *************"); Console.WriteLine(rawplan.Explain()); // -- optimize the plan PhysicNode phyplan = null; if (a.queryOpt_.optimize_.use_memo_) { Console.WriteLine("***************** optimized plan *************"); var optplan = a.SubstitutionOptimize(); Console.WriteLine(optplan.Explain(a.queryOpt_.explain_)); a.optimizer_.InitRootPlan(a); a.optimizer_.OptimizeRootPlan(a, null); Console.WriteLine(a.optimizer_.PrintMemo()); phyplan = a.optimizer_.CopyOutOptimalPlan(); Console.WriteLine(a.optimizer_.PrintMemo()); Console.WriteLine("***************** Memo plan *************"); Console.WriteLine(phyplan.Explain(a.queryOpt_.explain_)); } else { // -- optimize the plan Console.WriteLine("-- optimized plan --"); var optplan = a.SubstitutionOptimize(); Console.WriteLine(optplan.Explain(a.queryOpt_.explain_)); // -- physical plan Console.WriteLine("-- physical plan --"); phyplan = a.physicPlan_; Console.WriteLine(phyplan.Explain(a.queryOpt_.explain_)); } // -- output profile and query result Console.WriteLine("-- profiling plan --"); var final = new PhysicCollect(phyplan); a.physicPlan_ = final; ExecContext context = a.CreateExecContext(); final.ValidateThis(); if (a is SelectStmt select) { select.OpenSubQueries(context); } var code = final.Open(context); code += final.Exec(null); code += final.Close(); if (a.queryOpt_.optimize_.use_codegen_) { CodeWriter.WriteLine(code); Compiler.Run(Compiler.Compile(), a, context); } Console.WriteLine(phyplan.Explain(a.queryOpt_.explain_)); stopWatch.Stop(); Console.WriteLine("RunTime: " + stopWatch.Elapsed); Console.ReadKey(); }
static void Main(string[] args) { Catalog.Init(); string sql = ""; if (args.Length != 0) { sql = args[0]; } #pragma warning disable CS0162 // Unreachable code detected // The warnings are annoying, so using a pragma to suppress them. if (false) { JOBench.CreateTables(); var stats_fn = "../../../../jobench/statistics/jobench_stats"; Catalog.sysstat_.read_serialized_stats(stats_fn); sql = File.ReadAllText("../../../../jobench/10a.sql"); goto doit; } if (false) { Tpch.CreateTables(); Tpch.LoadTables("0001"); //Tpch.CreateIndexes(); Tpch.AnalyzeTables(); sql = File.ReadAllText("../../../../tpch/q20.sql"); goto doit; } if (false) { //84 sql = File.ReadAllText("../../../../tpcds/q33.sql"); Tpcds.CreateTables(); Tpcds.LoadTables("tiny"); Tpcds.AnalyzeTables(); // long time: 4 bad plan // 6: distinct not supported, causing wrong result // q23, q33, q56, q60: in-subquery plan bug // 10,11,13, 31, 38, 41, 48, 54, 66, 72, 74: too slow goto doit; } #pragma warning restore CS0162 // Unreachable code detected doit: bool convMode = false; // Application Arguments in Project properties seem to be // effective only after rebuilding. // This is a last ditch effort to be able to debug arbitrary // statements without rebuilding the solution. string inputFile = ""; if (sql.Length == 2 && sql == "-i") { convMode = true; } else if (sql.Length == 2 && sql.StartsWith("-f")) { inputFile = args[1]; } else if (sql.Length == 0) { sql = "select * from a tablesample row (2);"; } do { if (convMode == true || (sql.Length == 1 && sql.Equals("-"))) { System.Console.Write("QSQL> "); sql = System.Console.ReadLine(); System.Console.WriteLine(sql); } var datetime = new DateTime(); datetime = DateTime.Now; var stopWatch = new Stopwatch(); stopWatch.Start(); if (inputFile.Length != 0) { // read the file and execute all statements in it. RunSQLFromFile(inputFile); goto done; } // query options might be conflicting or incomplete Console.WriteLine(sql); var a = RawParser.ParseSingleSqlStatement(sql); ExplainOption.show_tablename_ = true; a.queryOpt_.profile_.enabled_ = true; a.queryOpt_.optimize_.enable_subquery_unnest_ = true; a.queryOpt_.optimize_.remove_from_ = true; a.queryOpt_.optimize_.use_memo_ = true; a.queryOpt_.optimize_.enable_cte_plan_ = true; a.queryOpt_.optimize_.use_codegen_ = false; a.queryOpt_.optimize_.memo_disable_crossjoin_ = false; a.queryOpt_.optimize_.memo_use_joinorder_solver_ = false; a.queryOpt_.explain_.show_output_ = true; a.queryOpt_.explain_.show_id_ = true; a.queryOpt_.explain_.show_estCost_ = a.queryOpt_.optimize_.use_memo_; a.queryOpt_.explain_.mode_ = ExplainMode.full; // -- Semantic analysis: // - bind the query a.queryOpt_.optimize_.ValidateOptions(); if (!(a is SelectStmt)) { SQLStatement.ExecSQLList(sql); goto done; } a.Bind(null); // -- generate an initial plan var rawplan = a.CreatePlan(); Console.WriteLine("***************** raw plan *************"); Console.WriteLine(rawplan.Explain()); // -- optimize the plan PhysicNode phyplan = null; if (a.queryOpt_.optimize_.use_memo_) { Console.WriteLine("***************** optimized plan *************"); var optplan = a.SubstitutionOptimize(); Console.WriteLine(optplan.Explain(a.queryOpt_.explain_)); a.optimizer_ = new Optimizer(a); a.optimizer_.ExploreRootPlan(a); phyplan = a.optimizer_.CopyOutOptimalPlan(); Console.WriteLine(a.optimizer_.PrintMemo()); Console.WriteLine("***************** Memo plan *************"); Console.WriteLine(phyplan.Explain(a.queryOpt_.explain_)); } else { // -- optimize the plan Console.WriteLine("-- optimized plan --"); var optplan = a.SubstitutionOptimize(); Console.WriteLine(optplan.Explain(a.queryOpt_.explain_)); // -- physical plan Console.WriteLine("-- physical plan --"); phyplan = a.physicPlan_; Console.WriteLine(phyplan.Explain(a.queryOpt_.explain_)); } // -- output profile and query result Console.WriteLine("-- profiling plan --"); var final = new PhysicCollect(phyplan); a.physicPlan_ = final; ExecContext context = a.CreateExecContext(); final.ValidateThis(); if (a is SelectStmt select) { select.OpenSubQueries(context); } final.Open(context); final.Exec(null); final.Close(); if (a.queryOpt_.optimize_.use_codegen_) { CodeWriter.WriteLine(context.code_); Compiler.Run(Compiler.Compile(), a, context); } Console.WriteLine(phyplan.Explain(a.queryOpt_.explain_)); done: stopWatch.Stop(); Console.WriteLine("RunTime: " + stopWatch.Elapsed); } while (convMode == true); Console.ReadKey(); }
public PhysicIndex(LogicIndex logic, PhysicNode l) : base(logic) => children_.Add(l);
override internal PhysicNode Run(JoinGraph graph, BigInteger expectC1) { int ntables = graph.vertices_.Count; var Trees = new List <PhysicNode>(); Console.WriteLine("GOO #tables: " + ntables); // Treees = {R1, R2, ..., Rn} graph_ = graph; foreach (var logic in graph.vertices_) { BitVector contained = 1 << graph.vertices_.IndexOf(logic); logic.tableContained_ = contained; if (graph.memo_ is null) { Trees.Add(new PhysicScanTable(logic)); } else { // vertices are already inserted into memo var cgroup = graph.memo_.LookupCGroup(logic); var logicref = new LogicMemoRef(cgroup); Trees.Add(new PhysicMemoRef(logicref)); } } while (Trees.Count != 1) { PhysicNode Ti = null, Tj = null; PhysicNode bestJoin = null; // find Ti, Tj in Trees s.t. i < j (avoid duplicates) and TixTj is minimal for (int i = 0; i < Trees.Count; i++) { for (int j = i + 1; j < Trees.Count; j++) { var join = CreateMinimalJoinTree(Trees[i], Trees[j], true); if (bestJoin == null || join.Cost() < bestJoin.Cost()) { bestJoin = join; Ti = Trees[i]; Tj = Trees[j]; } } } Debug.Assert(Ti != null && Tj != null); Trees.Remove(Ti); Trees.Remove(Tj); Trees.Add(bestJoin); } // compare with DPccp solver // ideally, DPccp shall always generate better plans since GOO is heuristic - but DPccp does not consider // CP, so in some cases where CP is beneficial, GOO can win // var result = Trees[0]; var dpccp = new DPccp().Run(graph, expectC1); Console.WriteLine(result); if (dpccp.InclusiveCost() < result.InclusiveCost()) { Console.WriteLine("warning: GOO non optimal plan: {0} vs. {1}", dpccp.InclusiveCost(), result.InclusiveCost()); } if (dpccp.Cost() > result.Cost()) { Console.WriteLine("warning: DPCC shall consider CP in the case: {0} vs. {1}", dpccp.InclusiveCost(), result.InclusiveCost()); } return(result); }
public CGroupMember(PhysicNode node, CMemoGroup group) { physic_ = node; group_ = group; Debug.Assert(!(Logic() is LogicMemoRef)); }
public PhysicAnalyze(LogicAnalyze logic, PhysicNode l) : base(logic) => children_.Add(l);