Class RelDecorrelator

  • All Implemented Interfaces:
    ReflectiveVisitor

    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.

    TODO:

    • 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

        @Deprecated
        public static RelNode decorrelateQuery​(RelNode rootRel)
        Deprecated.
      • decorrelateQuery

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

        This is the main entry point to RelDecorrelator.

        Parameters:
        rootRel - Root node of the query
        relBuilder - Builder for relational expressions
        Returns:
        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.
        Parameters:
        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.
        Parameters:
        correlations - correlated variables to generate
        valueGenFieldOffset - offset in the output that generated columns will start
        corDefOutputs - output positions for the correlated variables generated
        Returns:
        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.
        Parameters:
        join - Join
        project - Original project as the right-hand input of the join
        nullIndicatorPos - Position of null indicator
        Returns:
        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.
        Parameters:
        correlate - Correlate
        project - the original project as the RHS input of the join
        isCount - Positions which are calls to the COUNT aggregation function
        Returns:
        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.
        Parameters:
        correlate - Correlate
        project - The original Project as the RHS input of the join
        filter - Filter
        correlatedJoinKeys - Correlated join keys
        Returns:
        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
        Parameters:
        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.
        Parameters:
        input - Input relational expression
        additionalExprs - Additional expressions and names
        Returns:
        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)