exec-ddl
CREATE TABLE abcd (a INT, b INT, c INT, INDEX (a,b))
----

exec-ddl
CREATE TABLE small (m INT, n INT)
----

exec-ddl
ALTER TABLE small INJECT STATISTICS '[
  {
    "columns": ["m"],
    "created_at": "2018-01-01 1:00:00.00000+00:00",
    "row_count": 10,
    "distinct_count": 10
  }
]'
----

# We can only test lookup stat generation when using non-covering indexes
# (that's when we create a group with LookupJoin). We can compare the
# statistics with the top-level join, they should be in the same ballpark.

opt
SELECT * FROM small JOIN abcd ON a=m
----
inner-join (lookup abcd)
 ├── columns: m:1(int!null) n:2(int) a:4(int!null) b:5(int) c:6(int)
 ├── key columns: [7] = [7]
 ├── stats: [rows=100, distinct(1)=10, null(1)=0, distinct(4)=10, null(4)=0]
 ├── fd: (1)==(4), (4)==(1)
 ├── inner-join (lookup abcd@secondary)
 │    ├── columns: m:1(int!null) n:2(int) a:4(int!null) b:5(int) abcd.rowid:7(int!null)
 │    ├── key columns: [1] = [4]
 │    ├── stats: [rows=100, distinct(1)=10, null(1)=0, distinct(4)=10, null(4)=0, distinct(7)=95.617925, null(7)=0]
 │    ├── fd: (7)-->(4,5), (1)==(4), (4)==(1)
 │    ├── scan small
 │    │    ├── columns: m:1(int) n:2(int)
 │    │    └── stats: [rows=10, distinct(1)=10, null(1)=0]
 │    └── filters (true)
 └── filters (true)

# Filter that gets pushed down on both sides, but comes back into the ON
# condition for the lookup side.
opt
SELECT * FROM small JOIN abcd ON a=m WHERE n > 2
----
inner-join (lookup abcd)
 ├── columns: m:1(int!null) n:2(int!null) a:4(int!null) b:5(int) c:6(int)
 ├── key columns: [7] = [7]
 ├── stats: [rows=33, distinct(1)=3.3, null(1)=0, distinct(2)=0.333333333, null(2)=0, distinct(4)=3.3, null(4)=0]
 ├── fd: (1)==(4), (4)==(1)
 ├── inner-join (lookup abcd@secondary)
 │    ├── columns: m:1(int!null) n:2(int!null) a:4(int!null) b:5(int) abcd.rowid:7(int!null)
 │    ├── key columns: [1] = [4]
 │    ├── stats: [rows=33, distinct(1)=3.3, null(1)=0, distinct(2)=0.333333333, null(2)=0, distinct(4)=3.3, null(4)=0, distinct(7)=32.6221433, null(7)=0]
 │    ├── fd: (7)-->(4,5), (1)==(4), (4)==(1)
 │    ├── select
 │    │    ├── columns: m:1(int) n:2(int!null)
 │    │    ├── stats: [rows=3.3, distinct(1)=3.3, null(1)=0, distinct(2)=0.333333333, null(2)=0]
 │    │    ├── scan small
 │    │    │    ├── columns: m:1(int) n:2(int)
 │    │    │    └── stats: [rows=10, distinct(1)=10, null(1)=0, distinct(2)=1, null(2)=0.1]
 │    │    └── filters
 │    │         └── n > 2 [type=bool, outer=(2), constraints=(/2: [/3 - ]; tight)]
 │    └── filters (true)
 └── filters (true)

# Filter that applies to the right side and gets pulled back into the ON
# condition.
opt
SELECT * FROM small JOIN abcd ON a=m WHERE b > 2
----
inner-join (lookup abcd)
 ├── columns: m:1(int!null) n:2(int) a:4(int!null) b:5(int!null) c:6(int)
 ├── key columns: [7] = [7]
 ├── stats: [rows=33.6127051, distinct(1)=10, null(1)=0, distinct(4)=10, null(4)=0, distinct(5)=21.2357454, null(5)=0]
 ├── fd: (1)==(4), (4)==(1)
 ├── inner-join (lookup abcd@secondary)
 │    ├── columns: m:1(int!null) n:2(int) a:4(int!null) b:5(int!null) abcd.rowid:7(int!null)
 │    ├── key columns: [1] = [4]
 │    ├── stats: [rows=33.3333333, distinct(1)=10, null(1)=0, distinct(4)=10, null(4)=0, distinct(5)=33.3333333, null(5)=0, distinct(7)=32.837752, null(7)=0]
 │    ├── fd: (7)-->(4,5), (1)==(4), (4)==(1)
 │    ├── scan small
 │    │    ├── columns: m:1(int) n:2(int)
 │    │    └── stats: [rows=10, distinct(1)=10, null(1)=0]
 │    └── filters
 │         └── b > 2 [type=bool, outer=(5), constraints=(/5: [/3 - ]; tight)]
 └── filters (true)

