exec-ddl
CREATE TABLE a (k INT PRIMARY KEY, i INT, f FLOAT, s STRING, j JSON)
----

# --------------------------------------------------
# ReduceWindowPartitionCols
# --------------------------------------------------

norm expect=ReduceWindowPartitionCols
SELECT rank() OVER (PARTITION BY k, i) FROM a
----
project
 ├── columns: rank:6(int)
 └── window partition=(1)
      ├── columns: k:1(int!null) rank:6(int)
      ├── key: (1)
      ├── scan a
      │    ├── columns: k:1(int!null)
      │    └── key: (1)
      └── windows
           └── rank [type=int]

norm expect=ReduceWindowPartitionCols
SELECT rank() OVER (PARTITION BY i, i+1) FROM a
----
project
 ├── columns: rank:6(int)
 └── window partition=(2)
      ├── columns: i:2(int) rank:6(int)
      ├── scan a
      │    └── columns: i:2(int)
      └── windows
           └── rank [type=int]

# --------------------------------------------------
# SimplifyWindowOrdering
# --------------------------------------------------

norm expect=SimplifyWindowOrdering
SELECT rank() OVER (ORDER BY k, i) FROM a
----
project
 ├── columns: rank:6(int)
 └── window partition=() ordering=+1
      ├── columns: k:1(int!null) rank:6(int)
      ├── key: (1)
      ├── scan a
      │    ├── columns: k:1(int!null)
      │    └── key: (1)
      └── windows
           └── rank [type=int]

# We can simplify the ordering with the knowledge that within any partition
# the set of partition cols is held constant.

# TODO(justin): ensure these are fixed once we handle framing.
norm
SELECT rank() OVER (PARTITION BY k ORDER BY i) FROM a
----
project
 ├── columns: rank:6(int)
 └── window partition=(1)
      ├── columns: k:1(int!null) rank:6(int)
      ├── key: (1)
      ├── scan a
      │    ├── columns: k:1(int!null)
      │    └── key: (1)
      └── windows
           └── rank [type=int]

norm expect=SimplifyWindowOrdering
SELECT rank() OVER (PARTITION BY i ORDER BY f, i+1) FROM a
----
project
 ├── columns: rank:6(int)
 └── window partition=(2) ordering=+3 opt(2,7)
      ├── columns: i:2(int) f:3(float) rank:6(int)
      ├── scan a
      │    └── columns: i:2(int) f:3(float)
      └── windows
           └── rank [type=int]

norm expect=SimplifyWindowOrdering
SELECT rank() OVER (PARTITION BY f ORDER BY i) FROM a
----
project
 ├── columns: rank:6(int)
 └── window partition=(3) ordering=+2 opt(3)
      ├── columns: i:2(int) f:3(float) rank:6(int)
      ├── scan a
      │    └── columns: i:2(int) f:3(float)
      └── windows
           └── rank [type=int]

# Do not simplify ordering column when in RANGE mode with an offset boundary of
# PRECEDING, because execution requires a column in this case.
norm expect-not=SimplifyWindowOrdering
SELECT rank() OVER (ORDER BY k RANGE 1 PRECEDING) FROM a WHERE false
----
values
 ├── columns: rank:6(int!null)
 ├── cardinality: [0 - 0]
 ├── key: ()
 └── fd: ()-->(6)

# Do not simplify ordering column when in RANGE mode with an offset boundary of
# FOLLOWING, because execution requires a column in this case.
norm expect-not=SimplifyWindowOrdering
SELECT rank() OVER (ORDER BY k RANGE BETWEEN 1 FOLLOWING AND 3 FOLLOWING) FROM a WHERE false
----
values
 ├── columns: rank:6(int!null)
 ├── cardinality: [0 - 0]
 ├── key: ()
 └── fd: ()-->(6)

# PushSelectIntoWindow

