# Adapted from the window logic test.

exec-ddl
CREATE TABLE kv (
  k INT PRIMARY KEY,
  v INT,
  w INT,
  f FLOAT,
  d DECIMAL,
  s STRING,
  b BOOL,
  FAMILY (k, v, w, f, b),
  FAMILY (d),
  FAMILY (s)
)
----

build
SELECT * FROM kv GROUP BY v, rank() OVER ()
----
error (42P20): rank(): window functions are not allowed in GROUP BY

build
SELECT sum(rank() over ()) FROM kv
----
error (42P20): sum(): rank(): window functions are not allowed in aggregate

build
SELECT rank() OVER () FROM kv GROUP BY 1
----
error (42P20): rank(): window functions are not allowed in GROUP BY

build
SELECT count(w) OVER () FROM kv GROUP BY 1
----
error (42P20): count(): window functions are not allowed in GROUP BY

build
SELECT avg(avg(k) OVER ()) OVER () FROM kv
----
error (42P20): avg(): avg(): window function calls cannot be nested

build
INSERT INTO kv (k, v) VALUES (99, 100) RETURNING sum(v) OVER ()
----
error (42P20): sum(): window functions are not allowed in RETURNING

build
SELECT sum(v) FROM kv GROUP BY k LIMIT sum(v) OVER ()
----
error (42703): column "v" does not exist

build
SELECT sum(v) FROM kv GROUP BY k LIMIT 1 OFFSET sum(v) OVER ()
----
error (42703): column "v" does not exist

build
INSERT INTO kv (k, v) VALUES (99, count(1) OVER ())
----
error (42P20): count(): window functions are not allowed in VALUES

build
SELECT k FROM kv WHERE avg(k) OVER () > 1
----
error (42P20): avg(): window functions are not allowed in WHERE

build
SELECT avg(DISTINCT k) OVER () FROM kv
----
error (0A000): DISTINCT is not implemented for window functions

build
SELECT 1 FROM kv GROUP BY 1 HAVING sum(1) OVER (PARTITION BY 1) > 1
----
error (42P20): sum(): window functions are not allowed in HAVING

build
SELECT lag('foo'::string) OVER (), lag(1) OVER () FROM kv
----
project
 ├── columns: lag:8(string) lag:9(int)
 └── window partition=()
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) lag:8(string) lag:9(int) lag_1_arg1:10(string) lag_1_arg2:11(int!null) lag_1_arg3:12(string) lag_2_arg3:13(int)
      ├── project
      │    ├── columns: lag_1_arg1:10(string) lag_1_arg2:11(int!null) lag_1_arg3:12(string) lag_2_arg3:13(int) k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      │    ├── scan kv
      │    │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      │    └── projections
      │         ├── cast: STRING [type=string]
      │         │    └── const: 'foo' [type=string]
      │         ├── const: 1 [type=int]
      │         ├── cast: STRING [type=string]
      │         │    └── null [type=unknown]
      │         └── cast: INT8 [type=int]
      │              └── null [type=unknown]
      └── windows
           ├── lag [type=string]
           │    ├── variable: lag_1_arg1 [type=string]
           │    ├── variable: lag_1_arg2 [type=int]
           │    └── variable: lag_1_arg3 [type=string]
           └── lag [type=int]
                ├── variable: lag_1_arg2 [type=int]
                ├── variable: lag_1_arg2 [type=int]
                └── variable: lag_2_arg3 [type=int]

build
SELECT count(*) OVER () FROM kv
----
project
 ├── columns: count:8(int)
 └── window partition=()
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) count_rows:8(int)
      ├── scan kv
      │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      └── windows
           └── count-rows [type=int]

build
SELECT lag((SELECT k FROM kv kv2 WHERE kv2.k = kv.k)) OVER () FROM kv
----
project
 ├── columns: lag:15(int)
 └── window partition=()
      ├── columns: kv.k:1(int!null) kv.v:2(int) kv.w:3(int) kv.f:4(float) kv.d:5(decimal) kv.s:6(string) kv.b:7(bool) lag:15(int) lag_1_arg1:16(int) lag_1_arg2:17(int!null) lag_1_arg3:18(int)
      ├── project
      │    ├── columns: lag_1_arg1:16(int) lag_1_arg2:17(int!null) lag_1_arg3:18(int) kv.k:1(int!null) kv.v:2(int) kv.w:3(int) kv.f:4(float) kv.d:5(decimal) kv.s:6(string) kv.b:7(bool)
      │    ├── scan kv
      │    │    └── columns: kv.k:1(int!null) kv.v:2(int) kv.w:3(int) kv.f:4(float) kv.d:5(decimal) kv.s:6(string) kv.b:7(bool)
      │    └── projections
      │         ├── subquery [type=int]
      │         │    └── max1-row
      │         │         ├── columns: kv2.k:8(int!null)
      │         │         └── project
      │         │              ├── columns: kv2.k:8(int!null)
      │         │              └── select
      │         │                   ├── columns: kv2.k:8(int!null) kv2.v:9(int) kv2.w:10(int) kv2.f:11(float) kv2.d:12(decimal) kv2.s:13(string) kv2.b:14(bool)
      │         │                   ├── scan kv2
      │         │                   │    └── columns: kv2.k:8(int!null) kv2.v:9(int) kv2.w:10(int) kv2.f:11(float) kv2.d:12(decimal) kv2.s:13(string) kv2.b:14(bool)
      │         │                   └── filters
      │         │                        └── eq [type=bool]
      │         │                             ├── variable: kv2.k [type=int]
      │         │                             └── variable: kv.k [type=int]
      │         ├── const: 1 [type=int]
      │         └── cast: INT8 [type=int]
      │              └── null [type=unknown]
      └── windows
           └── lag [type=int]
                ├── variable: lag_1_arg1 [type=int]
                ├── variable: lag_1_arg2 [type=int]
                └── variable: lag_1_arg3 [type=int]

build
SELECT lag(1) OVER (), lead(1) OVER () FROM kv
----
project
 ├── columns: lag:8(int) lead:9(int)
 └── window partition=()
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) lag:8(int) lead:9(int) lag_1_arg1:10(int!null) lag_1_arg3:11(int)
      ├── project
      │    ├── columns: lag_1_arg1:10(int!null) lag_1_arg3:11(int) k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      │    ├── scan kv
      │    │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      │    └── projections
      │         ├── const: 1 [type=int]
      │         └── cast: INT8 [type=int]
      │              └── null [type=unknown]
      └── windows
           ├── lag [type=int]
           │    ├── variable: lag_1_arg1 [type=int]
           │    ├── variable: lag_1_arg1 [type=int]
           │    └── variable: lag_1_arg3 [type=int]
           └── lead [type=int]
                ├── variable: lag_1_arg1 [type=int]
                ├── variable: lag_1_arg1 [type=int]
                └── variable: lag_1_arg3 [type=int]

build
SELECT
    lag(1) OVER (PARTITION BY k, v),
    lag(1) OVER (PARTITION BY k),
    lag(1) OVER (PARTITION BY v),
    lead(1) OVER (PARTITION BY k),
    lead(1) OVER (PARTITION BY v)