# Filter that can only be applied after the primary index join.
opt
SELECT * FROM small JOIN abcd ON a=m WHERE c>2
----
inner-join (lookup abcd)
 ├── columns: m:1(int!null) n:2(int) a:4(int!null) b:5(int) c:6(int!null)
 ├── key columns: [7] = [7]
 ├── stats: [rows=33.6127051, distinct(1)=10, null(1)=0, distinct(4)=10, null(4)=0, distinct(6)=21.2357454, null(6)=0]
 ├── fd: (1)==(4), (4)==(1)
 ├── inner-join (lookup abcd@secondary)
 │    ├── columns: m:1(int!null) n:2(int) a:4(int!null) b:5(int) abcd.rowid:7(int!null)
 │    ├── key columns: [1] = [4]
 │    ├── stats: [rows=100, distinct(1)=10, null(1)=0, distinct(4)=10, null(4)=0, distinct(7)=95.617925, null(7)=0]
 │    ├── fd: (7)-->(4,5), (1)==(4), (4)==(1)
 │    ├── scan small
 │    │    ├── columns: m:1(int) n:2(int)
 │    │    └── stats: [rows=10, distinct(1)=10, null(1)=0]
 │    └── filters (true)
 └── filters
      └── c > 2 [type=bool, outer=(6), constraints=(/6: [/3 - ]; tight)]

# Multiple equalities.
opt
SELECT * FROM small JOIN abcd ON a=m AND b=n WHERE c>2
----
inner-join (lookup abcd)
 ├── columns: m:1(int!null) n:2(int!null) a:4(int!null) b:5(int!null) c:6(int!null)
 ├── key columns: [7] = [7]
 ├── stats: [rows=0.342367862, distinct(1)=0.342367862, null(1)=0, distinct(2)=0.342367862, null(2)=0, distinct(4)=0.342367862, null(4)=0, distinct(5)=0.342367862, null(5)=0, distinct(6)=0.340633209, null(6)=0]
 ├── fd: (1)==(4), (4)==(1), (2)==(5), (5)==(2)
 ├── inner-join (lookup abcd@secondary)
 │    ├── columns: m:1(int!null) n:2(int!null) a:4(int!null) b:5(int!null) abcd.rowid:7(int!null)
 │    ├── key columns: [1 2] = [4 5]
 │    ├── stats: [rows=1, distinct(1)=1, null(1)=0, distinct(2)=1, null(2)=0, distinct(4)=1, null(4)=0, distinct(5)=1, null(5)=0, distinct(7)=0.99955012, null(7)=0]
 │    ├── fd: (7)-->(4,5), (1)==(4), (4)==(1), (2)==(5), (5)==(2)
 │    ├── scan small
 │    │    ├── columns: m:1(int) n:2(int)
 │    │    └── stats: [rows=10, distinct(1)=10, null(1)=0, distinct(2)=1, null(2)=0.1]
 │    └── filters (true)
 └── filters
      └── c > 2 [type=bool, outer=(6), constraints=(/6: [/3 - ]; tight)]

exec-ddl
CREATE TABLE abc (a INT, b INT, c INT, PRIMARY KEY (a, c))
----

exec-ddl
CREATE TABLE def (d INT, e INT, f INT, g FLOAT, PRIMARY KEY (f, e), INDEX e_idx (e) STORING (d), INDEX d_idx (d))
----

# Set up the statistics as if the first table is much smaller than the second.
exec-ddl
ALTER TABLE abc INJECT STATISTICS '[
  {
    "columns": ["a"],
    "created_at": "2018-01-01 1:00:00.00000+00:00",
    "row_count": 100,
    "distinct_count": 100
  }
]'
----

exec-ddl
ALTER TABLE def INJECT STATISTICS '[
  {
    "columns": ["d"],
    "created_at": "2018-01-01 1:00:00.00000+00:00",
    "row_count": 10000,
    "distinct_count": 1000
  },
  {
    "columns": ["e"],
    "created_at": "2018-01-01 1:00:00.00000+00:00",
    "row_count": 10000,
    "distinct_count": 100
  },
  {
    "columns": ["f"],
    "created_at": "2018-01-01 1:00:00.00000+00:00",
    "row_count": 10000,
    "distinct_count": 10000
  }
]'
----

