org.eigenbase.sql2rel
Class SqlToRelConverter

java.lang.Object
  extended by org.eigenbase.sql2rel.SqlToRelConverter

public class SqlToRelConverter
extends Object

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

The public entry points are: convertQuery(org.eigenbase.sql.SqlNode, boolean, boolean), convertExpression(SqlNode).

Since:
Oct 10, 2003
Version:
$Id: //open/dev/farrago/src/org/eigenbase/sql2rel/SqlToRelConverter.java#98 $
Author:
jhyde

Nested Class Summary
protected  class SqlToRelConverter.AggConverter
          Converts expressions to aggregates.
protected  class SqlToRelConverter.Blackboard
          Workspace for translating an individual SELECT statement (or sub-SELECT).
private static class SqlToRelConverter.DeferredLookup
           
private  class SqlToRelConverter.HistogramShuttle
          Shuttle which walks over a tree of RexNodes and applies 'over' to all agg functions.
private static class SqlToRelConverter.LookupContext
          Context to find a relational expression to a field offset.
private  class SqlToRelConverter.NoOpSubqueryConverter
          A default implementation of SubqueryConverter that does no conversion.
(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 schema object.
 
Field Summary
protected  RelOptCluster cluster
           
private  RelOptConnection connection
           
private  String correlPrefix
           
private  Stack<String> datasetStack
          Stack of names of datasets requested by the TABLE(SAMPLE(<datasetName>, <query>)) construct.
private  boolean decorrelationEnabled
           
private  DefaultValueFactory defaultValueFactory
           
private  List<SqlDynamicParam> dynamicParamSqlNodes
           
private  SqlNodeToRexConverter exprConverter
           
private  boolean isExplain
           
protected  List<RelNode> leaves
           
private  Map<SqlNode,RexNode> mapConvertedNonCorrSubqs
          Mapping of non-correlated subqueries that have been converted to their equivalent constants.
private  Map<String,SqlToRelConverter.DeferredLookup> mapCorrelToDeferred
          Fields used in name resolution for correlated subqueries.
private  Map<String,RelNode> mapCorrelToRefRel
          Fields used in decorrelation.
private  SortedMap<CorrelatorRel.Correlation,CorrelatorRel> mapCorVarToCorRel
           
private  Map<RexFieldAccess,CorrelatorRel.Correlation> mapFieldAccessToCorVar
           
private  Map<RelNode,SortedSet<CorrelatorRel.Correlation>> mapRefRelToCorVar
           
private  Map<SqlValidatorScope,SqlToRelConverter.LookupContext> mapScopeToLux
           
private  int nDynamicParamsInExplain
           
private  int nextCorrel
           
private  SqlOperatorTable opTab
           
protected  RexBuilder rexBuilder
           
protected  RelOptSchema schema
           
private  boolean shouldConvertTableAccess
           
private  boolean shouldCreateValuesRel
           
private static Logger sqlToRelTracer
           
private  SubqueryConverter subqueryConverter
           
protected  RelDataTypeFactory typeFactory
           
protected  SqlValidator validator
           
 
Constructor Summary
SqlToRelConverter(SqlValidator validator, RelOptSchema schema, Environment env, RelOptPlanner planner, RelOptConnection connection, RexBuilder rexBuilder)
          Creates a converter.
 
Method Summary
 void addConvertedNonCorrSubqs(Map<SqlNode,RexNode> alreadyConvertedNonCorrSubqs)
          Adds to the current map of non-correlated converted subqueries the elements from another map that contains non-correlated subqueries that have been converted by another SqlToRelConverter.
protected  RexNode adjustInputRef(SqlToRelConverter.Blackboard bb, RexInputRef inputRef)
          Adjust the type of a reference to an input field to account for nulls introduced by outer joins; and adjust the offset to match the physical implementation.
protected  void afterTableFunction(SqlToRelConverter.Blackboard bb, SqlCall call, TableFunctionRel callRel)
           
private  RexNode castNullLiteralIfNeeded(RexNode node, RelDataType type)
           
protected  boolean checkConvertedRowType(SqlNode query, RelDataType convertedRowType)
           
private  void checkConvertedType(SqlNode query, RelNode result)
           
protected  void collectInsertTargets(SqlInsert call, RexNode sourceRef, List<String> targetColumnNames, List<RexNode> columnExprs)
          Given an INSERT statement, collects the list of names to be populated and the expressions to put in them.
private static boolean containsInOperator(SqlNode node)
          Returns whether a given node contains a SqlInOperator.
protected  void convertAgg(SqlToRelConverter.Blackboard bb, SqlSelect select, List<SqlNode> orderExprList)
          Converts the SELECT, GROUP BY and HAVING clauses of an aggregate query.
protected  void convertCollectionTable(SqlToRelConverter.Blackboard bb, SqlCall call)
           
protected  RelNode convertColumnList(SqlInsert call, RelNode sourceRel)
          Creates a source for an INSERT statement.
private  RelNode convertCursor(SqlToRelConverter.Blackboard bb, SqlCall cursorCall)
           
private  RelNode convertDelete(SqlDelete call)
           
 RexDynamicParam convertDynamicParam(SqlDynamicParam dynamicParam)
           
private  RelNode convertExists(SqlNode seek, boolean isIn, boolean isExists, boolean needsOuterJoin)
          Converts an EXISTS or IN predicate into a join.
 RexNode convertExpression(SqlNode node)
          Converts an expression from SqlNode to RexNode format.
 RexNode convertExpression(SqlNode node, Map<String,RexNode> nameToNodeMap)
          Converts an expression from SqlNode to RexNode format, mapping identifier references to predefined expressions.
protected  RexNode convertExtendedExpression(SqlNode node, SqlToRelConverter.Blackboard bb)
          Converts a non-standard expression.
 RexNode convertField(RelDataType inputRowType, RelDataTypeField field)
           
protected  void convertFrom(SqlToRelConverter.Blackboard bb, SqlNode from)
          Converts a FROM clause into a relational expression.
private  RexNode convertIdentifier(SqlToRelConverter.Blackboard bb, SqlIdentifier identifier)
          Converts an identifier into an expression in a given scope.
protected  RelNode convertInsert(SqlInsert call)
           
private  RexNode convertInToOr(SqlToRelConverter.Blackboard bb, RexNode[] leftKeys, SqlNodeList valuesList, boolean isNotIn)
          Converts "x IN (1, 2, ...)" to "x=1 OR x=2 OR ...".
private  RexNode convertJoinCondition(SqlToRelConverter.Blackboard bb, SqlNode condition, SqlJoinOperator.ConditionType conditionType, RelNode leftRel, RelNode rightRel)
           
private static JoinRelType convertJoinType(SqlJoinOperator.JoinType joinType)
           
private  RexLiteral convertLiteralInValuesList(SqlNode sqlNode, SqlToRelConverter.Blackboard bb, RelDataType rowType, int iField)
           
private  RelNode convertMerge(SqlMerge call)
           
private  RelNode convertMultisets(SqlNode[] operands, SqlToRelConverter.Blackboard bb)
           
private  boolean convertNonCorrelatedSubq(SqlCall call, SqlToRelConverter.Blackboard bb, RelNode converted, boolean isExists)
          Determines if a subquery is non-correlated and if so, converts it to a constant.
private  void convertOrder(SqlSelect select, SqlToRelConverter.Blackboard bb, List<RelFieldCollation> collationList, List<SqlNode> orderExprList)
           
private  RelFieldCollation convertOrderItem(SqlSelect select, SqlNode orderItem, List<SqlNode> extraExprs, RelFieldCollation.Direction direction)
           
private  RexNode convertOver(SqlToRelConverter.Blackboard bb, SqlNode node)
           
 RelNode convertQuery(SqlNode query, boolean needsValidation, boolean top)
          Converts an unvalidated query's parse tree into a relational expression.
private  RelNode convertQueryOrInList(SqlToRelConverter.Blackboard bb, SqlNode seek)
           
protected  RelNode convertQueryRecursive(SqlNode query, boolean top)
          Recursively converts a query to a relational expression.
private  RelNode convertRowConstructor(SqlToRelConverter.Blackboard bb, SqlCall rowConstructor)
          Converts a row constructor into a relational expression.
private  RelNode convertRowValues(SqlToRelConverter.Blackboard bb, SqlNode rowList, Collection<SqlNode> rows, boolean allowLiteralsOnly)
           
 RelNode convertSelect(SqlSelect select)
          Converts a SELECT statement's parse tree into a relational expression.
protected  void convertSelectImpl(SqlToRelConverter.Blackboard bb, SqlSelect select)
          Implementation of convertSelect(SqlSelect); derived class may override.
private  void convertSelectList(SqlToRelConverter.Blackboard bb, SqlSelect select, List<SqlNode> orderList)
           
protected  RelNode convertSetOp(SqlCall call)
          Converts a set operation (UNION, INTERSECT, MINUS) into relational expressions.
 RelNode convertToSingleValueSubq(SqlSelect select, RelNode plan)
          Converts the RelNode tree for a select statement to a select that produces a single value.
private  RelNode convertUpdate(SqlUpdate call)
           
private  RexNode convertUsing(RelNode leftRel, RelNode rightRel, List<String> nameList)
          Returns an expression for matching columns of a USING clause or inferred from NATURAL JOIN.
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)
          Converts a WHERE clause.
