Files
greptimedb/tests/cases/standalone/common/window/window_null_handling.sql
dennis zhuang 9dbf6dd8d0 test: migrate duckdb tests part2, window functions (#6875)
* test: migrate window tests

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* fix: blank line at the end

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

---------

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>
2025-09-03 06:55:47 +00:00

34 lines
1.2 KiB
SQL

-- Migrated from DuckDB test: Multiple window tests with NULL handling
-- Tests window functions with NULL values
CREATE TABLE null_test("id" INTEGER, val INTEGER, ts TIMESTAMP TIME INDEX);
INSERT INTO null_test VALUES
(1, 100, 1000), (2, NULL, 2000), (3, 300, 3000), (4, NULL, 4000), (5, 500, 5000);
-- Window functions with NULL values
SELECT "id", val,
ROW_NUMBER() OVER (ORDER BY val NULLS LAST) as row_num,
RANK() OVER (ORDER BY val NULLS LAST) as rank_val
FROM null_test ORDER BY "id";
-- LEAD/LAG with NULL values
SELECT "id", val,
LAG(val, 1) OVER (ORDER BY "id") as prev_val,
LEAD(val, 1) OVER (ORDER BY "id") as next_val
FROM null_test ORDER BY "id";
-- Aggregate window functions with NULL
SELECT "id", val,
SUM(val) OVER (ORDER BY "id" ROWS UNBOUNDED PRECEDING) as running_sum,
COUNT(val) OVER (ORDER BY "id" ROWS UNBOUNDED PRECEDING) as running_count
FROM null_test ORDER BY "id";
-- FIRST_VALUE/LAST_VALUE with NULL
SELECT "id", val,
FIRST_VALUE(val) OVER (ORDER BY "id" ROWS UNBOUNDED PRECEDING) as first_val,
LAST_VALUE(val) OVER (ORDER BY "id" ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as last_val
FROM null_test ORDER BY "id";
DROP TABLE null_test;