FROM kv
----
project
 ├── columns: lag:8(int) lag:9(int) lag:10(int) lead:11(int) lead:12(int)
 └── window partition=(2)
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) lag:8(int) lag:9(int) lag:10(int) lead:11(int) lead:12(int) lag_1_arg1:13(int!null) lag_1_arg3:14(int)
      ├── window partition=(1)
      │    ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) lag:8(int) lag:9(int) lead:11(int) lag_1_arg1:13(int!null) lag_1_arg3:14(int)
      │    ├── window partition=(1,2)
      │    │    ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) lag:8(int) lag_1_arg1:13(int!null) lag_1_arg3:14(int)
      │    │    ├── project
      │    │    │    ├── columns: lag_1_arg1:13(int!null) lag_1_arg3:14(int) k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      │    │    │    ├── scan kv
      │    │    │    │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      │    │    │    └── projections
      │    │    │         ├── const: 1 [type=int]
      │    │    │         └── cast: INT8 [type=int]
      │    │    │              └── null [type=unknown]
      │    │    └── windows
      │    │         └── lag [type=int]
      │    │              ├── variable: lag_1_arg1 [type=int]
      │    │              ├── variable: lag_1_arg1 [type=int]
      │    │              └── variable: lag_1_arg3 [type=int]
      │    └── windows
      │         ├── lag [type=int]
      │         │    ├── variable: lag_1_arg1 [type=int]
      │         │    ├── variable: lag_1_arg1 [type=int]
      │         │    └── variable: lag_1_arg3 [type=int]
      │         └── lead [type=int]
      │              ├── variable: lag_1_arg1 [type=int]
      │              ├── variable: lag_1_arg1 [type=int]
      │              └── variable: lag_1_arg3 [type=int]
      └── windows
           ├── lag [type=int]
           │    ├── variable: lag_1_arg1 [type=int]
           │    ├── variable: lag_1_arg1 [type=int]
           │    └── variable: lag_1_arg3 [type=int]
           └── lead [type=int]
                ├── variable: lag_1_arg1 [type=int]
                ├── variable: lag_1_arg1 [type=int]
                └── variable: lag_1_arg3 [type=int]

build
SELECT
    lag(1) OVER (PARTITION BY k, v),
    lag(1) OVER (PARTITION BY k ORDER BY v),
    lag(1) OVER (PARTITION BY v ORDER BY f),
    lead(1) OVER (PARTITION BY k ORDER BY v),
    lead(1) OVER (PARTITION BY v)
FROM kv
----
project
 ├── columns: lag:8(int) lag:9(int) lag:10(int) lead:11(int) lead:12(int)
 └── window partition=(2)
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) lag:8(int) lag:9(int) lag:10(int) lead:11(int) lead:12(int) lag_1_arg1:13(int!null) lag_1_arg3:14(int)
      ├── window partition=(2) ordering=+4
      │    ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) lag:8(int) lag:9(int) lag:10(int) lead:11(int) lag_1_arg1:13(int!null) lag_1_arg3:14(int)
      │    ├── window partition=(1) ordering=+2
      │    │    ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) lag:8(int) lag:9(int) lead:11(int) lag_1_arg1:13(int!null) lag_1_arg3:14(int)
      │    │    ├── window partition=(1,2)
      │    │    │    ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) lag:8(int) lag_1_arg1:13(int!null) lag_1_arg3:14(int)
      │    │    │    ├── project
      │    │    │    │    ├── columns: lag_1_arg1:13(int!null) lag_1_arg3:14(int) k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      │    │    │    │    ├── scan kv
      │    │    │    │    │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      │    │    │    │    └── projections
      │    │    │    │         ├── const: 1 [type=int]
      │    │    │    │         └── cast: INT8 [type=int]
      │    │    │    │              └── null [type=unknown]
      │    │    │    └── windows
      │    │    │         └── lag [type=int]
      │    │    │              ├── variable: lag_1_arg1 [type=int]
      │    │    │              ├── variable: lag_1_arg1 [type=int]
      │    │    │              └── variable: lag_1_arg3 [type=int]
      │    │    └── windows
      │    │         ├── lag [type=int]
      │    │         │    ├── variable: lag_1_arg1 [type=int]
      │    │         │    ├── variable: lag_1_arg1 [type=int]
      │    │         │    └── variable: lag_1_arg3 [type=int]
      │    │         └── lead [type=int]
      │    │              ├── variable: lag_1_arg1 [type=int]
      │    │              ├── variable: lag_1_arg1 [type=int]
      │    │              └── variable: lag_1_arg3 [type=int]
      │    └── windows
      │         └── lag [type=int]
      │              ├── variable: lag_1_arg1 [type=int]
      │              ├── variable: lag_1_arg1 [type=int]
      │              └── variable: lag_1_arg3 [type=int]
      └── windows
           └── lead [type=int]
                ├── variable: lag_1_arg1 [type=int]
                ├── variable: lag_1_arg1 [type=int]
                └── variable: lag_1_arg3 [type=int]

build
SELECT lag(1, 2) OVER (), lead(1, 2) OVER () FROM kv
----
project
 ├── columns: lag:8(int) lead:9(int)
 └── window partition=()
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) lag:8(int) lead:9(int) lag_1_arg1:10(int!null) lag_1_arg2:11(int!null) lag_1_arg3:12(int)
      ├── project
      │    ├── columns: lag_1_arg1:10(int!null) lag_1_arg2:11(int!null) lag_1_arg3:12(int) k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      │    ├── scan kv
      │    │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      │    └── projections
      │         ├── const: 1 [type=int]
      │         ├── const: 2 [type=int]
      │         └── cast: INT8 [type=int]
      │              └── null [type=unknown]
      └── windows
           ├── lag [type=int]
           │    ├── variable: lag_1_arg1 [type=int]
           │    ├── variable: lag_1_arg2 [type=int]
           │    └── variable: lag_1_arg3 [type=int]
           └── lead [type=int]
                ├── variable: lag_1_arg1 [type=int]
                ├── variable: lag_1_arg2 [type=int]
                └── variable: lag_1_arg3 [type=int]

build
SELECT lag(1, 2, 3) OVER (), lead(1, 2, 3) OVER () FROM kv
----
project
 ├── columns: lag:8(int) lead:9(int)
 └── window partition=()
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) lag:8(int) lead:9(int) lag_1_arg1:10(int!null) lag_1_arg2:11(int!null) lag_1_arg3:12(int!null)
      ├── project
      │    ├── columns: lag_1_arg1:10(int!null) lag_1_arg2:11(int!null) lag_1_arg3:12(int!null) k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      │    ├── scan kv
      │    │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      │    └── projections
      │         ├── const: 1 [type=int]
      │         ├── const: 2 [type=int]
      │         └── const: 3 [type=int]
      └── windows
           ├── lag [type=int]
           │    ├── variable: lag_1_arg1 [type=int]
           │    ├── variable: lag_1_arg2 [type=int]
           │    └── variable: lag_1_arg3 [type=int]
           └── lead [type=int]
                ├── variable: lag_1_arg1 [type=int]
                ├── variable: lag_1_arg2 [type=int]
                └── variable: lag_1_arg3 [type=int]

