# LogicTest: default

# Prepare a trace to be inspected below.

statement ok
SET tracing = on; BEGIN; SELECT 1; COMMIT; SELECT 2; SET tracing = off;

# Inspect the trace: we exclude messages containing newlines as these
# may contain non-deterministic txn object descriptions.
# This also checks that the span column properly reports separate
# SQL transactions.
query TTT
SELECT span, message, operation FROM [SHOW TRACE FOR SESSION] WHERE message NOT LIKE e'%\n%'
----
(0,0)  === SPAN START: sql txn implicit ===  sql txn implicit
(1,0)  === SPAN START: sql txn ===           sql txn
(1,0)  executing 1/5: BEGIN TRANSACTION      sql txn
(1,0)  executing 2/5: SELECT 1               sql txn
(1,0)  executing 3/5: COMMIT TRANSACTION     sql txn
(2,0)  === SPAN START: sql txn implicit ===  sql txn implicit
(2,0)  executing 1/1: SELECT 2               sql txn implicit
(3,0)  === SPAN START: sql txn implicit ===  sql txn implicit
(3,0)  executing 1/1: SET tracing = off      sql txn implicit

# Same, with SHOW TRACE FOR.
# This also tests that sub-spans are reported properly.

query TTT
SELECT span, message, operation FROM [SHOW TRACE FOR SELECT 1]
----
(0,0)  === SPAN START: sql txn implicit ===  sql txn implicit
(0,1)  === SPAN START: starting plan ===     starting plan
(0,2)  === SPAN START: consuming rows ===    consuming rows
(0,2)  plan completed execution              consuming rows
(0,2)  resources released, stopping trace    consuming rows

statement ok
PREPARE x AS SELECT 1

query TTT
SELECT span, message, operation FROM [SHOW TRACE FOR EXECUTE x]
----
(0,0)  === SPAN START: sql txn implicit ===  sql txn implicit
(0,1)  === SPAN START: starting plan ===     starting plan
(0,2)  === SPAN START: consuming rows ===    consuming rows
(0,2)  plan completed execution              consuming rows
(0,2)  resources released, stopping trace    consuming rows

# Check SHOW KV TRACE FOR SESSION.

statement ok
SET tracing = on,kv; CREATE DATABASE t; SET tracing = off

# Check the KV trace; we need to remove the eventlog entry since the
# timestamp is non-deterministic.
query TTT
SELECT span, operation, message FROM [SHOW KV TRACE FOR SESSION] WHERE message NOT LIKE '%Z/%'
----
(1,0)  sql txn implicit  querying next range at /Table/2/1/0/"t"/3/1
(1,0)  sql txn implicit  r1: sending batch 1 Get to (n1,s1):1
(1,0)  sql txn implicit  querying next range at /System/"desc-idgen"
(1,0)  sql txn implicit  r1: sending batch 1 Inc to (n1,s1):1
(1,0)  sql txn implicit  CPut /Table/2/1/0/"t"/3/1 -> 51
(1,0)  sql txn implicit  CPut /Table/3/1/51/2/1 -> database:<name:"t" id:51 privileges:<users:<user:"root" privileges:2 > > >
(1,0)  sql txn implicit  querying next range at /Table/SystemConfigSpan/Start
(1,0)  sql txn implicit  r1: sending batch 2 CPut, 1 BeginTxn to (n1,s1):1
(1,0)  sql txn implicit  querying next range at /Table/2/1/0/"system"/3/1
(1,0)  sql txn implicit  r1: sending batch 1 Get to (n1,s1):1
(1,0)  sql txn implicit  querying next range at /Table/3/1/1/2/1
(1,0)  sql txn implicit  r1: sending batch 1 Get to (n1,s1):1
(1,0)  sql txn implicit  querying next range at /Table/2/1/1/"eventlog"/3/1
(1,0)  sql txn implicit  r1: sending batch 1 Get to (n1,s1):1
(1,0)  sql txn implicit  querying next range at /Table/3/1/12/2/1
(1,0)  sql txn implicit  r1: sending batch 1 Get to (n1,s1):1
(1,0)  sql txn implicit  querying next range at /Table/3/1/1/2/1
(1,0)  sql txn implicit  r1: sending batch 1 Get to (n1,s1):1
(1,0)  sql txn implicit  r1: sending batch 5 CPut to (n1,s1):1
(1,0)  sql txn implicit  querying next range at /Table/SystemConfigSpan/Start
(1,0)  sql txn implicit  r1: sending batch 1 EndTxn to (n1,s1):1


# More KV operations.

