Class AggregateExpandDistinctAggregatesRule


  • public final class AggregateExpandDistinctAggregatesRule
    extends RelOptRule
    Planner rule that expands distinct aggregates (such as COUNT(DISTINCT x)) from a Aggregate.

    How this is done depends upon the arguments to the function. If all functions have the same argument (e.g. COUNT(DISTINCT x), SUM(DISTINCT x) both have the argument x) then one extra Aggregate is sufficient.

    If there are multiple arguments (e.g. COUNT(DISTINCT x), COUNT(DISTINCT y)) the rule creates separate Aggregates and combines using a Join.

    • Constructor Detail

      • AggregateExpandDistinctAggregatesRule

        public AggregateExpandDistinctAggregatesRule​(java.lang.Class<? extends Aggregate> clazz,
                                                     boolean useGroupingSets,
                                                     RelBuilderFactory relBuilderFactory)
      • AggregateExpandDistinctAggregatesRule

        @Deprecated
        public AggregateExpandDistinctAggregatesRule​(java.lang.Class<? extends LogicalAggregate> clazz,
                                                     boolean useGroupingSets,
                                                     RelFactories.JoinFactory joinFactory)
        Deprecated.
      • AggregateExpandDistinctAggregatesRule

        @Deprecated
        public AggregateExpandDistinctAggregatesRule​(java.lang.Class<? extends LogicalAggregate> clazz,
                                                     RelFactories.JoinFactory joinFactory)
        Deprecated.
    • Method Detail

      • convertSingletonDistinct

        private RelBuilder convertSingletonDistinct​(RelBuilder relBuilder,
                                                    Aggregate aggregate,
                                                    java.util.Set<Pair<java.util.List<java.lang.Integer>,​java.lang.Integer>> argLists)
        Converts an aggregate with one distinct aggregate and one or more non-distinct aggregates to multi-phase aggregates (see reference example below).
        Parameters:
        relBuilder - Contains the input relational expression
        aggregate - Original aggregate
        argLists - Arguments and filters to the distinct aggregate function
      • remap

        private static java.util.List<java.lang.Integer> remap​(ImmutableBitSet groupSet,
                                                               java.util.List<java.lang.Integer> argList)
      • convertMonopole

        private RelBuilder convertMonopole​(RelBuilder relBuilder,
                                           Aggregate aggregate,
                                           java.util.List<java.lang.Integer> argList,
                                           int filterArg)
        Converts an aggregate relational expression that contains just one distinct aggregate function (or perhaps several over the same arguments) and no non-distinct aggregate functions.
      • doRewrite

        private void doRewrite​(RelBuilder relBuilder,
                               Aggregate aggregate,
                               int n,
                               java.util.List<java.lang.Integer> argList,
                               int filterArg,
                               java.util.List<RexInputRef> refs)
        Converts all distinct aggregate calls to a given set of arguments.

        This method is called several times, one for each set of arguments. Each time it is called, it generates a JOIN to a new SELECT DISTINCT relational expression, and modifies the set of top-level calls.

        Parameters:
        aggregate - Original aggregate
        n - Ordinal of this in a join. relBuilder contains the input relational expression (either the original aggregate, the output from the previous call to this method. n is 0 if we're converting the first distinct aggregate in a query with no non-distinct aggregates)
        argList - Arguments to the distinct aggregate function
        filterArg - Argument that filters input to aggregate function, or -1
        refs - Array of expressions which will be the projected by the result of this rule. Those relating to this arg list will be modified @return Relational expression
      • rewriteAggCalls

        private static void rewriteAggCalls​(java.util.List<AggregateCall> newAggCalls,
                                            java.util.List<java.lang.Integer> argList,
                                            java.util.Map<java.lang.Integer,​java.lang.Integer> sourceOf)
      • createSelectDistinct

        private RelBuilder createSelectDistinct​(RelBuilder relBuilder,
                                                Aggregate aggregate,
                                                java.util.List<java.lang.Integer> argList,
                                                int filterArg,
                                                java.util.Map<java.lang.Integer,​java.lang.Integer> sourceOf)
        Given an Aggregate and the ordinals of the arguments to a particular call to an aggregate function, creates a 'select distinct' relational expression which projects the group columns and those arguments but nothing else.

        For example, given

        select f0, count(distinct f1), count(distinct f2)
         from t group by f0

        and the argument list

        {2}

        returns

        select distinct f0, f2 from t

        The sourceOf map is populated with the source of each column; in this case sourceOf.get(0) = 0, and sourceOf.get(1) = 2.

        Parameters:
        relBuilder - Relational expression builder
        aggregate - Aggregate relational expression
        argList - Ordinals of columns to make distinct
        filterArg - Ordinal of column to filter on, or -1
        sourceOf - Out parameter, is populated with a map of where each output field came from
        Returns:
        Aggregate relational expression which projects the required columns