protected  void createAggImpl(SqlToRelConverter.Blackboard bb, SqlToRelConverter.AggConverter aggConverter, SqlNodeList selectList, SqlNodeList groupList, SqlNode having, List<SqlNode> orderExprList)
           
protected  RelNode createAggregate(SqlToRelConverter.Blackboard bb, int groupCount, List<AggregateCall> aggCalls)
          Creates an AggregateRel.
protected  SqlToRelConverter.Blackboard createBlackboard(SqlValidatorScope scope, Map<String,RexNode> nameToNodeMap)
          Factory method for creating translation workspace.
private  String createCorrel()
           
protected  RelNode createJoin(RelNode left, RelNode right, RexNode condition, JoinRelType joinType, Set<String> variablesStopped)
          Factory method that creates a join.
protected  RelNode createJoin(SqlToRelConverter.Blackboard bb, RelNode leftRel, RelNode rightRel, RexNode joinCond, JoinRelType joinType)
           
private  RexNode createJoinConditionForIn(SqlToRelConverter.Blackboard bb, RexNode[] leftKeys, RelNode rightRel)
          Creates the condition for a join implementing an IN clause.
 RelNode decorrelate(SqlNode query, RelNode rootRel)
           
protected  RelNode decorrelateQuery(RelNode rootRel)
           
