# ---------------------------------------------------------
# Index With Delete Preserving Encoding
# ---------------------------------------------------------
statement
CREATE TABLE ti (
    a INT PRIMARY KEY,
    b INT,
    c INT,
    FAMILY (a, b, c),
    INDEX index_to_mutate (b, c)
);
----

mutate-index ti index_to_mutate WRITE_ONLY use_delete_preserving_encoding=true
----

statement
INSERT INTO ti VALUES (1, 2, 100)
----

kvtrace
UPDATE ti SET b = b + 1, c = c + 1
----
Scan /Table/106/{1-2} lock Exclusive (Block, Unreplicated)
Put /Table/106/1/1/0 -> /TUPLE/2:2:Int/3/1:3:Int/101
Put (delete) /Table/106/2/2/100/1/0
Put /Table/106/2/3/101/1/0 -> /BYTES/0x0a0103

# ---------------------------------------------------------
# Partial Index With Delete Preserving Encoding
# ---------------------------------------------------------
statement
CREATE TABLE tpi (
    a INT PRIMARY KEY,
    b INT,
    c STRING,
    FAMILY (a, b, c),
    INDEX partial (c) WHERE a > b AND c IN ('foo', 'foobar')
);
----

mutate-index tpi partial WRITE_ONLY use_delete_preserving_encoding=true
----

statement
INSERT INTO tpi VALUES (3, 4, 'bar')
----

# Update a row that doesn't match the partial index.
kvtrace
UPDATE tpi SET b = b + 1
----
Scan /Table/107/{1-2} lock Exclusive (Block, Unreplicated)
Put /Table/107/1/3/0 -> /TUPLE/2:2:Int/5/1:3:Bytes/bar

# Update a row that didn't match the partial index before but matches after.
kvtrace
UPDATE tpi SET b = b - 3, c = 'foo'
----
Scan /Table/107/{1-2} lock Exclusive (Block, Unreplicated)
Put /Table/107/1/3/0 -> /TUPLE/2:2:Int/2/1:3:Bytes/foo
Put /Table/107/2/"foo"/3/0 -> /BYTES/0x0a0103

# Update a row that matches the partial index before and after. While
# the index entry doesn't change, it is still written to the delete
# preserving index.
kvtrace
UPDATE tpi SET b = b - 1
----
Scan /Table/107/{1-2} lock Exclusive (Block, Unreplicated)
Put /Table/107/1/3/0 -> /TUPLE/2:2:Int/1/1:3:Bytes/foo
Put /Table/107/2/"foo"/3/0 -> /BYTES/0x0a0103

# Update a row that matches the partial index before and after, and the index
# entry changes.
kvtrace
UPDATE tpi SET b = b + 1, c = 'foobar'
----
Scan /Table/107/{1-2} lock Exclusive (Block, Unreplicated)
Put /Table/107/1/3/0 -> /TUPLE/2:2:Int/2/1:3:Bytes/foobar
Put (delete) /Table/107/2/"foo"/3/0
Put /Table/107/2/"foobar"/3/0 -> /BYTES/0x0a0103

# Update a row that matches the partial index before but not after.
kvtrace
UPDATE tpi SET c = 'baz'
----
Scan /Table/107/{1-2} lock Exclusive (Block, Unreplicated)
Put /Table/107/1/3/0 -> /TUPLE/2:2:Int/2/1:3:Bytes/baz
Put (delete) /Table/107/2/"foobar"/3/0

# ---------------------------------------------------------
# Expression Index With Delete Preserving Encoding
# ---------------------------------------------------------
statement
CREATE TABLE tei (
  k INT PRIMARY KEY,
  a INT,
  b INT,
  FAMILY (k, a, b),
  INDEX t_a_plus_b_idx ((a + b))
)
----

mutate-index tei t_a_plus_b_idx WRITE_ONLY use_delete_preserving_encoding=true
----

statement
INSERT INTO tei VALUES (1, 2, 100)
----

# Update a row which changes the index entry.
kvtrace
UPDATE tei SET a = a + 1, b = b + 100
----
Scan /Table/108/{1-2} lock Exclusive (Block, Unreplicated)
Put /Table/108/1/1/0 -> /TUPLE/2:2:Int/3/1:3:Int/200
Put (delete) /Table/108/2/102/1/0
Put /Table/108/2/203/1/0 -> /BYTES/0x0a0103

# Update a row with different values without changing the expected index entry.
# The index entry is still written for a delete preserving index
kvtrace
UPDATE tei SET a = a + 1, b = b - 1
----
Scan /Table/108/{1-2} lock Exclusive (Block, Unreplicated)
Put /Table/108/1/1/0 -> /TUPLE/2:2:Int/4/1:3:Int/199
Put /Table/108/2/203/1/0 -> /BYTES/0x0a0103

# ---------------------------------------------------------
# Inverted Index With Delete Preserving Encoding
# ---------------------------------------------------------

statement
CREATE TABLE tii (
  a INT PRIMARY KEY,
  b INT[],
  FAMILY (a,b),
  INVERTED INDEX inverted (b)
)
----

mutate-index tii inverted WRITE_ONLY use_delete_preserving_encoding=true
----

statement
INSERT INTO tii VALUES (1, ARRAY[1, 2, 3, 2, 2, NULL, 3])
----

# Update a row that has 1 new entry and 1 removed entry in the index.
kvtrace
UPDATE tii SET b = ARRAY[1, 2, 2, NULL, 4, 4]
----
Scan /Table/109/{1-2} lock Exclusive (Block, Unreplicated)
Put /Table/109/1/1/0 -> /TUPLE/
Put (delete) /Table/109/2/NULL/1/0
Put (delete) /Table/109/2/1/1/0
Put (delete) /Table/109/2/2/1/0
Put (delete) /Table/109/2/3/1/0
Put /Table/109/2/NULL/1/0 -> /BYTES/0x0a0103
Put /Table/109/2/1/1/0 -> /BYTES/0x0a0103
Put /Table/109/2/2/1/0 -> /BYTES/0x0a0103
Put /Table/109/2/4/1/0 -> /BYTES/0x0a0103

# ---------------------------------------------------------
# Multicolumn Inverted Index With Delete Preserving Encoding
# ---------------------------------------------------------

statement
CREATE TABLE tmi (
  a INT PRIMARY KEY,
  b INT,
  c JSON,
  FAMILY (a, b, c),
  INVERTED INDEX inverted (b, c)
)
----

mutate-index tmi inverted WRITE_ONLY use_delete_preserving_encoding=true
----

statement
INSERT INTO tmi VALUES (1, 2, '{"a": "foo", "b": "bar"}'::json)
----

kvtrace
UPDATE tmi SET b = 3, c = '{"a": "foobar", "c": "baz"}'::json
----
Scan /Table/110/{1-2} lock Exclusive (Block, Unreplicated)
Put /Table/110/1/1/0 -> /TUPLE/2:2:Int/3/
Put (delete) /Table/110/2/2/"a"/"foo"/1/0
Put (delete) /Table/110/2/2/"b"/"bar"/1/0
Put /Table/110/2/3/"a"/"foobar"/1/0 -> /BYTES/0x0a0103
Put /Table/110/2/3/"c"/"baz"/1/0 -> /BYTES/0x0a0103
