set-time unixSecs=1610000000
----

update-cache injectSpanStatsErrors=error1
----
updatedTables: 62, errors: 4, run #: 1, duration > 0: true

# Since this is the first update and we encountered an error we should see the zero value for
# the non nullable columns, except for the last updated time which is set to the current time.
query
SELECT * FROM system.table_metadata
WHERE db_name <> 'system' OR table_name IN ('comments', 'locations', 'descriptor_id_seq')
ORDER BY (db_name, table_name)
----
1 24 system public comments 4 1 {} 0 0 0 0 0 An error has occurred while fetching span stats. 2021-01-07 06:13:20 +0000 UTC TABLE {"auto_stats_enabled": null, "replica_count": 0, "stats_last_updated": null}
1 7 system public descriptor_id_seq 1 1 {} 0 0 0 0 0 An error has occurred while fetching span stats. 2021-01-07 06:13:20 +0000 UTC SEQUENCE {"auto_stats_enabled": null, "replica_count": 0, "stats_last_updated": null}
1 21 system public locations 4 1 {} 0 0 0 0 0 An error has occurred while fetching span stats. 2021-01-07 06:13:20 +0000 UTC TABLE {"auto_stats_enabled": null, "replica_count": 0, "stats_last_updated": null}


set-time unixSecs=1710000000
----

update-cache
----
updatedTables: 62, errors: 0, run #: 2, duration > 0: true

# Now the last_update_error column should be nil and data
# should be updated.
query
SELECT
  table_name,
  db_name,
  schema_name,
  db_id,
  table_id,
  total_columns,
  total_indexes,
  table_type,
  details->'auto_stats_enabled',
  store_ids,
  total_ranges,
  last_updated,
  last_update_error
FROM system.table_metadata
WHERE db_name <> 'system' OR table_name IN ('comments', 'locations', 'descriptor_id_seq')
ORDER BY (db_name, table_name)
----
comments system public 1 24 4 1 TABLE null {1} 1 2024-03-09 16:00:00 +0000 UTC <nil>
descriptor_id_seq system public 1 7 1 1 SEQUENCE null {1} 1 2024-03-09 16:00:00 +0000 UTC <nil>
locations system public 1 21 4 1 TABLE null {1} 1 2024-03-09 16:00:00 +0000 UTC <nil>


set-time unixSecs=1711000000
----

# Verify that encountering a new error does not clear the previous row data,
# including the last_updated time.
update-cache injectSpanStatsErrors=error2,error3
----
updatedTables: 62, errors: 4, run #: 3, duration > 0: true

query
SELECT
  db_id,
  table_id
  db_name,
  table_name,
  total_columns,
  store_ids,
  total_ranges,
  last_updated,
  last_update_error
FROM system.table_metadata
WHERE db_name <> 'system' OR table_name IN ('comments', 'locations', 'descriptor_id_seq')
----
1 7 descriptor_id_seq 1 {1} 1 2024-03-09 16:00:00 +0000 UTC An error has occurred while fetching span stats.
1 21 locations 4 {1} 1 2024-03-09 16:00:00 +0000 UTC An error has occurred while fetching span stats.
1 24 comments 4 {1} 1 2024-03-09 16:00:00 +0000 UTC An error has occurred while fetching span stats.


set-time unixSecs=1810010000
----

# Test that a span stats rpc error for a single batch does not affect the other batches
# from being retrieved. We should see tables with an error have the previous date.

update-cache injectSpanStatsErrors=error4 spanStatsErrBatch=1
----
updatedTables: 62, errors: 1, run #: 4, duration > 0: true

query
SELECT
  last_updated,
  last_update_error,
  db_id,
  table_id
  db_name,
  table_name
FROM system.table_metadata
WHERE db_name <> 'system' OR table_name IN ('comments', 'locations', 'descriptor_id_seq')
ORDER BY last_updated
----
2024-03-09 16:00:00 +0000 UTC An error has occurred while fetching span stats. 1 7 descriptor_id_seq
2024-03-09 16:00:00 +0000 UTC An error has occurred while fetching span stats. 1 21 locations
2024-03-09 16:00:00 +0000 UTC An error has occurred while fetching span stats. 1 24 comments