private  String deriveAlias(SqlNode node, Collection<String> aliases, int ordinal)
           
private  void distinctify(SqlToRelConverter.Blackboard bb, boolean checkForDupExprs)
          Having translated 'SELECT ...
protected  boolean enableDecorrelation()
           
 void enableTableAccessConversion(boolean enabled)
          Controls whether table access references are converted to physical rels immediately.
 void enableValuesRelCreation(boolean enabled)
          Controls whether instances of ValuesRel are generated.
protected  void extraSelectItems(SqlToRelConverter.Blackboard bb, SqlSelect select, List<RexNode> exprList, List<String> nameList, Collection<String> aliasList, List<SqlMonotonicity> columnMonotonicityList)
          Adds extra select items.
private  int findExpr(RexNode seek, RexNode[] exprs, int count)
           
private  void findSubqueries(SqlToRelConverter.Blackboard bb, SqlNode node, boolean registerOnlyScalarSubqueries)
          Builds a list of all IN or EXISTS operators inside SQL parse tree.
 RelNode flattenTypes(RelNode rootRel, boolean restructure)
           
private  void gatherOrderExprs(SqlToRelConverter.Blackboard bb, SqlSelect select, SqlNodeList orderList, List<SqlNode> extraOrderExprs, List<RelFieldCollation> collationList)
          Creates a list of collations required to implement the ORDER BY clause, if there is one.
 RelOptCluster getCluster()
           
private  Set<RelColumnMapping> getColumnMappings(SqlOperator op)
           
private  int getCorrelOrdinal(String correlName)
           
 int getDynamicParamCount()
          Returns the number of dynamic parameters encountered during translation; this must only be called after convertQuery(org.eigenbase.sql.SqlNode, boolean, boolean).
 int getDynamicParamCountInExplain(boolean increment)
          Returns the current count of the number of dynamic parameters in an EXPLAIN PLAN statement.
 RelDataType getDynamicParamType(int index)
          Returns the type inferred for a dynamic parameter.
protected  int getInSubqueryThreshold()
          Gets the list size threshold under which convertInToOr(org.eigenbase.sql2rel.SqlToRelConverter.Blackboard, org.eigenbase.rex.RexNode[], org.eigenbase.sql.SqlNodeList, boolean) is used.
 Map<SqlNode,RexNode> getMapConvertedNonCorrSubqs()
           
 RexBuilder getRexBuilder()
          Returns the row-expression builder.
protected  List<RelDataTypeField> getSystemFields()
          Returns a list of fields to be prefixed to each relational expression.
protected  RelOptTable getTargetTable(SqlNode call)
           
 boolean hasCorrelation()
           
private  boolean isRowConstructor(SqlNode node)
           
private  boolean isSubqNonCorrelated(RelNode subq, SqlToRelConverter.Blackboard bb)
          Determines whether a subquery is non-correlated.
private static SqlNode pushdownNotForIn(SqlNode sqlNode)
          Pushdown allthe NOT logical operators into any IN/NOT IN operators.