norm expect=PushSelectIntoWindow
SELECT * FROM (SELECT i, rank() OVER (PARTITION BY i) FROM a) WHERE i > 4
----
window partition=(2)
 ├── columns: i:2(int!null) rank:6(int)
 ├── select
 │    ├── columns: i:2(int!null)
 │    ├── scan a
 │    │    └── columns: i:2(int)
 │    └── filters
 │         └── i > 4 [type=bool, outer=(2), constraints=(/2: [/5 - ]; tight)]
 └── windows
      └── rank [type=int]

# Only push down filters bound by the partition cols.
norm expect=PushSelectIntoWindow
SELECT * FROM (SELECT i, s, rank() OVER (PARTITION BY i) FROM a) WHERE i > 4 AND s = 'foo'
----
select
 ├── columns: i:2(int!null) s:4(string!null) rank:6(int)
 ├── fd: ()-->(4)
 ├── window partition=(2)
 │    ├── columns: i:2(int!null) s:4(string) rank:6(int)
 │    ├── select
 │    │    ├── columns: i:2(int!null) s:4(string)
 │    │    ├── scan a
 │    │    │    └── columns: i:2(int) s:4(string)
 │    │    └── filters
 │    │         └── i > 4 [type=bool, outer=(2), constraints=(/2: [/5 - ]; tight)]
 │    └── windows
 │         └── rank [type=int]
 └── filters
      └── s = 'foo' [type=bool, outer=(4), constraints=(/4: [/'foo' - /'foo']; tight), fd=()-->(4)]

# Multiple partition cols.
norm expect=PushSelectIntoWindow
SELECT * FROM (SELECT i, s, f, rank() OVER (PARTITION BY i, f) FROM a) WHERE i > 4 AND f = 3.0 AND s = 'foo'
----
select
 ├── columns: i:2(int!null) s:4(string!null) f:3(float!null) rank:6(int)
 ├── fd: ()-->(3,4)
 ├── window partition=(2)
 │    ├── columns: i:2(int!null) f:3(float!null) s:4(string) rank:6(int)
 │    ├── fd: ()-->(3)
 │    ├── select
 │    │    ├── columns: i:2(int!null) f:3(float!null) s:4(string)
 │    │    ├── fd: ()-->(3)
 │    │    ├── scan a
 │    │    │    └── columns: i:2(int) f:3(float) s:4(string)
 │    │    └── filters
 │    │         ├── i > 4 [type=bool, outer=(2), constraints=(/2: [/5 - ]; tight)]
 │    │         └── f = 3.0 [type=bool, outer=(3), constraints=(/3: [/3.0 - /3.0]; tight), fd=()-->(3)]
 │    └── windows
 │         └── rank [type=int]
 └── filters
      └── s = 'foo' [type=bool, outer=(4), constraints=(/4: [/'foo' - /'foo']; tight), fd=()-->(4)]

# This is not really correct, but there isn't a precedent for rejecting such filters.
# TODO(justin): consider revisiting this and not pushing this filter down.
norm expect=PushSelectIntoWindow
SELECT * FROM (SELECT i, s, f, rank() OVER (PARTITION BY i, f) FROM a) WHERE random() < 0.5
----
window partition=(2,3)
 ├── columns: i:2(int) s:4(string) f:3(float) rank:6(int)
 ├── side-effects
 ├── select
 │    ├── columns: i:2(int) f:3(float) s:4(string)
 │    ├── side-effects
 │    ├── scan a
 │    │    └── columns: i:2(int) f:3(float) s:4(string)
 │    └── filters
 │         └── random() < 0.5 [type=bool, side-effects]
 └── windows
      └── rank [type=int]

# Can't push down a filter on an ordering column.
norm expect-not=PushSelectIntoWindow
SELECT * FROM (SELECT f, rank() OVER (PARTITION BY i ORDER BY f) FROM a) WHERE f > 4
----
project
 ├── columns: f:3(float!null) rank:6(int)
 └── select
      ├── columns: i:2(int) f:3(float!null) rank:6(int)
      ├── window partition=(2) ordering=+3 opt(2)
      │    ├── columns: i:2(int) f:3(float) rank:6(int)
      │    ├── scan a
      │    │    └── columns: i:2(int) f:3(float)
      │    └── windows
      │         └── rank [type=int]
      └── filters
           └── f > 4.0 [type=bool, outer=(3), constraints=(/3: [/4.000000000000001 - ]; tight)]

