Skip to content

Commit 54ba68b

Browse files
committed
update stats in rules
1 parent b28632d commit 54ba68b

File tree

5 files changed

+171
-62
lines changed

5 files changed

+171
-62
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
authorized_key.json
22
stat.yaml
3+
issues_stat.yaml

tools/greenplum-to-pg-tests/cmd/extraxtSessions.go

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ var extractSessionsConfig struct {
3232
ydbConnectionString string
3333
limitRequests int
3434
rulesFile string
35+
writeRulesWithStat string
36+
sortRulesByCount bool
3537
printKnownIssues bool
3638
printQueryForKnownIssue bool
3739
filterReason string
@@ -53,6 +55,8 @@ func init() {
5355
extractSessionsCmd.PersistentFlags().StringVar(&extractSessionsConfig.ydbConnectionString, "ydb-connection", "grpc://localhost:2136/local", "Connection string to ydb server for check queries")
5456
extractSessionsCmd.PersistentFlags().IntVar(&extractSessionsConfig.limitRequests, "requests-limit", 100, "Limit number of parse requests, 0 mean unlimited")
5557
extractSessionsCmd.PersistentFlags().StringVar(&extractSessionsConfig.rulesFile, "rules-file", "issues.yaml", "Rules for detect issue. Set empty for skip read rules.")
58+
extractSessionsCmd.PersistentFlags().StringVar(&extractSessionsConfig.writeRulesWithStat, "write-updated-rules", "", "Write rules with updated stats, may be same or other file as for rules-file")
59+
extractSessionsCmd.PersistentFlags().BoolVar(&extractSessionsConfig.sortRulesByCount, "sort-updates-rules-by-count", true, "")
5660
extractSessionsCmd.PersistentFlags().BoolVar(&extractSessionsConfig.printKnownIssues, "print-known-issues", false, "Print known issues instead of unknown")
5761
extractSessionsCmd.PersistentFlags().BoolVar(&extractSessionsConfig.printQueryForKnownIssue, "print-query-for-known-issues", true, "Print query for known issues")
5862
extractSessionsCmd.PersistentFlags().IntVar(&extractSessionsConfig.errorLimit, "print-errors-limit", 0, "Limit of printed errors. 0 mean infinite")
@@ -105,8 +109,15 @@ var extractSessionsCmd = &cobra.Command{
105109
sessions := readSessions()
106110

107111
log.Println("Start check queries")
108-
checkQueries(rules, schema, db, sessions)
112+
var stats SessionStats
113+
checkQueries(rules, &stats, schema, db, sessions)
109114

115+
if extractSessionsConfig.writeRulesWithStat != "" {
116+
rules.UpdateFromStats(stats, extractSessionsConfig.sortRulesByCount)
117+
if err := rules.WriteToFile(extractSessionsConfig.writeRulesWithStat); err != nil {
118+
log.Printf("Failed to update rules stat: %v", err)
119+
}
120+
}
110121
},
111122
}
112123

@@ -204,7 +215,7 @@ readLoop:
204215
return res
205216
}
206217

