# LogicTest: local local-opt local-parallel-stmts fakedist fakedist-opt fakedist-metadata

# This test verifies that we correctly perform an index join when the KV
# batches span ranges. This is testing that SQL disables batch limits for index
# join; otherwise it can get out of order results from KV that it can't handle.

kv-batch-size 10

statement ok
CREATE TABLE t (
  a INT PRIMARY KEY,
  b INT,
  c INT,
  d INT,
  FAMILY (a),
  FAMILY (b),
  FAMILY (c),
  FAMILY (d),
  INDEX c (c)
)

statement ok
INSERT INTO t VALUES
(1, 0, 99, 0),
(2, 0, 80, 0),
(3, 0, 90, 0),
(4, 0, 10, 0),
(5, 0, 20, 0),
(6, 0, 85, 0),
(7, 0, 91, 0),
(8, 0, 12, 0),
(9, 0, 91, 0),
(10, 0, 11, 0),
(11, 0, 12, 0),
(12, 0, 88, 0),
(13, 0, 13, 0)

# Prevent the merge queue from immediately discarding our splits.
statement ok
SET CLUSTER SETTING kv.range_merge.queue_enabled = false;

# Split the table across multiple ranges.
statement ok
ALTER TABLE t SPLIT AT VALUES (2)

statement ok
ALTER TABLE t SPLIT AT VALUES (3)

statement ok
ALTER TABLE t SPLIT AT VALUES (5)

statement ok
ALTER TABLE t SPLIT AT VALUES (8)

statement ok
ALTER INDEX t@c SPLIT AT VALUES (90)

statement ok
ALTER INDEX c SPLIT AT VALUES (10)

query IIII partialsort(3)
SELECT * FROM t@c WHERE (c >= 80) ORDER BY c
----
2   0  80  0
6   0  85  0
12  0  88  0
3   0  90  0
7   0  91  0
9   0  91  0
1   0  99  0