# Can't push down a filter on an arbitrary column.
norm expect-not=PushSelectIntoWindow
SELECT * FROM (SELECT s, rank() OVER (PARTITION BY i ORDER BY f) FROM a) WHERE s = 'foo'
----
project
 ├── columns: s:4(string!null) rank:6(int)
 ├── fd: ()-->(4)
 └── select
      ├── columns: i:2(int) f:3(float) s:4(string!null) rank:6(int)
      ├── fd: ()-->(4)
      ├── window partition=(2) ordering=+3 opt(2)
      │    ├── columns: i:2(int) f:3(float) s:4(string) rank:6(int)
      │    ├── scan a
      │    │    └── columns: i:2(int) f:3(float) s:4(string)
      │    └── windows
      │         └── rank [type=int]
      └── filters
           └── s = 'foo' [type=bool, outer=(4), constraints=(/4: [/'foo' - /'foo']; tight), fd=()-->(4)]

# Can push down filters on columns in the closure of the partition columns.
norm expect=PushSelectIntoWindow
SELECT * FROM (SELECT i, rank() OVER (PARTITION BY k ORDER BY f) FROM a) WHERE i = 3
----
project
 ├── columns: i:2(int!null) rank:6(int)
 ├── fd: ()-->(2)
 └── window partition=(1)
      ├── columns: k:1(int!null) i:2(int!null) rank:6(int)
      ├── key: (1)
      ├── fd: ()-->(2)
      ├── select
      │    ├── columns: k:1(int!null) i:2(int!null)
      │    ├── key: (1)
      │    ├── fd: ()-->(2)
      │    ├── scan a
      │    │    ├── columns: k:1(int!null) i:2(int)
      │    │    ├── key: (1)
      │    │    └── fd: (1)-->(2)
      │    └── filters
      │         └── i = 3 [type=bool, outer=(2), constraints=(/2: [/3 - /3]; tight), fd=()-->(2)]
      └── windows
           └── rank [type=int]

norm expect=PushSelectIntoWindow
SELECT * FROM (SELECT i, f, rank() OVER (PARTITION BY k ORDER BY f) FROM a) WHERE i*f::int = 3
----
project
 ├── columns: i:2(int) f:3(float) rank:6(int)
 └── window partition=(1)
      ├── columns: k:1(int!null) i:2(int) f:3(float) rank:6(int)
      ├── key: (1)
      ├── fd: (1)-->(2,3)
      ├── select
      │    ├── columns: k:1(int!null) i:2(int) f:3(float)
      │    ├── key: (1)
      │    ├── fd: (1)-->(2,3)
      │    ├── scan a
      │    │    ├── columns: k:1(int!null) i:2(int) f:3(float)
      │    │    ├── key: (1)
      │    │    └── fd: (1)-->(2,3)
      │    └── filters
      │         └── (i * f::INT8) = 3 [type=bool, outer=(2,3)]
      └── windows
           └── rank [type=int]

norm expect-not=PushSelectIntoWindow
SELECT * FROM (SELECT i, f, rank() OVER (PARTITION BY k ORDER BY f) AS rnk FROM a) WHERE rnk = 3
----
project
 ├── columns: i:2(int) f:3(float) rnk:6(int!null)
 ├── fd: ()-->(6)
 └── select
      ├── columns: k:1(int!null) i:2(int) f:3(float) rank:6(int!null)
      ├── key: (1)
      ├── fd: ()-->(6), (1)-->(2,3)
      ├── window partition=(1)
      │    ├── columns: k:1(int!null) i:2(int) f:3(float) rank:6(int)
      │    ├── key: (1)
      │    ├── fd: (1)-->(2,3)
      │    ├── scan a
      │    │    ├── columns: k:1(int!null) i:2(int) f:3(float)
      │    │    ├── key: (1)
      │    │    └── fd: (1)-->(2,3)
      │    └── windows
      │         └── rank [type=int]
      └── filters
           └── rank = 3 [type=bool, outer=(6), constraints=(/6: [/3 - /3]; tight), fd=()-->(6)]

