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

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


The following commit(s) were added to refs/heads/branch-3.0 by this push:
     new 753546f4710 [Bug](function) fix wrong result when percentile's second 
argument is 1 (#47586) (#47710)
753546f4710 is described below

commit 753546f4710cd678d0aac1a308e172e530e701cf
Author: Pxl <x...@selectdb.com>
AuthorDate: Tue Feb 11 19:03:39 2025 +0800

    [Bug](function) fix wrong result when percentile's second argument is 1 
(#47586) (#47710)
    
    pick from #47586
---
 be/src/util/counts.h                                  |   8 +++++---
 .../test_aggregate_percentile_no_cast.out             | Bin 1110 -> 1372 bytes
 .../test_aggregate_percentile_no_cast.groovy          |  18 ++++++++++++++++++
 3 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/be/src/util/counts.h b/be/src/util/counts.h
index 968dc00e2ae..054f4434e27 100644
--- a/be/src/util/counts.h
+++ b/be/src/util/counts.h
@@ -195,13 +195,15 @@ public:
                 // get val in aggregate_function_percentile_approx.h
                 return 0.0;
             }
-            if (quantile == 1 || _nums.size() == 1) {
-                return _nums.back();
-            }
+
             if (UNLIKELY(!std::is_sorted(_nums.begin(), _nums.end()))) {
                 pdqsort(_nums.begin(), _nums.end());
             }
 
+            if (quantile == 1 || _nums.size() == 1) {
+                return _nums.back();
+            }
+
             double u = (_nums.size() - 1) * quantile;
             auto index = static_cast<uint32_t>(u);
             return _nums[index] +
diff --git 
a/regression-test/data/nereids_p0/sql_functions/aggregate_functions/test_aggregate_percentile_no_cast.out
 
b/regression-test/data/nereids_p0/sql_functions/aggregate_functions/test_aggregate_percentile_no_cast.out
index 1764ba21dde..ead42332d0c 100644
Binary files 
a/regression-test/data/nereids_p0/sql_functions/aggregate_functions/test_aggregate_percentile_no_cast.out
 and 
b/regression-test/data/nereids_p0/sql_functions/aggregate_functions/test_aggregate_percentile_no_cast.out
 differ
diff --git 
a/regression-test/suites/nereids_p0/sql_functions/aggregate_functions/test_aggregate_percentile_no_cast.groovy
 
b/regression-test/suites/nereids_p0/sql_functions/aggregate_functions/test_aggregate_percentile_no_cast.groovy
index ef76aee4405..412bec35d8f 100644
--- 
a/regression-test/suites/nereids_p0/sql_functions/aggregate_functions/test_aggregate_percentile_no_cast.groovy
+++ 
b/regression-test/suites/nereids_p0/sql_functions/aggregate_functions/test_aggregate_percentile_no_cast.groovy
@@ -94,4 +94,22 @@ suite("test_aggregate_percentile_no_cast") {
     sql "INSERT INTO percentile_test_db values(1,10), (2,8), (2,114) ,(3,10) 
,(5,29) ,(6,101)"
     qt_select "select id,percentile(level,0.5) , percentile(level,0.55) , 
percentile(level,0.805) , percentile_array(level,[0.5,0.55,0.805])from 
percentile_test_db group by id order by id"
 
+    sql "DROP TABLE IF EXISTS TINYINTDATA_NOT_EMPTY_NOT_NULLABLE"
+    sql """
+    CREATE TABLE TINYINTDATA_NOT_EMPTY_NOT_NULLABLE(id INT, data TINYINT  NOT 
NULL) DISTRIBUTED BY HASH(id) BUCKETS 1 PROPERTIES ('replication_num' = '1');
+    """
+    sql "DROP TABLE IF EXISTS TEMPDATA"
+    sql """
+    CREATE TABLE IF NOT EXISTS TEMPDATA(id INT, data INT) DISTRIBUTED BY 
HASH(id) BUCKETS 1 PROPERTIES ('replication_num' = '1');
+    """
+    sql """
+    INSERT INTO TINYINTDATA_NOT_EMPTY_NOT_NULLABLE values (0, -1),(1, 0),(2, 
1),(3, 2),(4, 12),(5, -128),(6, 127);
+    """
+    sql """
+    INSERT INTO TEMPDATA values(1, 1); 
+    """
+    qt_test """
+    SELECT ARG0,PERCENTILE(NULLABLE(ARG0),1) OVER(ORDER BY t.ARG0 ROWS BETWEEN 
 CURRENT ROW  AND  UNBOUNDED FOLLOWING ) as a,PERCENTILE(NULLABLE(ARG0),0.9999) 
OVER(ORDER BY t.ARG0 ROWS BETWEEN  CURRENT ROW  AND  UNBOUNDED FOLLOWING ) as 
b,PERCENTILE(NULLABLE(ARG0),0) OVER(ORDER BY t.ARG0 ROWS BETWEEN  CURRENT ROW  
AND  UNBOUNDED FOLLOWING ) as c,PERCENTILE(NULLABLE(ARG0),0.0001) OVER(ORDER BY 
t.ARG0 ROWS BETWEEN  CURRENT ROW  AND  UNBOUNDED FOLLOWING ) as d FROM (SELECT 
TEMPDATA . data,  [...]
+ FROM TINYINTDATA_NOT_EMPTY_NOT_NULLABLE ) AS TABLE0) t  GROUP BY ARG0 order 
by ARG0;
+    """
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to