exec-ddl
CREATE TABLE a (x INT PRIMARY KEY, y INT, s STRING, d DECIMAL NOT NULL, b BOOL, 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
  }
]'
----

build
SELECT * FROM a
----
scan a
 ├── columns: x:1(int!null) y:2(int) s:3(string) d:4(decimal!null) b:5(bool)
 ├── stats: [rows=2000]
 ├── key: (1)
 └── fd: (1)-->(2-5), (3,4)~~>(1,2,5)

# Check that boolean columns have distinct count 2 when there are no stats
# available.
build
SELECT * FROM a WHERE b
----
select
 ├── columns: x:1(int!null) y:2(int) s:3(string) d:4(decimal!null) b:5(bool!null)
 ├── stats: [rows=990, distinct(5)=1, null(5)=0]
 ├── key: (1)
 ├── fd: ()-->(5), (1)-->(2-4), (3,4)~~>(1,2)
 ├── scan a
 │    ├── columns: x:1(int!null) y:2(int) s:3(string) d:4(decimal!null) b:5(bool)
 │    ├── stats: [rows=2000, distinct(1)=2000, null(1)=0, distinct(4)=200, null(4)=0, distinct(5)=3, null(5)=20]
 │    ├── key: (1)
 │    └── fd: (1)-->(2-5), (3,4)~~>(1,2,5)
 └── filters
      └── variable: b [type=bool, outer=(5), constraints=(/5: [/true - /true]; tight), fd=()-->(5)]

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:00:00.00000+00:00",
    "row_count": 2000,
    "distinct_count": 2000
  },
  {
    "columns": ["y"],
    "created_at": "2018-01-01 1:30:00.00000+00:00",
    "row_count": 2000,
    "distinct_count": 400
  },
  {
    "columns": ["y"],
    "created_at": "2018-01-01 2:00:00.00000+00:00",
    "row_count": 3000,
    "distinct_count": 500
  },
  {
    "columns": ["s"],
    "created_at": "2018-01-01 2:00:00.00000+00:00",
    "row_count": 3000,
    "distinct_count": 2
  }
]'
----

build
SELECT * FROM a
----
scan a
 ├── columns: x:1(int!null) y:2(int) s:3(string) d:4(decimal!null) b:5(bool)
 ├── stats: [rows=3000]
 ├── key: (1)
 └── fd: (1)-->(2-5), (3,4)~~>(1,2,5)

# Test constrained scan.
opt
SELECT s, x FROM a WHERE x > 0 AND x <= 100
----
scan a
 ├── columns: s:3(string) x:1(int!null)
 ├── constraint: /1: [/1 - /100]
 ├── cardinality: [0 - 100]
 ├── stats: [rows=100, distinct(1)=100, null(1)=0]
 ├── key: (1)
 └── fd: (1)-->(3)

# Test limited scan.
opt
SELECT s, x FROM a WHERE x > 5 AND x <= 10 LIMIT 2
----
scan a
 ├── columns: s:3(string) x:1(int!null)
 ├── constraint: /1: [/6 - /10]
 ├── limit: 2
 ├── stats: [rows=2]
 ├── key: (1)
 └── fd: (1)-->(3)

opt
SELECT count(*), y, x FROM a WHERE x > 0 AND x <= 100 GROUP BY x, y
----
group-by
 ├── columns: count:6(int) y:2(int) x:1(int!null)
 ├── grouping columns: x:1(int!null)
 ├── internal-ordering: +1
 ├── cardinality: [0 - 100]
 ├── stats: [rows=100, distinct(1)=100, null(1)=0]
 ├── key: (1)
 ├── fd: (1)-->(2,6)
 ├── scan a
 │    ├── columns: x:1(int!null) y:2(int)
 │    ├── constraint: /1: [/1 - /100]
 │    ├── cardinality: [0 - 100]
 │    ├── stats: [rows=100, distinct(1)=100, null(1)=0]
 │    ├── key: (1)
 │    ├── fd: (1)-->(2)
 │    └── ordering: +1
 └── aggregations
      ├── count-rows [type=int]
      └── const-agg [type=int, outer=(2)]
           └── variable: y [type=int]

# Test calculation of multi-column stats.
opt
SELECT y, s FROM a GROUP BY y, s
----
distinct-on
 ├── columns: y:2(int) s:3(string)
 ├── grouping columns: y:2(int) s:3(string)
 ├── stats: [rows=1000, distinct(2,3)=1000, null(2,3)=0]
 ├── key: (2,3)
 └── scan a
      ├── columns: y:2(int) s:3(string)
      └── stats: [rows=3000, distinct(2,3)=1000, null(2,3)=0]

opt
SELECT s, d, x FROM a WHERE (s <= 'aaa') OR (s >= 'bar' AND s <= 'foo')
----
select
 ├── columns: s:3(string!null) d:4(decimal!null) x:1(int!null)
 ├── stats: [rows=1500, distinct(3)=1, null(3)=0]
 ├── key: (1)
 ├── fd: (1)-->(3,4), (3,4)-->(1)
 ├── scan a@secondary
 │    ├── columns: x:1(int!null) s:3(string!null) d:4(decimal!null)
 │    ├── constraint: /-3/4
 │    │    ├── [/'foo' - /'bar']
 │    │    └── [/'aaa' - /NULL)
 │    ├── stats: [rows=1500, distinct(3)=1, null(3)=0]
 │    ├── key: (1)
 │    └── fd: (1)-->(3,4), (3,4)-->(1)
 └── filters
      └── (s <= 'aaa') OR (s >= 'bar') [type=bool, outer=(3), constraints=(/3: (/NULL - /'aaa'] [/'bar' - ]; tight)]