build
SELECT avg(k) OVER () FROM kv
----
project
 ├── columns: avg:8(decimal)
 └── window partition=()
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) avg:8(decimal)
      ├── scan kv
      │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      └── windows
           └── avg [type=decimal]
                └── variable: k [type=int]

build
SELECT x FROM (SELECT avg(k) OVER () AS x FROM kv)
----
project
 ├── columns: x:8(decimal)
 └── window partition=()
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) avg:8(decimal)
      ├── scan kv
      │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      └── windows
           └── avg [type=decimal]
                └── variable: k [type=int]

build
SELECT avg(DISTINCT k) OVER () FROM kv
----
error (0A000): DISTINCT is not implemented for window functions

build
SELECT avg(k) FILTER (WHERE k > 5) OVER () FROM kv
----
project
 ├── columns: avg:8(decimal)
 └── window partition=()
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) avg:8(decimal) avg_1_filter:9(bool)
      ├── project
      │    ├── columns: avg_1_filter:9(bool) k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      │    ├── scan kv
      │    │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      │    └── projections
      │         └── gt [type=bool]
      │              ├── variable: k [type=int]
      │              └── const: 5 [type=int]
      └── windows
           └── agg-filter [type=decimal]
                ├── avg [type=decimal]
                │    └── variable: k [type=int]
                └── variable: avg_1_filter [type=bool]

build
SELECT avg(k), max(v) OVER () FROM kv ORDER BY 1
----
error (42803): column "v" must appear in the GROUP BY clause or be used in an aggregate function

build
SELECT avg(k) OVER () FROM kv ORDER BY 1
----
sort
 ├── columns: avg:8(decimal)
 ├── ordering: +8
 └── project
      ├── columns: avg:8(decimal)
      └── window partition=()
           ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) avg:8(decimal)
           ├── scan kv
           │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
           └── windows
                └── avg [type=decimal]
                     └── variable: k [type=int]

build
SELECT k, v, rank() OVER w FROM kv WINDOW w AS ()
----
project
 ├── columns: k:1(int!null) v:2(int) rank:8(int)
 └── window partition=()
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) rank:8(int)
      ├── scan kv
      │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      └── windows
           └── rank [type=int]

build
SELECT k, v, first_value(v) OVER () FROM kv
----
project
 ├── columns: k:1(int!null) v:2(int) first_value:8(int)
 └── window partition=()
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) first_value:8(int)
      ├── scan kv
      │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      └── windows
           └── first-value [type=int]
                └── variable: v [type=int]

build
SELECT avg(k), max(v), min(w), 2 + row_number() OVER () FROM kv ORDER BY 1
----
project
 ├── columns: avg:8(decimal) max:9(int) min:10(int) "?column?":12(int)
 ├── ordering: +8
 ├── window partition=()
 │    ├── columns: avg:8(decimal) max:9(int) min:10(int) row_number:11(int)
 │    ├── scalar-group-by
 │    │    ├── columns: avg:8(decimal) max:9(int) min:10(int)
 │    │    ├── project
 │    │    │    ├── columns: k:1(int!null) v:2(int) w:3(int)
 │    │    │    └── scan kv
 │    │    │         └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
 │    │    └── aggregations
 │    │         ├── avg [type=decimal]
 │    │         │    └── variable: k [type=int]
 │    │         ├── max [type=int]
 │    │         │    └── variable: v [type=int]
 │    │         └── min [type=int]
 │    │              └── variable: w [type=int]
 │    └── windows
 │         └── row-number [type=int]
 └── projections
      └── plus [type=int]
           ├── const: 2 [type=int]
           └── variable: row_number [type=int]

build
SELECT k, rank() OVER (), dense_rank() OVER (), percent_rank() OVER (), cume_dist() OVER () FROM kv
----
project
 ├── columns: k:1(int!null) rank:8(int) dense_rank:9(int) percent_rank:10(float) cume_dist:11(float)
 └── window partition=()
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) rank:8(int) dense_rank:9(int) percent_rank:10(float) cume_dist:11(float)
      ├── scan kv
      │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      └── windows
           ├── rank [type=int]
           ├── dense-rank [type=int]
           ├── percent-rank [type=float]
           └── cume-dist [type=float]

build
SELECT k, rank() OVER (), rank() OVER () FROM kv
----
project
 ├── columns: k:1(int!null) rank:8(int) rank:8(int)
 └── window partition=()
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) rank:8(int)
      ├── scan kv
      │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      └── windows
           └── rank [type=int]

build
SELECT k, rank() OVER (), row_number() OVER () FROM kv
----
project
 ├── columns: k:1(int!null) rank:8(int) row_number:9(int)
 └── window partition=()
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) rank:8(int) row_number:9(int)
      ├── scan kv
      │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      └── windows
           ├── rank [type=int]
           └── row-number [type=int]

build
SELECT k, rank() OVER (), row_number() OVER () FROM kv ORDER BY 1
----
sort
 ├── columns: k:1(int!null) rank:8(int) row_number:9(int)
 ├── ordering: +1
 └── project
      ├── columns: k:1(int!null) rank:8(int) row_number:9(int)
      └── window partition=()
           ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) rank:8(int) row_number:9(int)
           ├── scan kv
           │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
           └── windows
                ├── rank [type=int]
                └── row-number [type=int]

build
SELECT k, v, rank() OVER (PARTITION BY v) FROM kv ORDER BY 1
----
sort
 ├── columns: k:1(int!null) v:2(int) rank:8(int)
 ├── ordering: +1
 └── project
      ├── columns: k:1(int!null) v:2(int) rank:8(int)
      └── window partition=(2)
           ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) rank:8(int)
           ├── scan kv
           │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
           └── windows
                └── rank [type=int]

build
SELECT k, row_number() OVER (PARTITION BY v), rank() OVER (PARTITION BY v) FROM kv ORDER BY 1
----
sort
 ├── columns: k:1(int!null) row_number:8(int) rank:9(int)
 ├── ordering: +1
 └── project
      ├── columns: k:1(int!null) row_number:8(int) rank:9(int)
      └── window partition=(2)
           ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) row_number:8(int) rank:9(int)
           ├── scan kv
           │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
           └── windows
                ├── row-number [type=int]
                └── rank [type=int]

build
SELECT k, v, ntile(1) OVER () FROM kv
----
project
 ├── columns: k:1(int!null) v:2(int) ntile:8(int)
 └── window partition=()
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) ntile:8(int) ntile_1_arg1:9(int!null)
      ├── project
      │    ├── columns: ntile_1_arg1:9(int!null) k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      │    ├── scan kv
      │    │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      │    └── projections
      │         └── const: 1 [type=int]
      └── windows
           └── ntile [type=int]
                └── variable: ntile_1_arg1 [type=int]

