This is an automated email from the ASF dual-hosted git repository.

yiguolei pushed a commit to branch branch-4.1
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-4.1 by this push:
     new 5e73a7cda8b branch-4.1:[fix](topn_to_max)Remove the topnToMax 
optimizer rewrite that converted TOPN into MAX.#63519 (#64367)
5e73a7cda8b is described below

commit 5e73a7cda8b73c67e41065de027c3f935342e02c
Author: starocean999 <[email protected]>
AuthorDate: Mon Jun 15 11:54:27 2026 +0800

    branch-4.1:[fix](topn_to_max)Remove the topnToMax optimizer rewrite that 
converted TOPN into MAX.#63519 (#64367)
    
    pick https://github.com/apache/doris/pull/63519
    
    ### What problem does this PR solve?
    
    Issue Number: close #xxx
    
    Related PR: #xxx
    
    Problem Summary:
    
    ### Release note
    
    None
    
    ### Check List (For Author)
    
    - Test <!-- At least one of them must be included. -->
        - [ ] Regression test
        - [ ] Unit Test
        - [ ] Manual test (add detailed scripts or steps below)
        - [ ] No need to test or manual test. Explain why:
    - [ ] This is a refactor/code format and no logic has been changed.
            - [ ] Previous test can cover this change.
            - [ ] No code files have been changed.
            - [ ] Other reason <!-- Add your reason?  -->
    
    - Behavior changed:
        - [ ] No.
        - [ ] Yes. <!-- Explain the behavior change -->
    
    - Does this need documentation?
        - [ ] No.
    - [ ] Yes. <!-- Add document PR link here. eg:
    https://github.com/apache/doris-website/pull/1214 -->
    
    ### Check List (For Reviewer who merge this PR)
    
    - [ ] Confirm the release note
    - [ ] Confirm test cases
    - [ ] Confirm document
    - [ ] Add branch pick label <!-- Add branch pick label that this PR
    should merge into -->
---
 .../rules/expression/ExpressionOptimization.java   |  2 -
 .../nereids/rules/expression/rules/TopnToMax.java  | 56 ----------------------
 .../rules/expression/rules/TopnToMaxTest.java      | 42 ----------------
 .../data/nereids_p0/expression/topn_to_max.out     |  8 ----
 .../nereids_p0/expression/topn_to_max.groovy       | 49 -------------------
 5 files changed, 157 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/ExpressionOptimization.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/ExpressionOptimization.java
index a75bc0ecce1..ca06e93f453 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/ExpressionOptimization.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/ExpressionOptimization.java
@@ -36,7 +36,6 @@ import 
org.apache.doris.nereids.rules.expression.rules.SimplifyInPredicate;
 import org.apache.doris.nereids.rules.expression.rules.SimplifyRange;
 import org.apache.doris.nereids.rules.expression.rules.SimplifySelfComparison;
 import 
org.apache.doris.nereids.rules.expression.rules.SimplifyTimeFieldFromUnixtime;
-import org.apache.doris.nereids.rules.expression.rules.TopnToMax;
 
 import com.google.common.collect.ImmutableList;
 
