exec-ddl
CREATE TABLE a (k INT PRIMARY KEY, i INT, s STRING, d DECIMAL NOT NULL)
----
TABLE a
 ├── k int not null
 ├── i int
 ├── s string
 ├── d decimal not null
 └── INDEX primary
      └── k int not null

exec-ddl
CREATE TABLE b (x INT, z INT NOT NULL)
----
TABLE b
 ├── x int
 ├── z int not null
 ├── rowid int not null (hidden)
 └── INDEX primary
      └── rowid int not null (hidden)

opt
SELECT k, x FROM a INNER JOIN b ON k=x WHERE d=1.0
----
project
 ├── columns: k:1(int!null) x:5(int!null)
 ├── stats: [rows=100]
 ├── cost: 2124.725
 ├── fd: (1)==(5), (5)==(1)
 └── inner-join
      ├── columns: k:1(int!null) d:4(decimal!null) x:5(int!null)
      ├── stats: [rows=100, distinct(1)=10, null(1)=0, distinct(4)=1, null(4)=0, distinct(5)=10, null(5)=0]
      ├── cost: 2123.715
      ├── fd: ()-->(4), (1)==(5), (5)==(1)
      ├── scan b
      │    ├── columns: x:5(int)
      │    ├── stats: [rows=1000, distinct(5)=100, null(5)=10]
      │    └── cost: 1040.01
      ├── select
      │    ├── columns: k:1(int!null) d:4(decimal!null)
      │    ├── stats: [rows=10, distinct(1)=10, null(1)=0, distinct(4)=1, null(4)=0]
      │    ├── cost: 1070.02
      │    ├── key: (1)
      │    ├── fd: ()-->(4)
      │    ├── scan a
      │    │    ├── columns: k:1(int!null) d:4(decimal!null)
      │    │    ├── stats: [rows=1000, distinct(1)=1000, null(1)=0, distinct(4)=100, null(4)=0]
      │    │    ├── cost: 1060.01
      │    │    ├── key: (1)
      │    │    └── fd: (1)-->(4)
      │    └── filters
      │         └── d = 1.0 [type=bool, outer=(4), constraints=(/4: [/1.0 - /1.0]; tight), fd=()-->(4)]
      └── filters
           └── k = x [type=bool, outer=(1,5), constraints=(/1: (/NULL - ]; /5: (/NULL - ]), fd=(1)==(5), (5)==(1)]

# Verify that we pick merge join if we force it.
opt
SELECT k, x FROM a INNER MERGE JOIN b ON k=x
----
inner-join (merge)
 ├── columns: k:1(int!null) x:5(int!null)
 ├── left ordering: +1
 ├── right ordering: +5
 ├── stats: [rows=1000, distinct(1)=100, null(1)=0, distinct(5)=100, null(5)=0]
 ├── cost: 2339.35569
 ├── fd: (1)==(5), (5)==(1)
 ├── scan a
 │    ├── columns: k:1(int!null)
 │    ├── stats: [rows=1000, distinct(1)=1000, null(1)=0]
 │    ├── cost: 1050.01
 │    ├── key: (1)
 │    └── ordering: +1
 ├── sort
 │    ├── columns: x:5(int)
 │    ├── stats: [rows=1000, distinct(5)=100, null(5)=10]
 │    ├── cost: 1259.33569
 │    ├── ordering: +5
 │    └── scan b
 │         ├── columns: x:5(int)
 │         ├── stats: [rows=1000, distinct(5)=100, null(5)=10]
 │         └── cost: 1040.01
 └── filters (true)

# Verify that we pick lookup join if we force it. Note that lookup join is only
# possible if b is the left table.
opt
SELECT k, x FROM b INNER LOOKUP JOIN a ON k=x
----
inner-join (lookup a)
 ├── columns: k:4(int!null) x:1(int!null)
 ├── key columns: [1] = [4]
 ├── stats: [rows=1000, distinct(1)=100, null(1)=0, distinct(4)=100, null(4)=0]
 ├── cost: 6090.02
 ├── fd: (1)==(4), (4)==(1)
 ├── scan b
 │    ├── columns: x:1(int)
 │    ├── stats: [rows=1000, distinct(1)=100, null(1)=10]
 │    └── cost: 1040.01
 └── filters (true)


# Verify that if we force lookup join but one isn't possible, the hash join has
# huge cost (this will result in an error if we try to execbuild the result).
opt
SELECT k, x FROM a INNER LOOKUP JOIN b ON k=x
----
inner-join
 ├── columns: k:1(int!null) x:5(int!null)
 ├── flags: no-merge-join;no-hash-join
 ├── stats: [rows=1000, distinct(1)=100, null(1)=0, distinct(5)=100, null(5)=0]
 ├── cost: 1e+100
 ├── fd: (1)==(5), (5)==(1)
 ├── scan a
 │    ├── columns: k:1(int!null)
 │    ├── stats: [rows=1000, distinct(1)=1000, null(1)=0]
 │    ├── cost: 1050.01
 │    └── key: (1)
 ├── scan b
 │    ├── columns: x:5(int)
 │    ├── stats: [rows=1000, distinct(5)=100, null(5)=10]
 │    └── cost: 1040.01
 └── filters
      └── k = x [type=bool, outer=(1,5), constraints=(/1: (/NULL - ]; /5: (/NULL - ]), fd=(1)==(5), (5)==(1)]


exec-ddl
CREATE TABLE abc (a INT PRIMARY KEY, b INT, c INT, INDEX c_idx (c))
----
TABLE abc
 ├── a int not null
 ├── b int
 ├── c int
 ├── INDEX primary
 │    └── a int not null
 └── INDEX c_idx
      ├── c int
      └── a int not null

exec-ddl
ALTER TABLE abc INJECT STATISTICS '[
  {
    "columns": ["a"],
    "created_at": "2018-05-01 1:00:00.00000+00:00",
    "row_count": 500000000,
    "distinct_count": 500000000
  }
]'
----

# Check that we choose the index join when it makes sense.
opt
SELECT * FROM abc WHERE c = 1
----
index-join abc
 ├── columns: a:1(int!null) b:2(int) c:3(int!null)
 ├── stats: [rows=9.9, distinct(1)=9.9, null(1)=0, distinct(3)=1, null(3)=0]
 ├── cost: 50.609
 ├── key: (1)
 ├── fd: ()-->(3), (1)-->(2)
 └── scan abc@c_idx
      ├── columns: a:1(int!null) c:3(int!null)
      ├── constraint: /3/1: [/1 - /1]
      ├── stats: [rows=9.9, distinct(1)=9.9, null(1)=0, distinct(3)=1, null(3)=0]
      ├── cost: 10.306
      ├── key: (1)
      └── fd: ()-->(3)