build
SELECT k, v, ntile(1) OVER (), ntile(50) OVER () FROM kv
----
project
 ├── columns: k:1(int!null) v:2(int) ntile:8(int) ntile:9(int)
 └── window partition=()
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) ntile:8(int) ntile:9(int) ntile_1_arg1:10(int!null) ntile_2_arg1:11(int!null)
      ├── project
      │    ├── columns: ntile_1_arg1:10(int!null) ntile_2_arg1:11(int!null) k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      │    ├── scan kv
      │    │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      │    └── projections
      │         ├── const: 1 [type=int]
      │         └── const: 50 [type=int]
      └── windows
           ├── ntile [type=int]
           │    └── variable: ntile_1_arg1 [type=int]
           └── ntile [type=int]
                └── variable: ntile_2_arg1 [type=int]

build
SELECT k, v, nth_value('foo', 1) OVER () FROM kv
----
project
 ├── columns: k:1(int!null) v:2(int) nth_value:8(string)
 └── window partition=()
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) nth_value:8(string) nth_value_1_arg1:9(string!null) nth_value_1_arg2:10(int!null)
      ├── project
      │    ├── columns: nth_value_1_arg1:9(string!null) nth_value_1_arg2:10(int!null) k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      │    ├── scan kv
      │    │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      │    └── projections
      │         ├── const: 'foo' [type=string]
      │         └── const: 1 [type=int]
      └── windows
           └── nth-value [type=string]
                ├── variable: nth_value_1_arg1 [type=string]
                └── variable: nth_value_1_arg2 [type=int]

build
SELECT k, v, nth_value(1, k) OVER () FROM kv
----
project
 ├── columns: k:1(int!null) v:2(int) nth_value:8(int)
 └── window partition=()
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) nth_value:8(int) nth_value_1_arg1:9(int!null)
      ├── project
      │    ├── columns: nth_value_1_arg1:9(int!null) k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      │    ├── scan kv
      │    │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      │    └── projections
      │         └── const: 1 [type=int]
      └── windows
           └── nth-value [type=int]
                ├── variable: nth_value_1_arg1 [type=int]
                └── variable: k [type=int]

# Partitions

build
SELECT row_number() OVER (PARTITION BY rank() OVER ()) FROM kv
----
error (42P20): rank(): window function calls cannot be nested

build
SELECT row_number() OVER (PARTITION BY rank() OVER () + 1) FROM kv
----
error (42P20): rank(): window function calls cannot be nested

build
SELECT row_number() OVER (PARTITION BY v) FROM kv
----
project
 ├── columns: row_number:8(int)
 └── window partition=(2)
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) row_number:8(int)
      ├── scan kv
      │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      └── windows
           └── row-number [type=int]

build
SELECT v, row_number() OVER (PARTITION BY v) FROM kv
----
project
 ├── columns: v:2(int) row_number:8(int)
 └── window partition=(2)
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) row_number:8(int)
      ├── scan kv
      │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      └── windows
           └── row-number [type=int]

build
SELECT v, row_number() OVER (PARTITION BY v+1) FROM kv
----
project
 ├── columns: v:2(int) row_number:8(int)
 └── window partition=(9)
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) row_number:8(int) row_number_1_partition_1:9(int)
      ├── project
      │    ├── columns: row_number_1_partition_1:9(int) k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      │    ├── scan kv
      │    │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      │    └── projections
      │         └── plus [type=int]
      │              ├── variable: v [type=int]
      │              └── const: 1 [type=int]
      └── windows
           └── row-number [type=int]

build
SELECT v, row_number() OVER (PARTITION BY avg(k)) FROM kv GROUP BY v
----
project
 ├── columns: v:2(int) row_number:9(int)
 └── window partition=(8)
      ├── columns: v:2(int) avg:8(decimal) row_number:9(int)
      ├── group-by
      │    ├── columns: v:2(int) avg:8(decimal)
      │    ├── grouping columns: v:2(int)
      │    ├── project
      │    │    ├── columns: k:1(int!null) v:2(int)
      │    │    └── scan kv
      │    │         └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      │    └── aggregations
      │         └── avg [type=decimal]
      │              └── variable: k [type=int]
      └── windows
           └── row-number [type=int]

# TODO(justin): expand these tuples.
build
SELECT k, row_number() OVER (PARTITION BY (k, v)) FROM kv
----
project
 ├── columns: k:1(int!null) row_number:8(int)
 └── window partition=(1,2)
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) row_number:8(int)
      ├── scan kv
      │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      └── windows
           └── row-number [type=int]

build
SELECT k, row_number() OVER (PARTITION BY kv.*) FROM kv
----
project
 ├── columns: k:1(int!null) row_number:8(int)
 └── window partition=(1-7)
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) row_number:8(int)
      ├── scan kv
      │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      └── windows
           └── row-number [type=int]

build
SELECT row_number() OVER (PARTITION BY v), rank() OVER (PARTITION BY v, f) FROM kv
----
project
 ├── columns: row_number:8(int) rank:9(int)
 └── window partition=(2,4)
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) row_number:8(int) rank:9(int)
      ├── window partition=(2)
      │    ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) row_number:8(int)
      │    ├── scan kv
      │    │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      │    └── windows
      │         └── row-number [type=int]
      └── windows
           └── rank [type=int]

# Ordering

build
SELECT row_number() OVER (ORDER BY v) FROM kv
----
project
 ├── columns: row_number:8(int)
 └── window partition=() ordering=+2
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) row_number:8(int)
      ├── scan kv
      │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      └── windows
           └── row-number [type=int]

build
SELECT k, v, rank() OVER (ORDER BY k) FROM kv ORDER BY 1
----
sort
 ├── columns: k:1(int!null) v:2(int) rank:8(int)
 ├── ordering: +1
 └── project
      ├── columns: k:1(int!null) v:2(int) rank:8(int)
      └── window partition=() ordering=+1
           ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) rank:8(int)
           ├── scan kv
           │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
           └── windows
                └── rank [type=int]

# Ensure tuples in orderings get expanded.

build
SELECT
  k,
  v,
  rank() OVER (ORDER BY (v, w)),
  row_number() OVER (ORDER BY (v, w), f),
  dense_rank() OVER (ORDER BY f, (v, w))
FROM kv ORDER BY 1
----
sort
 ├── columns: k:1(int!null) v:2(int) rank:8(int) row_number:9(int) dense_rank:10(int)
 ├── ordering: +1
 └── project
      ├── columns: k:1(int!null) v:2(int) rank:8(int) row_number:9(int) dense_rank:10(int)
      └── window partition=() ordering=+4,+2,+3
           ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) rank:8(int) row_number:9(int) dense_rank:10(int)
           ├── window partition=() ordering=+2,+3,+4
           │    ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) rank:8(int) row_number:9(int)
           │    ├── window partition=() ordering=+2,+3
           │    │    ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) rank:8(int)
           │    │    ├── scan kv
           │    │    │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
           │    │    └── windows
           │    │         └── rank [type=int]
           │    └── windows
           │         └── row-number [type=int]
           └── windows
                └── dense-rank [type=int]