private  void replaceSubqueries(SqlToRelConverter.Blackboard bb, SqlNode expr)
           
 void setDefaultValueFactory(DefaultValueFactory factory)
          Set a new DefaultValueFactory.
 void setIsExplain(int nDynamicParams)
          Indicates that the current statement is part of an EXPLAIN PLAN statement
 void setSubqueryConverter(SubqueryConverter converter)
          Sets a new SubqueryConverter.
private  void substituteSubquery(SqlToRelConverter.Blackboard bb, SqlNode node)
           
protected  RelDataType uniquifyFields(RelDataType rowType)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

sqlToRelTracer

private static final Logger sqlToRelTracer

validator

protected final SqlValidator validator

rexBuilder

protected final RexBuilder rexBuilder

connection

private final RelOptConnection connection

schema

protected final RelOptSchema schema

cluster

protected final RelOptCluster cluster

mapScopeToLux

private final Map<SqlValidatorScope,SqlToRelConverter.LookupContext> mapScopeToLux

defaultValueFactory

private DefaultValueFactory defaultValueFactory

subqueryConverter

private SubqueryConverter subqueryConverter

leaves

protected final List<RelNode> leaves

dynamicParamSqlNodes

private final List<SqlDynamicParam> dynamicParamSqlNodes

opTab

private final SqlOperatorTable opTab

shouldConvertTableAccess

private boolean shouldConvertTableAccess

typeFactory

protected final RelDataTypeFactory typeFactory

exprConverter

private final SqlNodeToRexConverter exprConverter

decorrelationEnabled

private boolean decorrelationEnabled

shouldCreateValuesRel

private boolean shouldCreateValuesRel

isExplain

private boolean isExplain

nDynamicParamsInExplain

private int nDynamicParamsInExplain

mapCorrelToDeferred

private final Map<String,SqlToRelConverter.DeferredLookup> mapCorrelToDeferred
Fields used in name resolution for correlated subqueries.


nextCorrel

private int nextCorrel

correlPrefix

private final String correlPrefix
See Also:
Constant Field Values

mapCorrelToRefRel

private final Map<String,RelNode> mapCorrelToRefRel
Fields used in decorrelation.


mapCorVarToCorRel

private final SortedMap<CorrelatorRel.Correlation,CorrelatorRel> mapCorVarToCorRel

mapRefRelToCorVar

private final Map<RelNode,SortedSet<CorrelatorRel.Correlation>> mapRefRelToCorVar

mapFieldAccessToCorVar

private final Map<RexFieldAccess,CorrelatorRel.Correlation> mapFieldAccessToCorVar

datasetStack

private final Stack<String> datasetStack
Stack of names of datasets requested by the TABLE(SAMPLE(<datasetName>, <query>)) construct.


mapConvertedNonCorrSubqs

private final Map<SqlNode,RexNode> mapConvertedNonCorrSubqs
Mapping of non-correlated subqueries that have been converted to their equivalent constants. Used to avoid re-evaluating the subquery if it's already been evaluated.

Constructor Detail

SqlToRelConverter

public SqlToRelConverter(SqlValidator validator,
                         RelOptSchema schema,
                         Environment env,
                         RelOptPlanner planner,
                         RelOptConnection connection,
                         RexBuilder rexBuilder)
Creates a converter.

Parameters:
validator - Validator
schema - Schema
env - Environment
connection - Connection
rexBuilder - Rex builder
"Precondition:"
connection != null
Method Detail

getCluster

public RelOptCluster getCluster()
Returns:
the RelOptCluster in use.

getRexBuilder

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


getDynamicParamCount

public int getDynamicParamCount()
Returns the number of dynamic parameters encountered during translation; this must only be called after convertQuery(org.eigenbase.sql.SqlNode, boolean, boolean).

Returns:
number of dynamic parameters

getDynamicParamType

public RelDataType getDynamicParamType(int index)
Returns the type inferred for a dynamic parameter.

Parameters:
index - 0-based index of dynamic parameter
Returns:
inferred type, never null

getDynamicParamCountInExplain

public int getDynamicParamCountInExplain(boolean increment)
Returns the current count of the number of dynamic parameters in an EXPLAIN PLAN statement.

Parameters:
increment - if true, increment the count
Returns:
the current count before the optional increment

getMapConvertedNonCorrSubqs

public Map<SqlNode,RexNode> getMapConvertedNonCorrSubqs()
Returns:
mapping of non-correlated subqueries that have been converted to the constants that they evaluate to

addConvertedNonCorrSubqs

public void addConvertedNonCorrSubqs(Map<SqlNode,RexNode> alreadyConvertedNonCorrSubqs)
Adds to the current map of non-correlated converted subqueries the elements from another map that contains non-correlated subqueries that have been converted by another SqlToRelConverter.

