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

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


The following commit(s) were added to refs/heads/branch-4.2 by this push:
     new 94b6b3f548a branch-4.2: [fix](fe) Reject LARGEINT bucket of NTILE 
#68030 (#70042)
94b6b3f548a is described below

commit 94b6b3f548a1edf209376206d8a06069b5ec76d4
Author: morrySnow <[email protected]>
AuthorDate: Tue Nov 3 17:22:27 2026 +0800

    branch-4.2: [fix](fe) Reject LARGEINT bucket of NTILE #68030 (#70042)
    
    picked from master #68030
    
    Co-authored-by: TengJianPing <[email protected]>
---
 .../trees/expressions/functions/window/Ntile.java  | 26 ++++++------
 .../functions/window/NtileBucketBackportTest.java  | 49 ++++++++++++++++++++++
 .../window_functions/test_ntile_function.groovy    | 11 ++++-
 3 files changed, 71 insertions(+), 15 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/window/Ntile.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/window/Ntile.java
index e9ef3b19f5f..bbb915cdcb8 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/window/Ntile.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/window/Ntile.java
@@ -28,7 +28,6 @@ import 
org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
 import org.apache.doris.nereids.types.BigIntType;
 import org.apache.doris.nereids.types.DataType;
 import org.apache.doris.nereids.types.IntegerType;
-import org.apache.doris.nereids.types.LargeIntType;
 import org.apache.doris.nereids.types.SmallIntType;
 import org.apache.doris.nereids.types.TinyIntType;
 
@@ -46,15 +45,11 @@ public class Ntile extends WindowFunction implements 
LeafExpression, AlwaysNotNu
             
FunctionSignature.ret(BigIntType.INSTANCE).args(TinyIntType.INSTANCE),
             
FunctionSignature.ret(BigIntType.INSTANCE).args(SmallIntType.INSTANCE),
             
FunctionSignature.ret(BigIntType.INSTANCE).args(IntegerType.INSTANCE),
-            
FunctionSignature.ret(BigIntType.INSTANCE).args(BigIntType.INSTANCE),
-            
FunctionSignature.ret(LargeIntType.INSTANCE).args(LargeIntType.INSTANCE)
+            
FunctionSignature.ret(BigIntType.INSTANCE).args(BigIntType.INSTANCE)
     );
 
-    private Expression buckets;
-
     public Ntile(Expression buckets) {
         super("ntile", buckets);
-        this.buckets = buckets;
     }
 
     /** constructor for withChildren and reuse signature */
@@ -62,10 +57,6 @@ public class Ntile extends WindowFunction implements 
LeafExpression, AlwaysNotNu
         super(functionParams);
     }
 
-    public Expression getBuckets() {
-        return buckets;
-    }
-
     @Override
     public Ntile withChildren(List<Expression> children) {
         Preconditions.checkArgument(children.size() == 1);
@@ -74,16 +65,23 @@ public class Ntile extends WindowFunction implements 
LeafExpression, AlwaysNotNu
 
     @Override
     public void checkLegalityBeforeTypeCoercion() {
-        DataType type = getBuckets().getDataType();
+        Expression buckets = getArgument(0);
+        DataType type = buckets.getDataType();
         if (!type.isIntegralType()) {
             throw new AnalysisException("The bucket of NTILE must be a 
integer: " + this.toSql());
         }
-        if (!getBuckets().isConstant()) {
+        if (type.isLargeIntType()) {
+            // NTILE always returns BIGINT, and backend computes the bucket 
index with an int64 value,
+            // so a LARGEINT bucket can not be handled.
+            throw new AnalysisException("The bucket of NTILE must be an 
integer within the range of BIGINT, "
+                    + "but got " + type.toSql() + ": " + this.toSql());
+        }
+        if (!buckets.isConstant()) {
             throw new AnalysisException(
                 "The bucket of NTILE must be a constant value: " + 
this.toSql());
         }
-        if (getBuckets() instanceof Literal) {
-            if (((Literal) getBuckets()).getDouble() <= 0) {
+        if (buckets instanceof Literal) {
+            if (((Literal) buckets).getDouble() <= 0) {
                 throw new AnalysisException(
                     "The bucket parameter of NTILE must be a constant positive 
integer: " + this.toSql());
             }
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/functions/window/NtileBucketBackportTest.java
 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/functions/window/NtileBucketBackportTest.java
new file mode 100644
index 00000000000..c44b384dbf6
--- /dev/null
+++ 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/functions/window/NtileBucketBackportTest.java
@@ -0,0 +1,49 @@
+// 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.trees.expressions.functions.window;
+
+import org.apache.doris.nereids.exceptions.AnalysisException;
+import org.apache.doris.nereids.trees.expressions.SlotReference;
+import org.apache.doris.nereids.trees.expressions.literal.BigIntLiteral;
+import org.apache.doris.nereids.trees.expressions.literal.LargeIntLiteral;
+import org.apache.doris.nereids.types.BigIntType;
+
+import com.google.common.collect.ImmutableList;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import java.math.BigInteger;
+
+class NtileBucketBackportTest {
+    @Test
+    void rejectsLargeIntAndUsesTheRewrittenArgument() {
+        Assertions.assertThrows(AnalysisException.class,
+                () -> new Ntile(new 
LargeIntLiteral(BigInteger.ONE)).checkLegalityBeforeTypeCoercion());
+        Assertions.assertThrows(AnalysisException.class,
+                () -> new Ntile(new SlotReference("bucket", 
BigIntType.INSTANCE))
+                        .checkLegalityBeforeTypeCoercion());
+        Ntile ntile = new Ntile(new BigIntLiteral(2));
+        Assertions.assertDoesNotThrow(ntile::checkLegalityBeforeTypeCoercion);
+        Assertions.assertEquals(BigIntType.INSTANCE, ntile.getDataType());
+        Assertions.assertThrows(AnalysisException.class,
+                () -> ntile.withChildren(ImmutableList.of(new 
BigIntLiteral(0)))
+                        .checkLegalityBeforeTypeCoercion());
+        Assertions.assertDoesNotThrow(() -> ntile.withChildren(
+                ImmutableList.of(new 
BigIntLiteral(Long.MAX_VALUE))).checkLegalityBeforeTypeCoercion());
+    }
+}
diff --git 
a/regression-test/suites/query_p0/sql_functions/window_functions/test_ntile_function.groovy
 
b/regression-test/suites/query_p0/sql_functions/window_functions/test_ntile_function.groovy
index 63f3505130e..1b4f297a9d2 100644
--- 
a/regression-test/suites/query_p0/sql_functions/window_functions/test_ntile_function.groovy
+++ 
b/regression-test/suites/query_p0/sql_functions/window_functions/test_ntile_function.groovy
@@ -80,8 +80,17 @@ suite("test_ntile_function") {
         sql "select k1, k2, k3, ntile(k1) over (partition by k1 order by k2) 
as ntile from ${tableName} order by k1, k2, k3 desc;"
         exception ""
     }
-}
 
+    test {
+        sql "select k1, k2, k3, ntile(170141183460469231731687303715884105727) 
over (partition by k1 order by k2) as ntile from ${tableName} order by k1, k2, 
k3 desc;"
+        exception "The bucket of NTILE must be an integer within the range of 
BIGINT, but got LARGEINT"
+    }
+
+    test {
+        sql "select k1, k2, k3, ntile(cast(3 as largeint)) over (partition by 
k1 order by k2) as ntile from ${tableName} order by k1, k2, k3 desc;"
+        exception "The bucket of NTILE must be an integer within the range of 
BIGINT, but got LARGEINT"
+    }
+}
 
 
 


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

Reply via email to