# --------------------------------------------------
# PushLimitIntoWindow
# --------------------------------------------------

norm
SELECT rank() OVER () FROM a LIMIT 10
----
window partition=()
 ├── columns: rank:6(int)
 ├── cardinality: [0 - 10]
 ├── limit
 │    ├── cardinality: [0 - 10]
 │    ├── scan a
 │    └── const: 10 [type=int]
 └── windows
      └── rank [type=int]

norm
SELECT rank() OVER (PARTITION BY i) FROM a LIMIT 10
----
project
 ├── columns: rank:6(int)
 ├── cardinality: [0 - 10]
 └── window partition=(2)
      ├── columns: i:2(int) rank:6(int)
      ├── cardinality: [0 - 10]
      ├── limit
      │    ├── columns: i:2(int)
      │    ├── internal-ordering: +2
      │    ├── cardinality: [0 - 10]
      │    ├── sort
      │    │    ├── columns: i:2(int)
      │    │    ├── ordering: +2
      │    │    └── scan a
      │    │         └── columns: i:2(int)
      │    └── const: 10 [type=int]
      └── windows
           └── rank [type=int]

norm
SELECT rank() OVER (PARTITION BY i ORDER BY f) FROM a LIMIT 10
----
project
 ├── columns: rank:6(int)
 ├── cardinality: [0 - 10]
 └── window partition=(2) ordering=+3 opt(2)
      ├── columns: i:2(int) f:3(float) rank:6(int)
      ├── cardinality: [0 - 10]
      ├── limit
      │    ├── columns: i:2(int) f:3(float)
      │    ├── internal-ordering: +2,+3
      │    ├── cardinality: [0 - 10]
      │    ├── sort
      │    │    ├── columns: i:2(int) f:3(float)
      │    │    ├── ordering: +2,+3
      │    │    └── scan a
      │    │         └── columns: i:2(int) f:3(float)
      │    └── const: 10 [type=int]
      └── windows
           └── rank [type=int]

norm
SELECT
  rank() OVER (PARTITION BY i ORDER BY f),
  dense_rank() OVER (PARTITION BY i ORDER BY f)
FROM a LIMIT 10
----
project
 ├── columns: rank:6(int) dense_rank:7(int)
 ├── cardinality: [0 - 10]
 └── window partition=(2) ordering=+3 opt(2)
      ├── columns: i:2(int) f:3(float) rank:6(int) dense_rank:7(int)
      ├── cardinality: [0 - 10]
      ├── limit
      │    ├── columns: i:2(int) f:3(float)
      │    ├── internal-ordering: +2,+3
      │    ├── cardinality: [0 - 10]
      │    ├── sort
      │    │    ├── columns: i:2(int) f:3(float)
      │    │    ├── ordering: +2,+3
      │    │    └── scan a
      │    │         └── columns: i:2(int) f:3(float)
      │    └── const: 10 [type=int]
      └── windows
           ├── rank [type=int]
           └── dense-rank [type=int]

# Can't push the limit down, because the window function used is not
# "prefix-safe".
norm expect-not=PushLimitIntoWindow
SELECT avg(k) OVER () FROM a LIMIT 10
----
project
 ├── columns: avg:6(decimal)
 ├── cardinality: [0 - 10]
 └── limit
      ├── columns: k:1(int!null) avg:6(decimal)
      ├── cardinality: [0 - 10]
      ├── key: (1)
      ├── window partition=()
      │    ├── columns: k:1(int!null) avg:6(decimal)
      │    ├── key: (1)
      │    ├── scan a
      │    │    ├── columns: k:1(int!null)
      │    │    └── key: (1)
      │    └── windows
      │         └── avg [type=decimal, outer=(1)]
      │              └── variable: k [type=int]
      └── const: 10 [type=int]

