public override void AddToPlanTree()
{
var op = expression.ExpressionType;
var lhsVar = expression.Left.AsReferenceName();
var rhsVar = expression.Right.AsReferenceName();
var lhsVars = expression.Left.DiscoverReferences();
var rhsVars = expression.Right.DiscoverReferences();
var lhsPlan = planner.JoinPlansForColumns(lhsVars);
var rhsPlan = planner.JoinPlansForColumns(rhsVars);
if (lhsPlan != rhsPlan) {
// If either the LHS or the RHS is a single column then we can
// optimize the join.
if (lhsVar != null || rhsVar != null) {
// If right column is a single and left column is not then we must
// reverse the expression.
JoinNode joinNode;
if (lhsVar == null) {
// Reverse the expressions and the operator
joinNode = new JoinNode(rhsPlan.Plan, lhsPlan.Plan, rhsVar, op.Reverse(), expression.Left);
planner.MergePlans(rhsPlan, lhsPlan, joinNode);
} else {
// Otherwise, use it as it is.
joinNode = new JoinNode(lhsPlan.Plan, rhsPlan.Plan, lhsVar, op, expression.Right);
planner.MergePlans(lhsPlan, rhsPlan, joinNode);
}
// Return because we are done
return;
}
}
// If we get here either both the lhs and rhs are complex expressions
// or the lhs and rhs of the variable are not different plans, or
// the operator is not a conditional. Either way, we must evaluate
// this via a natural join of the variables involved coupled with an
// exhaustive select. These types of queries are poor performing.
var columnNames = expression.DiscoverReferences();
var tablePlan = planner.JoinPlansForColumns(columnNames);
tablePlan.UpdatePlan(new ExhaustiveSelectNode(tablePlan.Plan, expression));
}