Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: add backward compatibility to stats and plan #70683

Merged
merged 1 commit into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions pkg/sql/sqlstats/persistedsqlstats/sqlstatsutil/json_decoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,18 @@ func DecodeStmtStatsStatisticsJSON(jsonVal json.JSON, result *roachpb.StatementS
func JSONToExplainTreePlanNode(jsonVal json.JSON) (*roachpb.ExplainTreePlanNode, error) {
node := roachpb.ExplainTreePlanNode{}

nameAttr, err := safeFetchVal(jsonVal, "Name")
nameAttr, err := jsonVal.FetchValKey("Name")
if err != nil {
return nil, err
}

str, err := nameAttr.AsText()
if err != nil {
return nil, err
if nameAttr != nil {
str, err := nameAttr.AsText()
if err != nil {
return nil, err
}
node.Name = *str
}
node.Name = *str

iter, err := jsonVal.ObjectIter()
if err != nil {
Expand All @@ -86,15 +88,17 @@ func JSONToExplainTreePlanNode(jsonVal json.JSON) (*roachpb.ExplainTreePlanNode,

if key == "Children" {
for childIdx := 0; childIdx < value.Len(); childIdx++ {
childJSON, err := safeFetchValIdx(value, childIdx)
childJSON, err := value.FetchValIdx(childIdx)
if err != nil {
return nil, err
}
child, err := JSONToExplainTreePlanNode(childJSON)
if err != nil {
return nil, err
if childJSON != nil {
child, err := JSONToExplainTreePlanNode(childJSON)
if err != nil {
return nil, err
}
node.Children = append(node.Children, child)
}
node.Children = append(node.Children, child)
}
} else {
str, err := value.AsText()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,121 @@ func TestSQLStatsJsonEncoding(t *testing.T) {
require.Equal(t, input, actualJSONUnmarshalled)
})

// When a new statistic is added to a statement payload, older versions won't have the
// new parameter, so this test is to confirm that all other parameters will be set and
// the new one will be empty, without breaking the decoding process.
t.Run("statement_statistics with new parameter", func(t *testing.T) {
data := genRandomData()
expectedStatistics := roachpb.CollectedStatementStatistics{}

expectedMetadataStrTemplate := `
{
"stmtTyp": "{{.String}}",
"query": "{{.String}}",
"db": "{{.String}}",
"distsql": {{.Bool}},
"failed": {{.Bool}},
"implicitTxn": {{.Bool}},
"vec": {{.Bool}},
"fullScan": {{.Bool}}
}
`
expectedStatisticsStrTemplate := `
{
"statistics": {
"cnt": {{.Int64}},
"firstAttemptCnt": {{.Int64}},
"maxRetries": {{.Int64}},
"lastExecAt": "{{stringifyTime .Time}}",
"numRows": {
"mean": {{.Float}},
"sqDiff": {{.Float}}
},
"parseLat": {
"mean": {{.Float}},
"sqDiff": {{.Float}}
},
"planLat": {
"mean": {{.Float}},
"sqDiff": {{.Float}}
},
"runLat": {
"mean": {{.Float}},
"sqDiff": {{.Float}}
},
"svcLat": {
"mean": {{.Float}},
"sqDiff": {{.Float}}
},
"ovhLat": {
"mean": {{.Float}},
"sqDiff": {{.Float}}
},
"bytesRead": {
"mean": {{.Float}},
"sqDiff": {{.Float}}
},
"rowsRead": {
"mean": {{.Float}},
"sqDiff": {{.Float}}
},
"rowsWritten": {
"mean": {{.Float}},
"sqDiff": {{.Float}}
},
"nodes": [{{joinInts .IntArray}}]
},
"execution_statistics": {
"cnt": {{.Int64}},
"networkBytes": {
"mean": {{.Float}},
"sqDiff": {{.Float}}
},
"maxMemUsage": {
"mean": {{.Float}},
"sqDiff": {{.Float}}
},
"contentionTime": {
"mean": {{.Float}},
"sqDiff": {{.Float}}
},
"networkMsgs": {
"mean": {{.Float}},
"sqDiff": {{.Float}}
},
"maxDiskUsage": {
"mean": {{.Float}},
"sqDiff": {{.Float}}
}
}
}
`

fillTemplate(t, expectedMetadataStrTemplate, data)
fillTemplate(t, expectedStatisticsStrTemplate, data)
fillObject(t, reflect.ValueOf(&expectedStatistics), &data)

actualMetadataJSON, err := BuildStmtMetadataJSON(&expectedStatistics)
require.NoError(t, err)
actualStatisticsJSON, err := BuildStmtStatisticsJSON(&expectedStatistics.Stats)
require.NoError(t, err)

var actualJSONUnmarshalled roachpb.CollectedStatementStatistics

err = DecodeStmtStatsMetadataJSON(actualMetadataJSON, &actualJSONUnmarshalled)
require.NoError(t, err)

// Remove one of the statistics on the object so its value doesn't get populated on
// the final actualJSONUnmarshalled.Stats.
actualStatisticsJSON, _, _ = actualStatisticsJSON.RemovePath([]string{"statistics", "numRows"})
// Initialize the field again to remove the existing value.
expectedStatistics.Stats.NumRows = roachpb.NumericStat{}

err = DecodeStmtStatsStatisticsJSON(actualStatisticsJSON, &actualJSONUnmarshalled.Stats)
require.NoError(t, err)
require.Equal(t, expectedStatistics, actualJSONUnmarshalled)
})

t.Run("transaction_statistics", func(t *testing.T) {
data := genRandomData()

Expand Down
32 changes: 6 additions & 26 deletions pkg/sql/sqlstats/persistedsqlstats/sqlstatsutil/json_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,13 +296,15 @@ func (jf jsonFields) decodeJSON(js json.JSON) (err error) {

for i := range jf {
fieldName = jf[i].field
field, err := safeFetchVal(js, fieldName)
field, err := js.FetchValKey(fieldName)
if err != nil {
return err
}
err = jf[i].val.decodeJSON(field)
if err != nil {
return err
if field != nil {
err = jf[i].val.decodeJSON(field)
if err != nil {
return err
}
}
}

Expand Down Expand Up @@ -437,25 +439,3 @@ func (d *decimal) decodeJSON(js json.JSON) error {
func (d *decimal) encodeJSON() (json.JSON, error) {
return json.FromDecimal(*(*apd.Decimal)(d)), nil
}

func safeFetchVal(jsonVal json.JSON, key string) (json.JSON, error) {
field, err := jsonVal.FetchValKey(key)
if err != nil {
return nil, err
}
if field == nil {
return nil, errors.Newf("%s field is not found in the JSON payload", key)
}
return field, nil
}

func safeFetchValIdx(jsonVal json.JSON, idx int) (json.JSON, error) {
field, err := jsonVal.FetchValIdx(idx)
if err != nil {
return nil, err
}
if field == nil {
return nil, errors.Newf("%dth element is not found in the JSON payload", idx)
}
return field, nil
}