query TTT
SELECT span, operation, regexp_replace(message, 'wall_time:\d+', 'wall_time:...') as message
  FROM [SHOW KV TRACE FOR CREATE TABLE t.kv(k INT PRIMARY KEY, v INT)] WHERE message NOT LIKE '%Z/%'
----
(0,1)  starting plan  querying next range at /Table/2/1/51/"kv"/3/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /System/"desc-idgen"
(0,1)  starting plan  r1: sending batch 1 Inc to (n1,s1):1
(0,1)  starting plan  CPut /Table/2/1/51/"kv"/3/1 -> 52
(0,1)  starting plan  CPut /Table/3/1/52/2/1 -> table:<name:"kv" id:52 parent_id:51 version:1 up_version:false modification_time:<wall_time:... logical:0 > columns:<name:"k" id:1 type:<semantic_type:INT width:0 precision:0 visible_type:NONE > nullable:false hidden:false > columns:<name:"v" id:2 type:<semantic_type:INT width:0 precision:0 visible_type:NONE > nullable:true hidden:false > next_column_id:3 families:<name:"primary" id:0 column_names:"k" column_names:"v" column_ids:1 column_ids:2 default_column_id:2 > next_family_id:1 primary_index:<name:"primary" id:1 unique:true column_names:"k" column_directions:ASC column_ids:1 foreign_key:<table:0 index:0 name:"" validity:Validated shared_prefix_len:0 > interleave:<> > next_index_id:2 privileges:<users:<user:"root" privileges:2 > > next_mutation_id:1 format_version:InterleavedFormatVersion state:PUBLIC view_query:"" >
(0,1)  starting plan  querying next range at /Table/SystemConfigSpan/Start
(0,1)  starting plan  r1: sending batch 2 CPut, 1 BeginTxn to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/3/1/51/2/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/2/1/0/"system"/3/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/3/1/1/2/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/2/1/1/"eventlog"/3/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/3/1/12/2/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/3/1/1/2/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  r1: sending batch 5 CPut to (n1,s1):1

query TTT
SELECT span, operation, regexp_replace(regexp_replace(message, 'mutationJobs:<[^>]*>', 'mutationJobs:<...>'), 'wall_time:\d+', 'wall_time:...') as message
  FROM [SHOW KV TRACE FOR CREATE UNIQUE INDEX woo ON t.kv(v)] WHERE message NOT LIKE '%Z/%'
----
(0,1)  starting plan  querying next range at /Table/2/1/0/"system"/3/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/3/1/1/2/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/2/1/1/"jobs"/3/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/3/1/15/2/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/3/1/1/2/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/SystemConfigSpan/Start
(0,1)  starting plan  r1: sending batch 2 CPut, 1 BeginTxn to (n1,s1):1
(0,1)  starting plan  Put /Table/3/1/52/2/1 -> table:<name:"kv" id:52 parent_id:51 version:1 up_version:true modification_time:<wall_time:... logical:0 > columns:<name:"k" id:1 type:<semantic_type:INT width:0 precision:0 visible_type:NONE > nullable:false hidden:false > columns:<name:"v" id:2 type:<semantic_type:INT width:0 precision:0 visible_type:NONE > nullable:true hidden:false > next_column_id:3 families:<name:"primary" id:0 column_names:"k" column_names:"v" column_ids:1 column_ids:2 default_column_id:2 > next_family_id:1 primary_index:<name:"primary" id:1 unique:true column_names:"k" column_directions:ASC column_ids:1 foreign_key:<table:0 index:0 name:"" validity:Validated shared_prefix_len:0 > interleave:<> > next_index_id:3 privileges:<users:<user:"root" privileges:2 > > mutations:<index:<name:"woo" id:2 unique:true column_names:"v" column_directions:ASC column_ids:2 extra_column_ids:1 foreign_key:<table:0 index:0 name:"" validity:Validated shared_prefix_len:0 > interleave:<> > state:DELETE_ONLY direction:ADD mutation_id:1 > next_mutation_id:2 format_version:InterleavedFormatVersion state:PUBLIC view_query:"" mutationJobs:<...> >
(0,1)  starting plan  querying next range at /Table/3/1/52/2/1
(0,1)  starting plan  r1: sending batch 1 Put to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/2/1/0/"system"/3/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/3/1/1/2/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/2/1/1/"eventlog"/3/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/3/1/12/2/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/3/1/1/2/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  r1: sending batch 5 CPut to (n1,s1):1

