# LogicTest: fakedist-opt

statement ok
CREATE TABLE ab (a INT PRIMARY KEY, b INT); INSERT INTO ab (a, b) VALUES (1, 10)

## Table index change: Add/remove index that query depends on, and ensure that
## the plan is recomputed each time.
statement ok
PREPARE change_index AS SELECT * FROM [EXPLAIN SELECT * FROM ab WHERE b=10]

query TTT
EXECUTE change_index
----
scan  ·       ·
·     table   ab@primary
·     spans   ALL
·     filter  b = 10

statement ok
CREATE INDEX bindex ON ab (b)

query TTT
EXECUTE change_index
----
scan  ·      ·
·     table  ab@bindex
·     spans  /10-/11

statement ok
DROP INDEX bindex

query TTT
EXECUTE change_index
----
scan  ·       ·
·     table   ab@primary
·     spans   ALL
·     filter  b = 10

## Statistics change: Create statistics and ensure that the plan is recalculated.
statement ok
CREATE TABLE cd (c INT PRIMARY KEY, d INT)

statement ok
PREPARE change_stats AS SELECT * FROM [EXPLAIN SELECT * FROM ab JOIN cd ON a=c]

query TTT
EXECUTE change_stats
----
merge-join  ·               ·
 │          type            inner
 │          equality        (a) = (c)
 │          mergeJoinOrder  +"(a=c)"
 ├── scan   ·               ·
 │          table           ab@primary
 │          spans           ALL
 └── scan   ·               ·
·           table           cd@primary
·           spans           ALL

statement ok
CREATE STATISTICS s FROM ab

# Now that the optimizer knows table ab has one row (and it assumes a much
# higher number of rows for cd), it should choose lookup join.
# We allow retry because stat cache invalidation happens asynchronously.
query TTT retry
EXECUTE change_stats
----
lookup-join  ·      ·
 │           table  cd@primary
 │           type   inner
 └── scan    ·      ·
·            table  ab@primary
·            spans  ALL
