/* // $Id: //guest/julian_hyde/saffron/src/main/saffron/rel/InternalTranslator.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.QueryEnvironment; import openjava.ptree.AliasedExpression; import openjava.ptree.Expression; import openjava.ptree.QueryExpression; import openjava.ptree.Variable; import openjava.ptree.util.EvaluationShuttle; /** * Converts an expression into one which references only internal * variables. * * <p>For example, the filter expression <code>emp.sal > 50</code> from * <code>select * from emp where emp.sal > 50</code> becomes * <code>$input0.sal > 50</code>. And <code>emp.deptno == * dept.deptno</code> becomes <code>$input.$0.deptno == * $input.$1.deptno</code>.</p> **/ class InternalTranslator extends EvaluationShuttle { QueryInfo queryInfo; Rel[] inputs; InternalTranslator(QueryInfo queryInfo, Rel[] inputs) { super(queryInfo.env); this.queryInfo = queryInfo; this.inputs = inputs; } public Expression evaluateUp(AliasedExpression p) { // strip away "AliasedExpression(e)" to leave "e". return p.getExpression(); } public Expression evaluateDown(Variable p) { String varName = p.toString(); // The variable might refer to: // 1. the current row of a relational input --> convert it to a // reference to the pseudo-variable which holds the current row // input relation (or a field of this input relation, if there // is more than one input) // 2. the current row of a query in some enclosing context (it is a // correlating variable) Expression exp = lookupExp(varName); if (exp != null) { return exp; } // 3. a regular java variable --> leave it be return p; } // overrides EvaluationShuttle /** Do not walk into a sub-query. **/ public void visit(QueryExpression p) {} /** * Returns an expression with which to reference a from-list item. * * @param rel the relation which is the context for the expression * @param name the alias of the from item * @return a {@link Variable} or {@link FieldAccess} **/ Expression lookupExp(String name) { QueryEnvironment qenv = (QueryEnvironment) this.getEnvironment(); int count = qenv.lookupFrom(name); if (count >= 0) { // Found in current query's from list. Find which from item. // We assume that the order of the from clause items has been // preserved. return queryInfo.lookup(count, inputs, true); } // Looks for the name in enclosing queries' from lists. for (QueryInfo qi = queryInfo.parent; qi != null; qi = qi.parent) { count = qi.env.lookupFrom(name); if (count < 0) { continue; } return qi.lookup(count, new Rel[] {qi.getRoot()}, false); } // Not found. Must be a java variable return null; } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#3 | 1801 | Julian Hyde |
saffron: add ObjectSchema; rules can now be matched more than once; started to implement correlations in queries in from list. |
||
#2 | 1474 | Julian Hyde |
saffron: Aggregations are working. Renamed 'aggregator' to 'aggregation'. |
||
#1 | 1467 | Julian Hyde |
saffron: First saffron check-in; incorporate my changes to openjava. |