Author: gates Date: Fri Jan 15 00:53:47 2010 New Revision: 899502 URL: http://svn.apache.org/viewvc?rev=899502&view=rev Log: PIG-1178. Commit expressions-2.patch.
Added: hadoop/pig/trunk/src/org/apache/pig/experimental/logical/expression/AndExpression.java hadoop/pig/trunk/src/org/apache/pig/experimental/logical/expression/BinaryExpression.java hadoop/pig/trunk/src/org/apache/pig/experimental/logical/expression/ColumnExpression.java hadoop/pig/trunk/src/org/apache/pig/experimental/logical/expression/ConstantExpression.java hadoop/pig/trunk/src/org/apache/pig/experimental/logical/expression/EqualExpression.java hadoop/pig/trunk/src/org/apache/pig/experimental/logical/expression/LogicalExpressionVisitor.java hadoop/pig/trunk/src/org/apache/pig/experimental/logical/expression/ProjectExpression.java Modified: hadoop/pig/trunk/src/org/apache/pig/experimental/logical/expression/LogicalExpression.java hadoop/pig/trunk/src/org/apache/pig/experimental/logical/relational/LogicalPlanVisitor.java hadoop/pig/trunk/src/org/apache/pig/experimental/logical/relational/LogicalSchema.java hadoop/pig/trunk/src/org/apache/pig/experimental/plan/DependencyOrderWalker.java hadoop/pig/trunk/src/org/apache/pig/experimental/plan/DepthFirstWalker.java hadoop/pig/trunk/src/org/apache/pig/experimental/plan/Operator.java hadoop/pig/trunk/src/org/apache/pig/experimental/plan/PlanVisitor.java hadoop/pig/trunk/src/org/apache/pig/experimental/plan/PlanWalker.java hadoop/pig/trunk/src/org/apache/pig/experimental/plan/ReverseDependencyOrderWalker.java hadoop/pig/trunk/test/org/apache/pig/test/TestExperimentalOperatorPlan.java Added: hadoop/pig/trunk/src/org/apache/pig/experimental/logical/expression/AndExpression.java URL: http://svn.apache.org/viewvc/hadoop/pig/trunk/src/org/apache/pig/experimental/logical/expression/AndExpression.java?rev=899502&view=auto ============================================================================== --- hadoop/pig/trunk/src/org/apache/pig/experimental/logical/expression/AndExpression.java (added) +++ hadoop/pig/trunk/src/org/apache/pig/experimental/logical/expression/AndExpression.java Fri Jan 15 00:53:47 2010 @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.pig.experimental.logical.expression; + +import org.apache.pig.data.DataType; +import org.apache.pig.experimental.plan.OperatorPlan; +import org.apache.pig.experimental.plan.PlanVisitor; + +/** + * Boolean and expression. + */ +public class AndExpression extends BinaryExpression { + + /** + * Will add this operator to the plan and connect it to the + * left and right hand side operators. + * @param plan plan this operator is part of + * @param lhs expression on its left hand side + * @param rhs expression on its right hand side + */ + public AndExpression(OperatorPlan plan, + LogicalExpression lhs, + LogicalExpression rhs) { + super("And", plan, DataType.BOOLEAN, lhs, rhs); + } + + /** + * @link org.apache.pig.experimental.plan.Operator#accept(org.apache.pig.experimental.plan.PlanVisitor) + */ + @Override + public void accept(PlanVisitor v) { + if (!(v instanceof LogicalExpressionVisitor)) { + throw new RuntimeException("Expected LogicalExpressionVisitor"); + } + ((LogicalExpressionVisitor)v).visitAnd(this); + } + +} Added: hadoop/pig/trunk/src/org/apache/pig/experimental/logical/expression/BinaryExpression.java URL: http://svn.apache.org/viewvc/hadoop/pig/trunk/src/org/apache/pig/experimental/logical/expression/BinaryExpression.java?rev=899502&view=auto ============================================================================== --- hadoop/pig/trunk/src/org/apache/pig/experimental/logical/expression/BinaryExpression.java (added) +++ hadoop/pig/trunk/src/org/apache/pig/experimental/logical/expression/BinaryExpression.java Fri Jan 15 00:53:47 2010 @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.pig.experimental.logical.expression; + +import java.io.IOException; + +import org.apache.pig.experimental.plan.OperatorPlan; + +/** + * Superclass for all binary expressions + * + */ +public abstract class BinaryExpression extends LogicalExpression { + + /** + * Will add this operator to the plan and connect it to the + * left and right hand side operators. + * @param name of the operator + * @param plan plan this operator is part of + * @param b Datatype of this expression + * @param lhs expression on its left hand side + * @param rhs expression on its right hand side + */ + public BinaryExpression(String name, + OperatorPlan plan, + byte b, + LogicalExpression lhs, + LogicalExpression rhs) { + super(name, plan, b); + plan.add(this); + plan.connect(this, lhs); + plan.connect(this, rhs); + } + + /** + * Get the left hand side of this binary expression. + * @return expression on the left hand side + * @throws IOException + */ + public LogicalExpression getLhs() throws IOException { + return (LogicalExpression)plan.getSuccessors(this).get(0); + + } + + /** + * Get the right hand side of this binary expression. + * @return expression on the right hand side + * @throws IOException + */ + public LogicalExpression getRhs() throws IOException { + return (LogicalExpression)plan.getSuccessors(this).get(1); + + } + +} Added: hadoop/pig/trunk/src/org/apache/pig/experimental/logical/expression/ColumnExpression.java URL: http://svn.apache.org/viewvc/hadoop/pig/trunk/src/org/apache/pig/experimental/logical/expression/ColumnExpression.java?rev=899502&view=auto ============================================================================== --- hadoop/pig/trunk/src/org/apache/pig/experimental/logical/expression/ColumnExpression.java (added) +++ hadoop/pig/trunk/src/org/apache/pig/experimental/logical/expression/ColumnExpression.java Fri Jan 15 00:53:47 2010 @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.pig.experimental.logical.expression; + +import org.apache.pig.experimental.plan.OperatorPlan; + +/** + * Super class for all column expressions, including projection, constants, and deferences. + * + */ +public abstract class ColumnExpression extends LogicalExpression { + + /** + * + * @param name of the operator + * @param plan LogicalExpressionPlan this column expression is part of + * @param type datatype of this column expression + */ + public ColumnExpression(String name, OperatorPlan plan, byte type) { + super(name, plan, type); + } + +} Added: hadoop/pig/trunk/src/org/apache/pig/experimental/logical/expression/ConstantExpression.java URL: http://svn.apache.org/viewvc/hadoop/pig/trunk/src/org/apache/pig/experimental/logical/expression/ConstantExpression.java?rev=899502&view=auto ============================================================================== --- hadoop/pig/trunk/src/org/apache/pig/experimental/logical/expression/ConstantExpression.java (added) +++ hadoop/pig/trunk/src/org/apache/pig/experimental/logical/expression/ConstantExpression.java Fri Jan 15 00:53:47 2010 @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.pig.experimental.logical.expression; + +import org.apache.pig.experimental.plan.OperatorPlan; +import org.apache.pig.experimental.plan.PlanVisitor; + +/** + * A constant + * + */ +public class ConstantExpression extends ColumnExpression { + + // Stupid Java needs a union + Object val; + + /** + * Adds expression to the plan + * @param plan LogicalExpressionPlan this constant is a part of. + * @param type type of the constant. This could be determined dynamically, + * but it would require a long chain of instanceofs, and the parser will + * already know the type, so there's no reason to take the performance hit. + * @param val Value of this constant. + */ + public ConstantExpression(OperatorPlan plan, byte type, Object val) { + super("Constant", plan, type); + this.val = val; + plan.add(this); + } + + /** + * @link org.apache.pig.experimental.plan.Operator#accept(org.apache.pig.experimental.plan.PlanVisitor) + */ + @Override + public void accept(PlanVisitor v) { + if (!(v instanceof LogicalExpressionVisitor)) { + throw new RuntimeException("Expected LogicalExpressionVisitor"); + } + ((LogicalExpressionVisitor)v).visitConstant(this); + + } + + /** + * Get the value of this constant. + * @return value of the constant + */ + public Object getValue() { + return val; + } + +} Added: hadoop/pig/trunk/src/org/apache/pig/experimental/logical/expression/EqualExpression.java URL: http://svn.apache.org/viewvc/hadoop/pig/trunk/src/org/apache/pig/experimental/logical/expression/EqualExpression.java?rev=899502&view=auto ============================================================================== --- hadoop/pig/trunk/src/org/apache/pig/experimental/logical/expression/EqualExpression.java (added) +++ hadoop/pig/trunk/src/org/apache/pig/experimental/logical/expression/EqualExpression.java Fri Jan 15 00:53:47 2010 @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.pig.experimental.logical.expression; + +import org.apache.pig.data.DataType; +import org.apache.pig.experimental.plan.OperatorPlan; +import org.apache.pig.experimental.plan.PlanVisitor; + +/** + * Equality test expression. + */ +public class EqualExpression extends BinaryExpression { + + /** + * Will add this operator to the plan and connect it to the + * left and right hand side operators. + * @param plan plan this operator is part of + * @param lhs expression on its left hand side + * @param rhs expression on its right hand side + */ + public EqualExpression(OperatorPlan plan, + LogicalExpression lhs, + LogicalExpression rhs) { + super("Equal", plan, DataType.BOOLEAN, lhs, rhs); + } + + /** + * @link org.apache.pig.experimental.plan.Operator#accept(org.apache.pig.experimental.plan.PlanVisitor) + */ + @Override + public void accept(PlanVisitor v) { + if (!(v instanceof LogicalExpressionVisitor)) { + throw new RuntimeException( + "Expected LogicalExpressionVisitor"); + } + ((LogicalExpressionVisitor)v).visitEqual(this); + } + +} Modified: hadoop/pig/trunk/src/org/apache/pig/experimental/logical/expression/LogicalExpression.java URL: http://svn.apache.org/viewvc/hadoop/pig/trunk/src/org/apache/pig/experimental/logical/expression/LogicalExpression.java?rev=899502&r1=899501&r2=899502&view=diff ============================================================================== --- hadoop/pig/trunk/src/org/apache/pig/experimental/logical/expression/LogicalExpression.java (original) +++ hadoop/pig/trunk/src/org/apache/pig/experimental/logical/expression/LogicalExpression.java Fri Jan 15 00:53:47 2010 @@ -18,7 +18,6 @@ package org.apache.pig.experimental.logical.expression; -import org.apache.pig.data.DataType; import org.apache.pig.experimental.plan.Operator; import org.apache.pig.experimental.plan.OperatorPlan; @@ -29,19 +28,42 @@ */ public abstract class LogicalExpression extends Operator { - protected DataType type; + protected byte type; protected long uid; - public LogicalExpression(String name, OperatorPlan plan) { + /** + * + * @param name of the operator + * @param plan LogicalExpressionPlan this is part of + * @param b datatype of this expression + */ + public LogicalExpression(String name, OperatorPlan plan, byte b) { super(name, plan); + type = b; } - public DataType getType() { + /** + * Get the data type for this expression. + * @return data type, one of the static bytes of DataType + */ + public byte getType() { return type; } + /** + * Get the unique identifier for this expression + * @return unique identifier + */ public long getUid() { return uid; } + + /** + * Set the unique identify for this expression + * @param uid unique identifier + */ + public void setUid(long uid) { + this.uid = uid; + } } Added: hadoop/pig/trunk/src/org/apache/pig/experimental/logical/expression/LogicalExpressionVisitor.java URL: http://svn.apache.org/viewvc/hadoop/pig/trunk/src/org/apache/pig/experimental/logical/expression/LogicalExpressionVisitor.java?rev=899502&view=auto ============================================================================== --- hadoop/pig/trunk/src/org/apache/pig/experimental/logical/expression/LogicalExpressionVisitor.java (added) +++ hadoop/pig/trunk/src/org/apache/pig/experimental/logical/expression/LogicalExpressionVisitor.java Fri Jan 15 00:53:47 2010 @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.pig.experimental.logical.expression; + +import org.apache.pig.experimental.plan.OperatorPlan; +import org.apache.pig.experimental.plan.PlanVisitor; +import org.apache.pig.experimental.plan.PlanWalker; + +/** + * A visitor for expression plans. + */ +public abstract class LogicalExpressionVisitor extends PlanVisitor { + + protected LogicalExpressionVisitor(OperatorPlan p, + PlanWalker walker) { + super(p, walker); + + if (!(plan instanceof LogicalExpressionPlan)) { + throw new RuntimeException( + "LogicalExpressionVisitor expects to visit " + + "expression plans."); + } + } + + public void visitAnd(AndExpression andExpr) { + } + + public void visitEqual(EqualExpression equal) { + } + + public void visitProject(ProjectExpression project) { + } + + public void visitConstant(ConstantExpression constant) { + } + + + +} Added: hadoop/pig/trunk/src/org/apache/pig/experimental/logical/expression/ProjectExpression.java URL: http://svn.apache.org/viewvc/hadoop/pig/trunk/src/org/apache/pig/experimental/logical/expression/ProjectExpression.java?rev=899502&view=auto ============================================================================== --- hadoop/pig/trunk/src/org/apache/pig/experimental/logical/expression/ProjectExpression.java (added) +++ hadoop/pig/trunk/src/org/apache/pig/experimental/logical/expression/ProjectExpression.java Fri Jan 15 00:53:47 2010 @@ -0,0 +1,107 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.pig.experimental.logical.expression; + +import org.apache.pig.experimental.plan.OperatorPlan; +import org.apache.pig.experimental.plan.PlanVisitor; + +/** + * Projection of columns in an expression. + * + */ +public class ProjectExpression extends ColumnExpression { + + private int input; // Which input of the relational operator this project + // is projecting from. Count is zero based. So if this + // project is in a filter the input number will always + // be 0 (since filter has only one input). If it is + // in a join, cross, cogroup, or union it could be + // greater than 0. + private int col; // The column in the input which the project references. + // Count is zero based. + + + /** + * Adds projection to the plan. + * @param plan LogicalExpressionPlan this projection will be a part of + * @param type type of this projection, can be unknown + * @param inputNum Input number this project references. + * @param colNum Column number this project references. + */ + public ProjectExpression(OperatorPlan plan, + byte type, + int inputNum, + int colNum) { + super("Project", plan, type); + input = inputNum; + col = colNum; + plan.add(this); + } + + /** + * @link org.apache.pig.experimental.plan.Operator#accept(org.apache.pig.experimental.plan.PlanVisitor) + */ + @Override + public void accept(PlanVisitor v) { + if (!(v instanceof LogicalExpressionVisitor)) { + throw new RuntimeException("Expected LogicalExpressionVisitor"); + } + ((LogicalExpressionVisitor)v).visitProject(this); + + } + + /** + * Input number this project references. This is the input number for the + * relational operator that contains this expression. The count is zero + * based. + * @return input number + */ + public int getInputNum() { + return input; + } + + + /** + * Column number this project references. The column number is the column + * in the relational operator that contains this expression. The count + * is zero based. + * @return column number + */ + public int getColNum() { + return col; + } + + /** + * Set the column number for this project. This should only be called by + * ProjectionPatcher. Stupid Java needs friends. + * @param colNum new column number for projection + */ + public void setColNum(int colNum) { + col = colNum; + } + + /** + * Set the type of the projection. + * @param type to set this projection to + */ + public void setType(byte type) { + this.type = type; + } + +} Modified: hadoop/pig/trunk/src/org/apache/pig/experimental/logical/relational/LogicalPlanVisitor.java URL: http://svn.apache.org/viewvc/hadoop/pig/trunk/src/org/apache/pig/experimental/logical/relational/LogicalPlanVisitor.java?rev=899502&r1=899501&r2=899502&view=diff ============================================================================== --- hadoop/pig/trunk/src/org/apache/pig/experimental/logical/relational/LogicalPlanVisitor.java (original) +++ hadoop/pig/trunk/src/org/apache/pig/experimental/logical/relational/LogicalPlanVisitor.java Fri Jan 15 00:53:47 2010 @@ -36,7 +36,7 @@ } } - protected void visitLOLoad(LOLoad load) { + public void visitLOLoad(LOLoad load) { } } Modified: hadoop/pig/trunk/src/org/apache/pig/experimental/logical/relational/LogicalSchema.java URL: http://svn.apache.org/viewvc/hadoop/pig/trunk/src/org/apache/pig/experimental/logical/relational/LogicalSchema.java?rev=899502&r1=899501&r2=899502&view=diff ============================================================================== --- hadoop/pig/trunk/src/org/apache/pig/experimental/logical/relational/LogicalSchema.java (original) +++ hadoop/pig/trunk/src/org/apache/pig/experimental/logical/relational/LogicalSchema.java Fri Jan 15 00:53:47 2010 @@ -33,6 +33,7 @@ public static class LogicalFieldSchema { public String alias; public DataType type; + public long uid; public LogicalSchema schema; } @@ -44,6 +45,10 @@ aliases = new HashMap<String, Integer>(); } + /** + * Add a field to this schema. + * @param field to be added to the schema + */ public void addField(LogicalFieldSchema field) { fields.add(field); if (field.alias != null && field.alias.equals("")) { @@ -51,18 +56,48 @@ } } + /** + * Fetch a field by alias + * @param alias + * @return field associated with alias, or null if no such field + */ public LogicalFieldSchema getField(String alias) { - return null; + Integer i = aliases.get(alias); + if (i == null) return null; + else return fields.get(i); } + /** + * Fetch a field by field number + * @param fieldNum field number to fetch + * @return field + */ public LogicalFieldSchema getField(int fieldNum) { return fields.get(fieldNum); } + + /** + * Get all fields + * @return list of all fields + */ + public List<LogicalFieldSchema> getFields() { + return fields; + } + /** + * Get the size of the schema. + * @return size + */ public Integer size() { return null; } + /** + * Merge two schemas. + * @param s1 + * @param s2 + * @return a merged schema, or null if the merge fails + */ public static LogicalSchema merge(LogicalSchema s1, LogicalSchema s2) { // TODO return null; Modified: hadoop/pig/trunk/src/org/apache/pig/experimental/plan/DependencyOrderWalker.java URL: http://svn.apache.org/viewvc/hadoop/pig/trunk/src/org/apache/pig/experimental/plan/DependencyOrderWalker.java?rev=899502&r1=899501&r2=899502&view=diff ============================================================================== --- hadoop/pig/trunk/src/org/apache/pig/experimental/plan/DependencyOrderWalker.java (original) +++ hadoop/pig/trunk/src/org/apache/pig/experimental/plan/DependencyOrderWalker.java Fri Jan 15 00:53:47 2010 @@ -27,6 +27,12 @@ import org.apache.pig.impl.plan.VisitorException; +/** + * A walker to walk graphs in dependency order. It is guaranteed that a node + * will not be visited until all of its predecessors have been visited. This + * is equivalent to doing a topilogical sort on the graph and then visiting + * the nodes in order. + */ public class DependencyOrderWalker extends PlanWalker { /** @@ -47,7 +53,7 @@ * @throws VisitorException if an error is encountered while walking. */ @Override - public void walk(PlanVisitor visitor) throws VisitorException { + public void walk(PlanVisitor visitor) throws IOException { // This is highly inefficient, but our graphs are small so it should be okay. // The algorithm works by starting at any node in the graph, finding it's // predecessors and calling itself for each of those predecessors. When it @@ -71,24 +77,19 @@ protected void doAllPredecessors(Operator node, Set<Operator> seen, - Collection<Operator> fifo) throws VisitorException { + Collection<Operator> fifo) throws IOException { if (!seen.contains(node)) { // We haven't seen this one before. - try { - Collection<Operator> preds = plan.getPredecessors(node); - if (preds != null && preds.size() > 0) { - // Do all our predecessors before ourself - for (Operator op : preds) { - doAllPredecessors(op, seen, fifo); - } + Collection<Operator> preds = plan.getPredecessors(node); + if (preds != null && preds.size() > 0) { + // Do all our predecessors before ourself + for (Operator op : preds) { + doAllPredecessors(op, seen, fifo); } - }catch(IOException e) { - throw new VisitorException(e); } // Now do ourself seen.add(node); fifo.add(node); - } } } Modified: hadoop/pig/trunk/src/org/apache/pig/experimental/plan/DepthFirstWalker.java URL: http://svn.apache.org/viewvc/hadoop/pig/trunk/src/org/apache/pig/experimental/plan/DepthFirstWalker.java?rev=899502&r1=899501&r2=899502&view=diff ============================================================================== --- hadoop/pig/trunk/src/org/apache/pig/experimental/plan/DepthFirstWalker.java (original) +++ hadoop/pig/trunk/src/org/apache/pig/experimental/plan/DepthFirstWalker.java Fri Jan 15 00:53:47 2010 @@ -24,8 +24,9 @@ import java.util.List; import java.util.Set; -import org.apache.pig.impl.plan.VisitorException; - +/** + * Do a depth first traversal of the graph. + */ public class DepthFirstWalker extends PlanWalker { public DepthFirstWalker(OperatorPlan plan) { @@ -43,7 +44,7 @@ * @throws IOException if an error is encountered while walking. */ @Override - public void walk(PlanVisitor visitor) throws VisitorException { + public void walk(PlanVisitor visitor) throws IOException { List<Operator> roots = plan.getRoots(); Set<Operator> seen = new HashSet<Operator>(); @@ -53,18 +54,14 @@ private void depthFirst(Operator node, Collection<Operator> successors, Set<Operator> seen, - PlanVisitor visitor) throws VisitorException { + PlanVisitor visitor) throws IOException { if (successors == null) return; for (Operator suc : successors) { if (seen.add(suc)) { suc.accept(visitor); - try { - Collection<Operator> newSuccessors = plan.getSuccessors(suc); - depthFirst(suc, newSuccessors, seen, visitor); - }catch(IOException e) { - throw new VisitorException(e); - } + Collection<Operator> newSuccessors = plan.getSuccessors(suc); + depthFirst(suc, newSuccessors, seen, visitor); } } } Modified: hadoop/pig/trunk/src/org/apache/pig/experimental/plan/Operator.java URL: http://svn.apache.org/viewvc/hadoop/pig/trunk/src/org/apache/pig/experimental/plan/Operator.java?rev=899502&r1=899501&r2=899502&view=diff ============================================================================== --- hadoop/pig/trunk/src/org/apache/pig/experimental/plan/Operator.java (original) +++ hadoop/pig/trunk/src/org/apache/pig/experimental/plan/Operator.java Fri Jan 15 00:53:47 2010 @@ -18,14 +18,19 @@ package org.apache.pig.experimental.plan; +import java.util.HashMap; +import java.util.Map; + public abstract class Operator { protected String name; protected OperatorPlan plan; // plan that contains this operator + protected Map<String, Object> annotations; public Operator(String n, OperatorPlan p) { name = n; plan = p; + annotations = new HashMap<String, Object>(); } /** @@ -45,5 +50,24 @@ public OperatorPlan getPlan() { return plan; } + + /** + * Add an annotation to a node in the plan. + * @param key string name of this annotation + * @param val value, as an Object + */ + public void annotate(String key, Object val) { + annotations.put(key, val); + } + + /** + * Look to see if a node is annotated. + * @param key string name of annotation to look for + * @return value of the annotation, as an Object, or null if the key is + * not present in the map. + */ + public Object getAnnotation(String key) { + return annotations.get(key); + } } Modified: hadoop/pig/trunk/src/org/apache/pig/experimental/plan/PlanVisitor.java URL: http://svn.apache.org/viewvc/hadoop/pig/trunk/src/org/apache/pig/experimental/plan/PlanVisitor.java?rev=899502&r1=899501&r2=899502&view=diff ============================================================================== --- hadoop/pig/trunk/src/org/apache/pig/experimental/plan/PlanVisitor.java (original) +++ hadoop/pig/trunk/src/org/apache/pig/experimental/plan/PlanVisitor.java Fri Jan 15 00:53:47 2010 @@ -18,6 +18,7 @@ package org.apache.pig.experimental.plan; +import java.io.IOException; import java.util.Stack; import org.apache.pig.impl.plan.VisitorException; @@ -43,7 +44,7 @@ * Entry point for visiting the plan. * @throws VisitorException if an error is encountered while visiting. */ - public void visit() throws VisitorException { + public void visit() throws IOException { currentWalker.walk(this); } @@ -52,11 +53,11 @@ } /** - * @param p OperatorPlan this visitor will visit. + * @param plan OperatorPlan this visitor will visit. * @param walker PlanWalker this visitor will use to traverse the plan. */ - protected PlanVisitor(OperatorPlan p, PlanWalker walker) { - plan = p; + protected PlanVisitor(OperatorPlan plan, PlanWalker walker) { + this.plan = plan; currentWalker = walker; walkers = new Stack<PlanWalker>(); } Modified: hadoop/pig/trunk/src/org/apache/pig/experimental/plan/PlanWalker.java URL: http://svn.apache.org/viewvc/hadoop/pig/trunk/src/org/apache/pig/experimental/plan/PlanWalker.java?rev=899502&r1=899501&r2=899502&view=diff ============================================================================== --- hadoop/pig/trunk/src/org/apache/pig/experimental/plan/PlanWalker.java (original) +++ hadoop/pig/trunk/src/org/apache/pig/experimental/plan/PlanWalker.java Fri Jan 15 00:53:47 2010 @@ -18,17 +18,17 @@ package org.apache.pig.experimental.plan; -import org.apache.pig.impl.plan.VisitorException; +import java.io.IOException; public abstract class PlanWalker { protected OperatorPlan plan; /** - * @param p Plan for this walker to traverse. + * @param plan Plan for this walker to traverse. */ - public PlanWalker(OperatorPlan p) { - plan = p; + public PlanWalker(OperatorPlan plan) { + this.plan = plan; } /** @@ -38,7 +38,7 @@ * not yet have a 'this' pointer to send as an argument. * @throws VisitorException if an error is encountered while walking. */ - public abstract void walk(PlanVisitor visitor) throws VisitorException; + public abstract void walk(PlanVisitor visitor) throws IOException; /** * Return a new instance of this same type of walker for a subplan. @@ -55,8 +55,12 @@ return plan ; } - public void setPlan(OperatorPlan p) { - plan = p; + /** + * Set the plan for this walker to operate on. + * @param plan to walk + */ + public void setPlan(OperatorPlan plan) { + this.plan = plan; } } Modified: hadoop/pig/trunk/src/org/apache/pig/experimental/plan/ReverseDependencyOrderWalker.java URL: http://svn.apache.org/viewvc/hadoop/pig/trunk/src/org/apache/pig/experimental/plan/ReverseDependencyOrderWalker.java?rev=899502&r1=899501&r2=899502&view=diff ============================================================================== --- hadoop/pig/trunk/src/org/apache/pig/experimental/plan/ReverseDependencyOrderWalker.java (original) +++ hadoop/pig/trunk/src/org/apache/pig/experimental/plan/ReverseDependencyOrderWalker.java Fri Jan 15 00:53:47 2010 @@ -25,8 +25,11 @@ import java.util.List; import java.util.Set; -import org.apache.pig.impl.plan.VisitorException; - +/** + * Visit a plan in the reverse of the dependency order. That is, every node + * after every node that depends on it is visited. Thus this is equivalent to + * doing a reverse topilogical sort on the graph and then visiting it in order. + */ public class ReverseDependencyOrderWalker extends PlanWalker { public ReverseDependencyOrderWalker(OperatorPlan plan) { @@ -44,7 +47,7 @@ * @throws VisitorException if an error is encountered while walking. */ @Override - public void walk(PlanVisitor visitor) throws VisitorException { + public void walk(PlanVisitor visitor) throws IOException { // This is highly inefficient, but our graphs are small so it should be okay. // The algorithm works by starting at any node in the graph, finding it's // successors and calling itself for each of those successors. When it @@ -68,19 +71,15 @@ protected void doAllSuccessors(Operator node, Set<Operator> seen, - Collection<Operator> fifo) throws VisitorException { + Collection<Operator> fifo) throws IOException { if (!seen.contains(node)) { // We haven't seen this one before. - try { - Collection<Operator> succs = plan.getSuccessors(node); - if (succs != null && succs.size() > 0) { - // Do all our successors before ourself - for (Operator op : succs) { - doAllSuccessors(op, seen, fifo); - } + Collection<Operator> succs = plan.getSuccessors(node); + if (succs != null && succs.size() > 0) { + // Do all our successors before ourself + for (Operator op : succs) { + doAllSuccessors(op, seen, fifo); } - }catch(IOException e) { - throw new VisitorException(e); } // Now do ourself seen.add(node); Modified: hadoop/pig/trunk/test/org/apache/pig/test/TestExperimentalOperatorPlan.java URL: http://svn.apache.org/viewvc/hadoop/pig/trunk/test/org/apache/pig/test/TestExperimentalOperatorPlan.java?rev=899502&r1=899501&r2=899502&view=diff ============================================================================== --- hadoop/pig/trunk/test/org/apache/pig/test/TestExperimentalOperatorPlan.java (original) +++ hadoop/pig/trunk/test/org/apache/pig/test/TestExperimentalOperatorPlan.java Fri Jan 15 00:53:47 2010 @@ -22,6 +22,17 @@ import java.util.Collection; import java.util.List; +import org.apache.pig.data.DataType; +import org.apache.pig.experimental.logical.expression.AndExpression; +import org.apache.pig.experimental.logical.expression.ConstantExpression; +import org.apache.pig.experimental.logical.expression.EqualExpression; +import org.apache.pig.experimental.logical.expression.LogicalExpressionPlan; +import org.apache.pig.experimental.logical.expression.LogicalExpressionVisitor; +import org.apache.pig.experimental.logical.expression.ProjectExpression; +import org.apache.pig.experimental.logical.relational.LOLoad; +import org.apache.pig.experimental.logical.relational.LogicalPlan; +import org.apache.pig.experimental.logical.relational.LogicalPlanVisitor; +import org.apache.pig.experimental.logical.relational.LogicalRelationalOperator; import org.apache.pig.experimental.plan.BaseOperatorPlan; import org.apache.pig.experimental.plan.DependencyOrderWalker; import org.apache.pig.experimental.plan.DepthFirstWalker; @@ -642,5 +653,96 @@ fail(); } } + + private static class TestLogicalVisitor extends LogicalPlanVisitor { + + StringBuffer bf = new StringBuffer(); + + protected TestLogicalVisitor(OperatorPlan plan) { + super(plan, new DepthFirstWalker(plan)); + } + + @Override + public void visitLOLoad(LOLoad load) { + bf.append("load "); + } + + String getVisitPlan() { + return bf.toString(); + } + + } + + @Test + public void testLogicalPlanVisitor() throws IOException { + LogicalPlan lp = new LogicalPlan(); + LOLoad load = new LOLoad(null, null, lp); + lp.add((LogicalRelationalOperator)null, load, + (LogicalRelationalOperator)null); + + TestLogicalVisitor v = new TestLogicalVisitor(lp); + v.visit(); + + assertEquals("load ", v.getVisitPlan()); + } + + @Test + public void testBinaryOperatorOrder() throws IOException { + LogicalExpressionPlan ep = new LogicalExpressionPlan(); + ConstantExpression c = new ConstantExpression(ep, DataType.INTEGER, new Integer(5)); + ProjectExpression p = new ProjectExpression(ep, DataType.INTEGER, 0, 0); + EqualExpression e = new EqualExpression(ep, p, c); + assertEquals(p, e.getLhs()); + assertEquals(c, e.getRhs()); + + } + + private static class TestExpressionVisitor extends LogicalExpressionVisitor { + + StringBuffer bf = new StringBuffer(); + protected TestExpressionVisitor(OperatorPlan plan) { + super(plan, new DepthFirstWalker(plan)); + } + + @Override + public void visitAnd(AndExpression andExpr) { + bf.append("and "); + } + + @Override + public void visitEqual(EqualExpression equal) { + bf.append("equal "); + } + + @Override + public void visitProject(ProjectExpression project) { + bf.append("project "); + } + + @Override + public void visitConstant(ConstantExpression constant) { + bf.append("constant "); + } + + String getVisitPlan() { + return bf.toString(); + } + + } + + @Test + public void testExpressionPlanVisitor() throws IOException { + LogicalExpressionPlan ep = new LogicalExpressionPlan(); + ConstantExpression c = new ConstantExpression(ep, DataType.INTEGER, new Integer(5)); + ProjectExpression p = new ProjectExpression(ep, DataType.INTEGER, 0, 0); + EqualExpression e = new EqualExpression(ep, p, c); + ConstantExpression c2 = new ConstantExpression(ep, DataType.BOOLEAN, new Boolean("true")); + new AndExpression(ep, e, c2); + + TestExpressionVisitor v = new TestExpressionVisitor(ep); + v.visit(); + assertEquals("and equal project constant constant ", v.getVisitPlan()); + } + }