Class TopDownGeneralDecorrelator

java.lang.Object
org.apache.calcite.sql2rel.TopDownGeneralDecorrelator
All Implemented Interfaces:
ReflectiveVisitor

public class TopDownGeneralDecorrelator extends Object implements ReflectiveVisitor
A top‑down, generic decorrelation algorithm that can handle deep nestings of correlated subqueries and that generalizes to complex query constructs. More details are in paper: Improving Unnesting of Complex Queries. It's an improved version of the paper: Unnesting Arbitrary Queries.

Usage notes for TopDownGeneralDecorrelator:

TopDownGeneralDecorrelator is not yet integrated into other modules and needs to be called separately. If you want to use it to replace RelDecorrelator, we recommend:

  1. When generating the initial plan by SqlToRelConverter, do not remove subqueries and do not enable decorrelation.
  2. Build a HepPlanner and apply rules for removing subqueries to the initial plan. With subqueries removed correctly, TopDownGeneralDecorrelator can in theory eliminate all correlation. We recommend using CoreRules.FILTER_SUB_QUERY_TO_MARK_CORRELATE and CoreRules.PROJECT_SUB_QUERY_TO_MARK_CORRELATE to remove subqueries from Filter and Project. These rules produce LEFT MARK Join/Correlate which are better suited for TopDownGeneralDecorrelator. There is not yet a corresponding, specially tailored rule for Join; you may choose to use CoreRules.JOIN_SUB_QUERY_TO_CORRELATE. Alternatively, for greater stability, you can run TopDownGeneralDecorrelator first and then apply CoreRules.JOIN_SUB_QUERY_TO_CORRELATE together with RelDecorrelator.
  3. Call decorrelateQuery(RelNode, RelBuilder) to obtain the decorrelated plan.
  4. Continue with other optimizations.

See org.apache.calcite.test.RelOptRulesTest#testTopDownGeneralDecorrelateForFilterExists() and org.apache.calcite.test.RelOptFixture#checkPlanning(boolean) for working examples.

  • Method Details

    • createEmptyDecorrelator

      public static TopDownGeneralDecorrelator createEmptyDecorrelator(RelBuilder builder)
    • decorrelateQuery

      public static RelNode decorrelateQuery(RelNode rel, RelBuilder builder)
      Decorrelates a query. This is the entry point for this class.
      Parameters:
      rel - Root node of the query
      builder - RelBuilder
      Returns:
      Equivalent node without correlation
    • unnestInternal

      public RelNode unnestInternal(Filter filter, boolean allowEmptyOutputFromRewrite)
    • unnestInternal

      public RelNode unnestInternal(Project project, boolean allowEmptyOutputFromRewrite)
    • unnestInternal

      public RelNode unnestInternal(Aggregate aggregate, boolean allowEmptyOutputFromRewrite)
    • unnestInternal

      public RelNode unnestInternal(Sort sort, boolean allowEmptyOutputFromRewrite)
    • unnestInternal

      public RelNode unnestInternal(Correlate correlate, boolean allowEmptyOutputFromRewrite)
    • unnestInternal

      public RelNode unnestInternal(Join join, boolean allowEmptyOutputFromRewrite)
    • unnestInternal

      public RelNode unnestInternal(SetOp setOp, boolean allowEmptyOutputFromRewrite)
    • unnestInternal

      public RelNode unnestInternal(RelNode other, boolean allowEmptyOutputFromRewrite)
    • getVisitor

      public TopDownGeneralDecorrelator getVisitor()