@@ -68,7 +67,6 @@ public class ExpressionOptimization extends ExpressionRewrite 
{
                     CaseWhenToIf.INSTANCE,
                     CaseWhenToCompoundPredicate.INSTANCE,
                     PushIntoCaseWhenBranch.INSTANCE,
-                    TopnToMax.INSTANCE,
                     NullSafeEqualToEqual.INSTANCE,
                     LikeToEqualRewrite.INSTANCE,
                     BetweenToEqual.INSTANCE
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/TopnToMax.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/TopnToMax.java
deleted file mode 100644
index 972018b4244..00000000000
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/TopnToMax.java
+++ /dev/null
@@ -1,56 +0,0 @@
-// 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.expression.rules;
-
-import org.apache.doris.nereids.rules.expression.ExpressionPatternMatcher;
-import org.apache.doris.nereids.rules.expression.ExpressionPatternRuleFactory;
-import org.apache.doris.nereids.rules.expression.ExpressionRuleType;
-import org.apache.doris.nereids.trees.expressions.Expression;
-import org.apache.doris.nereids.trees.expressions.functions.agg.Max;
-import org.apache.doris.nereids.trees.expressions.functions.agg.TopN;
-import org.apache.doris.nereids.trees.expressions.literal.IntegerLikeLiteral;
-
-import com.google.common.collect.ImmutableList;
-
-import java.util.List;
-
-/**
- * Convert topn(x, 1) to max(x)
- */
-public class TopnToMax implements ExpressionPatternRuleFactory {
-
-    public static final TopnToMax INSTANCE = new TopnToMax();
-
-    @Override
-    public List<ExpressionPatternMatcher<? extends Expression>> buildRules() {
-        return ImmutableList.of(
-                matchesTopType(TopN.class).then(TopnToMax::rewrite)
-                        .toRule(ExpressionRuleType.TOPN_TO_MAX)
-        );
-    }
-
-    /** rewrite */
-    public static Expression rewrite(TopN topN) {
-        if (topN.arity() == 2 && topN.child(1) instanceof IntegerLikeLiteral
-                && ((IntegerLikeLiteral) topN.child(1)).getIntValue() == 1) {
-            return new Max(topN.child(0));
-        } else {
-            return topN;
-        }
-    }
-}
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/rules/TopnToMaxTest.java
 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/rules/TopnToMaxTest.java
deleted file mode 100644
index c0595136614..00000000000
--- 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/rules/TopnToMaxTest.java
+++ /dev/null
@@ -1,42 +0,0 @@
-// 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.expression.rules;
-
-import org.apache.doris.nereids.rules.expression.ExpressionRewriteTestHelper;
-import org.apache.doris.nereids.rules.expression.ExpressionRuleExecutor;
-import org.apache.doris.nereids.trees.expressions.Slot;
-import org.apache.doris.nereids.trees.expressions.SlotReference;
-import org.apache.doris.nereids.trees.expressions.functions.agg.Max;
-import org.apache.doris.nereids.trees.expressions.functions.agg.TopN;
-import org.apache.doris.nereids.trees.expressions.literal.Literal;
-import org.apache.doris.nereids.types.StringType;
-
-import com.google.common.collect.ImmutableList;
-import org.junit.jupiter.api.Test;
-
-class TopnToMaxTest extends ExpressionRewriteTestHelper {
-    @Test
-    void testSimplifyComparisonPredicateRule() {
-        executor = new ExpressionRuleExecutor(ImmutableList.of(
-                bottomUp(TopnToMax.INSTANCE)
-        ));
-
-        Slot slot = new SlotReference("a", StringType.INSTANCE);
-        assertRewrite(new TopN(slot, Literal.of(1)), new Max(slot));
-    }
-}
diff --git a/regression-test/data/nereids_p0/expression/topn_to_max.out 
b/regression-test/data/nereids_p0/expression/topn_to_max.out
deleted file mode 100644
index 6c8d190500a..00000000000
--- a/regression-test/data/nereids_p0/expression/topn_to_max.out
+++ /dev/null
@@ -1,8 +0,0 @@
--- This file is automatically generated. You should know what you did if you 
want to edit this
--- !sql --
-1      1
-2      2
-
--- !sql --
-2
-
diff --git a/regression-test/suites/nereids_p0/expression/topn_to_max.groovy 
b/regression-test/suites/nereids_p0/expression/topn_to_max.groovy
deleted file mode 100644
index 83fb9cc8492..00000000000
--- a/regression-test/suites/nereids_p0/expression/topn_to_max.groovy
+++ /dev/null
@@ -1,49 +0,0 @@
-// 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.
-
-suite("test_topn_to_max") {
-    sql 'set enable_nereids_planner=true'
-    sql 'set enable_fallback_to_original_planner=false'
-
-    sql 'drop table if exists test_topn_to_max;'
-
-    sql '''create table test_topn_to_max (k1 int, k2 string) distributed by 
hash(k1) buckets 3 properties('replication_num' = '1');'''
-    sql '''insert into test_topn_to_max values (1, "1"),  (2, "2");'''
-
-
-    order_qt_sql '''
-    select k1, topn(k2, 1)
-    from test_topn_to_max
-    group by k1;
-    '''
-    def res = sql '''
-    explain rewritten plan select k1, topn(k2, 1)
-    from test_topn_to_max
-    group by k1;
-    '''
-    assertTrue(res.toString().contains("max"), res.toString() + " should 
contain max")
-
-    order_qt_sql '''
-    select topn(k2, 1)
-    from test_topn_to_max;
-    '''
-    def res1 = sql '''
-    explain rewritten plan select topn(k2, 1)
-    from test_topn_to_max;
-    '''
-    assertTrue(res1.toString().contains("max"), res1.toString() + " should 
contain max")
-}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to