opt
SELECT s, d, x FROM a WHERE (s <= 'aaa') OR (s >= 'bar' AND s <= 'foo') OR s IS NULL
----
select
 ├── columns: s:3(string) d:4(decimal!null) x:1(int!null)
 ├── stats: [rows=1500, distinct(3)=1, null(3)=0]
 ├── key: (1)
 ├── fd: (1)-->(3,4), (3,4)~~>(1)
 ├── scan a@secondary
 │    ├── columns: x:1(int!null) s:3(string) d:4(decimal!null)
 │    ├── constraint: /-3/4
 │    │    ├── [/'foo' - /'bar']
 │    │    └── [/'aaa' - /NULL]
 │    ├── stats: [rows=1500, distinct(3)=1, null(3)=0]
 │    ├── key: (1)
 │    └── fd: (1)-->(3,4), (3,4)~~>(1)
 └── filters
      └── ((s <= 'aaa') OR ((s >= 'bar') AND (s <= 'foo'))) OR (s IS NULL) [type=bool, outer=(3), constraints=(/3: [/NULL - /'aaa'] [/'bar' - /'foo']; tight)]

opt
SELECT s, d, x FROM a WHERE s IS NOT NULL
----
scan a@secondary
 ├── columns: s:3(string!null) d:4(decimal!null) x:1(int!null)
 ├── constraint: /-3/4: [ - /NULL)
 ├── stats: [rows=3000, distinct(3)=2, null(3)=0]
 ├── key: (1)
 └── fd: (1)-->(3,4), (3,4)-->(1)

opt
SELECT s, d, x FROM a WHERE (s >= 'bar' AND s <= 'foo') OR (s >= 'foobar')
----
select
 ├── columns: s:3(string!null) d:4(decimal!null) x:1(int!null)
 ├── stats: [rows=1500, distinct(3)=1, null(3)=0]
 ├── key: (1)
 ├── fd: (1)-->(3,4), (3,4)-->(1)
 ├── scan a@secondary
 │    ├── columns: x:1(int!null) s:3(string!null) d:4(decimal!null)
 │    ├── constraint: /-3/4
 │    │    ├── [ - /'foobar']
 │    │    └── [/'foo' - /'bar']
 │    ├── stats: [rows=1500, distinct(3)=1, null(3)=0]
 │    ├── key: (1)
 │    └── fd: (1)-->(3,4), (3,4)-->(1)
 └── filters
      └── (s <= 'foo') OR (s >= 'foobar') [type=bool, outer=(3), constraints=(/3: (/NULL - /'foo'] [/'foobar' - ]; tight)]

opt
SELECT * FROM a WHERE ((s >= 'bar' AND s <= 'foo') OR (s >= 'foobar')) AND d > 5.0
----
select
 ├── columns: x:1(int!null) y:2(int) s:3(string!null) d:4(decimal!null) b:5(bool)
 ├── stats: [rows=500, distinct(3)=1, null(3)=0, distinct(4)=100, null(4)=0]
 ├── key: (1)
 ├── fd: (1)-->(2-5), (3,4)-->(1,2,5)
 ├── scan a
 │    ├── columns: x:1(int!null) y:2(int) s:3(string) d:4(decimal!null) b:5(bool)
 │    ├── stats: [rows=3000, distinct(1)=2000, null(1)=0, distinct(3)=2, null(3)=0, distinct(4)=300, null(4)=0]
 │    ├── key: (1)
 │    └── fd: (1)-->(2-5), (3,4)~~>(1,2,5)
 └── filters
      ├── ((s >= 'bar') AND (s <= 'foo')) OR (s >= 'foobar') [type=bool, outer=(3), constraints=(/3: [/'bar' - /'foo'] [/'foobar' - ]; tight)]
      └── d > 5.0 [type=bool, outer=(4), constraints=(/4: (/5.0 - ]; tight)]

opt
SELECT * FROM a WHERE ((s >= 'bar' AND s <= 'foo') OR (s >= 'foobar')) AND d <= 5.0 AND s IS NOT NULL
----
index-join a
 ├── columns: x:1(int!null) y:2(int) s:3(string!null) d:4(decimal!null) b:5(bool)
 ├── stats: [rows=500, distinct(3)=1, null(3)=0, distinct(4)=100, null(4)=0]
 ├── key: (1)
 ├── fd: (1)-->(2-5), (3,4)-->(1,2,5)
 └── select
      ├── columns: x:1(int!null) s:3(string!null) d:4(decimal!null)
      ├── stats: [rows=166.666667, distinct(3)=1, null(3)=0, distinct(4)=33.3333333, null(4)=0]
      ├── key: (1)
      ├── fd: (1)-->(3,4), (3,4)-->(1)
      ├── scan a@secondary
      │    ├── columns: x:1(int!null) s:3(string!null) d:4(decimal!null)
      │    ├── constraint: /-3/4
      │    │    ├── [ - /'foobar'/5.0]
      │    │    └── [/'foo' - /'bar'/5.0]
      │    ├── stats: [rows=500, distinct(1)=478.548451, null(1)=0, distinct(3)=1, null(3)=0, distinct(4)=100, null(4)=0]
      │    ├── key: (1)
      │    └── fd: (1)-->(3,4), (3,4)-->(1)
      └── filters
           ├── (s <= 'foo') OR (s >= 'foobar') [type=bool, outer=(3), constraints=(/3: (/NULL - /'foo'] [/'foobar' - ]; tight)]
           └── d <= 5.0 [type=bool, outer=(4), constraints=(/4: (/NULL - /5.0]; tight)]

