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

panxiaolei 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 a746054456b [Bug](function) fix wrong result when percentile's second 
argument is 1 (#47586)
a746054456b is described below

commit a746054456be40daae6b0111f49d9e5ec20ddf4d
Author: Pxl <[email protected]>
AuthorDate: Fri Feb 7 18:57:04 2025 +0800

    [Bug](function) fix wrong result when percentile's second argument is 1 
(#47586)
    
    ### What problem does this PR solve?
    fix wrong result when percentile's second argument is 1
    related with https://github.com/apache/doris/pull/34382
    ### Check List (For Author)
    
    - Test <!-- At least one of them must be included. -->
        - [x] 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:
        - [x] No.
        - [ ] Yes. <!-- Explain the behavior change -->
    
    - Does this need documentation?
        - [x] 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 -->
---
 be/src/util/counts.h                                 |   8 +++++---
 .../test_aggregate_percentile_no_cast.out            | Bin 1148 -> 1410 bytes
 .../test_aggregate_percentile_no_cast.groovy         |  19 +++++++++++++++++++
 3 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/be/src/util/counts.h b/be/src/util/counts.h
index 9f45eb4d426..43b815cf8ca 100644
--- a/be/src/util/counts.h
+++ b/be/src/util/counts.h
@@ -87,13 +87,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 296589ed425..53cbce2c30b 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 a7f382abe21..1a9a67ebbaf 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
@@ -111,4 +111,23 @@ suite("test_aggregate_percentile_no_cast") {
     }
     sql "INSERT INTO percentile_test_db2 values(1,10.1), (2,8.2), (2,114.3) 
,(3,10.4) ,(5,29.5) ,(6,101.6)"
     qt_select "select percentile_array(level,[0.5,0.55,0.805])from 
percentile_test_db2;"
+
+    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: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to