exec-ddl
CREATE TABLE a
(
    x INT PRIMARY KEY,
    y INT,
    z DECIMAL,
    s STRING NOT NULL,
    INDEX (y, s)
)
----

# Ensure that the extra cost for unbounded cardinality operators causes the
# limit to be pushed below the index join, even though the limit + offset
# exceeds the estimated number of rows.
opt
SELECT * FROM a
WHERE y = 10 ORDER BY s, x DESC
LIMIT 20 OFFSET 1000
----
index-join a
 ├── columns: x:1!null y:2!null z:3 s:4!null
 ├── cardinality: [0 - 20]
 ├── stats: [rows=1]
 ├── cost: 35.4529295
 ├── key: (1)
 ├── fd: ()-->(2), (1)-->(3,4)
 ├── ordering: +4,-1 opt(2) [actual: +4,-1]
 └── offset
      ├── columns: x:1!null y:2!null s:4!null
      ├── internal-ordering: +4,-1 opt(2)
      ├── cardinality: [0 - 20]
      ├── stats: [rows=1]
      ├── cost: 29.3829295
      ├── key: (1)
      ├── fd: ()-->(2), (1)-->(4)
      ├── ordering: +4,-1 opt(2) [actual: +4,-1]
      ├── limit
      │    ├── columns: x:1!null y:2!null s:4!null
      │    ├── internal-ordering: +4,-1 opt(2)
      │    ├── cardinality: [0 - 1020]
      │    ├── stats: [rows=10, distinct(4)=9.56179, null(4)=0]
      │    ├── cost: 29.3629295
      │    ├── key: (1)
      │    ├── fd: ()-->(2), (1)-->(4)
      │    ├── ordering: +4,-1 opt(2) [actual: +4,-1]
      │    ├── sort (segmented)
      │    │    ├── columns: x:1!null y:2!null s:4!null
      │    │    ├── stats: [rows=10, distinct(2)=1, null(2)=0, distinct(4)=9.56179, null(4)=0]
      │    │    ├── cost: 29.2529295
      │    │    ├── key: (1)
      │    │    ├── fd: ()-->(2), (1)-->(4)
      │    │    ├── ordering: +4,-1 opt(2) [actual: +4,-1]
      │    │    ├── limit hint: 1020.00
      │    │    └── scan a@a_y_s_idx
      │    │         ├── columns: x:1!null y:2!null s:4!null
      │    │         ├── constraint: /2/4/1: [/10 - /10]
      │    │         ├── stats: [rows=10, distinct(2)=1, null(2)=0, distinct(4)=9.56179, null(4)=0]
      │    │         ├── cost: 28.6200001
      │    │         ├── key: (1)
      │    │         ├── fd: ()-->(2), (1)-->(4)
      │    │         └── ordering: +4 opt(2) [actual: +4]
      │    └── 1020
      └── 1000

exec-ddl
ALTER TABLE a INJECT STATISTICS '[
  {
    "columns": ["y"],
    "created_at": "2019-02-08 04:10:40.001179+00:00",
    "row_count": 10000,
    "distinct_count": 1000
  }
]'
----

# The limit hint from the TopK leads to a lower cost for the index join and
# index scan.
opt
SELECT * FROM a
ORDER BY y, z
LIMIT 10
----
top-k
 ├── columns: x:1!null y:2 z:3 s:4!null
 ├── internal-ordering: +2,+3
 ├── k: 10
 ├── cardinality: [0 - 10]
 ├── stats: [rows=10, distinct(2)=9.95512, null(2)=0]
 ├── cost: 7840.39841
 ├── key: (1)
 ├── fd: (1)-->(2-4)
 ├── ordering: +2,+3
 └── index-join a
      ├── columns: x:1!null y:2 z:3 s:4!null
      ├── stats: [rows=10000]
      ├── cost: 7748.81875
      ├── key: (1)
      ├── fd: (1)-->(2-4)
      ├── ordering: +2
      ├── limit hint: 1004.51
      └── scan a@a_y_s_idx
           ├── columns: x:1!null y:2 s:4!null
           ├── stats: [rows=10000]
           ├── cost: 1082.79875
           ├── key: (1)
           ├── fd: (1)-->(2,4)
           ├── ordering: +2
           └── limit hint: 1004.51
