Uploaded image for project: 'Calcite'
  1. Calcite
  2. CALCITE-2624

Add a rule to copy a sort below a join operator

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.17.0
    • 1.21.0
    • core

    Description

      Currently, the only rule that allows a sort to traverse a binary operator is the SortJoinTransposeRule. The rule was introduced mainly to push limits in the case of left and right outer joins (see CALCITE-831).

      I assume that the main reason that we don't have more rules is that sorts with limits and offsets cannot be pushed safely below many types of join operators. However, in many cases, it is possible and beneficial for optimization purposes to just push the sort without the limit and offset. Since we do not know in advance if the join operator preserves the order we cannot remove (that is why I am saying copy and not transpose) the sort operator on top of the join. The latter is not really a problem since the SortRemoveRule can detect such cases and remove the sort if it is redundant.

      A few concrete examples where this optimization makes sense are outlined below:

      • allow the sort to be later absorbed by an index scan and disappear from the plan (Sort + Tablescan => IndexScan with RelCollation);
      • allow operators that require sorted inputs to be exploited more easily (e.g., merge join);
      • allow the sort to be performed on a possibly smaller result (assuming that the physical binary operator that is going to be used preserves the order of left/right input and the top sort operator can be removed entirely).

      I propose to add a new rule (e.g., SortCopyBelowJoinRule, SortJoinCopyBelowRule) which allows a sort to be copied to the left or right (or to both if it is rather easy to decompose the sort) of a join operator (excluding the limit and offset attributes) if the respective inputs are not already sorted.

      Attachments

        Issue Links

          Activity

            People

              khawlamhb Khawla Mouhoubi
              zabetak Stamatis Zampetakis
              Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 1h 20m
                  1h 20m