DistinctWindowExpression.java

// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

package org.apache.doris.nereids.rules.rewrite;

import org.apache.doris.nereids.exceptions.AnalysisException;
import org.apache.doris.nereids.rules.Rule;
import org.apache.doris.nereids.rules.RuleType;
import org.apache.doris.nereids.trees.expressions.Alias;
import org.apache.doris.nereids.trees.expressions.NamedExpression;
import org.apache.doris.nereids.trees.expressions.WindowExpression;
import org.apache.doris.nereids.trees.expressions.functions.agg.AggregateFunction;
import org.apache.doris.nereids.trees.expressions.functions.agg.Count;
import org.apache.doris.nereids.trees.expressions.functions.agg.SupportMultiDistinct;
import org.apache.doris.nereids.trees.plans.Plan;
import org.apache.doris.nereids.trees.plans.logical.LogicalWindow;

import com.google.common.collect.Lists;

import java.util.List;
import java.util.Optional;

/**
 * count(distinct A) over(...)
 * =>
 * multi_distinct_count(A) over(...)
 *
 * The same rewrite applies to every distinct aggregate that implements SupportMultiDistinct
 * (count, sum, group_concat, array_agg, collect_list, ...).
 */

public class DistinctWindowExpression extends OneRewriteRuleFactory {
    @Override
    public Rule build() {
        return logicalWindow()
                .then(this::rewrite)
                .toRule(RuleType.DISTINCT_WINDOW_EXPRESSION);
    }

    private Plan rewrite(LogicalWindow<Plan> window) {
        List<NamedExpression> newWindowExpressions = Lists.newArrayList();
        boolean windExprChanged = false;
        for (NamedExpression expr : window.getWindowExpressions()) {
            boolean converted = false;
            if (expr instanceof Alias) {
                Alias alias = (Alias) expr;
                if (alias.child() instanceof WindowExpression) {
                    WindowExpression windowExpression = (WindowExpression) expr.child(0);
                    if (windowExpression.getFunction() instanceof AggregateFunction) {
                        AggregateFunction aggregateFunction = (AggregateFunction) windowExpression.getFunction();
                        Optional<AggregateFunction> multiDistinct = convertToMultiDistinctFunction(aggregateFunction);
                        if (multiDistinct.isPresent()) {
                            converted = true;
                            windExprChanged = true;
                            Alias newAlias = (Alias) alias.withChildren(
                                    windowExpression.withFunction(multiDistinct.get()));
                            newWindowExpressions.add(newAlias);
                        }
                    }
                }
            }
            if (!converted) {
                newWindowExpressions.add(expr);
            }
        }
        if (windExprChanged) {
            return window.withExpressionsAndChild(newWindowExpressions, window.child());
        }
        return null;
    }

    private Optional<AggregateFunction> convertToMultiDistinctFunction(AggregateFunction func) {
        if (func.isDistinct() && func instanceof SupportMultiDistinct) {
            if (func instanceof Count && func.arity() != 1) {
                throw new AnalysisException("COUNT with DISTINCT only support 1 parameter in analytic function");
            }
            return Optional.of(((SupportMultiDistinct) func).convertToMultiDistinct());
        }
        return Optional.empty();
    }
}