exec-ddl
CREATE TABLE a (x INT PRIMARY KEY, y INT, s STRING, d DECIMAL NOT NULL, UNIQUE (s DESC, d))
----

exec-ddl
ALTER TABLE a INJECT STATISTICS '[
  {
    "columns": ["x"],
    "created_at": "2018-01-01 1:00:00.00000+00:00",
    "row_count": 2000,
    "distinct_count": 2000
  },
  {
    "columns": ["x","y"],
    "created_at": "2018-01-01 1:30:00.00000+00:00",
    "row_count": 2000,
    "distinct_count": 2000
  },
  {
    "columns": ["s"],
    "created_at": "2018-01-01 1:30:00.00000+00:00",
    "row_count": 2000,
    "distinct_count": 10
  },
  {
    "columns": ["s","y"],
    "created_at": "2018-01-01 1:40:00.00000+00:00",
    "row_count": 2000,
    "distinct_count": 100
  }
]'
----

build
SELECT * FROM a WHERE s = 'foo' LIMIT 5
----
limit
 ├── columns: x:1(int!null) y:2(int) s:3(string!null) d:4(decimal!null)
 ├── cardinality: [0 - 5]
 ├── stats: [rows=5]
 ├── key: (1)
 ├── fd: ()-->(3), (1)-->(2,4), (4)-->(1,2)
 ├── project
 │    ├── columns: x:1(int!null) y:2(int) s:3(string!null) d:4(decimal!null)
 │    ├── stats: [rows=200]
 │    ├── key: (1)
 │    ├── fd: ()-->(3), (1)-->(2,4), (4)-->(1,2)
 │    ├── limit hint: 5.00
 │    └── select
 │         ├── columns: x:1(int!null) y:2(int) s:3(string!null) d:4(decimal!null) crdb_internal_mvcc_timestamp:5(decimal) tableoid:6(oid)
 │         ├── stats: [rows=200, distinct(3)=1, null(3)=0]
 │         ├── key: (1)
 │         ├── fd: ()-->(3), (1)-->(2,4-6), (4)-->(1,2,5,6)
 │         ├── limit hint: 5.00
 │         ├── scan a
 │         │    ├── columns: x:1(int!null) y:2(int) s:3(string) d:4(decimal!null) crdb_internal_mvcc_timestamp:5(decimal) tableoid:6(oid)
 │         │    ├── stats: [rows=2000, distinct(1)=2000, null(1)=0, distinct(3)=10, null(3)=0, distinct(4)=200, null(4)=0]
 │         │    ├── key: (1)
 │         │    ├── fd: (1)-->(2-6), (3,4)~~>(1,2,5,6)
 │         │    └── limit hint: 50.00
 │         └── filters
 │              └── s:3 = 'foo' [type=bool, outer=(3), constraints=(/3: [/'foo' - /'foo']; tight), fd=()-->(3)]
 └── 5 [type=int]

build
SELECT * FROM a WHERE s = 'foo' LIMIT (SELECT 5 AS c)
----
limit
 ├── columns: x:1(int!null) y:2(int) s:3(string!null) d:4(decimal!null)
 ├── immutable
 ├── stats: [rows=200]
 ├── key: (1)
 ├── fd: ()-->(3), (1)-->(2,4), (4)-->(1,2)
 ├── project
 │    ├── columns: x:1(int!null) y:2(int) s:3(string!null) d:4(decimal!null)
 │    ├── stats: [rows=200]
 │    ├── key: (1)
 │    ├── fd: ()-->(3), (1)-->(2,4), (4)-->(1,2)
 │    └── select
 │         ├── columns: x:1(int!null) y:2(int) s:3(string!null) d:4(decimal!null) crdb_internal_mvcc_timestamp:5(decimal) tableoid:6(oid)
 │         ├── stats: [rows=200, distinct(3)=1, null(3)=0]
 │         ├── key: (1)
 │         ├── fd: ()-->(3), (1)-->(2,4-6), (4)-->(1,2,5,6)
 │         ├── scan a
 │         │    ├── columns: x:1(int!null) y:2(int) s:3(string) d:4(decimal!null) crdb_internal_mvcc_timestamp:5(decimal) tableoid:6(oid)
 │         │    ├── stats: [rows=2000, distinct(1)=2000, null(1)=0, distinct(3)=10, null(3)=0, distinct(4)=200, null(4)=0]
 │         │    ├── key: (1)
 │         │    └── fd: (1)-->(2-6), (3,4)~~>(1,2,5,6)
 │         └── filters
 │              └── s:3 = 'foo' [type=bool, outer=(3), constraints=(/3: [/'foo' - /'foo']; tight), fd=()-->(3)]
 └── subquery [type=int]
      └── max1-row
           ├── columns: c:7(int!null)
           ├── error: "more than one row returned by a subquery used as an expression"
           ├── cardinality: [1 - 1]
           ├── stats: [rows=1]
           ├── key: ()
           ├── fd: ()-->(7)
           └── project
                ├── columns: c:7(int!null)
                ├── cardinality: [1 - 1]
                ├── stats: [rows=1]
                ├── key: ()
                ├── fd: ()-->(7)
                ├── values
                │    ├── cardinality: [1 - 1]
                │    ├── stats: [rows=1]
                │    ├── key: ()
                │    └── () [type=tuple]
                └── projections
                     └── 5 [as=c:7, type=int]