# 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:00:00.00000+00:00",
    "row_count": 2000,
    "distinct_count": 2000,
    "null_count": 300
  },
  {
    "columns": ["y"],
    "created_at": "2018-01-01 1:30:00.00000+00:00",
    "row_count": 2000,
    "distinct_count": 401,
    "null_count": 800
  },
  {
    "columns": ["y"],
    "created_at": "2018-01-01 2:00:00.00000+00:00",
    "row_count": 3000,
    "distinct_count": 501,
    "null_count": 1000
  },
  {
    "columns": ["s"],
    "created_at": "2018-01-01 2:00:00.00000+00:00",
    "row_count": 3000,
    "distinct_count": 3,
    "null_count": 1000
  },
  {
    "columns": ["b"],
    "created_at": "2018-01-01 2:00:00.00000+00:00",
    "row_count": 3000,
    "distinct_count": 3,
    "null_count": 1500
  }
]'
----

# Test calculation of multi-column stats.
opt colstat=2 colstat=3 colstat=5 colstat=(2,3,5) colstat=(2,3) colstat=(3,5)
SELECT y,s,b FROM a
----
scan a
 ├── columns: y:2(int) s:3(string) b:5(bool)
 └── stats: [rows=3000, distinct(2)=501, null(2)=1000, distinct(3)=3, null(3)=1000, distinct(5)=3, null(5)=1500, distinct(2,3)=1503, null(2,3)=1666.66667, distinct(3,5)=9, null(3,5)=2000, distinct(2,3,5)=3000, null(2,3,5)=2333.33333]

opt colstat=1 colstat=3 colstat=5 colstat=(1,3,5) colstat=(1,3) colstat=(3,5)
SELECT x,y,s FROM a
----
scan a
 ├── columns: x:1(int!null) y:2(int) s:3(string)
 ├── stats: [rows=3000, distinct(1)=2000, null(1)=0, distinct(3)=3, null(3)=1000, distinct(5)=3, null(5)=1500, distinct(1,3)=2001, null(1,3)=1000, distinct(3,5)=9, null(3,5)=2000, distinct(1,3,5)=1001, null(1,3,5)=2000]
 ├── key: (1)
 └── fd: (1)-->(2,3)

opt
SELECT y, s FROM a GROUP BY y, s
----
distinct-on
 ├── columns: y:2(int) s:3(string)
 ├── grouping columns: y:2(int) s:3(string)
 ├── stats: [rows=1503, distinct(2,3)=1503, null(2,3)=1503]
 ├── key: (2,3)
 └── scan a
      ├── columns: y:2(int) s:3(string)
      └── stats: [rows=3000, distinct(2,3)=1503, null(2,3)=1666.66667]

opt
SELECT s, d, x FROM a WHERE ((s <= 'aaa') OR (s >= 'bar' AND s <= 'foo')) AND s IS NOT NULL
----
select
 ├── columns: s:3(string!null) d:4(decimal!null) x:1(int!null)
 ├── stats: [rows=1000, distinct(3)=1, null(3)=0]
 ├── key: (1)
 ├── fd: (1)-->(3,4), (3,4)-->(1)
 ├── scan a@secondary
 │    ├── columns: x:1(int!null) s:3(string!null) d:4(decimal!null)
 │    ├── constraint: /-3/4
 │    │    ├── [/'foo' - /'bar']
 │    │    └── [/'aaa' - /NULL)
 │    ├── stats: [rows=1000, distinct(3)=1, null(3)=0]
 │    ├── key: (1)
 │    └── fd: (1)-->(3,4), (3,4)-->(1)
 └── filters
      └── (s <= 'aaa') OR (s >= 'bar') [type=bool, outer=(3), constraints=(/3: (/NULL - /'aaa'] [/'bar' - ]; tight)]

opt
SELECT s, d, x FROM a WHERE (s <= 'aaa') OR (s >= 'bar' AND s <= 'foo') OR s IS NULL
----
select
 ├── columns: s:3(string) d:4(decimal!null) x:1(int!null)
 ├── stats: [rows=1000, distinct(3)=1, null(3)=1000]
 ├── key: (1)
 ├── fd: (1)-->(3,4), (3,4)~~>(1)
 ├── scan a@secondary
 │    ├── columns: x:1(int!null) s:3(string) d:4(decimal!null)
 │    ├── constraint: /-3/4
 │    │    ├── [/'foo' - /'bar']
 │    │    └── [/'aaa' - /NULL]
 │    ├── stats: [rows=1000, distinct(3)=1, null(3)=1000]
 │    ├── key: (1)
 │    └── fd: (1)-->(3,4), (3,4)~~>(1)
 └── filters
      └── ((s <= 'aaa') OR ((s >= 'bar') AND (s <= 'foo'))) OR (s IS NULL) [type=bool, outer=(3), constraints=(/3: [/NULL - /'aaa'] [/'bar' - /'foo']; tight)]

opt
SELECT s, d, x FROM a WHERE s IS NOT NULL
----
scan a@secondary
 ├── columns: s:3(string!null) d:4(decimal!null) x:1(int!null)
 ├── constraint: /-3/4: [ - /NULL)
 ├── stats: [rows=2000, distinct(3)=3, null(3)=0]
 ├── key: (1)
 └── fd: (1)-->(3,4), (3,4)-->(1)

