private QueryTablePlanner Clone() { var copy = new QueryTablePlanner(); int sz = tablePlans.Count; for (int i = 0; i < sz; ++i) { copy.tablePlans.Add(tablePlans[i].Clone()); } // Copy the left and right links in the PlanTableSource for (int i = 0; i < sz; ++i) { var src = tablePlans[i]; var mod = copy.tablePlans[i]; // See how the left plan links to which index, if (src.LeftPlan != null) { int n = IndexOfPlan(src.LeftPlan); mod.LeftJoin(copy.tablePlans[n], src.LeftJoinType, src.LeftOnExpression); } // See how the right plan links to which index, if (src.RightPlan != null) { int n = IndexOfPlan(src.RightPlan); mod.RightJoin(copy.tablePlans[n], src.RightJoinType, src.RightOnExpression); } } return(copy); }
public SimplePatternPlan(QueryTablePlanner planner, ObjectName columnName, SqlExpression expression) : base(0.25f) { this.planner = planner; this.columnName = columnName; this.expression = expression; }
public ComplexSinglePlan(QueryTablePlanner planner, ObjectName columnName, SqlExpression expression) : base(0.8f) { this.planner = planner; this.columnName = columnName; this.expression = expression; }
public ExhaustiveSubQueryPlan(QueryTablePlanner planner, ObjectName[] columnNames, SqlExpression expression) : base(0.85f) { this.planner = planner; this.columnNames = columnNames; this.expression = expression; }
private void PrepareJoins(QueryTablePlanner tablePlanner, SqlQueryExpression queryExpression, QueryExpressionFrom queryFrom, ref SqlExpression searchExpression) { var fromClause = queryExpression.FromClause; bool allInner = true; for (int i = 0; i < fromClause.JoinPartCount; i++) { var joinPart = fromClause.GetJoinPart(i); if (joinPart.JoinType != JoinType.Inner) { allInner = false; } } for (int i = 0; i < fromClause.JoinPartCount; i++) { var joinPart = fromClause.GetJoinPart(i); var joinType = joinPart.JoinType; var onExpression = joinPart.OnExpression; if (allInner) { // If the whole join set is inner joins then simply move the on // expression (if there is one) to the WHERE clause. if (searchExpression != null && onExpression != null) { searchExpression = SqlExpression.And(searchExpression, onExpression); } else if (searchExpression == null) { searchExpression = onExpression; } } else { // Not all inner joins, if (joinType == JoinType.Inner && onExpression == null) { // Regular join with no ON expression, so no preparation necessary } else { // Either an inner join with an ON expression, or an outer join with // ON expression if (onExpression == null) { throw new InvalidOperationException(String.Format("Join of type {0} requires ON expression.", joinType)); } // Resolve the on_expression onExpression = onExpression.Prepare(queryFrom.ExpressionPreparer); // And set it in the planner tablePlanner.JoinAt(i, joinType, onExpression); } } } }
public SimpleSelectPlan(QueryTablePlanner planner, ObjectName columnName, SqlExpressionType op, SqlExpression expression) : base(0.2f) { this.planner = planner; this.columnName = columnName; this.op = op; this.expression = expression; }
private QueryTablePlanner CreateTablePlanner(IRequest context, QueryExpressionFrom queryFrom) { // Set up plans for each table in the from clause of the command. For // sub-queries, we recurse. var tablePlanner = new QueryTablePlanner(); for (int i = 0; i < queryFrom.SourceCount; i++) { var tableSource = queryFrom.GetTableSource(i); IQueryPlanNode plan; if (tableSource is FromTableSubQuerySource) { var subQuerySource = (FromTableSubQuerySource)tableSource; var subQueryExpr = subQuerySource.QueryExpression; var subQueryFrom = subQuerySource.QueryFrom; plan = PlanQuery(context, subQueryExpr, subQueryFrom, null, null); if (!(plan is SubsetNode)) { throw new InvalidOperationException("The root node of a sub-query plan must be a subset."); } var subsetNode = (SubsetNode)plan; subsetNode.SetAliasParentName(subQuerySource.AliasName); } else if (tableSource is FromTableDirectSource) { var directSource = (FromTableDirectSource)tableSource; plan = directSource.QueryPlan; } else { throw new InvalidOperationException(String.Format("The type of FROM source '{0}' is not supported.", tableSource.GetType())); } tablePlanner.AddPlan(plan, tableSource); } return(tablePlanner); }
private TablePlan NaturallyJoinPlans(TablePlan plan1, TablePlan plan2) { JoinType joinType; SqlExpression onExpr; TablePlan leftPlan, rightPlan; // Are the plans linked by common join information? if (plan1.RightPlan == plan2) { joinType = plan1.RightJoinType; onExpr = plan1.RightOnExpression; leftPlan = plan1; rightPlan = plan2; } else if (plan1.LeftPlan == plan2) { joinType = plan1.LeftJoinType; onExpr = plan1.LeftOnExpression; leftPlan = plan2; rightPlan = plan1; } else { // Assertion - make sure no join clashes! if ((plan1.LeftPlan != null && plan2.LeftPlan != null) || (plan1.RightPlan != null && plan2.RightPlan != null)) { throw new InvalidOperationException("Plans can not be naturally join because " + "the left/right join plans clash."); } // Else we must assume a non-dependent join (not an outer join). // Perform a natural join IQueryPlanNode node1 = new NaturalJoinNode(plan1.Plan, plan2.Plan); return MergePlans(plan1, plan2, node1); } // This means plan1 and plan2 are linked by a common join and ON // expression which we evaluate now. bool outerJoin; if (joinType == JoinType.Left) { // Mark the left plan leftPlan.UpdatePlan(new MarkerNode(leftPlan.Plan, "OUTER_JOIN")); outerJoin = true; } else if (joinType == JoinType.Right) { // Mark the right plan rightPlan.UpdatePlan(new MarkerNode(rightPlan.Plan, "OUTER_JOIN")); outerJoin = true; } else if (joinType == JoinType.Inner) { // Inner join with ON expression outerJoin = false; } else { throw new InvalidOperationException(String.Format("Join type ({0}) is not supported.", joinType)); } // Make a Planner object for joining these plans. var planner = new QueryTablePlanner(); planner.AddPlan(leftPlan.Clone()); planner.AddPlan(rightPlan.Clone()); // Evaluate the on expression var node = planner.LogicalEvaluate(onExpr); // If outer join add the left outer join node if (outerJoin) node = new LeftOuterJoinNode(node, "OUTER_JOIN"); // And merge the plans in this set with the new node. return MergePlans(plan1, plan2, node); }
public SubLogicPlan(QueryTablePlanner planner, SqlExpression expression, float optimizeFactor) : base(optimizeFactor) { this.planner = planner; this.expression = expression; }
private QueryTablePlanner Clone() { var copy = new QueryTablePlanner(); int sz = tablePlans.Count; for (int i = 0; i < sz; ++i) { copy.tablePlans.Add(tablePlans[i].Clone()); } // Copy the left and right links in the PlanTableSource for (int i = 0; i < sz; ++i) { var src = tablePlans[i]; var mod = copy.tablePlans[i]; // See how the left plan links to which index, if (src.LeftPlan != null) { int n = IndexOfPlan(src.LeftPlan); mod.LeftJoin(copy.tablePlans[n], src.LeftJoinType, src.LeftOnExpression); } // See how the right plan links to which index, if (src.RightPlan != null) { int n = IndexOfPlan(src.RightPlan); mod.RightJoin(copy.tablePlans[n], src.RightJoinType, src.RightOnExpression); } } return copy; }
public ExhaustiveJoinPlan(QueryTablePlanner planner, SqlExpression expression) : base(0.68f) { this.planner = planner; this.expression = expression; }
public StandardJoinPlan(QueryTablePlanner planner, SqlBinaryExpression expression, float optimizeFactor) : base(optimizeFactor) { this.planner = planner; this.expression = expression; }
public ConstantPlan(QueryTablePlanner planner, SqlExpression expression) : base(0f) { this.planner = planner; this.expression = expression; }
public ExhaustiveSelectPlan(QueryTablePlanner planner, SqlExpression expression) : base(0.82f) { this.planner = planner; this.expression = expression; }
private QueryTablePlanner CreateTablePlanner(IQueryContext context, QueryExpressionFrom queryFrom) { // Set up plans for each table in the from clause of the command. For // sub-queries, we recurse. var tablePlanner = new QueryTablePlanner(); for (int i = 0; i < queryFrom.SourceCount; i++) { var tableSource = queryFrom.GetTableSource(i); IQueryPlanNode plan; if (tableSource is FromTableSubQuerySource) { var subQuerySource = (FromTableSubQuerySource) tableSource; var subQueryExpr = subQuerySource.QueryExpression; var subQueryFrom = subQuerySource.QueryFrom; plan = PlanQuery(context, subQueryExpr, subQueryFrom, null); if (!(plan is SubsetNode)) throw new InvalidOperationException("The root node of a sub-query plan must be a subset."); var subsetNode = (SubsetNode) plan; subsetNode.SetAliasParentName(subQuerySource.AliasName); } else if (tableSource is FromTableDirectSource) { var directSource = (FromTableDirectSource) tableSource; plan = directSource.QueryPlan; } else { throw new InvalidOperationException(String.Format("The type of FROM source '{0}' is not supported.", tableSource.GetType())); } tablePlanner.AddPlan(plan, tableSource); } return tablePlanner; }
private TablePlan NaturallyJoinPlans(TablePlan plan1, TablePlan plan2) { JoinType joinType; SqlExpression onExpr; TablePlan leftPlan, rightPlan; // Are the plans linked by common join information? if (plan1.RightPlan == plan2) { joinType = plan1.RightJoinType; onExpr = plan1.RightOnExpression; leftPlan = plan1; rightPlan = plan2; } else if (plan1.LeftPlan == plan2) { joinType = plan1.LeftJoinType; onExpr = plan1.LeftOnExpression; leftPlan = plan2; rightPlan = plan1; } else { // Assertion - make sure no join clashes! if ((plan1.LeftPlan != null && plan2.LeftPlan != null) || (plan1.RightPlan != null && plan2.RightPlan != null)) { throw new InvalidOperationException("Plans can not be naturally join because " + "the left/right join plans clash."); } // Else we must assume a non-dependent join (not an outer join). // Perform a natural join var node1 = new NaturalJoinNode(plan1.Plan, plan2.Plan); return(MergePlans(plan1, plan2, node1)); } // This means plan1 and plan2 are linked by a common join and ON // expression which we evaluate now. bool outerJoin; if (joinType == JoinType.Left) { // Mark the left plan leftPlan.UpdatePlan(new MarkerNode(leftPlan.Plan, "OUTER_JOIN")); outerJoin = true; } else if (joinType == JoinType.Right) { // Mark the right plan rightPlan.UpdatePlan(new MarkerNode(rightPlan.Plan, "OUTER_JOIN")); outerJoin = true; } else if (joinType == JoinType.Inner) { // Inner join with ON expression outerJoin = false; } else { throw new InvalidOperationException(String.Format("Join type ({0}) is not supported.", joinType)); } // Make a Planner object for joining these plans. var planner = new QueryTablePlanner(); planner.AddPlan(leftPlan.Clone()); planner.AddPlan(rightPlan.Clone()); // Evaluate the on expression var node = planner.LogicalEvaluate(onExpr); // If outer join add the left outer join node if (outerJoin) { node = new LeftOuterJoinNode(node, "OUTER_JOIN"); } // And merge the plans in this set with the new node. return(MergePlans(plan1, plan2, node)); }
public SimpleSubQueryPlan(QueryTablePlanner planner, SqlBinaryExpression expression) : base(0.3f) { this.planner = planner; this.expression = expression; }
private void PrepareJoins(QueryTablePlanner tablePlanner, SqlQueryExpression queryExpression, QueryExpressionFrom queryFrom, ref SqlExpression searchExpression) { var fromClause = queryExpression.FromClause; bool allInner = true; for (int i = 0; i < fromClause.JoinPartCount; i++) { var joinPart = fromClause.GetJoinPart(i); if (joinPart.JoinType != JoinType.Inner) allInner = false; } for (int i = 0; i < fromClause.JoinPartCount; i++) { var joinPart = fromClause.GetJoinPart(i); var joinType = joinPart.JoinType; var onExpression = joinPart.OnExpression; if (allInner) { // If the whole join set is inner joins then simply move the on // expression (if there is one) to the WHERE clause. if (searchExpression != null && onExpression != null) searchExpression = SqlExpression.And(searchExpression, onExpression); } else { // Not all inner joins, if (joinType == JoinType.Inner && onExpression == null) { // Regular join with no ON expression, so no preparation necessary } else { // Either an inner join with an ON expression, or an outer join with // ON expression if (onExpression == null) throw new InvalidOperationException(String.Format("Join of type {0} requires ON expression.", joinType)); // Resolve the on_expression onExpression = onExpression.Prepare(queryFrom.ExpressionPreparer); // And set it in the planner tablePlanner.JoinAt(i, joinType, onExpression); } } } }