build
SELECT * FROM (SELECT * FROM a ORDER BY s LIMIT 5) WHERE s = 'foo'
----
select
 ├── columns: x:1(int!null) y:2(int) s:3(string!null) d:4(decimal!null)
 ├── cardinality: [0 - 5]
 ├── stats: [rows=1.269522, distinct(3)=1, null(3)=0]
 ├── key: (1)
 ├── fd: ()-->(3), (1)-->(2,4), (4)-->(1,2)
 ├── limit
 │    ├── columns: x:1(int!null) y:2(int) s:3(string) d:4(decimal!null)
 │    ├── internal-ordering: +3
 │    ├── cardinality: [0 - 5]
 │    ├── stats: [rows=5, distinct(1)=5, null(1)=0, distinct(3)=3.93849, null(3)=0, distinct(4)=4.94412, null(4)=0]
 │    ├── key: (1)
 │    ├── fd: (1)-->(2-4), (3,4)~~>(1,2)
 │    ├── sort
 │    │    ├── columns: x:1(int!null) y:2(int) s:3(string) d:4(decimal!null)
 │    │    ├── stats: [rows=2000, distinct(1)=2000, null(1)=0, distinct(3)=10, null(3)=0, distinct(4)=200, null(4)=0]
 │    │    ├── key: (1)
 │    │    ├── fd: (1)-->(2-4), (3,4)~~>(1,2)
 │    │    ├── ordering: +3
 │    │    ├── limit hint: 5.00
 │    │    └── project
 │    │         ├── columns: x:1(int!null) y:2(int) s:3(string) d:4(decimal!null)
 │    │         ├── stats: [rows=2000, distinct(1)=2000, null(1)=0, distinct(3)=10, null(3)=0, distinct(4)=200, null(4)=0]
 │    │         ├── key: (1)
 │    │         ├── fd: (1)-->(2-4), (3,4)~~>(1,2)
 │    │         └── scan a
 │    │              ├── columns: x:1(int!null) y:2(int) s:3(string) d:4(decimal!null) crdb_internal_mvcc_timestamp:5(decimal) tableoid:6(oid)
 │    │              ├── stats: [rows=2000, distinct(1)=2000, null(1)=0, distinct(3)=10, null(3)=0, distinct(4)=200, null(4)=0]
 │    │              ├── key: (1)
 │    │              └── fd: (1)-->(2-6), (3,4)~~>(1,2,5,6)
 │    └── 5 [type=int]
 └── filters
      └── s:3 = 'foo' [type=bool, outer=(3), constraints=(/3: [/'foo' - /'foo']; tight), fd=()-->(3)]

# Bump up null counts.
exec-ddl
ALTER TABLE a INJECT STATISTICS '[
  {
    "columns": ["x"],
    "created_at": "2018-01-01 1:00:00.00000+00:00",
    "row_count": 2000,
    "distinct_count": 2000
  },
  {
    "columns": ["x","y"],
    "created_at": "2018-01-01 1:30:00.00000+00:00",
    "row_count": 2000,
    "distinct_count": 2000,
    "null_count": 1000
  },
  {
    "columns": ["y"],
    "created_at": "2018-01-01 1:30:00.00000+00:00",
    "row_count": 2000,
    "distinct_count": 501,
    "null_count": 1000
  },
  {
    "columns": ["s"],
    "created_at": "2018-01-01 1:30:00.00000+00:00",
    "row_count": 2000,
    "distinct_count": 11,
    "null_count": 1000
  }
]'
----

build colstat=2 colstat=3 colstat=(2,3)
SELECT * FROM a LIMIT 5
----
limit
 ├── columns: x:1(int!null) y:2(int) s:3(string) d:4(decimal!null)
 ├── cardinality: [0 - 5]
 ├── stats: [rows=5, distinct(2)=4.98133, null(2)=2.5, distinct(3)=4.02187, null(3)=2.5, distinct(2,3)=5, null(2,3)=1.25]
 ├── key: (1)
 ├── fd: (1)-->(2-4), (3,4)~~>(1,2)
 ├── project
 │    ├── columns: x:1(int!null) y:2(int) s:3(string) d:4(decimal!null)
 │    ├── stats: [rows=2000, distinct(2)=501, null(2)=1000, distinct(3)=11, null(3)=1000, distinct(2,3)=2000, null(2,3)=500]
 │    ├── key: (1)
 │    ├── fd: (1)-->(2-4), (3,4)~~>(1,2)
 │    ├── limit hint: 5.00
 │    └── scan a
 │         ├── columns: x:1(int!null) y:2(int) s:3(string) d:4(decimal!null) crdb_internal_mvcc_timestamp:5(decimal) tableoid:6(oid)
 │         ├── stats: [rows=2000, distinct(2)=501, null(2)=1000, distinct(3)=11, null(3)=1000, distinct(2,3)=2000, null(2,3)=500]
 │         ├── key: (1)
 │         ├── fd: (1)-->(2-6), (3,4)~~>(1,2,5,6)
 │         └── limit hint: 5.00
 └── 5 [type=int]

