# Tests for InterestingOrderings property.

exec-ddl
CREATE TABLE abc (a INT, b INT, c INT, INDEX (a, b), UNIQUE INDEX (c))
----

# Scan operator.
opt
SELECT * FROM abc
----
scan abc
 ├── columns: a:1 b:2 c:3
 ├── lax-key: (1-3)
 ├── fd: (3)~~>(1,2)
 ├── prune: (1-3)
 └── interesting orderings: (+1,+2) (+3)

opt
SELECT a, c FROM abc
----
scan abc
 ├── columns: a:1 c:3
 ├── lax-key: (1,3)
 ├── fd: (3)~~>(1)
 ├── prune: (1,3)
 └── interesting orderings: (+1) (+3)

opt
SELECT b, c FROM abc
----
scan abc
 ├── columns: b:2 c:3
 ├── lax-key: (2,3)
 ├── fd: (3)~~>(2)
 ├── prune: (2,3)
 └── interesting orderings: (+3)


# Project operator (we use build instead of opt).
build
SELECT a, c FROM abc
----
project
 ├── columns: a:1 c:3
 ├── lax-key: (1,3)
 ├── fd: (3)~~>(1)
 ├── prune: (1,3)
 ├── interesting orderings: (+1) (+3)
 └── scan abc
      ├── columns: a:1 b:2 c:3 rowid:4!null crdb_internal_mvcc_timestamp:5 tableoid:6
      ├── key: (4)
      ├── fd: (4)-->(1-3,5,6), (3)~~>(1,2,4-6)
      ├── prune: (1-6)
      └── interesting orderings: (+4) (+1,+2,+4) (+3,+4)

build
SELECT b, c FROM abc
----
project
 ├── columns: b:2 c:3
 ├── lax-key: (2,3)
 ├── fd: (3)~~>(2)
 ├── prune: (2,3)
 ├── interesting orderings: (+3)
 └── scan abc
      ├── columns: a:1 b:2 c:3 rowid:4!null crdb_internal_mvcc_timestamp:5 tableoid:6
      ├── key: (4)
      ├── fd: (4)-->(1-3,5,6), (3)~~>(1,2,4-6)
      ├── prune: (1-6)
      └── interesting orderings: (+4) (+1,+2,+4) (+3,+4)

# GroupBy operator.
opt
SELECT min(b), a FROM abc GROUP BY a
----
group-by (streaming)
 ├── columns: min:7 a:1
 ├── grouping columns: a:1
 ├── internal-ordering: +1
 ├── key: (1)
 ├── fd: (1)-->(7)
 ├── prune: (7)
 ├── interesting orderings: (+1)
 ├── scan abc@abc_a_b_idx
 │    ├── columns: a:1 b:2
 │    ├── ordering: +1
 │    ├── prune: (1,2)
 │    └── interesting orderings: (+1,+2)
 └── aggregations
      └── min [as=min:7, outer=(2)]
           └── b:2

opt
SELECT min(b), c FROM abc GROUP BY c
----
group-by (hash)
 ├── columns: min:7 c:3
 ├── grouping columns: c:3
 ├── key: (3)
 ├── fd: (3)-->(7)
 ├── prune: (7)
 ├── interesting orderings: (+3)
 ├── scan abc
 │    ├── columns: b:2 c:3
 │    ├── lax-key: (2,3)
 │    ├── fd: (3)~~>(2)
 │    ├── prune: (2,3)
 │    └── interesting orderings: (+3)
 └── aggregations
      └── min [as=min:7, outer=(2)]
           └── b:2

# GroupBy with required ordering.
opt
SELECT array_agg(a), b, c FROM (SELECT * FROM abc ORDER BY b, a) GROUP BY b, c
----
group-by (hash)
 ├── columns: array_agg:7 b:2 c:3
 ├── grouping columns: b:2 c:3
 ├── internal-ordering: +1 opt(2,3)
 ├── key: (2,3)
 ├── fd: (3)~~>(2), (2,3)-->(7)
 ├── prune: (7)
 ├── sort
 │    ├── columns: a:1 b:2 c:3
 │    ├── lax-key: (1-3)
 │    ├── fd: (3)~~>(1,2)
 │    ├── ordering: +1 opt(2,3) [actual: +1]
 │    ├── prune: (1-3)
 │    ├── interesting orderings: (+1,+2) (+3)
 │    └── scan abc
 │         ├── columns: a:1 b:2 c:3
 │         ├── lax-key: (1-3)
 │         ├── fd: (3)~~>(1,2)
 │         ├── prune: (1-3)
 │         └── interesting orderings: (+1,+2) (+3)
 └── aggregations
      └── array-agg [as=array_agg:7, outer=(1)]
           └── a:1

