net.sf.saffron.sql2rel
Class SqlToRelConverter

java.lang.Object
  |
  +--net.sf.saffron.sql2rel.SqlToRelConverter

public class SqlToRelConverter
extends Object

Converts a SQL parse tree (consisting of SqlNode objects) into a relational algebra expression (consisting of SaffronRel objects).

This class accomplishes in one step what you could accomplish in two steps using a SqlToOpenjavaConverter followed by a OJQueryExpander.

Since:
Oct 10, 2003
Version:
$Id: //open/saffron/src/net/sf/saffron/sql2rel/SqlToRelConverter.java#12 $
Author:
jhyde

Nested Class Summary
private  class SqlToRelConverter.Blackboard
          Workspace for translating an individual SELECT statement (or sub-SELECT).
(package private) static class SqlToRelConverter.DeferredLookup
          Contains the information necessary to repeat a call to Blackboard#lookup.
(package private)  class SqlToRelConverter.NullDefaultValueFactory
          An implementation of DefaultValueFactory which always supplies NULL.
static class SqlToRelConverter.SchemaCatalogReader
          A SchemaCatalogReader looks up catalog information from a saffron schema object.
 
Field Summary
private static HashMap binaryMap
          Maps an operator name to an operator.
private  VolcanoCluster cluster
           
private  SaffronConnection connection
           
private  DefaultValueFactory defaultValueFactory
           
private static HashMap functionMap
          Maps a function name to an operator.
(package private)  ArrayList leaves
           
private  HashMap mapScopeToRel
           
private static HashMap postfixMap
          Maps an operator name to an operator.
private static HashMap prefixMap
          Maps an operator name to an operator.
private  boolean producingFlatReferences
           
private  RexBuilder rexBuilder
           
private  SaffronSchema schema
           
private  SqlValidator validator
           
 
Constructor Summary
SqlToRelConverter(SqlValidator validator, SaffronSchema schema, Environment env, SaffronConnection connection)
          Creates a converter
 
Method Summary
private static boolean contains(String[] names, int length, String s)
           
private  SaffronRel convertDelete(SqlDelete call)
           
private  SaffronRel convertExists(SqlToRelConverter.Blackboard bb, SqlNode seek, SqlNode condition, RexLiteral extraExpr, String extraName)
          Converts a query into a join with an indicator variable.
 RexNode convertExpression(SqlToRelConverter.Blackboard bb, SqlNode node)
          Converts an expression from SqlNode to RexNode format.
private  RexNode[] convertExpressionList(SqlToRelConverter.Blackboard bb, SqlNode[] nodes)
           
private  void convertFrom(SqlToRelConverter.Blackboard bb, SqlNode from)
          Converts a FROM clause into a relational expression.
private  void convertGroup(SqlToRelConverter.Blackboard bb, SqlNodeList groupList)
          Converts a GROUP BY clause.
private  void convertHaving(SqlToRelConverter.Blackboard bb, SqlNode having)
           
private  RexNode convertIdentifier(SqlToRelConverter.Blackboard bb, SqlIdentifier identifier)
          Converts an identifier into an expression in a given scope.
private  SaffronRel convertInsert(SqlInsert call)
           
private  RexNode convertJoinCondition(SqlToRelConverter.Blackboard bb, SqlNode condition, SqlJoinOperator.ConditionType conditionType, SaffronRel leftRel, SaffronRel rightRel)
           
private static int convertJoinType(SqlJoinOperator.JoinType joinType)
           
private  RexLiteral convertLiteral(SqlLiteral literal)
           
private  void convertOrder(SqlToRelConverter.Blackboard bb, SqlNodeList orderList)
          Converts an ORDER BY clause.
 SaffronRel convertQuery(SqlNode query)
          Converts a query's parse tree into a relational expression.
private  SaffronRel convertQueryOrInList(SqlToRelConverter.Blackboard bb, SqlNode seek)
           
private  SaffronRel convertQueryRecursive(SqlNode query)
           
private  SaffronRel convertRowConstructor(SqlToRelConverter.Blackboard bb, SqlCall rowConstructor)
          Converts a row constructor into a relational expression.
 SaffronRel convertSelect(SqlSelect query)
          Converts a SELECT statement's parse tree into a relational expression.
private  void convertSelectList(SqlToRelConverter.Blackboard bb, SqlNodeList selectList)
           
private  void convertValues(SqlToRelConverter.Blackboard bb, SqlCall values)
          Converts a values clause (as in "INSERT INTO T(x,y) VALUES (1,2)") into a relational expression.
private  void convertWhere(SqlToRelConverter.Blackboard bb, SqlNode where)
           
static HashMap createBinaryMap()
           
(package private)  VolcanoCluster createCluster(Environment env)
           
static HashMap createFunctionMap()
           