opt
SELECT s, d, x FROM a WHERE ((s >= 'bar' AND s <= 'foo') OR (s >= 'foobar')) AND s IS NOT NULL
----
select
 ├── columns: s:3(string!null) d:4(decimal!null) x:1(int!null)
 ├── stats: [rows=1000, distinct(3)=1, null(3)=0]
 ├── key: (1)
 ├── fd: (1)-->(3,4), (3,4)-->(1)
 ├── scan a@secondary
 │    ├── columns: x:1(int!null) s:3(string!null) d:4(decimal!null)
 │    ├── constraint: /-3/4
 │    │    ├── [ - /'foobar']
 │    │    └── [/'foo' - /'bar']
 │    ├── stats: [rows=1000, distinct(3)=1, null(3)=0]
 │    ├── key: (1)
 │    └── fd: (1)-->(3,4), (3,4)-->(1)
 └── filters
      └── (s <= 'foo') OR (s >= 'foobar') [type=bool, outer=(3), constraints=(/3: (/NULL - /'foo'] [/'foobar' - ]; tight)]

opt
SELECT * FROM a WHERE ((s >= 'bar' AND s <= 'foo') OR (s >= 'foobar')) AND d <= 5.0 AND s IS NOT NULL
----
index-join a
 ├── columns: x:1(int!null) y:2(int) s:3(string!null) d:4(decimal!null) b:5(bool)
 ├── stats: [rows=333.333333, distinct(3)=1, null(3)=0, distinct(4)=100, null(4)=0]
 ├── key: (1)
 ├── fd: (1)-->(2-5), (3,4)-->(1,2,5)
 └── select
      ├── columns: x:1(int!null) s:3(string!null) d:4(decimal!null)
      ├── stats: [rows=111.111111, distinct(3)=1, null(3)=0, distinct(4)=33.3333333, null(4)=0]
      ├── key: (1)
      ├── fd: (1)-->(3,4), (3,4)-->(1)
      ├── scan a@secondary
      │    ├── columns: x:1(int!null) s:3(string!null) d:4(decimal!null)
      │    ├── constraint: /-3/4
      │    │    ├── [ - /'foobar'/5.0]
      │    │    └── [/'foo' - /'bar'/5.0]
      │    ├── stats: [rows=333.333333, distinct(1)=323.895037, null(1)=0, distinct(3)=1, null(3)=0, distinct(4)=100, null(4)=0]
      │    ├── key: (1)
      │    └── fd: (1)-->(3,4), (3,4)-->(1)
      └── filters
           ├── (s <= 'foo') OR (s >= 'foobar') [type=bool, outer=(3), constraints=(/3: (/NULL - /'foo'] [/'foobar' - ]; tight)]
           └── d <= 5.0 [type=bool, outer=(4), constraints=(/4: (/NULL - /5.0]; tight)]

exec-ddl
CREATE TABLE abcde (
  a INT PRIMARY KEY,
  b INT,
  c STRING,
  d INT,
  e INT,
  INDEX bad(b, d),
  INDEX good(b, c, d)
)
----

# Regression test for #31929. Ensure that the good index is chosen.
opt
SELECT * FROM abcde WHERE b = 1 AND c LIKE '+1-1000%'
----
index-join abcde
 ├── columns: a:1(int!null) b:2(int!null) c:3(string!null) d:4(int) e:5(int)
 ├── stats: [rows=1.11111111, distinct(2)=1, null(2)=0, distinct(3)=1.11111111, null(3)=0]
 ├── key: (1)
 ├── fd: ()-->(2), (1)-->(3-5)
 └── scan abcde@good
      ├── columns: a:1(int!null) b:2(int!null) c:3(string!null) d:4(int)
      ├── constraint: /2/3/4/1: [/1/'+1-1000' - /1/'+1-1001')
      ├── stats: [rows=1.11111111, distinct(2)=1, null(2)=0, distinct(3)=1.11111111, null(3)=0]
      ├── key: (1)
      └── fd: ()-->(2), (1)-->(3,4)

exec-ddl
CREATE SEQUENCE seq
----

opt
SELECT * FROM seq
----
sequence-select seq
 ├── columns: last_value:1(int!null) log_cnt:2(int!null) is_called:3(bool!null)
 ├── cardinality: [1 - 1]
 ├── stats: [rows=1]
 ├── key: ()
 └── fd: ()-->(1-3)

exec-ddl
CREATE TABLE empty (x INT)
----

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

# We should always estimate at least 1 row even if the stats have 0 rows.
opt
SELECT * FROM empty
----
scan empty
 ├── columns: x:1(int)
 └── stats: [rows=1]

# Regression test: previously, overflow when computing estimated distinct count
# here resulted in a row count of zero being estimated.
opt
SELECT x FROM a WHERE x >= -9223372036854775808 AND x <= 0 ORDER BY x LIMIT 10
----
scan a
 ├── columns: x:1(int!null)
 ├── constraint: /1: [/-9223372036854775808 - /0]
 ├── limit: 10
 ├── stats: [rows=10]
 ├── key: (1)
 └── ordering: +1

# Regression test for #37953.
exec-ddl
CREATE TABLE t37953 (
    a UUID NOT NULL,
    b FLOAT8 NOT NULL,
    c TIME NOT NULL,
    d UUID NOT NULL,
    e VARCHAR,
    f "char" NULL,
    g INT4 NOT NULL,
    h VARCHAR NULL,
    i REGPROC NULL,
    j FLOAT8 NOT NULL
)
----

