exec-ddl
CREATE TABLE a (x INT PRIMARY KEY, y INT, s STRING, d DECIMAL NOT NULL, UNIQUE (s DESC, d))
----
TABLE a
 ├── x int not null
 ├── y int
 ├── s string
 ├── d decimal not null
 ├── INDEX primary
 │    └── x int not null
 └── INDEX secondary
      ├── s string desc
      ├── d decimal not null
      └── x int not null (storing)

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)
 ├── select
 │    ├── columns: x:1(int!null) y:2(int) s:3(string!null) d:4(decimal!null)
 │    ├── stats: [rows=200, distinct(3)=1]
 │    ├── key: (1)
 │    ├── fd: ()-->(3), (1)-->(2,4), (4)-->(1,2)
 │    ├── scan a
 │    │    ├── columns: x:1(int!null) y:2(int) s:3(string) d:4(decimal!null)
 │    │    ├── stats: [rows=2000, distinct(3)=10]
 │    │    ├── key: (1)
 │    │    └── fd: (1)-->(2-4), (3,4)~~>(1,2)
 │    └── filters [type=bool, outer=(3), constraints=(/3: [/'foo' - /'foo']; tight), fd=()-->(3)]
 │         └── s = 'foo' [type=bool, outer=(3), constraints=(/3: [/'foo' - /'foo']; tight)]
 └── const: 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)
 ├── side-effects
 ├── 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)
 │    ├── stats: [rows=200, distinct(3)=1]
 │    ├── key: (1)
 │    ├── fd: ()-->(3), (1)-->(2,4), (4)-->(1,2)
 │    ├── scan a
 │    │    ├── columns: x:1(int!null) y:2(int) s:3(string) d:4(decimal!null)
 │    │    ├── stats: [rows=2000, distinct(3)=10]
 │    │    ├── key: (1)
 │    │    └── fd: (1)-->(2-4), (3,4)~~>(1,2)
 │    └── filters [type=bool, outer=(3), constraints=(/3: [/'foo' - /'foo']; tight), fd=()-->(3)]
 │         └── s = 'foo' [type=bool, outer=(3), constraints=(/3: [/'foo' - /'foo']; tight)]
 └── subquery [type=int]
      └── max1-row
           ├── columns: c:5(int!null)
           ├── cardinality: [1 - 1]
           ├── stats: [rows=1]
           ├── key: ()
           ├── fd: ()-->(5)
           └── project
                ├── columns: c:5(int!null)
                ├── cardinality: [1 - 1]
                ├── stats: [rows=1]
                ├── key: ()
                ├── fd: ()-->(5)
                ├── values
                │    ├── cardinality: [1 - 1]
                │    ├── stats: [rows=1]
                │    ├── key: ()
                │    └── tuple [type=tuple]
                └── projections
                     └── const: 5 [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.26952228, distinct(3)=1]
 ├── 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(3)=3.93848936]
 │    ├── 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(3)=10]
 │    │    ├── key: (1)
 │    │    ├── fd: (1)-->(2-4), (3,4)~~>(1,2)
 │    │    ├── ordering: +3
 │    │    └── scan a
 │    │         ├── columns: x:1(int!null) y:2(int) s:3(string) d:4(decimal!null)
 │    │         ├── stats: [rows=2000, distinct(3)=10]
 │    │         ├── key: (1)
 │    │         └── fd: (1)-->(2-4), (3,4)~~>(1,2)
 │    └── const: 5 [type=int]
 └── filters [type=bool, outer=(3), constraints=(/3: [/'foo' - /'foo']; tight), fd=()-->(3)]
      └── s = 'foo' [type=bool, outer=(3), constraints=(/3: [/'foo' - /'foo']; tight)]