private  JoinRel createJoin(SqlToRelConverter.Blackboard bb, SaffronRel leftRel, SaffronRel rightRel, SqlNode condition, SqlJoinOperator.ConditionType conditionType, int joinType)
           
static HashMap createPostfixMap()
           
static HashMap createPrefixMap()
           
private  void findSubqueries(SqlToRelConverter.Blackboard bb, SqlNode node)
          Builds a list of all IN or EXISTS operators inside SQL parse tree.
 RexBuilder getRexBuilder()
          Returns the row-expression builder.
static SaffronTable getSaffronTable(SqlValidator.Scope scope, SaffronSchema schema)
          Converts a scope into a SaffronTable.
private  boolean isRowConstructor(SqlNode node)
           
private static RexKind mapSqlFunToRex(SqlFunction op)
           
private  void replaceSubqueries(SqlToRelConverter.Blackboard bb, SqlNode expr)
           
 void requestFlatReferences()
          Request that references to join outputs be produced as "flat" FieldAccesses rather than preserving the structure of the join.
 void setDefaultValueFactory(DefaultValueFactory factory)
          Set a new DefaultValueFactory.
private  void substituteSubqueries(SqlToRelConverter.Blackboard bb)
           
static Test suite()
          Standard method recognised by JUnit.
private static String uniqueFieldName(String[] names, int length, String s)
          Generates a unique name
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

binaryMap

private static final HashMap binaryMap
Maps an operator name to an operator. For example, binaryMap.get("/") yields RexKind.Divide.


prefixMap

private static final HashMap prefixMap
Maps an operator name to an operator. For example, binaryMap.get("-") yields RexKind.MinusPrefix.


postfixMap

private static final HashMap postfixMap
Maps an operator name to an operator.


functionMap

private static final HashMap functionMap
Maps a function name to an operator.


validator

private final SqlValidator validator

rexBuilder

private RexBuilder rexBuilder

connection

private SaffronConnection connection

schema

private SaffronSchema schema

cluster

private VolcanoCluster cluster

mapScopeToRel

private HashMap mapScopeToRel

defaultValueFactory

private DefaultValueFactory defaultValueFactory

producingFlatReferences

private boolean producingFlatReferences

leaves

final ArrayList leaves
Constructor Detail

SqlToRelConverter

public SqlToRelConverter(SqlValidator validator,
                         SaffronSchema schema,
                         Environment env,
                         SaffronConnection connection)
Creates a converter

Parameters:
validator -
schema -
env -
connection -
Pre-condition:
connection != null
Method Detail

getRexBuilder

public RexBuilder getRexBuilder()
Returns the row-expression builder.


setDefaultValueFactory

public void setDefaultValueFactory(DefaultValueFactory factory)
Set a new DefaultValueFactory. To have any effect, this must be called before any convert method.

Parameters:
factory - new DefaultValueFactory

requestFlatReferences

public void requestFlatReferences()
Request that references to join outputs be produced as "flat" FieldAccesses rather than preserving the structure of the join.


convertQuery

public SaffronRel convertQuery(SqlNode query)
Converts a query's parse tree into a relational expression.


convertSelect

public SaffronRel convertSelect(SqlSelect query)
Converts a SELECT statement's parse tree into a relational expression.


convertHaving

private void convertHaving(SqlToRelConverter.Blackboard bb,
                           SqlNode having)

convertWhere

private void convertWhere(SqlToRelConverter.Blackboard bb,
                          SqlNode where)

replaceSubqueries

private void replaceSubqueries(SqlToRelConverter.Blackboard bb,
                               SqlNode expr)

substituteSubqueries

private void substituteSubqueries(SqlToRelConverter.Blackboard bb)

convertExists

private SaffronRel convertExists(SqlToRelConverter.Blackboard bb,
                                 SqlNode seek,
                                 SqlNode condition,
                                 RexLiteral extraExpr,
                                 String extraName)
Converts a query into a join with an indicator variable. The result is a relational expression which outer joins a boolean condition column to the original query. After performing the outer join, the condition will be TRUE if the EXISTS condition holds, NULL otherwise.

Parameters:
bb -
seek - A query, for example 'select * from emp' or 'values (1,2,3)' or '('Foo', 34)'.
condition - May be null, or a node, or a node list
extraExpr - Column expression to add. "TRUE" for EXISTS and IN
extraName - Name of expression to add.
Returns:
relational expression which outer joins a boolean condition column
Pre-condition:
extraExpr == null || extraName != null

convertQueryOrInList

private SaffronRel convertQueryOrInList(SqlToRelConverter.Blackboard bb,
                                        SqlNode seek)

isRowConstructor

private boolean isRowConstructor(SqlNode node)

uniqueFieldName

private static String uniqueFieldName(String[] names,
                                      int length,
                                      String s)
