/* // $Id: //guest/julian_hyde/saffron/src/main/saffron/rel/Union.java#1 $ // Saffron preprocessor and data engine // Copyright (C) 2002 Julian Hyde <julian.hyde@mail.com> // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Library General Public // License as published by the Free Software Foundation; either // version 2 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Library General Public License for more details. // // You should have received a copy of the GNU Library General Public // License along with this library; if not, write to the // Free Software Foundation, Inc., 59 Temple Place - Suite 330, // Boston, MA 02111-1307, USA. // // See the COPYING file located in the top-level-directory of // the archive of this library for complete text of license. */ package saffron.rel; import openjava.mop.OJClass; import saffron.Planner; import saffron.rel.java.JavaUnionAll; import saffron.rel.iter.IterConcatenate; import saffron.util.Util; import java.util.Iterator; import java.util.NoSuchElementException; /** * todo: * * @author jhyde * @since 23 September, 2001 * @version $Id: //guest/julian_hyde/saffron/src/main/saffron/rel/Union.java#1 $ **/ public class Union extends Rel { protected Rel[] inputs; protected boolean all; public Union(saffron.opt.Cluster cluster, Rel[] inputs, boolean all) { super(cluster); this.inputs = inputs; this.all = all; } public Object clone() { return new Union(cluster, Util.clone(inputs), all); } // implement Rel public Rel[] getInputs() { return inputs; } // implement Rel public void replaceInput(int ordinalInParent, Rel p) { inputs[ordinalInParent] = p; } // implement Rel protected OJClass deriveRowType() { OJClass clazz = null; for (int i = 0; i < inputs.length; i++) { OJClass rowType = inputs[i].getRowType(); if (i == 0) { clazz = rowType; } else { clazz = Util.leastRestrictiveClass(clazz, rowType); } } return clazz; } // implement Rel public double getRows() { double dRows = 0; for (int i = 0; i < inputs.length; i++) { dRows += inputs[i].getRows(); } if (!all) { dRows *= 0.5; } return dRows; } // override Rel public Rel changeConvention(Planner planner, int convention) { switch (convention) { case saffron.opt.CallingConvention.JAVA: { if (getClass() != Union.class) { return null; // require precise class, otherwise we loop } if (!all) { return null; // can only convert non-distinct Union } Rel[] newInputs = new Rel[inputs.length]; for (int i = 0; i < newInputs.length; i++) { newInputs[i] = planner.convert( inputs[i], saffron.opt.CallingConvention.JAVA); if (newInputs[i] == null) { return null; } } return new JavaUnionAll(cluster, newInputs); } case saffron.opt.CallingConvention.ITERATOR: { if (getClass() != Union.class) { return null; // require precise class, otherwise we loop } if (!all) { return null; // can only convert non-distinct Union } Rel[] newInputs = new Rel[inputs.length]; for (int i = 0; i < newInputs.length; i++) { newInputs[i] = planner.convert( inputs[i], saffron.opt.CallingConvention.ITERATOR); if (newInputs[i] == null) { return null; } } return new IterConcatenate(cluster, newInputs); } default: return null; } } ; /** * <code>ConcatenationIterator</code> iterates over a collection of {@link * Iterator}s. * * todo: Move to saffron.runtime. **/ public static class ConcatenationIterator implements Iterator { Iterator[] iterators; int current; public ConcatenationIterator(Iterator[] iterators) { this.iterators = iterators; this.current = 0; } // implement Iterator public boolean hasNext() { for (int i = current; i < iterators.length; i++) { if (iterators[i].hasNext()) { return true; } } return false; } // implement Iterator public Object next() { for (; current < iterators.length; current++) { if (iterators[current].hasNext()) { return iterators[current].next(); } } throw new NoSuchElementException(); } // implement Iterator public void remove() { throw new UnsupportedOperationException(); } }; } // End Union.java
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#1 | 1467 | Julian Hyde |
saffron: First saffron check-in; incorporate my changes to openjava. |