Class RelMdPredicates

  • All Implemented Interfaces:
    org.apache.calcite.rel.metadata.MetadataHandler<org.apache.calcite.rel.metadata.BuiltInMetadata.Predicates>

    public class RelMdPredicates
    extends Object
    implements org.apache.calcite.rel.metadata.MetadataHandler<org.apache.calcite.rel.metadata.BuiltInMetadata.Predicates>
    Utility to infer Predicates that are applicable above a RelNode.

    The class was copied over because of * CALCITE-6317 * and should be removed after upgraded to calcite-1.37.0.

    FLINK modifications are at lines

    1. Port fix of CALCITE-6317 (Calcite 1.37.0): Lines 333~357, 402~404

    This is currently used by JoinPushTransitivePredicatesRule to infer Predicates that can be inferred from one side of a Join to the other.

    The PullUp Strategy is sound but not complete. Here are some of the limitations:

    1. For Aggregations we only PullUp predicates that only contain Grouping Keys. This can be extended to infer predicates on Aggregation expressions from expressions on the aggregated columns. For e.g.
       select a, max(b) from R1 where b > 7
         → max(b) > 7 or max(b) is null
       
    2. For Projections we only look at columns that are projected without any function applied. So:
       select a from R1 where a > 7
         → "a > 7" is pulled up from the Projection.
       select a + 1 from R1 where a + 1 > 7
         → "a + 1 gt; 7" is not pulled up
       
    3. There are several restrictions on Joins:
      • We only pullUp inferred predicates for now. Pulling up existing predicates causes an explosion of duplicates. The existing predicates are pushed back down as new predicates. Once we have rules to eliminate duplicate Filter conditions, we should pullUp all predicates.
      • For Left Outer: we infer new predicates from the left and set them as applicable on the Right side. No predicates are pulledUp.
      • Right Outer Joins are handled in an analogous manner.
      • For Full Outer Joins no predicates are pulledUp or inferred.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static org.apache.calcite.rel.metadata.RelMetadataProvider SOURCE  
    • Constructor Summary

      Constructors 
      Constructor Description
      RelMdPredicates()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      org.apache.calcite.rel.metadata.MetadataDef<org.apache.calcite.rel.metadata.BuiltInMetadata.Predicates> getDef()  
      org.apache.calcite.plan.RelOptPredicateList getPredicates​(org.apache.calcite.plan.volcano.RelSubset r, org.apache.calcite.rel.metadata.RelMetadataQuery mq)
      Returns the BuiltInMetadata.Predicates.getPredicates() statistic.
      org.apache.calcite.plan.RelOptPredicateList getPredicates​(org.apache.calcite.rel.core.Aggregate agg, org.apache.calcite.rel.metadata.RelMetadataQuery mq)
      Infers predicates for an Aggregate.
      org.apache.calcite.plan.RelOptPredicateList getPredicates​(org.apache.calcite.rel.core.Correlate correlate, org.apache.calcite.rel.metadata.RelMetadataQuery mq)
      Infers predicates for a correlate node.
      org.apache.calcite.plan.RelOptPredicateList getPredicates​(org.apache.calcite.rel.core.Exchange exchange, org.apache.calcite.rel.metadata.RelMetadataQuery mq)
      Infers predicates for an Exchange.
      org.apache.calcite.plan.RelOptPredicateList getPredicates​(org.apache.calcite.rel.core.Filter filter, org.apache.calcite.rel.metadata.RelMetadataQuery mq)
      Add the Filter condition to the pulledPredicates list from the input.
      org.apache.calcite.plan.RelOptPredicateList getPredicates​(org.apache.calcite.rel.core.Intersect intersect, org.apache.calcite.rel.metadata.RelMetadataQuery mq)
      Infers predicates for a Intersect.
      org.apache.calcite.plan.RelOptPredicateList getPredicates​(org.apache.calcite.rel.core.Join join, org.apache.calcite.rel.metadata.RelMetadataQuery mq)
      Infers predicates for a Join (including SemiJoin).
      org.apache.calcite.plan.RelOptPredicateList getPredicates​(org.apache.calcite.rel.core.Minus minus, org.apache.calcite.rel.metadata.RelMetadataQuery mq)
      Infers predicates for a Minus.
      org.apache.calcite.plan.RelOptPredicateList getPredicates​(org.apache.calcite.rel.core.Project project, org.apache.calcite.rel.metadata.RelMetadataQuery mq)
      Infers predicates for a project.
      org.apache.calcite.plan.RelOptPredicateList getPredicates​(org.apache.calcite.rel.core.Sort sort, org.apache.calcite.rel.metadata.RelMetadataQuery mq)
      Infers predicates for a Sort.
      org.apache.calcite.plan.RelOptPredicateList getPredicates​(org.apache.calcite.rel.core.TableModify tableModify, org.apache.calcite.rel.metadata.RelMetadataQuery mq)
      Infers predicates for a TableModify.
      org.apache.calcite.plan.RelOptPredicateList getPredicates​(org.apache.calcite.rel.core.TableScan scan, org.apache.calcite.rel.metadata.RelMetadataQuery mq)
      Infers predicates for a table scan.
      org.apache.calcite.plan.RelOptPredicateList getPredicates​(org.apache.calcite.rel.core.Union union, org.apache.calcite.rel.metadata.RelMetadataQuery mq)
      Infers predicates for a Union.
      org.apache.calcite.plan.RelOptPredicateList getPredicates​(org.apache.calcite.rel.RelNode rel, org.apache.calcite.rel.metadata.RelMetadataQuery mq)
      Catch-all implementation for BuiltInMetadata.Predicates.getPredicates(), invoked using reflection.
    • Field Detail

      • SOURCE

        public static final org.apache.calcite.rel.metadata.RelMetadataProvider SOURCE
    • Constructor Detail

      • RelMdPredicates

        public RelMdPredicates()
    • Method Detail

      • getDef

        public org.apache.calcite.rel.metadata.MetadataDef<org.apache.calcite.rel.metadata.BuiltInMetadata.Predicates> getDef()
        Specified by:
        getDef in interface org.apache.calcite.rel.metadata.MetadataHandler<org.apache.calcite.rel.metadata.BuiltInMetadata.Predicates>
      • getPredicates

        public org.apache.calcite.plan.RelOptPredicateList getPredicates​(org.apache.calcite.rel.RelNode rel,
                                                                         org.apache.calcite.rel.metadata.RelMetadataQuery mq)
        Catch-all implementation for BuiltInMetadata.Predicates.getPredicates(), invoked using reflection.
        See Also:
        RelMetadataQuery.getPulledUpPredicates(RelNode)
      • getPredicates

        public org.apache.calcite.plan.RelOptPredicateList getPredicates​(org.apache.calcite.rel.core.TableScan scan,
                                                                         org.apache.calcite.rel.metadata.RelMetadataQuery mq)
        Infers predicates for a table scan.
      • getPredicates

        public org.apache.calcite.plan.RelOptPredicateList getPredicates​(org.apache.calcite.rel.core.Project project,
                                                                         org.apache.calcite.rel.metadata.RelMetadataQuery mq)
        Infers predicates for a project.
        1. create a mapping from input to projection. Map only positions that directly reference an input column.
        2. Expressions that only contain above columns are retained in the Project's pullExpressions list.
        3. For e.g. expression 'a + e = 9' below will not be pulled up because 'e' is not in the projection list.
           inputPullUpExprs:      {a > 7, b + c < 10, a + e = 9}
           projectionExprs:       {a, b, c, e / 2}
           projectionPullupExprs: {a > 7, b + c < 10}
           
      • getPredicates

        public org.apache.calcite.plan.RelOptPredicateList getPredicates​(org.apache.calcite.rel.core.Correlate correlate,
                                                                         org.apache.calcite.rel.metadata.RelMetadataQuery mq)
        Infers predicates for a correlate node.
      • getPredicates

        public org.apache.calcite.plan.RelOptPredicateList getPredicates​(org.apache.calcite.rel.core.Filter filter,
                                                                         org.apache.calcite.rel.metadata.RelMetadataQuery mq)
        Add the Filter condition to the pulledPredicates list from the input.
      • getPredicates

        public org.apache.calcite.plan.RelOptPredicateList getPredicates​(org.apache.calcite.rel.core.Join join,
                                                                         org.apache.calcite.rel.metadata.RelMetadataQuery mq)
        Infers predicates for a Join (including SemiJoin).
      • getPredicates

        public org.apache.calcite.plan.RelOptPredicateList getPredicates​(org.apache.calcite.rel.core.Aggregate agg,
                                                                         org.apache.calcite.rel.metadata.RelMetadataQuery mq)
        Infers predicates for an Aggregate.

        Pulls up predicates that only contains references to columns in the GroupSet. For e.g.

         inputPullUpExprs : { a > 7, b + c < 10, a + e = 9}
         groupSet         : { a, b}
         pulledUpExprs    : { a > 7}
         
      • getPredicates

        public org.apache.calcite.plan.RelOptPredicateList getPredicates​(org.apache.calcite.rel.core.Union union,
                                                                         org.apache.calcite.rel.metadata.RelMetadataQuery mq)
        Infers predicates for a Union.
      • getPredicates

        public org.apache.calcite.plan.RelOptPredicateList getPredicates​(org.apache.calcite.rel.core.Intersect intersect,
                                                                         org.apache.calcite.rel.metadata.RelMetadataQuery mq)
        Infers predicates for a Intersect.
      • getPredicates

        public org.apache.calcite.plan.RelOptPredicateList getPredicates​(org.apache.calcite.rel.core.Minus minus,
                                                                         org.apache.calcite.rel.metadata.RelMetadataQuery mq)
        Infers predicates for a Minus.
      • getPredicates

        public org.apache.calcite.plan.RelOptPredicateList getPredicates​(org.apache.calcite.rel.core.Sort sort,
                                                                         org.apache.calcite.rel.metadata.RelMetadataQuery mq)
        Infers predicates for a Sort.
      • getPredicates

        public org.apache.calcite.plan.RelOptPredicateList getPredicates​(org.apache.calcite.rel.core.TableModify tableModify,
                                                                         org.apache.calcite.rel.metadata.RelMetadataQuery mq)
        Infers predicates for a TableModify.
      • getPredicates

        public org.apache.calcite.plan.RelOptPredicateList getPredicates​(org.apache.calcite.rel.core.Exchange exchange,
                                                                         org.apache.calcite.rel.metadata.RelMetadataQuery mq)
        Infers predicates for an Exchange.
      • getPredicates

        public org.apache.calcite.plan.RelOptPredicateList getPredicates​(org.apache.calcite.plan.volcano.RelSubset r,
                                                                         org.apache.calcite.rel.metadata.RelMetadataQuery mq)
        Returns the BuiltInMetadata.Predicates.getPredicates() statistic.
        See Also:
        RelMetadataQuery.getPulledUpPredicates(RelNode)