You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pig.apache.org by ol...@apache.org on 2008/09/26 23:23:35 UTC

svn commit: r699505 [1/2] - in /incubator/pig/branches/types/src/org/apache/pig/pen: ./ util/

Author: olga
Date: Fri Sep 26 14:23:35 2008
New Revision: 699505

URL: http://svn.apache.org/viewvc?rev=699505&view=rev
Log:
missing illustrate files

Added:
    incubator/pig/branches/types/src/org/apache/pig/pen/
    incubator/pig/branches/types/src/org/apache/pig/pen/AugmentBaseDataVisitor.java
    incubator/pig/branches/types/src/org/apache/pig/pen/DerivedDataVisitor.java
    incubator/pig/branches/types/src/org/apache/pig/pen/EquivalenceClasses.java
    incubator/pig/branches/types/src/org/apache/pig/pen/ExampleGenerator.java
    incubator/pig/branches/types/src/org/apache/pig/pen/LineageTrimmingVisitor.java
    incubator/pig/branches/types/src/org/apache/pig/pen/util/
    incubator/pig/branches/types/src/org/apache/pig/pen/util/DependencyOrderLimitedWalker.java
    incubator/pig/branches/types/src/org/apache/pig/pen/util/DisplayExamples.java
    incubator/pig/branches/types/src/org/apache/pig/pen/util/ExampleTuple.java
    incubator/pig/branches/types/src/org/apache/pig/pen/util/FunctionalLogicalOptimizer.java
    incubator/pig/branches/types/src/org/apache/pig/pen/util/LineageTracer.java
    incubator/pig/branches/types/src/org/apache/pig/pen/util/MetricEvaluation.java
    incubator/pig/branches/types/src/org/apache/pig/pen/util/PreOrderDepthFirstWalker.java

