exec-ddl
CREATE TABLE xyzs (x INT PRIMARY KEY, y INT, z FLOAT NOT NULL, s STRING, UNIQUE (s DESC, z))
----

exec-ddl
CREATE TABLE kuv (k INT PRIMARY KEY, u FLOAT, v STRING)
----

build
SELECT * FROM xyzs LIMIT 1
----
limit
 ├── columns: x:1(int!null) y:2(int) z:3(float!null) s:4(string)
 ├── cardinality: [0 - 1]
 ├── key: ()
 ├── fd: ()-->(1-4)
 ├── prune: (1-4)
 ├── interesting orderings: (+1) (-4,+3,+1)
 ├── project
 │    ├── columns: x:1(int!null) y:2(int) z:3(float!null) s:4(string)
 │    ├── key: (1)
 │    ├── fd: (1)-->(2-4), (3,4)~~>(1,2)
 │    ├── limit hint: 1.00
 │    ├── prune: (1-4)
 │    ├── interesting orderings: (+1) (-4,+3,+1)
 │    └── scan xyzs
 │         ├── columns: x:1(int!null) y:2(int) z:3(float!null) s:4(string) crdb_internal_mvcc_timestamp:5(decimal) tableoid:6(oid)
 │         ├── key: (1)
 │         ├── fd: (1)-->(2-6), (3,4)~~>(1,2,5,6)
 │         ├── limit hint: 1.00
 │         ├── prune: (1-6)
 │         └── interesting orderings: (+1) (-4,+3,+1)
 └── const: 1 [type=int]

build
SELECT * FROM xyzs WHERE x = y LIMIT 1
----
limit
 ├── columns: x:1(int!null) y:2(int!null) z:3(float!null) s:4(string)
 ├── cardinality: [0 - 1]
 ├── key: ()
 ├── fd: ()-->(1-4), (1)==(2), (2)==(1)
 ├── prune: (1-4)
 ├── interesting orderings: (+(1|2)) (-4,+3,+(1|2))
 ├── project
 │    ├── columns: x:1(int!null) y:2(int!null) z:3(float!null) s:4(string)
 │    ├── key: (1)
 │    ├── fd: (1)-->(3,4), (3,4)~~>(1,2), (1)==(2), (2)==(1)
 │    ├── limit hint: 1.00
 │    ├── prune: (1-4)
 │    ├── interesting orderings: (+(1|2)) (-4,+3,+(1|2))
 │    └── select
 │         ├── columns: x:1(int!null) y:2(int!null) z:3(float!null) s:4(string) crdb_internal_mvcc_timestamp:5(decimal) tableoid:6(oid)
 │         ├── key: (1)
 │         ├── fd: (1)-->(3-6), (3,4)~~>(1,2,5,6), (1)==(2), (2)==(1)
 │         ├── limit hint: 1.00
 │         ├── prune: (3-6)
 │         ├── interesting orderings: (+(1|2)) (-4,+3,+(1|2))
 │         ├── scan xyzs
 │         │    ├── columns: x:1(int!null) y:2(int) z:3(float!null) s:4(string) crdb_internal_mvcc_timestamp:5(decimal) tableoid:6(oid)
 │         │    ├── key: (1)
 │         │    ├── fd: (1)-->(2-6), (3,4)~~>(1,2,5,6)
 │         │    ├── prune: (1-6)
 │         │    └── interesting orderings: (+1) (-4,+3,+1)
 │         └── filters
 │              └── eq [type=bool, outer=(1,2), constraints=(/1: (/NULL - ]; /2: (/NULL - ]), fd=(1)==(2), (2)==(1)]
 │                   ├── variable: x:1 [type=int]
 │                   └── variable: y:2 [type=int]
 └── const: 1 [type=int]

build
SELECT count(*) FROM xyzs LIMIT 10
----
limit
 ├── columns: count:7(int!null)
 ├── cardinality: [1 - 1]
 ├── key: ()
 ├── fd: ()-->(7)
 ├── prune: (7)
 ├── scalar-group-by
 │    ├── columns: count_rows:7(int!null)
 │    ├── cardinality: [1 - 1]
 │    ├── key: ()
 │    ├── fd: ()-->(7)
 │    ├── limit hint: 10.00
 │    ├── prune: (7)
 │    ├── project
 │    │    └── scan xyzs
 │    │         ├── columns: x:1(int!null) y:2(int) z:3(float!null) s:4(string) crdb_internal_mvcc_timestamp:5(decimal) tableoid:6(oid)
 │    │         ├── key: (1)
 │    │         ├── fd: (1)-->(2-6), (3,4)~~>(1,2,5,6)
 │    │         ├── prune: (1-6)
 │    │         └── interesting orderings: (+1) (-4,+3,+1)
 │    └── aggregations
 │         └── count-rows [as=count_rows:7, type=int]
 └── const: 10 [type=int]

