Skip to content

Commit

Permalink
*: fix flaky test TestRecordHistoryStatsAfterAnalyze (#55365)
Browse files Browse the repository at this point in the history
close #55364
  • Loading branch information
hawkingrei authored Aug 12, 2024
1 parent 2fb61a5 commit cde5603
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/executor/historical_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func TestRecordHistoryStatsAfterAnalyze(t *testing.T) {
// 3. dump current stats json
dumpJSONTable, err := h.DumpStatsToJSON("test", tableInfo.Meta(), nil, true)
require.NoError(t, err)
dumpJSONTable.Sort()
jsOrigin, _ := json.Marshal(dumpJSONTable)

// 4. get the historical stats json
Expand All @@ -89,6 +90,7 @@ func TestRecordHistoryStatsAfterAnalyze(t *testing.T) {
}
jsonTbl, err := storage.BlocksToJSONTable(data)
require.NoError(t, err)
jsonTbl.Sort()
jsCur, err := json.Marshal(jsonTbl)
require.NoError(t, err)
// 5. historical stats must be equal to the current stats
Expand Down
9 changes: 9 additions & 0 deletions pkg/statistics/handle/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
package util

import (
"cmp"
"context"
"slices"
"strconv"
"time"

Expand Down Expand Up @@ -258,6 +260,13 @@ type JSONTable struct {
IsHistoricalStats bool `json:"is_historical_stats"`
}

// Sort is used to sort the object in the JSONTable. it is used for testing to avoid flaky test.
func (j *JSONTable) Sort() {
slices.SortFunc(j.PredicateColumns, func(a, b *JSONPredicateColumn) int {
return cmp.Compare(a.ID, b.ID)
})
}

// JSONExtendedStats is used for dumping extended statistics.
type JSONExtendedStats struct {
StatsName string `json:"stats_name"`
Expand Down

0 comments on commit cde5603

Please sign in to comment.