public class SqlToRelConverter extends Object
SqlNode objects) into a relational algebra
expression (consisting of RelNode objects).
The public entry points are: convertQuery(org.apache.calcite.sql.SqlNode, boolean, boolean),
convertExpression(SqlNode).
| Modifier and Type | Class and Description |
|---|---|
protected class |
SqlToRelConverter.AggConverter
Converts expressions to aggregates.
|
protected class |
SqlToRelConverter.Blackboard
Workspace for translating an individual SELECT statement (or sub-SELECT).
|
| Modifier and Type | Field and Description |
|---|---|
protected Prepare.CatalogReader |
catalogReader |
protected RelOptCluster |
cluster |
protected List<RelNode> |
leaves |
protected RexBuilder |
rexBuilder |
protected static Logger |
SQL2REL_LOGGER |
protected RelDataTypeFactory |
typeFactory |
protected SqlValidator |
validator |
RelOptTable.ViewExpander |
viewExpander |
| Constructor and Description |
|---|
SqlToRelConverter(RelOptTable.ViewExpander viewExpander,
SqlValidator validator,
Prepare.CatalogReader catalogReader,
RelOptPlanner planner,
RexBuilder rexBuilder,
SqlRexConvertletTable convertletTable)
Creates a converter.
|
| Modifier and Type | Method and Description |
|---|---|
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)
Adjusts the type of a reference to an input field to account for nulls
introduced by outer joins; and adjusts the offset to match the physical
implementation.
|
protected void |
afterTableFunction(SqlToRelConverter.Blackboard bb,
SqlCall call,
LogicalTableFunctionScan callRel) |
protected boolean |
checkConvertedRowType(SqlNode query,
RelDataType convertedRowType) |
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.
|
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.
|
RexDynamicParam |
convertDynamicParam(SqlDynamicParam dynamicParam) |
RexNode |
convertExpression(SqlNode node)
|
RexNode |
convertExpression(SqlNode node,
Map<String,RexNode> nameToNodeMap)
|
protected RexNode |
convertExtendedExpression(SqlNode node,
SqlToRelConverter.Blackboard bb)
Converts a non-standard expression.
|
protected void |
convertFrom(SqlToRelConverter.Blackboard bb,
SqlNode from)
Converts a FROM clause into a relational expression.
|
protected RelNode |
convertInsert(SqlInsert call) |
protected void |
convertOrder(SqlSelect select,
SqlToRelConverter.Blackboard bb,
RelCollation collation,
List<SqlNode> orderExprList,
SqlNode offset,
SqlNode fetch)
Converts a query's ORDER BY clause, if any.
|
protected RelFieldCollation |
convertOrderItem(SqlSelect select,
SqlNode orderItem,
List<SqlNode> extraExprs,
RelFieldCollation.Direction direction,
RelFieldCollation.NullDirection nullDirection) |
RelNode |
convertQuery(SqlNode query,
boolean needsValidation,
boolean top)
Converts an unvalidated query's parse tree into a relational expression.
|
protected RelNode |
convertQueryRecursive(SqlNode query,
boolean top,
RelDataType targetRowType)
Recursively converts a query to a relational expression.
|
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. |
protected RelNode |
convertSetOp(SqlCall call)
Converts a set operation (UNION, INTERSECT, MINUS) into relational
expressions.
|
RelNode |
convertToSingleValueSubq(SqlNode query,
RelNode plan)
Converts the RelNode tree for a select statement to a select that
produces a single value.
|
RelNode |
convertValues(SqlCall values,
RelDataType targetRowType)
Converts a SELECT statement's parse tree into a relational expression.
|
RelNode |
convertWith(SqlWith with)
Converts a WITH sub-query into a relational expression.
|
protected void |
createAggImpl(SqlToRelConverter.Blackboard bb,
SqlToRelConverter.AggConverter aggConverter,
SqlNodeList selectList,
SqlNodeList groupList,
SqlNode having,
List<SqlNode> orderExprList) |
protected RelNode |
createAggregate(SqlToRelConverter.Blackboard bb,
boolean indicator,
ImmutableBitSet groupSet,
com.google.common.collect.ImmutableList<ImmutableBitSet> groupSets,
List<AggregateCall> aggCalls)
Creates an Aggregate.
|
protected SqlToRelConverter.Blackboard |
createBlackboard(SqlValidatorScope scope,
Map<String,RexNode> nameToNodeMap)
Factory method for creating translation workspace.
|
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) |
RelNode |
decorrelate(SqlNode query,
RelNode rootRel)
If subquery is correlated and decorrelation is enabled, performs
decorrelation.
|
protected RelNode |
decorrelateQuery(RelNode rootRel) |
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
LogicalValues 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.
|
RelNode |
flattenTypes(RelNode rootRel,
boolean restructure) |
protected 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() |
int |
getDynamicParamCount()
Returns the number of dynamic parameters encountered during translation;
this must only be called after
convertQuery(org.apache.calcite.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.apache.calcite.sql2rel.SqlToRelConverter.Blackboard, java.util.List<org.apache.calcite.rex.RexNode>, org.apache.calcite.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 |
isTrimUnusedFields()
Returns whether to trim unused fields as part of the conversion process.
|
protected RelFieldTrimmer |
newFieldTrimmer()
Creates a RelFieldTrimmer.
|
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.
|
void |
setTrimUnusedFields(boolean trim)
Sets whether to trim unused fields as part of the conversion process.
|
RelNode |
toRel(RelOptTable table) |
RelNode |
trimUnusedFields(RelNode rootRel)
Walks over a tree of relational expressions, replacing each
RelNode with a 'slimmed down' relational expression that projects
only the fields required by its consumer. |
protected RelDataType |
uniquifyFields(RelDataType rowType) |
protected static final Logger SQL2REL_LOGGER
protected final SqlValidator validator
protected final RexBuilder rexBuilder
protected final Prepare.CatalogReader catalogReader
protected final RelOptCluster cluster
protected final RelDataTypeFactory typeFactory
public final RelOptTable.ViewExpander viewExpander
public SqlToRelConverter(RelOptTable.ViewExpander viewExpander, SqlValidator validator, Prepare.CatalogReader catalogReader, RelOptPlanner planner, RexBuilder rexBuilder, SqlRexConvertletTable convertletTable)
viewExpander - Preparing statementvalidator - ValidatorcatalogReader - Schemaplanner - PlannerrexBuilder - Rex builderconvertletTable - Expression converterpublic RelOptCluster getCluster()
public RexBuilder getRexBuilder()
public int getDynamicParamCount()
convertQuery(org.apache.calcite.sql.SqlNode, boolean, boolean).public RelDataType getDynamicParamType(int index)
index - 0-based index of dynamic parameterpublic int getDynamicParamCountInExplain(boolean increment)
increment - if true, increment the countpublic Map<SqlNode,RexNode> getMapConvertedNonCorrSubqs()
public void addConvertedNonCorrSubqs(Map<SqlNode,RexNode> alreadyConvertedNonCorrSubqs)
alreadyConvertedNonCorrSubqs - the other mappublic void setDefaultValueFactory(DefaultValueFactory factory)
factory - new DefaultValueFactorypublic void setSubqueryConverter(SubqueryConverter converter)
converter - new SubqueryConverterpublic void setIsExplain(int nDynamicParams)
nDynamicParams - number of dynamic parameters in the statementpublic void enableTableAccessConversion(boolean enabled)
Convention.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.enabled - true for immediate conversion (the default); false to
generate logical LogicalTableScan instancespublic void enableValuesRelCreation(boolean enabled)
LogicalValues are generated. These
may not be supported by all physical implementations. To have any effect,
this must be called before any convert method.enabled - true to allow LogicalValues to be generated (the default);
false to force substitution of Project+OneRow insteadpublic RelNode decorrelate(SqlNode query, RelNode rootRel)
query - QueryrootRel - Root relational expressionpublic RelNode trimUnusedFields(RelNode rootRel)
RelNode with a 'slimmed down' relational expression that projects
only the fields required by its consumer.
This may make things easier for the optimizer, by removing crud that
would expand the search space, but is difficult for the optimizer itself
to do it, because optimizer rules must preserve the number and type of
fields. Hence, this transform that operates on the entire tree, similar
to the type-flattening transform.
Currently this functionality is disabled in farrago/luciddb; the default implementation of this method does nothing.
rootRel - Relational expression that is at the root of the treeprotected RelFieldTrimmer newFieldTrimmer()
public RelNode convertQuery(SqlNode query, boolean needsValidation, boolean top)
query - Query to convertneedsValidation - 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.protected boolean checkConvertedRowType(SqlNode query, RelDataType convertedRowType)
protected RelDataType uniquifyFields(RelDataType rowType)
public RelNode convertSelect(SqlSelect select)
protected SqlToRelConverter.Blackboard createBlackboard(SqlValidatorScope scope, Map<String,RexNode> nameToNodeMap)
protected void convertSelectImpl(SqlToRelConverter.Blackboard bb, SqlSelect select)
convertSelect(SqlSelect); derived class may
override.protected void convertOrder(SqlSelect select, SqlToRelConverter.Blackboard bb, RelCollation collation, List<SqlNode> orderExprList, SqlNode offset, SqlNode fetch)
select - Querybb - Blackboardcollation - Collation listorderExprList - Method populates this list with orderBy expressions
not present in selectListoffset - Expression for number of rows to discard before
returning first rowfetch - Expression for number of rows to fetchpublic RelNode convertToSingleValueSubq(SqlNode query, RelNode plan)
query - the queryplan - the original RelNode tree corresponding to the statementprotected int getInSubqueryThreshold()
convertInToOr(org.apache.calcite.sql2rel.SqlToRelConverter.Blackboard, java.util.List<org.apache.calcite.rex.RexNode>, org.apache.calcite.sql.SqlNodeList, boolean) is used.
Lists of this size or greater will instead be converted to use a join
against an inline table
(LogicalValues) 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 casespublic RexNode convertExpression(SqlNode node)
node - Expression to translatepublic RexNode convertExpression(SqlNode node, Map<String,RexNode> nameToNodeMap)
SqlNode to RexNode format,
mapping identifier references to predefined expressions.node - Expression to translatenameToNodeMap - map from String to RexNode; when an
SqlIdentifier is encountered, it is used as a
key and translated to the corresponding value from
this mapprotected RexNode convertExtendedExpression(SqlNode node, SqlToRelConverter.Blackboard bb)
This method is an extension-point that derived classes can override. If this method returns a null result, the normal expression translation process will proceed. The default implementation always returns null.
node - Expressionbb - Blackboardprotected void convertFrom(SqlToRelConverter.Blackboard bb, SqlNode from)
bb - Scope within which to resolve identifiersfrom - FROM clause of a query. Examples include:
protected void convertCollectionTable(SqlToRelConverter.Blackboard bb, SqlCall call)
protected void afterTableFunction(SqlToRelConverter.Blackboard bb, SqlCall call, LogicalTableFunctionScan callRel)
protected RelNode createJoin(SqlToRelConverter.Blackboard bb, RelNode leftRel, RelNode rightRel, RexNode joinCond, JoinRelType joinType)
protected List<RelDataTypeField> getSystemFields()
protected void convertAgg(SqlToRelConverter.Blackboard bb, SqlSelect select, List<SqlNode> orderExprList)
This method extracts SELECT, GROUP BY and HAVING clauses, and creates
an SqlToRelConverter.AggConverter, then delegates to createAggImpl(org.apache.calcite.sql2rel.SqlToRelConverter.Blackboard, org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter, org.apache.calcite.sql.SqlNodeList, org.apache.calcite.sql.SqlNodeList, org.apache.calcite.sql.SqlNode, java.util.List<org.apache.calcite.sql.SqlNode>).
Derived class may override this method to change any of those clauses or
specify a different SqlToRelConverter.AggConverter.
bb - Scope within which to resolve identifiersselect - QueryorderExprList - Additional expressions needed to implement ORDER BYprotected final void createAggImpl(SqlToRelConverter.Blackboard bb, SqlToRelConverter.AggConverter aggConverter, SqlNodeList selectList, SqlNodeList groupList, SqlNode having, List<SqlNode> orderExprList)
protected RelNode createAggregate(SqlToRelConverter.Blackboard bb, boolean indicator, ImmutableBitSet groupSet, com.google.common.collect.ImmutableList<ImmutableBitSet> groupSets, List<AggregateCall> aggCalls)
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.
bb - Blackboardindicator - Whether to output fields indicating grouping setsgroupSet - Bit set of ordinals of grouping columnsgroupSets - Grouping setsaggCalls - Array of calls to aggregate functionspublic RexDynamicParam convertDynamicParam(SqlDynamicParam dynamicParam)
protected void gatherOrderExprs(SqlToRelConverter.Blackboard bb, SqlSelect select, SqlNodeList orderList, List<SqlNode> extraOrderExprs, List<RelFieldCollation> collationList)
extraOrderExprs with any sort
expressions which are not in the select clause.bb - Scope within which to resolve identifiersselect - Select clause. Never null, because we invent a
dummy SELECT if ORDER BY is applied to a set
operation (UNION etc.)orderList - Order by clause, may be nullextraOrderExprs - Sort expressions which are not in the select
clause (output)collationList - List of collations (output)protected RelFieldCollation convertOrderItem(SqlSelect select, SqlNode orderItem, List<SqlNode> extraExprs, RelFieldCollation.Direction direction, RelFieldCollation.NullDirection nullDirection)
protected boolean enableDecorrelation()
public void setTrimUnusedFields(boolean trim)
trim - Whether to trim unused fieldspublic boolean isTrimUnusedFields()
protected RelNode convertQueryRecursive(SqlNode query, boolean top, RelDataType targetRowType)
query - Querytop - Whether this query is the top-level query of the
statementtargetRowType - Target row type, or nullprotected RelNode convertSetOp(SqlCall call)
call - Call to set operatorpublic RelNode toRel(RelOptTable table)
protected RelOptTable getTargetTable(SqlNode call)
protected RelNode convertColumnList(SqlInsert call, RelNode sourceRel)
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.
call - Insert expressionsourceRel - Source relational expressionprotected void collectInsertTargets(SqlInsert call, RexNode sourceRef, List<String> targetColumnNames, List<RexNode> columnExprs)
call - Insert statementsourceRef - Expression representing a row from the source
relational expressiontargetColumnNames - List of target column names, to be populatedcolumnExprs - List of expressions, to be populatedprotected RexNode adjustInputRef(SqlToRelConverter.Blackboard bb, RexInputRef inputRef)
bb - BlackboardinputRef - Input refprotected RelNode createJoin(RelNode left, RelNode right, RexNode condition, JoinRelType joinType, Set<String> variablesStopped)
left - Left inputright - Right inputcondition - Join conditionjoinType - Join typevariablesStopped - Set of names of variables which are set by the
LHS and used by the RHS and are not available to
nodes above this LogicalJoin in the treeprotected void extraSelectItems(SqlToRelConverter.Blackboard bb, SqlSelect select, List<RexNode> exprList, List<String> nameList, Collection<String> aliasList, List<SqlMonotonicity> columnMonotonicityList)
bb - Blackboardselect - Select statement being translatedexprList - List of expressions in select clausenameList - List of names, one per columnaliasList - Collection of aliases that have been used
alreadycolumnMonotonicityList - List of monotonicity, one per columnpublic RelNode convertWith(SqlWith with)
public RelNode convertValues(SqlCall values, RelDataType targetRowType)
Copyright © 2012–2015 The Apache Software Foundation. All rights reserved.