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

starocean999 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


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

commit 5667e9de714cd916e07336e68296f009c39bd318
Author: starocean999 <[email protected]>
AuthorDate: Mon May 25 16:54:55 2026 +0800

    [fix](topn_to_max)Remove the topnToMax optimizer rewrite that converted 
TOPN into MAX. (#63519)
    
    TOPN and MAX no longer have equivalent semantics, so the transformation
    is unsafe and can produce incorrect results.
---
 .../rules/expression/ExpressionOptimization.java   |  2 -
 .../nereids/rules/expression/rules/TopnToMax.java  | 56 ----------------------
 .../rules/expression/rules/TopnToMaxTest.java      | 42 ----------------
 .../data/query_p0/expression/topn_to_max.out       |  8 ----
 .../suites/query_p0/expression/topn_to_max.groovy  | 47 ------------------
 5 files changed, 155 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 fadad654539..ca7cdb6aac8 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
@@ -37,7 +37,6 @@ 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.StringEmptyToLengthRule;
-import org.apache.doris.nereids.rules.expression.rules.TopnToMax;
 
 import com.google.common.collect.ImmutableList;
 
@@ -69,7 +68,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/query_p0/expression/topn_to_max.out 
b/regression-test/data/query_p0/expression/topn_to_max.out
deleted file mode 100644
index 6c8d190500a..00000000000
--- a/regression-test/data/query_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/query_p0/expression/topn_to_max.groovy 
b/regression-test/suites/query_p0/expression/topn_to_max.groovy
deleted file mode 100644
index 4873be66334..00000000000
--- a/regression-test/suites/query_p0/expression/topn_to_max.groovy
+++ /dev/null
@@ -1,47 +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 '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