query TTT
SELECT span, operation, message FROM [SHOW KV TRACE FOR INSERT INTO t.kv(k, v) VALUES (1,2)]
----
(0,0)  sql txn implicit  CPut /Table/52/1/1/0 -> /TUPLE/2:2:Int/2
(0,0)  sql txn implicit  CPut /Table/52/2/2/0 -> /BYTES/
(0,2)  consuming rows    output row: []
(0,0)  sql txn implicit  querying next range at /Table/52/1/1/0
(0,0)  sql txn implicit  r1: sending batch 2 CPut, 1 BeginTxn, 1 EndTxn to (n1,s1):1

query TTT
SELECT span, operation, message FROM [SHOW KV TRACE FOR INSERT INTO t.kv(k, v) VALUES (1,2)]
----
(0,0)  sql txn implicit  CPut /Table/52/1/1/0 -> /TUPLE/2:2:Int/2
(0,0)  sql txn implicit  CPut /Table/52/2/2/0 -> /BYTES/
(0,2)  consuming rows    output row: []
(0,0)  sql txn implicit  querying next range at /Table/52/1/1/0
(0,0)  sql txn implicit  r1: sending batch 2 CPut, 1 BeginTxn, 1 EndTxn to (n1,s1):1
(0,2)  consuming rows    execution failed: duplicate key value (k)=(1) violates unique constraint "primary"

query TTT
SELECT span, operation, message FROM [SHOW KV TRACE FOR INSERT INTO t.kv(k, v) VALUES (2,2)]
----
(0,0)  sql txn implicit  CPut /Table/52/1/2/0 -> /TUPLE/2:2:Int/2
(0,0)  sql txn implicit  CPut /Table/52/2/2/0 -> /BYTES/
(0,2)  consuming rows    output row: []
(0,0)  sql txn implicit  querying next range at /Table/52/1/2/0
(0,0)  sql txn implicit  r1: sending batch 2 CPut, 1 BeginTxn, 1 EndTxn to (n1,s1):1
(0,2)  consuming rows    execution failed: duplicate key value (v)=(2) violates unique constraint "woo"

query TTT
SELECT span, operation, message FROM [SHOW KV TRACE FOR UPSERT INTO t.kv(k, v) VALUES (2,3)]
----
(0,2)  consuming rows    output row: []
(0,0)  sql txn implicit  querying next range at /Table/52/1/2
(0,0)  sql txn implicit  r1: sending batch 1 Scan to (n1,s1):1
(0,0)  sql txn implicit  CPut /Table/52/1/2/0 -> /TUPLE/2:2:Int/3
(0,0)  sql txn implicit  CPut /Table/52/2/3/0 -> /BYTES/
(0,0)  sql txn implicit  querying next range at /Table/52/1/2/0
(0,0)  sql txn implicit  r1: sending batch 2 CPut, 1 BeginTxn, 1 EndTxn to (n1,s1):1

query TTT
SELECT span, operation, message FROM [SHOW KV TRACE FOR UPSERT INTO t.kv(k, v) VALUES (1,2)]
----
(0,2)  consuming rows    output row: []
(0,0)  sql txn implicit  querying next range at /Table/52/1/1
(0,0)  sql txn implicit  r1: sending batch 1 Scan to (n1,s1):1
(0,0)  sql txn implicit  fetched: /kv/primary/1/v -> /2
(0,0)  sql txn implicit  Put /Table/52/1/1/0 -> /TUPLE/2:2:Int/2
(0,0)  sql txn implicit  querying next range at /Table/52/1/1/0
(0,0)  sql txn implicit  r1: sending batch 1 Put, 1 BeginTxn, 1 EndTxn to (n1,s1):1

query TTT
SELECT span, operation, message FROM [SHOW KV TRACE FOR UPSERT INTO t.kv(k, v) VALUES (2,2)]
----
(0,2)  consuming rows    output row: []
(0,0)  sql txn implicit  querying next range at /Table/52/1/2
(0,0)  sql txn implicit  r1: sending batch 1 Scan to (n1,s1):1
(0,0)  sql txn implicit  fetched: /kv/primary/2/v -> /3
(0,0)  sql txn implicit  Put /Table/52/1/2/0 -> /TUPLE/2:2:Int/2
(0,0)  sql txn implicit  Del /Table/52/2/3/0
(0,0)  sql txn implicit  CPut /Table/52/2/2/0 -> /BYTES/
(0,0)  sql txn implicit  querying next range at /Table/52/1/2/0
(0,0)  sql txn implicit  r1: sending batch 1 Put, 1 CPut, 1 Del, 1 BeginTxn, 1 EndTxn to (n1,s1):1
(0,2)  consuming rows    execution failed: duplicate key value (v)=(2) violates unique constraint "woo"