Parameters:
alreadyConvertedNonCorrSubqs - the other map

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

setSubqueryConverter

public void setSubqueryConverter(SubqueryConverter converter)
Sets a new SubqueryConverter. To have any effect, this must be called before any convert method.

Parameters:
converter - new SubqueryConverter

setIsExplain

public void setIsExplain(int nDynamicParams)
Indicates that the current statement is part of an EXPLAIN PLAN statement

Parameters:
nDynamicParams - number of dynamic parameters in the statement

enableTableAccessConversion

public void enableTableAccessConversion(boolean enabled)
Controls whether table access references are converted to physical rels immediately. The optimizer doesn't like leaf rels to have CallingConvention.NONE. However, if we are doing further conversion passes (e.g. RelStructuredTypeFlattener), then we may need to defer conversion. To have any effect, this must be called before any convert method.

Parameters:
enabled - true for immediate conversion (the default); false to generate logical TableAccessRel instances

enableValuesRelCreation

public void enableValuesRelCreation(boolean enabled)
Controls whether instances of ValuesRel are generated. These may not be supported by all physical implementations. To have any effect, this must be called before any convert method.

Parameters:
enabled - true to allow ValuesRel to be generated (the default); false to force substitution of ProjectRel+OneRowRel instead

checkConvertedType

private void checkConvertedType(SqlNode query,
                                RelNode result)

flattenTypes

public RelNode flattenTypes(RelNode rootRel,
                            boolean restructure)

decorrelate

public RelNode decorrelate(SqlNode query,
                           RelNode rootRel)

convertQuery

public RelNode convertQuery(SqlNode query,
                            boolean needsValidation,
                            boolean top)
Converts an unvalidated query's parse tree into a relational expression.

Parameters:
query - Query to convert
needsValidation - Whether to validate the query before converting; false if the query has already been validated.
top - Whether the query is top-level, say if its result will become a JDBC result set; false if the query will be part of a view.

checkConvertedRowType

protected boolean checkConvertedRowType(SqlNode query,
                                        RelDataType convertedRowType)

uniquifyFields

protected RelDataType uniquifyFields(RelDataType rowType)

convertSelect

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


createBlackboard

protected SqlToRelConverter.Blackboard createBlackboard(SqlValidatorScope scope,
                                                        Map<String,RexNode> nameToNodeMap)
Factory method for creating translation workspace.


convertSelectImpl

protected void convertSelectImpl(SqlToRelConverter.Blackboard bb,
                                 SqlSelect select)
Implementation of convertSelect(SqlSelect); derived class may override.


distinctify

private void distinctify(SqlToRelConverter.Blackboard bb,
                         boolean checkForDupExprs)
Having translated 'SELECT ... FROM ... [GROUP BY ...] [HAVING ...]', adds a relational expression to make the results unique.

If the SELECT clause contains duplicate expressions, adds ProjectRels so that we are grouping on the minimal set of keys. The performance gain isn't huge, but it is difficult to detect these duplicate expressions later.

Parameters:
bb - Blackboard
checkForDupExprs - Check for duplicate expressions

findExpr

private int findExpr(RexNode seek,
                     RexNode[] exprs,
                     int count)

convertOrder

private void convertOrder(SqlSelect select,
                          SqlToRelConverter.Blackboard bb,
                          List<RelFieldCollation> collationList,
                          List<SqlNode> orderExprList)

containsInOperator

private static boolean containsInOperator(SqlNode node)
Returns whether a given node contains a SqlInOperator.

Parameters:
node - a RexNode tree

pushdownNotForIn

private static SqlNode pushdownNotForIn(SqlNode sqlNode)
Pushdown allthe NOT logical operators into any IN/NOT IN operators.

Parameters:
sqlNode - the root node from which to look for NOT operators
Returns:
the transformed SqlNode representation with NOT pushed down.

convertWhere

private void convertWhere(SqlToRelConverter.Blackboard bb,
                          SqlNode where)
Converts a WHERE clause.

Parameters:
bb - Blackboard
where - WHERE clause, may be null

replaceSubqueries

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

substituteSubquery

private void substituteSubquery(SqlToRelConverter.Blackboard bb,
                                SqlNode node)

convertNonCorrelatedSubq

private boolean convertNonCorrelatedSubq(SqlCall call,
                                         SqlToRelConverter.Blackboard bb,
                                         RelNode converted,
                                         boolean isExists)
Determines if a subquery is non-correlated and if so, converts it to a constant.

