Package org.apache.calcite.sql2rel
Class RelStructuredTypeFlattener
- java.lang.Object
-
- org.apache.calcite.sql2rel.RelStructuredTypeFlattener
-
- All Implemented Interfaces:
ReflectiveVisitor
public class RelStructuredTypeFlattener extends java.lang.Object implements ReflectiveVisitor
RelStructuredTypeFlattener removes all structured types from a tree of relational expressions. Because it must operate globally on the tree, it is implemented as an explicit self-contained rewrite operation instead of via normal optimizer rules. This approach has the benefit that real optimizer and codegen rules never have to deal with structured types.As an example, suppose we have a structured type
ST(A1 smallint, A2 bigint)
, a tableT(c1 ST, c2 double)
, and a queryselect t.c2, t.c1.a2 from t
. After SqlToRelConverter executes, the unflattened tree looks like:LogicalProject(C2=[$1], A2=[$0.A2]) LogicalTableScan(table=[T])
After flattening, the resulting tree looks like
LogicalProject(C2=[$3], A2=[$2]) FtrsIndexScanRel(table=[T], index=[clustered])
The index scan produces a flattened row type
(boolean, smallint, bigint, double)
(the boolean is a null indicator for c1), and the projection picks out the desired attributes (omitting$0
and$1
altogether). After optimization, the projection might be pushed down into the index scan, resulting in a final tree likeFtrsIndexScanRel(table=[T], index=[clustered], projection=[3, 2])
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
RelStructuredTypeFlattener.RewriteRelVisitor
Visitor that flattens each relational expression in a tree.private class
RelStructuredTypeFlattener.RewriteRexShuttle
Shuttle that rewrites scalar expressions.static interface
RelStructuredTypeFlattener.SelfFlatteningRel
Mix-in interface for relational expressions that know how to flatten themselves.
-
Field Summary
Fields Modifier and Type Field Description private RelNode
currentRel
private RelDataType
flattenedRootType
private int
iRestructureInput
private java.util.Map<RelNode,RelNode>
oldToNewRelMap
private RelBuilder
relBuilder
private boolean
restructure
(package private) boolean
restructured
private RexBuilder
rexBuilder
private RelOptTable.ToRelContext
toRelContext
-
Constructor Summary
Constructors Constructor Description RelStructuredTypeFlattener(RexBuilder rexBuilder, RelOptTable.ToRelContext toRelContext, boolean restructure)
Deprecated.RelStructuredTypeFlattener(RelBuilder relBuilder, RexBuilder rexBuilder, RelOptTable.ToRelContext toRelContext, boolean restructure)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private int
calculateFlattenedOffset(RelDataType rowType, int ordinal)
private void
flattenInputs(java.util.List<RelDataTypeField> fieldList, RexNode prefix, java.util.List<Pair<RexNode,java.lang.String>> flattenedExpList)
Generates expressions that reference the flattened input fields from a given row type.private void
flattenNullLiteral(RelDataType type, java.util.List<Pair<RexNode,java.lang.String>> flattenedExps)
private void
flattenProjection(RelStructuredTypeFlattener.RewriteRexShuttle shuttle, RexNode exp, java.lang.String fieldName, java.util.List<Pair<RexNode,java.lang.String>> flattenedExps)
private void
flattenProjections(RelStructuredTypeFlattener.RewriteRexShuttle shuttle, java.util.List<? extends RexNode> exps, java.util.List<java.lang.String> fieldNames, java.lang.String prefix, java.util.List<Pair<RexNode,java.lang.String>> flattenedExps)
protected Ord<RelDataType>
getNewFieldForOldInput(int oldOrdinal)
Maps the ordinal of a field pre-flattening to the ordinal of the corresponding field post-flattening, and also returns its type.protected int
getNewForOldInput(int oldOrdinal)
Maps the ordinal of a field pre-flattening to the ordinal of the corresponding field post-flattening.private Mappings.TargetMapping
getNewForOldInputMapping(RelNode oldRel)
Returns a mapping between old and new fields.protected RelNode
getNewForOldRel(RelNode oldRel)
private boolean
isConstructor(RexNode rexNode)
private RexNode
restructure(RelDataType structuredType)
private java.util.List<RexNode>
restructureFields(RelDataType structuredType)
RelNode
rewrite(RelNode root)
void
rewriteGeneric(RelNode rel)
void
rewriteRel(Collect rel)
void
rewriteRel(Sample rel)
void
rewriteRel(Sort rel)
void
rewriteRel(TableScan rel)
void
rewriteRel(Uncollect rel)
void
rewriteRel(LogicalAggregate rel)
void
rewriteRel(LogicalCalc rel)
void
rewriteRel(LogicalCorrelate rel)
void
rewriteRel(LogicalFilter rel)
void
rewriteRel(LogicalIntersect rel)
void
rewriteRel(LogicalJoin rel)
void
rewriteRel(LogicalMatch rel)
void
rewriteRel(LogicalMinus rel)
void
rewriteRel(LogicalProject rel)
void
rewriteRel(LogicalTableFunctionScan rel)
void
rewriteRel(LogicalTableModify rel)
void
rewriteRel(LogicalUnion rel)
void
rewriteRel(LogicalValues rel)
void
rewriteRel(LogicalChi rel)
void
rewriteRel(LogicalDelta rel)
void
rewriteRel(RelStructuredTypeFlattener.SelfFlatteningRel rel)
protected void
setNewForOldRel(RelNode oldRel, RelNode newRel)
void
updateRelInMap(com.google.common.collect.SortedSetMultimap<RelNode,CorrelationId> mapRefRelToCorVar)
void
updateRelInMap(java.util.SortedMap<CorrelationId,LogicalCorrelate> mapCorVarToCorRel)
-
-
-
Field Detail
-
relBuilder
private final RelBuilder relBuilder
-
rexBuilder
private final RexBuilder rexBuilder
-
restructure
private final boolean restructure
-
currentRel
private RelNode currentRel
-
iRestructureInput
private int iRestructureInput
-
flattenedRootType
private RelDataType flattenedRootType
-
restructured
boolean restructured
-
toRelContext
private final RelOptTable.ToRelContext toRelContext
-
-
Constructor Detail
-
RelStructuredTypeFlattener
@Deprecated public RelStructuredTypeFlattener(RexBuilder rexBuilder, RelOptTable.ToRelContext toRelContext, boolean restructure)
Deprecated.
-
RelStructuredTypeFlattener
public RelStructuredTypeFlattener(RelBuilder relBuilder, RexBuilder rexBuilder, RelOptTable.ToRelContext toRelContext, boolean restructure)
-
-
Method Detail
-
updateRelInMap
public void updateRelInMap(com.google.common.collect.SortedSetMultimap<RelNode,CorrelationId> mapRefRelToCorVar)
-
updateRelInMap
public void updateRelInMap(java.util.SortedMap<CorrelationId,LogicalCorrelate> mapCorVarToCorRel)
-
restructureFields
private java.util.List<RexNode> restructureFields(RelDataType structuredType)
-
restructure
private RexNode restructure(RelDataType structuredType)
-
getNewForOldInput
protected int getNewForOldInput(int oldOrdinal)
Maps the ordinal of a field pre-flattening to the ordinal of the corresponding field post-flattening.- Parameters:
oldOrdinal
- Pre-flattening ordinal- Returns:
- Post-flattening ordinal
-
getNewFieldForOldInput
protected Ord<RelDataType> getNewFieldForOldInput(int oldOrdinal)
Maps the ordinal of a field pre-flattening to the ordinal of the corresponding field post-flattening, and also returns its type.- Parameters:
oldOrdinal
- Pre-flattening ordinal- Returns:
- Post-flattening ordinal and type
-
getNewForOldInputMapping
private Mappings.TargetMapping getNewForOldInputMapping(RelNode oldRel)
Returns a mapping between old and new fields.- Parameters:
oldRel
- Old relational expression- Returns:
- Mapping between fields of old and new
-
calculateFlattenedOffset
private int calculateFlattenedOffset(RelDataType rowType, int ordinal)
-
rewriteRel
public void rewriteRel(LogicalTableModify rel)
-
rewriteRel
public void rewriteRel(LogicalAggregate rel)
-
rewriteRel
public void rewriteRel(Sort rel)
-
rewriteRel
public void rewriteRel(LogicalFilter rel)
-
rewriteRel
public void rewriteRel(LogicalJoin rel)
-
rewriteRel
public void rewriteRel(LogicalCorrelate rel)
-
rewriteRel
public void rewriteRel(Collect rel)
-
rewriteRel
public void rewriteRel(Uncollect rel)
-
rewriteRel
public void rewriteRel(LogicalIntersect rel)
-
rewriteRel
public void rewriteRel(LogicalMinus rel)
-
rewriteRel
public void rewriteRel(LogicalUnion rel)
-
rewriteRel
public void rewriteRel(LogicalValues rel)
-
rewriteRel
public void rewriteRel(LogicalTableFunctionScan rel)
-
rewriteRel
public void rewriteRel(Sample rel)
-
rewriteRel
public void rewriteRel(LogicalProject rel)
-
rewriteRel
public void rewriteRel(LogicalCalc rel)
-
rewriteRel
public void rewriteRel(RelStructuredTypeFlattener.SelfFlatteningRel rel)
-
rewriteGeneric
public void rewriteGeneric(RelNode rel)
-
flattenProjections
private void flattenProjections(RelStructuredTypeFlattener.RewriteRexShuttle shuttle, java.util.List<? extends RexNode> exps, java.util.List<java.lang.String> fieldNames, java.lang.String prefix, java.util.List<Pair<RexNode,java.lang.String>> flattenedExps)
-
flattenProjection
private void flattenProjection(RelStructuredTypeFlattener.RewriteRexShuttle shuttle, RexNode exp, java.lang.String fieldName, java.util.List<Pair<RexNode,java.lang.String>> flattenedExps)
-
flattenNullLiteral
private void flattenNullLiteral(RelDataType type, java.util.List<Pair<RexNode,java.lang.String>> flattenedExps)
-
isConstructor
private boolean isConstructor(RexNode rexNode)
-
rewriteRel
public void rewriteRel(TableScan rel)
-
rewriteRel
public void rewriteRel(LogicalDelta rel)
-
rewriteRel
public void rewriteRel(LogicalChi rel)
-
rewriteRel
public void rewriteRel(LogicalMatch rel)
-
flattenInputs
private void flattenInputs(java.util.List<RelDataTypeField> fieldList, RexNode prefix, java.util.List<Pair<RexNode,java.lang.String>> flattenedExpList)
Generates expressions that reference the flattened input fields from a given row type.
-
-