query TTT
SELECT span, operation, regexp_replace(regexp_replace(message, 'wall_time:\d+', 'wall_time:...'), '\d\d\d\d\d+', '...PK...') as message
  FROM [SHOW KV TRACE FOR CREATE TABLE t.kv2 AS TABLE t.kv] WHERE message NOT LIKE '%Z/%'
----
(0,1)  starting plan  querying next range at /Table/2/1/51/"kv2"/3/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /System/"desc-idgen"
(0,1)  starting plan  r1: sending batch 1 Inc to (n1,s1):1
(0,1)  starting plan  CPut /Table/2/1/51/"kv2"/3/1 -> 53
(0,1)  starting plan  CPut /Table/3/1/53/2/1 -> table:<name:"kv2" id:53 parent_id:51 version:1 up_version:false modification_time:<wall_time:... logical:0 > columns:<name:"k" id:1 type:<semantic_type:INT width:0 precision:0 visible_type:NONE > nullable:true hidden:false > columns:<name:"v" id:2 type:<semantic_type:INT width:0 precision:0 visible_type:NONE > nullable:true hidden:false > columns:<name:"rowid" id:3 type:<semantic_type:INT width:0 precision:0 visible_type:NONE > nullable:false default_expr:"unique_rowid()" hidden:true > next_column_id:4 families:<name:"primary" id:0 column_names:"k" column_names:"v" column_names:"rowid" column_ids:1 column_ids:2 column_ids:3 default_column_id:0 > next_family_id:1 primary_index:<name:"primary" id:1 unique:true column_names:"rowid" column_directions:ASC column_ids:3 foreign_key:<table:0 index:0 name:"" validity:Validated shared_prefix_len:0 > interleave:<> > next_index_id:2 privileges:<users:<user:"root" privileges:2 > > next_mutation_id:1 format_version:InterleavedFormatVersion state:PUBLIC view_query:"" >
(0,1)  starting plan  querying next range at /Table/SystemConfigSpan/Start
(0,1)  starting plan  r1: sending batch 2 CPut, 1 BeginTxn to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/3/1/51/2/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/2/1/0/"system"/3/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/3/1/1/2/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/2/1/1/"eventlog"/3/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/3/1/12/2/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/3/1/1/2/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  r1: sending batch 5 CPut to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/52/1
(0,1)  starting plan  r1: sending batch 1 Scan to (n1,s1):1
(0,1)  starting plan  fetched: /kv/primary/1/v -> /2
(0,1)  starting plan  CPut /Table/53/1/...PK.../0 -> /TUPLE/1:1:Int/1/1:2:Int/2
(0,1)  starting plan  fetched: /kv/primary/2/v -> /3
(0,1)  starting plan  CPut /Table/53/1/...PK.../0 -> /TUPLE/1:1:Int/2/1:2:Int/3
(0,1)  starting plan  querying next range at /Table/SystemConfigSpan/Start
(0,1)  starting plan  r1: sending batch 2 CPut, 1 EndTxn to (n1,s1):1

query TTT
SELECT span, operation, regexp_replace(message, '\d\d\d\d\d+', '...PK...') as message
  FROM [SHOW KV TRACE FOR UPDATE t.kv2 SET v = v + 2]
----
(0,0)  sql txn implicit  querying next range at /Table/53/1
(0,0)  sql txn implicit  r1: sending batch 1 Scan to (n1,s1):1
(0,0)  sql txn implicit  fetched: /kv2/primary/...PK.../k/v -> /1/2
(0,0)  sql txn implicit  Put /Table/53/1/...PK.../0 -> /TUPLE/1:1:Int/1/1:2:Int/4
(0,2)  consuming rows    output row: [...PK... 1 4]
(0,0)  sql txn implicit  fetched: /kv2/primary/...PK.../k/v -> /2/3
(0,0)  sql txn implicit  Put /Table/53/1/...PK.../0 -> /TUPLE/1:1:Int/2/1:2:Int/5
(0,2)  consuming rows    output row: [...PK... 2 5]
(0,0)  sql txn implicit  querying next range at /Table/53/1/...PK.../0
(0,0)  sql txn implicit  r1: sending batch 2 Put, 1 BeginTxn, 1 EndTxn to (n1,s1):1