build
SELECT k, v, w, v - w + 2 + row_number() OVER (PARTITION BY v, k ORDER BY w) FROM kv ORDER BY 1
----
sort
 ├── columns: k:1(int!null) v:2(int) w:3(int) "?column?":9(int)
 ├── ordering: +1
 └── project
      ├── columns: "?column?":9(int) k:1(int!null) v:2(int) w:3(int)
      ├── window partition=(1,2) ordering=+3
      │    ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) row_number:8(int)
      │    ├── scan kv
      │    │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      │    └── windows
      │         └── row-number [type=int]
      └── projections
           └── plus [type=int]
                ├── plus [type=int]
                │    ├── minus [type=int]
                │    │    ├── variable: v [type=int]
                │    │    └── variable: w [type=int]
                │    └── const: 2 [type=int]
                └── variable: row_number [type=int]

build
SELECT
    DISTINCT ON (row_number() OVER (ORDER BY (k, v))) w
FROM
    kv
ORDER BY
    row_number() OVER (ORDER BY (k, v)) DESC
----
distinct-on
 ├── columns: w:3(int)  [hidden: row_number:8(int)]
 ├── grouping columns: row_number:8(int)
 ├── ordering: -8
 ├── sort
 │    ├── columns: w:3(int) row_number:8(int)
 │    ├── ordering: -8
 │    └── project
 │         ├── columns: w:3(int) row_number:8(int)
 │         └── window partition=() ordering=+1,+2
 │              ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) row_number:8(int)
 │              ├── scan kv
 │              │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
 │              └── windows
 │                   └── row-number [type=int]
 └── aggregations
      └── first-agg [type=int]
           └── variable: w [type=int]

# Frames

# Default is unbounded to current-row
build
SELECT avg(k) OVER () FROM kv
----
project
 ├── columns: avg:8(decimal)
 └── window partition=()
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) avg:8(decimal)
      ├── scan kv
      │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      └── windows
           └── avg [type=decimal]
                └── variable: k [type=int]

build
SELECT avg(k) OVER (RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED PRECEDING) FROM kv
----
error (42601): at or near "preceding": syntax error: frame end cannot be UNBOUNDED PRECEDING

build
SELECT avg(k) OVER (RANGE BETWEEN UNBOUNDED FOLLOWING AND UNBOUNDED FOLLOWING) FROM kv
----
error (42601): at or near "following": syntax error: frame start cannot be UNBOUNDED FOLLOWING

build
SELECT
  avg(k) OVER (RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING),
  avg(k) OVER (RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW),
  avg(k) OVER (RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING),
  avg(k) OVER (RANGE BETWEEN CURRENT ROW AND CURRENT ROW)
FROM kv
----
project
 ├── columns: avg:8(decimal) avg:9(decimal) avg:10(decimal) avg:11(decimal)
 └── window partition=()
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) avg:8(decimal) avg:9(decimal) avg:10(decimal) avg:11(decimal)
      ├── scan kv
      │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      └── windows
           ├── windows-item: range from unbounded to unbounded [type=decimal]
           │    └── avg [type=decimal]
           │         └── variable: k [type=int]
           ├── avg [type=decimal]
           │    └── variable: k [type=int]
           ├── windows-item: range from current-row to unbounded [type=decimal]
           │    └── avg [type=decimal]
           │         └── variable: k [type=int]
           └── windows-item: range from current-row to current-row [type=decimal]
                └── avg [type=decimal]
                     └── variable: k [type=int]

build
SELECT
  avg(k) OVER (ORDER BY v RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING),
  avg(k) OVER (ORDER BY v RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW),
  avg(k) OVER (ORDER BY v RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING),
  avg(k) OVER (ORDER BY v RANGE BETWEEN CURRENT ROW AND CURRENT ROW)
FROM kv
----
project
 ├── columns: avg:8(decimal) avg:9(decimal) avg:10(decimal) avg:11(decimal)
 └── window partition=() ordering=+2
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) avg:8(decimal) avg:9(decimal) avg:10(decimal) avg:11(decimal)
      ├── scan kv
      │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      └── windows
           ├── windows-item: range from unbounded to unbounded [type=decimal]
           │    └── avg [type=decimal]
           │         └── variable: k [type=int]
           ├── avg [type=decimal]
           │    └── variable: k [type=int]
           ├── windows-item: range from current-row to unbounded [type=decimal]
           │    └── avg [type=decimal]
           │         └── variable: k [type=int]
           └── windows-item: range from current-row to current-row [type=decimal]
                └── avg [type=decimal]
                     └── variable: k [type=int]

build
SELECT
  avg(k) OVER (RANGE UNBOUNDED PRECEDING),
  avg(k) OVER (RANGE CURRENT ROW)
FROM kv
----
project
 ├── columns: avg:8(decimal) avg:9(decimal)
 └── window partition=()
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) avg:8(decimal) avg:9(decimal)
      ├── scan kv
      │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      └── windows
           ├── avg [type=decimal]
           │    └── variable: k [type=int]
           └── windows-item: range from current-row to current-row [type=decimal]
                └── avg [type=decimal]
                     └── variable: k [type=int]

# TODO(justin): add support for offset expressions.
build
SELECT avg(k) OVER (ORDER BY v RANGE BETWEEN k - 10 PRECEDING AND CURRENT ROW) FROM kv
----
error (42P10): argument of RANGE must not contain variables

build
SELECT avg(k) OVER (ORDER BY v RANGE BETWEEN UNBOUNDED PRECEDING AND 10 FOLLOWING) FROM kv
----
project
 ├── columns: avg:8(decimal)
 └── window partition=() ordering=+2
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) avg:8(decimal)
      ├── scan kv
      │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      └── windows
           └── windows-item: range from unbounded to offset [type=decimal]
                └── window-to-offset [type=decimal]
                     ├── avg [type=decimal]
                     │    └── variable: k [type=int]
                     └── const: 10 [type=int]

build
SELECT
    avg(v) OVER (PARTITION BY w ORDER BY f RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS avg_price
FROM kv
----
project
 ├── columns: avg_price:8(decimal)
 └── window partition=(3) ordering=+4
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) avg:8(decimal)
      ├── scan kv
      │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      └── windows
           └── windows-item: range from offset to offset [type=decimal]
                └── window-to-offset [type=decimal]
                     ├── window-from-offset [type=decimal]
                     │    ├── avg [type=decimal]
                     │    │    └── variable: v [type=int]
                     │    └── const: 1.0 [type=float]
                     └── const: 1.0 [type=float]

build
SELECT
    avg(v) OVER (PARTITION BY w ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS avg_price
FROM kv
----
project
 ├── columns: avg_price:8(decimal)
 └── window partition=(3)
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) avg:8(decimal)
      ├── scan kv
      │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      └── windows
           └── windows-item: rows from offset to offset [type=decimal]
                └── window-to-offset [type=decimal]
                     ├── window-from-offset [type=decimal]
                     │    ├── avg [type=decimal]
                     │    │    └── variable: v [type=int]
                     │    └── const: 1 [type=int]
                     └── const: 1 [type=int]

