Class 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 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:

    
     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 like

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

      • restructure

        private final boolean restructure
      • oldToNewRelMap

        private final java.util.Map<RelNode,​RelNode> oldToNewRelMap
      • currentRel

        private RelNode currentRel
      • iRestructureInput

        private int iRestructureInput
      • flattenedRootType

        private RelDataType flattenedRootType
      • restructured

        boolean restructured
    • Method Detail

      • updateRelInMap

        public void updateRelInMap​(com.google.common.collect.SortedSetMultimap<RelNode,​CorrelationId> mapRefRelToCorVar)
      • restructureFields

        private java.util.List<RexNode> restructureFields​(RelDataType structuredType)
      • setNewForOldRel

        protected void setNewForOldRel​(RelNode oldRel,
                                       RelNode newRel)
      • getNewForOldRel

        protected RelNode getNewForOldRel​(RelNode oldRel)
      • 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​(Sort rel)
      • rewriteRel

        public void rewriteRel​(LogicalJoin rel)
      • rewriteRel

        public void rewriteRel​(Collect rel)
      • rewriteRel

        public void rewriteRel​(Uncollect rel)
      • rewriteRel

        public void rewriteRel​(Sample rel)
      • rewriteRel

        public void rewriteRel​(LogicalCalc rel)
      • rewriteGeneric

        public void rewriteGeneric​(RelNode rel)
      • 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​(LogicalChi 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.