query TTT
SELECT span, operation, message FROM [SHOW KV TRACE FOR DELETE FROM t.kv2]
----
(0,1)  starting plan   querying next range at /Table/53/1
(0,1)  starting plan   r1: sending batch 1 Scan to (n1,s1):1
(0,1)  starting plan   DelRange /Table/53/1 - /Table/53/2
(0,1)  starting plan   querying next range at /Table/53/1
(0,1)  starting plan   r1: sending batch 1 DelRng, 1 BeginTxn, 1 EndTxn to (n1,s1):1
(0,4)  consuming rows  fast path - rows affected: 2

query TTT
SELECT span, operation, regexp_replace(message, 'wall_time:\d+', 'wall_time:...') as message
  FROM [SHOW KV TRACE FOR DROP TABLE t.kv2] WHERE message NOT LIKE '%Z/%'
----
(0,1)  starting plan  Put /Table/3/1/53/2/1 -> table:<name:"kv2" id:53 parent_id:51 version:1 up_version:true modification_time:<wall_time:... logical:0 > columns:<name:"k" id:1 type:<semantic_type:INT width:0 precision:0 visible_type:NONE > nullable:true hidden:false > columns:<name:"v" id:2 type:<semantic_type:INT width:0 precision:0 visible_type:NONE > nullable:true hidden:false > columns:<name:"rowid" id:3 type:<semantic_type:INT width:0 precision:0 visible_type:NONE > nullable:false default_expr:"unique_rowid()" hidden:true > next_column_id:4 families:<name:"primary" id:0 column_names:"k" column_names:"v" column_names:"rowid" column_ids:1 column_ids:2 column_ids:3 default_column_id:0 > next_family_id:1 primary_index:<name:"primary" id:1 unique:true column_names:"rowid" column_directions:ASC column_ids:3 foreign_key:<table:0 index:0 name:"" validity:Validated shared_prefix_len:0 > interleave:<> > next_index_id:2 privileges:<users:<user:"root" privileges:2 > > next_mutation_id:1 format_version:InterleavedFormatVersion state:DROP view_query:"" >
(0,1)  starting plan  querying next range at /Table/SystemConfigSpan/Start
(0,1)  starting plan  r1: sending batch 1 Put, 1 BeginTxn to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/2/1/0/"system"/3/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/3/1/1/2/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/2/1/1/"eventlog"/3/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/3/1/12/2/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/3/1/1/2/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  r1: sending batch 5 CPut to (n1,s1):1

query TTT
SELECT span, operation, message FROM [SHOW KV TRACE FOR DELETE FROM t.kv]
----
(0,0)  sql txn implicit  querying next range at /Table/52/1
(0,0)  sql txn implicit  r1: sending batch 1 Scan to (n1,s1):1
(0,0)  sql txn implicit  fetched: /kv/primary/1/v -> /2
(0,0)  sql txn implicit  Del /Table/52/2/2/0
(0,0)  sql txn implicit  DelRange /Table/52/1/1 - /Table/52/1/1/#
(0,2)  consuming rows    output row: [1 2]
(0,0)  sql txn implicit  fetched: /kv/primary/2/v -> /3
(0,0)  sql txn implicit  Del /Table/52/2/3/0
(0,0)  sql txn implicit  DelRange /Table/52/1/2 - /Table/52/1/2/#
(0,2)  consuming rows    output row: [2 3]
(0,0)  sql txn implicit  querying next range at /Table/52/1/1
(0,0)  sql txn implicit  r1: sending batch 2 Del, 2 DelRng, 1 BeginTxn, 1 EndTxn to (n1,s1):1

query TTT
SELECT span, operation, regexp_replace(regexp_replace(message, 'mutationJobs:<[^>]*>', 'mutationJobs:<...>'), 'wall_time:\d+', 'wall_time:...') as message
  FROM [SHOW KV TRACE FOR DROP INDEX t.kv@woo] WHERE message NOT LIKE '%Z/%'
