# LogicTest: local

statement ok
CREATE TABLE kv (
  k INT PRIMARY KEY,
  v INT,
  UNIQUE INDEX foo (v),
  INDEX bar (k, v)
)

statement ok
CREATE TABLE unindexed (
  k INT PRIMARY KEY,
  v INT
)

statement ok
CREATE TABLE indexed (id int primary key, value int, other int, index (value))

statement count 4
INSERT INTO kv VALUES (1, 2), (3, 4), (5, 6), (7, 8)

statement count 2
DELETE FROM kv WHERE k=3 OR v=6

query II
DELETE FROM kv RETURNING k, v
----
1 2
7 8

statement ok
SET tracing = on,kv,results; SELECT * FROM kv; SET tracing = off

query T
SELECT message FROM [SHOW KV TRACE FOR SESSION] WITH ORDINALITY
 WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
 ORDER BY message LIKE 'fetched:%' DESC, ordinality ASC
----

statement ok
SET tracing = on,kv,results; SELECT * FROM kv@foo; SET tracing = off

query T
SELECT message FROM [SHOW KV TRACE FOR SESSION] WITH ORDINALITY
 WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
 ORDER BY message LIKE 'fetched:%' DESC, ordinality ASC
----

statement ok
SET tracing = on,kv,results; SELECT * FROM kv@bar; SET tracing = off

query T
SELECT message FROM [SHOW KV TRACE FOR SESSION] WITH ORDINALITY
 WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
 ORDER BY message LIKE 'fetched:%' DESC, ordinality ASC
----

# Check that EXPLAIN does not destroy data (#6613)
query TTT colnames
EXPLAIN DELETE FROM unindexed
----
tree          field        description
·             distributed  false
·             vectorized   false
delete range  ·            ·
·             from         unindexed
·             spans        ALL

query TTT
EXPLAIN DELETE FROM unindexed WHERE v = 7 ORDER BY v LIMIT 10
----
·                         distributed  false
·                         vectorized   false
count                     ·            ·
 └── delete               ·            ·
      │                   from         unindexed
      │                   strategy     deleter
      └── render          ·            ·
           └── limit      ·            ·
                │         count        10
                └── scan  ·            ·
·                         table        unindexed@primary
·                         spans        ALL
·                         filter       v = 7

# Check DELETE with LIMIT clause (MySQL extension)
query TTT
EXPLAIN DELETE FROM unindexed WHERE v = 5 LIMIT 10
----
·                         distributed  false
·                         vectorized   false
count                     ·            ·
 └── delete               ·            ·
      │                   from         unindexed
      │                   strategy     deleter
      └── render          ·            ·
           └── limit      ·            ·
                │         count        10
                └── scan  ·            ·
·                         table        unindexed@primary
·                         spans        ALL
·                         filter       v = 5

# Check fast DELETE.
query TTT
EXPLAIN DELETE FROM unindexed WHERE k > 0
----
·             distributed  false
·             vectorized   false
delete range  ·            ·
·             from         unindexed
·             spans        /1-

# Check fast DELETE with reverse scans (not supported by optimizer).
query error DELETE statement requires LIMIT when ORDER BY is used
EXPLAIN DELETE FROM unindexed WHERE true ORDER BY k DESC

# Check that limits don't permit fast deletes.
query TTT
EXPLAIN DELETE FROM unindexed WHERE k > 0 LIMIT 1
----
·               distributed  false
·               vectorized   false
count           ·            ·
 └── delete     ·            ·
      │         from         unindexed
      │         strategy     deleter
      └── scan  ·            ·
·               table        unindexed@primary
·               spans        /1-
·               limit        1

query TTT
EXPLAIN DELETE FROM indexed WHERE value = 5 LIMIT 10
----
·               distributed  false
·               vectorized   false
count           ·            ·
 └── delete     ·            ·
      │         from         indexed
      │         strategy     deleter
      └── scan  ·            ·
·               table        indexed@indexed_value_idx
·               spans        /5-/6
·               limit        10

query TTT
EXPLAIN DELETE FROM indexed LIMIT 10
----
·               distributed  false
·               vectorized   false
count           ·            ·
 └── delete     ·            ·
      │         from         indexed
      │         strategy     deleter
      └── scan  ·            ·
·               table        indexed@indexed_value_idx
·               spans        ALL
·               limit        10

# TODO(andyk): Prune columns so that index-join is not necessary.
query TTT
EXPLAIN DELETE FROM indexed WHERE value = 5 LIMIT 10 RETURNING id
----
·               distributed  false
·               vectorized   false
run             ·            ·
 └── delete     ·            ·
      │         from         indexed
      │         strategy     deleter
      └── scan  ·            ·
·               table        indexed@indexed_value_idx
·               spans        /5-/6
·               limit        10

# Ensure that index hints in DELETE statements force the choice of a specific index
# as described in #38799.
statement ok
CREATE TABLE t38799 (a INT PRIMARY KEY, b INT, c INT, INDEX foo(b))

query TTTTT
EXPLAIN (VERBOSE) DELETE FROM t38799@foo
----
·               distributed  false       ·       ·
·               vectorized   false       ·       ·
count           ·            ·           ()      ·
 └── delete     ·            ·           ()      ·
      │         from         t38799      ·       ·
      │         strategy     deleter     ·       ·
      └── scan  ·            ·           (a, b)  ·
·               table        t38799@foo  ·       ·
·               spans        ALL         ·       ·
