Class RelDecorrelator

  • All Implemented Interfaces:

    public class RelDecorrelator
    extends java.lang.Object
    implements ReflectiveVisitor
    RelDecorrelator replaces all correlated expressions (corExp) in a relational expression (RelNode) tree with non-correlated expressions that are produced from joining the RelNode that produces the corExp with the RelNode that references it.


    • replace CorelMap constructor parameter with a RelNode
    • make currentRel immutable (would require a fresh RelDecorrelator for each node being decorrelated)
    • make fields of CorelMap immutable
    • make sub-class rules static, and have them create their own de-correlator
    • Method Detail

      • decorrelateQuery

        public static RelNode decorrelateQuery​(RelNode rootRel)
      • decorrelateQuery

        public static RelNode decorrelateQuery​(RelNode rootRel,
                                               RelBuilder relBuilder)
        Decorrelates a query.

        This is the main entry point to RelDecorrelator.

        rootRel - Root node of the query
        relBuilder - Builder for relational expressions
        Equivalent query with all LogicalCorrelate instances removed
      • removeCorrelationViaRule

        public RelNode removeCorrelationViaRule​(RelNode root)
      • removeCorrelationExpr

        protected RexNode removeCorrelationExpr​(RexNode exp,
                                                boolean projectPulledAboveLeftCorrelator)
      • removeCorrelationExpr

        protected RexNode removeCorrelationExpr​(RexNode exp,
                                                boolean projectPulledAboveLeftCorrelator,
                                                RexInputRef nullIndicator)
      • removeCorrelationExpr

        protected RexNode removeCorrelationExpr​(RexNode exp,
                                                boolean projectPulledAboveLeftCorrelator,
                                                java.util.Set<java.lang.Integer> isCount)
      • decorrelateRel

        public RelDecorrelator.Frame decorrelateRel​(Sort rel)
        Rewrite Sort.
        rel - Sort to be rewritten
      • projectedLiteral

        private static RexLiteral projectedLiteral​(RelNode rel,
                                                   int i)
        Returns a literal output field, or null if it is not literal.
      • createValueGenerator

        private RelNode createValueGenerator​(java.lang.Iterable<RelDecorrelator.CorRef> correlations,
                                             int valueGenFieldOffset,
                                             java.util.SortedMap<RelDecorrelator.CorDef,​java.lang.Integer> corDefOutputs)
        Create RelNode tree that produces a list of correlated variables.
        correlations - correlated variables to generate
        valueGenFieldOffset - offset in the output that generated columns will start
        corDefOutputs - output positions for the correlated variables generated
        RelNode the root of the resultant RelNode tree
      • maybeAddValueGenerator

        private RelDecorrelator.Frame maybeAddValueGenerator​(RelNode rel,
                                                             RelDecorrelator.Frame frame)
        Adds a value generator to satisfy the correlating variables used by a relational expression, if those variables are not already provided by its input.
      • isWidening

        private boolean isWidening​(RelDataType type,
                                   RelDataType type1)
        Returns whether one type is just a widening of another.

        For example:

        • VARCHAR(10) is a widening of VARCHAR(5).
        • VARCHAR(10) is a widening of VARCHAR(10) NOT NULL.
      • projectJoinOutputWithNullability

        private RelNode projectJoinOutputWithNullability​(LogicalJoin join,
                                                         LogicalProject project,
                                                         int nullIndicatorPos)
        Pulls project above the join from its RHS input. Enforces nullability for join output.
        join - Join
        project - Original project as the right-hand input of the join
        nullIndicatorPos - Position of null indicator
        the subtree with the new Project at the root
      • aggregateCorrelatorOutput

        private RelNode aggregateCorrelatorOutput​(Correlate correlate,
                                                  LogicalProject project,
                                                  java.util.Set<java.lang.Integer> isCount)
        Pulls a Project above a Correlate from its RHS input. Enforces nullability for join output.
        correlate - Correlate
        project - the original project as the RHS input of the join
        isCount - Positions which are calls to the COUNT aggregation function
        the subtree with the new Project at the root
      • checkCorVars

        private boolean checkCorVars​(LogicalCorrelate correlate,
                                     LogicalProject project,
                                     LogicalFilter filter,
                                     java.util.List<RexFieldAccess> correlatedJoinKeys)
        Checks whether the correlations in projRel and filter are related to the correlated variables provided by corRel.
        correlate - Correlate
        project - The original Project as the RHS input of the join
        filter - Filter
        correlatedJoinKeys - Correlated join keys
        true if filter and proj only references corVar provided by corRel
      • removeCorVarFromTree

        private void removeCorVarFromTree​(LogicalCorrelate correlate)
        Remove correlated variables from the tree at root corRel
        correlate - Correlate
      • createProjectWithAdditionalExprs

        private RelNode createProjectWithAdditionalExprs​(RelNode input,
                                                         java.util.List<Pair<RexNode,​java.lang.String>> additionalExprs)
        Projects all input output fields plus the additional expressions.
        input - Input relational expression
        additionalExprs - Additional expressions and names
        the new Project
      • identityMap

        static java.util.Map<java.lang.Integer,​java.lang.Integer> identityMap​(int count)
      • register

        RelDecorrelator.Frame register​(RelNode rel,
                                       RelNode newRel,
                                       java.util.Map<java.lang.Integer,​java.lang.Integer> oldToNewOutputs,
                                       java.util.SortedMap<RelDecorrelator.CorDef,​java.lang.Integer> corDefOutputs)
        Registers a relational expression and the relational expression it became after decorrelation.
      • allLessThan

        static boolean allLessThan​(java.util.Collection<java.lang.Integer> integers,
                                   int limit,
                                   Litmus ret)