# Can't push the limit down, because the limit operator's ordering does not
# agree with the window function's ordering.
norm expect-not=PushLimitIntoWindow
SELECT rank() OVER (ORDER BY i) FROM a ORDER BY f LIMIT 10
----
project
 ├── columns: rank:6(int)  [hidden: f:3(float)]
 ├── cardinality: [0 - 10]
 ├── ordering: +3
 └── limit
      ├── columns: i:2(int) f:3(float) rank:6(int)
      ├── internal-ordering: +3
      ├── cardinality: [0 - 10]
      ├── ordering: +3
      ├── sort
      │    ├── columns: i:2(int) f:3(float) rank:6(int)
      │    ├── ordering: +3
      │    └── window partition=() ordering=+2
      │         ├── columns: i:2(int) f:3(float) rank:6(int)
      │         ├── scan a
      │         │    └── columns: i:2(int) f:3(float)
      │         └── windows
      │              └── rank [type=int]
      └── const: 10 [type=int]

# The limit should become stronger as it gets pushed down (going from +f to
# +f,+i), because the new limit needs to match the window function's ordering,
# rather than its own (weaker) ordering.
norm
SELECT rank() OVER (ORDER BY f, i) FROM a ORDER BY f LIMIT 10
----
sort
 ├── columns: rank:6(int)  [hidden: f:3(float)]
 ├── cardinality: [0 - 10]
 ├── ordering: +3
 └── project
      ├── columns: f:3(float) rank:6(int)
      ├── cardinality: [0 - 10]
      └── window partition=() ordering=+3,+2
           ├── columns: i:2(int) f:3(float) rank:6(int)
           ├── cardinality: [0 - 10]
           ├── limit
           │    ├── columns: i:2(int) f:3(float)
           │    ├── internal-ordering: +3,+2
           │    ├── cardinality: [0 - 10]
           │    ├── sort
           │    │    ├── columns: i:2(int) f:3(float)
           │    │    ├── ordering: +3,+2
           │    │    └── scan a
           │    │         └── columns: i:2(int) f:3(float)
           │    └── const: 10 [type=int]
           └── windows
                └── rank [type=int]

# Here we would only be able to push below the rank(), and not the avg(k). This
# is not profitable because we still have to do the partitioning and ordering
# for the one we were unable to push the limit below, which is the expensive
# part.
norm
SELECT
    rank() OVER (PARTITION BY i ORDER BY f),
    avg(k) OVER (PARTITION BY i ORDER BY f)
FROM
    a
LIMIT
    10
----
project
 ├── columns: rank:6(int) avg:7(decimal)
 ├── cardinality: [0 - 10]
 └── limit
      ├── columns: k:1(int!null) i:2(int) f:3(float) rank:6(int) avg:7(decimal)
      ├── cardinality: [0 - 10]
      ├── key: (1)
      ├── fd: (1)-->(2,3)
      ├── window partition=(2) ordering=+3 opt(2)
      │    ├── columns: k:1(int!null) i:2(int) f:3(float) rank:6(int) avg:7(decimal)
      │    ├── key: (1)
      │    ├── fd: (1)-->(2,3)
      │    ├── scan a
      │    │    ├── columns: k:1(int!null) i:2(int) f:3(float)
      │    │    ├── key: (1)
      │    │    └── fd: (1)-->(2,3)
      │    └── windows
      │         ├── rank [type=int]
      │         └── avg [type=decimal, outer=(1)]
      │              └── variable: k [type=int]
      └── const: 10 [type=int]

exec-ddl
CREATE TABLE wxyz (w INT PRIMARY KEY, x INT, y INT, z INT)
----

norm expect-not=PushLimitIntoWindow
SELECT *, rank() OVER (PARTITION BY z ORDER BY y) FROM wxyz ORDER BY y LIMIT 2
----
limit
 ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int) rank:5(int)
 ├── internal-ordering: +3
 ├── cardinality: [0 - 2]
 ├── key: (1)
 ├── fd: (1)-->(2-4)
 ├── ordering: +3
 ├── sort
 │    ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int) rank:5(int)
 │    ├── key: (1)
 │    ├── fd: (1)-->(2-4)
 │    ├── ordering: +3
 │    └── window partition=(4) ordering=+3 opt(4)
 │         ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int) rank:5(int)
 │         ├── key: (1)
 │         ├── fd: (1)-->(2-4)
 │         ├── scan wxyz
 │         │    ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int)
 │         │    ├── key: (1)
 │         │    └── fd: (1)-->(2-4)
 │         └── windows
 │              └── rank [type=int]
 └── const: 2 [type=int]