build colstat=2 colstat=3 colstat=(2,3)
SELECT * FROM a WHERE s = 'foo' LIMIT 5
----
limit
 ├── columns: x:1(int!null) y:2(int) s:3(string!null) d:4(decimal!null)
 ├── cardinality: [0 - 5]
 ├── stats: [rows=5, distinct(2)=4.9901, null(2)=2.5, distinct(3)=0.994079, null(3)=0, distinct(2,3)=4.9901, null(2,3)=0]
 ├── key: (1)
 ├── fd: ()-->(3), (1)-->(2,4), (4)-->(1,2)
 ├── project
 │    ├── columns: x:1(int!null) y:2(int) s:3(string!null) d:4(decimal!null)
 │    ├── stats: [rows=100, distinct(2)=92.7652, null(2)=50, distinct(3)=1, null(3)=0, distinct(2,3)=92.7652, null(2,3)=0]
 │    ├── key: (1)
 │    ├── fd: ()-->(3), (1)-->(2,4), (4)-->(1,2)
 │    ├── limit hint: 5.00
 │    └── select
 │         ├── columns: x:1(int!null) y:2(int) s:3(string!null) d:4(decimal!null) crdb_internal_mvcc_timestamp:5(decimal) tableoid:6(oid)
 │         ├── stats: [rows=100, distinct(2)=92.7652, null(2)=50, distinct(3)=1, null(3)=0, distinct(2,3)=92.7652, null(2,3)=0]
 │         ├── key: (1)
 │         ├── fd: ()-->(3), (1)-->(2,4-6), (4)-->(1,2,5,6)
 │         ├── limit hint: 5.00
 │         ├── scan a
 │         │    ├── columns: x:1(int!null) y:2(int) s:3(string) d:4(decimal!null) crdb_internal_mvcc_timestamp:5(decimal) tableoid:6(oid)
 │         │    ├── stats: [rows=2000, distinct(1)=2000, null(1)=0, distinct(2)=501, null(2)=1000, distinct(3)=11, null(3)=1000, distinct(4)=200, null(4)=0, distinct(2,3)=2000, null(2,3)=500]
 │         │    ├── key: (1)
 │         │    ├── fd: (1)-->(2-6), (3,4)~~>(1,2,5,6)
 │         │    └── limit hint: 100.00
 │         └── filters
 │              └── s:3 = 'foo' [type=bool, outer=(3), constraints=(/3: [/'foo' - /'foo']; tight), fd=()-->(3)]
 └── 5 [type=int]

exec-ddl
CREATE TABLE b (x int)
----

# Regression test for #32578. Ensure that we don't estimate 0 rows for the
# offset.
opt colstat=1
SELECT * FROM b ORDER BY x LIMIT 1 OFFSET 9999
----
offset
 ├── columns: x:1(int)
 ├── internal-ordering: +1
 ├── cardinality: [0 - 1]
 ├── stats: [rows=1, distinct(1)=0.995512, null(1)=0.01]
 ├── ordering: +1
 ├── top-k
 │    ├── columns: x:1(int)
 │    ├── internal-ordering: +1
 │    ├── k: 10000
 │    ├── cardinality: [0 - 10000]
 │    ├── stats: [rows=1000, distinct(1)=100, null(1)=10]
 │    ├── ordering: +1
 │    └── scan b
 │         ├── columns: x:1(int)
 │         └── stats: [rows=1000, distinct(1)=100, null(1)=10]
 └── 9999 [type=int]

# TopK
opt colstat=1
SELECT * FROM a ORDER BY y LIMIT 500
----
top-k
 ├── columns: x:1(int!null) y:2(int) s:3(string) d:4(decimal!null)
 ├── internal-ordering: +2
 ├── k: 500
 ├── cardinality: [0 - 500]
 ├── stats: [rows=500, distinct(1)=500, null(1)=0]
 ├── key: (1)
 ├── fd: (1)-->(2-4), (3,4)~~>(1,2)
 ├── ordering: +2
 └── scan a
      ├── columns: x:1(int!null) y:2(int) s:3(string) d:4(decimal!null)
      ├── stats: [rows=2000, distinct(1)=2000, null(1)=0]
      ├── key: (1)
      └── fd: (1)-->(2-4), (3,4)~~>(1,2)
