# LogicTest: local

statement ok
CREATE TABLE abcd (
  a INT PRIMARY KEY,
  b INT,
  c INT,
  d INT,
  INDEX b (b),
  INDEX cd (c,d),
  UNIQUE INDEX bcd (b,c,d)
)

# No hint
query TTT
EXPLAIN SELECT * FROM abcd WHERE a >= 20 AND a <= 30
----
·     distributed  false
·     vectorized   true
scan  ·            ·
·     table        abcd@primary
·     spans        /20-/30/#
·     parallel     ·

# No hint, reverse scan.
query TTT
EXPLAIN SELECT * FROM abcd WHERE a >= 20 AND a <= 30 ORDER BY a DESC
----
·        distributed  false
·        vectorized   true
revscan  ·            ·
·        table        abcd@primary
·        spans        /20-/30/#
·        parallel     ·

# Force primary
query TTT
EXPLAIN SELECT * FROM abcd@primary WHERE a >= 20 AND a <= 30
----
·     distributed  false
·     vectorized   true
scan  ·            ·
·     table        abcd@primary
·     spans        /20-/30/#
·     parallel     ·

# Force primary, reverse scan.
query TTT
EXPLAIN SELECT * FROM abcd@{FORCE_INDEX=primary,DESC} WHERE a >= 20 AND a <= 30
----
·        distributed  false
·        vectorized   true
revscan  ·            ·
·        table        abcd@primary
·        spans        /20-/30/#
·        parallel     ·

# Force primary, allow reverse scan.
query TTT
EXPLAIN SELECT * FROM abcd@primary WHERE a >= 20 AND a <= 30 ORDER BY a DESC
----
·        distributed  false
·        vectorized   true
revscan  ·            ·
·        table        abcd@primary
·        spans        /20-/30/#
·        parallel     ·

# Force primary, forward scan.
query TTT
EXPLAIN SELECT * FROM abcd@{FORCE_INDEX=primary,ASC} WHERE a >= 20 AND a <= 30 ORDER BY a DESC
----
·          distributed  false
·          vectorized   false
sort       ·            ·
 │         order        -a
 └── scan  ·            ·
·          table        abcd@primary
·          spans        /20-/30/#
·          parallel     ·

# Force index b
query TTT
EXPLAIN SELECT * FROM abcd@b WHERE a >= 20 AND a <= 30
----
·                distributed  false
·                vectorized   true
filter           ·            ·
 │               filter       (a >= 20) AND (a <= 30)
 └── index-join  ·            ·
      │          table        abcd@primary
      └── scan   ·            ·
·                table        abcd@b
·                spans        ALL

# Force index b, reverse scan.
query TTT
EXPLAIN SELECT * FROM abcd@{FORCE_INDEX=b,DESC} WHERE a >= 20 AND a <= 30
----
·                  distributed  false
·                  vectorized   true
filter             ·            ·
 │                 filter       (a >= 20) AND (a <= 30)
 └── index-join    ·            ·
      │            table        abcd@primary
      └── revscan  ·            ·
·                  table        abcd@b
·                  spans        ALL

# Force index b, allowing reverse scan.
query TTT
EXPLAIN SELECT * FROM abcd@b ORDER BY b DESC LIMIT 5
----
·             distributed  false
·             vectorized   true
index-join    ·            ·
 │            table        abcd@primary
 └── revscan  ·            ·
·             table        abcd@b
·             spans        ALL
·             limit        5

# Force index b, reverse scan.
query TTT
EXPLAIN SELECT * FROM abcd@{FORCE_INDEX=b,DESC} ORDER BY b DESC LIMIT 5
----
·             distributed  false
·             vectorized   true
index-join    ·            ·
 │            table        abcd@primary
 └── revscan  ·            ·
·             table        abcd@b
·             spans        ALL
·             limit        5


# Force index b, forward scan.
query TTT
EXPLAIN SELECT * FROM abcd@{FORCE_INDEX=b,ASC} ORDER BY b DESC LIMIT 5
----
·                     distributed  false
·                     vectorized   true
limit                 ·            ·
 │                    count        5
 └── sort             ·            ·
      │               order        -b
      └── index-join  ·            ·
           │          table        abcd@primary
           └── scan   ·            ·
