org.eigenbase.sql2rel
Class RelStructuredTypeFlattener

java.lang.Object
  extended by org.eigenbase.sql2rel.RelStructuredTypeFlattener
All Implemented Interfaces:
ReflectiveVisitor

public class RelStructuredTypeFlattener
extends 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 table T(c1 ST, c2 double), and a query select t.c2, t.c1.a2 from t. After SqlToRelConverter executes, the unflattened tree looks like:


 ProjectRel(C2=[$1], A2=[$0.A2])
   TableAccessRel(table=[T])
After flattening, the resulting tree looks like

 ProjectRel(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 like

 FtrsIndexScanRel(table=[T], index=[clustered], projection=[3, 2])

Version:
$Id: //open/dev/farrago/src/org/eigenbase/sql2rel/RelStructuredTypeFlattener.java#32 $
Author:
John V. Sichi

Nested Class Summary
private  class RelStructuredTypeFlattener.RewriteRelVisitor
           
private  class RelStructuredTypeFlattener.RewriteRexShuttle
           
static interface RelStructuredTypeFlattener.SelfFlatteningRel
           
 
Field Summary
private  RelNode currentRel
           
private  RelDataType flattenedRootType
           
private  int iRestructureInput
           
private  Map<RelNode,RelNode> oldToNewRelMap
           
(package private)  boolean restructured
           
private  RexBuilder rexBuilder
           
private  RelStructuredTypeFlattener.RewriteRelVisitor visitor
           
 
Constructor Summary
RelStructuredTypeFlattener(RexBuilder rexBuilder)
           
 
Method Summary
private  int calculateFlattenedOffset(RelDataType rowType, int ordinal)
           
protected  RexNode flattenFieldAccesses(RexNode exp)
           
private  void flattenNullLiteral(RelDataType type, List<RexNode> flattenedExps, List<String> flattenedFieldNames)
           
private  void flattenProjection(RexNode exp, String fieldName, List<RexNode> flattenedExps, List<String> flattenedFieldNames)
           
private  void flattenProjections(RexNode[] exps, String[] fieldNames, String prefix, List<RexNode> flattenedExps, List<String> flattenedFieldNames)
           
private  int getNewForOldInput(int oldOrdinal)
          Maps the ordinal of a field pre-flattening to the ordinal of the corresponding field post-flattening, and optionally returns its type.
protected  RelNode getNewForOldRel(RelNode oldRel)
           
private  boolean isConstructor(RexNode rexNode)
           
private  RexNode restructure(RelDataType structuredType)
           
private  RexNode[] restructureFields(RelDataType structuredType)
           
 RelNode rewrite(RelNode root, boolean restructure)
           
 void rewriteGeneric(RelNode rel)
           
 void rewriteRel(AggregateRel rel)
           
 void rewriteRel(CalcRel rel)
           
 void rewriteRel(CollectRel rel)
           
 void rewriteRel(CorrelatorRel rel)
           
 void rewriteRel(FilterRel rel)
           
 void rewriteRel(IntersectRel rel)
           
 void rewriteRel(JoinRel rel)
           
 void rewriteRel(MinusRel rel)
           
 void rewriteRel(OneRowRel rel)
           
 void rewriteRel(ProjectRel rel)
           
 void rewriteRel(RelStructuredTypeFlattener.SelfFlatteningRel rel)
           
 void rewriteRel(SamplingRel rel)
           
 void rewriteRel(SortRel rel)
           
 void rewriteRel(TableAccessRel rel)
           
 void rewriteRel(TableFunctionRel rel)
           
 void rewriteRel(TableModificationRel rel)
           
 void rewriteRel(UncollectRel rel)
           
 void rewriteRel(UnionRel rel)
           
 void rewriteRel(ValuesRel rel)
           
protected  void setNewForOldRel(RelNode oldRel, RelNode newRel)
           
 void updateRelInMap(Map<RelNode,SortedSet<CorrelatorRel.Correlation>> mapRefRelToCorVar)
           
 void updateRelInMap(SortedMap<CorrelatorRel.Correlation,CorrelatorRel> mapCorVarToCorRel)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

rexBuilder

private final RexBuilder rexBuilder

visitor

private final RelStructuredTypeFlattener.RewriteRelVisitor visitor

oldToNewRelMap

private Map<RelNode,RelNode> oldToNewRelMap

currentRel

private RelNode currentRel

iRestructureInput

private int iRestructureInput

flattenedRootType

private RelDataType flattenedRootType

restructured

boolean restructured
Constructor Detail

RelStructuredTypeFlattener

public RelStructuredTypeFlattener(RexBuilder rexBuilder)
Method Detail

updateRelInMap

public void updateRelInMap(Map<RelNode,SortedSet<CorrelatorRel.Correlation>> mapRefRelToCorVar)

updateRelInMap

public void updateRelInMap(SortedMap<CorrelatorRel.Correlation,CorrelatorRel> mapCorVarToCorRel)

rewrite

public RelNode rewrite(RelNode root,
                       boolean restructure)

restructureFields

private RexNode[] restructureFields(RelDataType structuredType)

restructure

private RexNode restructure(RelDataType structuredType)

setNewForOldRel

protected void setNewForOldRel(RelNode oldRel,
                               RelNode newRel)

getNewForOldRel

protected RelNode getNewForOldRel(RelNode oldRel)

getNewForOldInput

private int getNewForOldInput(int oldOrdinal)
Maps the ordinal of a field pre-flattening to the ordinal of the corresponding field post-flattening, and optionally returns its type.

Parameters:
oldOrdinal - Pre-flattening ordinal
Returns:
Post-flattening ordinal

calculateFlattenedOffset

private int calculateFlattenedOffset(RelDataType rowType,
                                     int ordinal)

flattenFieldAccesses

protected RexNode flattenFieldAccesses(RexNode exp)

rewriteRel

public void rewriteRel(TableModificationRel rel)

rewriteRel

public void rewriteRel(AggregateRel rel)

rewriteRel

public void rewriteRel(SortRel rel)

rewriteRel

public void rewriteRel(FilterRel rel)

rewriteRel

public void rewriteRel(JoinRel rel)

rewriteRel

public void rewriteRel(CorrelatorRel rel)

rewriteRel

public void rewriteRel(CollectRel rel)

rewriteRel

public void rewriteRel(UncollectRel rel)

rewriteRel

public void rewriteRel(IntersectRel rel)

rewriteRel

public void rewriteRel(MinusRel rel)

rewriteRel

public void rewriteRel(UnionRel rel)

rewriteRel

public void rewriteRel(OneRowRel rel)

rewriteRel

public void rewriteRel(ValuesRel rel)

rewriteRel

public void rewriteRel(TableFunctionRel rel)

rewriteRel

public void rewriteRel(SamplingRel rel)

rewriteRel

public void rewriteRel(ProjectRel rel)

rewriteRel

public void rewriteRel(CalcRel rel)

rewriteRel

public void rewriteRel(RelStructuredTypeFlattener.SelfFlatteningRel rel)

rewriteGeneric

public void rewriteGeneric(RelNode rel)

flattenProjections

private void flattenProjections(RexNode[] exps,
                                String[] fieldNames,
                                String prefix,
                                List<RexNode> flattenedExps,
                                List<String> flattenedFieldNames)

flattenProjection

private void flattenProjection(RexNode exp,
                               String fieldName,
                               List<RexNode> flattenedExps,
                               List<String> flattenedFieldNames)

flattenNullLiteral

private void flattenNullLiteral(RelDataType type,
                                List<RexNode> flattenedExps,
                                List<String> flattenedFieldNames)

isConstructor

private boolean isConstructor(RexNode rexNode)

rewriteRel

public void rewriteRel(TableAccessRel rel)