----
(0,1)  starting plan  querying next range at /Table/2/1/0/"t"/3/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/3/1/51/2/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/2/1/51/"kv"/3/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/3/1/52/2/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/3/1/51/2/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/3/1/51/2/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/2/1/0/"system"/3/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/3/1/1/2/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/2/1/1/"jobs"/3/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/3/1/15/2/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/3/1/1/2/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/SystemConfigSpan/Start
(0,1)  starting plan  r1: sending batch 2 CPut, 1 BeginTxn to (n1,s1):1
(0,1)  starting plan  Put /Table/3/1/52/2/1 -> table:<name:"kv" id:52 parent_id:51 version:4 up_version:true modification_time:<wall_time:... logical:0 > columns:<name:"k" id:1 type:<semantic_type:INT width:0 precision:0 visible_type:NONE > nullable:false hidden:false > columns:<name:"v" id:2 type:<semantic_type:INT width:0 precision:0 visible_type:NONE > nullable:true hidden:false > next_column_id:3 families:<name:"primary" id:0 column_names:"k" column_names:"v" column_ids:1 column_ids:2 default_column_id:2 > next_family_id:1 primary_index:<name:"primary" id:1 unique:true column_names:"k" column_directions:ASC column_ids:1 foreign_key:<table:0 index:0 name:"" validity:Validated shared_prefix_len:0 > interleave:<> > next_index_id:3 privileges:<users:<user:"root" privileges:2 > > mutations:<index:<name:"woo" id:2 unique:true column_names:"v" column_directions:ASC column_ids:2 extra_column_ids:1 foreign_key:<table:0 index:0 name:"" validity:Validated shared_prefix_len:0 > interleave:<> > state:DELETE_AND_WRITE_ONLY direction:DROP mutation_id:2 > next_mutation_id:3 format_version:InterleavedFormatVersion state:PUBLIC view_query:"" mutationJobs:<...> >
(0,1)  starting plan  querying next range at /Table/3/1/52/2/1
(0,1)  starting plan  r1: sending batch 1 Put to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/2/1/0/"system"/3/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/3/1/1/2/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/2/1/1/"eventlog"/3/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/3/1/12/2/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/3/1/1/2/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  r1: sending batch 5 CPut to (n1,s1):1

query TTT
SELECT span, operation, regexp_replace(regexp_replace(message, 'mutationJobs:<[^>]*>', 'mutationJobs:<...>'), 'wall_time:\d+', 'wall_time:...') as message
  FROM [SHOW KV TRACE FOR DROP TABLE t.kv] WHERE message NOT LIKE '%Z/%'
----
(0,1)  starting plan  Put /Table/3/1/52/2/1 -> table:<name:"kv" id:52 parent_id:51 version:7 up_version:true modification_time:<wall_time:... logical:0 > columns:<name:"k" id:1 type:<semantic_type:INT width:0 precision:0 visible_type:NONE > nullable:false hidden:false > columns:<name:"v" id:2 type:<semantic_type:INT width:0 precision:0 visible_type:NONE > nullable:true hidden:false > next_column_id:3 families:<name:"primary" id:0 column_names:"k" column_names:"v" column_ids:1 column_ids:2 default_column_id:2 > next_family_id:1 primary_index:<name:"primary" id:1 unique:true column_names:"k" column_directions:ASC column_ids:1 foreign_key:<table:0 index:0 name:"" validity:Validated shared_prefix_len:0 > interleave:<> > next_index_id:3 privileges:<users:<user:"root" privileges:2 > > next_mutation_id:3 format_version:InterleavedFormatVersion state:DROP view_query:"" >
(0,1)  starting plan  querying next range at /Table/SystemConfigSpan/Start
(0,1)  starting plan  r1: sending batch 1 Put, 1 BeginTxn to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/2/1/0/"system"/3/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/3/1/1/2/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/2/1/1/"eventlog"/3/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/3/1/12/2/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  querying next range at /Table/3/1/1/2/1
(0,1)  starting plan  r1: sending batch 1 Get to (n1,s1):1
(0,1)  starting plan  r1: sending batch 5 CPut to (n1,s1):1

# SELECT queries - used to exercise the KV tracing for other tests

statement ok
CREATE TABLE abc (
  a INT,
  b TEXT,
  c FLOAT,
  PRIMARY KEY (a, b),
  UNIQUE INDEX foo (b),
  INDEX bar (a),
  FAMILY (a, b),
  FAMILY (c)
)

query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM abc]
 WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----

statement ok
INSERT INTO abc VALUES (1, 'one', 1.1), (2, 'two', NULL), (3, 'three', NULL)

query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM abc]
 WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /abc/primary/1/'one' -> NULL
fetched: /abc/primary/1/'one'/c -> 1.1
output row: [1 'one' 1.1]
fetched: /abc/primary/2/'two' -> NULL
output row: [2 'two' NULL]
fetched: /abc/primary/3/'three' -> NULL
output row: [3 'three' NULL]

query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM abc LIMIT 2]
 WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /abc/primary/1/'one' -> NULL
fetched: /abc/primary/1/'one'/c -> 1.1
output row: [1 'one' 1.1]
fetched: /abc/primary/2/'two' -> NULL
output row: [2 'two' NULL]

query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM abc OFFSET 2]
 WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /abc/primary/1/'one' -> NULL
fetched: /abc/primary/1/'one'/c -> 1.1
fetched: /abc/primary/2/'two' -> NULL
fetched: /abc/primary/3/'three' -> NULL
output row: [3 'three' NULL]