·                     table        abcd@b
·                     spans        ALL

# Force index cd
query TTT
EXPLAIN SELECT * FROM abcd@cd WHERE a >= 20 AND a <= 30
----
·                distributed  false
·                vectorized   true
filter           ·            ·
 │               filter       (a >= 20) AND (a <= 30)
 └── index-join  ·            ·
      │          table        abcd@primary
      └── scan   ·            ·
·                table        abcd@cd
·                spans        ALL

# Force index bcd
query TTT
EXPLAIN SELECT * FROM abcd@bcd WHERE a >= 20 AND a <= 30
----
·     distributed  false
·     vectorized   true
scan  ·            ·
·     table        abcd@bcd
·     spans        ALL
·     filter       (a >= 20) AND (a <= 30)

# Force index b (covering)
query TTT
EXPLAIN SELECT b FROM abcd@b WHERE a >= 20 AND a <= 30
----
·          distributed  false
·          vectorized   true
render     ·            ·
 └── scan  ·            ·
·          table        abcd@b
·          spans        ALL
·          filter       (a >= 20) AND (a <= 30)

# Force index b (non-covering due to WHERE clause)
query TTT
EXPLAIN SELECT b FROM abcd@b WHERE c >= 20 AND c <= 30
----
·                     distributed  false
·                     vectorized   true
render                ·            ·
 └── filter           ·            ·
      │               filter       (c >= 20) AND (c <= 30)
      └── index-join  ·            ·
           │          table        abcd@primary
           └── scan   ·            ·
·                     table        abcd@b
·                     spans        ALL

# No hint, should be using index cd
query TTT
EXPLAIN SELECT c, d FROM abcd WHERE c >= 20 AND c < 40
----
·     distributed  false
·     vectorized   true
scan  ·            ·
·     table        abcd@cd
·     spans        /20-/40

# Force primary index
query TTT
EXPLAIN SELECT c, d FROM abcd@primary WHERE c >= 20 AND c < 40
----
·     distributed  false
·     vectorized   true
scan  ·            ·
·     table        abcd@primary
·     spans        ALL
·     filter       (c >= 20) AND (c < 40)

# Force index b
query TTT
EXPLAIN SELECT c, d FROM abcd@b WHERE c >= 20 AND c < 40
----
·                distributed  false
·                vectorized   true
filter           ·            ·
 │               filter       (c >= 20) AND (c < 40)
 └── index-join  ·            ·
      │          table        abcd@primary
      └── scan   ·            ·
·                table        abcd@b
·                spans        ALL

query TTT
EXPLAIN SELECT * FROM abcd@{FORCE_INDEX=b} WHERE a >= 20 AND a <= 30
----
·                distributed  false
·                vectorized   true
filter           ·            ·
 │               filter       (a >= 20) AND (a <= 30)
 └── index-join  ·            ·
      │          table        abcd@primary
      └── scan   ·            ·
·                table        abcd@b
·                spans        ALL

query TTT
EXPLAIN SELECT b, c, d FROM abcd WHERE c = 10
----
·           distributed  false
·           vectorized   true
index-join  ·            ·
 │          table        abcd@primary
 └── scan   ·            ·
·           table        abcd@cd
·           spans        /10-/11

query TTT
EXPLAIN SELECT b, c, d FROM abcd@{NO_INDEX_JOIN} WHERE c = 10
----
·     distributed  false
·     vectorized   true
scan  ·            ·
·     table        abcd@primary
·     spans        ALL
·     filter       c = 10

query TTT
EXPLAIN SELECT b, c, d FROM abcd@{FORCE_INDEX=bcd} WHERE c = 10
----
·     distributed  false
·     vectorized   true
scan  ·            ·
·     table        abcd@bcd
·     spans        ALL
·     filter       c = 10

query TTT
EXPLAIN SELECT b, c, d FROM abcd@{FORCE_INDEX=primary} WHERE c = 10
----
·     distributed  false
·     vectorized   true
scan  ·            ·
·     table        abcd@primary
·     spans        ALL
·     filter       c = 10