norm
WITH
  subq (col0, col1)
    AS (
      SELECT
        tab1.g AS col0,
        CASE
        WHEN ilike_escape(
          regexp_replace(
            tab0.h,
            tab1.e,
            tab0.f,
            tab0.e::STRING
          ),
          tab1.f,
          ''
        )
        THEN true
        ELSE false
        END
          AS col1
      FROM
        t37953 AS tab0, t37953 AS tab1
      WHERE
        tab0.j IN (tab1.j,)
    )
SELECT
  1
FROM
  subq
WHERE
  subq.col1;
----
project
 ├── columns: "?column?":26(int!null)
 ├── stats: [rows=0.950992903]
 ├── fd: ()-->(26)
 ├── select
 │    ├── columns: col1:25(bool!null)
 │    ├── stats: [rows=0.950992903, distinct(25)=0.950992903, null(25)=0]
 │    ├── fd: ()-->(25)
 │    ├── project
 │    │    ├── columns: col1:25(bool)
 │    │    ├── stats: [rows=333333.333, distinct(25)=333333.333, null(25)=16336.65]
 │    │    ├── inner-join (cross)
 │    │    │    ├── columns: tab0.e:5(varchar) tab0.f:6("char") tab0.h:8(varchar) tab0.j:10(float!null) tab1.e:16(varchar) tab1.f:17("char") tab1.j:21(float!null)
 │    │    │    ├── stats: [rows=333333.333, distinct(5,6,8,16,17)=333333.333, null(5,6,8,16,17)=16336.65]
 │    │    │    ├── scan tab0
 │    │    │    │    ├── columns: tab0.e:5(varchar) tab0.f:6("char") tab0.h:8(varchar) tab0.j:10(float!null)
 │    │    │    │    └── stats: [rows=1000, distinct(5,6,8)=1000, null(5,6,8)=29.701]
 │    │    │    ├── scan tab1
 │    │    │    │    ├── columns: tab1.e:16(varchar) tab1.f:17("char") tab1.j:21(float!null)
 │    │    │    │    └── stats: [rows=1000, distinct(16,17)=1000, null(16,17)=19.9]
 │    │    │    └── filters
 │    │    │         └── tab0.j IN (tab1.j,) [type=bool, outer=(10,21)]
 │    │    └── projections
 │    │         └── CASE WHEN ilike_escape(regexp_replace(tab0.h, tab1.e, tab0.f, tab0.e::STRING), tab1.f, '') THEN true ELSE false END [type=bool, outer=(5,6,8,16,17)]
 │    └── filters
 │         └── variable: col1 [type=bool, outer=(25), constraints=(/25: [/true - /true]; tight), fd=()-->(25)]
 └── projections
      └── const: 1 [type=int]

# ---------------------
# Tests with Histograms
# ---------------------

exec-ddl
CREATE TABLE hist (
  a INT,
  b DATE,
  c DECIMAL,
  d FLOAT,
  e TIMESTAMP,
  f TIMESTAMPTZ,
  g STRING,
  INDEX idx_a (a),
  INDEX idx_b (b),
  INDEX idx_c (c),
  INDEX idx_d (d),
  INDEX idx_e (e),
  INDEX idx_f (f),
  INDEX idx_g (g)
)
----