build
SELECT * FROM xyzs LIMIT (SELECT 1)
----
limit
 ├── columns: x:1(int!null) y:2(int) z:3(float!null) s:4(string)
 ├── immutable
 ├── key: (1)
 ├── fd: (1)-->(2-4), (3,4)~~>(1,2)
 ├── prune: (1-4)
 ├── interesting orderings: (+1) (-4,+3,+1)
 ├── project
 │    ├── columns: x:1(int!null) y:2(int) z:3(float!null) s:4(string)
 │    ├── key: (1)
 │    ├── fd: (1)-->(2-4), (3,4)~~>(1,2)
 │    ├── prune: (1-4)
 │    ├── interesting orderings: (+1) (-4,+3,+1)
 │    └── scan xyzs
 │         ├── columns: x:1(int!null) y:2(int) z:3(float!null) s:4(string) crdb_internal_mvcc_timestamp:5(decimal) tableoid:6(oid)
 │         ├── key: (1)
 │         ├── fd: (1)-->(2-6), (3,4)~~>(1,2,5,6)
 │         ├── prune: (1-6)
 │         └── interesting orderings: (+1) (-4,+3,+1)
 └── subquery [type=int]
      └── max1-row
           ├── columns: "?column?":7(int!null)
           ├── error: "more than one row returned by a subquery used as an expression"
           ├── cardinality: [1 - 1]
           ├── key: ()
           ├── fd: ()-->(7)
           └── project
                ├── columns: "?column?":7(int!null)
                ├── cardinality: [1 - 1]
                ├── key: ()
                ├── fd: ()-->(7)
                ├── prune: (7)
                ├── values
                │    ├── cardinality: [1 - 1]
                │    ├── key: ()
                │    └── tuple [type=tuple]
                └── projections
                     └── const: 1 [as="?column?":7, type=int]

build
SELECT * FROM xyzs LIMIT 0
----
limit
 ├── columns: x:1(int!null) y:2(int) z:3(float!null) s:4(string)
 ├── cardinality: [0 - 0]
 ├── key: ()
 ├── fd: ()-->(1-4)
 ├── prune: (1-4)
 ├── interesting orderings: (+1) (-4,+3,+1)
 ├── project
 │    ├── columns: x:1(int!null) y:2(int) z:3(float!null) s:4(string)
 │    ├── key: (1)
 │    ├── fd: (1)-->(2-4), (3,4)~~>(1,2)
 │    ├── limit hint: 1.00
 │    ├── prune: (1-4)
 │    ├── interesting orderings: (+1) (-4,+3,+1)
 │    └── scan xyzs
 │         ├── columns: x:1(int!null) y:2(int) z:3(float!null) s:4(string) crdb_internal_mvcc_timestamp:5(decimal) tableoid:6(oid)
 │         ├── key: (1)
 │         ├── fd: (1)-->(2-6), (3,4)~~>(1,2,5,6)
 │         ├── limit hint: 1.00
 │         ├── prune: (1-6)
 │         └── interesting orderings: (+1) (-4,+3,+1)
 └── const: 0 [type=int]

