EliminateUnnecessaryProject.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.annotation.DependsRules;
import org.apache.doris.nereids.jobs.JobContext;
import org.apache.doris.nereids.trees.expressions.NamedExpression;
import org.apache.doris.nereids.trees.expressions.SlotReference;
import org.apache.doris.nereids.trees.expressions.StatementScopeIdGenerator;
import org.apache.doris.nereids.trees.plans.Plan;
import org.apache.doris.nereids.trees.plans.algebra.Project;
import org.apache.doris.nereids.trees.plans.logical.LogicalEmptyRelation;
import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
import org.apache.doris.nereids.trees.plans.visitor.CustomRewriter;

import com.google.common.collect.ImmutableList;

/**
 * remove the project that output same with its child to avoid we get two consecutive projects in best plan.
 * for more information, please see <a href="https://github.com/apache/doris/pull/13886">this PR</a>
 */
@DependsRules(ColumnPruning.class)
public class EliminateUnnecessaryProject implements CustomRewriter {

    @Override
    public Plan rewriteRoot(Plan plan, JobContext jobContext) {
        if (!plan.containsType(Project.class)) {
            return plan;
        }
        return rewrite(plan);
    }

    private Plan rewrite(Plan plan) {
        if (plan instanceof LogicalProject) {
            return rewriteProject((LogicalProject<?>) plan);
        } else {
            return rewriteChildren(plan);
        }
    }

    private Plan rewriteProject(LogicalProject<?> project) {
        if (project.child() instanceof LogicalEmptyRelation) {
            // eliminate unnecessary project
            return new LogicalEmptyRelation(StatementScopeIdGenerator.newRelationId(), project.getProjects());
        } else if (isPassThroughProject(project)) {
            // eliminate unnecessary project
            return rewrite(project.child());
        } else {
            return rewriteChildren(project);
        }
    }

    /**
     * ������ project ��������������������������������������������������������� project ���������������������
     *
     * <p>��������������������� ExprId���ConstantPropagation ��� slot ������������������������������������ slot ��� ExprId
     * ������ ConstantPropagation#visitLogicalAggregate ������
     * {@code new Alias(expr.getExprId(), replacedExpr, expr.getName())}������������������������
     * ��������� ExprId ��������������������������������������������������������������� project��������� RQG case 11000258898���
     *
     * <pre>
     * BEFORE:
     * LogicalResultSink[61] ( outputExprs=[2004-04-28#55, 2014-07-07#56] )
     * +--LogicalLimit ( limit=1, offset=0, phase=GLOBAL )
     *    +--LogicalProject[260] ( projects=['2011-11-15 13:33:05.000' AS `2004-04-28`#55,
     *                                       '2011-11-15 13:33:05.000' AS `2014-07-07`#56] )  <- ��������������������������������� slot
     *       +--LogicalIntersect ( qualifier=DISTINCT, outputs=[2004-04-28#55, 2014-07-07#56], ... )
     *                                                                     ^ ��������������� ExprId ������ {55, 56}
     * AFTER:  (��������������� OPERATIVE_COLUMN_DERIVE ��� BEFORE ��������� Project ������������)
     * LogicalResultSink[61] ( outputExprs=[2004-04-28#55, 2014-07-07#56] )
     * +--LogicalLimit ( limit=1, offset=0, phase=GLOBAL )
     *    +--LogicalIntersect ( qualifier=DISTINCT, outputs=[2004-04-28#55, 2014-07-07#56], ... )
     * </pre>
     *
     * <p>������ project ��������� ExprId ������ {55,56} ������������ Intersect ��������� ExprId ������ {55,56}���
     * ��������� ExprId ��������������������������������������������������� project ��� #55 ������������������
     * ���cast('2011-11-15 13:33:05.000' as TEXT)���NOT NULL������������ Intersect ��� #55 ������ schema ���
     * nullable������������������������ nullable ��� col_datetime_3__undef_signed���������������������������������������
     * Intersect ��� nullable slot��������������������� rewrite ������������������ NOT NULL ������������������ AdjustNullable
     * ������������������������
     * {@code AdjustNullable convert slot 2004-04-28#55 from not-nullable to nullable}���
     *
     * <p>������������������ project ������������������������������ slot ������������SlotReference���������������������������������������
     * ������������������������
     */
    private static boolean isPassThroughProject(LogicalProject<?> project) {
        for (NamedExpression namedExpression : project.getProjects()) {
            if (!(namedExpression instanceof SlotReference)) {
                return false;
            }
        }
        return project.getOutputExprIdBitSet().equals(project.getChildrenOutputExprIdBitSet());
    }

    private Plan rewriteChildren(Plan plan) {
        ImmutableList.Builder<Plan> newChildren = ImmutableList.builderWithExpectedSize(plan.arity());
        boolean hasNewChildren = false;
        for (Plan child : plan.children()) {
            Plan newChild = rewrite(child);
            if (newChild != child) {
                hasNewChildren = true;
            }
            newChildren.add(newChild);
        }
        return hasNewChildren ? plan.withChildren(newChildren.build()) : plan;
    }
}