build
SELECT
    avg(v) OVER (
        PARTITION BY
            w
        ROWS
            BETWEEN (SELECT count(*) FROM kv) PRECEDING AND 1 FOLLOWING
    )
FROM
    kv
----
project
 ├── columns: avg:16(decimal)
 └── window partition=(3)
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) avg:16(decimal)
      ├── scan kv
      │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      └── windows
           └── windows-item: rows from offset to offset [type=decimal]
                └── window-to-offset [type=decimal]
                     ├── window-from-offset [type=decimal]
                     │    ├── avg [type=decimal]
                     │    │    └── variable: v [type=int]
                     │    └── subquery [type=int]
                     │         └── max1-row
                     │              ├── columns: count_rows:15(int)
                     │              └── scalar-group-by
                     │                   ├── columns: count_rows:15(int)
                     │                   ├── project
                     │                   │    └── scan kv
                     │                   │         └── columns: k:8(int!null) v:9(int) w:10(int) f:11(float) d:12(decimal) s:13(string) b:14(bool)
                     │                   └── aggregations
                     │                        └── count-rows [type=int]
                     └── const: 1 [type=int]

# TODO(justin): consider adding rules to hoist correlated subqueries in the
# OFFSET positions.
build
SELECT
    avg(v) OVER (
        PARTITION BY
            w
        ROWS
            BETWEEN (SELECT count(*) FROM kv inner_table WHERE inner_table.k = outer_table.v) PRECEDING AND 1 FOLLOWING
    )
FROM
    kv outer_table
----
error (42P10): argument of ROWS must not contain variables

build
SELECT
    avg(v) OVER (ORDER BY v GROUPS BETWEEN 1 PRECEDING AND 1 FOLLOWING)
FROM kv
----
project
 ├── columns: avg:8(decimal)
 └── window partition=() ordering=+2
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) avg:8(decimal)
      ├── scan kv
      │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      └── windows
           └── windows-item: groups from offset to offset [type=decimal]
                └── window-to-offset [type=decimal]
                     ├── window-from-offset [type=decimal]
                     │    ├── avg [type=decimal]
                     │    │    └── variable: v [type=int]
                     │    └── const: 1 [type=int]
                     └── const: 1 [type=int]

build
SELECT
    avg(v) OVER (ORDER BY k GROUPS BETWEEN 1 PRECEDING AND UNBOUNDED FOLLOWING)
FROM kv
----
project
 ├── columns: avg:8(decimal)
 └── window partition=() ordering=+1
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) avg:8(decimal)
      ├── scan kv
      │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      └── windows
           └── windows-item: groups from offset to unbounded [type=decimal]
                └── window-from-offset [type=decimal]
                     ├── avg [type=decimal]
                     │    └── variable: v [type=int]
                     └── const: 1 [type=int]

build
SELECT avg(v) OVER (ORDER BY f RANGE BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING) FROM kv
----
project
 ├── columns: avg:8(decimal)
 └── window partition=() ordering=+4
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) avg:8(decimal)
      ├── scan kv
      │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      └── windows
           └── windows-item: range from offset to unbounded [type=decimal]
                └── window-from-offset [type=decimal]
                     ├── avg [type=decimal]
                     │    └── variable: v [type=int]
                     └── const: 1.0 [type=float]

build
SELECT
  avg(v) OVER (ORDER BY f RANGE BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING),
  avg(v) OVER (ORDER BY d RANGE BETWEEN 2 FOLLOWING AND UNBOUNDED FOLLOWING)
FROM kv
----
project
 ├── columns: avg:8(decimal) avg:9(decimal)
 └── window partition=() ordering=+5
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) avg:8(decimal) avg:9(decimal)
      ├── window partition=() ordering=+4
      │    ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) avg:8(decimal)
      │    ├── scan kv
      │    │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      │    └── windows
      │         └── windows-item: range from offset to unbounded [type=decimal]
      │              └── window-from-offset [type=decimal]
      │                   ├── avg [type=decimal]
      │                   │    └── variable: v [type=int]
      │                   └── const: 1.0 [type=float]
      └── windows
           └── windows-item: range from offset to unbounded [type=decimal]
                └── window-from-offset [type=decimal]
                     ├── avg [type=decimal]
                     │    └── variable: v [type=int]
                     └── const: 2 [type=decimal]

# Ensure window functions get appropriately deduplicated.

build
SELECT
    avg(k) OVER (RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING),
    avg(k) OVER (RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW),
    avg(k) OVER (RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING),
    avg(k) OVER (RANGE BETWEEN CURRENT ROW AND CURRENT ROW),
    avg(k) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING),
    avg(k) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW),
    avg(k) OVER (ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING),
    avg(k) OVER (ROWS BETWEEN CURRENT ROW AND CURRENT ROW),
    avg(k) OVER (ORDER BY v GROUPS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING),
    avg(k) OVER (ORDER BY v GROUPS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW),
    avg(k) OVER (ORDER BY v GROUPS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING),
    avg(k) OVER (ORDER BY v GROUPS BETWEEN CURRENT ROW AND CURRENT ROW),
    1 + avg(k) OVER (RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING),
    1 + avg(k) OVER (RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW),
    1 + avg(k) OVER (RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING),
    1 + avg(k) OVER (RANGE BETWEEN CURRENT ROW AND CURRENT ROW),
    1 + avg(k) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING),
    1 + avg(k) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW),
    1 + avg(k) OVER (ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING),
    1 + avg(k) OVER (ROWS BETWEEN CURRENT ROW AND CURRENT ROW),
    1 + avg(k) OVER (ORDER BY v GROUPS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING),
    1 + avg(k) OVER (ORDER BY v GROUPS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW),
    1 + avg(k) OVER (ORDER BY v GROUPS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING),
    1 + avg(k) OVER (ORDER BY v GROUPS BETWEEN CURRENT ROW AND CURRENT ROW)
FROM
    kv