norm expect=PushLimitIntoWindow
SELECT *, rank() OVER (PARTITION BY w ORDER BY y) FROM wxyz ORDER BY y LIMIT 2
----
sort
 ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int) rank:5(int)
 ├── cardinality: [0 - 2]
 ├── key: (1)
 ├── fd: (1)-->(2-4)
 ├── ordering: +3
 └── window partition=(1)
      ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int) rank:5(int)
      ├── cardinality: [0 - 2]
      ├── key: (1)
      ├── fd: (1)-->(2-4)
      ├── limit
      │    ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int)
      │    ├── internal-ordering: +3,+1
      │    ├── cardinality: [0 - 2]
      │    ├── key: (1)
      │    ├── fd: (1)-->(2-4)
      │    ├── sort
      │    │    ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int)
      │    │    ├── key: (1)
      │    │    ├── fd: (1)-->(2-4)
      │    │    ├── ordering: +3,+1
      │    │    └── scan wxyz
      │    │         ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int)
      │    │         ├── key: (1)
      │    │         └── fd: (1)-->(2-4)
      │    └── const: 2 [type=int]
      └── windows
           └── rank [type=int]

norm expect=PushLimitIntoWindow
SELECT *, rank() OVER (PARTITION BY w ORDER BY y) FROM wxyz ORDER BY w, y LIMIT 2
----
sort
 ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int) rank:5(int)
 ├── cardinality: [0 - 2]
 ├── key: (1)
 ├── fd: (1)-->(2-4)
 ├── ordering: +1
 └── window partition=(1)
      ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int) rank:5(int)
      ├── cardinality: [0 - 2]
      ├── key: (1)
      ├── fd: (1)-->(2-4)
      ├── limit
      │    ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int)
      │    ├── internal-ordering: +1
      │    ├── cardinality: [0 - 2]
      │    ├── key: (1)
      │    ├── fd: (1)-->(2-4)
      │    ├── scan wxyz
      │    │    ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int)
      │    │    ├── key: (1)
      │    │    ├── fd: (1)-->(2-4)
      │    │    └── ordering: +1
      │    └── const: 2 [type=int]
      └── windows
           └── rank [type=int]

norm expect=PushLimitIntoWindow
SELECT *, rank() OVER (PARTITION BY w ORDER BY y) FROM wxyz ORDER BY w LIMIT 2
----
sort
 ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int) rank:5(int)
 ├── cardinality: [0 - 2]
 ├── key: (1)
 ├── fd: (1)-->(2-4)
 ├── ordering: +1
 └── window partition=(1)
      ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int) rank:5(int)
      ├── cardinality: [0 - 2]
      ├── key: (1)
      ├── fd: (1)-->(2-4)
      ├── limit
      │    ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int)
      │    ├── internal-ordering: +1
      │    ├── cardinality: [0 - 2]
      │    ├── key: (1)
      │    ├── fd: (1)-->(2-4)
      │    ├── scan wxyz
      │    │    ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int)
      │    │    ├── key: (1)
      │    │    ├── fd: (1)-->(2-4)
      │    │    └── ordering: +1
      │    └── const: 2 [type=int]
      └── windows
           └── rank [type=int]

norm expect=PushLimitIntoWindow
SELECT *, rank() OVER (PARTITION BY w ORDER BY y) FROM wxyz ORDER BY y, w LIMIT 2
----
sort
 ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int) rank:5(int)
 ├── cardinality: [0 - 2]
 ├── key: (1)
 ├── fd: (1)-->(2-4)
 ├── ordering: +3,+1
 └── window partition=(1)
      ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int) rank:5(int)
      ├── cardinality: [0 - 2]
      ├── key: (1)
      ├── fd: (1)-->(2-4)
      ├── limit
      │    ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int)
      │    ├── internal-ordering: +3,+1
      │    ├── cardinality: [0 - 2]
      │    ├── key: (1)
      │    ├── fd: (1)-->(2-4)
      │    ├── sort
      │    │    ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int)
      │    │    ├── key: (1)
      │    │    ├── fd: (1)-->(2-4)
      │    │    ├── ordering: +3,+1
      │    │    └── scan wxyz
      │    │         ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int)
      │    │         ├── key: (1)
      │    │         └── fd: (1)-->(2-4)
      │    └── const: 2 [type=int]
      └── windows
           └── rank [type=int]

