exec-ddl
CREATE TABLE a (x INT PRIMARY KEY, y INT)
----
TABLE a
 ├── x int not null
 ├── y int
 └── INDEX primary
      └── x int not null

exec-ddl
ALTER TABLE a INJECT STATISTICS '[
  {
    "columns": ["x"],
    "created_at": "2018-01-01 1:00:00.00000+00:00",
    "row_count": 5000,
    "distinct_count": 5000
  },
  {
    "columns": ["y"],
    "created_at": "2018-01-01 1:30:00.00000+00:00",
    "row_count": 4000,
    "distinct_count": 400
  }
]'
----

norm
SELECT * FROM (SELECT * FROM a WITH ORDINALITY) WHERE ordinality > 0 AND ordinality <= 10
----
select
 ├── columns: x:1(int!null) y:2(int) ordinality:3(int!null)
 ├── stats: [rows=10, distinct(1)=10, null(1)=0, distinct(3)=10, null(3)=0]
 ├── key: (1)
 ├── fd: (1)-->(2,3), (3)-->(1,2)
 ├── row-number
 │    ├── columns: x:1(int!null) y:2(int) ordinality:3(int!null)
 │    ├── stats: [rows=4000, distinct(1)=5000, null(1)=0, distinct(3)=4000, null(3)=0]
 │    ├── key: (1)
 │    ├── fd: (1)-->(2,3), (3)-->(1,2)
 │    └── scan a
 │         ├── columns: x:1(int!null) y:2(int)
 │         ├── stats: [rows=4000, distinct(1)=5000, null(1)=0]
 │         ├── key: (1)
 │         └── fd: (1)-->(2)
 └── filters
      └── (ordinality > 0) AND (ordinality <= 10) [type=bool, outer=(3), constraints=(/3: [/1 - /10]; tight)]

norm
SELECT * FROM (SELECT * FROM a WITH ORDINALITY) WHERE y > 0 AND y <= 10
----
select
 ├── columns: x:1(int!null) y:2(int!null) ordinality:3(int!null)
 ├── stats: [rows=100, distinct(1)=100, null(1)=0, distinct(2)=10, null(2)=0, distinct(3)=100, null(3)=0]
 ├── key: (1)
 ├── fd: (1)-->(2,3), (3)-->(1,2)
 ├── row-number
 │    ├── columns: x:1(int!null) y:2(int) ordinality:3(int!null)
 │    ├── stats: [rows=4000, distinct(1)=5000, null(1)=0, distinct(2)=400, null(2)=0, distinct(3)=4000, null(3)=0]
 │    ├── key: (1)
 │    ├── fd: (1)-->(2,3), (3)-->(1,2)
 │    └── scan a
 │         ├── columns: x:1(int!null) y:2(int)
 │         ├── stats: [rows=4000, distinct(1)=5000, null(1)=0, distinct(2)=400, null(2)=0]
 │         ├── key: (1)
 │         └── fd: (1)-->(2)
 └── filters
      └── (y > 0) AND (y <= 10) [type=bool, outer=(2), constraints=(/2: [/1 - /10]; tight)]

norm
SELECT 1 x FROM a WITH ORDINALITY
----
project
 ├── columns: x:4(int!null)
 ├── stats: [rows=4000]
 ├── fd: ()-->(4)
 ├── row-number
 │    ├── columns: ordinality:3(int!null)
 │    ├── stats: [rows=4000]
 │    ├── key: (3)
 │    └── scan a
 │         └── stats: [rows=4000]
 └── projections
      └── const: 1 [type=int]

norm
SELECT x FROM (SELECT * FROM a WITH ORDINALITY) WHERE ordinality > 0 AND ordinality <= 10
----
project
 ├── columns: x:1(int!null)
 ├── stats: [rows=10]
 ├── key: (1)
 └── select
      ├── columns: x:1(int!null) ordinality:3(int!null)
      ├── stats: [rows=10, distinct(1)=10, null(1)=0, distinct(3)=10, null(3)=0]
      ├── key: (1)
      ├── fd: (1)-->(3), (3)-->(1)
      ├── row-number
      │    ├── columns: x:1(int!null) ordinality:3(int!null)
      │    ├── stats: [rows=4000, distinct(1)=5000, null(1)=0, distinct(3)=4000, null(3)=0]
      │    ├── key: (1)
      │    ├── fd: (1)-->(3), (3)-->(1)
      │    └── scan a
      │         ├── columns: x:1(int!null)
      │         ├── stats: [rows=4000, distinct(1)=5000, null(1)=0]
      │         └── key: (1)
      └── filters
           └── (ordinality > 0) AND (ordinality <= 10) [type=bool, outer=(3), constraints=(/3: [/1 - /10]; tight)]


norm
SELECT * FROM (SELECT * FROM a WITH ORDINALITY) WHERE ordinality = 2
----
select
 ├── columns: x:1(int!null) y:2(int) ordinality:3(int!null)
 ├── cardinality: [0 - 1]
 ├── stats: [rows=1, distinct(1)=1, null(1)=0, distinct(3)=1, null(3)=0]
 ├── key: ()
 ├── fd: ()-->(1-3)
 ├── row-number
 │    ├── columns: x:1(int!null) y:2(int) ordinality:3(int!null)
 │    ├── stats: [rows=4000, distinct(1)=5000, null(1)=0, distinct(3)=4000, null(3)=0]
 │    ├── key: (1)
 │    ├── fd: (1)-->(2,3), (3)-->(1,2)
 │    └── scan a
 │         ├── columns: x:1(int!null) y:2(int)
 │         ├── stats: [rows=4000, distinct(1)=5000, null(1)=0]
 │         ├── key: (1)
 │         └── fd: (1)-->(2)
 └── filters
      └── ordinality = 2 [type=bool, outer=(3), constraints=(/3: [/2 - /2]; tight), fd=()-->(3)]

build
SELECT DISTINCT ordinality FROM (SELECT * FROM a WITH ORDINALITY)
----
distinct-on
 ├── columns: ordinality:3(int!null)
 ├── grouping columns: ordinality:3(int!null)
 ├── stats: [rows=4000, distinct(3)=4000, null(3)=0]
 ├── key: (3)
 └── project
      ├── columns: ordinality:3(int!null)
      ├── stats: [rows=4000, distinct(3)=4000, null(3)=0]
      ├── key: (3)
      └── row-number
           ├── columns: x:1(int!null) y:2(int) ordinality:3(int!null)
           ├── stats: [rows=4000, distinct(3)=4000, null(3)=0]
           ├── key: (1)
           ├── fd: (1)-->(2,3), (3)-->(1,2)
           └── scan a
                ├── columns: x:1(int!null) y:2(int)
                ├── stats: [rows=4000]
                ├── key: (1)
                └── fd: (1)-->(2)