Added: incubator/pig/branches/types/src/org/apache/pig/pen/AugmentBaseDataVisitor.java
URL: http://svn.apache.org/viewvc/incubator/pig/branches/types/src/org/apache/pig/pen/AugmentBaseDataVisitor.java?rev=699505&view=auto
==============================================================================
--- incubator/pig/branches/types/src/org/apache/pig/pen/AugmentBaseDataVisitor.java (added)
+++ incubator/pig/branches/types/src/org/apache/pig/pen/AugmentBaseDataVisitor.java Fri Sep 26 14:23:35 2008
@@ -0,0 +1,908 @@
+/*
+ * 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.pen;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.pig.backend.executionengine.ExecException;
+import org.apache.pig.data.BagFactory;
+import org.apache.pig.data.DataBag;
+import org.apache.pig.data.DataByteArray;
+import org.apache.pig.data.DataType;
+import org.apache.pig.data.Tuple;
+import org.apache.pig.data.TupleFactory;
+import org.apache.pig.impl.logicalLayer.BinaryExpressionOperator;
+import org.apache.pig.impl.logicalLayer.ExpressionOperator;
+import org.apache.pig.impl.logicalLayer.FrontendException;
+import org.apache.pig.impl.logicalLayer.LOAdd;
+import org.apache.pig.impl.logicalLayer.LOAnd;
+import org.apache.pig.impl.logicalLayer.LOCast;
+import org.apache.pig.impl.logicalLayer.LOCogroup;
+import org.apache.pig.impl.logicalLayer.LOConst;
+import org.apache.pig.impl.logicalLayer.LOCross;
+import org.apache.pig.impl.logicalLayer.LODistinct;
+import org.apache.pig.impl.logicalLayer.LODivide;
+import org.apache.pig.impl.logicalLayer.LOEqual;
+import org.apache.pig.impl.logicalLayer.LOFilter;
+import org.apache.pig.impl.logicalLayer.LOForEach;
+import org.apache.pig.impl.logicalLayer.LOGreaterThan;
+import org.apache.pig.impl.logicalLayer.LOGreaterThanEqual;
+import org.apache.pig.impl.logicalLayer.LOLesserThan;
+import org.apache.pig.impl.logicalLayer.LOLesserThanEqual;
+import org.apache.pig.impl.logicalLayer.LOLoad;
+import org.apache.pig.impl.logicalLayer.LOMod;
+import org.apache.pig.impl.logicalLayer.LOMultiply;
+import org.apache.pig.impl.logicalLayer.LONot;
+import org.apache.pig.impl.logicalLayer.LONotEqual;
+import org.apache.pig.impl.logicalLayer.LOOr;
+import org.apache.pig.impl.logicalLayer.LOProject;
+import org.apache.pig.impl.logicalLayer.LORegexp;
+import org.apache.pig.impl.logicalLayer.LOSort;
+import org.apache.pig.impl.logicalLayer.LOSplit;
+import org.apache.pig.impl.logicalLayer.LOStore;
+import org.apache.pig.impl.logicalLayer.LOSubtract;
+import org.apache.pig.impl.logicalLayer.LOUnion;
+import org.apache.pig.impl.logicalLayer.LOVisitor;
+import org.apache.pig.impl.logicalLayer.LogicalOperator;
+import org.apache.pig.impl.logicalLayer.LogicalPlan;
+import org.apache.pig.impl.logicalLayer.schema.Schema;
+import org.apache.pig.impl.plan.DepthFirstWalker;
+import org.apache.pig.impl.plan.VisitorException;
+import org.apache.pig.pen.util.ExampleTuple;
+import org.apache.pig.pen.util.PreOrderDepthFirstWalker;
+
+//This is used to generate synthetic data
+//Synthetic data generation is done by making constraint tuples for each operator as we traverse the plan
+//and try to replace the constraints with values as far as possible. We only deal with simple conditions right now
+
+public class AugmentBaseDataVisitor extends LOVisitor {
+
+    Map<LOLoad, DataBag> baseData = null;
+    Map<LOLoad, DataBag> newBaseData = new HashMap<LOLoad, DataBag>();
+    Map<LogicalOperator, DataBag> derivedData = null;
+
+    Map<LogicalOperator, DataBag> outputConstraintsMap = new HashMap<LogicalOperator, DataBag>();
+
+    Log log = LogFactory.getLog(getClass());
+
+    // Augmentation moves from the leaves to root and hence needs a
+    // depthfirstwalker
+    public AugmentBaseDataVisitor(LogicalPlan plan,
+            Map<LOLoad, DataBag> baseData,
+            Map<LogicalOperator, DataBag> derivedData) {
+        super(plan, new PreOrderDepthFirstWalker<LogicalOperator, LogicalPlan>(
+                plan));
+        this.baseData = baseData;
+        this.derivedData = derivedData;
+
+    }
+
+    public Map<LOLoad, DataBag> getNewBaseData() {
+        for (Map.Entry<LOLoad, DataBag> e : baseData.entrySet()) {
+            DataBag bag = newBaseData.get(e.getKey());
+            if (bag == null) {
+                bag = BagFactory.getInstance().newDefaultBag();
+                newBaseData.put(e.getKey(), bag);
+            }
+            bag.addAll(e.getValue());
+        }
+        return newBaseData;
+    }
+
+    @Override
+    protected void visit(LOCogroup cg) throws VisitorException {
+        // we first get the outputconstraints for the current cogroup
+        DataBag outputConstraints = outputConstraintsMap.get(cg);
+        outputConstraintsMap.remove(cg);
+        boolean ableToHandle = true;
+        // we then check if we can handle this cogroup and try to collect some
+        // information about grouping
+        List<List<Integer>> groupSpecs = new LinkedList<List<Integer>>();
+        int numCols = -1;
+
+        int minGroupSize = (cg.getInputs().size() == 1) ? 1 : 2;
+
+        for (LogicalOperator op : cg.getInputs()) {
+            List<LogicalPlan> groupByPlans = (List<LogicalPlan>) cg
+                    .getGroupByPlans().get(op);
+            List<Integer> groupCols = new ArrayList<Integer>();
+            for (LogicalPlan plan : groupByPlans) {
+                LogicalOperator leaf = plan.getLeaves().get(0);
+                if (leaf instanceof LOProject) {
+                    groupCols.add(((LOProject) leaf).getCol());
+                } else {
+                    ableToHandle = false;
+                    break;
+                }
+            }
+            if (numCols == -1) {
+                numCols = groupCols.size();
+            }
+            if (groupCols.size() != groupByPlans.size()
+                    || groupCols.size() != numCols) {
+                // we came across an unworkable cogroup plan
+                break;
+            } else {
+                groupSpecs.add(groupCols);
+            }
+        }
+
+        // we should now have some workable data at this point to synthesize
+        // tuples
+        try {
+            if (ableToHandle) {
+                // we need to go through the output constraints first
+                int numInputs = cg.getInputs().size();
+                if (outputConstraints != null) {
+                    for (Iterator<Tuple> it = outputConstraints.iterator(); it
+                            .hasNext();) {
+                        Tuple outputConstraint = it.next();
+                        Object groupLabel = outputConstraint.get(0);
+
+                        for (int input = 0; input < numInputs; input++) {
+
+                            int numInputFields = cg.getInputs().get(input)
+                                    .getSchema().size();
+                            List<Integer> groupCols = groupSpecs.get(input);
+
+                            DataBag output = outputConstraintsMap.get(cg
+                                    .getInputs().get(input));
+                            if (output == null) {
+                                output = BagFactory.getInstance()
+                                        .newDefaultBag();
+                                outputConstraintsMap.put(cg.getInputs().get(
+                                        input), output);
+                            }
+                            for (int i = 0; i < minGroupSize; i++) {
+                                Tuple inputConstraint = GetGroupByInput(
+                                        groupLabel, groupCols, numInputFields);
+                                if (inputConstraint != null)
+                                    output.add(inputConstraint);
+                            }
+                        }
+                    }
+                }
+                // then, go through all organic data groups and add input
+                // constraints to make each group big enough
+                DataBag outputData = derivedData.get(cg);
+
+                for (Iterator<Tuple> it = outputData.iterator(); it.hasNext();) {
+                    Tuple groupTup = it.next();
+                    Object groupLabel = groupTup.get(0);
+
+                    for (int input = 0; input < numInputs; input++) {
+                        int numInputFields = cg.getInputs().get(input)
+                                .getSchema().size();
+                        List<Integer> groupCols = groupSpecs.get(input);
+
+                        DataBag output = outputConstraintsMap.get(cg
+                                .getInputs().get(input));
+                        if (output == null) {
+                            output = BagFactory.getInstance().newDefaultBag();
+                            outputConstraintsMap.put(cg.getInputs().get(input),
+                                    output);
+                        }
+                        int numTupsToAdd = minGroupSize
+                                - (int) ((DataBag) groupTup.get(input + 1))
+                                        .size();
+                        for (int i = 0; i < numTupsToAdd; i++) {
+                            Tuple inputConstraint = GetGroupByInput(groupLabel,
+                                    groupCols, numInputFields);
+                            if (inputConstraint != null)
+                                output.add(inputConstraint);
+                        }
+                    }
+                }
+            }
+        } catch (Exception e) {
+            log
+                    .error("Error visiting Cogroup during Augmentation phase of Example Generator! "
+                            + e.getMessage());
+            throw new VisitorException(
+                    "Error visiting Cogroup during Augmentation phase of Example Generator! "
+                            + e.getMessage());
+        }
+    }
+
+    @Override
+    protected void visit(LOCross cs) throws VisitorException {
+
+    }
+
+    @Override
+    protected void visit(LODistinct dt) throws VisitorException {
+
+    }
+
+    @Override
+    protected void visit(LOFilter filter) throws VisitorException {
+        DataBag outputConstraints = outputConstraintsMap.get(filter);
+        outputConstraintsMap.remove(filter);
+
+        LogicalPlan filterCond = filter.getComparisonPlan();
+        DataBag inputConstraints = outputConstraintsMap.get(filter.getInput());
+        if (inputConstraints == null) {
+            inputConstraints = BagFactory.getInstance().newDefaultBag();
+            outputConstraintsMap.put(filter.getInput(), inputConstraints);
+        }
+
+        DataBag outputData = derivedData.get(filter);
+        DataBag inputData = derivedData.get(filter.getInput());
+        try {
+            if (outputConstraints != null && outputConstraints.size() > 0) { // there
+                // 's
+                // one
+                // or
+                // more
+                // output
+                // constraints
+                // ;
+                // generate
+                // corresponding
+                // input
+                // constraints
+                for (Iterator<Tuple> it = outputConstraints.iterator(); it
+                        .hasNext();) {
+                    Tuple outputConstraint = it.next();
+                    ExampleTuple inputConstraint = GenerateMatchingTuple(
+                            outputConstraint, filterCond, false);
+                    if (inputConstraint != null)
+                        inputConstraints.add(inputConstraint);
+                }
+            } else if (outputData.size() == 0) { // no output constraints, but
+                // output is empty; generate
+                // one input that will pass the
+                // filter
+                ExampleTuple inputConstraint = GenerateMatchingTuple(filter
+                        .getSchema(), filterCond, false);
+
+                if (inputConstraint != null)
+                    inputConstraints.add(inputConstraint);
+            }
+
+            // if necessary, insert a negative example (i.e. a tuple that does
+            // not pass the filter)
+            if (outputData.size() == inputData.size()) { // all tuples pass the
+                // filter; generate one
+                // input that will not
+                // pass the filter
+
+                ExampleTuple inputConstraint = GenerateMatchingTuple(filter
+                        .getSchema(), filterCond, true);
+                if (inputConstraint != null)
+                    inputConstraints.add(inputConstraint);
+
+            }
+        } catch (Exception e) {
+            log
+                    .error("Error visiting Load during Augmentation phase of Example Generator! "
+                            + e.getMessage());
+            throw new VisitorException(
+                    "Error visiting Load during Augmentation phase of Example Generator! "
+                            + e.getMessage());
+        }
+    }
+
+    @Override
+    protected void visit(LOForEach forEach) throws VisitorException {
+        DataBag outputConstraints = outputConstraintsMap.get(forEach);
+        outputConstraintsMap.remove(forEach);
+        List<LogicalPlan> plans = forEach.getForEachPlans();
+        boolean ableToHandle = true;
+        List<Integer> cols = new ArrayList<Integer>();
+        boolean cast = false;
+
+        if (outputConstraints == null || outputConstraints.size() == 0)
+            // we dont have to do anything in this case
+            return;
+
+        for (LogicalPlan plan : plans) {
+            LogicalOperator op = plan.getLeaves().get(0);
+            if (op instanceof LOCast) {
+                cast = true;
+                op = ((LOCast) op).getExpression();
+            }
+
+            if (!(op instanceof LOProject)) {
+                ableToHandle = false;
+                break;
+            } else {
+                cols.add(((LOProject) op).getCol());
+            }
+        }
+
+        if (ableToHandle) {
+            // we can only handle simple projections
+            DataBag output = BagFactory.getInstance().newDefaultBag();
+            for (Iterator<Tuple> it = outputConstraints.iterator(); it
+                    .hasNext();) {
+                Tuple outputConstraint = it.next();
+                try {
+                    Tuple inputConstraint = BackPropConstraint(
+                            outputConstraint, cols, (forEach.getPlan()
+                                    .getPredecessors(forEach)).get(0)
+                                    .getSchema(), cast);
+                    output.add(inputConstraint);
+                } catch (Exception e) {
+                    e.printStackTrace();
+                    throw new VisitorException(
+                            "Operator error during Augmenting Phase in Example Generator "
+                                    + e.getMessage());
+                }
+            }
+            outputConstraintsMap.put(forEach.getPlan().getPredecessors(forEach)
+                    .get(0), output);
+        }
+
+    }
+
+    @Override
+    protected void visit(LOLoad load) throws VisitorException {
+        DataBag inputData = baseData.get(load);
+
+        DataBag newInputData = newBaseData.get(load);
+        if (newInputData == null) {
+            newInputData = BagFactory.getInstance().newDefaultBag();
+            newBaseData.put(load, newInputData);
+        }
+
+        Schema schema;
+        try {
+            schema = load.getSchema();
+            if (schema == null)
+                throw new RuntimeException(
+                        "Example Generator requires a schema. Please provide a schema while loading data");
+        } catch (FrontendException e) {
+
+            log
+                    .error("Error visiting Load during Augmentation phase of Example Generator! "
+                            + e.getMessage());
+            throw new VisitorException(
+                    "Error visiting Load during Augmentation phase of Example Generator! "
+                            + e.getMessage());
+        }
+        DataBag outputConstraints = outputConstraintsMap.get(load);
+        outputConstraintsMap.remove(load);
+        // check if the inputData exists
+        if (inputData == null || inputData.size() == 0) {
+            log.error("No input data found!");
+            throw new RuntimeException("No input data found!");
+        }
+
+        // first of all, we are required to guarantee that there is at least one
+        // output tuple
+        if (outputConstraints == null || outputConstraints.size() == 0) {
+            outputConstraints = BagFactory.getInstance().newDefaultBag();
+            outputConstraints.add(TupleFactory.getInstance().newTuple(
+                    schema.getFields().size()));
+        }
+
+        // create example tuple to steal values from when we encounter
+        // "don't care" fields (i.e. null fields)
+        Tuple exampleTuple = inputData.iterator().next();
+
+        // run through output constraints; for each one synthesize a tuple and
+        // add it to the base data
+        // (while synthesizing individual fields, try to match fields that exist
+        // in the real data)
+        for (Iterator<Tuple> it = outputConstraints.iterator(); it.hasNext();) {
+            Tuple outputConstraint = it.next();
+
+            // sanity check:
+            if (outputConstraint.size() != schema.getFields().size())
+                throw new RuntimeException(
+                        "Internal error: incorrect number of fields in constraint tuple.");
+
+            Tuple inputT = TupleFactory.getInstance().newTuple(
+                    outputConstraint.size());
+            ExampleTuple inputTuple = new ExampleTuple(inputT);
+
+            try {
+                for (int i = 0; i < inputTuple.size(); i++) {
+                    Object d = outputConstraint.get(i);
+                    if (d == null)
+                        d = exampleTuple.get(i);
+                    inputTuple.set(i, d);
+                }
+            } catch (ExecException e) {
+                log
+                        .error("Error visiting Load during Augmentation phase of Example Generator! "
+                                + e.getMessage());
+                throw new VisitorException(
+                        "Error visiting Load during Augmentation phase of Example Generator! "
+                                + e.getMessage());
+
+            }
+            if (!inputTuple.equals(exampleTuple))
+                inputTuple.synthetic = true;
+
+            newInputData.add(inputTuple);
+        }
+    }
+
+    @Override
+    protected void visit(LOSort s) throws VisitorException {
+        DataBag outputConstraints = outputConstraintsMap.get(s);
+        outputConstraintsMap.remove(s);
+
+        if (outputConstraints == null)
+            outputConstraintsMap.put(s.getInput(), BagFactory.getInstance()
+                    .newDefaultBag());
+        else
+            outputConstraintsMap.put(s.getInput(), outputConstraints);
+    }
+
+    @Override
+    protected void visit(LOSplit split) throws VisitorException {
+
+    }
+
+    @Override
+    protected void visit(LOStore store) throws VisitorException {
+        DataBag outputConstraints = outputConstraintsMap.get(store);
+        if (outputConstraints == null) {
+            outputConstraintsMap.put(store.getPlan().getPredecessors(store)
+                    .get(0), BagFactory.getInstance().newDefaultBag());
+        } else {
+            outputConstraintsMap.remove(store);
+            outputConstraintsMap.put(store.getPlan().getPredecessors(store)
+                    .get(0), outputConstraints);
+        }
+    }
+
+    @Override
+    protected void visit(LOUnion u) throws VisitorException {
+        DataBag outputConstraints = outputConstraintsMap.get(u);
+        outputConstraintsMap.remove(u);
+        if (outputConstraints == null || outputConstraints.size() == 0) {
+            // we dont need to do anything
+            // we just find the inputs, create empty bags as their
+            // outputConstraints and return
+            for (LogicalOperator op : u.getInputs()) {
+                DataBag constraints = BagFactory.getInstance().newDefaultBag();
+                outputConstraintsMap.put(op, constraints);
+            }
+            return;
+        }
+
+        // since we have some outputConstraints, we apply them to the inputs
+        // round-robin
+        int count = 0;
+        List<LogicalOperator> inputs = u.getInputs();
+        int noInputs = inputs.size();
+
+        for (LogicalOperator op : inputs) {
+            DataBag constraint = BagFactory.getInstance().newDefaultBag();
+            outputConstraintsMap.put(op, constraint);
+        }
+        for (Iterator<Tuple> it = outputConstraints.iterator(); it.hasNext();) {
+            DataBag constraint = outputConstraintsMap.get(inputs.get(count));
+            constraint.add(it.next());
+            count = (count + 1) % noInputs;
+        }
+
+    }
+
+    Tuple GetGroupByInput(Object groupLabel, List<Integer> groupCols,
+            int numFields) throws ExecException {
+        Tuple t = TupleFactory.getInstance().newTuple(numFields);
+
+        if (groupCols.size() == 1) {
+            // GroupLabel would be a data atom
+            t.set(groupCols.get(0), groupLabel);
+        } else {
+            if (!(groupLabel instanceof Tuple))
+                throw new RuntimeException("Unrecognized group label!");
+            Tuple group = (Tuple) groupLabel;
+            for (int i = 0; i < groupCols.size(); i++) {
+                t.set(groupCols.get(i), group.get(i));
+            }
+        }
+
+        return t;
+    }
+
+    Tuple BackPropConstraint(Tuple outputConstraint, List<Integer> cols,
+            Schema inputSchema, boolean cast) throws ExecException {
+        Tuple inputConst = TupleFactory.getInstance().newTuple(
+                inputSchema.getFields().size());
+
+        Tuple inputConstraint = new ExampleTuple(inputConst);
+
+        for (int outCol = 0; outCol < outputConstraint.size(); outCol++) {
+            int inCol = cols.get(outCol);
+            Object outVal = outputConstraint.get(outCol);
+            Object inVal = inputConstraint.get(inCol);
+
+            if (inVal == null && outVal != null) {
+                // inputConstraint.set(inCol, outVal);
+                inputConstraint.set(inCol, (cast) ? new DataByteArray(outVal
+                        .toString().getBytes()) : outVal);
+
+            } else {
+                if (outVal != null) {
+                    // unable to back-propagate, due to conflicting column
+                    // constraints, so give up
+                    return null;
+                }
+            }
+        }
+
+        return inputConstraint;
+    }
+
+    // generate a constraint tuple that conforms to the schema and passes the
+    // predicate
+    // (or null if unable to find such a tuple)
+
+    ExampleTuple GenerateMatchingTuple(Schema schema, LogicalPlan plan,
+            boolean invert) throws ExecException {
+        return GenerateMatchingTuple(TupleFactory.getInstance().newTuple(
+                schema.getFields().size()), plan, invert);
+    }
+
+    // generate a constraint tuple that conforms to the constraint and passes
+    // the predicate
+    // (or null if unable to find such a tuple)
+    //
+    // for now, constraint tuples are tuples whose fields are a blend of actual
+    // data values and nulls,
+    // where a null stands for "don't care"
+    //
+    // in the future, may want to replace "don't care" with a more rich
+    // constraint language; this would
+    // help, e.g. in the case of two filters in a row (you want the downstream
+    // filter to tell the upstream filter
+    // what predicate it wants satisfied in a given field)
+    //
+
+    ExampleTuple GenerateMatchingTuple(Tuple constraint, LogicalPlan predicate,
+            boolean invert) throws ExecException {
+        Tuple t = TupleFactory.getInstance().newTuple(constraint.size());
+        ExampleTuple tOut = new ExampleTuple(t);
+        for (int i = 0; i < t.size(); i++)
+            tOut.set(i, constraint.get(i));
+
+        GenerateMatchingTupleHelper(tOut, (ExpressionOperator) predicate
+                .getLeaves().get(0), invert);
+        tOut.synthetic = true;
+        return tOut;
+
+    }
+
+    void GenerateMatchingTupleHelper(Tuple t, ExpressionOperator pred,
+            boolean invert) throws ExecException {
+        if (pred instanceof BinaryExpressionOperator)
+            GenerateMatchingTupleHelper(t, (BinaryExpressionOperator) pred,
+                    invert);
+        else if (pred instanceof LONot)
+            GenerateMatchingTupleHelper(t, (LONot) pred, invert);
+        else
+            throw new ExecException("Unknown operator in filter predicate");
+    }
+
+    void GenerateMatchingTupleHelper(Tuple t, BinaryExpressionOperator pred,
+            boolean invert) throws ExecException {
+
+        if (pred instanceof LOAnd) {
+            GenerateMatchingTupleHelper(t, (LOAnd) pred, invert);
+            return;
+        } else if (pred instanceof LOOr) {
+            GenerateMatchingTupleHelper(t, (LOOr) pred, invert);
+            return;
+        }
+
+        // now we are sure that the expression operators are the roots of the
+        // plan
+
+        boolean leftIsConst = false, rightIsConst = false;
+        Object leftConst = null, rightConst = null;
+        byte leftDataType = 0, rightDataType = 0;
+
+        int leftCol = -1, rightCol = -1;
+
+        if (pred instanceof LOAdd || pred instanceof LOSubtract
+                || pred instanceof LOMultiply || pred instanceof LODivide
+                || pred instanceof LOMod || pred instanceof LORegexp)
+            return; // We don't try to work around these operators right now
+
+        if (pred.getLhsOperand() instanceof LOConst) {
+            leftIsConst = true;
+            leftConst = ((LOConst) (pred.getLhsOperand())).getValue();
+        } else {
+            LogicalOperator lhs = pred.getLhsOperand();
+            if (lhs instanceof LOCast)
+                lhs = ((LOCast) lhs).getExpression();
+            // if (!(pred.getLhsOperand() instanceof LOProject && ((LOProject)
+            // pred
+            // .getLhsOperand()).getProjection().size() == 1))
+            // return; // too hard
+            if (!(lhs instanceof LOProject && ((LOProject) lhs).getProjection()
+                    .size() == 1))
+                return;
+            leftCol = ((LOProject) lhs).getCol();
+            leftDataType = ((LOProject) lhs).getType();
+
+            Object d = t.get(leftCol);
+            if (d != null) {
+                leftIsConst = true;
+                leftConst = d;
+            }
+        }
+
+        if (pred.getRhsOperand() instanceof LOConst) {
+            rightIsConst = true;
+            rightConst = ((LOConst) (pred.getRhsOperand())).getValue();
+        } else {
+            LogicalOperator rhs = pred.getRhsOperand();
+            if (rhs instanceof LOCast)
+                rhs = ((LOCast) rhs).getExpression();
+            // if (!(pred.getRhsOperand() instanceof LOProject && ((LOProject)
+            // pred
+            // .getRhsOperand()).getProjection().size() == 1))
+            // return; // too hard
+            if (!(rhs instanceof LOProject && ((LOProject) rhs).getProjection()
+                    .size() == 1))
+                return;
+            rightCol = ((LOProject) rhs).getCol();
+            rightDataType = ((LOProject) rhs).getType();
+
+            Object d = t.get(rightCol);
+            if (d != null) {
+                rightIsConst = true;
+                rightConst = d;
+            }
+        }
+
+        if (leftIsConst && rightIsConst)
+            return; // can't really change the result if both are constants
+
+        // now we try to change some nulls to constants
+
+        // convert some nulls to constants
+        if (!invert) {
+            if (pred instanceof LOEqual) {
+                if (leftIsConst) {
+                    t.set(rightCol, generateData(rightDataType, leftConst
+                            .toString()));
+                } else if (rightIsConst) {
+                    t.set(leftCol, generateData(leftDataType, rightConst
+                            .toString()));
+                } else {
+                    t.set(leftCol, generateData(leftDataType, "0"));
+                    t.set(rightCol, generateData(rightDataType, "0"));
+                }
+            } else if (pred instanceof LONotEqual) {
+                if (leftIsConst) {
+                    t.set(rightCol, generateData(rightDataType,
+                            GetUnequalValue(leftConst).toString()));
+                } else if (rightIsConst) {
+                    t.set(leftCol, generateData(leftDataType, GetUnequalValue(
+                            rightConst).toString()));
+                } else {
+                    t.set(leftCol, generateData(leftDataType, "0"));
+                    t.set(rightCol, generateData(rightDataType, "1"));
+                }
+            } else if (pred instanceof LOGreaterThan
+                    || pred instanceof LOGreaterThanEqual) {
+                if (leftIsConst) {
+                    t.set(rightCol, generateData(rightDataType,
+                            GetSmallerValue(leftConst).toString()));
+                } else if (rightIsConst) {
+                    t.set(leftCol, generateData(leftDataType, GetLargerValue(
+                            rightConst).toString()));
+                } else {
+                    t.set(leftCol, generateData(leftDataType, "1"));
+                    t.set(rightCol, generateData(rightDataType, "0"));
+                }
+            } else if (pred instanceof LOLesserThan
+                    || pred instanceof LOLesserThanEqual) {
+                if (leftIsConst) {
+                    t.set(rightCol, generateData(rightDataType, GetLargerValue(
+                            leftConst).toString()));
+                } else if (rightIsConst) {
+                    t.set(leftCol, generateData(leftDataType, GetSmallerValue(
+                            rightConst).toString()));
+                } else {
+                    t.set(leftCol, generateData(leftDataType, "0"));
+                    t.set(rightCol, generateData(rightDataType, "1"));
+                }
+            }
+        } else {
+            if (pred instanceof LOEqual) {
+                if (leftIsConst) {
+                    t.set(rightCol, generateData(rightDataType,
+                            GetUnequalValue(leftConst).toString()));
+                } else if (rightIsConst) {
+                    t.set(leftCol, generateData(leftDataType, GetUnequalValue(
+                            rightConst).toString()));
+                } else {
+                    t.set(leftCol, generateData(leftDataType, "0"));
+                    t.set(rightCol, generateData(rightDataType, "1"));
+                }
+            } else if (pred instanceof LONotEqual) {
+                if (leftIsConst) {
+                    t.set(rightCol, generateData(rightDataType, leftConst
+                            .toString()));
+                } else if (rightIsConst) {
+                    t.set(leftCol, generateData(leftDataType, rightConst
+                            .toString()));
+                } else {
+                    t.set(leftCol, generateData(leftDataType, "0"));
+                    t.set(rightCol, generateData(rightDataType, "0"));
+                }
+            } else if (pred instanceof LOGreaterThan
+                    || pred instanceof LOGreaterThanEqual) {
+                if (leftIsConst) {
+                    t.set(rightCol, generateData(rightDataType, GetLargerValue(
+                            leftConst).toString()));
+                } else if (rightIsConst) {
+                    t.set(leftCol, generateData(leftDataType, GetSmallerValue(
+                            rightConst).toString()));
+                } else {
+                    t.set(leftCol, generateData(leftDataType, "0"));
+                    t.set(rightCol, generateData(rightDataType, "1"));
+                }
+            } else if (pred instanceof LOLesserThan
+                    || pred instanceof LOLesserThanEqual) {
+                if (leftIsConst) {
+                    t.set(rightCol, generateData(rightDataType,
+                            GetSmallerValue(leftConst).toString()));
+                } else if (rightIsConst) {
+                    t.set(leftCol, generateData(leftDataType, GetLargerValue(
+                            rightConst).toString()));
+                } else {
+                    t.set(leftCol, generateData(leftDataType, "1"));
+                    t.set(rightCol, generateData(rightDataType, "0"));
+                }
+            }
+        }
+
+    }
+
+    void GenerateMatchingTupleHelper(Tuple t, LOAnd op, boolean invert)
+            throws ExecException {
+        ExpressionOperator input = op.getLhsOperand();
+        GenerateMatchingTupleHelper(t, input, invert);
+        input = op.getRhsOperand();
+        GenerateMatchingTupleHelper(t, input, invert);
+
+    }
+
+    void GenerateMatchingTupleHelper(Tuple t, LOOr op, boolean invert)
+            throws ExecException {
+        ExpressionOperator input = op.getLhsOperand();
+        GenerateMatchingTupleHelper(t, input, invert);
+        input = op.getRhsOperand();
+        GenerateMatchingTupleHelper(t, input, invert);
+
+    }
+
+    void GenerateMatchingTupleHelper(Tuple t, LONot op, boolean invert)
+            throws ExecException {
+        ExpressionOperator input = op.getOperand();
+        GenerateMatchingTupleHelper(t, input, !invert);
+
+    }
+
+    Object GetUnequalValue(Object v) {
+        byte type = DataType.findType(v);
+
+        if (type == DataType.BAG || type == DataType.TUPLE
+                || type == DataType.MAP)
+            return null;
+
+        Object zero = generateData(type, "0");
+
+        if (v.equals(zero))
+            return generateData(type, "1");
+
+        return zero;
+    }
+
+    Object GetSmallerValue(Object v) {
+        byte type = DataType.findType(v);
+
+        if (type == DataType.BAG || type == DataType.TUPLE
+                || type == DataType.MAP)
+            return null;
+
+        switch (type) {
+        case DataType.CHARARRAY:
+            String str = (String) v;
+            if (str.length() > 0)
+                return new String(str.substring(0, str.length() - 1));
+            else
+                return null;
+        case DataType.BYTEARRAY:
+            DataByteArray data = (DataByteArray) v;
+            if (data.size() > 0)
+                return new DataByteArray(data.get(), 0, data.size() - 1);
+            else
+                return null;
+        case DataType.INTEGER:
+            return new Integer((Integer) v - 1);
+        case DataType.LONG:
+            return new Long((Long) v - 1);
+        case DataType.FLOAT:
+            return new Float((Float) v - 1);
+        case DataType.DOUBLE:
+            return new Double((Double) v - 1);
+        default:
+            return null;
+        }
+
+    }
+
+    Object GetLargerValue(Object v) {
+        byte type = DataType.findType(v);
+
+        if (type == DataType.BAG || type == DataType.TUPLE
+                || type == DataType.MAP)
+            return null;
+
+        switch (type) {
+        case DataType.CHARARRAY:
+            return new String((String) v + "0");
+        case DataType.BYTEARRAY:
+            String str = ((DataByteArray) v).toString();
+            str = str + "0";
+            return new DataByteArray(str);
+        case DataType.INTEGER:
+            return new Integer((Integer) v + 1);
+        case DataType.LONG:
+            return new Long((Long) v + 1);
+        case DataType.FLOAT:
+            return new Float((Float) v + 1);
+        case DataType.DOUBLE:
+            return new Double((Double) v + 1);
+        default:
+            return null;
+        }
+    }
+
+    Object generateData(byte type, String data) {
+        switch (type) {
+        case DataType.BOOLEAN:
+            return new Boolean(data);
+        case DataType.BYTEARRAY:
+            return new DataByteArray(data.getBytes());
+        case DataType.DOUBLE:
+            return new Double(data);
+        case DataType.FLOAT:
+            return new Float(data);
+        case DataType.INTEGER:
+            return new Integer(data);
+        case DataType.LONG:
+            return new Long(data);
+        case DataType.CHARARRAY:
+            return data;
+        default:
+            return null;
+        }
+    }
+
+}

Added: incubator/pig/branches/types/src/org/apache/pig/pen/DerivedDataVisitor.java
URL: http://svn.apache.org/viewvc/incubator/pig/branches/types/src/org/apache/pig/pen/DerivedDataVisitor.java?rev=699505&view=auto
==============================================================================
--- incubator/pig/branches/types/src/org/apache/pig/pen/DerivedDataVisitor.java (added)
+++ incubator/pig/branches/types/src/org/apache/pig/pen/DerivedDataVisitor.java Fri Sep 26 14:23:35 2008
@@ -0,0 +1,482 @@
+/*
+ * 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.pen;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.pig.backend.executionengine.ExecException;
+import org.apache.pig.backend.hadoop.executionengine.physicalLayer.POStatus;
+import org.apache.pig.backend.hadoop.executionengine.physicalLayer.PhysicalOperator;
+import org.apache.pig.backend.hadoop.executionengine.physicalLayer.Result;
+import org.apache.pig.backend.hadoop.executionengine.physicalLayer.plans.PhysicalPlan;
+import org.apache.pig.backend.hadoop.executionengine.physicalLayer.relationalOperators.PORead;
+import org.apache.pig.backend.local.executionengine.physicalLayer.LocalLogToPhyTranslationVisitor;
+import org.apache.pig.data.BagFactory;
+import org.apache.pig.data.DataBag;
+import org.apache.pig.data.Tuple;
+import org.apache.pig.impl.PigContext;
+import org.apache.pig.impl.logicalLayer.FrontendException;
+import org.apache.pig.impl.logicalLayer.LOCogroup;
+import org.apache.pig.impl.logicalLayer.LOCross;
+import org.apache.pig.impl.logicalLayer.LODistinct;
+import org.apache.pig.impl.logicalLayer.LOFilter;
+import org.apache.pig.impl.logicalLayer.LOForEach;
+import org.apache.pig.impl.logicalLayer.LOLimit;
+import org.apache.pig.impl.logicalLayer.LOLoad;
+import org.apache.pig.impl.logicalLayer.LOSort;
+import org.apache.pig.impl.logicalLayer.LOSplit;
+import org.apache.pig.impl.logicalLayer.LOStore;
+import org.apache.pig.impl.logicalLayer.LOUnion;
+import org.apache.pig.impl.logicalLayer.LOVisitor;
+import org.apache.pig.impl.logicalLayer.LogicalOperator;
+import org.apache.pig.impl.logicalLayer.LogicalPlan;
+import org.apache.pig.impl.logicalLayer.PlanSetter;
+import org.apache.pig.impl.logicalLayer.validators.LogicalPlanValidationExecutor;
+import org.apache.pig.impl.plan.CompilationMessageCollector;
+import org.apache.pig.impl.plan.DependencyOrderWalker;
+import org.apache.pig.impl.plan.OperatorKey;
+import org.apache.pig.impl.plan.PlanException;
+import org.apache.pig.impl.plan.VisitorException;
+import org.apache.pig.impl.util.IdentityHashSet;
+import org.apache.pig.pen.util.DependencyOrderLimitedWalker;
+import org.apache.pig.pen.util.LineageTracer;
+
+
+//This class is used to pass data through the entire plan and save the intermediates results.
+public class DerivedDataVisitor extends LOVisitor {
+
+    Map<LogicalOperator, DataBag> derivedData = new HashMap<LogicalOperator, DataBag>();
+    PigContext pc;
+    PhysicalPlan physPlan = null;
+    Map<LOLoad, DataBag> baseData = null;
+
+    Map<LogicalOperator, PhysicalOperator> LogToPhyMap = null;
+    Log log = LogFactory.getLog(getClass());
+
+    Map<LogicalOperator, Collection<IdentityHashSet<Tuple>>> OpToEqClasses = null;
+    Collection<IdentityHashSet<Tuple>> EqClasses = null;
+
+    LineageTracer lineage = new LineageTracer();
+
+    public DerivedDataVisitor(LogicalPlan plan, PigContext pc,
+            Map<LOLoad, DataBag> baseData,
+            Map<LogicalOperator, PhysicalOperator> logToPhyMap,
+            PhysicalPlan physPlan) {
+        super(plan, new DependencyOrderWalker<LogicalOperator, LogicalPlan>(
+                plan));
+        this.pc = pc;
+        this.baseData = baseData;
+
+        OpToEqClasses = new HashMap<LogicalOperator, Collection<IdentityHashSet<Tuple>>>();
+        EqClasses = new LinkedList<IdentityHashSet<Tuple>>();
+
+        LogToPhyMap = logToPhyMap;
+        this.physPlan = physPlan;
+        // if(logToPhyMap == null)
+        // compilePlan(plan);
+        // else
+        // LogToPhyMap = logToPhyMap;
+
+    }
+
+    public DerivedDataVisitor(LogicalOperator op, PigContext pc,
+            Map<LOLoad, DataBag> baseData,
+            Map<LogicalOperator, PhysicalOperator> logToPhyMap,
+            PhysicalPlan physPlan) {
+        super(op.getPlan(),
+                new DependencyOrderLimitedWalker<LogicalOperator, LogicalPlan>(
+                        op, op.getPlan()));
+        this.pc = pc;
+        this.baseData = baseData;
+
+        OpToEqClasses = new HashMap<LogicalOperator, Collection<IdentityHashSet<Tuple>>>();
+        EqClasses = new LinkedList<IdentityHashSet<Tuple>>();
+
+        LogToPhyMap = logToPhyMap;
+        this.physPlan = physPlan;
+        // if(logToPhyMap == null)
+        // compilePlan(op.getPlan());
+        // else
+        // LogToPhyMap = logToPhyMap;
+    }
+
+    public void setOperatorToEvaluate(LogicalOperator op) {
+        mCurrentWalker = new DependencyOrderLimitedWalker<LogicalOperator, LogicalPlan>(
+                op, op.getPlan());
+    }
+
+    @Override
+    protected void visit(LOCogroup cg) throws VisitorException {
+        // evaluateOperator(cg);
+        // there is a slightly different code path for cogroup because of the
+        // local rearranges
+        PhysicalOperator physOp = LogToPhyMap.get(cg);
+        Random r = new Random();
+        // get the list of original inputs
+
+        // List<PhysicalOperator> inputs = physOp.getInputs();
+        List<PhysicalOperator> inputs = new ArrayList<PhysicalOperator>();
+        PhysicalPlan phy = new PhysicalPlan();
+        phy.add(physOp);
+
+        // for(PhysicalOperator input : physOp.getInputs()) {
+        for (PhysicalOperator input : physPlan.getPredecessors(physOp)) {
+            inputs.add(input.getInputs().get(0));
+            // input.setInputs(null);
+            phy.add(input);
+            try {
+                phy.connect(input, physOp);
+            } catch (PlanException e) {
+                // TODO Auto-generated catch block
+                e.printStackTrace();
+                log.error("Error connecting " + input.name() + " to "
+                        + physOp.name());
+            }
+        }
+
+        physOp.setLineageTracer(lineage);
+
+        // replace the original inputs by POReads
+        for (int i = 0; i < inputs.size(); i++) {
+            DataBag bag = derivedData.get(cg.getInputs().get(i));
+            PORead por = new PORead(new OperatorKey("", r.nextLong()), bag);
+            phy.add(por);
+            try {
+                phy.connect(por, physOp.getInputs().get(i));
+            } catch (PlanException e) {
+                // TODO Auto-generated catch block
+                e.printStackTrace();
+                log.error("Error connecting " + por.name() + " to "
+                        + physOp.name());
+            }
+        }
+
+        DataBag output = BagFactory.getInstance().newDefaultBag();
+        Tuple t = null;
+        try {
+            for (Result res = physOp.getNext(t); res.returnStatus != POStatus.STATUS_EOP; res = physOp
+                    .getNext(t)) {
+                output.add((Tuple) res.result);
+            }
+        } catch (ExecException e) {
+            log.error("Error evaluating operator : " + physOp.name());
+        }
+        derivedData.put(cg, output);
+
+        try {
+            Collection<IdentityHashSet<Tuple>> eq = EquivalenceClasses
+                    .GetEquivalenceClasses(cg, derivedData);
+            EqClasses.addAll(eq);
+            OpToEqClasses.put(cg, eq);
+        } catch (ExecException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+            log
+                    .error("Error updating equivalence classes while evaluating operators. \n"
+                            + e.getMessage());
+        }
+
+        // re-attach the original operators
+        // for(int i = 0; i < inputs.size(); i++) {
+        // try {
+        // physPlan.connect(inputs.get(i), physOp.getInputs().get(i));
+        //		
+        // } catch (PlanException e) {
+        // // TODO Auto-generated catch block
+        // e.printStackTrace();
+        // log.error("Error connecting " + inputs.get(i).name() + " to " +
+        // physOp.getInputs().get(i).name());
+        // }
+        // }
+        physOp.setLineageTracer(null);
+    }
+
+    @Override
+    protected void visit(LOCross cs) throws VisitorException {
+        evaluateOperator(cs);
+    }
+
+    @Override
+    protected void visit(LODistinct dt) throws VisitorException {
+        evaluateOperator(dt);
+    }
+
+    @Override
+    protected void visit(LOFilter filter) throws VisitorException {
+        evaluateOperator(filter);
+    }
+
+    @Override
+    protected void visit(LOForEach forEach) throws VisitorException {
+        evaluateOperator(forEach);
+    }
+
+    @Override
+    protected void visit(LOLoad load) throws VisitorException {
+        derivedData.put(load, baseData.get(load));
+
+        Collection<IdentityHashSet<Tuple>> eq = EquivalenceClasses
+                .GetEquivalenceClasses(load, derivedData);
+        EqClasses.addAll(eq);
+        OpToEqClasses.put(load, eq);
+
+        for (Iterator<Tuple> it = derivedData.get(load).iterator(); it
+                .hasNext();) {
+            lineage.insert(it.next());
+        }
+
+    }
+
+    @Override
+    protected void visit(LOSplit split) throws VisitorException {
+        evaluateOperator(split);
+    }
+
+    @Override
+    protected void visit(LOStore store) throws VisitorException {
+        derivedData.put(store, derivedData.get(store.getPlan().getPredecessors(
+                store).get(0)));
+    }
+
+    @Override
+    protected void visit(LOUnion u) throws VisitorException {
+        evaluateOperator(u);
+    }
+
+    @Override
+    protected void visit(LOLimit l) throws VisitorException {
+        evaluateOperator(l);
+    }
+    
+    @Override
+    protected void visit(LOSort sort) throws VisitorException {
+        evaluateOperator(sort);
+    }
+
+    // private void compilePlan(LogicalPlan plan) {
+    //	
+    // plan = refineLogicalPlan(plan);
+    //	
+    // LocalLogToPhyTranslationVisitor visitor = new
+    // LocalLogToPhyTranslationVisitor(plan);
+    // visitor.setPigContext(pc);
+    // try {
+    // visitor.visit();
+    // } catch (VisitorException e) {
+    // // TODO Auto-generated catch block
+    // e.printStackTrace();
+    // log.error("Error visiting the logical plan in ExampleGenerator");
+    // }
+    // physPlan = visitor.getPhysicalPlan();
+    // LogToPhyMap = visitor.getLogToPhyMap();
+    // }
+    //    
+    // private LogicalPlan refineLogicalPlan(LogicalPlan plan) {
+    // PlanSetter ps = new PlanSetter(plan);
+    // try {
+    // ps.visit();
+    //	    
+    // } catch (VisitorException e) {
+    // // TODO Auto-generated catch block
+    // e.printStackTrace();
+    // }
+    //        
+    // // run through validator
+    // CompilationMessageCollector collector = new CompilationMessageCollector()
+    // ;
+    // FrontendException caught = null;
+    // try {
+    // LogicalPlanValidationExecutor validator =
+    // new LogicalPlanValidationExecutor(plan, pc);
+    // validator.validate(plan, collector);
+    // } catch (FrontendException fe) {
+    // // Need to go through and see what the collector has in it. But
+    // // remember what we've caught so we can wrap it into what we
+    // // throw.
+    // caught = fe;
+    // }
+    //        
+    //        
+    // return plan;
+    //
+    // }
+
+    private void evaluateOperator(LogicalOperator op) {
+        PhysicalOperator physOp = LogToPhyMap.get(op);
+        Random r = new Random();
+        // get the list of original inputs
+
+        List<PhysicalOperator> inputs = physOp.getInputs();
+        physOp.setInputs(null);
+        physOp.setLineageTracer(lineage);
+        PhysicalPlan phy = new PhysicalPlan();
+        phy.add(physOp);
+
+        // replace the original inputs by POReads
+        for (LogicalOperator l : op.getPlan().getPredecessors(op)) {
+            DataBag bag = derivedData.get(l);
+            PORead por = new PORead(new OperatorKey("", r.nextLong()), bag);
+            phy.add(por);
+            try {
+                phy.connect(por, physOp);
+            } catch (PlanException e) {
+                // TODO Auto-generated catch block
+                e.printStackTrace();
+                log.error("Error connecting " + por.name() + " to "
+                        + physOp.name());
+            }
+        }
+
+        DataBag output = BagFactory.getInstance().newDefaultBag();
+        Tuple t = null;
+        try {
+            for (Result res = physOp.getNext(t); res.returnStatus != POStatus.STATUS_EOP; res = physOp
+                    .getNext(t)) {
+                output.add((Tuple) res.result);
+            }
+        } catch (ExecException e) {
+            log.error("Error evaluating operator : " + physOp.name());
+        }
+        derivedData.put(op, output);
+
+        try {
+            Collection<IdentityHashSet<Tuple>> eq = EquivalenceClasses
+                    .GetEquivalenceClasses(op, derivedData);
+            EqClasses.addAll(eq);
+            OpToEqClasses.put(op, eq);
+        } catch (ExecException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+            log
+                    .error("Error updating equivalence classes while evaluating operators. \n"
+                            + e.getMessage());
+        }
+
+        // re-attach the original operators
+        physOp.setInputs(inputs);
+        physOp.setLineageTracer(null);
+    }
+
+    public DataBag evaluateIsolatedOperator(LOCogroup op,
+            List<DataBag> inputBags) {
+        if (op.getPlan().getPredecessors(op).size() > inputBags.size())
+            return null;
+
+        int count = 0;
+        for (LogicalOperator inputs : op.getPlan().getPredecessors(op)) {
+            derivedData.put(inputs, inputBags.get(count++));
+        }
+
+        return evaluateIsolatedOperator(op);
+
+    }
+
+    public DataBag evaluateIsolatedOperator(LOCogroup op) {
+        // return null if the inputs are not already evaluated
+        for (LogicalOperator in : op.getPlan().getPredecessors(op)) {
+            if (derivedData.get(in) == null)
+                return null;
+        }
+
+        LineageTracer oldLineage = this.lineage;
+        this.lineage = new LineageTracer();
+
+        PhysicalOperator physOp = LogToPhyMap.get(op);
+        Random r = new Random();
+        // get the list of original inputs
+        // List<PhysicalOperator> inputs = physOp.getInputs();
+        List<PhysicalOperator> inputs = new ArrayList<PhysicalOperator>();
+        PhysicalPlan phy = new PhysicalPlan();
+        phy.add(physOp);
+
+        for (PhysicalOperator input : physOp.getInputs()) {
+            inputs.add(input.getInputs().get(0));
+            input.setInputs(null);
+            phy.add(input);
+            try {
+                phy.connect(input, physOp);
+            } catch (PlanException e) {
+                // TODO Auto-generated catch block
+                e.printStackTrace();
+                log.error("Error connecting " + input.name() + " to "
+                        + physOp.name());
+            }
+        }
+        physOp.setLineageTracer(lineage);
+
+        physOp.setLineageTracer(null);
+
+        // replace the original inputs by POReads
+        for (int i = 0; i < inputs.size(); i++) {
+            DataBag bag = derivedData.get(((LOCogroup) op).getInputs().get(i));
+            PORead por = new PORead(new OperatorKey("", r.nextLong()), bag);
+            phy.add(por);
+            try {
+                phy.connect(por, physOp.getInputs().get(i));
+            } catch (PlanException e) {
+                // TODO Auto-generated catch block
+                e.printStackTrace();
+                log.error("Error connecting " + por.name() + " to "
+                        + physOp.name());
+            }
+        }
+
+        // replace the original inputs by POReads
+        // for(LogicalOperator l : op.getPlan().getPredecessors(op)) {
+        // DataBag bag = derivedData.get(l);
+        // PORead por = new PORead(new OperatorKey("", r.nextLong()), bag);
+        // phy.add(por);
+        // try {
+        // phy.connect(por, physOp);
+        // } catch (PlanException e) {
+        // // TODO Auto-generated catch block
+        // e.printStackTrace();
+        // log.error("Error connecting " + por.name() + " to " + physOp.name());
+        // }
+        // }
+
+        DataBag output = BagFactory.getInstance().newDefaultBag();
+        Tuple t = null;
+        try {
+            for (Result res = physOp.getNext(t); res.returnStatus != POStatus.STATUS_EOP; res = physOp
+                    .getNext(t)) {
+                output.add((Tuple) res.result);
+            }
+        } catch (ExecException e) {
+            log.error("Error evaluating operator : " + physOp.name());
+        }
+
+        this.lineage = oldLineage;
+
+        physOp.setInputs(inputs);
+        physOp.setLineageTracer(null);
+
+        return output;
+    }
+
+}

Added: incubator/pig/branches/types/src/org/apache/pig/pen/EquivalenceClasses.java
URL: http://svn.apache.org/viewvc/incubator/pig/branches/types/src/org/apache/pig/pen/EquivalenceClasses.java?rev=699505&view=auto
==============================================================================
--- incubator/pig/branches/types/src/org/apache/pig/pen/EquivalenceClasses.java (added)
+++ incubator/pig/branches/types/src/org/apache/pig/pen/EquivalenceClasses.java Fri Sep 26 14:23:35 2008
@@ -0,0 +1,193 @@
+/*
+ * 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.pen;
+
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.Map;
+
+import org.apache.pig.backend.executionengine.ExecException;
+import org.apache.pig.data.DataBag;
+import org.apache.pig.data.Tuple;
+import org.apache.pig.impl.logicalLayer.LOCogroup;
+import org.apache.pig.impl.logicalLayer.LOFilter;
+import org.apache.pig.impl.logicalLayer.LOForEach;
+import org.apache.pig.impl.logicalLayer.LOLoad;
+import org.apache.pig.impl.logicalLayer.LOSort;
+import org.apache.pig.impl.logicalLayer.LOSplit;
+import org.apache.pig.impl.logicalLayer.LOUnion;
+import org.apache.pig.impl.logicalLayer.LogicalOperator;
+import org.apache.pig.impl.util.IdentityHashSet;
+
+import com.sun.tools.jdi.EventSetImpl.Itr;
+
+//These methods are used to generate equivalence classes given the operator name and the output from the operator
+//For example, it gives out 2 eq. classes for filter, one that passes the filter and one that doesn't
+public class EquivalenceClasses {
+    public static Collection<IdentityHashSet<Tuple>> GetEquivalenceClasses(
+            LogicalOperator op, Map<LogicalOperator, DataBag> derivedData)
+            throws ExecException {
+        if (op instanceof LOCogroup)
+            return GetEquivalenceClasses((LOCogroup) op, derivedData);
+        else if (op instanceof LOForEach)
+            return GetEquivalenceClasses((LOForEach) op, derivedData);
+        else if (op instanceof LOFilter)
+            return GetEquivalenceClasses((LOFilter) op, derivedData);
+        else if (op instanceof LOSort)
+            return GetEquivalenceClasses((LOSort) op, derivedData);
+        else if (op instanceof LOSplit)
+            return GetEquivalenceClasses((LOSplit) op, derivedData);
+        else if (op instanceof LOUnion)
+            return GetEquivalenceClasses((LOUnion) op, derivedData);
+        else if (op instanceof LOLoad)
+            return GetEquivalenceClasses((LOLoad) op, derivedData);
+            throw new RuntimeException("Unrecognized logical operator.");
+    }
+
+    static Collection<IdentityHashSet<Tuple>> GetEquivalenceClasses(LOLoad op,
+            Map<LogicalOperator, DataBag> derivedData) {
+        // Since its a load, all the tuples belong to a single equivalence class
+        Collection<IdentityHashSet<Tuple>> equivClasses = new LinkedList<IdentityHashSet<Tuple>>();
+        IdentityHashSet<Tuple> input = new IdentityHashSet<Tuple>();
+
+        equivClasses.add(input);
+
+        DataBag output = derivedData.get(op);
+
+        for (Iterator<Tuple> it = output.iterator(); it.hasNext();) {
+            Tuple t = it.next();
+
+            input.add(t);
+        }
+
+        return equivClasses;
+    }
+
+    static Collection<IdentityHashSet<Tuple>> GetEquivalenceClasses(
+            LOCogroup op, Map<LogicalOperator, DataBag> derivedData)
+            throws ExecException {
+        Collection<IdentityHashSet<Tuple>> equivClasses = new LinkedList<IdentityHashSet<Tuple>>();
+        IdentityHashSet<Tuple> acceptableGroups = new IdentityHashSet<Tuple>();
+
+        equivClasses.add(acceptableGroups);
+
+        for (Iterator<Tuple> it = derivedData.get(op).iterator(); it.hasNext();) {
+            Tuple t = it.next();
+
+            boolean isAcceptable;
+
+            if (t.size() == 2) {
+                isAcceptable = (((DataBag) (t.get(1))).size() >= 2);
+            } else {
+                isAcceptable = true;
+                for (int field = 1; field < t.size(); field++) {
+                    DataBag bag = (DataBag) t.get(field);
+                    if (bag.size() == 0) {
+                        isAcceptable = false;
+                        break;
+                    }
+                }
+            }
+
+            if (isAcceptable)
+                acceptableGroups.add(t);
+
+        }
+        return equivClasses;
+    }
+
+    static Collection<IdentityHashSet<Tuple>> GetEquivalenceClasses(
+            LOForEach op, Map<LogicalOperator, DataBag> derivedData) {
+        Collection<IdentityHashSet<Tuple>> equivClasses = new LinkedList<IdentityHashSet<Tuple>>();
+
+        IdentityHashSet<Tuple> equivClass = new IdentityHashSet<Tuple>();
+        equivClasses.add(equivClass);
+
+        for (Iterator<Tuple> it = derivedData.get(op).iterator(); it.hasNext();) {
+            equivClass.add(it.next());
+        }
+
+        return equivClasses;
+    }
+
+    static Collection<IdentityHashSet<Tuple>> GetEquivalenceClasses(
+            LOFilter op, Map<LogicalOperator, DataBag> derivedData) {
+        Collection<IdentityHashSet<Tuple>> equivClasses = new LinkedList<IdentityHashSet<Tuple>>();
+
+        IdentityHashSet<Tuple> pass = new IdentityHashSet<Tuple>();
+        IdentityHashSet<Tuple> fail = new IdentityHashSet<Tuple>();
+
+        for (Iterator<Tuple> it = derivedData.get(op).iterator(); it.hasNext();) {
+            pass.add(it.next());
+        }
+
+        LogicalOperator input = op.getInput();
+
+        for (Iterator<Tuple> it = derivedData.get(input).iterator(); it
+                .hasNext();) {
+            Tuple t = it.next();
+            if (pass.contains(t))
+                continue;
+            else
+                fail.add(t);
+        }
+
+        equivClasses.add(pass);
+        equivClasses.add(fail);
+
+        return equivClasses;
+
+    }
+
+    static Collection<IdentityHashSet<Tuple>> GetEquivalenceClasses(LOSort op,
+            Map<LogicalOperator, DataBag> derivedData) {
+        //We don't create any eq. class for sort
+        IdentityHashSet<Tuple> temp = new IdentityHashSet<Tuple>();
+        Collection<IdentityHashSet<Tuple>> output = new LinkedList<IdentityHashSet<Tuple>>();
+        output.add(temp);
+        return output;
+    }
+
+    static Collection<IdentityHashSet<Tuple>> GetEquivalenceClasses(LOSplit op,
+            Map<LogicalOperator, DataBag> derivedData) {
+        throw new RuntimeException(
+                "LOSplit not supported yet in example generator.");
+    }
+
+    static Collection<IdentityHashSet<Tuple>> GetEquivalenceClasses(LOUnion op,
+            Map<LogicalOperator, DataBag> derivedData) {
+
+        // make one equivalence class per input relation
+
+        Collection<IdentityHashSet<Tuple>> equivClasses = new LinkedList<IdentityHashSet<Tuple>>();
+
+        for (LogicalOperator input : op.getInputs()) {
+            IdentityHashSet<Tuple> equivClass = new IdentityHashSet<Tuple>();
+
+            for (Iterator<Tuple> it = derivedData.get(input).iterator(); it
+                    .hasNext();) {
+                equivClass.add(it.next());
+            }
+            equivClasses.add(equivClass);
+        }
+
+        return equivClasses;
+    }
+}

Added: incubator/pig/branches/types/src/org/apache/pig/pen/ExampleGenerator.java
URL: http://svn.apache.org/viewvc/incubator/pig/branches/types/src/org/apache/pig/pen/ExampleGenerator.java?rev=699505&view=auto
==============================================================================
--- incubator/pig/branches/types/src/org/apache/pig/pen/ExampleGenerator.java (added)
+++ incubator/pig/branches/types/src/org/apache/pig/pen/ExampleGenerator.java Fri Sep 26 14:23:35 2008
@@ -0,0 +1,235 @@
+/*
+ * 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.pen;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.pig.ExecType;
+import org.apache.pig.backend.executionengine.ExecException;
+import org.apache.pig.backend.hadoop.executionengine.physicalLayer.POStatus;
+import org.apache.pig.backend.hadoop.executionengine.physicalLayer.PhysicalOperator;
+import org.apache.pig.backend.hadoop.executionengine.physicalLayer.Result;
+import org.apache.pig.backend.hadoop.executionengine.physicalLayer.plans.PhysicalPlan;
+import org.apache.pig.backend.hadoop.executionengine.physicalLayer.relationalOperators.POLoad;
+import org.apache.pig.backend.local.executionengine.physicalLayer.LocalLogToPhyTranslationVisitor;
+import org.apache.pig.data.BagFactory;
+import org.apache.pig.data.DataBag;
+import org.apache.pig.data.Tuple;
+import org.apache.pig.impl.PigContext;
+import org.apache.pig.impl.logicalLayer.FrontendException;
+import org.apache.pig.impl.logicalLayer.LOLoad;
+import org.apache.pig.impl.logicalLayer.LogicalOperator;
+import org.apache.pig.impl.logicalLayer.LogicalPlan;
+import org.apache.pig.impl.logicalLayer.PlanSetter;
+import org.apache.pig.impl.logicalLayer.optimizer.LogicalOptimizer;
+import org.apache.pig.impl.logicalLayer.schema.Schema;
+import org.apache.pig.impl.logicalLayer.validators.LogicalPlanValidationExecutor;
+import org.apache.pig.impl.plan.CompilationMessageCollector;
+import org.apache.pig.impl.plan.VisitorException;
+import org.apache.pig.pen.util.DisplayExamples;
+import org.apache.pig.pen.util.FunctionalLogicalOptimizer;
+import org.apache.pig.pen.util.LineageTracer;
+
+public class ExampleGenerator {
+
+    LogicalPlan plan;
+    Map<LOLoad, DataBag> baseData;
+    PigContext pigContext;
+
+    Map<LogicalOperator, PhysicalOperator> LogToPhyMap;
+    PhysicalPlan physPlan;
+
+    Log log = LogFactory.getLog(getClass());
+
+    private int MAX_RECORDS = 10000;
+
+    public ExampleGenerator(LogicalPlan plan, PigContext hadoopPigContext) {
+        this.plan = plan;
+//        pigContext = new PigContext(ExecType.LOCAL, hadoopPigContext
+//                .getProperties());
+        pigContext = hadoopPigContext;
+        try {
+            pigContext.connect();
+        } catch (ExecException e) {
+            log.error("Error connecting to the cluster "
+                    + e.getLocalizedMessage());
+
+        }
+
+    }
+
+    public void setMaxRecords(int max) {
+        MAX_RECORDS = max;
+    }
+
+    public Map<LogicalOperator, DataBag> getExamples() {
+
+        compilePlan(plan);
+
+        List<LogicalOperator> loads = plan.getRoots();
+
+        try {
+            readBaseData(loads);
+        } catch (ExecException e) {
+            // TODO Auto-generated catch block
+            log.error("Error reading data. " + e.getMessage());
+            throw new RuntimeException(e.getMessage());
+        } catch (FrontendException e) {
+            // TODO Auto-generated catch block
+            log.error("Error reading data. " + e.getMessage());
+        }
+
+        DerivedDataVisitor derivedData = null;
+        try {
+
+            // create derived data and trim base data
+            LineageTrimmingVisitor trimmer = new LineageTrimmingVisitor(plan,
+                    baseData, LogToPhyMap, physPlan, pigContext);
+            trimmer.visit();
+            // System.out.println(
+            // "Obtained the first level derived and trimmed data");
+            // create new derived data from trimmed basedata
+            derivedData = new DerivedDataVisitor(plan, null, baseData,
+                    trimmer.LogToPhyMap, physPlan);
+            derivedData.visit();
+
+            // System.out.println(
+            // "Got new derived data from the trimmed base data");
+            // augment base data
+            AugmentBaseDataVisitor augment = new AugmentBaseDataVisitor(plan,
+                    baseData, derivedData.derivedData);
+            augment.visit();
+            this.baseData = augment.getNewBaseData();
+            // System.out.println("Obtained augmented base data");
+            // create new derived data and trim the base data after augmenting
+            // base data with synthetic tuples
+            trimmer = new LineageTrimmingVisitor(plan, baseData,
+                    derivedData.LogToPhyMap, physPlan, pigContext);
+            trimmer.visit();
+            // System.out.println("Final trimming");
+            // create the final version of derivedData to give to the output
+            derivedData = new DerivedDataVisitor(plan, null, baseData,
+                    trimmer.LogToPhyMap, physPlan);
+            derivedData.visit();
+            // System.out.println("Obtaining final derived data for output");
+
+        } catch (VisitorException e) {
+            // TODO Auto-generated catch block
+            log.error("Visitor exception while creating example data "
+                    + e.getMessage());
+        }
+
+        // DisplayExamples.PrintSimple(plan.getLeaves().get(0),
+        // derivedData.derivedData);
+        System.out.println(DisplayExamples.PrintTabular(plan,
+                derivedData.derivedData));
+        return derivedData.derivedData;
+    }
+
+    private void readBaseData(List<LogicalOperator> loads) throws ExecException, FrontendException {
+        baseData = new HashMap<LOLoad, DataBag>();
+        for (LogicalOperator op : loads) {
+            Schema schema = op.getSchema();
+            if(schema == null) {
+                throw new ExecException("Example Generator requires a schema. Please provide a schema while loading data.");
+            }
+            
+            DataBag opBaseData = BagFactory.getInstance().newDefaultBag();
+
+            POLoad poLoad = (POLoad) LogToPhyMap.get(op);
+//            PigContext oldPC = poLoad.getPc();
+//            poLoad.setPc(pigContext);
+
+            poLoad.setLineageTracer(new LineageTracer());
+
+            Tuple t = null;
+            int count = 0;
+            for (Result res = poLoad.getNext(t); res.returnStatus != POStatus.STATUS_EOP
+                    && count < MAX_RECORDS; res = poLoad.getNext(t)) {
+                if (res.returnStatus == POStatus.STATUS_NULL)
+                    continue;
+                if (res.returnStatus == POStatus.STATUS_ERR) {
+                    log.error("Error reading Tuple");
+                } else {
+                    opBaseData.add((Tuple) res.result);
+                    count++;
+                }
+
+            }
+            baseData.put((LOLoad) op, opBaseData);
+            // poLoad.setPc(oldPC);
+            poLoad.setLineageTracer(null);
+        }
+
+    }
+
+    private void compilePlan(LogicalPlan plan) {
+
+        plan = refineLogicalPlan(plan);
+
+        LocalLogToPhyTranslationVisitor visitor = new LocalLogToPhyTranslationVisitor(
+                plan);
+        visitor.setPigContext(pigContext);
+        try {
+            visitor.visit();
+        } catch (VisitorException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+            log.error("Error visiting the logical plan in ExampleGenerator");
+        }
+        physPlan = visitor.getPhysicalPlan();
+        LogToPhyMap = visitor.getLogToPhyMap();
+    }
+
+    private LogicalPlan refineLogicalPlan(LogicalPlan plan) {
+        PlanSetter ps = new PlanSetter(plan);
+        try {
+            ps.visit();
+
+        } catch (VisitorException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        }
+
+        // run through validator
+        CompilationMessageCollector collector = new CompilationMessageCollector();
+        FrontendException caught = null;
+        try {
+            LogicalPlanValidationExecutor validator = new LogicalPlanValidationExecutor(
+                    plan, pigContext);
+            validator.validate(plan, collector);
+
+            FunctionalLogicalOptimizer optimizer = new FunctionalLogicalOptimizer(
+                    plan);
+            optimizer.optimize();
+        } catch (FrontendException fe) {
+            // Need to go through and see what the collector has in it. But
+            // remember what we've caught so we can wrap it into what we
+            // throw.
+            caught = fe;
+        }
+
+        return plan;
+
+    }
+}
\ No newline at end of file