exec-ddl
ALTER TABLE hist INJECT STATISTICS '[
  {
    "columns": ["a"],
    "created_at": "2018-01-01 1:00:00.00000+00:00",
    "row_count": 1000,
    "distinct_count": 40,
    "histo_col_type": "int",
    "histo_buckets": [
      {"num_eq": 0, "num_range": 0, "distinct_range": 0, "upper_bound": "0"},
      {"num_eq": 10, "num_range": 90, "distinct_range": 9, "upper_bound": "10"},
      {"num_eq": 20, "num_range": 180, "distinct_range": 9, "upper_bound": "20"},
      {"num_eq": 30, "num_range": 270, "distinct_range": 9, "upper_bound": "30"},
      {"num_eq": 40, "num_range": 360, "distinct_range": 9, "upper_bound": "40"}
    ]
  },
  {
    "columns": ["b"],
    "created_at": "2018-01-01 1:00:00.00000+00:00",
    "row_count": 1000,
    "distinct_count": 120,
    "histo_col_type": "date",
    "histo_buckets": [
      {"num_eq": 0, "num_range": 0, "distinct_range": 0, "upper_bound": "2018-06-30"},
      {"num_eq": 10, "num_range": 90, "distinct_range": 29, "upper_bound": "2018-07-31"},
      {"num_eq": 20, "num_range": 180, "distinct_range": 29, "upper_bound": "2018-08-31"},
      {"num_eq": 30, "num_range": 270, "distinct_range": 29, "upper_bound": "2018-09-30"},
      {"num_eq": 40, "num_range": 360, "distinct_range": 29, "upper_bound": "2018-10-31"}
    ]
  },
  {
    "columns": ["c"],
    "created_at": "2018-01-01 1:00:00.00000+00:00",
    "row_count": 1000,
    "distinct_count": 45,
    "histo_col_type": "decimal",
    "histo_buckets": [
      {"num_eq": 0, "num_range": 0, "distinct_range": 0, "upper_bound": "0"},
      {"num_eq": 10, "num_range": 90, "distinct_range": 9, "upper_bound": "10"},
      {"num_eq": 20, "num_range": 180, "distinct_range": 10, "upper_bound": "20"},
      {"num_eq": 30, "num_range": 270, "distinct_range": 11, "upper_bound": "30"},
      {"num_eq": 40, "num_range": 360, "distinct_range": 11, "upper_bound": "40"}
    ]
  },
  {
    "columns": ["d"],
    "created_at": "2018-01-01 1:00:00.00000+00:00",
    "row_count": 1000,
    "distinct_count": 45,
    "histo_col_type": "float",
    "histo_buckets": [
      {"num_eq": 0, "num_range": 0, "distinct_range": 0, "upper_bound": "0"},
      {"num_eq": 10, "num_range": 90, "distinct_range": 9, "upper_bound": "10"},
      {"num_eq": 20, "num_range": 180, "distinct_range": 10, "upper_bound": "20"},
      {"num_eq": 30, "num_range": 270, "distinct_range": 11, "upper_bound": "30"},
      {"num_eq": 40, "num_range": 360, "distinct_range": 11, "upper_bound": "40"}
    ]
  },
  {
    "columns": ["e"],
    "created_at": "2018-01-01 1:00:00.00000+00:00",
    "row_count": 1000,
    "distinct_count": 200,
    "histo_col_type": "timestamp",
    "histo_buckets": [
      {"num_eq": 0, "num_range": 0, "distinct_range": 0, "upper_bound": "2018-06-30"},
      {"num_eq": 10, "num_range": 90, "distinct_range": 49, "upper_bound": "2018-07-31"},
      {"num_eq": 20, "num_range": 180, "distinct_range": 49, "upper_bound": "2018-08-31"},
      {"num_eq": 30, "num_range": 270, "distinct_range": 49, "upper_bound": "2018-09-30"},
      {"num_eq": 40, "num_range": 360, "distinct_range": 49, "upper_bound": "2018-10-31"}
    ]
  },
  {
    "columns": ["f"],
    "created_at": "2018-01-01 1:00:00.00000+00:00",
    "row_count": 1000,
    "distinct_count": 200,
    "histo_col_type": "timestamptz",
    "histo_buckets": [
      {"num_eq": 0, "num_range": 0, "distinct_range": 0, "upper_bound": "2018-06-30"},
      {"num_eq": 10, "num_range": 90, "distinct_range": 49, "upper_bound": "2018-07-31"},
      {"num_eq": 20, "num_range": 180, "distinct_range": 49, "upper_bound": "2018-08-31"},
      {"num_eq": 30, "num_range": 270, "distinct_range": 49, "upper_bound": "2018-09-30"},
      {"num_eq": 40, "num_range": 360, "distinct_range": 49, "upper_bound": "2018-10-31"}
    ]
  },
  {
    "columns": ["g"],
    "created_at": "2018-01-01 1:00:00.00000+00:00",
    "row_count": 1000,
    "distinct_count": 40,
    "histo_col_type": "string",
    "histo_buckets": [
      {"num_eq": 0, "num_range": 0, "distinct_range": 0, "upper_bound": "apple"},
      {"num_eq": 10, "num_range": 90, "distinct_range": 9, "upper_bound": "banana"},
      {"num_eq": 20, "num_range": 180, "distinct_range": 9, "upper_bound": "cherry"},
      {"num_eq": 30, "num_range": 270, "distinct_range": 9, "upper_bound": "mango"},
      {"num_eq": 40, "num_range": 360, "distinct_range": 9, "upper_bound": "pineapple"}
    ]
  }
]'
----

# An index join is worthwhile for a < 10.
opt
SELECT * FROM hist WHERE a < 10
----
index-join hist
 ├── columns: a:1(int!null) b:2(date) c:3(decimal) d:4(float) e:5(timestamp) f:6(timestamptz) g:7(string)
 ├── stats: [rows=90, distinct(1)=9, null(1)=0]
 │   histogram(1)=  0  0  80 10
 │                <--- 0 ---- 9
 └── scan hist@idx_a
      ├── columns: a:1(int!null) rowid:8(int!null)
      ├── constraint: /1/8: (/NULL - /9]
      ├── stats: [rows=90, distinct(1)=9, null(1)=0]
      │   histogram(1)=  0  0  80 10
      │                <--- 0 ---- 9
      ├── key: (8)
      └── fd: (8)-->(1)

# An index join is not worthwhile for a > 30.
opt
SELECT * FROM hist WHERE a > 30
----
select
 ├── columns: a:1(int!null) b:2(date) c:3(decimal) d:4(float) e:5(timestamp) f:6(timestamptz) g:7(string)
 ├── stats: [rows=400, distinct(1)=10, null(1)=0]
 │   histogram(1)=  0  0   360  40
 │                <--- 30 ----- 40
 ├── scan hist
 │    ├── columns: a:1(int) b:2(date) c:3(decimal) d:4(float) e:5(timestamp) f:6(timestamptz) g:7(string)
 │    └── stats: [rows=1000, distinct(1)=40, null(1)=0]
 │        histogram(1)=  0  0  90  10  180  20  270  30  360  40
 │                     <--- 0 ---- 10 ----- 20 ----- 30 ----- 40
 └── filters
      └── a > 30 [type=bool, outer=(1), constraints=(/1: [/31 - ]; tight)]

opt
SELECT * FROM hist WHERE b > '2018-07-31'::DATE AND b < '2018-08-05'::DATE
----
index-join hist
 ├── columns: a:1(int) b:2(date!null) c:3(decimal) d:4(float) e:5(timestamp) f:6(timestamptz) g:7(string)
 ├── stats: [rows=24, distinct(2)=3.9, null(2)=0]
 │   histogram(2)=  0       0        18       6
 │                <--- '2018-07-31' ---- '2018-08-04'
 └── scan hist@idx_b
      ├── columns: b:2(date!null) rowid:8(int!null)
      ├── constraint: /2/8: [/'2018-08-01' - /'2018-08-04']
      ├── stats: [rows=24, distinct(2)=3.9, null(2)=0]
      │   histogram(2)=  0       0        18       6
      │                <--- '2018-07-31' ---- '2018-08-04'
      ├── key: (8)
      └── fd: (8)-->(2)

