MCPcopy Create free account
hub / github.com/PostHog/duckgres / TestDQLWindowFunctions

Function TestDQLWindowFunctions

tests/integration/dql_test.go:362–410  ·  view source on GitHub ↗

TestDQLWindowFunctions tests window functions

(t *testing.T)

Source from the content-addressed store, hash-verified

360
361// TestDQLWindowFunctions tests window functions
362func TestDQLWindowFunctions(t *testing.T) {
363 tests := []QueryTest{
364 // ROW_NUMBER, RANK, DENSE_RANK
365 {Name: "row_number", Query: "SELECT *, ROW_NUMBER() OVER (ORDER BY age) FROM users"},
366 {Name: "rank", Query: "SELECT *, RANK() OVER (ORDER BY score DESC) FROM users"},
367 {Name: "dense_rank", Query: "SELECT *, DENSE_RANK() OVER (ORDER BY score DESC) FROM users"},
368 {Name: "ntile", Query: "SELECT *, NTILE(4) OVER (ORDER BY score DESC) FROM users"},
369
370 // Partitioned windows
371 {Name: "row_number_partitioned", Query: "SELECT *, ROW_NUMBER() OVER (PARTITION BY active ORDER BY score DESC) FROM users"},
372 {Name: "rank_partitioned", Query: "SELECT *, RANK() OVER (PARTITION BY active ORDER BY score DESC) FROM users"},
373
374 // Aggregate window functions
375 {Name: "sum_window", Query: "SELECT *, SUM(amount) OVER (ORDER BY sale_date) AS running_sum FROM sales"},
376 {Name: "avg_window", Query: "SELECT *, AVG(amount) OVER (ORDER BY sale_date) AS running_avg FROM sales"},
377 {Name: "count_partitioned", Query: "SELECT *, COUNT(*) OVER (PARTITION BY region) AS region_count FROM sales"},
378
379 // Window frame specification
380 {Name: "window_rows_unbounded", Query: "SELECT *, SUM(amount) OVER (ORDER BY sale_date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM sales"},
381 {Name: "window_rows_range", Query: "SELECT *, AVG(amount) OVER (ORDER BY sale_date ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) FROM sales"},
382 {Name: "window_rows_all", Query: "SELECT *, SUM(amount) OVER (ORDER BY sale_date ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM sales"},
383
384 // LAG, LEAD
385 {Name: "lag", Query: "SELECT *, LAG(amount) OVER (ORDER BY sale_date) AS prev_amount FROM sales"},
386 {Name: "lag_offset", Query: "SELECT *, LAG(amount, 2) OVER (ORDER BY sale_date) AS prev_2_amount FROM sales"},
387 {Name: "lag_default", Query: "SELECT *, LAG(amount, 1, 0) OVER (ORDER BY sale_date) AS prev_amount FROM sales"},
388 {Name: "lead", Query: "SELECT *, LEAD(amount) OVER (ORDER BY sale_date) AS next_amount FROM sales"},
389
390 // FIRST_VALUE, LAST_VALUE, NTH_VALUE
391 {Name: "first_value", Query: "SELECT *, FIRST_VALUE(amount) OVER (PARTITION BY region ORDER BY sale_date) FROM sales"},
392 {Name: "last_value", Query: "SELECT *, LAST_VALUE(amount) OVER (PARTITION BY region ORDER BY sale_date ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM sales"},
393 {Name: "nth_value", Query: "SELECT *, NTH_VALUE(amount, 2) OVER (ORDER BY sale_date) FROM sales"},
394
395 // Named windows
396 {Name: "named_window", Query: `
397 SELECT *,
398 SUM(amount) OVER w AS sum,
399 AVG(amount) OVER w AS avg,
400 COUNT(*) OVER w AS cnt
401 FROM sales
402 WINDOW w AS (PARTITION BY region ORDER BY sale_date)
403 `},
404
405 // PERCENT_RANK, CUME_DIST
406 {Name: "percent_rank", Query: "SELECT *, PERCENT_RANK() OVER (ORDER BY score) FROM users"},
407 {Name: "cume_dist", Query: "SELECT *, CUME_DIST() OVER (ORDER BY score) FROM users"},
408 }
409 runQueryTests(t, tests)
410}
411
412// TestDQLValues tests VALUES clause
413func TestDQLValues(t *testing.T) {

Callers

nothing calls this directly

Calls 1

runQueryTestsFunction · 0.85

Tested by

no test coverage detected