----
project
 ├── columns: avg:8(decimal) avg:9(decimal) avg:10(decimal) avg:11(decimal) avg:12(decimal) avg:13(decimal) avg:14(decimal) avg:15(decimal) avg:16(decimal) avg:17(decimal) avg:18(decimal) avg:19(decimal) "?column?":20(decimal) "?column?":21(decimal) "?column?":22(decimal) "?column?":23(decimal) "?column?":24(decimal) "?column?":25(decimal) "?column?":26(decimal) "?column?":27(decimal) "?column?":28(decimal) "?column?":29(decimal) "?column?":30(decimal) "?column?":31(decimal)
 ├── window partition=() ordering=+2
 │    ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) avg:8(decimal) avg:9(decimal) avg:10(decimal) avg:11(decimal) avg:12(decimal) avg:13(decimal) avg:14(decimal) avg:15(decimal) avg:16(decimal) avg:17(decimal) avg:18(decimal) avg:19(decimal)
 │    ├── window partition=()
 │    │    ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) avg:8(decimal) avg:9(decimal) avg:10(decimal) avg:11(decimal) avg:12(decimal) avg:13(decimal) avg:14(decimal) avg:15(decimal)
 │    │    ├── scan kv
 │    │    │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
 │    │    └── windows
 │    │         ├── windows-item: range from unbounded to unbounded [type=decimal]
 │    │         │    └── avg [type=decimal]
 │    │         │         └── variable: k [type=int]
 │    │         ├── avg [type=decimal]
 │    │         │    └── variable: k [type=int]
 │    │         ├── windows-item: range from current-row to unbounded [type=decimal]
 │    │         │    └── avg [type=decimal]
 │    │         │         └── variable: k [type=int]
 │    │         ├── windows-item: range from current-row to current-row [type=decimal]
 │    │         │    └── avg [type=decimal]
 │    │         │         └── variable: k [type=int]
 │    │         ├── windows-item: rows from unbounded to unbounded [type=decimal]
 │    │         │    └── avg [type=decimal]
 │    │         │         └── variable: k [type=int]
 │    │         ├── windows-item: rows from unbounded to current-row [type=decimal]
 │    │         │    └── avg [type=decimal]
 │    │         │         └── variable: k [type=int]
 │    │         ├── windows-item: rows from current-row to unbounded [type=decimal]
 │    │         │    └── avg [type=decimal]
 │    │         │         └── variable: k [type=int]
 │    │         └── windows-item: rows from current-row to current-row [type=decimal]
 │    │              └── avg [type=decimal]
 │    │                   └── variable: k [type=int]
 │    └── windows
 │         ├── windows-item: groups from unbounded to unbounded [type=decimal]
 │         │    └── avg [type=decimal]
 │         │         └── variable: k [type=int]
 │         ├── windows-item: groups from unbounded to current-row [type=decimal]
 │         │    └── avg [type=decimal]
 │         │         └── variable: k [type=int]
 │         ├── windows-item: groups from current-row to unbounded [type=decimal]
 │         │    └── avg [type=decimal]
 │         │         └── variable: k [type=int]
 │         └── windows-item: groups from current-row to current-row [type=decimal]
 │              └── avg [type=decimal]
 │                   └── variable: k [type=int]
 └── projections
      ├── plus [type=decimal]
      │    ├── const: 1 [type=decimal]
      │    └── variable: avg [type=decimal]
      ├── plus [type=decimal]
      │    ├── const: 1 [type=decimal]
      │    └── variable: avg [type=decimal]
      ├── plus [type=decimal]
      │    ├── const: 1 [type=decimal]
      │    └── variable: avg [type=decimal]
      ├── plus [type=decimal]
      │    ├── const: 1 [type=decimal]
      │    └── variable: avg [type=decimal]
      ├── plus [type=decimal]
      │    ├── const: 1 [type=decimal]
      │    └── variable: avg [type=decimal]
      ├── plus [type=decimal]
      │    ├── const: 1 [type=decimal]
      │    └── variable: avg [type=decimal]
      ├── plus [type=decimal]
      │    ├── const: 1 [type=decimal]
      │    └── variable: avg [type=decimal]
      ├── plus [type=decimal]
      │    ├── const: 1 [type=decimal]
      │    └── variable: avg [type=decimal]
      ├── plus [type=decimal]
      │    ├── const: 1 [type=decimal]
      │    └── variable: avg [type=decimal]
      ├── plus [type=decimal]
      │    ├── const: 1 [type=decimal]
      │    └── variable: avg [type=decimal]
      ├── plus [type=decimal]
      │    ├── const: 1 [type=decimal]
      │    └── variable: avg [type=decimal]
      └── plus [type=decimal]
           ├── const: 1 [type=decimal]
           └── variable: avg [type=decimal]

build
SELECT
    avg(k) OVER (ORDER BY v RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING),
    avg(k) OVER (ORDER BY v RANGE BETWEEN 1 PRECEDING AND UNBOUNDED FOLLOWING),
    avg(k) OVER (ORDER BY v RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING),
    avg(k) OVER (ORDER BY v RANGE BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING),
    1 + avg(k) OVER (ORDER BY v RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING),
    1 + avg(k) OVER (ORDER BY v RANGE BETWEEN 1 PRECEDING AND UNBOUNDED FOLLOWING),
    1 + avg(k) OVER (ORDER BY v RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING),
    1 + avg(k) OVER (ORDER BY v RANGE BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING)
FROM
    kv
----
project
 ├── columns: avg:8(decimal) avg:9(decimal) avg:10(decimal) avg:11(decimal) "?column?":12(decimal) "?column?":13(decimal) "?column?":14(decimal) "?column?":15(decimal)
 ├── window partition=() ordering=+2
 │    ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) avg:8(decimal) avg:9(decimal) avg:10(decimal) avg:11(decimal)
 │    ├── scan kv
 │    │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
 │    └── windows
 │         ├── windows-item: range from unbounded to unbounded [type=decimal]
 │         │    └── avg [type=decimal]
 │         │         └── variable: k [type=int]
 │         ├── windows-item: range from offset to unbounded [type=decimal]
 │         │    └── window-from-offset [type=decimal]
 │         │         ├── avg [type=decimal]
 │         │         │    └── variable: k [type=int]
 │         │         └── const: 1 [type=int]
 │         ├── windows-item: range from offset to offset [type=decimal]
 │         │    └── window-to-offset [type=decimal]
 │         │         ├── window-from-offset [type=decimal]
 │         │         │    ├── avg [type=decimal]
 │         │         │    │    └── variable: k [type=int]
 │         │         │    └── const: 1 [type=int]
 │         │         └── const: 1 [type=int]
 │         └── windows-item: range from unbounded to offset [type=decimal]
 │              └── window-to-offset [type=decimal]
 │                   ├── avg [type=decimal]
 │                   │    └── variable: k [type=int]
 │                   └── const: 1 [type=int]
 └── projections
      ├── plus [type=decimal]
      │    ├── const: 1 [type=decimal]
      │    └── variable: avg [type=decimal]
      ├── plus [type=decimal]
      │    ├── const: 1 [type=decimal]
      │    └── variable: avg [type=decimal]
      ├── plus [type=decimal]
      │    ├── const: 1 [type=decimal]
      │    └── variable: avg [type=decimal]
      └── plus [type=decimal]
           ├── const: 1 [type=decimal]
           └── variable: avg [type=decimal]

# Filter.

build
SELECT rank() FILTER (WHERE k = 1) OVER () FROM kv
----
error (42809): FILTER specified but rank() is not an aggregate function

build
SELECT avg(f) FILTER (WHERE k = 1) OVER () FROM kv
----
project
 ├── columns: avg:8(float)
 └── window partition=()
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) avg:8(float) avg_1_filter:9(bool)
      ├── project
      │    ├── columns: avg_1_filter:9(bool) k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      │    ├── scan kv
      │    │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      │    └── projections
      │         └── eq [type=bool]
      │              ├── variable: k [type=int]
      │              └── const: 1 [type=int]
      └── windows
           └── agg-filter [type=float]
                ├── avg [type=float]
                │    └── variable: f [type=float]
                └── variable: avg_1_filter [type=bool]