opt
SELECT * FROM hist WHERE c = 20 OR (c < 10)
----
index-join hist
 ├── columns: a:1(int) b:2(date) c:3(decimal!null) d:4(float) e:5(timestamp) f:6(timestamptz) g:7(string)
 ├── stats: [rows=110, distinct(3)=10, null(3)=0]
 │   histogram(3)=  0  0  90  0   0  20
 │                <--- 0 ---- 10 --- 20
 └── scan hist@idx_c
      ├── columns: c:3(decimal!null) rowid:8(int!null)
      ├── constraint: /3/8
      │    ├── (/NULL - /10)
      │    └── [/20 - /20]
      ├── stats: [rows=110, distinct(3)=10, null(3)=0]
      │   histogram(3)=  0  0  90  0   0  20
      │                <--- 0 ---- 10 --- 20
      ├── key: (8)
      └── fd: (8)-->(3)

opt
SELECT * FROM hist WHERE c = 20 OR (c <= 10)
----
index-join hist
 ├── columns: a:1(int) b:2(date) c:3(decimal!null) d:4(float) e:5(timestamp) f:6(timestamptz) g:7(string)
 ├── stats: [rows=120, distinct(3)=11, null(3)=0]
 │   histogram(3)=  0  0  90  10  0  20
 │                <--- 0 ---- 10 --- 20
 └── scan hist@idx_c
      ├── columns: c:3(decimal!null) rowid:8(int!null)
      ├── constraint: /3/8
      │    ├── (/NULL - /10]
      │    └── [/20 - /20]
      ├── stats: [rows=120, distinct(3)=11, null(3)=0]
      │   histogram(3)=  0  0  90  10  0  20
      │                <--- 0 ---- 10 --- 20
      ├── key: (8)
      └── fd: (8)-->(3)

opt
SELECT * FROM hist WHERE (d >= 5 AND d < 15) OR d >= 40
----
index-join hist
 ├── columns: a:1(int) b:2(date) c:3(decimal) d:4(float!null) e:5(timestamp) f:6(timestamptz) g:7(string)
 ├── stats: [rows=185, distinct(4)=11.5, null(4)=0]
 │   histogram(4)=  0          0          45   10   90          0           0   40
 │                <--- 4.999999999999999 ---- 10.0 ---- 14.999999999999998 --- 40.0
 └── select
      ├── columns: d:4(float!null) rowid:8(int!null)
      ├── stats: [rows=185, distinct(4)=11.5, null(4)=0]
      │   histogram(4)=  0          0          45   10   90          0           0   40
      │                <--- 4.999999999999999 ---- 10.0 ---- 14.999999999999998 --- 40.0
      ├── key: (8)
      ├── fd: (8)-->(4)
      ├── scan hist@idx_d
      │    ├── columns: d:4(float!null) rowid:8(int!null)
      │    ├── constraint: /4/8
      │    │    ├── [/5.0 - /14.999999999999998]
      │    │    └── [/40.0 - ]
      │    ├── stats: [rows=185, distinct(4)=11.5, null(4)=0, distinct(8)=185, null(8)=0]
      │    │   histogram(4)=  0          0          45   10   90          0           0   40
      │    │                <--- 4.999999999999999 ---- 10.0 ---- 14.999999999999998 --- 40.0
      │    ├── key: (8)
      │    └── fd: (8)-->(4)
      └── filters
           └── (d < 15.0) OR (d >= 40.0) [type=bool, outer=(4), constraints=(/4: (/NULL - /14.999999999999998] [/40.0 - ]; tight)]

opt
SELECT * FROM hist WHERE e < '2018-07-31 23:00:00'::TIMESTAMP
----
index-join hist
 ├── columns: a:1(int) b:2(date) c:3(decimal) d:4(float) e:5(timestamp!null) f:6(timestamptz) g:7(string)
 ├── stats: [rows=105.564516, distinct(5)=51.5147849, null(5)=0]
 │   histogram(5)=  0               0               90              10               5.5645              6.7204e-14
 │                <--- '2018-06-30 00:00:00+00:00' ---- '2018-07-31 00:00:00+00:00' -------- '2018-07-31 22:59:59.999999+00:00'
 └── scan hist@idx_e
      ├── columns: e:5(timestamp!null) rowid:8(int!null)
      ├── constraint: /5/8: (/NULL - /'2018-07-31 22:59:59.999999+00:00']
      ├── stats: [rows=105.564516, distinct(5)=51.5147849, null(5)=0]
      │   histogram(5)=  0               0               90              10               5.5645              6.7204e-14
      │                <--- '2018-06-30 00:00:00+00:00' ---- '2018-07-31 00:00:00+00:00' -------- '2018-07-31 22:59:59.999999+00:00'
      ├── key: (8)
      └── fd: (8)-->(5)

opt
SELECT * FROM hist WHERE f = '2019-10-30 23:00:00'::TIMESTAMPTZ
----
index-join hist
 ├── columns: a:1(int) b:2(date) c:3(decimal) d:4(float) e:5(timestamp) f:6(timestamptz!null) g:7(string)
 ├── stats: [rows=1e-07, distinct(6)=1e-07, null(6)=0]
 │   histogram(6)=
 ├── fd: ()-->(6)
 └── scan hist@idx_f
      ├── columns: f:6(timestamptz!null) rowid:8(int!null)
      ├── constraint: /6/8: [/'2019-10-30 23:00:00+00:00' - /'2019-10-30 23:00:00+00:00']
      ├── stats: [rows=1e-07, distinct(6)=1e-07, null(6)=0]
      │   histogram(6)=
      ├── key: (8)
      └── fd: ()-->(6)

