-- source include/not_embedded.inc
-- source ../include/ps_truncate_all_tables.inc
# Tests for sys schema
# Verify the sys.schema_tables_with_full_table_scans view

# Ensure structure changes don't slip in
DESC sys.schema_tables_with_full_table_scans;

# Make sure view select does not error, but ignore results
--disable_result_log
SELECT * FROM sys.schema_tables_with_full_table_scans;
--enable_result_log


# Ensure structure changes don't slip in
DESC sys.x$schema_tables_with_full_table_scans;

# Make sure view select does not error, but ignore results
--disable_result_log
SELECT * FROM sys.x$schema_tables_with_full_table_scans;
--enable_result_log

# Create a dummy table, force some full table scans, verify the results
CREATE DATABASE v_schema_tables_with_full_table_scans;

CREATE TABLE v_schema_tables_with_full_table_scans.t (i BIGINT AUTO_INCREMENT PRIMARY KEY, j BIGINT) ENGINE = innodb;

INSERT INTO v_schema_tables_with_full_table_scans.t (j) VALUES (1), (2), (3);

INSERT INTO v_schema_tables_with_full_table_scans.t (j) (SELECT j*2 FROM v_schema_tables_with_full_table_scans.t);

INSERT INTO v_schema_tables_with_full_table_scans.t (j) (SELECT j*2 FROM v_schema_tables_with_full_table_scans.t);

INSERT INTO v_schema_tables_with_full_table_scans.t (j) (SELECT j*2 FROM v_schema_tables_with_full_table_scans.t);

INSERT INTO v_schema_tables_with_full_table_scans.t (j) (SELECT j*2 FROM v_schema_tables_with_full_table_scans.t);

INSERT INTO v_schema_tables_with_full_table_scans.t (j) (SELECT j*2 FROM v_schema_tables_with_full_table_scans.t);

INSERT INTO v_schema_tables_with_full_table_scans.t (j) (SELECT j*2 FROM v_schema_tables_with_full_table_scans.t);

INSERT INTO v_schema_tables_with_full_table_scans.t (j) (SELECT j*2 FROM v_schema_tables_with_full_table_scans.t);

INSERT INTO v_schema_tables_with_full_table_scans.t (j) (SELECT j*2 FROM v_schema_tables_with_full_table_scans.t);

# The table should now have 768 rows

# Now truncate the P_S tables, to get fresh results
CALL sys.ps_truncate_all_tables(false);

# Following returns 28 rows, but should full scan as j has no index
# Using RAND() to force no query caching, so ignore results, they are not important
--disable_result_log
SELECT i, j, RAND() FROM v_schema_tables_with_full_table_scans.t WHERE j = 12;
--enable_result_log

# Now verify the table shows up in the views with the right row count (should be 768)
SELECT object_schema, object_name, rows_full_scanned FROM sys.schema_tables_with_full_table_scans;

SELECT object_schema, object_name, rows_full_scanned FROM sys.x$schema_tables_with_full_table_scans;

# Scan again
--disable_result_log
SELECT i, j, RAND() FROM v_schema_tables_with_full_table_scans.t WHERE j = 12;
--enable_result_log

# Now verify that double the amount of rows in the table should be shown as scanned (should be 1536)
SELECT object_schema, object_name, rows_full_scanned FROM sys.schema_tables_with_full_table_scans;

SELECT object_schema, object_name, rows_full_scanned FROM sys.x$schema_tables_with_full_table_scans;

# Do a point lookup
SELECT * FROM v_schema_tables_with_full_table_scans.t WHERE i = 10;

# The number of rows scanned should not have changed (should still be 1536)
SELECT object_schema, object_name, rows_full_scanned FROM sys.schema_tables_with_full_table_scans;

SELECT object_schema, object_name, rows_full_scanned FROM sys.x$schema_tables_with_full_table_scans;

# Cleanup
DROP DATABASE v_schema_tables_with_full_table_scans;

# Ensure structure changes don't slip in
DESC sys.x$schema_tables_with_full_table_scans;

# Make sure view select does not error, but ignore results
--disable_result_log
SELECT * FROM sys.x$schema_tables_with_full_table_scans;
--enable_result_log

