Skip to content

Apache DataFusion SQL Query Engine

minor: port window tests to slt (part 2) (#5399)

Andrew Lambcb09142c7e44
diff --git a/datafusion/core/tests/sql/window.rs b/datafusion/core/tests/sql/window.rs
index c60ee304db..329eaee529 100644
--- a/datafusion/core/tests/sql/window.rs
+++ b/datafusion/core/tests/sql/window.rs
@@ -20,620 +20,6 @@ use ::parquet::arrow::arrow_writer::ArrowWriter;
use ::parquet::file::properties::WriterProperties;
use datafusion::execution::options::ReadOptions;
-#[tokio::test]
-async fn window_frame_rows_preceding_with_unique_partition() -> Result<()> {
- let ctx = SessionContext::new();
- register_aggregate_csv(&ctx).await?;
- let sql = "SELECT \
- SUM(c4) OVER(PARTITION BY c1 ORDER BY c9 ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING),\
- COUNT(*) OVER(PARTITION BY c1 ORDER BY c9 ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)\
- FROM aggregate_test_100 \
- ORDER BY c9 \
- LIMIT 5";
- let actual = execute_to_batches(&ctx, sql).await;
- let expected = vec![
- "+----------------------------+-----------------+",
- "| SUM(aggregate_test_100.c4) | COUNT(UInt8(1)) |",
- "+----------------------------+-----------------+",
- "| -38611 | 2 |",
- "| 17547 | 2 |",
- "| -1301 | 2 |",
- "| 26638 | 2 |",
- "| 26861 | 3 |",
- "+----------------------------+-----------------+",
- ];
- assert_batches_eq!(expected, &actual);
- Ok(())
-}
-
-#[tokio::test]
-async fn window_frame_ranges_preceding_following() -> Result<()> {
- let ctx = SessionContext::new();
- register_aggregate_csv(&ctx).await?;
- let sql = "SELECT \
- SUM(c4) OVER(ORDER BY c2 RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING),\
- SUM(c3) OVER(ORDER BY c2 RANGE BETWEEN 10000 PRECEDING AND 10000 FOLLOWING),\
- COUNT(*) OVER(ORDER BY c2 RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING) \
- FROM aggregate_test_100 \
- ORDER BY c9 \
- LIMIT 5";
- let actual = execute_to_batches(&ctx, sql).await;
- let expected = vec![
- "+----------------------------+----------------------------+-----------------+",
- "| SUM(aggregate_test_100.c4) | SUM(aggregate_test_100.c3) | COUNT(UInt8(1)) |",
- "+----------------------------+----------------------------+-----------------+",
- "| 52276 | 781 | 56 |",
- "| 260620 | 781 | 63 |",
- "| -28623 | 781 | 37 |",
- "| 260620 | 781 | 63 |",
- "| 260620 | 781 | 63 |",
- "+----------------------------+----------------------------+-----------------+",
- ];
- assert_batches_eq!(expected, &actual);
- Ok(())
-}
-
-#[tokio::test]
-async fn window_frame_ranges_ntile() -> Result<()> {
- let ctx = SessionContext::new();
- register_aggregate_csv(&ctx).await?;
- let sql = "SELECT \
- NTILE(8) OVER (ORDER BY C4) as ntile1,\
- NTILE(12) OVER (ORDER BY C12 DESC) as ntile2 \
- FROM aggregate_test_100 \
- ORDER BY c7 \
- LIMIT 5";
- let actual = execute_to_batches(&ctx, sql).await;
- let expected = vec![
- "+--------+--------+",
- "| ntile1 | ntile2 |",
- "+--------+--------+",
- "| 8 | 12 |",
- "| 5 | 11 |",
- "| 3 | 11 |",
- "| 2 | 7 |",
- "| 7 | 12 |",
- "+--------+--------+",
- ];
- assert_batches_eq!(expected, &actual);
- Ok(())
-}
-
-#[tokio::test]
-async fn window_frame_ranges_string_check() -> Result<()> {
- let ctx = SessionContext::new();
- register_aggregate_csv(&ctx).await?;
- let sql = "SELECT \
- SUM(LENGTH(c13)) OVER(ORDER BY c13), \
- SUM(LENGTH(c1)) OVER(ORDER BY c1) \
- FROM aggregate_test_100 \
- ORDER BY c9 \
- LIMIT 5";
- let actual = execute_to_batches(&ctx, sql).await;
- let expected = vec![
- "+----------------------------------------------+---------------------------------------------+",
- "| SUM(characterlength(aggregate_test_100.c13)) | SUM(characterlength(aggregate_test_100.c1)) |",
- "+----------------------------------------------+---------------------------------------------+",
- "| 2100 | 100 |",
- "| 510 | 79 |",
- "| 1440 | 21 |",
- "| 1830 | 61 |",
- "| 2010 | 21 |",
- "+----------------------------------------------+---------------------------------------------+",
- ];
- assert_batches_eq!(expected, &actual);
- Ok(())
-}
-
-#[tokio::test]
-async fn window_frame_order_by_unique() -> Result<()> {
- let ctx = SessionContext::new();
- register_aggregate_csv(&ctx).await?;
- let sql = "SELECT \
- SUM(c5) OVER (ORDER BY c5) as sum1, \
- COUNT(*) OVER (ORDER BY c9) as count1 \
- FROM aggregate_test_100 \
- ORDER BY c9 \
- LIMIT 5";
- let actual = execute_to_batches(&ctx, sql).await;
- let expected = vec![
- "+--------------+--------+",
- "| sum1 | count1 |",
- "+--------------+--------+",
- "| -49877765574 | 1 |",
- "| -50025861694 | 2 |",
- "| -45402230071 | 3 |",
- "| -14557735645 | 4 |",
- "| -18365391649 | 5 |",
- "+--------------+--------+",
- ];
- assert_batches_eq!(expected, &actual);
- Ok(())
-}
-
-/// If the sorting columns have non unique values, the unstable sorting may produce
-/// indeterminate results. Therefore, we are commenting out the following test for now.
-///
-// #[tokio::test]
-// async fn window_frame_order_by_non_unique() -> Result<()> {
-// let ctx = SessionContext::new();
-// register_aggregate_csv(&ctx).await?;
-// let sql = "SELECT \
-// c2, \
-// c9, \
-// SUM(c5) OVER (ORDER BY c2), \
-// COUNT(*) OVER (ORDER BY c2) \
-// FROM aggregate_test_100 \
-// ORDER BY c2 \
-// LIMIT 5";
-// let actual = execute_to_batches(&ctx, sql).await;
-// let expected = vec![
-// "+----+------------+----------------------------+-----------------+",
-// "| c2 | c9 | SUM(aggregate_test_100.c5) | COUNT(UInt8(1)) |",
-// "+----+------------+----------------------------+-----------------+",
-// "| 1 | 879082834 | -438598674 | 22 |",
-// "| 1 | 3542840110 | -438598674 | 22 |",
-// "| 1 | 3275293996 | -438598674 | 22 |",
-// "| 1 | 774637006 | -438598674 | 22 |",
-// "| 1 | 4015442341 | -438598674 | 22 |",
-// "+----+------------+----------------------------+-----------------+",
-// ];
-// assert_batches_eq!(expected, &actual);
-// Ok(())
-// }
-
-#[tokio::test]
-async fn window_frame_ranges_unbounded_preceding_following() -> Result<()> {
- let ctx = SessionContext::new();
- register_aggregate_csv(&ctx).await?;
- let sql = "SELECT \
- SUM(c2) OVER (ORDER BY c2 RANGE BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING) as sum1, \
- COUNT(*) OVER (ORDER BY c2 RANGE BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING) as cnt1 \
- FROM aggregate_test_100 \
- ORDER BY c9 \
- LIMIT 5";
- let actual = execute_to_batches(&ctx, sql).await;
- let expected = vec![
- "+------+------+",
- "| sum1 | cnt1 |",
- "+------+------+",
- "| 285 | 100 |",
- "| 123 | 63 |",
- "| 285 | 100 |",
- "| 123 | 63 |",
- "| 123 | 63 |",
- "+------+------+",
- ];
- assert_batches_eq!(expected, &actual);
- Ok(())
-}
-
-#[tokio::test]
-async fn window_frame_ranges_preceding_and_preceding() -> Result<()> {
- let ctx = SessionContext::new();
- register_aggregate_csv(&ctx).await?;
- let sql = "SELECT \
- SUM(c2) OVER (ORDER BY c2 RANGE BETWEEN 3 PRECEDING AND 1 PRECEDING), \
- COUNT(*) OVER (ORDER BY c2 RANGE BETWEEN 3 PRECEDING AND 1 PRECEDING) \
- FROM aggregate_test_100 \
- ORDER BY c9 \
- LIMIT 5";
- let actual = execute_to_batches(&ctx, sql).await;
- let expected = vec![
- "+----------------------------+-----------------+",
- "| SUM(aggregate_test_100.c2) | COUNT(UInt8(1)) |",
- "+----------------------------+-----------------+",
- "| 123 | 63 |",
- "| 22 | 22 |",
- "| 193 | 64 |",
- "| 22 | 22 |",
- "| 22 | 22 |",
- "+----------------------------+-----------------+",
- ];
- assert_batches_eq!(expected, &actual);
- Ok(())
-}
-
-#[tokio::test]
-async fn window_frame_ranges_unbounded_preceding_following_diff_col() -> Result<()> {
- let ctx = SessionContext::new();
- register_aggregate_csv(&ctx).await?;
- let sql = "SELECT \
- SUM(c2) OVER (ORDER BY c2 RANGE BETWEEN CURRENT ROW AND 1 FOLLOWING), \
- COUNT(*) OVER (ORDER BY c2 RANGE BETWEEN CURRENT ROW AND 1 FOLLOWING) \
- FROM aggregate_test_100 \
- ORDER BY c9 \
- LIMIT 5";
- let actual = execute_to_batches(&ctx, sql).await;
- let expected = vec![
- "+----------------------------+-----------------+",
- "| SUM(aggregate_test_100.c2) | COUNT(UInt8(1)) |",
- "+----------------------------+-----------------+",
- "| 162 | 37 |",
- "| 101 | 41 |",
- "| 70 | 14 |",
- "| 101 | 41 |",
- "| 101 | 41 |",
- "+----------------------------+-----------------+",
- ];
- assert_batches_eq!(expected, &actual);
- Ok(())
-}
-
-#[tokio::test]
-async fn window_frame_partition_by_order_by_desc() -> Result<()> {
- let ctx = SessionContext::new();
- register_aggregate_csv(&ctx).await?;
- let sql = "SELECT
- SUM(c4) OVER(PARTITION BY c1 ORDER BY c2 DESC RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING)
- FROM aggregate_test_100
- ORDER BY c9
- LIMIT 5";
- let actual = execute_to_batches(&ctx, sql).await;
- let expected = vec![
- "+----------------------------+",
- "| SUM(aggregate_test_100.c4) |",
- "+----------------------------+",
- "| -124618 |",
- "| 205080 |",
- "| -40819 |",
- "| -19517 |",
- "| 47246 |",
- "+----------------------------+",
- ];
- assert_batches_eq!(expected, &actual);
- Ok(())
-}
-
-#[tokio::test]
-async fn window_frame_range_float() -> Result<()> {
- let ctx = SessionContext::new();
- register_aggregate_csv(&ctx).await?;
- let sql = "SELECT
- SUM(c12) OVER (ORDER BY C12 RANGE BETWEEN 0.2 PRECEDING AND 0.2 FOLLOWING)
- FROM aggregate_test_100
- ORDER BY C9
- LIMIT 5";
- let actual = execute_to_batches(&ctx, sql).await;
- let expected = vec![
- "+-----------------------------+",
- "| SUM(aggregate_test_100.c12) |",
- "+-----------------------------+",
- "| 2.5476701803634296 |",
- "| 10.6299412548214 |",
- "| 2.5476701803634296 |",
- "| 20.349518503437288 |",
- "| 21.408674363507753 |",
- "+-----------------------------+",
- ];
- assert_batches_eq!(expected, &actual);
- Ok(())
-}
-
-#[tokio::test]
-async fn window_frame_ranges_timestamp() -> Result<()> {
- // define a schema.
- let schema = Arc::new(Schema::new(vec![Field::new(
- "ts",
- DataType::Timestamp(TimeUnit::Nanosecond, None),
- false,
- )]));
-
- // define data in two partitions
- let batch = RecordBatch::try_new(
- schema.clone(),
- vec![Arc::new(TimestampNanosecondArray::from_slice([
- 1664264591000000000,
- 1664264592000000000,
- 1664264592000000000,
- 1664264593000000000,
- 1664264594000000000,
- 1664364594000000000,
- 1664464594000000000,
- 1664564594000000000,
- ]))],
- )
- .unwrap();
-
- let ctx = SessionContext::new();
- // declare a new context. In spark API, this corresponds to a new spark SQLsession
- // declare a table in memory. In spark API, this corresponds to createDataFrame(...).
- let provider = MemTable::try_new(schema, vec![vec![batch]]).unwrap();
- // Register table
- ctx.register_table("t", Arc::new(provider)).unwrap();
-
- // execute the query
- let df = ctx
- .sql(
- "SELECT
- ts,
- COUNT(*) OVER (ORDER BY ts RANGE BETWEEN INTERVAL '1' DAY PRECEDING AND INTERVAL '2 DAY' FOLLOWING) AS cnt1,
- COUNT(*) OVER (ORDER BY ts RANGE BETWEEN '0 DAY' PRECEDING AND '0' DAY FOLLOWING) as cnt2,
- COUNT(*) OVER (ORDER BY ts RANGE BETWEEN '5' SECOND PRECEDING AND CURRENT ROW) as cnt3
- FROM t
- ORDER BY ts"
- )
- .await?;
-
- let actual = df.collect().await?;
- let expected = vec![
- "+---------------------+------+------+------+",
- "| ts | cnt1 | cnt2 | cnt3 |",
- "+---------------------+------+------+------+",
- "| 2022-09-27T07:43:11 | 6 | 1 | 1 |",
- "| 2022-09-27T07:43:12 | 6 | 2 | 3 |",
- "| 2022-09-27T07:43:12 | 6 | 2 | 3 |",
- "| 2022-09-27T07:43:13 | 6 | 1 | 4 |",
- "| 2022-09-27T07:43:14 | 6 | 1 | 5 |",
- "| 2022-09-28T11:29:54 | 2 | 1 | 1 |",
- "| 2022-09-29T15:16:34 | 2 | 1 | 1 |",
- "| 2022-09-30T19:03:14 | 1 | 1 | 1 |",
- "+---------------------+------+------+------+",
- ];
- assert_batches_eq!(expected, &actual);
- Ok(())
-}
-
-#[tokio::test]
-async fn window_frame_ranges_unbounded_preceding_err() -> Result<()> {
- let ctx = SessionContext::new();
- register_aggregate_csv(&ctx).await?;
- // execute the query
- let df = ctx
- .sql(
- "SELECT \
- SUM(c2) OVER (ORDER BY c2 RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED PRECEDING), \
- COUNT(*) OVER (ORDER BY c2 RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED PRECEDING) \
- FROM aggregate_test_100 \
- ORDER BY c9 \
- LIMIT 5",
- )
- .await;
- assert_eq!(
- df.err().unwrap().to_string(),
- "Execution error: Invalid window frame: end bound cannot be unbounded preceding"
- .to_owned()
- );
- Ok(())
-}
-
-#[tokio::test]
-async fn window_frame_groups_preceding_following_desc() -> Result<()> {
- let ctx = SessionContext::new();
- register_aggregate_csv(&ctx).await?;
- let sql = "SELECT
- SUM(c4) OVER(ORDER BY c2 DESC GROUPS BETWEEN 1 PRECEDING AND 1 FOLLOWING),
- SUM(c3) OVER(ORDER BY c2 DESC GROUPS BETWEEN 10000 PRECEDING AND 10000 FOLLOWING),
- COUNT(*) OVER(ORDER BY c2 DESC GROUPS BETWEEN 1 PRECEDING AND 1 FOLLOWING)
- FROM aggregate_test_100
- ORDER BY c9
- LIMIT 5";
- let actual = execute_to_batches(&ctx, sql).await;
- let expected = vec![
- "+----------------------------+----------------------------+-----------------+",
- "| SUM(aggregate_test_100.c4) | SUM(aggregate_test_100.c3) | COUNT(UInt8(1)) |",
- "+----------------------------+----------------------------+-----------------+",
- "| 52276 | 781 | 56 |",
- "| 260620 | 781 | 63 |",
- "| -28623 | 781 | 37 |",
- "| 260620 | 781 | 63 |",
- "| 260620 | 781 | 63 |",
- "+----------------------------+----------------------------+-----------------+",
- ];
- assert_batches_eq!(expected, &actual);
- Ok(())
-}
-
-#[tokio::test]
-async fn window_frame_groups_order_by_null_desc() -> Result<()> {
- let ctx = SessionContext::new();
- register_aggregate_null_cases_csv(&ctx).await?;
- let sql = "SELECT
- COUNT(c2) OVER (ORDER BY c1 DESC GROUPS BETWEEN 5 PRECEDING AND 3 FOLLOWING)
- FROM null_cases
- LIMIT 5";
- let actual = execute_to_batches(&ctx, sql).await;
- let expected = vec![
- "+----------------------+",
- "| COUNT(null_cases.c2) |",
- "+----------------------+",
- "| 12 |",
- "| 12 |",
- "| 12 |",
- "| 12 |",
- "| 12 |",
- "+----------------------+",
- ];
- assert_batches_eq!(expected, &actual);
- Ok(())
-}
-
-#[tokio::test]
-async fn window_frame_groups() -> Result<()> {
- let ctx = SessionContext::new();
- register_aggregate_null_cases_csv(&ctx).await?;
- let sql = "SELECT
- SUM(c1) OVER (ORDER BY c3 GROUPS BETWEEN 9 PRECEDING AND 11 FOLLOWING) as a,
- SUM(c1) OVER (ORDER BY c3 DESC GROUPS BETWEEN 9 PRECEDING AND 11 FOLLOWING) as b,
- SUM(c1) OVER (ORDER BY c3 NULLS first GROUPS BETWEEN 9 PRECEDING AND 11 FOLLOWING) as c,
- SUM(c1) OVER (ORDER BY c3 DESC NULLS last GROUPS BETWEEN 9 PRECEDING AND 11 FOLLOWING) as d,
- SUM(c1) OVER (ORDER BY c3 DESC NULLS first GROUPS BETWEEN 9 PRECEDING AND 11 FOLLOWING) as e,
- SUM(c1) OVER (ORDER BY c3 NULLS first GROUPS BETWEEN 9 PRECEDING AND 11 FOLLOWING) as f,
- SUM(c1) OVER (ORDER BY c3 GROUPS current row) as a1,
- SUM(c1) OVER (ORDER BY c3 GROUPS BETWEEN 9 PRECEDING AND 5 PRECEDING) as a2,
- SUM(c1) OVER (ORDER BY c3 GROUPS BETWEEN UNBOUNDED PRECEDING AND 5 PRECEDING) as a3,
- SUM(c1) OVER (ORDER BY c3 GROUPS BETWEEN UNBOUNDED PRECEDING AND 11 FOLLOWING) as a4,
- SUM(c1) OVER (ORDER BY c3 GROUPS BETWEEN UNBOUNDED PRECEDING AND current row) as a5,
- SUM(c1) OVER (ORDER BY c3 GROUPS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) as a6,
- SUM(c1) OVER (ORDER BY c3 GROUPS BETWEEN 1 PRECEDING AND UNBOUNDED FOLLOWING) as a7,
- SUM(c1) OVER (ORDER BY c3 GROUPS BETWEEN 3 FOLLOWING AND UNBOUNDED FOLLOWING) as a8,
- SUM(c1) OVER (ORDER BY c3 GROUPS BETWEEN current row AND UNBOUNDED FOLLOWING) as a9,
- SUM(c1) OVER (ORDER BY c3 GROUPS BETWEEN current row AND 3 FOLLOWING) as a10,
- SUM(c1) OVER (ORDER BY c3 GROUPS BETWEEN 5 FOLLOWING AND 7 FOLLOWING) as a11,
- SUM(c1) OVER (ORDER BY c3 DESC GROUPS current row) as a21,
- SUM(c1) OVER (ORDER BY c3 NULLS first GROUPS BETWEEN 9 PRECEDING AND 5 PRECEDING) as a22,
- SUM(c1) OVER (ORDER BY c3 DESC NULLS last GROUPS BETWEEN UNBOUNDED PRECEDING AND 5 PRECEDING) as a23,
- SUM(c1) OVER (ORDER BY c3 NULLS last GROUPS BETWEEN UNBOUNDED PRECEDING AND 11 FOLLOWING) as a24,
- SUM(c1) OVER (ORDER BY c3 DESC NULLS first GROUPS BETWEEN UNBOUNDED PRECEDING AND current row) as a25
- FROM null_cases
- ORDER BY c3
- LIMIT 10";
- let actual = execute_to_batches(&ctx, sql).await;
- let expected = vec![
- "+-----+-----+-----+-----+-----+-----+----+-----+-----+-----+-----+------+------+------+------+-----+-----+-----+-----+------+-----+------+",
- "| a | b | c | d | e | f | a1 | a2 | a3 | a4 | a5 | a6 | a7 | a8 | a9 | a10 | a11 | a21 | a22 | a23 | a24 | a25 |",
- "+-----+-----+-----+-----+-----+-----+----+-----+-----+-----+-----+------+------+------+------+-----+-----+-----+-----+------+-----+------+",
- "| 412 | 307 | 412 | 307 | 307 | 412 | | | | 412 | | 4627 | 4627 | 4531 | 4627 | 115 | 85 | | | 4487 | 412 | 4627 |",
- "| 488 | 339 | 488 | 339 | 339 | 488 | 72 | | | 488 | 72 | 4627 | 4627 | 4512 | 4627 | 140 | 153 | 72 | | 4473 | 488 | 4627 |",
- "| 543 | 412 | 543 | 412 | 412 | 543 | 24 | | | 543 | 96 | 4627 | 4627 | 4487 | 4555 | 82 | 122 | 24 | | 4442 | 543 | 4555 |",
- "| 553 | 488 | 553 | 488 | 488 | 553 | 19 | | | 553 | 115 | 4627 | 4555 | 4473 | 4531 | 89 | 114 | 19 | | 4402 | 553 | 4531 |",
- "| 553 | 543 | 553 | 543 | 543 | 553 | 25 | | | 553 | 140 | 4627 | 4531 | 4442 | 4512 | 110 | 105 | 25 | | 4320 | 553 | 4512 |",
- "| 591 | 553 | 591 | 553 | 553 | 591 | 14 | | | 591 | 154 | 4627 | 4512 | 4402 | 4487 | 167 | 181 | 14 | | 4320 | 591 | 4487 |",
- "| 651 | 553 | 651 | 553 | 553 | 651 | 31 | 72 | 72 | 651 | 185 | 4627 | 4487 | 4320 | 4473 | 153 | 204 | 31 | 72 | 4288 | 651 | 4473 |",
- "| 662 | 591 | 662 | 591 | 591 | 662 | 40 | 96 | 96 | 662 | 225 | 4627 | 4473 | 4320 | 4442 | 154 | 141 | 40 | 96 | 4215 | 662 | 4442 |",
- "| 697 | 651 | 697 | 651 | 651 | 697 | 82 | 115 | 115 | 697 | 307 | 4627 | 4442 | 4288 | 4402 | 187 | 65 | 82 | 115 | 4139 | 697 | 4402 |",
- "| 758 | 662 | 758 | 662 | 662 | 758 | | 140 | 140 | 758 | 307 | 4627 | 4402 | 4215 | 4320 | 181 | 48 | | 140 | 4084 | 758 | 4320 |",
- "+-----+-----+-----+-----+-----+-----+----+-----+-----+-----+-----+------+------+------+------+-----+-----+-----+-----+------+-----+------+",
- ];
- assert_batches_eq!(expected, &actual);
- Ok(())
-}
-
-#[tokio::test]
-async fn window_frame_groups_multiple_order_columns() -> Result<()> {
- let ctx = SessionContext::new();
- register_aggregate_null_cases_csv(&ctx).await?;
- let sql = "SELECT
- SUM(c1) OVER (ORDER BY c2, c3 GROUPS BETWEEN 9 PRECEDING AND 11 FOLLOWING) as a,
- SUM(c1) OVER (ORDER BY c2, c3 DESC GROUPS BETWEEN 9 PRECEDING AND 11 FOLLOWING) as b,
- SUM(c1) OVER (ORDER BY c2, c3 NULLS first GROUPS BETWEEN 9 PRECEDING AND 11 FOLLOWING) as c,
- SUM(c1) OVER (ORDER BY c2, c3 DESC NULLS last GROUPS BETWEEN 9 PRECEDING AND 11 FOLLOWING) as d,
- SUM(c1) OVER (ORDER BY c2, c3 DESC NULLS first GROUPS BETWEEN 9 PRECEDING AND 11 FOLLOWING) as e,
- SUM(c1) OVER (ORDER BY c2, c3 NULLS first GROUPS BETWEEN 9 PRECEDING AND 11 FOLLOWING) as f,
- SUM(c1) OVER (ORDER BY c2, c3 GROUPS current row) as a1,
- SUM(c1) OVER (ORDER BY c2, c3 GROUPS BETWEEN 9 PRECEDING AND 5 PRECEDING) as a2,
- SUM(c1) OVER (ORDER BY c2, c3 GROUPS BETWEEN UNBOUNDED PRECEDING AND 5 PRECEDING) as a3,
- SUM(c1) OVER (ORDER BY c2, c3 GROUPS BETWEEN UNBOUNDED PRECEDING AND 11 FOLLOWING) as a4,
- SUM(c1) OVER (ORDER BY c2, c3 GROUPS BETWEEN UNBOUNDED PRECEDING AND current row) as a5,
- SUM(c1) OVER (ORDER BY c2, c3 GROUPS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) as a6,
- SUM(c1) OVER (ORDER BY c2, c3 GROUPS BETWEEN 1 PRECEDING AND UNBOUNDED FOLLOWING) as a7,
- SUM(c1) OVER (ORDER BY c2, c3 GROUPS BETWEEN 3 FOLLOWING AND UNBOUNDED FOLLOWING) as a8,
- SUM(c1) OVER (ORDER BY c2, c3 GROUPS BETWEEN current row AND UNBOUNDED FOLLOWING) as a9,
- SUM(c1) OVER (ORDER BY c2, c3 GROUPS BETWEEN current row AND 3 FOLLOWING) as a10,
- SUM(c1) OVER (ORDER BY c2, c3 GROUPS BETWEEN 5 FOLLOWING AND 7 FOLLOWING) as a11
- FROM null_cases
- ORDER BY c3
- LIMIT 10";
- let actual = execute_to_batches(&ctx, sql).await;
- let expected = vec![
- "+------+-----+------+-----+-----+------+----+-----+------+------+------+------+------+------+------+-----+-----+",
- "| a | b | c | d | e | f | a1 | a2 | a3 | a4 | a5 | a6 | a7 | a8 | a9 | a10 | a11 |",
- "+------+-----+------+-----+-----+------+----+-----+------+------+------+------+------+------+------+-----+-----+",
- "| 818 | 910 | 818 | 910 | 910 | 818 | | 249 | 249 | 818 | 432 | 4627 | 4234 | 4157 | 4195 | 98 | 82 |",
- "| 537 | 979 | 537 | 979 | 979 | 537 | 72 | | | 537 | 210 | 4627 | 4569 | 4378 | 4489 | 169 | 55 |",
- "| 811 | 838 | 811 | 838 | 838 | 811 | 24 | 221 | 3075 | 3665 | 3311 | 4627 | 1390 | 1276 | 1340 | 117 | 144 |",
- "| 763 | 464 | 763 | 464 | 464 | 763 | 19 | 168 | 3572 | 4167 | 3684 | 4627 | 962 | 829 | 962 | 194 | 80 |",
- "| 552 | 964 | 552 | 964 | 964 | 552 | 25 | | | 552 | 235 | 4627 | 4489 | 4320 | 4417 | 167 | 39 |",
- "| 963 | 930 | 963 | 930 | 930 | 963 | 14 | 201 | 818 | 1580 | 1098 | 4627 | 3638 | 3455 | 3543 | 177 | 224 |",
- "| 1113 | 814 | 1113 | 814 | 814 | 1113 | 31 | 415 | 2653 | 3351 | 2885 | 4627 | 1798 | 1694 | 1773 | 165 | 162 |",
- "| 780 | 868 | 780 | 868 | 868 | 780 | 40 | 258 | 3143 | 3665 | 3351 | 4627 | 1340 | 1223 | 1316 | 117 | 102 |",
- "| 740 | 466 | 740 | 466 | 466 | 740 | 82 | 164 | 3592 | 4168 | 3766 | 4627 | 962 | 768 | 943 | 244 | 122 |",
- "| 772 | 832 | 772 | 832 | 832 | 772 | | 277 | 3189 | 3684 | 3351 | 4627 | 1316 | 1199 | 1276 | 119 | 64 |",
- "+------+-----+------+-----+-----+------+----+-----+------+------+------+------+------+------+------+-----+-----+",
- ];
- assert_batches_eq!(expected, &actual);
- Ok(())
-}
-
-#[tokio::test]
-async fn window_frame_groups_without_order_by() -> Result<()> {
- let ctx = SessionContext::new();
- register_aggregate_csv(&ctx).await?;
- // Try executing an erroneous query (the ORDER BY clause is missing in the
- // window frame):
- let err = ctx
- .sql(
- "SELECT
- SUM(c4) OVER(PARTITION BY c2 GROUPS BETWEEN 1 PRECEDING AND 1 FOLLOWING)
- FROM aggregate_test_100
- ORDER BY c9;",
- )
- .await
- .unwrap_err();
- assert_contains!(
- err.to_string(),
- "Error during planning: GROUPS mode requires an ORDER BY clause".to_owned()
- );
- Ok(())
-}
-
-#[tokio::test]
-async fn window_frame_lag() -> Result<()> {
- let ctx = SessionContext::new();
- register_aggregate_csv(&ctx).await?;
- // execute the query
- let df = ctx
- .sql(
- "SELECT c2,
- lag(c2, c2, c2) OVER () as lag1
- FROM aggregate_test_100;",
- )
- .await?;
- let err = df.collect().await.unwrap_err();
- assert_eq!(
- err.to_string(),
- "This feature is not implemented: There is only support Literal types for field at idx: 1 in Window Function".to_owned()
- );
- Ok(())
-}
-
-#[tokio::test]
-async fn window_frame_creation() -> Result<()> {
- let ctx = SessionContext::new();
- register_aggregate_csv(&ctx).await?;
- // execute the query
- let df = ctx
- .sql(
- "SELECT
- COUNT(c1) OVER (ORDER BY c2 RANGE BETWEEN 1 PRECEDING AND 2 PRECEDING)
- FROM aggregate_test_100;",
- )
- .await?;
- let results = df.collect().await;
- assert_eq!(
- results.err().unwrap().to_string(),
- "Error during planning: Invalid window frame: start bound (1 PRECEDING) cannot be larger than end bound (2 PRECEDING)"
- );
-
- let df = ctx
- .sql(
- "SELECT
- COUNT(c1) OVER (ORDER BY c2 RANGE BETWEEN 2 FOLLOWING AND 1 FOLLOWING)
- FROM aggregate_test_100;",
- )
- .await?;
- let results = df.collect().await;
- assert_eq!(
- results.err().unwrap().to_string(),
- "Error during planning: Invalid window frame: start bound (2 FOLLOWING) cannot be larger than end bound (1 FOLLOWING)"
- );
-
- let err = ctx
- .sql(
- "SELECT
- COUNT(c1) OVER(GROUPS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
- FROM aggregate_test_100;",
- )
- .await
- .unwrap_err();
- assert_contains!(
- err.to_string(),
- "Error during planning: GROUPS mode requires an ORDER BY clause"
- );
-
- Ok(())
-}
-
#[tokio::test]
async fn window_frame_creation_type_checking() -> Result<()> {
// The following query has type error. We should test the error could be detected
@@ -675,221 +61,6 @@ async fn window_frame_creation_type_checking() -> Result<()> {
).await
}
-#[tokio::test]
-async fn test_window_row_number_aggregate() -> Result<()> {
- let config = SessionConfig::new();
- let ctx = SessionContext::with_config(config);
- register_aggregate_csv(&ctx).await?;
- let sql = "SELECT
- c8,
- ROW_NUMBER() OVER(ORDER BY c9) AS rn1,
- ROW_NUMBER() OVER(ORDER BY c9 ROWS BETWEEN 10 PRECEDING and 1 FOLLOWING) as rn2
- FROM aggregate_test_100
- ORDER BY c8
- LIMIT 5";
-
- let actual = execute_to_batches(&ctx, sql).await;
- let expected = vec![
- "+-----+-----+-----+",
- "| c8 | rn1 | rn2 |",
- "+-----+-----+-----+",
- "| 102 | 73 | 73 |",
- "| 299 | 1 | 1 |",
- "| 363 | 41 | 41 |",
- "| 417 | 14 | 14 |",
- "| 794 | 95 | 95 |",
- "+-----+-----+-----+",
- ];
- assert_batches_eq!(expected, &actual);
- Ok(())
-}
-
-#[tokio::test]
-async fn test_window_range_equivalent_frames() -> Result<()> {
- let config = SessionConfig::new();
- let ctx = SessionContext::with_config(config);
- register_aggregate_csv(&ctx).await?;
- let sql = "SELECT
- c9,
- COUNT(*) OVER(ORDER BY c9, c1 RANGE BETWEEN CURRENT ROW AND CURRENT ROW) AS cnt1,
- COUNT(*) OVER(ORDER BY c9, c1 RANGE UNBOUNDED PRECEDING) AS cnt2,
- COUNT(*) OVER(ORDER BY c9, c1 RANGE CURRENT ROW) AS cnt3,
- COUNT(*) OVER(RANGE BETWEEN CURRENT ROW AND CURRENT ROW) AS cnt4,
- COUNT(*) OVER(RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS cnt5,
- COUNT(*) OVER(RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) AS cnt6
- FROM aggregate_test_100
- ORDER BY c9
- LIMIT 5";
-
- let actual = execute_to_batches(&ctx, sql).await;
- let expected = vec![
- "+-----------+------+------+------+------+------+------+",
- "| c9 | cnt1 | cnt2 | cnt3 | cnt4 | cnt5 | cnt6 |",
- "+-----------+------+------+------+------+------+------+",
- "| 28774375 | 1 | 1 | 1 | 100 | 100 | 100 |",
- "| 63044568 | 1 | 2 | 1 | 100 | 100 | 100 |",
- "| 141047417 | 1 | 3 | 1 | 100 | 100 | 100 |",
- "| 141680161 | 1 | 4 | 1 | 100 | 100 | 100 |",
- "| 145294611 | 1 | 5 | 1 | 100 | 100 | 100 |",
- "+-----------+------+------+------+------+------+------+",
- ];
- assert_batches_eq!(expected, &actual);
- Ok(())
-}
-
-#[tokio::test]
-async fn test_window_cume_dist() -> Result<()> {
- let config = SessionConfig::new();
- let ctx = SessionContext::with_config(config);
- register_aggregate_csv(&ctx).await?;
- let sql = "SELECT
- c8,
- CUME_DIST() OVER(ORDER BY c9) as cd1,
- CUME_DIST() OVER(ORDER BY c9 ROWS BETWEEN 10 PRECEDING and 1 FOLLOWING) as cd2
- FROM aggregate_test_100
- ORDER BY c8
- LIMIT 5";
-
- let actual = execute_to_batches(&ctx, sql).await;
- let expected = vec![
- "+-----+------+------+",
- "| c8 | cd1 | cd2 |",
- "+-----+------+------+",
- "| 102 | 0.73 | 0.73 |",
- "| 299 | 0.01 | 0.01 |",
- "| 363 | 0.41 | 0.41 |",
- "| 417 | 0.14 | 0.14 |",
- "| 794 | 0.95 | 0.95 |",
- "+-----+------+------+",
- ];
- assert_batches_eq!(expected, &actual);
- Ok(())
-}
-
-#[tokio::test]
-async fn test_window_rank() -> Result<()> {
- let config = SessionConfig::new();
- let ctx = SessionContext::with_config(config);
- register_aggregate_csv(&ctx).await?;
- let sql = "SELECT
- c9,
- RANK() OVER(ORDER BY c1) AS rank1,
- RANK() OVER(ORDER BY c1 ROWS BETWEEN 10 PRECEDING and 1 FOLLOWING) as rank2,
- DENSE_RANK() OVER(ORDER BY c1) as dense_rank1,
- DENSE_RANK() OVER(ORDER BY c1 ROWS BETWEEN 10 PRECEDING and 1 FOLLOWING) as dense_rank2,
- PERCENT_RANK() OVER(ORDER BY c1) as percent_rank1,
- PERCENT_RANK() OVER(ORDER BY c1 ROWS BETWEEN 10 PRECEDING and 1 FOLLOWING) as percent_rank2
- FROM aggregate_test_100
- ORDER BY c9
- LIMIT 5";
-
- let actual = execute_to_batches(&ctx, sql).await;
- let expected = vec![
- "+-----------+-------+-------+-------------+-------------+---------------------+---------------------+",
- "| c9 | rank1 | rank2 | dense_rank1 | dense_rank2 | percent_rank1 | percent_rank2 |",
- "+-----------+-------+-------+-------------+-------------+---------------------+---------------------+",
- "| 28774375 | 80 | 80 | 5 | 5 | 0.797979797979798 | 0.797979797979798 |",
- "| 63044568 | 62 | 62 | 4 | 4 | 0.6161616161616161 | 0.6161616161616161 |",
- "| 141047417 | 1 | 1 | 1 | 1 | 0.0 | 0.0 |",
- "| 141680161 | 41 | 41 | 3 | 3 | 0.40404040404040403 | 0.40404040404040403 |",
- "| 145294611 | 1 | 1 | 1 | 1 | 0.0 | 0.0 |",
- "+-----------+-------+-------+-------------+-------------+---------------------+---------------------+",
- ];
- assert_batches_eq!(expected, &actual);
- Ok(())
-}
-
-#[tokio::test]
-async fn test_lag_lead() -> Result<()> {
- let config = SessionConfig::new();
- let ctx = SessionContext::with_config(config);
- register_aggregate_csv(&ctx).await?;
- let sql = "SELECT
- c9,
- LAG(c9, 2, 10101) OVER(ORDER BY c9) as lag1,
- LAG(c9, 2, 10101) OVER(ORDER BY c9 ROWS BETWEEN 10 PRECEDING and 1 FOLLOWING) as lag2,
- LEAD(c9, 2, 10101) OVER(ORDER BY c9) as lead1,
- LEAD(c9, 2, 10101) OVER(ORDER BY c9 ROWS BETWEEN 10 PRECEDING and 1 FOLLOWING) as lead2
- FROM aggregate_test_100
- ORDER BY c9
- LIMIT 5";
-
- let actual = execute_to_batches(&ctx, sql).await;
- let expected = vec![
- "+-----------+-----------+-----------+-----------+-----------+",
- "| c9 | lag1 | lag2 | lead1 | lead2 |",
- "+-----------+-----------+-----------+-----------+-----------+",
- "| 28774375 | 10101 | 10101 | 141047417 | 141047417 |",
- "| 63044568 | 10101 | 10101 | 141680161 | 141680161 |",
- "| 141047417 | 28774375 | 28774375 | 145294611 | 145294611 |",
- "| 141680161 | 63044568 | 63044568 | 225513085 | 225513085 |",
- "| 145294611 | 141047417 | 141047417 | 243203849 | 243203849 |",
- "+-----------+-----------+-----------+-----------+-----------+",
- ];
- assert_batches_eq!(expected, &actual);
- Ok(())
-}
-
-#[tokio::test]
-async fn test_window_frame_first_value_last_value_aggregate() -> Result<()> {
- let config = SessionConfig::new();
- let ctx = SessionContext::with_config(config);
- register_aggregate_csv(&ctx).await?;
-
- let sql = "SELECT
- FIRST_VALUE(c4) OVER(ORDER BY c9 ASC ROWS BETWEEN 10 PRECEDING AND 1 FOLLOWING) as first_value1,
- FIRST_VALUE(c4) OVER(ORDER BY c9 ASC ROWS BETWEEN 2 PRECEDING AND 3 FOLLOWING) as first_value2,
- LAST_VALUE(c4) OVER(ORDER BY c9 ASC ROWS BETWEEN 10 PRECEDING AND 1 FOLLOWING) as last_value1,
- LAST_VALUE(c4) OVER(ORDER BY c9 ASC ROWS BETWEEN 2 PRECEDING AND 3 FOLLOWING) as last_value2
- FROM aggregate_test_100
- ORDER BY c9
- LIMIT 5";
-
- let actual = execute_to_batches(&ctx, sql).await;
- let expected = vec![
- "+--------------+--------------+-------------+-------------+",
- "| first_value1 | first_value2 | last_value1 | last_value2 |",
- "+--------------+--------------+-------------+-------------+",
- "| -16110 | -16110 | 3917 | -1114 |",
- "| -16110 | -16110 | -16974 | 15673 |",
- "| -16110 | -16110 | -1114 | 13630 |",
- "| -16110 | 3917 | 15673 | -13217 |",
- "| -16110 | -16974 | 13630 | 20690 |",
- "+--------------+--------------+-------------+-------------+",
- ];
- assert_batches_eq!(expected, &actual);
- Ok(())
-}
-
-#[tokio::test]
-async fn test_window_frame_nth_value_aggregate() -> Result<()> {
- let config = SessionConfig::new();
- let ctx = SessionContext::with_config(config);
- register_aggregate_csv(&ctx).await?;
-
- let sql = "SELECT
- NTH_VALUE(c4, 3) OVER(ORDER BY c9 ASC ROWS BETWEEN 2 PRECEDING AND 1 FOLLOWING) as nth_value1,
- NTH_VALUE(c4, 2) OVER(ORDER BY c9 ASC ROWS BETWEEN 1 PRECEDING AND 3 FOLLOWING) as nth_value2
- FROM aggregate_test_100
- ORDER BY c9
- LIMIT 5";
-
- let actual = execute_to_batches(&ctx, sql).await;
- let expected = vec![
- "+------------+------------+",
- "| nth_value1 | nth_value2 |",
- "+------------+------------+",
- "| | 3917 |",
- "| -16974 | 3917 |",
- "| -16974 | -16974 |",
- "| -1114 | -1114 |",
- "| 15673 | 15673 |",
- "+------------+------------+",
- ];
- assert_batches_eq!(expected, &actual);
- Ok(())
-}
-
#[tokio::test]
async fn test_window_agg_sort() -> Result<()> {
// We need to specify the target partition number.
diff --git a/datafusion/core/tests/sqllogictests/test_files/window.slt b/datafusion/core/tests/sqllogictests/test_files/window.slt
index 88be8adc71..a7054d4ae3 100644
--- a/datafusion/core/tests/sqllogictests/test_files/window.slt
+++ b/datafusion/core/tests/sqllogictests/test_files/window.slt
@@ -376,11 +376,6 @@ SortPreservingMergeExec: [b@0 ASC NULLS LAST]
EmptyExec: produce_one_row=true
-
-
-
-
-
# check actual result
query TII
@@ -409,12 +404,12 @@ bb 7 2
# async fn window_in_expression
-query I
+query I rowsort
select 1 - lag(amount, 1) over (order by idx) as column1 from (values ('a', 1, 100), ('a', 2, 150)) as t (col1, idx, amount)
---
----
-NULL
-99
+NULL
# async fn window_with_agg_in_expression
@@ -702,3 +697,496 @@ SELECT
543 543 488 543 488 488 543 96 4555 96 4555 4555 96 96 4555 96 543 543 4627 543 4627 4627 543 96 96 4555 96 4555 4555 96 4555 4555 96 4555 96 96 4555
553 553 543 553 543 543 553 115 4531 115 4531 4531 115 115 4531 115 553 553 4627 553 4627 4627 553 115 115 4531 115 4531 4531 115 4531 4531 115 4531 115 115 4531
553 553 553 553 553 553 553 140 4512 140 4512 4512 140 140 4512 140 553 553 4627 553 4627 4627 553 140 140 4512 140 4512 4512 140 4512 4512 140 4512 140 140 4512
+
+
+
+# window_frame_rows_preceding_with_unique_partition
+query II
+SELECT
+SUM(c4) OVER(PARTITION BY c1 ORDER BY c9 ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING),
+COUNT(*) OVER(PARTITION BY c1 ORDER BY c9 ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)
+FROM aggregate_test_100
+ORDER BY c9
+LIMIT 5
+----
+-38611 2
+17547 2
+-1301 2
+26638 2
+26861 3
+
+
+#fn window_frame_ranges_preceding_following
+statement error DataFusion error: Internal error: Operator \- is not implemented for types
+SELECT
+SUM(c4) OVER(ORDER BY c2 RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING),
+SUM(c3) OVER(ORDER BY c2 RANGE BETWEEN 10000 PRECEDING AND 10000 FOLLOWING),
+COUNT(*) OVER(ORDER BY c2 RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING)
+FROM aggregate_test_100
+ORDER BY c9
+LIMIT 5
+
+
+#fn window_frame_ranges_ntile
+query II
+SELECT
+NTILE(8) OVER (ORDER BY C4) as ntile1,
+NTILE(12) OVER (ORDER BY C12 DESC) as ntile2
+FROM aggregate_test_100
+ORDER BY c7
+LIMIT 5
+----
+8 12
+5 11
+3 11
+2 7
+7 12
+
+#fn window_frame_ranges_string_check
+query II
+SELECT
+SUM(LENGTH(c13)) OVER(ORDER BY c13),
+SUM(LENGTH(c1)) OVER(ORDER BY c1)
+FROM aggregate_test_100
+ORDER BY c9
+LIMIT 5
+----
+2100 100
+510 79
+1440 21
+1830 61
+2010 21
+
+
+#fn window_frame_order_by_unique
+query II
+SELECT
+SUM(c5) OVER (ORDER BY c5) as sum1,
+COUNT(*) OVER (ORDER BY c9) as count1
+FROM aggregate_test_100
+ORDER BY c9
+LIMIT 5
+----
+-49877765574 1
+-50025861694 2
+-45402230071 3
+-14557735645 4
+-18365391649 5
+
+
+# /// If the sorting columns have non unique values, the unstable sorting may produce
+# /// indeterminate results. Therefore, we are commenting out the following test for now.
+# ///
+# // #[tokio::test]
+# // async fn window_frame_order_by_non_unique
+# // let ctx = SessionContext::new();
+# // register_aggregate_csv(&ctx).await?;
+# // let sql = "SELECT \
+# // c2, \
+# // c9, \
+# // SUM(c5) OVER (ORDER BY c2), \
+# // COUNT(*) OVER (ORDER BY c2) \
+# // FROM aggregate_test_100 \
+# // ORDER BY c2 \
+# // LIMIT 5";
+# // let actual = execute_to_batches(&ctx, sql).await;
+# // let expected = vec![
+# // "+----+------------+----------------------------+-----------------+",
+# // "| c2 | c9 | SUM(aggregate_test_100.c5) | COUNT(UInt8(1)) |",
+# // "+----+------------+----------------------------+-----------------+",
+# // "| 1 | 879082834 | -438598674 | 22 |",
+# // "| 1 | 3542840110 | -438598674 | 22 |",
+# // "| 1 | 3275293996 | -438598674 | 22 |",
+# // "| 1 | 774637006 | -438598674 | 22 |",
+# // "| 1 | 4015442341 | -438598674 | 22 |",
+# // "+----+------------+----------------------------+-----------------+",
+# // ];
+# // assert_batches_eq!(expected, &actual);
+# // Ok(())
+# // }
+
+#fn window_frame_ranges_unbounded_preceding_following
+query II
+SELECT
+SUM(c2) OVER (ORDER BY c2 RANGE BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING) as sum1,
+COUNT(*) OVER (ORDER BY c2 RANGE BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING) as cnt1
+FROM aggregate_test_100
+ORDER BY c9
+LIMIT 5
+----
+285 100
+123 63
+285 100
+123 63
+123 63
+
+
+#fn window_frame_ranges_preceding_and_preceding
+query II
+SELECT
+SUM(c2) OVER (ORDER BY c2 RANGE BETWEEN 3 PRECEDING AND 1 PRECEDING),
+COUNT(*) OVER (ORDER BY c2 RANGE BETWEEN 3 PRECEDING AND 1 PRECEDING)
+FROM aggregate_test_100
+ORDER BY c9
+LIMIT 5
+----
+123 63
+22 22
+193 64
+22 22
+22 22
+
+#fn window_frame_ranges_unbounded_preceding_following_diff_col
+query II
+SELECT
+SUM(c2) OVER (ORDER BY c2 RANGE BETWEEN CURRENT ROW AND 1 FOLLOWING),
+COUNT(*) OVER (ORDER BY c2 RANGE BETWEEN CURRENT ROW AND 1 FOLLOWING)
+FROM aggregate_test_100
+ORDER BY c9
+LIMIT 5
+----
+162 37
+101 41
+70 14
+101 41
+101 41
+
+#fn window_frame_partition_by_order_by_desc
+query I
+SELECT
+SUM(c4) OVER(PARTITION BY c1 ORDER BY c2 DESC RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING)
+FROM aggregate_test_100
+ORDER BY c9
+LIMIT 5
+----
+-124618
+205080
+-40819
+-19517
+47246
+
+
+#fn window_frame_range_float
+query R
+SELECT
+ SUM(c12) OVER (ORDER BY C12 RANGE BETWEEN 0.2 PRECEDING AND 0.2 FOLLOWING)
+ FROM aggregate_test_100
+ ORDER BY C9
+ LIMIT 5
+----
+2.547670180363
+10.629941254821
+2.547670180363
+20.349518503437
+21.408674363508
+
+#fn window_frame_ranges_timestamp
+
+statement ok
+create table temp as values
+(1664264591000000000),
+(1664264592000000000),
+(1664264592000000000),
+(1664264593000000000),
+(1664264594000000000),
+(1664364594000000000),
+(1664464594000000000),
+(1664564594000000000);
+
+statement ok
+create table t as select cast(column1 as timestamp) as ts from temp;
+
+query PIII
+SELECT
+ ts,
+ COUNT(*) OVER (ORDER BY ts RANGE BETWEEN INTERVAL '1' DAY PRECEDING AND INTERVAL '2 DAY' FOLLOWING) AS cnt1,
+ COUNT(*) OVER (ORDER BY ts RANGE BETWEEN '0 DAY' PRECEDING AND '0' DAY FOLLOWING) as cnt2,
+ COUNT(*) OVER (ORDER BY ts RANGE BETWEEN '5' SECOND PRECEDING AND CURRENT ROW) as cnt3
+ FROM t
+ ORDER BY ts
+----
+2022-09-27T07:43:11 6 1 1
+2022-09-27T07:43:12 6 2 3
+2022-09-27T07:43:12 6 2 3
+2022-09-27T07:43:13 6 1 4
+2022-09-27T07:43:14 6 1 5
+2022-09-28T11:29:54 2 1 1
+2022-09-29T15:16:34 2 1 1
+2022-09-30T19:03:14 1 1 1
+
+statement ok
+drop table t
+
+statement ok
+drop table temp
+
+
+#fn window_frame_ranges_unbounded_preceding_err
+statement error DataFusion error: Execution error: Invalid window frame: end bound cannot be unbounded preceding
+SELECT
+SUM(c2) OVER (ORDER BY c2 RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED PRECEDING),
+COUNT(*) OVER (ORDER BY c2 RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED PRECEDING)
+FROM aggregate_test_100
+ORDER BY c9
+LIMIT 5
+
+
+#fn window_frame_groups_preceding_following_desc
+query III
+SELECT
+SUM(c4) OVER(ORDER BY c2 DESC GROUPS BETWEEN 1 PRECEDING AND 1 FOLLOWING),
+SUM(c3) OVER(ORDER BY c2 DESC GROUPS BETWEEN 10000 PRECEDING AND 10000 FOLLOWING),
+COUNT(*) OVER(ORDER BY c2 DESC GROUPS BETWEEN 1 PRECEDING AND 1 FOLLOWING)
+FROM aggregate_test_100
+ORDER BY c9
+LIMIT 5
+----
+52276 781 56
+260620 781 63
+-28623 781 37
+260620 781 63
+260620 781 63
+
+#fn window_frame_groups_order_by_null_desc
+query I
+SELECT
+COUNT(c2) OVER (ORDER BY c1 DESC GROUPS BETWEEN 5 PRECEDING AND 3 FOLLOWING)
+FROM null_cases
+LIMIT 5
+----
+12
+12
+12
+12
+12
+
+#fn window_frame_groups
+query IIIIIIIIIIIIIIIIIIIIII
+SELECT
+SUM(c1) OVER (ORDER BY c3 GROUPS BETWEEN 9 PRECEDING AND 11 FOLLOWING) as a,
+SUM(c1) OVER (ORDER BY c3 DESC GROUPS BETWEEN 9 PRECEDING AND 11 FOLLOWING) as b,
+SUM(c1) OVER (ORDER BY c3 NULLS first GROUPS BETWEEN 9 PRECEDING AND 11 FOLLOWING) as c,
+SUM(c1) OVER (ORDER BY c3 DESC NULLS last GROUPS BETWEEN 9 PRECEDING AND 11 FOLLOWING) as d,
+SUM(c1) OVER (ORDER BY c3 DESC NULLS first GROUPS BETWEEN 9 PRECEDING AND 11 FOLLOWING) as e,
+SUM(c1) OVER (ORDER BY c3 NULLS first GROUPS BETWEEN 9 PRECEDING AND 11 FOLLOWING) as f,
+SUM(c1) OVER (ORDER BY c3 GROUPS current row) as a1,
+SUM(c1) OVER (ORDER BY c3 GROUPS BETWEEN 9 PRECEDING AND 5 PRECEDING) as a2,
+SUM(c1) OVER (ORDER BY c3 GROUPS BETWEEN UNBOUNDED PRECEDING AND 5 PRECEDING) as a3,
+SUM(c1) OVER (ORDER BY c3 GROUPS BETWEEN UNBOUNDED PRECEDING AND 11 FOLLOWING) as a4,
+SUM(c1) OVER (ORDER BY c3 GROUPS BETWEEN UNBOUNDED PRECEDING AND current row) as a5,
+SUM(c1) OVER (ORDER BY c3 GROUPS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) as a6,
+SUM(c1) OVER (ORDER BY c3 GROUPS BETWEEN 1 PRECEDING AND UNBOUNDED FOLLOWING) as a7,
+SUM(c1) OVER (ORDER BY c3 GROUPS BETWEEN 3 FOLLOWING AND UNBOUNDED FOLLOWING) as a8,
+SUM(c1) OVER (ORDER BY c3 GROUPS BETWEEN current row AND UNBOUNDED FOLLOWING) as a9,
+SUM(c1) OVER (ORDER BY c3 GROUPS BETWEEN current row AND 3 FOLLOWING) as a10,
+SUM(c1) OVER (ORDER BY c3 GROUPS BETWEEN 5 FOLLOWING AND 7 FOLLOWING) as a11,
+SUM(c1) OVER (ORDER BY c3 DESC GROUPS current row) as a21,
+SUM(c1) OVER (ORDER BY c3 NULLS first GROUPS BETWEEN 9 PRECEDING AND 5 PRECEDING) as a22,
+SUM(c1) OVER (ORDER BY c3 DESC NULLS last GROUPS BETWEEN UNBOUNDED PRECEDING AND 5 PRECEDING) as a23,
+SUM(c1) OVER (ORDER BY c3 NULLS last GROUPS BETWEEN UNBOUNDED PRECEDING AND 11 FOLLOWING) as a24,
+SUM(c1) OVER (ORDER BY c3 DESC NULLS first GROUPS BETWEEN UNBOUNDED PRECEDING AND current row) as a25
+FROM null_cases
+ORDER BY c3
+LIMIT 10
+----
+412 307 412 307 307 412 NULL NULL NULL 412 NULL 4627 4627 4531 4627 115 85 NULL NULL 4487 412 4627
+488 339 488 339 339 488 72 NULL NULL 488 72 4627 4627 4512 4627 140 153 72 NULL 4473 488 4627
+543 412 543 412 412 543 24 NULL NULL 543 96 4627 4627 4487 4555 82 122 24 NULL 4442 543 4555
+553 488 553 488 488 553 19 NULL NULL 553 115 4627 4555 4473 4531 89 114 19 NULL 4402 553 4531
+553 543 553 543 543 553 25 NULL NULL 553 140 4627 4531 4442 4512 110 105 25 NULL 4320 553 4512
+591 553 591 553 553 591 14 NULL NULL 591 154 4627 4512 4402 4487 167 181 14 NULL 4320 591 4487
+651 553 651 553 553 651 31 72 72 651 185 4627 4487 4320 4473 153 204 31 72 4288 651 4473
+662 591 662 591 591 662 40 96 96 662 225 4627 4473 4320 4442 154 141 40 96 4215 662 4442
+697 651 697 651 651 697 82 115 115 697 307 4627 4442 4288 4402 187 65 82 115 4139 697 4402
+758 662 758 662 662 758 NULL 140 140 758 307 4627 4402 4215 4320 181 48 NULL 140 4084 758 4320
+
+#fn window_frame_groups_multiple_order_columns
+query IIIIIIIIIIIIIIIII
+SELECT
+SUM(c1) OVER (ORDER BY c2, c3 GROUPS BETWEEN 9 PRECEDING AND 11 FOLLOWING) as a,
+SUM(c1) OVER (ORDER BY c2, c3 DESC GROUPS BETWEEN 9 PRECEDING AND 11 FOLLOWING) as b,
+SUM(c1) OVER (ORDER BY c2, c3 NULLS first GROUPS BETWEEN 9 PRECEDING AND 11 FOLLOWING) as c,
+SUM(c1) OVER (ORDER BY c2, c3 DESC NULLS last GROUPS BETWEEN 9 PRECEDING AND 11 FOLLOWING) as d,
+SUM(c1) OVER (ORDER BY c2, c3 DESC NULLS first GROUPS BETWEEN 9 PRECEDING AND 11 FOLLOWING) as e,
+SUM(c1) OVER (ORDER BY c2, c3 NULLS first GROUPS BETWEEN 9 PRECEDING AND 11 FOLLOWING) as f,
+SUM(c1) OVER (ORDER BY c2, c3 GROUPS current row) as a1,
+SUM(c1) OVER (ORDER BY c2, c3 GROUPS BETWEEN 9 PRECEDING AND 5 PRECEDING) as a2,
+SUM(c1) OVER (ORDER BY c2, c3 GROUPS BETWEEN UNBOUNDED PRECEDING AND 5 PRECEDING) as a3,
+SUM(c1) OVER (ORDER BY c2, c3 GROUPS BETWEEN UNBOUNDED PRECEDING AND 11 FOLLOWING) as a4,
+SUM(c1) OVER (ORDER BY c2, c3 GROUPS BETWEEN UNBOUNDED PRECEDING AND current row) as a5,
+SUM(c1) OVER (ORDER BY c2, c3 GROUPS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) as a6,
+SUM(c1) OVER (ORDER BY c2, c3 GROUPS BETWEEN 1 PRECEDING AND UNBOUNDED FOLLOWING) as a7,
+SUM(c1) OVER (ORDER BY c2, c3 GROUPS BETWEEN 3 FOLLOWING AND UNBOUNDED FOLLOWING) as a8,
+SUM(c1) OVER (ORDER BY c2, c3 GROUPS BETWEEN current row AND UNBOUNDED FOLLOWING) as a9,
+SUM(c1) OVER (ORDER BY c2, c3 GROUPS BETWEEN current row AND 3 FOLLOWING) as a10,
+SUM(c1) OVER (ORDER BY c2, c3 GROUPS BETWEEN 5 FOLLOWING AND 7 FOLLOWING) as a11
+FROM null_cases
+ORDER BY c3
+LIMIT 10
+----
+818 910 818 910 910 818 NULL 249 249 818 432 4627 4234 4157 4195 98 82
+537 979 537 979 979 537 72 NULL NULL 537 210 4627 4569 4378 4489 169 55
+811 838 811 838 838 811 24 221 3075 3665 3311 4627 1390 1276 1340 117 144
+763 464 763 464 464 763 19 168 3572 4167 3684 4627 962 829 962 194 80
+552 964 552 964 964 552 25 NULL NULL 552 235 4627 4489 4320 4417 167 39
+963 930 963 930 930 963 14 201 818 1580 1098 4627 3638 3455 3543 177 224
+1113 814 1113 814 814 1113 31 415 2653 3351 2885 4627 1798 1694 1773 165 162
+780 868 780 868 868 780 40 258 3143 3665 3351 4627 1340 1223 1316 117 102
+740 466 740 466 466 740 82 164 3592 4168 3766 4627 962 768 943 244 122
+772 832 772 832 832 772 NULL 277 3189 3684 3351 4627 1316 1199 1276 119 64
+
+#fn window_frame_groups_without_order_by
+# Try executing an erroneous query (the ORDER BY clause is missing in the
+# window frame):
+statement error Error during planning: GROUPS mode requires an ORDER BY clause
+SELECT
+ SUM(c4) OVER(PARTITION BY c2 GROUPS BETWEEN 1 PRECEDING AND 1 FOLLOWING)
+ FROM aggregate_test_100
+ ORDER BY c9;
+
+
+#fn window_frame_lag
+statement error This feature is not implemented: There is only support Literal types for field at idx: 1 in Window Function
+SELECT c2,
+ lag(c2, c2, c2) OVER () as lag1
+ FROM aggregate_test_100;
+
+
+#fn window_frame_creation
+statement error DataFusion error: Error during planning: Invalid window frame: start bound \(1 PRECEDING\) cannot be larger than end bound \(2 PRECEDING\)
+SELECT
+ COUNT(c1) OVER (ORDER BY c2 RANGE BETWEEN 1 PRECEDING AND 2 PRECEDING)
+ FROM aggregate_test_100
+
+statement error DataFusion error: Error during planning: Invalid window frame: start bound \(2 FOLLOWING\) cannot be larger than end bound \(1 FOLLOWING\)
+SELECT
+ COUNT(c1) OVER (ORDER BY c2 RANGE BETWEEN 2 FOLLOWING AND 1 FOLLOWING)
+ FROM aggregate_test_100
+
+statement error DataFusion error: Error during planning: GROUPS mode requires an ORDER BY clause
+SELECT
+ COUNT(c1) OVER(GROUPS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
+ FROM aggregate_test_100
+
+
+#fn test_window_row_number_aggregate
+query III
+SELECT
+ c8,
+ ROW_NUMBER() OVER(ORDER BY c9) AS rn1,
+ ROW_NUMBER() OVER(ORDER BY c9 ROWS BETWEEN 10 PRECEDING and 1 FOLLOWING) as rn2
+ FROM aggregate_test_100
+ ORDER BY c8
+ LIMIT 5
+----
+102 73 73
+299 1 1
+363 41 41
+417 14 14
+794 95 95
+
+#fn test_window_range_equivalent_frames
+query IIIIIII
+SELECT
+ c9,
+ COUNT(*) OVER(ORDER BY c9, c1 RANGE BETWEEN CURRENT ROW AND CURRENT ROW) AS cnt1,
+ COUNT(*) OVER(ORDER BY c9, c1 RANGE UNBOUNDED PRECEDING) AS cnt2,
+ COUNT(*) OVER(ORDER BY c9, c1 RANGE CURRENT ROW) AS cnt3,
+ COUNT(*) OVER(RANGE BETWEEN CURRENT ROW AND CURRENT ROW) AS cnt4,
+ COUNT(*) OVER(RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS cnt5,
+ COUNT(*) OVER(RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) AS cnt6
+ FROM aggregate_test_100
+ ORDER BY c9
+ LIMIT 5
+----
+28774375 1 1 1 100 100 100
+63044568 1 2 1 100 100 100
+141047417 1 3 1 100 100 100
+141680161 1 4 1 100 100 100
+145294611 1 5 1 100 100 100
+
+#fn test_window_cume_dist
+query IRR
+SELECT
+ c8,
+ CUME_DIST() OVER(ORDER BY c9) as cd1,
+ CUME_DIST() OVER(ORDER BY c9 ROWS BETWEEN 10 PRECEDING and 1 FOLLOWING) as cd2
+ FROM aggregate_test_100
+ ORDER BY c8
+ LIMIT 5
+----
+102 0.73 0.73
+299 0.01 0.01
+363 0.41 0.41
+417 0.14 0.14
+794 0.95 0.95
+
+#fn test_window_rank
+query IIIIIRR
+SELECT
+ c9,
+ RANK() OVER(ORDER BY c1) AS rank1,
+ RANK() OVER(ORDER BY c1 ROWS BETWEEN 10 PRECEDING and 1 FOLLOWING) as rank2,
+ DENSE_RANK() OVER(ORDER BY c1) as dense_rank1,
+ DENSE_RANK() OVER(ORDER BY c1 ROWS BETWEEN 10 PRECEDING and 1 FOLLOWING) as dense_rank2,
+ PERCENT_RANK() OVER(ORDER BY c1) as percent_rank1,
+ PERCENT_RANK() OVER(ORDER BY c1 ROWS BETWEEN 10 PRECEDING and 1 FOLLOWING) as percent_rank2
+ FROM aggregate_test_100
+ ORDER BY c9
+ LIMIT 5
+----
+28774375 80 80 5 5 0.79797979798 0.79797979798
+63044568 62 62 4 4 0.616161616162 0.616161616162
+141047417 1 1 1 1 0 0
+141680161 41 41 3 3 0.40404040404 0.40404040404
+145294611 1 1 1 1 0 0
+
+#fn test_lag_lead
+query IIIII
+SELECT
+ c9,
+ LAG(c9, 2, 10101) OVER(ORDER BY c9) as lag1,
+ LAG(c9, 2, 10101) OVER(ORDER BY c9 ROWS BETWEEN 10 PRECEDING and 1 FOLLOWING) as lag2,
+ LEAD(c9, 2, 10101) OVER(ORDER BY c9) as lead1,
+ LEAD(c9, 2, 10101) OVER(ORDER BY c9 ROWS BETWEEN 10 PRECEDING and 1 FOLLOWING) as lead2
+ FROM aggregate_test_100
+ ORDER BY c9
+ LIMIT 5
+----
+28774375 10101 10101 141047417 141047417
+63044568 10101 10101 141680161 141680161
+141047417 28774375 28774375 145294611 145294611
+141680161 63044568 63044568 225513085 225513085
+145294611 141047417 141047417 243203849 243203849
+
+#fn test_window_frame_first_value_last_value_aggregate
+query IIII
+SELECT
+ FIRST_VALUE(c4) OVER(ORDER BY c9 ASC ROWS BETWEEN 10 PRECEDING AND 1 FOLLOWING) as first_value1,
+ FIRST_VALUE(c4) OVER(ORDER BY c9 ASC ROWS BETWEEN 2 PRECEDING AND 3 FOLLOWING) as first_value2,
+ LAST_VALUE(c4) OVER(ORDER BY c9 ASC ROWS BETWEEN 10 PRECEDING AND 1 FOLLOWING) as last_value1,
+ LAST_VALUE(c4) OVER(ORDER BY c9 ASC ROWS BETWEEN 2 PRECEDING AND 3 FOLLOWING) as last_value2
+ FROM aggregate_test_100
+ ORDER BY c9
+ LIMIT 5
+----
+-16110 -16110 3917 -1114
+-16110 -16110 -16974 15673
+-16110 -16110 -1114 13630
+-16110 3917 15673 -13217
+-16110 -16974 13630 20690
+
+#fn test_window_frame_nth_value_aggregate
+query II
+SELECT
+ NTH_VALUE(c4, 3) OVER(ORDER BY c9 ASC ROWS BETWEEN 2 PRECEDING AND 1 FOLLOWING) as nth_value1,
+ NTH_VALUE(c4, 2) OVER(ORDER BY c9 ASC ROWS BETWEEN 1 PRECEDING AND 3 FOLLOWING) as nth_value2
+ FROM aggregate_test_100
+ ORDER BY c9
+ LIMIT 5
+----
+NULL 3917
+-16974 3917
+-16974 -16974
+-1114 -1114
+15673 15673