# The filter a=f is selective, so we expect a lookup join.
opt
SELECT a, b, c, d, e, f FROM abc JOIN def ON a = f
----
inner-join (lookup def)
 ├── columns: a:1(int!null) b:2(int) c:3(int!null) d:4(int) e:5(int!null) f:6(int!null)
 ├── key columns: [1] = [6]
 ├── stats: [rows=100, distinct(1)=100, null(1)=0, distinct(3)=9.99954623, null(3)=0, distinct(5)=63.2138954, null(5)=0, distinct(6)=100, null(6)=0]
 ├── key: (3,5,6)
 ├── fd: (1,3)-->(2), (5,6)-->(4), (1)==(6), (6)==(1)
 ├── scan abc
 │    ├── columns: a:1(int!null) b:2(int) c:3(int!null)
 │    ├── stats: [rows=100, distinct(1)=100, null(1)=0, distinct(3)=10, null(3)=0]
 │    ├── key: (1,3)
 │    └── fd: (1,3)-->(2)
 └── filters (true)

# The filter a=e is not very selective, so we do not expect a lookup join.
opt
SELECT a, b, c, d, e, f FROM abc JOIN def ON a = e
----
inner-join (merge)
 ├── columns: a:1(int!null) b:2(int) c:3(int!null) d:4(int) e:5(int!null) f:6(int!null)
 ├── left ordering: +1
 ├── right ordering: +5
 ├── stats: [rows=10000, distinct(1)=100, null(1)=0, distinct(3)=10, null(3)=0, distinct(5)=100, null(5)=0, distinct(6)=6339.67659, null(6)=0]
 ├── key: (3,5,6)
 ├── fd: (1,3)-->(2), (5,6)-->(4), (1)==(5), (5)==(1)
 ├── scan abc
 │    ├── columns: a:1(int!null) b:2(int) c:3(int!null)
 │    ├── stats: [rows=100, distinct(1)=100, null(1)=0, distinct(3)=10, null(3)=0]
 │    ├── key: (1,3)
 │    ├── fd: (1,3)-->(2)
 │    └── ordering: +1
 ├── scan def@e_idx
 │    ├── columns: d:4(int) e:5(int!null) f:6(int!null)
 │    ├── stats: [rows=10000, distinct(5)=100, null(5)=0, distinct(6)=10000, null(6)=0]
 │    ├── key: (5,6)
 │    ├── fd: (5,6)-->(4)
 │    └── ordering: +5
 └── filters (true)

# Check column statistics for lookup join.
opt colstat=1 colstat=2 colstat=3 colstat=4 colstat=5 colstat=6 colstat=(2,5,6)
SELECT a, b, c, d, e, f FROM abc JOIN DEF ON a = f
----
inner-join (lookup def)
 ├── columns: a:1(int!null) b:2(int) c:3(int!null) d:4(int) e:5(int!null) f:6(int!null)
 ├── key columns: [1] = [6]
 ├── stats: [rows=100, distinct(1)=100, null(1)=0, distinct(2)=9.99954623, null(2)=1, distinct(3)=9.99954623, null(3)=0, distinct(4)=95.1671064, null(4)=0, distinct(5)=63.2138954, null(5)=0, distinct(6)=100, null(6)=0, distinct(2,5,6)=100, null(2,5,6)=1]
 ├── key: (3,5,6)
 ├── fd: (1,3)-->(2), (5,6)-->(4), (1)==(6), (6)==(1)
 ├── scan abc
 │    ├── columns: a:1(int!null) b:2(int) c:3(int!null)
 │    ├── stats: [rows=100, distinct(1)=100, null(1)=0, distinct(2)=10, null(2)=1, distinct(3)=10, null(3)=0]
 │    ├── key: (1,3)
 │    └── fd: (1,3)-->(2)
 └── filters (true)

