# LogicTest: default

# Check that node_statement_statistics report per application

statement ok
SET application_name = hello; SELECT 1

statement ok
SET application_name = world; SELECT 2

query B
SELECT count > 0 FROM crdb_internal.node_statement_statistics WHERE application_name IN ('hello', 'world')
----
true
true

# Check that node_statement_statistics report per statement

statement ok
SET application_name = hello; SELECT 1; SELECT 1,2; SELECT 1

# reset for other tests.
statement ok
SET application_name = ''

query TB
SELECT key, count >= 1 FROM crdb_internal.node_statement_statistics WHERE application_name = 'hello' AND key LIKE 'SELECT%' ORDER BY key
----
SELECT _    true
SELECT _, _ true

statement ok
CREATE TABLE test(x INT, y INT, z INT); INSERT INTO test(x, y, z) VALUES (0,0,0);

# Disable DistSQL for most statements, so that they don't get the "+" flag.
statement ok
SET distsql = off

statement ok
SET application_name = 'valuetest'

# Check that shortening goes through functions.

statement ok
SELECT sin(1.23)

# Check stats for query errors.
statement error cannot take square root
SELECT sqrt(-1.0)

# Check that shortened queries can use virtual tables.

statement ok
SELECT key FROM crdb_internal.node_statement_statistics

# Check that multi-value clauses are shortened.

statement ok
SELECT x FROM (VALUES (1,2,3), (4,5,6)) AS t(x)

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

# Check that the RHS of IN comparisons are shortened.

statement ok
SELECT x FROM test WHERE y IN (4, 5, 6, 7, 8)

statement ok
SELECT x FROM test WHERE y NOT IN (4, 5, 6, 7, 8)

# Check that a non-constant prevents shortening.

statement ok
SELECT x FROM test WHERE y IN (4, 5, 6+x, 7, 8)

# Check that tuples in other positions are not shortened.

statement ok
SELECT ROW(1,2,3,4,5) FROM test WHERE FALSE

# Make one query run in distsql mode to test the flag
# and flag combinations

statement ok
set distsql = on

statement ok
SELECT x FROM test WHERE y IN (4, 5, 6, 7, 8)

statement error division by zero
SELECT x FROM test WHERE y = 1/z

statement ok
SET application_name = ''; RESET distsql

query TT colnames
SELECT key,flags FROM crdb_internal.node_statement_statistics WHERE application_name = 'valuetest' ORDER BY key, flags
----
key                                                      flags
INSERT INTO test VALUES (_, _, _)
SELECT ROW(_, _, _, _, _) FROM test WHERE _
SELECT key FROM crdb_internal.node_statement_statistics
SELECT sin(_)
SELECT sqrt(- _)                                         !
SELECT x FROM (VALUES (_, _, _)) AS t (x)
SELECT x FROM test WHERE y = (_ / z)                     !+
SELECT x FROM test WHERE y IN (_, _)
SELECT x FROM test WHERE y IN (_, _)                     +
SELECT x FROM test WHERE y IN (_, _, _ + x, _, _)
SELECT x FROM test WHERE y NOT IN (_, _)

# Check that names are anonymized properly:
# - virtual table names are preserved
# - function names are preserved
query T
SELECT anonymized FROM crdb_internal.node_statement_statistics WHERE application_name = 'valuetest' ORDER BY key
----
INSERT INTO _ VALUES (_, _, _)
SELECT ROW(_, _, _, _, _) FROM _ WHERE _
SELECT _ FROM crdb_internal.node_statement_statistics
SELECT sin(_)
SELECT sqrt(- _)
SELECT _ FROM (VALUES (_, _, _)) AS _ (_)
SELECT _ FROM _ WHERE _ = (_ / _)
SELECT _ FROM _ WHERE _ IN (_, _)
SELECT _ FROM _ WHERE _ IN (_, _)
SELECT _ FROM _ WHERE _ IN (_, _, _ + _, _, _)
SELECT _ FROM _ WHERE _ NOT IN (_, _)