query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM abc LIMIT 1 OFFSET 1]
 WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /abc/primary/1/'one' -> NULL
fetched: /abc/primary/1/'one'/c -> 1.1
fetched: /abc/primary/2/'two' -> NULL
output row: [2 'two' NULL]

query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM abc ORDER BY a]
 WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /abc/primary/1/'one' -> NULL
fetched: /abc/primary/1/'one'/c -> 1.1
output row: [1 'one' 1.1]
fetched: /abc/primary/2/'two' -> NULL
output row: [2 'two' NULL]
fetched: /abc/primary/3/'three' -> NULL
output row: [3 'three' NULL]

query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM abc ORDER BY b DESC]
 WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /abc/primary/1/'one' -> NULL
fetched: /abc/primary/1/'one'/c -> 1.1
fetched: /abc/primary/2/'two' -> NULL
fetched: /abc/primary/3/'three' -> NULL
output row: [2 'two' NULL]
output row: [3 'three' NULL]
output row: [1 'one' 1.1]

query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM abc ORDER BY b DESC LIMIT 1 OFFSET 1]
 WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /abc/foo/'two' -> /2
fetched: /abc/foo/'three' -> /3
fetched: /abc/primary/2/'two' -> NULL
fetched: /abc/primary/3/'three' -> NULL
output row: [3 'three' NULL]

query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM abc WHERE a = 2]
 WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /abc/primary/2/'two' -> NULL
output row: [2 'two' NULL]

query T
SELECT message FROM [SHOW KV TRACE FOR SELECT b FROM abc@foo]
 WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /abc/foo/'one' -> /1
output row: ['one']
fetched: /abc/foo/'three' -> /3
output row: ['three']
fetched: /abc/foo/'two' -> /2
output row: ['two']

query T
SELECT message FROM [SHOW KV TRACE FOR SELECT a FROM abc@bar]
 WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /abc/bar/1/'one' -> NULL
output row: [1]
fetched: /abc/bar/2/'two' -> NULL
output row: [2]
fetched: /abc/bar/3/'three' -> NULL
output row: [3]

statement ok
UPDATE abc SET c = NULL WHERE a = 1

query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM abc]
 WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /abc/primary/1/'one' -> NULL
output row: [1 'one' NULL]
fetched: /abc/primary/2/'two' -> NULL
output row: [2 'two' NULL]
fetched: /abc/primary/3/'three' -> NULL
output row: [3 'three' NULL]

query T
SELECT message FROM [SHOW KV TRACE FOR SELECT 3]
 WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
output row: [3]

query T
SELECT message FROM [SHOW KV TRACE FOR VALUES (1, 2, 3), (4, 5, 6)]
 WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
output row: [1 2 3]
output row: [4 5 6]

query T
SELECT message FROM [SHOW KV TRACE FOR VALUES (1, 2, 3), (4, 5, 6) ORDER BY 1 DESC]
 WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
output row: [4 5 6]
output row: [1 2 3]

query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM (VALUES (1, 2, 3), (4, 5, 6)) AS a]
 WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
output row: [1 2 3]
output row: [4 5 6]

query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM (SELECT * FROM abc) AS sub WHERE a % 2 = 0]
 WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /abc/primary/1/'one' -> NULL
fetched: /abc/primary/2/'two' -> NULL
output row: [2 'two' NULL]
fetched: /abc/primary/3/'three' -> NULL

query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM (SELECT a+a/a-1 FROM abc) AS sub(a) WHERE a > 1 OFFSET 1]
 WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /abc/primary/1/'one' -> NULL
fetched: /abc/primary/2/'two' -> NULL
fetched: /abc/primary/3/'three' -> NULL
output row: [3]

query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM (SELECT * FROM abc WHERE a = 2) AS sub]
 WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /abc/primary/2/'two' -> NULL
output row: [2 'two' NULL]

query T
SELECT message FROM [SHOW KV TRACE FOR SELECT DISTINCT * FROM (VALUES (1, 2, 3), (4, 5, 6), (1, 2, 3), (1, 2, 4)) AS a]
 WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
output row: [1 2 3]
output row: [4 5 6]
output row: [1 2 4]

statement ok
CREATE TABLE ab (a INT, b INT, PRIMARY KEY(a, b))

statement ok
INSERT INTO ab VALUES (1, 4), (1, 5), (2, 1), (2, 2), (2, 6), (3, 9)