Parameters:
call - the call that references the subquery
bb - blackboard used to convert the subquery
converted - RelNode tree corresponding to the subquery
isExists - true if the subquery is part of an EXISTS expression
Returns:
if the subquery can be converted to a constant

convertToSingleValueSubq

public RelNode convertToSingleValueSubq(SqlSelect select,
                                        RelNode plan)
Converts the RelNode tree for a select statement to a select that produces a single value.

Parameters:
select - the statement
plan - the original RelNode tree corresponding to the statement
Returns:
the converted RelNode tree

convertInToOr

private RexNode convertInToOr(SqlToRelConverter.Blackboard bb,
                              RexNode[] leftKeys,
                              SqlNodeList valuesList,
                              boolean isNotIn)
Converts "x IN (1, 2, ...)" to "x=1 OR x=2 OR ...".

Parameters:
leftKeys - LHS
valuesList - RHS
isNotIn - is this a NOT IN operator
Returns:
converted expression

getInSubqueryThreshold

protected int getInSubqueryThreshold()
Gets the list size threshold under which convertInToOr(org.eigenbase.sql2rel.SqlToRelConverter.Blackboard, org.eigenbase.rex.RexNode[], org.eigenbase.sql.SqlNodeList, boolean) is used. Lists of this size or greater will instead be converted to use a join against an inline table (ValuesRel) rather than a predicate. A threshold of 0 forces usage of an inline table in all cases; a threshold of Integer.MAX_VALUE forces usage of OR in all cases

Returns:
threshold, default 20

createJoinConditionForIn

private RexNode createJoinConditionForIn(SqlToRelConverter.Blackboard bb,
                                         RexNode[] leftKeys,
                                         RelNode rightRel)
Creates the condition for a join implementing an IN clause.

Parameters:
bb - blackboard to use, bb.root points to the LHS
leftKeys - LHS of IN
rightRel - Relational expression on RHS
Returns:
join condition

convertExists

private RelNode convertExists(SqlNode seek,
                              boolean isIn,
                              boolean isExists,
                              boolean needsOuterJoin)
Converts an EXISTS or IN predicate into a join. For EXISTS, the subquery produces an indicator variable, and the result is a relational expression which outer joins that indicator to the original query. After performing the outer join, the condition will be TRUE if the EXISTS condition holds, NULL otherwise.

FIXME jvs 1-May-2006: null semantics for IN are currently broken

Parameters:
seek - A query, for example 'select * from emp' or 'values (1,2,3)' or '('Foo', 34)'.
isIn - Whether is an IN predicate
isExists - Whether is an EXISTS predicate
needsOuterJoin - Whether an outer join is needed
Returns:
join expression
"Precondition:"
extraExpr == null || extraName != null

convertQueryOrInList

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

convertRowValues

private RelNode convertRowValues(SqlToRelConverter.Blackboard bb,
                                 SqlNode rowList,
                                 Collection<SqlNode> rows,
                                 boolean allowLiteralsOnly)

convertLiteralInValuesList

private RexLiteral convertLiteralInValuesList(SqlNode sqlNode,
                                              SqlToRelConverter.Blackboard bb,
                                              RelDataType rowType,
                                              int iField)

isRowConstructor

private boolean isRowConstructor(SqlNode node)

findSubqueries

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

Parameters:
bb - blackboard
node - the SQL parse tree
registerOnlyScalarSubqueries - if set to true and the parse tree corresponds to a variation of a select node, only register it if it's a scalar subquery

convertExpression

public RexNode convertExpression(SqlNode node)
Converts an expression from SqlNode to RexNode format.

Parameters:
node - Expression to translate
Returns:
Converted expression

convertExpression

public RexNode convertExpression(SqlNode node,
                                 Map<String,RexNode> nameToNodeMap)
Converts an expression from SqlNode to RexNode format, mapping identifier references to predefined expressions.

Parameters:
node - Expression to translate
nameToNodeMap - map from String to RexNode; when an SqlIdentifier is encountered, it is used as a key and translated to the corresponding value from this map
Returns:
Converted expression

convertExtendedExpression

protected RexNode convertExtendedExpression(SqlNode node,
                                            SqlToRelConverter.Blackboard bb)
Converts a non-standard expression.

This method is an extension-point for derived classes can override. If this method returns a null result, the normal expression translation process will proceeed. The default implementation always returns null.

Parameters:
node - Expression
bb - Blackboard
Returns:
null to proceed with the usual expression translation process

convertOver

private RexNode convertOver(SqlToRelConverter.Blackboard bb,
                            SqlNode node)

convertFrom

protected 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.
"Postcondition:"
return != null

