# LogicTest: fakedist fakedist-opt fakedist-metadata

statement ok
CREATE TABLE data (a INT, b INT, c FLOAT, d DECIMAL, PRIMARY KEY (a, b, c), INDEX d_idx (d))

# Enable automatic stats
statement ok
SET CLUSTER SETTING sql.stats.automatic_collection.enabled = true

# Generate all combinations of values 1 to 10.
statement ok
INSERT INTO data SELECT a, b, c::FLOAT, NULL FROM
   generate_series(1, 10) AS a(a),
   generate_series(1, 10) AS b(b),
   generate_series(1, 10) AS c(c)

query TTIII colnames,rowsort,retry
SELECT statistics_name, column_names, row_count, distinct_count, null_count
FROM [SHOW STATISTICS FOR TABLE data] ORDER BY column_names::STRING, created
----
statistics_name  column_names  row_count  distinct_count  null_count
__auto__         {a}           1000       10              0
__auto__         {b}           1000       10              0
__auto__         {c}           1000       10              0
__auto__         {d}           1000       0               1000

# Disable automatic stats
statement ok
SET CLUSTER SETTING sql.stats.automatic_collection.enabled = false

# Update more than 20% of the table.
statement ok
UPDATE data SET d = 10 WHERE (a = 1 OR a = 2 OR a = 3) AND b > 1

# There should be no change to stats.
query TTIII colnames,rowsort
SELECT statistics_name, column_names, row_count, distinct_count, null_count
FROM [SHOW STATISTICS FOR TABLE data] ORDER BY column_names::STRING, created
----
statistics_name  column_names  row_count  distinct_count  null_count
__auto__         {a}           1000       10              0
__auto__         {b}           1000       10              0
__auto__         {c}           1000       10              0
__auto__         {d}           1000       0               1000

# Enable automatic stats
statement ok
SET CLUSTER SETTING sql.stats.automatic_collection.enabled = true

# Update more than 20% of the table.
statement ok
UPDATE data SET d = 12 WHERE d = 10

query TTIII colnames,rowsort,retry
SELECT statistics_name, column_names, row_count, distinct_count, null_count
FROM [SHOW STATISTICS FOR TABLE data] ORDER BY column_names::STRING, created
----
statistics_name  column_names  row_count  distinct_count  null_count
__auto__         {a}           1000       10              0
__auto__         {a}           1000       10              0
__auto__         {b}           1000       10              0
__auto__         {b}           1000       10              0
__auto__         {c}           1000       10              0
__auto__         {c}           1000       10              0
__auto__         {d}           1000       0               1000
__auto__         {d}           1000       1               730

# Upsert more than 20% of the table.
statement ok
UPSERT INTO data SELECT a, b, c::FLOAT, 1 FROM
generate_series(1, 11) AS a(a),
generate_series(1, 10) AS b(b),
generate_series(1, 5) AS c(c)

query TTIII colnames,rowsort,retry
SELECT statistics_name, column_names, row_count, distinct_count, null_count
FROM [SHOW STATISTICS FOR TABLE data] ORDER BY column_names::STRING, created
----
statistics_name  column_names  row_count  distinct_count  null_count
__auto__         {a}           1000       10              0
__auto__         {a}           1000       10              0
__auto__         {a}           1050       11              0
__auto__         {b}           1000       10              0
__auto__         {b}           1000       10              0
__auto__         {b}           1050       10              0
__auto__         {c}           1000       10              0
__auto__         {c}           1000       10              0
__auto__         {c}           1050       10              0
__auto__         {d}           1000       0               1000
__auto__         {d}           1000       1               730
__auto__         {d}           1050       2               365

# Delete more than 20% of the table.
statement ok
DELETE FROM data WHERE c > 5

query TTIII colnames,rowsort,retry
SELECT statistics_name, column_names, row_count, distinct_count, null_count
FROM [SHOW STATISTICS FOR TABLE data] ORDER BY column_names::STRING, created
----
statistics_name  column_names  row_count  distinct_count  null_count
__auto__         {a}           1000       10              0
__auto__         {a}           1000       10              0
__auto__         {a}           1050       11              0
__auto__         {a}           550        11              0
__auto__         {b}           1000       10              0
__auto__         {b}           1000       10              0
__auto__         {b}           1050       10              0
__auto__         {b}           550        10              0
__auto__         {c}           1000       10              0
__auto__         {c}           1000       10              0
__auto__         {c}           1050       10              0
__auto__         {c}           550        5               0
__auto__         {d}           1000       0               1000
__auto__         {d}           1000       1               730
__auto__         {d}           1050       2               365
__auto__         {d}           550        1               0

# Test CREATE TABLE ... AS
statement ok
CREATE TABLE copy AS SELECT * FROM data

# Distinct count for rowid can be flaky, so don't show it. The estimate is
# almost always 550, but occasionally it is 549...
query TTII colnames,rowsort,retry
SELECT statistics_name, column_names, row_count, null_count
FROM [SHOW STATISTICS FOR TABLE copy] ORDER BY column_names::STRING, created
----
statistics_name  column_names  row_count  null_count
__auto__         {a}           550        0
__auto__         {b}           550        0
__auto__         {c}           550        0
__auto__         {d}           550        0
__auto__         {rowid}       550        0

# Test fast path delete.
statement ok
DELETE FROM copy WHERE true

query TTII colnames,rowsort,retry
SELECT statistics_name, column_names, row_count, null_count
FROM [SHOW STATISTICS FOR TABLE copy] ORDER BY column_names::STRING, created
----
statistics_name  column_names  row_count  null_count
__auto__         {a}           550        0
__auto__         {a}           0          0
__auto__         {b}           550        0
__auto__         {b}           0          0
__auto__         {c}           550        0
__auto__         {c}           0          0
__auto__         {d}           550        0
__auto__         {d}           0          0
__auto__         {rowid}       550        0
__auto__         {rowid}       0          0