# Scalar GroupBy case.
opt
SELECT max(a), min(b), sum(c) FROM abc
----
scalar-group-by
 ├── columns: max:7 min:8 sum:9
 ├── cardinality: [1 - 1]
 ├── key: ()
 ├── fd: ()-->(7-9)
 ├── prune: (7-9)
 ├── scan abc
 │    ├── columns: a:1 b:2 c:3
 │    ├── lax-key: (1-3)
 │    ├── fd: (3)~~>(1,2)
 │    ├── prune: (1-3)
 │    └── interesting orderings: (+1,+2) (+3)
 └── aggregations
      ├── max [as=max:7, outer=(1)]
      │    └── a:1
      ├── min [as=min:8, outer=(2)]
      │    └── b:2
      └── sum [as=sum:9, outer=(3)]
           └── c:3

# LookupJoin operator.
opt
SELECT * FROM abc WHERE a = 1
----
index-join abc
 ├── columns: a:1!null b:2 c:3
 ├── lax-key: (2,3)
 ├── fd: ()-->(1), (3)~~>(2)
 ├── prune: (2,3)
 ├── interesting orderings: (+2 opt(1)) (+3 opt(1))
 └── scan abc@abc_a_b_idx
      ├── columns: a:1!null b:2 rowid:4!null
      ├── constraint: /1/2/4: [/1 - /1]
      ├── key: (4)
      ├── fd: ()-->(1), (4)-->(2)
      ├── prune: (1,2,4)
      └── interesting orderings: (+2,+4 opt(1))

# Limit operator.
opt
SELECT * FROM abc ORDER BY a LIMIT 10
----
index-join abc
 ├── columns: a:1 b:2 c:3
 ├── cardinality: [0 - 10]
 ├── lax-key: (1-3)
 ├── fd: (3)~~>(1,2)
 ├── ordering: +1
 ├── prune: (2,3)
 ├── interesting orderings: (+1,+2)
 └── scan abc@abc_a_b_idx
      ├── columns: a:1 b:2 rowid:4!null
      ├── limit: 10
      ├── key: (4)
      ├── fd: (4)-->(1,2)
      ├── ordering: +1
      ├── prune: (1,2,4)
      └── interesting orderings: (+1,+2,+4)

opt
SELECT * FROM abc ORDER BY b LIMIT 10
----
top-k
 ├── columns: a:1 b:2 c:3
 ├── internal-ordering: +2
 ├── k: 10
 ├── cardinality: [0 - 10]
 ├── lax-key: (1-3)
 ├── fd: (3)~~>(1,2)
 ├── ordering: +2
 ├── prune: (1,3)
 ├── interesting orderings: (+2)
 └── scan abc
      ├── columns: a:1 b:2 c:3
      ├── lax-key: (1-3)
      ├── fd: (3)~~>(1,2)
      ├── prune: (1-3)
      └── interesting orderings: (+1,+2) (+3)

opt
SELECT * FROM abc ORDER BY a OFFSET 10
----
offset
 ├── columns: a:1 b:2 c:3
 ├── internal-ordering: +1
 ├── lax-key: (1-3)
 ├── fd: (3)~~>(1,2)
 ├── ordering: +1
 ├── prune: (2,3)
 ├── interesting orderings: (+1,+2)
 ├── sort
 │    ├── columns: a:1 b:2 c:3
 │    ├── lax-key: (1-3)
 │    ├── fd: (3)~~>(1,2)
 │    ├── ordering: +1
 │    ├── prune: (1-3)
 │    ├── interesting orderings: (+1,+2) (+3)
 │    └── scan abc
 │         ├── columns: a:1 b:2 c:3
 │         ├── lax-key: (1-3)
 │         ├── fd: (3)~~>(1,2)
 │         ├── prune: (1-3)
 │         └── interesting orderings: (+1,+2) (+3)
 └── 10

exec-ddl
CREATE TABLE xyz (x INT, y INT, z INT, INDEX(z), UNIQUE INDEX(x,y))
----

# Join operator.
opt
SELECT * FROM abc JOIN xyz ON a=x 
----
inner-join (hash)
 ├── columns: a:1!null b:2 c:3 x:7!null y:8 z:9
 ├── lax-key: (2,3,7-9)
 ├── fd: (3)~~>(1,2), (7,8)~~>(9), (1)==(7), (7)==(1)
 ├── prune: (2,3,8,9)
 ├── interesting orderings: (+1,+2) (+3) (+9) (+7,+8)
 ├── scan abc
 │    ├── columns: a:1 b:2 c:3
 │    ├── lax-key: (1-3)
 │    ├── fd: (3)~~>(1,2)
 │    ├── prune: (1-3)
 │    ├── interesting orderings: (+1,+2) (+3)
 │    └── unfiltered-cols: (1-6)
 ├── scan xyz
 │    ├── columns: x:7 y:8 z:9
 │    ├── lax-key: (7-9)
 │    ├── fd: (7,8)~~>(9)
 │    ├── prune: (7-9)
 │    ├── interesting orderings: (+9) (+7,+8)
 │    └── unfiltered-cols: (7-12)
 └── filters
      └── a:1 = x:7 [outer=(1,7), constraints=(/1: (/NULL - ]; /7: (/NULL - ]), fd=(1)==(7), (7)==(1)]