# Note: the output from GROUP BY is in random order; the example is constructed so
# those rows are identical.
query T
SELECT message FROM [SHOW KV TRACE FOR SELECT SUM(b) FROM ab GROUP BY a]
 WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /ab/primary/1/4 -> NULL
fetched: /ab/primary/1/5 -> NULL
fetched: /ab/primary/2/1 -> NULL
fetched: /ab/primary/2/2 -> NULL
fetched: /ab/primary/2/6 -> NULL
fetched: /ab/primary/3/9 -> NULL
output row: [9]
output row: [9]
output row: [9]

query T
SELECT message FROM [SHOW KV TRACE FOR VALUES (1, 2), (1, 1), (1, 2), (2, 1), (2, 1) UNION VALUES (1, 3), (3, 4), (1, 1)]
 WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
output row: [1 3]
output row: [3 4]
output row: [1 1]
output row: [1 2]
output row: [2 1]

query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM abc EXCEPT SELECT * FROM abc WHERE b > 'p']
 WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /abc/foo/'three' -> /3
fetched: /abc/foo/'two' -> /2
fetched: /abc/primary/3/'three' -> NULL
fetched: /abc/primary/2/'two' -> NULL
fetched: /abc/primary/1/'one' -> NULL
output row: [1 'one' NULL]
fetched: /abc/primary/2/'two' -> NULL
fetched: /abc/primary/3/'three' -> NULL

query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM ab WHERE a > 1 INTERSECT SELECT * FROM ab WHERE b > 1]
 WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /ab/primary/1/4 -> NULL
fetched: /ab/primary/1/5 -> NULL
fetched: /ab/primary/2/1 -> NULL
fetched: /ab/primary/2/2 -> NULL
fetched: /ab/primary/2/6 -> NULL
fetched: /ab/primary/3/9 -> NULL
fetched: /ab/primary/2/1 -> NULL
fetched: /ab/primary/2/2 -> NULL
output row: [2 2]
fetched: /ab/primary/2/6 -> NULL
output row: [2 6]
fetched: /ab/primary/3/9 -> NULL
output row: [3 9]

query T
SELECT message FROM [SHOW KV TRACE FOR INSERT INTO ab(a, b) VALUES (42, 51)]
 WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
output row: []

query T
SELECT message FROM [SHOW KV TRACE FOR INSERT INTO ab(a, b) SELECT a+1, b+1 FROM ab WHERE b > 6]
 WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /ab/primary/1/4 -> NULL
fetched: /ab/primary/1/5 -> NULL
fetched: /ab/primary/2/1 -> NULL
fetched: /ab/primary/2/2 -> NULL
fetched: /ab/primary/2/6 -> NULL
fetched: /ab/primary/3/9 -> NULL
output row: []
fetched: /ab/primary/42/51 -> NULL
output row: []

query T
SELECT message FROM [SHOW KV TRACE FOR UPDATE ab SET a = a + 1 WHERE b > 6]
 WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /ab/primary/1/4 -> NULL
fetched: /ab/primary/1/5 -> NULL
fetched: /ab/primary/2/1 -> NULL
fetched: /ab/primary/2/2 -> NULL
fetched: /ab/primary/2/6 -> NULL
fetched: /ab/primary/3/9 -> NULL
output row: [4 9]
fetched: /ab/primary/4/10 -> NULL
output row: [5 10]
fetched: /ab/primary/42/51 -> NULL
output row: [43 51]
fetched: /ab/primary/43/52 -> NULL
output row: [44 52]

query T
SELECT message FROM [SHOW KV TRACE FOR DELETE FROM ab WHERE b > 6]
 WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /ab/primary/1/4 -> NULL
fetched: /ab/primary/1/5 -> NULL
fetched: /ab/primary/2/1 -> NULL
fetched: /ab/primary/2/2 -> NULL
fetched: /ab/primary/2/6 -> NULL
fetched: /ab/primary/4/9 -> NULL
output row: [4 9]
fetched: /ab/primary/5/10 -> NULL
output row: [5 10]
fetched: /ab/primary/43/51 -> NULL
output row: [43 51]
fetched: /ab/primary/44/52 -> NULL
output row: [44 52]

query T
SELECT message FROM [SHOW KV TRACE FOR DELETE FROM ab]
 WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----

statement ok
UPDATE abc SET c = 1.1 WHERE a = 1

query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM ab, abc WHERE abc.a+ab.b = abc.a*ab.a]
 WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /abc/primary/1/'one' -> NULL
fetched: /abc/primary/1/'one'/c -> 1.1
fetched: /abc/primary/2/'two' -> NULL
fetched: /abc/primary/3/'three' -> NULL
