databendlabs / databend

𝗗𝗮𝘁𝗮, 𝗔𝗻𝗮𝗹𝘆𝘁𝗶𝗰𝘀 & 𝗔𝗜. Modern alternative to Snowflake. Cost-effective and simple for massive-scale analytics. https://databend.com
https://docs.databend.com
Other
7.85k stars 750 forks source link

bug: ntile frame not correct #16603

Closed TCeason closed 1 month ago

TCeason commented 1 month ago

Search before asking

Version

636

What's Wrong?

ntile frame not correct

How to Reproduce?

set max_block_size=10;
CREATE TABLE t (  id INT NULL,  kind VARCHAR NULL, per FLOAT NULL);
INSERT INTO t (id, kind, per) VALUES
(17, 'a', 10.0), (17, 'a', 20.5), (17, 'a', 30.25), (17, 'a', 40.75), (17, 'a', 50.0),
(17, 'a', 60.5), (17, 'a', 70.0), (17, 'a', 80.25), (17, 'a', 90.5), (17, 'a', 100.0),
(17, 'a', 110.75), (17, 'a', 120.5), (17, 'a', 130.0), (17, 'a', 140.25), (17, 'a', 150.5),
(17, 'a', 160.0), (17, 'a', 170.75), (17, 'a', 180.5), (17, 'a', 190.0), (17, 'a', 200.0),
(17, 'a', 210.5), (17, 'a', 220.25), (17, 'a', 230.0), (17, 'a', 240.75), (17, 'a', 250.5),
(17, 'a', 260.0), (17, 'a', 270.0), (17, 'a', 280.5), (17, 'a', 290.0), (17, 'a', 300.0),
(17, 'a', 310.5), (17, 'a', 320.0), (17, 'a', 330.25), (17, 'a', 340.0), (17, 'a', 350.5),
(17, 'a', 360.0), (17, 'a', 370.0), (17, 'a', 380.5), (17, 'a', 390.0), (17, 'a', 400.0),
(17, 'a', 410.75), (17, 'a', 420.5), (17, 'a', 430.0), (17, 'a', 440.25), (17, 'a', 450.5),
(17, 'a', 460.0), (17, 'a', 470.75), (17, 'a', 480.5), (17, 'a', 490.0), (17, 'a', 500.0),
(17, 'a', 510.5), (17, 'a', 520.25), (17, 'a', 530.0), (17, 'a', 540.5), (17, 'a', 550.0),
(17, 'a', 560.0), (17, 'a', 570.5), (17, 'a', 580.0), (17, 'a', 590.25), (17, 'a', 600.0),
(17, 'a', 610.5), (17, 'a', 620.0), (17, 'a', 630.0), (17, 'a', 640.5), (17, 'a', 650.0),
(17, 'a', 660.0), (17, 'a', 670.5), (17, 'a', 680.0), (17, 'a', 690.0), (17, 'a', 700.5),
(17, 'a', 710.0), (17, 'a', 720.0), (17, 'a', 730.5), (17, 'a', 740.0), (17, 'a', 750.25),
(17, 'a', 760.0), (17, 'a', 770.5), (17, 'a', 780.0), (17, 'a', 790.0), (17, 'a', 800.5),
(17, 'a', 810.0), (17, 'a', 820.0), (17, 'a', 830.5), (17, 'a', 840.0), (17, 'a', 850.0),
(17, 'a', 860.5), (17, 'a', 870.0), (17, 'a', 930.0), (17, 'a', 930.0), (17, 'a', 930.0),
(17, 'a', 930.0), (17, 'a', 930.0), (17, 'a', 930.0), (17, 'a', 1000.0), (17, 'a', 1000.0),
(17, 'a', 1000.0), (17, 'a', 1000.0), (17, 'a', 1000.0), (17, 'a', 1000.0), (17, 'a', 1000.0);

--right 
SELECT
    COUNT(*), quantile, id, kind
FROM
    (
        SELECT
            id, kind, ntile(10) OVER ( ORDER BY per ASC ) AS quantile
        FROM
            (SELECT * FROM t)
    )
GROUP BY
    quantile, id, kind
ORDER BY
    quantile, id, kind;
10  1   17  a
10  2   17  a
10  3   17  a
10  4   17  a
10  5   17  a
10  6   17  a
10  7   17  a
10  8   17  a
10  9   17  a
10  10  17  a

Bug:

SELECT
    COUNT(*), quantile, id, kind
FROM
    (
        SELECT
            id,
            kind,
            ntile(10) OVER ( PARTITION BY id, kind ORDER BY per ASC ) AS quantile
        FROM
            (SELECT * FROM t)
    )
GROUP BY
    quantile, id, kind
ORDER BY
    quantile, id, kind;
┌──────────────────────────────────────────────────────────┐
│ COUNT(*) │ quantile │        id       │       kind       │
│  UInt64  │  UInt64  │ Nullable(Int32) │ Nullable(String) │
├──────────┼──────────┼─────────────────┼──────────────────┤
│        1 │        1 │              17 │ a                │
│        1 │        2 │              17 │ a                │
│        1 │        3 │              17 │ a                │
│        1 │        4 │              17 │ a                │
│        2 │        5 │              17 │ a                │
│        3 │        6 │              17 │ a                │
│        5 │        7 │              17 │ a                │
│       10 │        8 │              17 │ a                │
│       30 │        9 │              17 │ a                │
│       46 │       10 │              17 │ a                │
└──────────────────────────────────────────────────────────┘

Are you willing to submit PR?