opt
SELECT * FROM hist WHERE g = 'mango' OR g = 'foo'
----
index-join hist
 ├── columns: a:1(int) b:2(date) c:3(decimal) d:4(float) e:5(timestamp) f:6(timestamptz) g:7(string!null)
 ├── stats: [rows=60, distinct(7)=2, null(7)=0]
 │   histogram(7)=  0   30    0    30
 │                <--- 'foo' --- 'mango'
 └── scan hist@idx_g
      ├── columns: g:7(string!null) rowid:8(int!null)
      ├── constraint: /7/8
      │    ├── [/'foo' - /'foo']
      │    └── [/'mango' - /'mango']
      ├── stats: [rows=60, distinct(7)=2, null(7)=0]
      │   histogram(7)=  0   30    0    30
      │                <--- 'foo' --- 'mango'
      ├── key: (8)
      └── fd: (8)-->(7)

# Select the correct index depending on which predicate is more selective.
opt
SELECT * FROM hist WHERE (a = 10 OR a = 20) AND (b = '2018-08-31'::DATE OR b = '2018-09-30'::DATE)
----
select
 ├── columns: a:1(int!null) b:2(date!null) c:3(decimal) d:4(float) e:5(timestamp) f:6(timestamptz) g:7(string)
 ├── stats: [rows=1.5, distinct(1)=1.5, null(1)=0, distinct(2)=1.5, null(2)=0]
 │   histogram(1)=  0 0.5  0  1
 │                <--- 10 --- 20
 │   histogram(2)=  0      0.6       0      0.9
 │                <--- '2018-08-31' --- '2018-09-30'
 ├── index-join hist
 │    ├── columns: a:1(int) b:2(date) c:3(decimal) d:4(float) e:5(timestamp) f:6(timestamptz) g:7(string)
 │    ├── stats: [rows=30]
 │    └── select
 │         ├── columns: a:1(int!null) rowid:8(int!null)
 │         ├── stats: [rows=30, distinct(1)=2, null(1)=0]
 │         │   histogram(1)=  0  10  0  20
 │         │                <--- 10 --- 20
 │         ├── key: (8)
 │         ├── fd: (8)-->(1)
 │         ├── scan hist@idx_a
 │         │    ├── columns: a:1(int!null) rowid:8(int!null)
 │         │    ├── constraint: /1/8
 │         │    │    ├── [/10 - /10]
 │         │    │    └── [/20 - /20]
 │         │    ├── stats: [rows=30, distinct(1)=2, null(1)=0, distinct(8)=30, null(8)=0]
 │         │    │   histogram(1)=  0  10  0  20
 │         │    │                <--- 10 --- 20
 │         │    ├── key: (8)
 │         │    └── fd: (8)-->(1)
 │         └── filters
 │              └── (a = 10) OR (a = 20) [type=bool, outer=(1), constraints=(/1: [/10 - /10] [/20 - /20]; tight)]
 └── filters
      └── (b = '2018-08-31') OR (b = '2018-09-30') [type=bool, outer=(2), constraints=(/2: [/'2018-08-31' - /'2018-08-31'] [/'2018-09-30' - /'2018-09-30']; tight)]

opt
SELECT * FROM hist WHERE (a = 30 OR a = 40) AND (b = '2018-06-30'::DATE OR b = '2018-07-31'::DATE)
----
select
 ├── columns: a:1(int!null) b:2(date!null) c:3(decimal) d:4(float) e:5(timestamp) f:6(timestamptz) g:7(string)
 ├── stats: [rows=0.7, distinct(1)=0.7, null(1)=0, distinct(2)=0.7, null(2)=0]
 │   histogram(1)=  0 0.3  0 0.4
 │                <--- 30 --- 40
 │   histogram(2)=  0      0.7
 │                <--- '2018-07-31'
 ├── index-join hist
 │    ├── columns: a:1(int) b:2(date) c:3(decimal) d:4(float) e:5(timestamp) f:6(timestamptz) g:7(string)
 │    ├── stats: [rows=10]
 │    └── select
 │         ├── columns: b:2(date!null) rowid:8(int!null)
 │         ├── stats: [rows=10, distinct(2)=1, null(2)=0]
 │         │   histogram(2)=  0       10
 │         │                <--- '2018-07-31'
 │         ├── key: (8)
 │         ├── fd: (8)-->(2)
 │         ├── scan hist@idx_b
 │         │    ├── columns: b:2(date!null) rowid:8(int!null)
 │         │    ├── constraint: /2/8
 │         │    │    ├── [/'2018-06-30' - /'2018-06-30']
 │         │    │    └── [/'2018-07-31' - /'2018-07-31']
 │         │    ├── stats: [rows=10, distinct(2)=1, null(2)=0, distinct(8)=10, null(8)=0]
 │         │    │   histogram(2)=  0       10
 │         │    │                <--- '2018-07-31'
 │         │    ├── key: (8)
 │         │    └── fd: (8)-->(2)
 │         └── filters
 │              └── (b = '2018-06-30') OR (b = '2018-07-31') [type=bool, outer=(2), constraints=(/2: [/'2018-06-30' - /'2018-06-30'] [/'2018-07-31' - /'2018-07-31']; tight)]
 └── filters
      └── (a = 30) OR (a = 40) [type=bool, outer=(1), constraints=(/1: [/30 - /30] [/40 - /40]; tight)]
