# LogicTest: local

# 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)
)

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

query T
SELECT message FROM [SHOW KV TRACE FOR SESSION]
 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)

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

query T
SELECT message FROM [SHOW KV TRACE FOR SESSION]
 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]

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

query T
SELECT message FROM [SHOW KV TRACE FOR SESSION]
 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]

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

query T
SELECT message FROM [SHOW KV TRACE FOR SESSION]
 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]

statement ok
SET tracing = on,kv,results; SELECT * FROM abc LIMIT 1 OFFSET 1; SET tracing = off

query T
SELECT message FROM [SHOW KV TRACE FOR SESSION]
 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]

statement ok
SET tracing = on,kv,results; SELECT * FROM abc ORDER BY a; SET tracing = off

query T
SELECT message FROM [SHOW KV TRACE FOR SESSION]
 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]

statement ok
SET tracing = on,kv,results; SELECT * FROM abc ORDER BY b DESC; SET tracing = off

query T
SELECT message FROM [SHOW KV TRACE FOR SESSION]
 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]

statement ok
SET tracing = on,kv,results; SELECT * FROM abc ORDER BY b DESC LIMIT 1 OFFSET 1; SET tracing = off

query T
SELECT message FROM [SHOW KV TRACE FOR SESSION]
 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]

statement ok
SET tracing = on,kv,results; SELECT * FROM abc WHERE a = 2; SET tracing = off

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

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

query T
SELECT message FROM [SHOW KV TRACE FOR SESSION]
 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']

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

query T
SELECT message FROM [SHOW KV TRACE FOR SESSION]
 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

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

query T
SELECT message FROM [SHOW KV TRACE FOR SESSION]
 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]

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

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

statement ok
SET tracing = on,kv,results; VALUES (1, 2, 3), (4, 5, 6); SET tracing = off

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

statement ok
SET tracing = on,kv,results; VALUES (1, 2, 3), (4, 5, 6) ORDER BY 1 DESC; SET tracing = off

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

statement ok
SET tracing = on,kv,results; SELECT * FROM (VALUES (1, 2, 3), (4, 5, 6)) AS a; SET tracing = off

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

statement ok
SET tracing = on,kv,results; SELECT * FROM (SELECT * FROM abc) AS sub WHERE a % 2 = 0; SET tracing = off

query T
SELECT message FROM [SHOW KV TRACE FOR SESSION]
 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

statement ok
SET tracing = on,kv,results; SELECT * FROM (SELECT a+a/a-1 FROM abc) AS sub(a) WHERE a > 1 OFFSET 1; SET tracing = off

query T
SELECT message FROM [SHOW KV TRACE FOR SESSION]
 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]

statement ok
SET tracing = on,kv,results; SELECT * FROM (SELECT * FROM abc WHERE a = 2) AS sub; SET tracing = off

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

statement ok
SET tracing = on,kv,results; SELECT DISTINCT * FROM (VALUES (1, 2, 3), (4, 5, 6), (1, 2, 3), (1, 2, 4)) AS a; SET tracing = off

query T
SELECT message FROM [SHOW KV TRACE FOR SESSION]
 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)

statement ok
SET tracing = on,kv,results; SELECT count(*) FROM ab; SET tracing = off

query T
SELECT message FROM [SHOW KV TRACE FOR SESSION]
 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: [6]

# Note: the output from GROUP BY is in random order; the example is constructed so
# those rows are identical.
statement ok
SET tracing = on,kv,results; SELECT sum(b) FROM ab GROUP BY a; SET tracing = off

query T
SELECT message FROM [SHOW KV TRACE FOR SESSION]
 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
output row: [9]
fetched: /ab/primary/2/2 -> NULL
fetched: /ab/primary/2/6 -> NULL
fetched: /ab/primary/3/9 -> NULL
output row: [9]
output row: [9]

statement ok
SET tracing = on,kv,results; VALUES (1, 2), (1, 1), (1, 2), (2, 1), (2, 1) UNION VALUES (1, 3), (3, 4), (1, 1); SET tracing = off

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

statement ok
SET tracing = on,kv,results; SELECT * FROM abc EXCEPT SELECT * FROM abc WHERE b > 'p'; SET tracing = off

query T rowsort
SELECT message FROM [SHOW KV TRACE FOR SESSION]
 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

statement ok
SET tracing = on,kv,results; SELECT * FROM ab WHERE a > 1 INTERSECT SELECT * FROM ab WHERE b > 1; SET tracing = off

query T
SELECT message FROM [SHOW KV TRACE FOR SESSION]
 WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
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/1/4 -> NULL
fetched: /ab/primary/1/5 -> 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]

statement ok
SET tracing = on,kv,results; INSERT INTO ab(a, b) VALUES (42, 51); SET tracing = off

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

statement ok
SET tracing = on,kv,results; INSERT INTO ab(a, b) SELECT a+1, b+1 FROM ab WHERE b > 6; SET tracing = off

query T
SELECT message FROM [SHOW KV TRACE FOR SESSION]
 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/42/51 -> NULL

statement ok
SET tracing = on,kv,results; UPDATE ab SET a = a + 1 WHERE b > 6; SET tracing = off

query T
SELECT message FROM [SHOW KV TRACE FOR SESSION]
 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/4/10 -> NULL
fetched: /ab/primary/42/51 -> NULL
fetched: /ab/primary/43/52 -> NULL

statement ok
SET tracing = on,kv,results; DELETE FROM ab WHERE b > 6; SET tracing = off

query T
SELECT message FROM [SHOW KV TRACE FOR SESSION]
 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
fetched: /ab/primary/5/10 -> NULL
fetched: /ab/primary/43/51 -> NULL
fetched: /ab/primary/44/52 -> NULL

statement ok
SET tracing = on,kv,results; DELETE FROM ab; SET tracing = off

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

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

statement ok
SET tracing = on,kv,results; SELECT * FROM ab, abc WHERE abc.a+ab.b = abc.a*ab.a; SET tracing = off

query T
SELECT message FROM [SHOW KV TRACE FOR SESSION]
 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