207-
func checkQueries(rules Rules, pgSchema *internal.PgSchema, db *ydb.Driver, sessions []internal.Session) {
218+
func checkQueries(rules Rules, stats *SessionStats, pgSchema *internal.PgSchema, db *ydb.Driver, sessions []internal.Session) {
208219
reasonFilter := regexp.MustCompile(extractSessionsConfig.filterReason)
209220
checked := map[string]bool{}
210221

@@ -222,7 +233,6 @@ func checkQueries(rules Rules, pgSchema *internal.PgSchema, db *ydb.Driver, sess
222233

223234
queryIndex := 0
224235

225-
var stats SessionStats
226236
for _, session := range sessions {
227237
for _, transaction := range session.Transactions {
228238
for _, pgQuery := range transaction.Queries {
@@ -235,7 +245,7 @@ func checkQueries(rules Rules, pgSchema *internal.PgSchema, db *ydb.Driver, sess
235245
}
236246
checked[pgQuery.Text] = true
237247

238-
reason, checkResult := checkQuery(&stats, rules, db, pgQuery.Text)
248+
reason, checkResult := checkQuery(stats, rules, db, pgQuery.Text)
239249
if !reasonFilter.MatchString(reason) {
240250
continue
241251
}
@@ -297,7 +307,7 @@ func checkQuery(stat *SessionStats, rules Rules, db *ydb.Driver, queryText strin
297307
}
298308

299309
if err == nil {
300-
stat.CountOK()
310+
stat.CountASOK(queryText)
301311
return "", checkResultOK
302312
}
303313

@@ -307,13 +317,13 @@ func checkQuery(stat *SessionStats, rules Rules, db *ydb.Driver, queryText strin
307317
issues := internal.ExtractIssues(err)
308318

309319
if reason = rules.FindKnownIssue(queryText, issues); reason != "" {
310-
stat.CountKnown(reason, queryText)
320+
stat.CountAsKnown(reason, queryText)
311321
return reason, checkResultErrKnown
312322
}
313323

314324
reason = fmt.Sprintf("%v (%v): %#v", ydbErr.Name(), ydbErr.Code(), issues)
315325

316-
stat.CountUnknown(issues, queryText)
326+
stat.CountAsUnknown(issues, queryText)
317327
return reason, checkResultErrUnknown
318328
}
319329

@@ -348,11 +358,15 @@ type SessionStats struct {
348358
UnknownProblems map[internal.YdbIssue]*CounterWithExample[internal.YdbIssue]
349359
}
350360

351-
func (s *SessionStats) CountOK() {
361+
func (s *SessionStats) GetOkPercent() float64 {
362+
return float64(s.OkCount) / float64(s.TotalCount) * 100
363+
}
364+
365+
func (s *SessionStats) CountASOK(query string) {
352366
s.OkCount++
353367
}
354368

355-
func (s *SessionStats) CountKnown(ruleName string, query string) {
369+
func (s *SessionStats) CountAsKnown(ruleName string, query string) {
356370
s.TotalCount++
357371
if s.MatchToRules == nil {
358372
s.MatchToRules = make(map[string]*CounterWithExample[string])
@@ -371,7 +385,7 @@ func (s *SessionStats) CountKnown(ruleName string, query string) {
371385
stat.Count++
372386
}
373387

374-
func (s *SessionStats) CountUnknown(issues []internal.YdbIssue, query string) {
388+
func (s *SessionStats) CountAsUnknown(issues []internal.YdbIssue, query string) {
375389
s.TotalCount++
376390
if s.UnknownProblems == nil {
377391
s.UnknownProblems = make(map[internal.YdbIssue]*CounterWithExample[internal.YdbIssue])
@@ -456,7 +470,7 @@ func (s *SessionStats) SaveToFile(path string) error {
456470

457471
statFile.TotalCount = s.TotalCount
458472
statFile.OkCount = s.OkCount
459-
statFile.OkPercent = float64(s.OkCount) / float64(s.TotalCount) * 100
473+
statFile.OkPercent = s.GetOkPercent()
460474
statFile.UnknownIssues = s.GetTopUnknown(math.MaxInt)
461475
statFile.KnownIssues = s.GetTopKnown(math.MaxInt)
462476

tools/greenplum-to-pg-tests/cmd/issue_rules.go

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
package cmd
22

33
import (
4+
"bytes"
45
"errors"
56
"fmt"
7+
"math"
68
"os"
79
"regexp"
10+
"slices"
811

912
"gopkg.in/yaml.v3"
1013

1114
"github.com/ydb-platform/postgres-compatibility-tests/tools/greenplum-to-pg-tests/internal"
1215
)
1316

1417
type Rules struct {
15-
StatTotalCount int `yaml:"stat_total_count"`
16-
Issues []PgIssueRules
18+
TotalStat struct {
19+
TotalCount int `yaml:"total_checked_queries,omitempty"`
20+
TotalOk int `yaml:"total_ok,omitempty"`
21+
OkPercent float64 `yaml:"ok_percent,omitempty"`
22+
} `yaml:"stat"`
23+
24+
Issues []PgIssueRules
1725
}
1826

1927
func (r *Rules) LoadFromFile(path string) error {
@@ -51,6 +59,21 @@ func (r *Rules) LoadFromFile(path string) error {
5159
return nil
5260
}
5361

62+
func (r *Rules) WriteToFile(path string) error {
63+
buf := &bytes.Buffer{}
64+
encoder := yaml.NewEncoder(buf)
65+
err := encoder.Encode(r)
66+
if err != nil {
67+
return fmt.Errorf("failed to encode rules: %w", err)
68+
}
69+
70+
err = os.WriteFile(path, buf.Bytes(), 0666)
71+
if err != nil {
72+
return fmt.Errorf("failed write file to update rules")
73+
}
74+
return nil
75+
}
76+
5477
func (r *Rules) FindKnownIssue(queryText string, ydbIssues []internal.YdbIssue) string {
5578
for _, ydbIssue := range ydbIssues {
5679
for _, item := range r.Issues {
@@ -63,15 +86,36 @@ func (r *Rules) FindKnownIssue(queryText string, ydbIssues []internal.YdbIssue)
6386
return ""
6487
}
6588

89+
func (r *Rules) UpdateFromStats(stats SessionStats, sortByCount bool) {
90+
r.TotalStat.TotalCount = stats.TotalCount
91+
r.TotalStat.TotalOk = stats.OkCount
92+
r.TotalStat.OkPercent = math.Round(stats.GetOkPercent()*100) / 100
93+
94+
okStats := stats.GetTopKnown(math.MaxInt)
95+
for _, stat := range okStats {
96+
for issueIndex, issue := range r.Issues {
97+
if issue.Name == stat.ID {
98+
r.Issues[issueIndex].Count = stat.Count
99+
}
100+
}
101+
}
102+
103+
if sortByCount {
104+
slices.SortFunc(r.Issues, func(a, b PgIssueRules) int {
105+
return b.Count - a.Count
106+
})
107+
}
108+
}
109+
66110
type PgIssueRules struct {
67111
Name string `yaml:"name"`
68-
Tag OneOrSliceString `yaml:"tag"`
69-
IssueLink string `yaml:"issue_link"`
70-
IssueRegexp OneOrSliceString `yaml:"issue_regexp"`
71-
QueryRegexp OneOrSliceString `yaml:"query_regexp"`
72-
Example string `yaml:"example"`
73-
Comment string `yaml:"comment"`
74112
Count int `yaml:"count"`
113+
Tag OneOrSliceString `yaml:"tag,omitempty"`
114+
IssueLink string `yaml:"issue_link,omitempty"`
115+
IssueRegexp OneOrSliceString `yaml:"issue_regexp,omitempty"`
116+
QueryRegexp OneOrSliceString `yaml:"query_regexp,omitempty"`
117+
Example string `yaml:"example,omitempty"`
118+
Comment string `yaml:"comment,omitempty"`
75119

76120
issuesRegexpCompiled []*regexp.Regexp
77121
queryRegexpCompiled []*regexp.Regexp
Lines changed: 90 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,97 @@
1+
stat:
2+
total_checked_queries: 23
3+
total_ok: 15
4+
ok_percent: 65.21739130434783
15
issues:
26
- name: Skip unknown tables
3-
issue_regexp: Cannot find table
4-
tag: skip
5-
- name: Rollback in query service (OK)
6-
issue_regexp: ROLLBACK not supported inside YDB query
7-
tag: skip
8-
- name: Commit in query service (OK)
9-
issue_regexp: COMMIT not supported inside YDB query
10-
tag: skip
7+
count: 11
8+
tag:
9+
- skip
10+
issue_regexp:
11+
- Cannot find table
12+
- name: drop external table
13+
count: 2
14+
tag:
15+
- greenplum
16+
issue_regexp:
17+
- 'ERROR: syntax error at or near "external"'
18+
query_regexp:
19+
- drop external table
1120
- name: Create table as
12-
issue_regexp: "^RawStmt: alternative is not implemented yet : 277$"
13-
tag: YQLParser
21+
count: 2
22+
tag:
23+
- YQLParser
24+
issue_regexp:
25+
- '^RawStmt: alternative is not implemented yet : 277$'
1426
example: create table t as select 1
15-
- name: Unimplemented Discard
16-
issue_regexp: "^RawStmt: alternative is not implemented yet : 282$"
17-
example: DISCARD ALL
18-
- name: Generator functions in SELECT
19-
issue_regexp: "^Generator functions are not allowed in: SELECT$"
20-
example: SELECT UNNEST($1)
21-
- name: Call function from own schema
22-
issue_regexp: "FuncCall: expected pg_catalog, but got: "
23-
- name: Partial request
24-
issue_regexp: "ERROR: syntax error at end of input"
25-
tag: skip
26-
- name: "YQL: Expected type cast node as is_local arg, but got node with tag"
27-
issue_regexp: "Expected type cast node as is_local arg, but got node with tag"
28-
- name: "PG: unrecognized configuration parameter"
29-
issue_regexp: unrecognized configuration parameter
30-
- name: "PG: InsertStmt: not supported onConflictClause"
31-
issue_regexp: "InsertStmt: not supported onConflictClause"
32-
- name: "PG: RangeFunction: unsupported coldeflist"
33-
issue_regexp: "RangeFunction: unsupported coldeflist"
34-
- name: "PG: Support NOT DISTINCT"
35-
issue_regexp: "A_Expr_Kind unsupported value: 4"
36-
- name: drop external table
37-
tag: greenplum
38-
issue_regexp: "ERROR: syntax error at or near \"external\""
39-
query_regexp: "drop external table"
27+
- name: Distributed randomly
28+
count: 2
29+
tag:
30+
- greenplum
31+
issue_regexp:
32+
- 'ERROR: syntax error at or near "RANDOMLY"'
33+
- 'ERROR: syntax error at or near "DISTRIBUTED"'
34+
query_regexp:
35+
- (?i)DISTRIBUTED\s+RANDOMLY
4036
- name: Stored procedures
41-
issue_regexp: "No such proc: "
42-
- name: "Distributed randomly"
43-
tag: greenplum
37+
count: 2
38+
issue_regexp:
39+
- 'No such proc: '
40+
- name: Rollback in query service (OK)
41+
count: 1
42+
tag:
43+
- skip
44+
issue_regexp:
45+
- ROLLBACK not supported inside YDB query
46+
- name: Commit in query service (OK)
47+
count: 1
48+
tag:
49+
- skip
4450
issue_regexp:
45-
- "ERROR: syntax error at or near \"RANDOMLY\""
46-
- "ERROR: syntax error at or near \"DISTRIBUTED\""
47-
query_regexp: "(?i)DISTRIBUTED\\s+RANDOMLY"
51+
- COMMIT not supported inside YDB query
4852
- name: Not supported set greenplum var
49-
issue_regexp: "VariableSetStmt, not supported name: gp_"
50-
query_regexp: ^SET gp_
53+
count: 1
54+
issue_regexp:
55+
- 'VariableSetStmt, not supported name: gp_'
56+
query_regexp:
57+
- ^SET gp_
58+
- name: Partial request
59+
count: 1
60+
tag:
61+
- skip
62+
issue_regexp:
63+
- 'ERROR: syntax error at end of input'
64+
- name: 'YQL: Expected type cast node as is_local arg, but got node with tag'
65+
count: 0
66+
issue_regexp:
67+
- Expected type cast node as is_local arg, but got node with tag
68+
- name: 'PG: InsertStmt: not supported onConflictClause'
69+
count: 0
70+
issue_regexp:
71+
- 'InsertStmt: not supported onConflictClause'
72+
- name: 'PG: RangeFunction: unsupported coldeflist'
73+
count: 0
74+
issue_regexp:
75+
- 'RangeFunction: unsupported coldeflist'
76+
- name: 'PG: Support NOT DISTINCT'
77+
count: 0
78+
issue_regexp:
79+
- 'A_Expr_Kind unsupported value: 4'
80+
- name: 'PG: unrecognized configuration parameter'
81+
count: 0
82+
issue_regexp:
83+
- unrecognized configuration parameter
84+
- name: Call function from own schema
85+
count: 0
86+
issue_regexp:
87+
- 'FuncCall: expected pg_catalog, but got: '
88+
- name: Generator functions in SELECT
89+
count: 0
90+
issue_regexp:
91+
- '^Generator functions are not allowed in: SELECT$'
92+
example: SELECT UNNEST($1)
93+
- name: Unimplemented Discard
94+
count: 0
95+
issue_regexp:
96+
- '^RawStmt: alternative is not implemented yet : 282$'
97+
example: DISCARD ALL

untitled/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module untitled
2+
3+
go 1.22

0 commit comments

Comments
 (0)