convertCollectionTable

protected void convertCollectionTable(SqlToRelConverter.Blackboard bb,
                                      SqlCall call)

afterTableFunction

protected void afterTableFunction(SqlToRelConverter.Blackboard bb,
                                  SqlCall call,
                                  TableFunctionRel callRel)

getColumnMappings

private Set<RelColumnMapping> getColumnMappings(SqlOperator op)

createJoin

protected RelNode createJoin(SqlToRelConverter.Blackboard bb,
                             RelNode leftRel,
                             RelNode rightRel,
                             RexNode joinCond,
                             JoinRelType joinType)

isSubqNonCorrelated

private boolean isSubqNonCorrelated(RelNode subq,
                                    SqlToRelConverter.Blackboard bb)
Determines whether a subquery is non-correlated. Note that a non-correlated subquery can contain correlated references, provided those references do not reference select statements that are parents of the subquery.

Parameters:
subq - the subquery
bb - blackboard used while converting the subquery, i.e., the blackboard of the parent query of this subquery
Returns:
true if the subquery is non-correlated.

getSystemFields

protected List<RelDataTypeField> getSystemFields()
Returns a list of fields to be prefixed to each relational expression.

Returns:
List of system fields

convertJoinCondition

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

convertUsing

private RexNode convertUsing(RelNode leftRel,
                             RelNode rightRel,
                             List<String> nameList)
Returns an expression for matching columns of a USING clause or inferred from NATURAL JOIN. "a JOIN b USING (x, y)" becomes "a.x = b.x AND a.y = b.y". Returns null if the column list is empty.

Parameters:
leftRel - Left input to the join
rightRel - Right input to the join
nameList - List of column names to join on
Returns:
Expression to match columns from name list, or null if name list is empty

convertJoinType

private static JoinRelType convertJoinType(SqlJoinOperator.JoinType joinType)

convertAgg

protected void convertAgg(SqlToRelConverter.Blackboard bb,
                          SqlSelect select,
                          List<SqlNode> orderExprList)
Converts the SELECT, GROUP BY and HAVING clauses of an aggregate query.

This method extracts SELECT, GROUP BY and HAVING clauses, and creates an SqlToRelConverter.AggConverter, then delegates to createAggImpl(org.eigenbase.sql2rel.SqlToRelConverter.Blackboard, org.eigenbase.sql2rel.SqlToRelConverter.AggConverter, org.eigenbase.sql.SqlNodeList, org.eigenbase.sql.SqlNodeList, org.eigenbase.sql.SqlNode, java.util.List). Derived class may override this method to change any of those clauses or specify a different SqlToRelConverter.AggConverter.

Parameters:
bb - Scope within which to resolve identifiers
select - Query
orderExprList - Additional expressions needed to implement ORDER BY

createAggImpl

protected final void createAggImpl(SqlToRelConverter.Blackboard bb,
                                   SqlToRelConverter.AggConverter aggConverter,
                                   SqlNodeList selectList,
                                   SqlNodeList groupList,
                                   SqlNode having,
                                   List<SqlNode> orderExprList)

createAggregate

protected RelNode createAggregate(SqlToRelConverter.Blackboard bb,
                                  int groupCount,
                                  List<AggregateCall> aggCalls)
Creates an AggregateRel.

In case the aggregate rel changes the order in which it projects fields, the groupExprProjection parameter is provided, and the implementation of this method may modify it.

The sortedCount parameter is the number of expressions known to be monotonic. These expressions must be on the leading edge of the grouping keys. The default implementation of this method ignores this parameter.

Parameters:
bb - Blackboard
groupCount - Number of expressions to group on
aggCalls - Array of calls to aggregate functions
Returns:
AggregateRel

convertDynamicParam

public RexDynamicParam convertDynamicParam(SqlDynamicParam dynamicParam)

gatherOrderExprs

private void gatherOrderExprs(SqlToRelConverter.Blackboard bb,
                              SqlSelect select,
                              SqlNodeList orderList,
                              List<SqlNode> extraOrderExprs,
                              List<RelFieldCollation> collationList)
Creates a list of collations required to implement the ORDER BY clause, if there is one. Populates extraOrderExprs with any sort expressions which are not in the select clause.

Parameters:
bb - Scope within which to resolve identifiers
select - Select clause, null if order by is applied to set operation (UNION etc.)
orderList - Order by clause, may be null
extraOrderExprs - Sort expressions which are not in the select clause (output)
collationList - List of collations (output)
"Precondition:"
bb.root != null

convertOrderItem