norm expect=PushLimitIntoWindow
SELECT *, rank() OVER (PARTITION BY w, z ORDER BY y) FROM wxyz ORDER BY w, z LIMIT 2
----
sort
 ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int) rank:5(int)
 ├── cardinality: [0 - 2]
 ├── key: (1)
 ├── fd: (1)-->(2-4)
 ├── ordering: +1
 └── window partition=(1)
      ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int) rank:5(int)
      ├── cardinality: [0 - 2]
      ├── key: (1)
      ├── fd: (1)-->(2-4)
      ├── limit
      │    ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int)
      │    ├── internal-ordering: +1
      │    ├── cardinality: [0 - 2]
      │    ├── key: (1)
      │    ├── fd: (1)-->(2-4)
      │    ├── scan wxyz
      │    │    ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int)
      │    │    ├── key: (1)
      │    │    ├── fd: (1)-->(2-4)
      │    │    └── ordering: +1
      │    └── const: 2 [type=int]
      └── windows
           └── rank [type=int]

norm
SELECT *, rank() OVER (PARTITION BY x, z ORDER BY y) FROM wxyz ORDER BY z, x LIMIT 2
----
sort
 ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int) rank:5(int)
 ├── cardinality: [0 - 2]
 ├── key: (1)
 ├── fd: (1)-->(2-4)
 ├── ordering: +4,+2
 └── window partition=(2,4) ordering=+3 opt(2,4)
      ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int) rank:5(int)
      ├── cardinality: [0 - 2]
      ├── key: (1)
      ├── fd: (1)-->(2-4)
      ├── limit
      │    ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int)
      │    ├── internal-ordering: +4,+2,+3
      │    ├── cardinality: [0 - 2]
      │    ├── key: (1)
      │    ├── fd: (1)-->(2-4)
      │    ├── sort
      │    │    ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int)
      │    │    ├── key: (1)
      │    │    ├── fd: (1)-->(2-4)
      │    │    ├── ordering: +4,+2,+3
      │    │    └── scan wxyz
      │    │         ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int)
      │    │         ├── key: (1)
      │    │         └── fd: (1)-->(2-4)
      │    └── const: 2 [type=int]
      └── windows
           └── rank [type=int]

norm expect=PushLimitIntoWindow
SELECT *, rank() OVER (PARTITION BY z ORDER BY y) FROM wxyz ORDER BY z, y LIMIT 2
----
sort
 ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int) rank:5(int)
 ├── cardinality: [0 - 2]
 ├── key: (1)
 ├── fd: (1)-->(2-4)
 ├── ordering: +4,+3
 └── window partition=(4) ordering=+3 opt(4)
      ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int) rank:5(int)
      ├── cardinality: [0 - 2]
      ├── key: (1)
      ├── fd: (1)-->(2-4)
      ├── limit
      │    ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int)
      │    ├── internal-ordering: +4,+3
      │    ├── cardinality: [0 - 2]
      │    ├── key: (1)
      │    ├── fd: (1)-->(2-4)
      │    ├── sort
      │    │    ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int)
      │    │    ├── key: (1)
      │    │    ├── fd: (1)-->(2-4)
      │    │    ├── ordering: +4,+3
      │    │    └── scan wxyz
      │    │         ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int)
      │    │         ├── key: (1)
      │    │         └── fd: (1)-->(2-4)
      │    └── const: 2 [type=int]
      └── windows
           └── rank [type=int]