Generates a unique name

Parameters:
names - Array of existing names
length - Number of existing names
s - Suggested name
Returns:
Name which does not match any of the names in the first length positions of the names array.

contains

private static boolean contains(String[] names,
                                int length,
                                String s)

findSubqueries

private void findSubqueries(SqlToRelConverter.Blackboard bb,
                            SqlNode node)
Builds a list of all IN or EXISTS operators inside SQL parse tree. Does not traverse inside queries.


suite

public static Test suite()
Standard method recognised by JUnit.


convertExpression

public RexNode convertExpression(SqlToRelConverter.Blackboard bb,
                                 SqlNode node)
Converts an expression from SqlNode to RexNode format.

Parameters:
bb - Workspace
node - Expression to translate
Returns:
Converted expression

mapSqlFunToRex

private static RexKind mapSqlFunToRex(SqlFunction op)

convertExpressionList

private RexNode[] convertExpressionList(SqlToRelConverter.Blackboard bb,
                                        SqlNode[] nodes)

convertFrom

private void convertFrom(SqlToRelConverter.Blackboard bb,
                         SqlNode from)
Converts a FROM clause into a relational expression.

Parameters:
bb - Scope within which to resolve identifiers
from - FROM clause of a query. Examples include: a single table ("SALES.EMP"), an aliased table ("EMP AS E"), a list of tables ("EMP, DEPT"), an ANSI Join expression ("EMP JOIN DEPT ON EMP.DEPTNO = DEPT.DEPTNO"), a VALUES clause ("VALUES ('Fred', 20)"), a query ("(SELECT * FROM EMP WHERE GENDER = 'F')"), or any combination of the above.
Post-condition:
return != null

createJoin

private JoinRel createJoin(SqlToRelConverter.Blackboard bb,
                           SaffronRel leftRel,
                           SaffronRel rightRel,
                           SqlNode condition,
                           SqlJoinOperator.ConditionType conditionType,
                           int joinType)

convertJoinCondition

private RexNode convertJoinCondition(SqlToRelConverter.Blackboard bb,
                                     SqlNode condition,
                                     SqlJoinOperator.ConditionType conditionType,
                                     SaffronRel leftRel,
                                     SaffronRel rightRel)

convertJoinType

private static int convertJoinType(SqlJoinOperator.JoinType joinType)

convertGroup

private void convertGroup(SqlToRelConverter.Blackboard bb,
                          SqlNodeList groupList)
Converts a GROUP BY clause.

Parameters:
bb - Scope within which to resolve identifiers
groupList - GROUP BY clause, or null
Pre-condition:
child != null
Post-condition:
return != null

convertLiteral

private RexLiteral convertLiteral(SqlLiteral literal)

convertOrder

private void convertOrder(SqlToRelConverter.Blackboard bb,
                          SqlNodeList orderList)
Converts an ORDER BY clause.

Parameters:
bb - Scope within which to resolve identifiers
orderList - Order by clause, or null
Pre-condition:
bb.root != null
Post-condition:
return != null

convertQueryRecursive

private SaffronRel convertQueryRecursive(SqlNode query)

convertInsert

private SaffronRel convertInsert(SqlInsert call)

convertDelete

private SaffronRel convertDelete(SqlDelete call)

convertIdentifier

private RexNode convertIdentifier(SqlToRelConverter.Blackboard bb,
                                  SqlIdentifier identifier)
Converts an identifier into an expression in a given scope. For example, the "empno" in "select empno from emp join dept" becomes "emp.empno".


convertRowConstructor

private SaffronRel convertRowConstructor(SqlToRelConverter.Blackboard bb,
                                         SqlCall rowConstructor)
Converts a row constructor into a relational expression.

Parameters:
bb -
rowConstructor -
Returns:
Relational expression which returns a single row.
Pre-condition:
rowConstructor, isRowConstructor(rowConstructor)

convertSelectList

private void convertSelectList(SqlToRelConverter.Blackboard bb,
                               SqlNodeList selectList)

convertValues

private void convertValues(SqlToRelConverter.Blackboard bb,
                           SqlCall values)
Converts a values clause (as in "INSERT INTO T(x,y) VALUES (1,2)") into a relational expression.


createCluster

VolcanoCluster createCluster(Environment env)

createBinaryMap

public static HashMap createBinaryMap()

createPrefixMap

public static HashMap createPrefixMap()

createPostfixMap

public static HashMap createPostfixMap()

createFunctionMap

public static HashMap createFunctionMap()

getSaffronTable

public static SaffronTable getSaffronTable(SqlValidator.Scope scope,
                                           SaffronSchema schema)
Converts a scope into a SaffronTable. This is only possible if the scope represents an identifier, such as "sales.emp". Otherwise, returns null.


SourceForge.net_Logo