# Propagate outer columns.
build
SELECT (SELECT x FROM kuv LIMIT y) FROM xyzs
----
project
 ├── columns: x:13(int)
 ├── immutable
 ├── prune: (13)
 ├── scan xyzs
 │    ├── columns: xyzs.x:1(int!null) y:2(int) z:3(float!null) s:4(string) xyzs.crdb_internal_mvcc_timestamp:5(decimal) xyzs.tableoid:6(oid)
 │    ├── key: (1)
 │    ├── fd: (1)-->(2-6), (3,4)~~>(1,2,5,6)
 │    ├── prune: (1-6)
 │    └── interesting orderings: (+1) (-4,+3,+1)
 └── projections
      └── subquery [as=x:13, type=int, outer=(1,2), immutable, correlated-subquery]
           └── max1-row
                ├── columns: x:12(int)
                ├── error: "more than one row returned by a subquery used as an expression"
                ├── outer: (1,2)
                ├── cardinality: [0 - 1]
                ├── immutable
                ├── key: ()
                ├── fd: ()-->(12)
                └── limit
                     ├── columns: x:12(int)
                     ├── outer: (1,2)
                     ├── immutable
                     ├── fd: ()-->(12)
                     ├── prune: (12)
                     ├── project
                     │    ├── columns: x:12(int)
                     │    ├── outer: (1)
                     │    ├── fd: ()-->(12)
                     │    ├── prune: (12)
                     │    ├── scan kuv
                     │    │    ├── columns: k:7(int!null) u:8(float) v:9(string) kuv.crdb_internal_mvcc_timestamp:10(decimal) kuv.tableoid:11(oid)
                     │    │    ├── key: (7)
                     │    │    ├── fd: (7)-->(8-11)
                     │    │    ├── prune: (7-11)
                     │    │    └── interesting orderings: (+7)
                     │    └── projections
                     │         └── variable: xyzs.x:1 [as=x:12, type=int, outer=(1)]
                     └── variable: y:2 [type=int]

# Test very high limit (> max uint32).
opt
SELECT s, x FROM xyzs WHERE s='foo' LIMIT 4294967296
----
scan xyzs@xyzs_s_z_key
 ├── columns: s:4(string!null) x:1(int!null)
 ├── constraint: /-4/3: [/'foo' - /'foo']
 ├── limit: 4294967296
 ├── key: (1)
 ├── fd: ()-->(4)
 ├── prune: (1)
 └── interesting orderings: (+1 opt(4))

# Regression test for #65038. Copy FDs from input regardless of the limit value
# to avoid error during test builds: "ordering column group X contains
# non-equivalent columns".
exec-ddl
CREATE TABLE t65038 (
  a INT PRIMARY KEY,
  b INT,
  c INT
)
----