# Check column statistics for double lookup join.
opt colstat=7
SELECT * FROM abc LEFT JOIN DEF ON a = d AND b = 3
----
left-join (lookup def)
 ├── columns: a:1(int!null) b:2(int) c:3(int!null) d:4(int) e:5(int) f:6(int) g:7(float)
 ├── key columns: [6 5] = [6 5]
 ├── stats: [rows=100, distinct(4)=100, null(4)=0, distinct(7)=95.1671064, null(7)=1]
 ├── key: (1,3,5,6)
 ├── fd: (1,3)-->(2), (5,6)-->(4,7)
 ├── left-join (lookup def@d_idx)
 │    ├── columns: a:1(int!null) b:2(int) c:3(int!null) d:4(int) e:5(int) f:6(int)
 │    ├── key columns: [1] = [4]
 │    ├── stats: [rows=100, distinct(4)=100, null(4)=0]
 │    ├── key: (1,3,5,6)
 │    ├── fd: (1,3)-->(2), (5,6)-->(4)
 │    ├── scan abc
 │    │    ├── columns: a:1(int!null) b:2(int) c:3(int!null)
 │    │    ├── stats: [rows=100, distinct(1)=100, null(1)=0, distinct(2)=10, null(2)=1, distinct(3)=10, null(3)=0]
 │    │    ├── key: (1,3)
 │    │    └── fd: (1,3)-->(2)
 │    └── filters
 │         └── b = 3 [type=bool, outer=(2), constraints=(/2: [/3 - /3]; tight), fd=()-->(2)]
 └── filters (true)

# The filter a=e is not very selective, so we do not expect a lookup join, even
# though there is an additional filter.
opt colstat=7
SELECT * FROM abc LEFT JOIN DEF ON a = e AND b = 3
----
right-join (hash)
 ├── columns: a:1(int!null) b:2(int) c:3(int!null) d:4(int) e:5(int) f:6(int) g:7(float)
 ├── stats: [rows=1000, distinct(5)=100, null(5)=0, distinct(7)=632.304575, null(7)=10]
 ├── key: (1,3,5,6)
 ├── fd: (1,3)-->(2), (5,6)-->(4,7)
 ├── scan def
 │    ├── columns: d:4(int) e:5(int!null) f:6(int!null) g:7(float)
 │    ├── stats: [rows=10000, distinct(5)=100, null(5)=0, distinct(7)=1000, null(7)=100]
 │    ├── key: (5,6)
 │    └── fd: (5,6)-->(4,7)
 ├── scan abc
 │    ├── columns: a:1(int!null) b:2(int) c:3(int!null)
 │    ├── stats: [rows=100, distinct(1)=100, null(1)=0, distinct(2)=10, null(2)=1, distinct(3)=10, null(3)=0]
 │    ├── key: (1,3)
 │    └── fd: (1,3)-->(2)
 └── filters
      ├── a = e [type=bool, outer=(1,5), constraints=(/1: (/NULL - ]; /5: (/NULL - ]), fd=(1)==(5), (5)==(1)]
      └── b = 3 [type=bool, outer=(2), constraints=(/2: [/3 - /3]; tight), fd=()-->(2)]

exec-ddl
CREATE TABLE t (x INT, y INT, INDEX x_idx (x) STORING (y), INDEX y_idx (y) STORING (x), INDEX xy_idx (x, y))
----

exec-ddl
CREATE TABLE u (x INT, y INT, INDEX x_idx (x) STORING (y), INDEX y_idx (y) STORING (x), INDEX xy_idx (x, y))
----

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

exec-ddl
ALTER TABLE u INJECT STATISTICS '[
  {
    "columns": ["x"],
    "created_at": "2018-01-01 1:00:00.00000+00:00",
    "row_count": 10,
    "distinct_count": 2
  },
  {
    "columns": ["y"],
    "created_at": "2018-01-01 1:00:00.00000+00:00",
    "row_count": 10,
    "distinct_count": 2
  }
]'
----

# Test that the correct index is used for the lookup join.
opt
SELECT * FROM u WHERE EXISTS (SELECT * FROM t WHERE u.x=t.x AND u.y=t.y);
----
semi-join (lookup t@xy_idx)
 ├── columns: x:1(int) y:2(int)
 ├── key columns: [1 2] = [4 5]
 ├── stats: [rows=10, distinct(1)=2, null(1)=0, distinct(2)=2, null(2)=0]
 ├── scan u
 │    ├── columns: u.x:1(int) u.y:2(int)
 │    └── stats: [rows=10, distinct(1)=2, null(1)=0, distinct(2)=2, null(2)=0]
 └── filters (true)

opt
SELECT * FROM u WHERE NOT EXISTS (SELECT * FROM t WHERE u.x=t.x AND u.y=t.y);
----
anti-join (lookup t@xy_idx)
 ├── columns: x:1(int) y:2(int)
 ├── key columns: [1 2] = [4 5]
 ├── stats: [rows=1e-10]
 ├── scan u
 │    ├── columns: u.x:1(int) u.y:2(int)
 │    └── stats: [rows=10, distinct(1)=2, null(1)=0, distinct(2)=2, null(2)=0]
 └── filters (true)
