Skip to content

Commit 56022cd

Browse files
craig[bot]normanchennkyle-a-wong
committed
144394: jsonpath: optimize string lookups with arrays instead of maps r=normanchenn a=normanchenn This commit replaces MethodTypeStrings and OperationTypeStrings maps with arrays since the number of keys are small. The String() methods both now include bounds checking to handle invalid indices. Epic: None Release note: None 144517: sqlstatsutil: add sqlType to statement stats json fields r=kyle-a-wong a=kyle-a-wong The jsonFields receiver on innerStmtStats didn't include the sqlType field, so the sql activity apis always returned an empty string for this field. This field is expected in db console and used to filter sql activity fingerprints by sql type. Now, this field is properly set and can be used to filter fingerprints in sql activity Fixes: CC-31238 Epic: CC-30965 Release note (bug fix): Fixes a bug in the sql activity page where filtering on "Statement Type" resulted in 0 results returned. This filter should now work. Co-authored-by: Norman Chen <[email protected]> Co-authored-by: Kyle Wong <[email protected]>
3 parents 3fd6c60 + 2a7d9e4 + 8419776 commit 56022cd

File tree

4 files changed

+14
-5
lines changed

4 files changed

+14
-5
lines changed

pkg/sql/sqlstats/persistedsqlstats/sqlstatsutil/json_encoding_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ func TestSQLStatsJsonEncoding(t *testing.T) {
106106
"min": {{.Float}},
107107
"max": {{.Float}}
108108
},
109-
"lastErrorCode": "{{.String}}"
109+
"lastErrorCode": "{{.String}}",
110+
"sqlType": "{{.String}}"
110111
},
111112
"execution_statistics": {
112113
"cnt": {{.Int64}},

pkg/sql/sqlstats/persistedsqlstats/sqlstatsutil/json_impl.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ func (s *innerStmtStats) jsonFields() jsonFields {
345345
{"lastErrorCode", (*jsonString)(&s.LastErrorCode)},
346346
{"failureCount", (*jsonInt)(&s.FailureCount)},
347347
{"genericCount", (*jsonInt)(&s.GenericCount)},
348+
{"sqlType", (*jsonString)(&s.SQLType)},
348349
}
349350
}
350351

pkg/util/jsonpath/method.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const (
1515
TypeMethod
1616
)
1717

18-
var MethodTypeStrings = map[MethodType]string{
18+
var methodTypeStrings = [...]string{
1919
SizeMethod: "size",
2020
TypeMethod: "type",
2121
}
@@ -27,5 +27,8 @@ type Method struct {
2727
var _ Path = Method{}
2828

2929
func (m Method) String() string {
30-
return fmt.Sprintf(".%s()", MethodTypeStrings[m.Type])
30+
if int(m.Type) < 0 || int(m.Type) >= len(methodTypeStrings) || m.Type == InvalidMethod {
31+
panic(fmt.Sprintf("invalid method type: %d", m.Type))
32+
}
33+
return fmt.Sprintf(".%s()", methodTypeStrings[m.Type])
3134
}

pkg/util/jsonpath/operation.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import "fmt"
1010
type OperationType int
1111

1212
const (
13-
OpCompEqual OperationType = iota
13+
OpInvalid OperationType = iota
14+
OpCompEqual
1415
OpCompNotEqual
1516
OpCompLess
1617
OpCompLessEqual
@@ -32,7 +33,7 @@ const (
3233
OpStartsWith
3334
)
3435

35-
var OperationTypeStrings = map[OperationType]string{
36+
var OperationTypeStrings = [...]string{
3637
OpCompEqual: "==",
3738
OpCompNotEqual: "!=",
3839
OpCompLess: "<",
@@ -64,6 +65,9 @@ type Operation struct {
6465
var _ Path = Operation{}
6566

6667
func (o Operation) String() string {
68+
if int(o.Type) < 0 || int(o.Type) >= len(OperationTypeStrings) || o.Type == OpInvalid {
69+
panic(fmt.Sprintf("invalid operation type: %d", o.Type))
70+
}
6771
// TODO(normanchenn): Fix recursive brackets. When there is a operation like
6872
// 1 == 1 && 1 != 1, postgres will output (1 == 1 && 1 != 1), but we output
6973
// ((1 == 1) && (1 != 1)).

0 commit comments

Comments
 (0)