opt
SELECT 1
FROM t65038 AS t1
WHERE t1.b IN (
  SELECT 1
  FROM t65038 CROSS JOIN t65038 AS t2
    JOIN t65038 AS t3 ON
      t2.a = t3.a
      AND t2.c = t3.c
      AND t2.b = t3.b
  ORDER BY t2.b
  LIMIT 1
)
ORDER BY t1.a ASC;
----
sort
 ├── columns: "?column?":23(int!null)  [hidden: t1.a:1(int!null)]
 ├── key: (1)
 ├── fd: ()-->(23)
 ├── ordering: +1 opt(23) [actual: +1]
 ├── prune: (1,23)
 ├── interesting orderings: (+1 opt(23))
 └── project
      ├── columns: "?column?":23(int!null) t1.a:1(int!null)
      ├── key: (1)
      ├── fd: ()-->(23)
      ├── prune: (1,23)
      ├── interesting orderings: (+1 opt(23))
      ├── semi-join (cross)
      │    ├── columns: t1.a:1(int!null) t1.b:2(int!null)
      │    ├── key: (1)
      │    ├── fd: ()-->(2)
      │    ├── prune: (1)
      │    ├── interesting orderings: (+1 opt(2))
      │    ├── select
      │    │    ├── columns: t1.a:1(int!null) t1.b:2(int!null)
      │    │    ├── key: (1)
      │    │    ├── fd: ()-->(2)
      │    │    ├── prune: (1)
      │    │    ├── interesting orderings: (+1 opt(2))
      │    │    ├── scan t65038 [as=t1]
      │    │    │    ├── columns: t1.a:1(int!null) t1.b:2(int)
      │    │    │    ├── key: (1)
      │    │    │    ├── fd: (1)-->(2)
      │    │    │    ├── prune: (1,2)
      │    │    │    └── interesting orderings: (+1)
      │    │    └── filters
      │    │         └── eq [type=bool, outer=(2), constraints=(/2: [/1 - /1]; tight), fd=()-->(2)]
      │    │              ├── variable: t1.b:2 [type=int]
      │    │              └── const: 1 [type=int]
      │    ├── top-k
      │    │    ├── columns: t2.a:11(int!null) t2.b:12(int!null) t2.c:13(int!null) t3.a:16(int!null) t3.b:17(int!null) t3.c:18(int!null)
      │    │    ├── internal-ordering: +(12|17)
      │    │    ├── k: 1
      │    │    ├── cardinality: [0 - 1]
      │    │    ├── key: ()
      │    │    ├── fd: ()-->(11-13,16-18), (11)==(16), (16)==(11), (13)==(18), (18)==(13), (12)==(17), (17)==(12)
      │    │    ├── interesting orderings: (+(12|17))
      │    │    └── inner-join (cross)
      │    │         ├── columns: t2.a:11(int!null) t2.b:12(int!null) t2.c:13(int!null) t3.a:16(int!null) t3.b:17(int!null) t3.c:18(int!null)
      │    │         ├── fd: (11)-->(12,13), (16)-->(17,18), (11)==(16), (16)==(11), (13)==(18), (18)==(13), (12)==(17), (17)==(12)
      │    │         ├── interesting orderings: (+11) (+16)
      │    │         ├── scan t65038
      │    │         │    └── unfiltered-cols: (6-10)
      │    │         ├── inner-join (merge)
      │    │         │    ├── columns: t2.a:11(int!null) t2.b:12(int!null) t2.c:13(int!null) t3.a:16(int!null) t3.b:17(int!null) t3.c:18(int!null)
      │    │         │    ├── left ordering: +11,+12,+13
      │    │         │    ├── right ordering: +16,+17,+18
      │    │         │    ├── key: (16)
      │    │         │    ├── fd: (11)-->(12,13), (16)-->(17,18), (11)==(16), (16)==(11), (13)==(18), (18)==(13), (12)==(17), (17)==(12)
      │    │         │    ├── interesting orderings: (+11) (+16)
      │    │         │    ├── scan t65038 [as=t2]
      │    │         │    │    ├── columns: t2.a:11(int!null) t2.b:12(int) t2.c:13(int)
      │    │         │    │    ├── key: (11)
      │    │         │    │    ├── fd: (11)-->(12,13)
      │    │         │    │    ├── ordering: +11
      │    │         │    │    ├── prune: (11-13)
      │    │         │    │    ├── interesting orderings: (+11)
      │    │         │    │    └── unfiltered-cols: (11-15)
      │    │         │    ├── scan t65038 [as=t3]
      │    │         │    │    ├── columns: t3.a:16(int!null) t3.b:17(int) t3.c:18(int)
      │    │         │    │    ├── key: (16)
      │    │         │    │    ├── fd: (16)-->(17,18)
      │    │         │    │    ├── ordering: +16
      │    │         │    │    ├── prune: (16-18)
      │    │         │    │    ├── interesting orderings: (+16)
      │    │         │    │    └── unfiltered-cols: (16-20)
      │    │         │    └── filters (true)
      │    │         └── filters (true)
      │    └── filters (true)
      └── projections
           └── const: 1 [as="?column?":23, type=int]

opt
SELECT * FROM xyzs ORDER BY y DESC LIMIT 10
----
top-k
 ├── columns: x:1(int!null) y:2(int) z:3(float!null) s:4(string)
 ├── internal-ordering: -2
 ├── k: 10
 ├── cardinality: [0 - 10]
 ├── key: (1)
 ├── fd: (1)-->(2-4), (3,4)~~>(1,2)
 ├── ordering: -2
 ├── prune: (1,3,4)
 ├── interesting orderings: (-2)
 └── scan xyzs
      ├── columns: x:1(int!null) y:2(int) z:3(float!null) s:4(string)
      ├── key: (1)
      ├── fd: (1)-->(2-4), (3,4)~~>(1,2)
      ├── prune: (1-4)
      └── interesting orderings: (+1) (-4,+3,+1)

# TopK with a very high limit (> max uint32).
opt
SELECT * FROM xyzs ORDER BY y LIMIT 4294967296
----
top-k
 ├── columns: x:1(int!null) y:2(int) z:3(float!null) s:4(string)
 ├── internal-ordering: +2
 ├── k: 4294967296
 ├── key: (1)
 ├── fd: (1)-->(2-4), (3,4)~~>(1,2)
 ├── ordering: +2
 ├── prune: (1,3,4)
 ├── interesting orderings: (+2)
 └── scan xyzs
      ├── columns: x:1(int!null) y:2(int) z:3(float!null) s:4(string)
      ├── key: (1)
      ├── fd: (1)-->(2-4), (3,4)~~>(1,2)
      ├── prune: (1-4)
      └── interesting orderings: (+1) (-4,+3,+1)