norm expect-not=PushLimitIntoWindow
SELECT *, rank() OVER (PARTITION BY z ORDER BY y) FROM wxyz ORDER BY y LIMIT 2
----
limit
 ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int) rank:5(int)
 ├── internal-ordering: +3
 ├── cardinality: [0 - 2]
 ├── key: (1)
 ├── fd: (1)-->(2-4)
 ├── ordering: +3
 ├── sort
 │    ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int) rank:5(int)
 │    ├── key: (1)
 │    ├── fd: (1)-->(2-4)
 │    ├── ordering: +3
 │    └── window partition=(4) ordering=+3 opt(4)
 │         ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int) rank:5(int)
 │         ├── key: (1)
 │         ├── fd: (1)-->(2-4)
 │         ├── scan wxyz
 │         │    ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int)
 │         │    ├── key: (1)
 │         │    └── fd: (1)-->(2-4)
 │         └── windows
 │              └── rank [type=int]
 └── const: 2 [type=int]

norm expect=PushLimitIntoWindow
SELECT *, rank() OVER (PARTITION BY w, z ORDER BY y) FROM wxyz ORDER BY w, z, y LIMIT 2
----
sort
 ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int) rank:5(int)
 ├── cardinality: [0 - 2]
 ├── key: (1)
 ├── fd: (1)-->(2-4)
 ├── ordering: +1
 └── window partition=(1)
      ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int) rank:5(int)
      ├── cardinality: [0 - 2]
      ├── key: (1)
      ├── fd: (1)-->(2-4)
      ├── limit
      │    ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int)
      │    ├── internal-ordering: +1
      │    ├── cardinality: [0 - 2]
      │    ├── key: (1)
      │    ├── fd: (1)-->(2-4)
      │    ├── scan wxyz
      │    │    ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int)
      │    │    ├── key: (1)
      │    │    ├── fd: (1)-->(2-4)
      │    │    └── ordering: +1
      │    └── const: 2 [type=int]
      └── windows
           └── rank [type=int]

norm
SELECT *, rank() OVER (PARTITION BY w, z ORDER BY y) FROM wxyz ORDER BY z, w, y LIMIT 2
----
sort
 ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int) rank:5(int)
 ├── cardinality: [0 - 2]
 ├── key: (1)
 ├── fd: (1)-->(2-4)
 ├── ordering: +4,+1
 └── window partition=(1)
      ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int) rank:5(int)
      ├── cardinality: [0 - 2]
      ├── key: (1)
      ├── fd: (1)-->(2-4)
      ├── limit
      │    ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int)
      │    ├── internal-ordering: +4,+1
      │    ├── cardinality: [0 - 2]
      │    ├── key: (1)
      │    ├── fd: (1)-->(2-4)
      │    ├── sort
      │    │    ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int)
      │    │    ├── key: (1)
      │    │    ├── fd: (1)-->(2-4)
      │    │    ├── ordering: +4,+1
      │    │    └── scan wxyz
      │    │         ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int)
      │    │         ├── key: (1)
      │    │         └── fd: (1)-->(2-4)
      │    └── const: 2 [type=int]
      └── windows
           └── rank [type=int]

norm
SELECT *, rank() OVER (PARTITION BY w ORDER BY y) FROM wxyz ORDER BY z LIMIT 2
----
sort
 ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int) rank:5(int)
 ├── cardinality: [0 - 2]
 ├── key: (1)
 ├── fd: (1)-->(2-4)
 ├── ordering: +4
 └── window partition=(1)
      ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int) rank:5(int)
      ├── cardinality: [0 - 2]
      ├── key: (1)
      ├── fd: (1)-->(2-4)
      ├── limit
      │    ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int)
      │    ├── internal-ordering: +4,+1
      │    ├── cardinality: [0 - 2]
      │    ├── key: (1)
      │    ├── fd: (1)-->(2-4)
      │    ├── sort
      │    │    ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int)
      │    │    ├── key: (1)
      │    │    ├── fd: (1)-->(2-4)
      │    │    ├── ordering: +4,+1
      │    │    └── scan wxyz
      │    │         ├── columns: w:1(int!null) x:2(int) y:3(int) z:4(int)
      │    │         ├── key: (1)
      │    │         └── fd: (1)-->(2-4)
      │    └── const: 2 [type=int]
      └── windows
           └── rank [type=int]