private RelFieldCollation convertOrderItem(SqlSelect select,
                                           SqlNode orderItem,
                                           List<SqlNode> extraExprs,
                                           RelFieldCollation.Direction direction)

enableDecorrelation

protected boolean enableDecorrelation()

hasCorrelation

public boolean hasCorrelation()

decorrelateQuery

protected RelNode decorrelateQuery(RelNode rootRel)

convertQueryRecursive

protected RelNode convertQueryRecursive(SqlNode query,
                                        boolean top)
Recursively converts a query to a relational expression.

Parameters:
query - Query
top - Whether this query is the top-level query of the statement
Returns:
Relational expression

convertSetOp

protected RelNode convertSetOp(SqlCall call)
Converts a set operation (UNION, INTERSECT, MINUS) into relational expressions.

Parameters:
call - Call to set operator
Returns:
Relational expression

convertInsert

protected RelNode convertInsert(SqlInsert call)

getTargetTable

protected RelOptTable getTargetTable(SqlNode call)

convertColumnList

protected RelNode convertColumnList(SqlInsert call,
                                    RelNode sourceRel)
Creates a source for an INSERT statement.

If the column list is not specified, source expressions match target columns in order.

If the column list is specified, Source expressions are mapped to target columns by name via targetColumnList, and may not cover the entire target table. So, we'll make up a full row, using a combination of default values and the source expressions provided.

Parameters:
call - Insert expression
sourceRel - Source relational expression
Returns:
Converted INSERT statement

castNullLiteralIfNeeded

private RexNode castNullLiteralIfNeeded(RexNode node,
                                        RelDataType type)

collectInsertTargets

protected void collectInsertTargets(SqlInsert call,
                                    RexNode sourceRef,
                                    List<String> targetColumnNames,
                                    List<RexNode> columnExprs)
Given an INSERT statement, collects the list of names to be populated and the expressions to put in them.

Parameters:
call - Insert statement
sourceRef - Expression representing a row from the source relational expression
targetColumnNames - List of target column names, to be populated
columnExprs - List of expressions, to be populated

convertDelete

private RelNode convertDelete(SqlDelete call)

convertUpdate

private RelNode convertUpdate(SqlUpdate call)

convertMerge

private RelNode convertMerge(SqlMerge 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".


adjustInputRef

protected RexNode adjustInputRef(SqlToRelConverter.Blackboard bb,
                                 RexInputRef inputRef)
Adjust the type of a reference to an input field to account for nulls introduced by outer joins; and adjust the offset to match the physical implementation.

Parameters:
bb - Blackboard
inputRef - Input ref
Returns:
Adjusted input ref

convertRowConstructor

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

Parameters:
bb - Blackboard
rowConstructor - Row constructor expression
Returns:
Relational expression which returns a single row.
"Precondition:"
isRowConstructor(rowConstructor)

convertCursor

private RelNode convertCursor(SqlToRelConverter.Blackboard bb,
                              SqlCall cursorCall)

convertMultisets

private RelNode convertMultisets(SqlNode[] operands,
                                 SqlToRelConverter.Blackboard bb)

createJoin

protected RelNode createJoin(RelNode left,
                             RelNode right,
                             RexNode condition,
                             JoinRelType joinType,
                             Set<String> variablesStopped)
Factory method that creates a join. A subclass can override to use a different kind of join.

Parameters:
left - Left input
right - Right input
condition - Join condition
joinType - Join type
variablesStopped - Set of names of variables which are set by the LHS and used by the RHS and are not available to nodes above this JoinRel in the tree
Returns:
A relational expression representing a join

convertSelectList

private void convertSelectList(SqlToRelConverter.Blackboard bb,
                               SqlSelect select,
                               List<SqlNode> orderList)

extraSelectItems

protected void extraSelectItems(SqlToRelConverter.Blackboard bb,
                                SqlSelect select,
                                List<RexNode> exprList,
                                List<String> nameList,
                                Collection<String> aliasList,
                                List<SqlMonotonicity> columnMonotonicityList)
Adds extra select items. The default implementation adds nothing; derived classes may add columns to exprList, nameList, aliasList and columnMonotonicityList.

Parameters:
bb - Blackboard
select - Select statement being translated
exprList - List of expressions in select clause
nameList - List of names, one per column
aliasList - Collection of aliases that have been used already
columnMonotonicityList - List of monotonicity, one per column

deriveAlias

private String deriveAlias(SqlNode node,
                           Collection<String> aliases,
                           int ordinal)

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.


convertField

public RexNode convertField(RelDataType inputRowType,
                            RelDataTypeField field)

createCorrel

private String createCorrel()

getCorrelOrdinal

private int getCorrelOrdinal(String correlName)