# knob-opt: sync-event-log

###################
# CREATE STATISTICS
###################

# Keep auto stats jobs on system tables from disrupting tests in this file.
statement ok
SET CLUSTER SETTING sql.stats.system_tables_autostats.enabled = FALSE

# This test verifies that events are posted for table statistics creation.
statement ok
SET CLUSTER SETTING sql.stats.post_events.enabled = TRUE

statement ok
CREATE TABLE a (id INT PRIMARY KEY, x INT, y INT, INDEX x_idx (x, y))

statement ok
CREATE STATISTICS s1 ON id FROM a

# TODO(msirek): Re-enable this test once autostats collection is made
#               consistently retryable.
# statement ok retry
# CREATE STATISTICS __auto__ FROM a

# Check explicitly for table id 106. System tables could trigger autostats
# collections at any time.
query IT
SELECT "reportingID", "info"::JSONB - 'Timestamp' - 'DescriptorID' - 'ApplicationName'
FROM system.eventlog
WHERE "eventType" = 'create_statistics' AND ("info"::JSONB ->> 'DescriptorID')::INT = 106
ORDER BY "timestamp", info
----
1  {"EventType": "create_statistics", "Statement": "CREATE STATISTICS s1 ON id FROM test.public.a WITH OPTIONS AS OF SYSTEM TIME '-1us'", "TableName": "test.public.a", "Tag": "CREATE STATISTICS", "User": "root"}

statement ok
DROP TABLE a
