/* // $Id: //guest/julian_hyde/saffron/src/main/saffron/rel/Join.java#2 $ // Saffron preprocessor and data engine // Copyright (C) 2002 Julian Hyde <julian.hyde@mail.com> // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Library General Public // License as published by the Free Software Foundation; either // version 2 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Library General Public License for more details. // // You should have received a copy of the GNU Library General Public // License along with this library; if not, write to the // Free Software Foundation, Inc., 59 Temple Place - Suite 330, // Boston, MA 02111-1307, USA. // // See the COPYING file located in the top-level-directory of // the archive of this library for complete text of license. */ package saffron.rel; import openjava.mop.OJClass; import openjava.ptree.Expression; import openjava.ptree.ParseTreeException; import openjava.ptree.Variable; import openjava.ptree.util.EvaluationShuttle; import openjava.ptree.util.SyntheticClass; import openjava.tools.parser.ParserConstants; import saffron.PlanWriter; import saffron.Planner; import saffron.opt.CallingConvention; import saffron.opt.Cluster; import saffron.rel.java.JavaNestedLoopJoin; import saffron.rel.java.JavaProject; import saffron.rel.plan.PlanNestedLoopJoin; import saffron.util.RelEnvironment; import saffron.util.Util; /** * A Join represents two relational expressions joined according to some * condition. **/ public class Join extends Rel { protected Rel left; protected Rel right; protected Expression condition; /** * Values are as {@link openjava.ptree.JoinExpression#getJoinTypeCode}, * except that <code>LEFT</code> is disallowed. **/ protected int joinType; protected static final int INNER = ParserConstants.INNER; protected static final int LEFT = ParserConstants.LEFT; protected static final int FULL = ParserConstants.FULL; public Join( Cluster cluster, Rel left, Rel right, Expression condition, int joinType) { super(cluster); this.left = left; this.right = right; this.condition = condition; Util.assert( joinType == INNER || joinType == LEFT || joinType == FULL); // RIGHT not allowed this.joinType = joinType; } public Object clone() { return new Join( cluster, Util.clone(left), Util.clone(right), Util.clone(condition), joinType); } public Rel getLeft() { return left; } public Rel getRight() { return right; } public Expression getCondition() { return condition; } // implement Rel public Rel[] getInputs() { return new Rel[] {left, right}; } // implement Rel public Expression[] getChildExps() { return new Expression[] {condition}; } // implement Rel public void replaceInput(int ordinalInParent, Rel p) { switch (ordinalInParent) { case 0: this.left = p; break; case 1: this.right = p; break; default: throw Util.newInternal(); } } // implement Rel protected OJClass deriveRowType() { return SyntheticClass.makeJoinType( getTempClass(), left.getRowType(), right.getRowType()); } // // implement QueryPart // public void validate( // Validator validator, QueryPart parent, int ordinalInParent) // { // validator.visit(left, 0, this); // validator.visit(right, 1, this); // try { // // create a resolver which looks in this join // FromResolver resolver = new FromResolver( // validator.resolver, this); // validator.push(resolver); // // validate ON clause // validator.visit(condition, 2, this); // } finally { // validator.pop(); // } // } // override Rel public Rel changeConvention(Planner planner, int convention) { switch (convention) { case CallingConvention.JAVA: Rel convertedLeft = planner.convert(left, convention); if (convertedLeft == null) { return null; } Rel convertedRight = planner.convert(right, convention); if (convertedRight == null) { return null; } Expression convertedCondition = (Expression) condition.makeRecursiveCopy(); // Make sure that left does not depend upon a correlating variable // coming from right, swapping them if necessary. boolean swapped = false; String[] variables = Util.getVariablesSetAndUsed(right, left); if (variables.length > 0) { variables = Util.getVariablesSetAndUsed(left, right); if (variables.length > 0) { throw Util.newInternal( "joined expressions are mutually dependent: " + this); } // Swap left and right. Rel old = left; left = right; right = old; old = convertedLeft; convertedLeft = convertedRight; convertedRight = old; switch (joinType) { case ParserConstants.LEFT: joinType = ParserConstants.RIGHT; break; case ParserConstants.RIGHT: joinType = ParserConstants.LEFT; break; default: } convertedCondition = Util.go( new EvaluationShuttle(null) { public Expression evaluateDown(Variable p) throws ParseTreeException { String name = p.toString(); if (name.equals(RelEnvironment.makeName(0))) { return RelEnvironment.makeReference(1); } else if (name.equals(RelEnvironment.makeName(1))) { return RelEnvironment.makeReference(0); } else { return super.evaluateDown(p); } } }, convertedCondition); swapped = true; } JavaNestedLoopJoin join = new JavaNestedLoopJoin( cluster, convertedLeft, convertedRight, convertedCondition, joinType); if (swapped) { return new JavaProject( cluster, join, new Expression[] { RelEnvironment.makeFieldAccess(0, 1), RelEnvironment.makeFieldAccess(0, 0)}, null, Project.flagBoxed | Project.flagAnonFields); } else { return join; } case CallingConvention.PLAN: convertedLeft = planner.convert(left, convention); if (convertedLeft == null) { return null; } convertedRight = planner.convert(right, convention); if (convertedRight == null) { return null; } return new PlanNestedLoopJoin( cluster, convertedLeft, convertedRight, condition, joinType); default: return null; } } // override Exp public void explain(PlanWriter pw) { pw.explain(this, new String[] {"left", "right", "condition"}); } } // End Join.java
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#2 | 1801 | Julian Hyde |
saffron: add ObjectSchema; rules can now be matched more than once; started to implement correlations in queries in from list. |
||
#1 | 1467 | Julian Hyde |
saffron: First saffron check-in; incorporate my changes to openjava. |