From 4319df771b0dd68b7a5db6bccfd7045b80255089 Mon Sep 17 00:00:00 2001 From: TCeason Date: Mon, 14 Oct 2024 10:47:00 +0800 Subject: [PATCH] fix(query): ntile frame should be unbounded --- .../sql/src/planner/semantic/type_check.rs | 26 ++---- .../query/window_function/window_ntile.test | 86 ++++++++++++++++++- 2 files changed, 91 insertions(+), 21 deletions(-) diff --git a/src/query/sql/src/planner/semantic/type_check.rs b/src/query/sql/src/planner/semantic/type_check.rs index cea2467b2afe..e570836ec62a 100644 --- a/src/query/sql/src/planner/semantic/type_check.rs +++ b/src/query/sql/src/planner/semantic/type_check.rs @@ -1165,13 +1165,8 @@ impl<'a> TypeChecker<'a> { } self.in_window_function = false; - let frame = self.resolve_window_frame( - span, - &func, - &partitions, - &mut order_by, - spec.window_frame.clone(), - )?; + let frame = + self.resolve_window_frame(span, &func, &mut order_by, spec.window_frame.clone())?; let data_type = func.return_type(); let window_func = WindowFunc { span, @@ -1319,7 +1314,6 @@ impl<'a> TypeChecker<'a> { &mut self, span: Span, func: &WindowFuncType, - partition_by: &[ScalarExpr], order_by: &mut [WindowOrderBy], window_frame: Option, ) -> Result { @@ -1354,18 +1348,10 @@ impl<'a> TypeChecker<'a> { }); } WindowFuncType::Ntile(_) => { - return Ok(if partition_by.is_empty() { - WindowFuncFrame { - units: WindowFuncFrameUnits::Rows, - start_bound: WindowFuncFrameBound::Preceding(None), - end_bound: WindowFuncFrameBound::Following(None), - } - } else { - WindowFuncFrame { - units: WindowFuncFrameUnits::Rows, - start_bound: WindowFuncFrameBound::CurrentRow, - end_bound: WindowFuncFrameBound::CurrentRow, - } + return Ok(WindowFuncFrame { + units: WindowFuncFrameUnits::Rows, + start_bound: WindowFuncFrameBound::Preceding(None), + end_bound: WindowFuncFrameBound::Following(None), }); } WindowFuncType::CumeDist => { diff --git a/tests/sqllogictests/suites/query/window_function/window_ntile.test b/tests/sqllogictests/suites/query/window_function/window_ntile.test index f00dd683bc3b..d9f467c3cd95 100644 --- a/tests/sqllogictests/suites/query/window_function/window_ntile.test +++ b/tests/sqllogictests/suites/query/window_function/window_ntile.test @@ -168,4 +168,88 @@ statement ok USE default statement ok -DROP DATABASE test_window_ntile \ No newline at end of file +DROP DATABASE test_window_ntile + +statement ok +CREATE or REPLACE TABLE t ( id INT NULL, kind VARCHAR NULL, per FLOAT NULL); + +statement ok +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); + + +query TTTT +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 + +query TTTT +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; +---- +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 + +statement ok +drop table if exists t;