Skip to content

Commit

Permalink
sql/stats: fix inject/restore of partial stats not creating merged stats
Browse files Browse the repository at this point in the history
Previously, JSON statistics would not include the statistic ID. This
meant that restoring statement bundles would recreate stats with new
IDs, breaking the statisticID/fullStatisticID relationship between full
& partial stats and fail to recreate merged stats as a result. This
commit adds the statistic ID to JSONStatistic and recreates stats with
the same ID if present when injected. Allows for merged stats to be
correctly recreated following inject/restore of full and partial stats.

Fixes: #94101

See also: #125950

Release note (bug fix): Fixed a bug that prevented merged stats from
being created after injecting stats or recreating statement bundles.
This would occur when the injected stats/statement bundle contained
related full and partial statistics.
  • Loading branch information
Uzair5162 committed Jul 16, 2024
1 parent 1d7cc7f commit b41f0bc
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 91 deletions.
71 changes: 53 additions & 18 deletions pkg/sql/alter_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -1587,11 +1587,45 @@ func insertJSONStatistic(
fullStatisticIDValue = s.FullStatisticID
}

_ /* rows */, err := txn.Exec(
ctx,
"insert-stats",
txn.KV(),
`INSERT INTO system.table_statistics (
if s.StatisticID != 0 {
_ /* rows */, err := txn.Exec(
ctx,
"insert-stats",
txn.KV(),
`INSERT INTO system.table_statistics (
"statisticID",
"tableID",
"name",
"columnIDs",
"createdAt",
"rowCount",
"distinctCount",
"nullCount",
"avgSize",
histogram,
"partialPredicate",
"fullStatisticID"
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)`,
s.StatisticID,
tableID,
name,
columnIDs,
s.CreatedAt,
s.RowCount,
s.DistinctCount,
s.NullCount,
s.AvgSize,
histogram,
predicateValue,
fullStatisticIDValue,
)
return err
} else {
_ /* rows */, err := txn.Exec(
ctx,
"insert-stats",
txn.KV(),
`INSERT INTO system.table_statistics (
"tableID",
"name",
"columnIDs",
Expand All @@ -1604,19 +1638,20 @@ func insertJSONStatistic(
"partialPredicate",
"fullStatisticID"
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)`,
tableID,
name,
columnIDs,
s.CreatedAt,
s.RowCount,
s.DistinctCount,
s.NullCount,
s.AvgSize,
histogram,
predicateValue,
fullStatisticIDValue,
)
return err
tableID,
name,
columnIDs,
s.CreatedAt,
s.RowCount,
s.DistinctCount,
s.NullCount,
s.AvgSize,
histogram,
predicateValue,
fullStatisticIDValue,
)
return err
}
}

// validateConstraintNameIsNotUsed checks that the name of the constraint we're
Expand Down
65 changes: 45 additions & 20 deletions pkg/sql/logictest/testdata/logic_test/distsql_stats
Original file line number Diff line number Diff line change
Expand Up @@ -1631,7 +1631,9 @@ statement ok
CREATE STATISTICS s FROM all_null

query T
SELECT jsonb_pretty(COALESCE(json_agg(stat), '[]'))
SELECT jsonb_pretty(
regexp_replace(COALESCE(json_agg(stat), '[]')::STRING, '"statistic_id": [0-9]+', '"statistic_id": 0', 'g')::JSONB
)
FROM (
SELECT json_array_elements(statistics) - 'created_at' AS stat
FROM [SHOW STATISTICS USING JSON FOR TABLE all_null]
Expand All @@ -1656,7 +1658,8 @@ FROM [SHOW STATISTICS USING JSON FOR TABLE all_null]
"histo_version": 3,
"name": "s",
"null_count": 0,
"row_count": 1
"row_count": 1,
"statistic_id": 0
},
{
"avg_size": 0,
Expand All @@ -1668,7 +1671,8 @@ FROM [SHOW STATISTICS USING JSON FOR TABLE all_null]
"histo_version": 3,
"name": "s",
"null_count": 1,
"row_count": 1
"row_count": 1,
"statistic_id": 0
}
]

Expand All @@ -1689,7 +1693,9 @@ statement ok
CREATE STATISTICS s FROM greeting_stats

query T
SELECT jsonb_pretty(COALESCE(json_agg(stat), '[]'))
SELECT jsonb_pretty(
regexp_replace(COALESCE(json_agg(stat), '[]')::STRING, '"statistic_id": [0-9]+', '"statistic_id": 0', 'g')::JSONB
)
FROM (
SELECT json_array_elements(statistics) - 'created_at' AS stat
FROM [SHOW STATISTICS USING JSON FOR TABLE greeting_stats]
Expand All @@ -1714,7 +1720,8 @@ FROM [SHOW STATISTICS USING JSON FOR TABLE greeting_stats]
"histo_version": 3,
"name": "s",
"null_count": 0,
"row_count": 1
"row_count": 1,
"statistic_id": 0
}
]

Expand Down Expand Up @@ -1877,7 +1884,9 @@ SHOW HISTOGRAM $hist_id_1
upper_bound range_rows distinct_range_rows equal_rows

query T
SELECT jsonb_pretty(COALESCE(json_agg(stat), '[]'))
SELECT jsonb_pretty(
regexp_replace(COALESCE(json_agg(stat), '[]')::STRING, '"statistic_id": [0-9]+', '"statistic_id": 0', 'g')::JSONB
)
FROM (SELECT json_array_elements(statistics) - 'created_at' AS stat
FROM [SHOW STATISTICS USING JSON FOR TABLE tabula])
----
Expand All @@ -1892,7 +1901,8 @@ SELECT jsonb_pretty(COALESCE(json_agg(stat), '[]'))
"histo_version": 3,
"name": "aristotle",
"null_count": 0,
"row_count": 0
"row_count": 0,
"statistic_id": 0
},
{
"avg_size": 0,
Expand All @@ -1904,7 +1914,8 @@ SELECT jsonb_pretty(COALESCE(json_agg(stat), '[]'))
"histo_version": 3,
"name": "aristotle",
"null_count": 0,
"row_count": 0
"row_count": 0,
"statistic_id": 0
},
{
"avg_size": 0,
Expand All @@ -1916,7 +1927,8 @@ SELECT jsonb_pretty(COALESCE(json_agg(stat), '[]'))
"histo_col_type": "",
"name": "aristotle",
"null_count": 0,
"row_count": 0
"row_count": 0,
"statistic_id": 0
},
{
"avg_size": 0,
Expand All @@ -1928,7 +1940,8 @@ SELECT jsonb_pretty(COALESCE(json_agg(stat), '[]'))
"histo_version": 3,
"name": "aristotle",
"null_count": 0,
"row_count": 0
"row_count": 0,
"statistic_id": 0
}
]

Expand Down Expand Up @@ -1973,7 +1986,9 @@ SHOW HISTOGRAM $hist_id_1
upper_bound range_rows distinct_range_rows equal_rows

query T
SELECT jsonb_pretty(COALESCE(json_agg(stat), '[]'))
SELECT jsonb_pretty(
regexp_replace(COALESCE(json_agg(stat), '[]')::STRING, '"statistic_id": [0-9]+', '"statistic_id": 0', 'g')::JSONB
)
FROM (SELECT json_array_elements(statistics) - 'created_at' - 'avg_size' AS stat
FROM [SHOW STATISTICS USING JSON FOR TABLE tabula])
----
Expand All @@ -1995,7 +2010,8 @@ SELECT jsonb_pretty(COALESCE(json_agg(stat), '[]'))
"histo_version": 3,
"name": "locke",
"null_count": 0,
"row_count": 1
"row_count": 1,
"statistic_id": 0
},
{
"columns": [
Expand All @@ -2014,7 +2030,8 @@ SELECT jsonb_pretty(COALESCE(json_agg(stat), '[]'))
"histo_version": 3,
"name": "locke",
"null_count": 0,
"row_count": 1
"row_count": 1,
"statistic_id": 0
},
{
"columns": [
Expand All @@ -2025,7 +2042,8 @@ SELECT jsonb_pretty(COALESCE(json_agg(stat), '[]'))
"histo_col_type": "",
"name": "locke",
"null_count": 0,
"row_count": 1
"row_count": 1,
"statistic_id": 0
},
{
"columns": [
Expand All @@ -2036,7 +2054,8 @@ SELECT jsonb_pretty(COALESCE(json_agg(stat), '[]'))
"histo_version": 3,
"name": "locke",
"null_count": 1,
"row_count": 1
"row_count": 1,
"statistic_id": 0
}
]

Expand Down Expand Up @@ -2081,7 +2100,9 @@ NULL {rowid} 0 0 0 true
t1_ab {a,b} 0 0 0 false

query T
SELECT jsonb_pretty(COALESCE(json_agg(stat), '[]'))
SELECT jsonb_pretty(
regexp_replace(COALESCE(json_agg(stat), '[]')::STRING, '"statistic_id": [0-9]+', '"statistic_id": 0', 'g')::JSONB
)
FROM (
SELECT json_array_elements(statistics) - 'created_at' AS stat
FROM [SHOW STATISTICS USING JSON FOR TABLE t1]
Expand All @@ -2097,7 +2118,8 @@ FROM [SHOW STATISTICS USING JSON FOR TABLE t1]
"histo_col_type": "INT8",
"histo_version": 3,
"null_count": 0,
"row_count": 0
"row_count": 0,
"statistic_id": 0
},
{
"avg_size": 0,
Expand All @@ -2108,7 +2130,8 @@ FROM [SHOW STATISTICS USING JSON FOR TABLE t1]
"histo_col_type": "INT8",
"histo_version": 3,
"null_count": 0,
"row_count": 0
"row_count": 0,
"statistic_id": 0
},
{
"avg_size": 0,
Expand All @@ -2119,7 +2142,8 @@ FROM [SHOW STATISTICS USING JSON FOR TABLE t1]
"histo_col_type": "INT8",
"histo_version": 3,
"null_count": 0,
"row_count": 0
"row_count": 0,
"statistic_id": 0
},
{
"avg_size": 0,
Expand All @@ -2131,7 +2155,8 @@ FROM [SHOW STATISTICS USING JSON FOR TABLE t1]
"histo_col_type": "",
"name": "t1_ab",
"null_count": 0,
"row_count": 0
"row_count": 0,
"statistic_id": 0
}
]

Expand Down
Loading

0 comments on commit b41f0bc

Please sign in to comment.