build
SELECT count(*) FILTER (WHERE true) OVER (), count(*) FILTER (WHERE false) OVER () FROM kv
----
project
 ├── columns: count:8(int) count:9(int)
 └── window partition=()
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) count:8(int) count:9(int) count_1_arg1:10(bool!null) count_2_filter:11(bool!null)
      ├── project
      │    ├── columns: count_1_arg1:10(bool!null) count_2_filter:11(bool!null) k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      │    ├── scan kv
      │    │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      │    └── projections
      │         ├── true [type=bool]
      │         └── false [type=bool]
      └── windows
           ├── agg-filter [type=int]
           │    ├── count [type=int]
           │    │    └── variable: count_1_arg1 [type=bool]
           │    └── variable: count_1_arg1 [type=bool]
           └── agg-filter [type=int]
                ├── count [type=int]
                │    └── variable: count_1_arg1 [type=bool]
                └── variable: count_2_filter [type=bool]

build
SELECT
    count(*) FILTER (WHERE false) OVER (),
    count(*) FILTER (WHERE false) OVER (),
    avg(k) FILTER (WHERE false) OVER ()
FROM
    kv
----
project
 ├── columns: count:8(int) count:8(int) avg:9(decimal)
 └── window partition=()
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) count:8(int) avg:9(decimal) count_1_arg1:10(bool!null) count_1_filter:11(bool!null)
      ├── project
      │    ├── columns: count_1_arg1:10(bool!null) count_1_filter:11(bool!null) k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      │    ├── scan kv
      │    │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      │    └── projections
      │         ├── true [type=bool]
      │         └── false [type=bool]
      └── windows
           ├── agg-filter [type=int]
           │    ├── count [type=int]
           │    │    └── variable: count_1_arg1 [type=bool]
           │    └── variable: count_1_filter [type=bool]
           └── agg-filter [type=decimal]
                ├── avg [type=decimal]
                │    └── variable: k [type=int]
                └── variable: count_1_filter [type=bool]

# Named windows.

build
SELECT
  rank() OVER (w)
FROM kv WINDOW w as (ORDER BY v)
----
project
 ├── columns: rank:8(int)
 └── window partition=() ordering=+2
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) rank:8(int)
      ├── scan kv
      │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      └── windows
           └── rank [type=int]

build
SELECT
  rank() OVER (w)
FROM kv WINDOW w as (ORDER BY v)
----
project
 ├── columns: rank:8(int)
 └── window partition=() ordering=+2
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) rank:8(int)
      ├── scan kv
      │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      └── windows
           └── rank [type=int]

build
SELECT
  rank() OVER (w),
  rank() OVER w,
  row_number() OVER (w2),
  row_number() OVER (w2 ORDER BY f),
  row_number() OVER w2
FROM kv WINDOW w as (ORDER BY v), w2 as (PARTITION BY v)
----
project
 ├── columns: rank:8(int) rank:9(int) row_number:10(int) row_number:11(int) row_number:12(int)
 └── window partition=(2) ordering=+4
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) rank:8(int) rank:9(int) row_number:10(int) row_number:11(int) row_number:12(int)
      ├── window partition=(2)
      │    ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) rank:8(int) rank:9(int) row_number:10(int) row_number:12(int)
      │    ├── window partition=() ordering=+2
      │    │    ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) rank:8(int) rank:9(int)
      │    │    ├── scan kv
      │    │    │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      │    │    └── windows
      │    │         ├── rank [type=int]
      │    │         └── rank [type=int]
      │    └── windows
      │         ├── row-number [type=int]
      │         └── row-number [type=int]
      └── windows
           └── row-number [type=int]

build
SELECT
  rank() OVER w
FROM kv WINDOW w as (ORDER BY v), w2 as (PARTITION BY v)
----
project
 ├── columns: rank:8(int)
 └── window partition=() ordering=+2
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) rank:8(int)
      ├── scan kv
      │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      └── windows
           └── rank [type=int]

build
SELECT
  rank() OVER w
FROM kv WINDOW w as (ORDER BY v)
ORDER BY rank() OVER w
----
sort
 ├── columns: rank:8(int)
 ├── ordering: +8
 └── project
      ├── columns: rank:8(int)
      └── window partition=() ordering=+2
           ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) rank:8(int)
           ├── scan kv
           │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
           └── windows
                └── rank [type=int]

build
SELECT
  rank() OVER w
FROM kv
WHERE rank() OVER w < 4
WINDOW w as (ORDER BY v)
----
error (42P20): rank(): window functions are not allowed in WHERE

# Window names don't descend through scopes.
build
SELECT
  (SELECT rank() OVER w)
FROM kv WINDOW w as (ORDER BY v)
----
error (42704): window "w" does not exist

# Window names may only be used a single time.
build
SELECT
  rank() OVER (w)
FROM kv WINDOW w as (ORDER BY v), w as (PARTITION BY v)
----
error (42P20): window "w" is already defined

# Rules for overriding a window definition.
build
SELECT rank() OVER (w ORDER BY f) FROM kv WINDOW w as (ORDER BY v)
----
error (42P20): cannot override ORDER BY clause of window "w"

build
SELECT rank() OVER (w PARTITION BY f) FROM kv WINDOW w as (ORDER BY v)
----
error (42P20): cannot override PARTITION BY clause of window "w"

build
SELECT rank() OVER (w) FROM kv WINDOW w as (ORDER BY v RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
----
error (42P20): cannot copy window "w" because it has a frame clause

build
SELECT rank() OVER (ORDER BY k GROUPS k PRECEDING) FROM kv
----
error (42P10): argument of GROUPS must not contain variables

build
SELECT
  rank() OVER (RANGE UNBOUNDED PRECEDING EXCLUDE CURRENT ROW),
  rank() OVER (RANGE UNBOUNDED PRECEDING EXCLUDE GROUP),
  rank() OVER (RANGE UNBOUNDED PRECEDING EXCLUDE TIES),
  rank() OVER (RANGE UNBOUNDED PRECEDING EXCLUDE NO OTHERS)
FROM kv
----
project
 ├── columns: rank:8(int) rank:9(int) rank:10(int) rank:11(int)
 └── window partition=()
      ├── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool) rank:8(int) rank:9(int) rank:10(int) rank:11(int)
      ├── scan kv
      │    └── columns: k:1(int!null) v:2(int) w:3(int) f:4(float) d:5(decimal) s:6(string) b:7(bool)
      └── windows
           ├── windows-item: range from unbounded to current-row exclude current row [type=int]
           │    └── rank [type=int]
           ├── windows-item: range from unbounded to current-row exclude group [type=int]
           │    └── rank [type=int]
           ├── windows-item: range from unbounded to current-row exclude ties [type=int]
           │    └── rank [type=int]
           └── rank [type=int]

exec-ddl
CREATE TABLE table1 (col5 CHAR, col8 INT2);
----

build
SELECT
    min(tab_536191.col5) OVER (
        ROWS BETWEEN tab_536191.col8 FOLLOWING AND 1 FOLLOWING
    )
FROM
    table1 AS tab_536191
GROUP BY
    tab_536191.col8, tab_536191.col5
----
error (42P10): argument of ROWS must not contain variables
